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.CtxI != nullptr)
580 return ConstantFoldFPInstOperands(Opcode, LHS: CLHS, RHS: CRHS, DL: Q.DL, I: Q.CtxI);
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.CtxI, 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, CtxI: Q.CtxI, 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, CtxI: Q.CtxI, 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, CtxI: Q.CtxI, 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, CtxI: Q.CtxI) &&
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, CtxI: Q.CtxI,
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 // zext(X) & sext(X) --> zext(X)
2139 // sext(X) & zext(X) --> zext(X)
2140 {
2141 Value *X = nullptr;
2142 if (match(V: Op0, P: m_ZExt(Op: m_Value(V&: X))) && match(V: Op1, P: m_SExt(Op: m_Specific(V: X))))
2143 return Op0;
2144 if (match(V: Op1, P: m_ZExt(Op: m_Value(V&: X))) && match(V: Op0, P: m_SExt(Op: m_Specific(V: X))))
2145 return Op1;
2146 }
2147
2148 // Try some generic simplifications for associative operations.
2149 if (Value *V =
2150 simplifyAssociativeBinOp(Opcode: Instruction::And, LHS: Op0, RHS: Op1, Q, MaxRecurse))
2151 return V;
2152
2153 // And distributes over Or. Try some generic simplifications based on this.
2154 if (Value *V = expandCommutativeBinOp(Opcode: Instruction::And, L: Op0, R: Op1,
2155 OpcodeToExpand: Instruction::Or, Q, MaxRecurse))
2156 return V;
2157
2158 // And distributes over Xor. Try some generic simplifications based on this.
2159 if (Value *V = expandCommutativeBinOp(Opcode: Instruction::And, L: Op0, R: Op1,
2160 OpcodeToExpand: Instruction::Xor, Q, MaxRecurse))
2161 return V;
2162
2163 if (isa<SelectInst>(Val: Op0) || isa<SelectInst>(Val: Op1)) {
2164 if (Op0->getType()->isIntOrIntVectorTy(BitWidth: 1)) {
2165 // A & (A && B) -> A && B
2166 if (match(V: Op1, P: m_Select(C: m_Specific(V: Op0), L: m_Value(), R: m_Zero())))
2167 return Op1;
2168 else if (match(V: Op0, P: m_Select(C: m_Specific(V: Op1), L: m_Value(), R: m_Zero())))
2169 return Op0;
2170 }
2171 // If the operation is with the result of a select instruction, check
2172 // whether operating on either branch of the select always yields the same
2173 // value.
2174 if (Value *V =
2175 threadBinOpOverSelect(Opcode: Instruction::And, LHS: Op0, RHS: Op1, Q, MaxRecurse))
2176 return V;
2177 }
2178
2179 // If the operation is with the result of a phi instruction, check whether
2180 // operating on all incoming values of the phi always yields the same value.
2181 if (isa<PHINode>(Val: Op0) || isa<PHINode>(Val: Op1))
2182 if (Value *V =
2183 threadBinOpOverPHI(Opcode: Instruction::And, LHS: Op0, RHS: Op1, Q, MaxRecurse))
2184 return V;
2185
2186 // Assuming the effective width of Y is not larger than A, i.e. all bits
2187 // from X and Y are disjoint in (X << A) | Y,
2188 // if the mask of this AND op covers all bits of X or Y, while it covers
2189 // no bits from the other, we can bypass this AND op. E.g.,
2190 // ((X << A) | Y) & Mask -> Y,
2191 // if Mask = ((1 << effective_width_of(Y)) - 1)
2192 // ((X << A) | Y) & Mask -> X << A,
2193 // if Mask = ((1 << effective_width_of(X)) - 1) << A
2194 // SimplifyDemandedBits in InstCombine can optimize the general case.
2195 // This pattern aims to help other passes for a common case.
2196 Value *XShifted;
2197 if (Q.IIQ.UseInstrInfo && match(V: Op1, P: m_APInt(Res&: Mask)) &&
2198 match(V: Op0, P: m_c_Or(L: m_CombineAnd(Ps: m_NUWShl(L: m_Value(V&: X), R: m_APInt(Res&: ShAmt)),
2199 Ps: m_Value(V&: XShifted)),
2200 R: m_Value(V&: Y)))) {
2201 const unsigned Width = Op0->getType()->getScalarSizeInBits();
2202 const unsigned ShftCnt = ShAmt->getLimitedValue(Limit: Width);
2203 const KnownBits YKnown = computeKnownBits(V: Y, Q);
2204 const unsigned EffWidthY = YKnown.countMaxActiveBits();
2205 if (EffWidthY <= ShftCnt) {
2206 const KnownBits XKnown = computeKnownBits(V: X, Q);
2207 const unsigned EffWidthX = XKnown.countMaxActiveBits();
2208 const APInt EffBitsY = APInt::getLowBitsSet(numBits: Width, loBitsSet: EffWidthY);
2209 const APInt EffBitsX = APInt::getLowBitsSet(numBits: Width, loBitsSet: EffWidthX) << ShftCnt;
2210 // If the mask is extracting all bits from X or Y as is, we can skip
2211 // this AND op.
2212 if (EffBitsY.isSubsetOf(RHS: *Mask) && !EffBitsX.intersects(RHS: *Mask))
2213 return Y;
2214 if (EffBitsX.isSubsetOf(RHS: *Mask) && !EffBitsY.intersects(RHS: *Mask))
2215 return XShifted;
2216 }
2217 }
2218
2219 // ((X | Y) ^ X ) & ((X | Y) ^ Y) --> 0
2220 // ((X | Y) ^ Y ) & ((X | Y) ^ X) --> 0
2221 BinaryOperator *Or;
2222 if (match(V: Op0, P: m_c_Xor(L: m_Value(V&: X),
2223 R: m_CombineAnd(Ps: m_BinOp(I&: Or),
2224 Ps: m_c_Or(L: m_Deferred(V: X), R: m_Value(V&: Y))))) &&
2225 match(V: Op1, P: m_c_Xor(L: m_Specific(V: Or), R: m_Specific(V: Y))))
2226 return Constant::getNullValue(Ty: Op0->getType());
2227
2228 const APInt *C1;
2229 Value *A;
2230 // (A ^ C) & (A ^ ~C) -> 0
2231 if (match(V: Op0, P: m_Xor(L: m_Value(V&: A), R: m_APInt(Res&: C1))) &&
2232 match(V: Op1, P: m_Xor(L: m_Specific(V: A), R: m_SpecificInt(V: ~*C1))))
2233 return Constant::getNullValue(Ty: Op0->getType());
2234
2235 if (Op0->getType()->isIntOrIntVectorTy(BitWidth: 1)) {
2236 if (std::optional<bool> Implied = isImpliedCondition(LHS: Op0, RHS: Op1, DL: Q.DL)) {
2237 // If Op0 is true implies Op1 is true, then Op0 is a subset of Op1.
2238 if (*Implied == true)
2239 return Op0;
2240 // If Op0 is true implies Op1 is false, then they are not true together.
2241 if (*Implied == false)
2242 return ConstantInt::getFalse(Ty: Op0->getType());
2243 }
2244 if (std::optional<bool> Implied = isImpliedCondition(LHS: Op1, RHS: Op0, DL: Q.DL)) {
2245 // If Op1 is true implies Op0 is true, then Op1 is a subset of Op0.
2246 if (*Implied)
2247 return Op1;
2248 // If Op1 is true implies Op0 is false, then they are not true together.
2249 if (!*Implied)
2250 return ConstantInt::getFalse(Ty: Op1->getType());
2251 }
2252 }
2253
2254 if (Value *V = simplifyByDomEq(Opcode: Instruction::And, Op0, Op1, Q, MaxRecurse))
2255 return V;
2256
2257 return nullptr;
2258}
2259
2260Value *llvm::simplifyAndInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
2261 return ::simplifyAndInst(Op0, Op1, Q, MaxRecurse: RecursionLimit);
2262}
2263
2264// TODO: Many of these folds could use LogicalAnd/LogicalOr.
2265static Value *simplifyOrLogic(Value *X, Value *Y) {
2266 assert(X->getType() == Y->getType() && "Expected same type for 'or' ops");
2267 Type *Ty = X->getType();
2268
2269 // X | ~X --> -1
2270 if (match(V: Y, P: m_Not(V: m_Specific(V: X))))
2271 return ConstantInt::getAllOnesValue(Ty);
2272
2273 // X | ~(X & ?) = -1
2274 if (match(V: Y, P: m_Not(V: m_c_And(L: m_Specific(V: X), R: m_Value()))))
2275 return ConstantInt::getAllOnesValue(Ty);
2276
2277 // X | (X & ?) --> X
2278 if (match(V: Y, P: m_c_And(L: m_Specific(V: X), R: m_Value())))
2279 return X;
2280
2281 Value *A, *B;
2282
2283 // (A ^ B) | (A | B) --> A | B
2284 // (A ^ B) | (B | A) --> B | A
2285 if (match(V: X, P: m_Xor(L: m_Value(V&: A), R: m_Value(V&: B))) &&
2286 match(V: Y, P: m_c_Or(L: m_Specific(V: A), R: m_Specific(V: B))))
2287 return Y;
2288
2289 // ~(A ^ B) | (A | B) --> -1
2290 // ~(A ^ B) | (B | A) --> -1
2291 if (match(V: X, P: m_Not(V: m_Xor(L: m_Value(V&: A), R: m_Value(V&: B)))) &&
2292 match(V: Y, P: m_c_Or(L: m_Specific(V: A), R: m_Specific(V: B))))
2293 return ConstantInt::getAllOnesValue(Ty);
2294
2295 // (A & ~B) | (A ^ B) --> A ^ B
2296 // (~B & A) | (A ^ B) --> A ^ B
2297 // (A & ~B) | (B ^ A) --> B ^ A
2298 // (~B & A) | (B ^ A) --> B ^ A
2299 if (match(V: X, P: m_c_And(L: m_Value(V&: A), R: m_Not(V: m_Value(V&: B)))) &&
2300 match(V: Y, P: m_c_Xor(L: m_Specific(V: A), R: m_Specific(V: B))))
2301 return Y;
2302
2303 // (~A ^ B) | (A & B) --> ~A ^ B
2304 // (B ^ ~A) | (A & B) --> B ^ ~A
2305 // (~A ^ B) | (B & A) --> ~A ^ B
2306 // (B ^ ~A) | (B & A) --> B ^ ~A
2307 if (match(V: X, P: m_c_Xor(L: m_Not(V: m_Value(V&: A)), R: m_Value(V&: B))) &&
2308 match(V: Y, P: m_c_And(L: m_Specific(V: A), R: m_Specific(V: B))))
2309 return X;
2310
2311 // (~A | B) | (A ^ B) --> -1
2312 // (~A | B) | (B ^ A) --> -1
2313 // (B | ~A) | (A ^ B) --> -1
2314 // (B | ~A) | (B ^ A) --> -1
2315 if (match(V: X, P: m_c_Or(L: m_Not(V: m_Value(V&: A)), R: m_Value(V&: B))) &&
2316 match(V: Y, P: m_c_Xor(L: m_Specific(V: A), R: m_Specific(V: B))))
2317 return ConstantInt::getAllOnesValue(Ty);
2318
2319 // (~A & B) | ~(A | B) --> ~A
2320 // (~A & B) | ~(B | A) --> ~A
2321 // (B & ~A) | ~(A | B) --> ~A
2322 // (B & ~A) | ~(B | A) --> ~A
2323 Value *NotA;
2324 if (match(V: X, P: m_c_And(L: m_CombineAnd(Ps: m_Value(V&: NotA), Ps: m_Not(V: m_Value(V&: A))),
2325 R: m_Value(V&: B))) &&
2326 match(V: Y, P: m_Not(V: m_c_Or(L: m_Specific(V: A), R: m_Specific(V: B)))))
2327 return NotA;
2328 // The same is true of Logical And
2329 // TODO: This could share the logic of the version above if there was a
2330 // version of LogicalAnd that allowed more than just i1 types.
2331 if (match(V: X, P: m_c_LogicalAnd(L: m_CombineAnd(Ps: m_Value(V&: NotA), Ps: m_Not(V: m_Value(V&: A))),
2332 R: m_Value(V&: B))) &&
2333 match(V: Y, P: m_Not(V: m_c_LogicalOr(L: m_Specific(V: A), R: m_Specific(V: B)))))
2334 return NotA;
2335
2336 // ~(A ^ B) | (A & B) --> ~(A ^ B)
2337 // ~(A ^ B) | (B & A) --> ~(A ^ B)
2338 Value *NotAB;
2339 if (match(V: X, P: m_CombineAnd(Ps: m_Not(V: m_Xor(L: m_Value(V&: A), R: m_Value(V&: B))),
2340 Ps: m_Value(V&: NotAB))) &&
2341 match(V: Y, P: m_c_And(L: m_Specific(V: A), R: m_Specific(V: B))))
2342 return NotAB;
2343
2344 // ~(A & B) | (A ^ B) --> ~(A & B)
2345 // ~(A & B) | (B ^ A) --> ~(A & B)
2346 if (match(V: X, P: m_CombineAnd(Ps: m_Not(V: m_And(L: m_Value(V&: A), R: m_Value(V&: B))),
2347 Ps: m_Value(V&: NotAB))) &&
2348 match(V: Y, P: m_c_Xor(L: m_Specific(V: A), R: m_Specific(V: B))))
2349 return NotAB;
2350
2351 return nullptr;
2352}
2353
2354/// Given operands for an Or, see if we can fold the result.
2355/// If not, this returns null.
2356static Value *simplifyOrInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
2357 unsigned MaxRecurse) {
2358 if (Constant *C = foldOrCommuteConstant(Opcode: Instruction::Or, Op0, Op1, Q))
2359 return C;
2360
2361 // X | poison -> poison
2362 if (isa<PoisonValue>(Val: Op1))
2363 return Op1;
2364
2365 // X | undef -> -1
2366 // X | -1 = -1
2367 // Do not return Op1 because it may contain undef elements if it's a vector.
2368 if (Q.isUndefValue(V: Op1) || match(V: Op1, P: m_AllOnes()))
2369 return Constant::getAllOnesValue(Ty: Op0->getType());
2370
2371 // X | X = X
2372 // X | 0 = X
2373 if (Op0 == Op1 || match(V: Op1, P: m_Zero()))
2374 return Op0;
2375
2376 if (Value *R = simplifyOrLogic(X: Op0, Y: Op1))
2377 return R;
2378 if (Value *R = simplifyOrLogic(X: Op1, Y: Op0))
2379 return R;
2380
2381 if (Value *V = simplifyLogicOfAddSub(Op0, Op1, Opcode: Instruction::Or))
2382 return V;
2383
2384 // Rotated -1 is still -1:
2385 // (-1 << X) | (-1 >> (C - X)) --> -1
2386 // (-1 >> X) | (-1 << (C - X)) --> -1
2387 // ...with C <= bitwidth (and commuted variants).
2388 Value *X, *Y;
2389 if ((match(V: Op0, P: m_Shl(L: m_AllOnes(), R: m_Value(V&: X))) &&
2390 match(V: Op1, P: m_LShr(L: m_AllOnes(), R: m_Value(V&: Y)))) ||
2391 (match(V: Op1, P: m_Shl(L: m_AllOnes(), R: m_Value(V&: X))) &&
2392 match(V: Op0, P: m_LShr(L: m_AllOnes(), R: m_Value(V&: Y))))) {
2393 const APInt *C;
2394 if ((match(V: X, P: m_Sub(L: m_APInt(Res&: C), R: m_Specific(V: Y))) ||
2395 match(V: Y, P: m_Sub(L: m_APInt(Res&: C), R: m_Specific(V: X)))) &&
2396 C->ule(RHS: X->getType()->getScalarSizeInBits())) {
2397 return ConstantInt::getAllOnesValue(Ty: X->getType());
2398 }
2399 }
2400
2401 // zext(X) | sext(X) --> sext(X)
2402 // sext(X) | zext(X) --> sext(X)
2403 {
2404 Value *X = nullptr;
2405 if (match(V: Op0, P: m_ZExt(Op: m_Value(V&: X))) && match(V: Op1, P: m_SExt(Op: m_Specific(V: X))))
2406 return Op1;
2407 if (match(V: Op1, P: m_ZExt(Op: m_Value(V&: X))) && match(V: Op0, P: m_SExt(Op: m_Specific(V: X))))
2408 return Op0;
2409 }
2410
2411 // A funnel shift (rotate) can be decomposed into simpler shifts. See if we
2412 // are mixing in another shift that is redundant with the funnel shift.
2413
2414 // (fshl X, ?, Y) | (shl X, Y) --> fshl X, ?, Y
2415 // (shl X, Y) | (fshl X, ?, Y) --> fshl X, ?, Y
2416 if (match(V: Op0,
2417 P: m_Intrinsic<Intrinsic::fshl>(Ops: m_Value(V&: X), Ops: m_Value(), Ops: m_Value(V&: Y))) &&
2418 match(V: Op1, P: m_Shl(L: m_Specific(V: X), R: m_Specific(V: Y))))
2419 return Op0;
2420 if (match(V: Op1,
2421 P: m_Intrinsic<Intrinsic::fshl>(Ops: m_Value(V&: X), Ops: m_Value(), Ops: m_Value(V&: Y))) &&
2422 match(V: Op0, P: m_Shl(L: m_Specific(V: X), R: m_Specific(V: Y))))
2423 return Op1;
2424
2425 // (fshr ?, X, Y) | (lshr X, Y) --> fshr ?, X, Y
2426 // (lshr X, Y) | (fshr ?, X, Y) --> fshr ?, X, Y
2427 if (match(V: Op0,
2428 P: m_Intrinsic<Intrinsic::fshr>(Ops: m_Value(), Ops: m_Value(V&: X), Ops: m_Value(V&: Y))) &&
2429 match(V: Op1, P: m_LShr(L: m_Specific(V: X), R: m_Specific(V: Y))))
2430 return Op0;
2431 if (match(V: Op1,
2432 P: m_Intrinsic<Intrinsic::fshr>(Ops: m_Value(), Ops: m_Value(V&: X), Ops: m_Value(V&: Y))) &&
2433 match(V: Op0, P: m_LShr(L: m_Specific(V: X), R: m_Specific(V: Y))))
2434 return Op1;
2435
2436 if (Value *V =
2437 simplifyAndOrWithICmpEq(Opcode: Instruction::Or, Op0, Op1, Q, MaxRecurse))
2438 return V;
2439 if (Value *V =
2440 simplifyAndOrWithICmpEq(Opcode: Instruction::Or, Op0: Op1, Op1: Op0, Q, MaxRecurse))
2441 return V;
2442
2443 if (Value *V = simplifyAndOrOfCmps(Q, Op0, Op1, IsAnd: false))
2444 return V;
2445
2446 // If we have a multiplication overflow check that is being 'and'ed with a
2447 // check that one of the multipliers is not zero, we can omit the 'and', and
2448 // only keep the overflow check.
2449 if (isCheckForZeroAndMulWithOverflow(Op0, Op1, IsAnd: false))
2450 return Op1;
2451 if (isCheckForZeroAndMulWithOverflow(Op0: Op1, Op1: Op0, IsAnd: false))
2452 return Op0;
2453
2454 // Try some generic simplifications for associative operations.
2455 if (Value *V =
2456 simplifyAssociativeBinOp(Opcode: Instruction::Or, LHS: Op0, RHS: Op1, Q, MaxRecurse))
2457 return V;
2458
2459 // Or distributes over And. Try some generic simplifications based on this.
2460 if (Value *V = expandCommutativeBinOp(Opcode: Instruction::Or, L: Op0, R: Op1,
2461 OpcodeToExpand: Instruction::And, Q, MaxRecurse))
2462 return V;
2463
2464 if (isa<SelectInst>(Val: Op0) || isa<SelectInst>(Val: Op1)) {
2465 if (Op0->getType()->isIntOrIntVectorTy(BitWidth: 1)) {
2466 // A | (A || B) -> A || B
2467 if (match(V: Op1, P: m_Select(C: m_Specific(V: Op0), L: m_One(), R: m_Value())))
2468 return Op1;
2469 else if (match(V: Op0, P: m_Select(C: m_Specific(V: Op1), L: m_One(), R: m_Value())))
2470 return Op0;
2471 }
2472 // If the operation is with the result of a select instruction, check
2473 // whether operating on either branch of the select always yields the same
2474 // value.
2475 if (Value *V =
2476 threadBinOpOverSelect(Opcode: Instruction::Or, LHS: Op0, RHS: Op1, Q, MaxRecurse))
2477 return V;
2478 }
2479
2480 // (A & C1)|(B & C2)
2481 Value *A, *B;
2482 const APInt *C1, *C2;
2483 if (match(V: Op0, P: m_And(L: m_Value(V&: A), R: m_APInt(Res&: C1))) &&
2484 match(V: Op1, P: m_And(L: m_Value(V&: B), R: m_APInt(Res&: C2)))) {
2485 if (*C1 == ~*C2) {
2486 // (A & C1)|(B & C2)
2487 // If we have: ((V + N) & C1) | (V & C2)
2488 // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0
2489 // replace with V+N.
2490 Value *N;
2491 if (C2->isMask() && // C2 == 0+1+
2492 match(V: A, P: m_c_Add(L: m_Specific(V: B), R: m_Value(V&: N)))) {
2493 // Add commutes, try both ways.
2494 if (MaskedValueIsZero(V: N, Mask: *C2, SQ: Q))
2495 return A;
2496 }
2497 // Or commutes, try both ways.
2498 if (C1->isMask() && match(V: B, P: m_c_Add(L: m_Specific(V: A), R: m_Value(V&: N)))) {
2499 // Add commutes, try both ways.
2500 if (MaskedValueIsZero(V: N, Mask: *C1, SQ: Q))
2501 return B;
2502 }
2503 }
2504 }
2505
2506 // If the operation is with the result of a phi instruction, check whether
2507 // operating on all incoming values of the phi always yields the same value.
2508 if (isa<PHINode>(Val: Op0) || isa<PHINode>(Val: Op1))
2509 if (Value *V = threadBinOpOverPHI(Opcode: Instruction::Or, LHS: Op0, RHS: Op1, Q, MaxRecurse))
2510 return V;
2511
2512 // (A ^ C) | (A ^ ~C) -> -1, i.e. all bits set to one.
2513 if (match(V: Op0, P: m_Xor(L: m_Value(V&: A), R: m_APInt(Res&: C1))) &&
2514 match(V: Op1, P: m_Xor(L: m_Specific(V: A), R: m_SpecificInt(V: ~*C1))))
2515 return Constant::getAllOnesValue(Ty: Op0->getType());
2516
2517 if (Op0->getType()->isIntOrIntVectorTy(BitWidth: 1)) {
2518 if (std::optional<bool> Implied =
2519 isImpliedCondition(LHS: Op0, RHS: Op1, DL: Q.DL, LHSIsTrue: false)) {
2520 // If Op0 is false implies Op1 is false, then Op1 is a subset of Op0.
2521 if (*Implied == false)
2522 return Op0;
2523 // If Op0 is false implies Op1 is true, then at least one is always true.
2524 if (*Implied == true)
2525 return ConstantInt::getTrue(Ty: Op0->getType());
2526 }
2527 if (std::optional<bool> Implied =
2528 isImpliedCondition(LHS: Op1, RHS: Op0, DL: Q.DL, LHSIsTrue: false)) {
2529 // If Op1 is false implies Op0 is false, then Op0 is a subset of Op1.
2530 if (*Implied == false)
2531 return Op1;
2532 // If Op1 is false implies Op0 is true, then at least one is always true.
2533 if (*Implied == true)
2534 return ConstantInt::getTrue(Ty: Op1->getType());
2535 }
2536 }
2537
2538 if (Value *V = simplifyByDomEq(Opcode: Instruction::Or, Op0, Op1, Q, MaxRecurse))
2539 return V;
2540
2541 return nullptr;
2542}
2543
2544Value *llvm::simplifyOrInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
2545 return ::simplifyOrInst(Op0, Op1, Q, MaxRecurse: RecursionLimit);
2546}
2547
2548/// Given operands for a Xor, see if we can fold the result.
2549/// If not, this returns null.
2550static Value *simplifyXorInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
2551 unsigned MaxRecurse) {
2552 if (Constant *C = foldOrCommuteConstant(Opcode: Instruction::Xor, Op0, Op1, Q))
2553 return C;
2554
2555 // X ^ poison -> poison
2556 if (isa<PoisonValue>(Val: Op1))
2557 return Op1;
2558
2559 // A ^ undef -> undef
2560 if (Q.isUndefValue(V: Op1))
2561 return Op1;
2562
2563 // A ^ 0 = A
2564 if (match(V: Op1, P: m_Zero()))
2565 return Op0;
2566
2567 // A ^ A = 0
2568 if (Op0 == Op1)
2569 return Constant::getNullValue(Ty: Op0->getType());
2570
2571 // A ^ ~A = ~A ^ A = -1
2572 if (match(V: Op0, P: m_Not(V: m_Specific(V: Op1))) || match(V: Op1, P: m_Not(V: m_Specific(V: Op0))))
2573 return Constant::getAllOnesValue(Ty: Op0->getType());
2574
2575 auto foldAndOrNot = [](Value *X, Value *Y) -> Value * {
2576 Value *A, *B;
2577 // (~A & B) ^ (A | B) --> A -- There are 8 commuted variants.
2578 if (match(V: X, P: m_c_And(L: m_Not(V: m_Value(V&: A)), R: m_Value(V&: B))) &&
2579 match(V: Y, P: m_c_Or(L: m_Specific(V: A), R: m_Specific(V: B))))
2580 return A;
2581
2582 // (~A | B) ^ (A & B) --> ~A -- There are 8 commuted variants.
2583 // The 'not' op must contain a complete -1 operand (no undef elements for
2584 // vector) for the transform to be safe.
2585 Value *NotA;
2586 if (match(V: X, P: m_c_Or(L: m_CombineAnd(Ps: m_Not(V: m_Value(V&: A)), Ps: m_Value(V&: NotA)),
2587 R: m_Value(V&: B))) &&
2588 match(V: Y, P: m_c_And(L: m_Specific(V: A), R: m_Specific(V: B))))
2589 return NotA;
2590
2591 return nullptr;
2592 };
2593 if (Value *R = foldAndOrNot(Op0, Op1))
2594 return R;
2595 if (Value *R = foldAndOrNot(Op1, Op0))
2596 return R;
2597
2598 if (Value *V = simplifyLogicOfAddSub(Op0, Op1, Opcode: Instruction::Xor))
2599 return V;
2600
2601 // Try some generic simplifications for associative operations.
2602 if (Value *V =
2603 simplifyAssociativeBinOp(Opcode: Instruction::Xor, LHS: Op0, RHS: Op1, Q, MaxRecurse))
2604 return V;
2605
2606 // Threading Xor over selects and phi nodes is pointless, so don't bother.
2607 // Threading over the select in "A ^ select(cond, B, C)" means evaluating
2608 // "A^B" and "A^C" and seeing if they are equal; but they are equal if and
2609 // only if B and C are equal. If B and C are equal then (since we assume
2610 // that operands have already been simplified) "select(cond, B, C)" should
2611 // have been simplified to the common value of B and C already. Analysing
2612 // "A^B" and "A^C" thus gains nothing, but costs compile time. Similarly
2613 // for threading over phi nodes.
2614
2615 if (Value *V = simplifyByDomEq(Opcode: Instruction::Xor, Op0, Op1, Q, MaxRecurse))
2616 return V;
2617
2618 // (xor (sub nuw C_Mask, X), C_Mask) -> X
2619 {
2620 Value *X;
2621 if (match(V: Op0, P: m_NUWSub(L: m_Specific(V: Op1), R: m_Value(V&: X))) &&
2622 match(V: Op1, P: m_LowBitMask()))
2623 return X;
2624 }
2625
2626 return nullptr;
2627}
2628
2629Value *llvm::simplifyXorInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
2630 return ::simplifyXorInst(Op0, Op1, Q, MaxRecurse: RecursionLimit);
2631}
2632
2633static Type *getCompareTy(Value *Op) {
2634 return CmpInst::makeCmpResultType(opnd_type: Op->getType());
2635}
2636
2637/// Rummage around inside V looking for something equivalent to the comparison
2638/// "LHS Pred RHS". Return such a value if found, otherwise return null.
2639/// Helper function for analyzing max/min idioms.
2640static Value *extractEquivalentCondition(Value *V, CmpPredicate Pred,
2641 Value *LHS, Value *RHS) {
2642 SelectInst *SI = dyn_cast<SelectInst>(Val: V);
2643 if (!SI)
2644 return nullptr;
2645 CmpInst *Cmp = dyn_cast<CmpInst>(Val: SI->getCondition());
2646 if (!Cmp)
2647 return nullptr;
2648 Value *CmpLHS = Cmp->getOperand(i_nocapture: 0), *CmpRHS = Cmp->getOperand(i_nocapture: 1);
2649 if (Pred == Cmp->getPredicate() && LHS == CmpLHS && RHS == CmpRHS)
2650 return Cmp;
2651 if (Pred == CmpInst::getSwappedPredicate(pred: Cmp->getPredicate()) &&
2652 LHS == CmpRHS && RHS == CmpLHS)
2653 return Cmp;
2654 return nullptr;
2655}
2656
2657static bool isByValArg(const Value *V) {
2658 const Argument *A = dyn_cast<Argument>(Val: V);
2659 return A && A->hasByValAttr();
2660}
2661
2662static bool isDereferenceableArg(const Value *V) {
2663 const Argument *A = dyn_cast<Argument>(Val: V);
2664 return A && A->getType()->isPointerTy() && A->getDereferenceableBytes() > 0;
2665}
2666
2667/// Return true if the underlying object (storage) must be disjoint from
2668/// storage returned by any noalias return call.
2669static bool isAllocDisjoint(const Value *V) {
2670 // For allocas, we consider only static ones (dynamic
2671 // allocas might be transformed into calls to malloc not simultaneously
2672 // live with the compared-to allocation). For globals, we exclude symbols
2673 // that might be resolve lazily to symbols in another dynamically-loaded
2674 // library (and, thus, could be malloc'ed by the implementation).
2675 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Val: V))
2676 return AI->isStaticAlloca();
2677 if (const GlobalValue *GV = dyn_cast<GlobalValue>(Val: V))
2678 return (GV->hasLocalLinkage() || GV->hasHiddenVisibility() ||
2679 GV->hasProtectedVisibility() || GV->hasGlobalUnnamedAddr()) &&
2680 !GV->isThreadLocal();
2681 // Byval arguments point to storage accessible to the caller, which is
2682 // disjoint from the allocated storage returned by a noalias pointer.
2683 // TODO: possibly extend this to `dereferenceable(N)` arguments once the LLVM
2684 // allocator model and its interaction with `noalias` on return values is
2685 // clarified.
2686 return isByValArg(V);
2687}
2688
2689/// Return true if V1 and V2 are each the base of some distict storage region
2690/// [V, object_size(V)] which do not overlap. Note that zero sized regions
2691/// *are* possible, and that zero sized regions do not overlap with any other.
2692static bool haveNonOverlappingStorage(const Value *V1, const Value *V2) {
2693 // Global variables always exist, so they always exist during the lifetime
2694 // of each other and all allocas. Global variables themselves usually have
2695 // non-overlapping storage, but since their addresses are constants, the
2696 // case involving two globals does not reach here and is instead handled in
2697 // constant folding.
2698 //
2699 // Two different allocas usually have different addresses...
2700 //
2701 // However, if there's an @llvm.stackrestore dynamically in between two
2702 // allocas, they may have the same address. It's tempting to reduce the
2703 // scope of the problem by only looking at *static* allocas here. That would
2704 // cover the majority of allocas while significantly reducing the likelihood
2705 // of having an @llvm.stackrestore pop up in the middle. However, it's not
2706 // actually impossible for an @llvm.stackrestore to pop up in the middle of
2707 // an entry block. Also, if we have a block that's not attached to a
2708 // function, we can't tell if it's "static" under the current definition.
2709 // Theoretically, this problem could be fixed by creating a new kind of
2710 // instruction kind specifically for static allocas. Such a new instruction
2711 // could be required to be at the top of the entry block, thus preventing it
2712 // from being subject to a @llvm.stackrestore. Instcombine could even
2713 // convert regular allocas into these special allocas. It'd be nifty.
2714 // However, until then, this problem remains open.
2715 //
2716 // So, we'll assume that two non-empty allocas have different addresses
2717 // for now.
2718 //
2719 // Furthermore, an argument marked with the `dereferenceable(N)` attribute is
2720 // guaranteed to point to N loadable bytes. Such a pointer cannot be a
2721 // one-past-the-end pointer whose address happens to coincide with the start
2722 // of another object (e.g., an alloca), as loading from a one-past-the-end
2723 // address would be UB (thus, in contrast with the premise).
2724
2725 // Byval args are backed by storage that does not overlap with allocas,
2726 // globals, other byval args, or any dereferenceable argument.
2727 if (isByValArg(V: V1))
2728 return isa<AllocaInst>(Val: V2) || isa<GlobalVariable>(Val: V2) || isByValArg(V: V2) ||
2729 isDereferenceableArg(V: V2);
2730 if (isByValArg(V: V2))
2731 return isa<AllocaInst>(Val: V1) || isa<GlobalVariable>(Val: V1) || isByValArg(V: V1) ||
2732 isDereferenceableArg(V: V1);
2733
2734 if ((isDereferenceableArg(V: V1) && isa<AllocaInst>(Val: V2)) ||
2735 (isDereferenceableArg(V: V2) && isa<AllocaInst>(Val: V1)))
2736 return true;
2737
2738 return isa<AllocaInst>(Val: V1) &&
2739 (isa<AllocaInst>(Val: V2) || isa<GlobalVariable>(Val: V2));
2740}
2741
2742// A significant optimization not implemented here is assuming that alloca
2743// addresses are not equal to incoming argument values. They don't *alias*,
2744// as we say, but that doesn't mean they aren't equal, so we take a
2745// conservative approach.
2746//
2747// This is inspired in part by C++11 5.10p1:
2748// "Two pointers of the same type compare equal if and only if they are both
2749// null, both point to the same function, or both represent the same
2750// address."
2751//
2752// This is pretty permissive.
2753//
2754// It's also partly due to C11 6.5.9p6:
2755// "Two pointers compare equal if and only if both are null pointers, both are
2756// pointers to the same object (including a pointer to an object and a
2757// subobject at its beginning) or function, both are pointers to one past the
2758// last element of the same array object, or one is a pointer to one past the
2759// end of one array object and the other is a pointer to the start of a
2760// different array object that happens to immediately follow the first array
2761// object in the address space.)
2762//
2763// C11's version is more restrictive, however there's no reason why an argument
2764// couldn't be a one-past-the-end value for a stack object in the caller and be
2765// equal to the beginning of a stack object in the callee.
2766//
2767// If the C and C++ standards are ever made sufficiently restrictive in this
2768// area, it may be possible to update LLVM's semantics accordingly and reinstate
2769// this optimization.
2770static Constant *computePointerICmp(CmpPredicate Pred, Value *LHS, Value *RHS,
2771 const SimplifyQuery &Q) {
2772 assert(LHS->getType() == RHS->getType() && "Must have same types");
2773 const DataLayout &DL = Q.DL;
2774 const TargetLibraryInfo *TLI = Q.TLI;
2775
2776 // We fold equality and unsigned predicates on pointer comparisons, but forbid
2777 // signed predicates since a GEP with inbounds could cross the sign boundary.
2778 if (CmpInst::isSigned(Pred))
2779 return nullptr;
2780
2781 // We have to switch to a signed predicate to handle negative indices from
2782 // the base pointer.
2783 Pred = ICmpInst::getSignedPredicate(Pred);
2784
2785 // Strip off any constant offsets so that we can reason about them.
2786 // It's tempting to use getUnderlyingObject or even just stripInBoundsOffsets
2787 // here and compare base addresses like AliasAnalysis does, however there are
2788 // numerous hazards. AliasAnalysis and its utilities rely on special rules
2789 // governing loads and stores which don't apply to icmps. Also, AliasAnalysis
2790 // doesn't need to guarantee pointer inequality when it says NoAlias.
2791
2792 // Even if an non-inbounds GEP occurs along the path we can still optimize
2793 // equality comparisons concerning the result.
2794 bool AllowNonInbounds = ICmpInst::isEquality(P: Pred);
2795 unsigned IndexSize = DL.getIndexTypeSizeInBits(Ty: LHS->getType());
2796 APInt LHSOffset(IndexSize, 0), RHSOffset(IndexSize, 0);
2797 LHS = LHS->stripAndAccumulateConstantOffsets(DL, Offset&: LHSOffset, AllowNonInbounds);
2798 RHS = RHS->stripAndAccumulateConstantOffsets(DL, Offset&: RHSOffset, AllowNonInbounds);
2799
2800 // If LHS and RHS are related via constant offsets to the same base
2801 // value, we can replace it with an icmp which just compares the offsets.
2802 if (LHS == RHS)
2803 return ConstantInt::get(Ty: getCompareTy(Op: LHS),
2804 V: ICmpInst::compare(LHS: LHSOffset, RHS: RHSOffset, Pred));
2805
2806 // Various optimizations for (in)equality comparisons.
2807 if (ICmpInst::isEquality(P: Pred)) {
2808 // Different non-empty allocations that exist at the same time have
2809 // different addresses (if the program can tell). If the offsets are
2810 // within the bounds of their allocations (and not one-past-the-end,
2811 // so inbounds is not sufficient), and their allocations aren't the same,
2812 // the pointers are not equal.
2813 if (haveNonOverlappingStorage(V1: LHS, V2: RHS)) {
2814 // Size of object V, falling back to `dereferenceable(N)` attribute on an
2815 // argument when getObjectSize cannot determine a concrete size.
2816 auto GetKnownSize = [&](Value *V, uint64_t &Size) {
2817 bool CanBeNull;
2818 Size = V->getPointerDereferenceableBytes(DL, CanBeNull,
2819 /*CanBeFreed=*/nullptr);
2820 return Size != 0 && !CanBeNull;
2821 };
2822
2823 uint64_t LHSSize, RHSSize;
2824 if (GetKnownSize(LHS, LHSSize) && GetKnownSize(RHS, RHSSize)) {
2825 APInt Dist = LHSOffset - RHSOffset;
2826 if (Dist.isNonNegative() ? Dist.ult(RHS: LHSSize) : (-Dist).ult(RHS: RHSSize))
2827 return ConstantInt::get(Ty: getCompareTy(Op: LHS),
2828 V: !CmpInst::isTrueWhenEqual(predicate: Pred));
2829 }
2830 }
2831
2832 // If one side of the equality comparison must come from a noalias call
2833 // (meaning a system memory allocation function), and the other side must
2834 // come from a pointer that cannot overlap with dynamically-allocated
2835 // memory within the lifetime of the current function (allocas, byval
2836 // arguments, globals), then determine the comparison result here.
2837 SmallVector<const Value *, 8> LHSUObjs, RHSUObjs;
2838 getUnderlyingObjects(V: LHS, Objects&: LHSUObjs);
2839 getUnderlyingObjects(V: RHS, Objects&: RHSUObjs);
2840
2841 // Is the set of underlying objects all noalias calls?
2842 auto IsNoAliasCall = [](ArrayRef<const Value *> Objects) {
2843 return all_of(Range&: Objects, P: isNoAliasCall);
2844 };
2845
2846 // Is the set of underlying objects all things which must be disjoint from
2847 // noalias calls. We assume that indexing from such disjoint storage
2848 // into the heap is undefined, and thus offsets can be safely ignored.
2849 auto IsAllocDisjoint = [](ArrayRef<const Value *> Objects) {
2850 return all_of(Range&: Objects, P: ::isAllocDisjoint);
2851 };
2852
2853 if ((IsNoAliasCall(LHSUObjs) && IsAllocDisjoint(RHSUObjs)) ||
2854 (IsNoAliasCall(RHSUObjs) && IsAllocDisjoint(LHSUObjs)))
2855 return ConstantInt::get(Ty: getCompareTy(Op: LHS),
2856 V: !CmpInst::isTrueWhenEqual(predicate: Pred));
2857
2858 // Fold comparisons for non-escaping pointer even if the allocation call
2859 // cannot be elided. We cannot fold malloc comparison to null. Also, the
2860 // dynamic allocation call could be either of the operands. Note that
2861 // the other operand can not be based on the alloc - if it were, then
2862 // the cmp itself would be a capture.
2863 Value *MI = nullptr;
2864 if (isAllocLikeFn(V: LHS, TLI) && llvm::isKnownNonZero(V: RHS, Q))
2865 MI = LHS;
2866 else if (isAllocLikeFn(V: RHS, TLI) && llvm::isKnownNonZero(V: LHS, Q))
2867 MI = RHS;
2868 if (MI) {
2869 // FIXME: This is incorrect, see PR54002. While we can assume that the
2870 // allocation is at an address that makes the comparison false, this
2871 // requires that *all* comparisons to that address be false, which
2872 // InstSimplify cannot guarantee.
2873 struct CustomCaptureTracker : public CaptureTracker {
2874 bool Captured = false;
2875 void tooManyUses() override { Captured = true; }
2876 Action captured(const Use *U, UseCaptureInfo CI) override {
2877 // TODO(captures): Use UseCaptureInfo.
2878 if (auto *ICmp = dyn_cast<ICmpInst>(Val: U->getUser())) {
2879 // Comparison against value stored in global variable. Given the
2880 // pointer does not escape, its value cannot be guessed and stored
2881 // separately in a global variable.
2882 unsigned OtherIdx = 1 - U->getOperandNo();
2883 auto *LI = dyn_cast<LoadInst>(Val: ICmp->getOperand(i_nocapture: OtherIdx));
2884 if (LI && isa<GlobalVariable>(Val: LI->getPointerOperand()))
2885 return Continue;
2886 }
2887
2888 Captured = true;
2889 return Stop;
2890 }
2891 };
2892 CustomCaptureTracker Tracker;
2893 PointerMayBeCaptured(V: MI, Tracker: &Tracker);
2894 if (!Tracker.Captured)
2895 return ConstantInt::get(Ty: getCompareTy(Op: LHS),
2896 V: CmpInst::isFalseWhenEqual(predicate: Pred));
2897 }
2898 }
2899
2900 // Otherwise, fail.
2901 return nullptr;
2902}
2903
2904/// Fold an icmp when its operands have i1 scalar type.
2905static Value *simplifyICmpOfBools(CmpPredicate Pred, Value *LHS, Value *RHS,
2906 const SimplifyQuery &Q) {
2907 Type *ITy = getCompareTy(Op: LHS); // The return type.
2908 Type *OpTy = LHS->getType(); // The operand type.
2909 if (!OpTy->isIntOrIntVectorTy(BitWidth: 1))
2910 return nullptr;
2911
2912 // A boolean compared to true/false can be reduced in 14 out of the 20
2913 // (10 predicates * 2 constants) possible combinations. The other
2914 // 6 cases require a 'not' of the LHS.
2915
2916 auto ExtractNotLHS = [](Value *V) -> Value * {
2917 Value *X;
2918 if (match(V, P: m_Not(V: m_Value(V&: X))))
2919 return X;
2920 return nullptr;
2921 };
2922
2923 if (match(V: RHS, P: m_Zero())) {
2924 switch (Pred) {
2925 case CmpInst::ICMP_NE: // X != 0 -> X
2926 case CmpInst::ICMP_UGT: // X >u 0 -> X
2927 case CmpInst::ICMP_SLT: // X <s 0 -> X
2928 return LHS;
2929
2930 case CmpInst::ICMP_EQ: // not(X) == 0 -> X != 0 -> X
2931 case CmpInst::ICMP_ULE: // not(X) <=u 0 -> X >u 0 -> X
2932 case CmpInst::ICMP_SGE: // not(X) >=s 0 -> X <s 0 -> X
2933 if (Value *X = ExtractNotLHS(LHS))
2934 return X;
2935 break;
2936
2937 case CmpInst::ICMP_ULT: // X <u 0 -> false
2938 case CmpInst::ICMP_SGT: // X >s 0 -> false
2939 return getFalse(Ty: ITy);
2940
2941 case CmpInst::ICMP_UGE: // X >=u 0 -> true
2942 case CmpInst::ICMP_SLE: // X <=s 0 -> true
2943 return getTrue(Ty: ITy);
2944
2945 default:
2946 break;
2947 }
2948 } else if (match(V: RHS, P: m_One())) {
2949 switch (Pred) {
2950 case CmpInst::ICMP_EQ: // X == 1 -> X
2951 case CmpInst::ICMP_UGE: // X >=u 1 -> X
2952 case CmpInst::ICMP_SLE: // X <=s -1 -> X
2953 return LHS;
2954
2955 case CmpInst::ICMP_NE: // not(X) != 1 -> X == 1 -> X
2956 case CmpInst::ICMP_ULT: // not(X) <=u 1 -> X >=u 1 -> X
2957 case CmpInst::ICMP_SGT: // not(X) >s 1 -> X <=s -1 -> X
2958 if (Value *X = ExtractNotLHS(LHS))
2959 return X;
2960 break;
2961
2962 case CmpInst::ICMP_UGT: // X >u 1 -> false
2963 case CmpInst::ICMP_SLT: // X <s -1 -> false
2964 return getFalse(Ty: ITy);
2965
2966 case CmpInst::ICMP_ULE: // X <=u 1 -> true
2967 case CmpInst::ICMP_SGE: // X >=s -1 -> true
2968 return getTrue(Ty: ITy);
2969
2970 default:
2971 break;
2972 }
2973 }
2974
2975 switch (Pred) {
2976 default:
2977 break;
2978 case ICmpInst::ICMP_UGE:
2979 if (isImpliedCondition(LHS: RHS, RHS: LHS, DL: Q.DL).value_or(u: false))
2980 return getTrue(Ty: ITy);
2981 break;
2982 case ICmpInst::ICMP_SGE:
2983 /// For signed comparison, the values for an i1 are 0 and -1
2984 /// respectively. This maps into a truth table of:
2985 /// LHS | RHS | LHS >=s RHS | LHS implies RHS
2986 /// 0 | 0 | 1 (0 >= 0) | 1
2987 /// 0 | 1 | 1 (0 >= -1) | 1
2988 /// 1 | 0 | 0 (-1 >= 0) | 0
2989 /// 1 | 1 | 1 (-1 >= -1) | 1
2990 if (isImpliedCondition(LHS, RHS, DL: Q.DL).value_or(u: false))
2991 return getTrue(Ty: ITy);
2992 break;
2993 case ICmpInst::ICMP_ULE:
2994 if (isImpliedCondition(LHS, RHS, DL: Q.DL).value_or(u: false))
2995 return getTrue(Ty: ITy);
2996 break;
2997 case ICmpInst::ICMP_SLE:
2998 /// SLE follows the same logic as SGE with the LHS and RHS swapped.
2999 if (isImpliedCondition(LHS: RHS, RHS: LHS, DL: Q.DL).value_or(u: false))
3000 return getTrue(Ty: ITy);
3001 break;
3002 }
3003
3004 return nullptr;
3005}
3006
3007/// Check if RHS is zero or can be transformed to an equivalent zero comparison.
3008/// E.g., icmp sgt X, -1 --> icmp sge X, 0
3009static bool matchEquivZeroRHS(CmpPredicate &Pred, const Value *RHS) {
3010 // icmp [pred] X, 0 --> as-is
3011 if (match(V: RHS, P: m_Zero()))
3012 return true;
3013
3014 // Handle comparisons with -1 (all ones)
3015 if (match(V: RHS, P: m_AllOnes())) {
3016 switch (Pred) {
3017 case ICmpInst::ICMP_SGT:
3018 // icmp sgt X, -1 --> icmp sge X, 0
3019 Pred = ICmpInst::ICMP_SGE;
3020 return true;
3021 case ICmpInst::ICMP_SLE:
3022 // icmp sle X, -1 --> icmp slt X, 0
3023 Pred = ICmpInst::ICMP_SLT;
3024 return true;
3025 // Note: unsigned comparisons with -1 (UINT_MAX) are not handled here:
3026 // - icmp ugt X, -1 is always false (nothing > UINT_MAX)
3027 // - icmp ule X, -1 is always true (everything <= UINT_MAX)
3028 default:
3029 return false;
3030 }
3031 }
3032
3033 // Handle comparisons with 1
3034 if (match(V: RHS, P: m_One())) {
3035 switch (Pred) {
3036 case ICmpInst::ICMP_SGE:
3037 // icmp sge X, 1 --> icmp sgt X, 0
3038 Pred = ICmpInst::ICMP_SGT;
3039 return true;
3040 case ICmpInst::ICMP_UGE:
3041 // icmp uge X, 1 --> icmp ugt X, 0
3042 Pred = ICmpInst::ICMP_UGT;
3043 return true;
3044 case ICmpInst::ICMP_SLT:
3045 // icmp slt X, 1 --> icmp sle X, 0
3046 Pred = ICmpInst::ICMP_SLE;
3047 return true;
3048 case ICmpInst::ICMP_ULT:
3049 // icmp ult X, 1 --> icmp ule X, 0
3050 Pred = ICmpInst::ICMP_ULE;
3051 return true;
3052 default:
3053 return false;
3054 }
3055 }
3056
3057 return false;
3058}
3059
3060/// Try hard to fold icmp with zero RHS because this is a common case.
3061/// Note that, this function also handles the equivalent zero RHS, e.g.,
3062/// icmp sgt X, -1 --> icmp sge X, 0
3063static Value *simplifyICmpWithZero(CmpPredicate Pred, Value *LHS, Value *RHS,
3064 const SimplifyQuery &Q) {
3065 // Check if RHS is zero or can be transformed to an equivalent zero comparison
3066 if (!matchEquivZeroRHS(Pred, RHS))
3067 return nullptr;
3068
3069 Type *ITy = getCompareTy(Op: LHS); // The return type.
3070 switch (Pred) {
3071 default:
3072 llvm_unreachable("Unknown ICmp predicate!");
3073 case ICmpInst::ICMP_ULT:
3074 return getFalse(Ty: ITy);
3075 case ICmpInst::ICMP_UGE:
3076 return getTrue(Ty: ITy);
3077 case ICmpInst::ICMP_EQ:
3078 case ICmpInst::ICMP_ULE:
3079 if (isKnownNonZero(V: LHS, Q))
3080 return getFalse(Ty: ITy);
3081 break;
3082 case ICmpInst::ICMP_NE:
3083 case ICmpInst::ICMP_UGT:
3084 if (isKnownNonZero(V: LHS, Q))
3085 return getTrue(Ty: ITy);
3086 break;
3087 case ICmpInst::ICMP_SLT: {
3088 KnownBits LHSKnown = computeKnownBits(V: LHS, Q);
3089 if (LHSKnown.isNegative())
3090 return getTrue(Ty: ITy);
3091 if (LHSKnown.isNonNegative())
3092 return getFalse(Ty: ITy);
3093 break;
3094 }
3095 case ICmpInst::ICMP_SLE: {
3096 KnownBits LHSKnown = computeKnownBits(V: LHS, Q);
3097 if (LHSKnown.isNegative())
3098 return getTrue(Ty: ITy);
3099 if (LHSKnown.isNonNegative() && isKnownNonZero(V: LHS, Q))
3100 return getFalse(Ty: ITy);
3101 break;
3102 }
3103 case ICmpInst::ICMP_SGE: {
3104 KnownBits LHSKnown = computeKnownBits(V: LHS, Q);
3105 if (LHSKnown.isNegative())
3106 return getFalse(Ty: ITy);
3107 if (LHSKnown.isNonNegative())
3108 return getTrue(Ty: ITy);
3109 break;
3110 }
3111 case ICmpInst::ICMP_SGT: {
3112 KnownBits LHSKnown = computeKnownBits(V: LHS, Q);
3113 if (LHSKnown.isNegative())
3114 return getFalse(Ty: ITy);
3115 if (LHSKnown.isNonNegative() && isKnownNonZero(V: LHS, Q))
3116 return getTrue(Ty: ITy);
3117 break;
3118 }
3119 }
3120
3121 return nullptr;
3122}
3123
3124static Value *simplifyICmpWithConstant(CmpPredicate Pred, Value *LHS,
3125 Value *RHS, const SimplifyQuery &Q) {
3126 Type *ITy = getCompareTy(Op: RHS); // The return type.
3127
3128 Value *X;
3129 const APInt *C;
3130 if (!match(V: RHS, P: m_APIntAllowPoison(Res&: C)))
3131 return nullptr;
3132
3133 // Sign-bit checks can be optimized to true/false after unsigned
3134 // floating-point casts:
3135 // icmp slt (bitcast (uitofp X)), 0 --> false
3136 // icmp sgt (bitcast (uitofp X)), -1 --> true
3137 if (match(V: LHS, P: m_ElementWiseBitCast(Op: m_UIToFP(Op: m_Value(V&: X))))) {
3138 bool TrueIfSigned;
3139 if (isSignBitCheck(Pred, RHS: *C, TrueIfSigned))
3140 return ConstantInt::getBool(Ty: ITy, V: !TrueIfSigned);
3141 }
3142
3143 // Rule out tautological comparisons (eg., ult 0 or uge 0).
3144 ConstantRange RHS_CR = ConstantRange::makeExactICmpRegion(Pred, Other: *C);
3145 if (RHS_CR.isEmptySet())
3146 return ConstantInt::getFalse(Ty: ITy);
3147 if (RHS_CR.isFullSet())
3148 return ConstantInt::getTrue(Ty: ITy);
3149
3150 ConstantRange LHS_CR = computeConstantRange(V: LHS, ForSigned: CmpInst::isSigned(Pred), SQ: Q);
3151 if (!LHS_CR.isFullSet()) {
3152 if (RHS_CR.contains(CR: LHS_CR))
3153 return ConstantInt::getTrue(Ty: ITy);
3154 if (RHS_CR.inverse().contains(CR: LHS_CR))
3155 return ConstantInt::getFalse(Ty: ITy);
3156 }
3157
3158 // (mul nuw/nsw X, MulC) != C --> true (if C is not a multiple of MulC)
3159 // (mul nuw/nsw X, MulC) == C --> false (if C is not a multiple of MulC)
3160 const APInt *MulC;
3161 if (Q.IIQ.UseInstrInfo && ICmpInst::isEquality(P: Pred) &&
3162 ((match(V: LHS, P: m_NUWMul(L: m_Value(), R: m_APIntAllowPoison(Res&: MulC))) &&
3163 *MulC != 0 && C->urem(RHS: *MulC) != 0) ||
3164 (match(V: LHS, P: m_NSWMul(L: m_Value(), R: m_APIntAllowPoison(Res&: MulC))) &&
3165 *MulC != 0 && C->srem(RHS: *MulC) != 0)))
3166 return ConstantInt::get(Ty: ITy, V: Pred == ICmpInst::ICMP_NE);
3167
3168 if (Pred == ICmpInst::ICMP_UGE && C->isOne() && isKnownNonZero(V: LHS, Q))
3169 return ConstantInt::getTrue(Ty: ITy);
3170
3171 return nullptr;
3172}
3173
3174enum class MonotonicType { GreaterEq, LowerEq };
3175
3176/// Get values V_i such that V uge V_i (GreaterEq) or V ule V_i (LowerEq).
3177static void getUnsignedMonotonicValues(SmallPtrSetImpl<Value *> &Res, Value *V,
3178 MonotonicType Type,
3179 const SimplifyQuery &Q,
3180 unsigned Depth = 0) {
3181 if (!Res.insert(Ptr: V).second)
3182 return;
3183
3184 // Can be increased if useful.
3185 if (++Depth > 1)
3186 return;
3187
3188 auto *I = dyn_cast<Instruction>(Val: V);
3189 if (!I)
3190 return;
3191
3192 Value *X, *Y;
3193 if (Type == MonotonicType::GreaterEq) {
3194 if (match(V: I, P: m_Or(L: m_Value(V&: X), R: m_Value(V&: Y))) ||
3195 match(V: I, P: m_Intrinsic<Intrinsic::uadd_sat>(Ops: m_Value(V&: X), Ops: m_Value(V&: Y)))) {
3196 getUnsignedMonotonicValues(Res, V: X, Type, Q, Depth);
3197 getUnsignedMonotonicValues(Res, V: Y, Type, Q, Depth);
3198 }
3199 // X * Y >= X --> true
3200 if (match(V: I, P: m_NUWMul(L: m_Value(V&: X), R: m_Value(V&: Y)))) {
3201 if (isKnownNonZero(V: X, Q))
3202 getUnsignedMonotonicValues(Res, V: Y, Type, Q, Depth);
3203 if (isKnownNonZero(V: Y, Q))
3204 getUnsignedMonotonicValues(Res, V: X, Type, Q, Depth);
3205 }
3206 } else {
3207 assert(Type == MonotonicType::LowerEq);
3208 switch (I->getOpcode()) {
3209 case Instruction::And:
3210 getUnsignedMonotonicValues(Res, V: I->getOperand(i: 0), Type, Q, Depth);
3211 getUnsignedMonotonicValues(Res, V: I->getOperand(i: 1), Type, Q, Depth);
3212 break;
3213 case Instruction::URem:
3214 case Instruction::UDiv:
3215 case Instruction::LShr:
3216 getUnsignedMonotonicValues(Res, V: I->getOperand(i: 0), Type, Q, Depth);
3217 break;
3218 case Instruction::Call:
3219 if (match(V: I, P: m_Intrinsic<Intrinsic::usub_sat>(Ops: m_Value(V&: X))))
3220 getUnsignedMonotonicValues(Res, V: X, Type, Q, Depth);
3221 break;
3222 default:
3223 break;
3224 }
3225 }
3226}
3227
3228static Value *simplifyICmpUsingMonotonicValues(CmpPredicate Pred, Value *LHS,
3229 Value *RHS,
3230 const SimplifyQuery &Q) {
3231 if (Pred != ICmpInst::ICMP_UGE && Pred != ICmpInst::ICMP_ULT)
3232 return nullptr;
3233
3234 // We have LHS uge GreaterValues and LowerValues uge RHS. If any of the
3235 // GreaterValues and LowerValues are the same, it follows that LHS uge RHS.
3236 SmallPtrSet<Value *, 4> GreaterValues;
3237 SmallPtrSet<Value *, 4> LowerValues;
3238 getUnsignedMonotonicValues(Res&: GreaterValues, V: LHS, Type: MonotonicType::GreaterEq, Q);
3239 getUnsignedMonotonicValues(Res&: LowerValues, V: RHS, Type: MonotonicType::LowerEq, Q);
3240 for (Value *GV : GreaterValues)
3241 if (LowerValues.contains(Ptr: GV))
3242 return ConstantInt::getBool(Ty: getCompareTy(Op: LHS),
3243 V: Pred == ICmpInst::ICMP_UGE);
3244 return nullptr;
3245}
3246
3247static Value *simplifyICmpWithBinOpOnLHS(CmpPredicate Pred, BinaryOperator *LBO,
3248 Value *RHS, const SimplifyQuery &Q,
3249 unsigned MaxRecurse) {
3250 Type *ITy = getCompareTy(Op: RHS); // The return type.
3251
3252 Value *Y = nullptr;
3253 // icmp pred (or X, Y), X
3254 if (match(V: LBO, P: m_c_Or(L: m_Value(V&: Y), R: m_Specific(V: RHS)))) {
3255 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGE) {
3256 KnownBits RHSKnown = computeKnownBits(V: RHS, Q);
3257 KnownBits YKnown = computeKnownBits(V: Y, Q);
3258 if (RHSKnown.isNonNegative() && YKnown.isNegative())
3259 return Pred == ICmpInst::ICMP_SLT ? getTrue(Ty: ITy) : getFalse(Ty: ITy);
3260 if (RHSKnown.isNegative() || YKnown.isNonNegative())
3261 return Pred == ICmpInst::ICMP_SLT ? getFalse(Ty: ITy) : getTrue(Ty: ITy);
3262 }
3263 }
3264
3265 // icmp pred (urem X, Y), Y
3266 if (match(V: LBO, P: m_URem(L: m_Value(), R: m_Specific(V: RHS)))) {
3267 switch (Pred) {
3268 default:
3269 break;
3270 case ICmpInst::ICMP_SGT:
3271 case ICmpInst::ICMP_SGE: {
3272 KnownBits Known = computeKnownBits(V: RHS, Q);
3273 if (!Known.isNonNegative())
3274 break;
3275 [[fallthrough]];
3276 }
3277 case ICmpInst::ICMP_EQ:
3278 case ICmpInst::ICMP_UGT:
3279 case ICmpInst::ICMP_UGE:
3280 return getFalse(Ty: ITy);
3281 case ICmpInst::ICMP_SLT:
3282 case ICmpInst::ICMP_SLE: {
3283 KnownBits Known = computeKnownBits(V: RHS, Q);
3284 if (!Known.isNonNegative())
3285 break;
3286 [[fallthrough]];
3287 }
3288 case ICmpInst::ICMP_NE:
3289 case ICmpInst::ICMP_ULT:
3290 case ICmpInst::ICMP_ULE:
3291 return getTrue(Ty: ITy);
3292 }
3293 }
3294
3295 // If x is nonzero:
3296 // x >>u C <u x --> true for C != 0.
3297 // x >>u C != x --> true for C != 0.
3298 // x >>u C >=u x --> false for C != 0.
3299 // x >>u C == x --> false for C != 0.
3300 // x udiv C <u x --> true for C != 1.
3301 // x udiv C != x --> true for C != 1.
3302 // x udiv C >=u x --> false for C != 1.
3303 // x udiv C == x --> false for C != 1.
3304 // TODO: allow non-constant shift amount/divisor
3305 const APInt *C;
3306 if ((match(V: LBO, P: m_LShr(L: m_Specific(V: RHS), R: m_APInt(Res&: C))) && *C != 0) ||
3307 (match(V: LBO, P: m_UDiv(L: m_Specific(V: RHS), R: m_APInt(Res&: C))) && *C != 1)) {
3308 if (isKnownNonZero(V: RHS, Q)) {
3309 switch (Pred) {
3310 default:
3311 break;
3312 case ICmpInst::ICMP_EQ:
3313 case ICmpInst::ICMP_UGE:
3314 case ICmpInst::ICMP_UGT:
3315 return getFalse(Ty: ITy);
3316 case ICmpInst::ICMP_NE:
3317 case ICmpInst::ICMP_ULT:
3318 case ICmpInst::ICMP_ULE:
3319 return getTrue(Ty: ITy);
3320 }
3321 }
3322 }
3323
3324 // (x*C1)/C2 <= x for C1 <= C2.
3325 // This holds even if the multiplication overflows: Assume that x != 0 and
3326 // arithmetic is modulo M. For overflow to occur we must have C1 >= M/x and
3327 // thus C2 >= M/x. It follows that (x*C1)/C2 <= (M-1)/C2 <= ((M-1)*x)/M < x.
3328 //
3329 // Additionally, either the multiplication and division might be represented
3330 // as shifts:
3331 // (x*C1)>>C2 <= x for C1 < 2**C2.
3332 // (x<<C1)/C2 <= x for 2**C1 < C2.
3333 const APInt *C1, *C2;
3334 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))) &&
3335 C1->ule(RHS: *C2)) ||
3336 (match(V: LBO, P: m_LShr(L: m_Mul(L: m_Specific(V: RHS), R: m_APInt(Res&: C1)), R: m_APInt(Res&: C2))) &&
3337 C1->ule(RHS: APInt(C2->getBitWidth(), 1) << *C2)) ||
3338 (match(V: LBO, P: m_UDiv(L: m_Shl(L: m_Specific(V: RHS), R: m_APInt(Res&: C1)), R: m_APInt(Res&: C2))) &&
3339 (APInt(C1->getBitWidth(), 1) << *C1).ule(RHS: *C2))) {
3340 if (Pred == ICmpInst::ICMP_UGT)
3341 return getFalse(Ty: ITy);
3342 if (Pred == ICmpInst::ICMP_ULE)
3343 return getTrue(Ty: ITy);
3344 }
3345
3346 // (sub C, X) == X, C is odd --> false
3347 // (sub C, X) != X, C is odd --> true
3348 if (match(V: LBO, P: m_Sub(L: m_APIntAllowPoison(Res&: C), R: m_Specific(V: RHS))) &&
3349 (*C & 1) == 1 && ICmpInst::isEquality(P: Pred))
3350 return (Pred == ICmpInst::ICMP_EQ) ? getFalse(Ty: ITy) : getTrue(Ty: ITy);
3351
3352 return nullptr;
3353}
3354
3355// If only one of the icmp's operands has NSW flags, try to prove that:
3356//
3357// icmp slt/sgt/sle/sge (x + C1), (x +nsw C2)
3358//
3359// is equivalent to:
3360//
3361// icmp slt/sgt/sle/sge C1, C2
3362//
3363// which is true if x + C2 has the NSW flags set and:
3364// *) C1 <= C2 && C1 >= 0, or
3365// *) C2 <= C1 && C1 <= 0.
3366//
3367static bool trySimplifyICmpWithAdds(CmpPredicate Pred, Value *LHS, Value *RHS,
3368 const InstrInfoQuery &IIQ) {
3369 // TODO: support other predicates.
3370 if (!ICmpInst::isSigned(Pred) || !IIQ.UseInstrInfo)
3371 return false;
3372
3373 // Canonicalize nsw add as RHS.
3374 if (!match(V: RHS, P: m_NSWAdd(L: m_Value(), R: m_Value())))
3375 std::swap(a&: LHS, b&: RHS);
3376 if (!match(V: RHS, P: m_NSWAdd(L: m_Value(), R: m_Value())))
3377 return false;
3378
3379 Value *X;
3380 const APInt *C1, *C2;
3381 if (!match(V: LHS, P: m_Add(L: m_Value(V&: X), R: m_APInt(Res&: C1))) ||
3382 !match(V: RHS, P: m_Add(L: m_Specific(V: X), R: m_APInt(Res&: C2))))
3383 return false;
3384
3385 return (C1->sle(RHS: *C2) && C1->isNonNegative()) ||
3386 (C2->sle(RHS: *C1) && C1->isNonPositive());
3387}
3388
3389/// TODO: A large part of this logic is duplicated in InstCombine's
3390/// foldICmpBinOp(). We should be able to share that and avoid the code
3391/// duplication.
3392static Value *simplifyICmpWithBinOp(CmpPredicate Pred, Value *LHS, Value *RHS,
3393 const SimplifyQuery &Q,
3394 unsigned MaxRecurse) {
3395 BinaryOperator *LBO = dyn_cast<BinaryOperator>(Val: LHS);
3396 BinaryOperator *RBO = dyn_cast<BinaryOperator>(Val: RHS);
3397 if (MaxRecurse && (LBO || RBO)) {
3398 // Analyze the case when either LHS or RHS is an add instruction.
3399 Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
3400 // LHS = A + B (or A and B are null); RHS = C + D (or C and D are null).
3401 bool NoLHSWrapProblem = false, NoRHSWrapProblem = false;
3402 if (LBO && LBO->getOpcode() == Instruction::Add) {
3403 A = LBO->getOperand(i_nocapture: 0);
3404 B = LBO->getOperand(i_nocapture: 1);
3405 NoLHSWrapProblem =
3406 ICmpInst::isEquality(P: Pred) ||
3407 (CmpInst::isUnsigned(Pred) &&
3408 Q.IIQ.hasNoUnsignedWrap(Op: cast<OverflowingBinaryOperator>(Val: LBO))) ||
3409 (CmpInst::isSigned(Pred) &&
3410 Q.IIQ.hasNoSignedWrap(Op: cast<OverflowingBinaryOperator>(Val: LBO)));
3411 }
3412 if (RBO && RBO->getOpcode() == Instruction::Add) {
3413 C = RBO->getOperand(i_nocapture: 0);
3414 D = RBO->getOperand(i_nocapture: 1);
3415 NoRHSWrapProblem =
3416 ICmpInst::isEquality(P: Pred) ||
3417 (CmpInst::isUnsigned(Pred) &&
3418 Q.IIQ.hasNoUnsignedWrap(Op: cast<OverflowingBinaryOperator>(Val: RBO))) ||
3419 (CmpInst::isSigned(Pred) &&
3420 Q.IIQ.hasNoSignedWrap(Op: cast<OverflowingBinaryOperator>(Val: RBO)));
3421 }
3422
3423 // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow.
3424 if ((A == RHS || B == RHS) && NoLHSWrapProblem)
3425 if (Value *V = simplifyICmpInst(Predicate: Pred, LHS: A == RHS ? B : A,
3426 RHS: Constant::getNullValue(Ty: RHS->getType()), Q,
3427 MaxRecurse: MaxRecurse - 1))
3428 return V;
3429
3430 // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow.
3431 if ((C == LHS || D == LHS) && NoRHSWrapProblem)
3432 if (Value *V =
3433 simplifyICmpInst(Predicate: Pred, LHS: Constant::getNullValue(Ty: LHS->getType()),
3434 RHS: C == LHS ? D : C, Q, MaxRecurse: MaxRecurse - 1))
3435 return V;
3436
3437 // icmp (X+Y), (X+Z) -> icmp Y,Z for equalities or if there is no overflow.
3438 bool CanSimplify = (NoLHSWrapProblem && NoRHSWrapProblem) ||
3439 trySimplifyICmpWithAdds(Pred, LHS, RHS, IIQ: Q.IIQ);
3440 if (A && C && (A == C || A == D || B == C || B == D) && CanSimplify) {
3441 // Determine Y and Z in the form icmp (X+Y), (X+Z).
3442 Value *Y, *Z;
3443 if (A == C) {
3444 // C + B == C + D -> B == D
3445 Y = B;
3446 Z = D;
3447 } else if (A == D) {
3448 // D + B == C + D -> B == C
3449 Y = B;
3450 Z = C;
3451 } else if (B == C) {
3452 // A + C == C + D -> A == D
3453 Y = A;
3454 Z = D;
3455 } else {
3456 assert(B == D);
3457 // A + D == C + D -> A == C
3458 Y = A;
3459 Z = C;
3460 }
3461 if (Value *V = simplifyICmpInst(Predicate: Pred, LHS: Y, RHS: Z, Q, MaxRecurse: MaxRecurse - 1))
3462 return V;
3463 }
3464 }
3465
3466 if (LBO)
3467 if (Value *V = simplifyICmpWithBinOpOnLHS(Pred, LBO, RHS, Q, MaxRecurse))
3468 return V;
3469
3470 if (RBO)
3471 if (Value *V = simplifyICmpWithBinOpOnLHS(
3472 Pred: ICmpInst::getSwappedPredicate(pred: Pred), LBO: RBO, RHS: LHS, Q, MaxRecurse))
3473 return V;
3474
3475 // 0 - (zext X) pred C
3476 if (!CmpInst::isUnsigned(Pred) && match(V: LHS, P: m_Neg(V: m_ZExt(Op: m_Value())))) {
3477 const APInt *C;
3478 if (match(V: RHS, P: m_APInt(Res&: C))) {
3479 if (C->isStrictlyPositive()) {
3480 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_NE)
3481 return ConstantInt::getTrue(Ty: getCompareTy(Op: RHS));
3482 if (Pred == ICmpInst::ICMP_SGE || Pred == ICmpInst::ICMP_EQ)
3483 return ConstantInt::getFalse(Ty: getCompareTy(Op: RHS));
3484 }
3485 if (C->isNonNegative()) {
3486 if (Pred == ICmpInst::ICMP_SLE)
3487 return ConstantInt::getTrue(Ty: getCompareTy(Op: RHS));
3488 if (Pred == ICmpInst::ICMP_SGT)
3489 return ConstantInt::getFalse(Ty: getCompareTy(Op: RHS));
3490 }
3491 }
3492 }
3493
3494 // If C2 is a power-of-2 and C is not:
3495 // (C2 << X) == C --> false
3496 // (C2 << X) != C --> true
3497 const APInt *C;
3498 if (match(V: LHS, P: m_Shl(L: m_Power2(), R: m_Value())) &&
3499 match(V: RHS, P: m_APIntAllowPoison(Res&: C)) && !C->isPowerOf2()) {
3500 // C2 << X can equal zero in some circumstances.
3501 // This simplification might be unsafe if C is zero.
3502 //
3503 // We know it is safe if:
3504 // - The shift is nsw. We can't shift out the one bit.
3505 // - The shift is nuw. We can't shift out the one bit.
3506 // - C2 is one.
3507 // - C isn't zero.
3508 if (Q.IIQ.hasNoSignedWrap(Op: cast<OverflowingBinaryOperator>(Val: LBO)) ||
3509 Q.IIQ.hasNoUnsignedWrap(Op: cast<OverflowingBinaryOperator>(Val: LBO)) ||
3510 match(V: LHS, P: m_Shl(L: m_One(), R: m_Value())) || !C->isZero()) {
3511 if (Pred == ICmpInst::ICMP_EQ)
3512 return ConstantInt::getFalse(Ty: getCompareTy(Op: RHS));
3513 if (Pred == ICmpInst::ICMP_NE)
3514 return ConstantInt::getTrue(Ty: getCompareTy(Op: RHS));
3515 }
3516 }
3517
3518 // If C is a power-of-2:
3519 // (C << X) >u 0x8000 --> false
3520 // (C << X) <=u 0x8000 --> true
3521 if (match(V: LHS, P: m_Shl(L: m_Power2(), R: m_Value())) && match(V: RHS, P: m_SignMask())) {
3522 if (Pred == ICmpInst::ICMP_UGT)
3523 return ConstantInt::getFalse(Ty: getCompareTy(Op: RHS));
3524 if (Pred == ICmpInst::ICMP_ULE)
3525 return ConstantInt::getTrue(Ty: getCompareTy(Op: RHS));
3526 }
3527
3528 if (!MaxRecurse || !LBO || !RBO || LBO->getOpcode() != RBO->getOpcode())
3529 return nullptr;
3530
3531 if (LBO->getOperand(i_nocapture: 0) == RBO->getOperand(i_nocapture: 0)) {
3532 switch (LBO->getOpcode()) {
3533 default:
3534 break;
3535 case Instruction::Shl: {
3536 bool NUW = Q.IIQ.hasNoUnsignedWrap(Op: LBO) && Q.IIQ.hasNoUnsignedWrap(Op: RBO);
3537 bool NSW = Q.IIQ.hasNoSignedWrap(Op: LBO) && Q.IIQ.hasNoSignedWrap(Op: RBO);
3538 if (!NUW || (ICmpInst::isSigned(Pred) && !NSW) ||
3539 !isKnownNonZero(V: LBO->getOperand(i_nocapture: 0), Q))
3540 break;
3541 if (Value *V = simplifyICmpInst(Predicate: Pred, LHS: LBO->getOperand(i_nocapture: 1),
3542 RHS: RBO->getOperand(i_nocapture: 1), Q, MaxRecurse: MaxRecurse - 1))
3543 return V;
3544 break;
3545 }
3546 // If C1 & C2 == C1, A = X and/or C1, B = X and/or C2:
3547 // icmp ule A, B -> true
3548 // icmp ugt A, B -> false
3549 // icmp sle A, B -> true (C1 and C2 are the same sign)
3550 // icmp sgt A, B -> false (C1 and C2 are the same sign)
3551 case Instruction::And:
3552 case Instruction::Or: {
3553 const APInt *C1, *C2;
3554 if (ICmpInst::isRelational(P: Pred) &&
3555 match(V: LBO->getOperand(i_nocapture: 1), P: m_APInt(Res&: C1)) &&
3556 match(V: RBO->getOperand(i_nocapture: 1), P: m_APInt(Res&: C2))) {
3557 if (!C1->isSubsetOf(RHS: *C2)) {
3558 std::swap(a&: C1, b&: C2);
3559 Pred = ICmpInst::getSwappedPredicate(pred: Pred);
3560 }
3561 if (C1->isSubsetOf(RHS: *C2)) {
3562 if (Pred == ICmpInst::ICMP_ULE)
3563 return ConstantInt::getTrue(Ty: getCompareTy(Op: LHS));
3564 if (Pred == ICmpInst::ICMP_UGT)
3565 return ConstantInt::getFalse(Ty: getCompareTy(Op: LHS));
3566 if (C1->isNonNegative() == C2->isNonNegative()) {
3567 if (Pred == ICmpInst::ICMP_SLE)
3568 return ConstantInt::getTrue(Ty: getCompareTy(Op: LHS));
3569 if (Pred == ICmpInst::ICMP_SGT)
3570 return ConstantInt::getFalse(Ty: getCompareTy(Op: LHS));
3571 }
3572 }
3573 }
3574 break;
3575 }
3576 }
3577 }
3578
3579 if (LBO->getOperand(i_nocapture: 1) == RBO->getOperand(i_nocapture: 1)) {
3580 switch (LBO->getOpcode()) {
3581 default:
3582 break;
3583 case Instruction::UDiv:
3584 case Instruction::LShr:
3585 if (ICmpInst::isSigned(Pred) || !Q.IIQ.isExact(Op: LBO) ||
3586 !Q.IIQ.isExact(Op: RBO))
3587 break;
3588 if (Value *V = simplifyICmpInst(Predicate: Pred, LHS: LBO->getOperand(i_nocapture: 0),
3589 RHS: RBO->getOperand(i_nocapture: 0), Q, MaxRecurse: MaxRecurse - 1))
3590 return V;
3591 break;
3592 case Instruction::SDiv:
3593 if (!ICmpInst::isEquality(P: Pred) || !Q.IIQ.isExact(Op: LBO) ||
3594 !Q.IIQ.isExact(Op: RBO))
3595 break;
3596 if (Value *V = simplifyICmpInst(Predicate: Pred, LHS: LBO->getOperand(i_nocapture: 0),
3597 RHS: RBO->getOperand(i_nocapture: 0), Q, MaxRecurse: MaxRecurse - 1))
3598 return V;
3599 break;
3600 case Instruction::AShr:
3601 if (!Q.IIQ.isExact(Op: LBO) || !Q.IIQ.isExact(Op: RBO))
3602 break;
3603 if (Value *V = simplifyICmpInst(Predicate: Pred, LHS: LBO->getOperand(i_nocapture: 0),
3604 RHS: RBO->getOperand(i_nocapture: 0), Q, MaxRecurse: MaxRecurse - 1))
3605 return V;
3606 break;
3607 case Instruction::Shl: {
3608 bool NUW = Q.IIQ.hasNoUnsignedWrap(Op: LBO) && Q.IIQ.hasNoUnsignedWrap(Op: RBO);
3609 bool NSW = Q.IIQ.hasNoSignedWrap(Op: LBO) && Q.IIQ.hasNoSignedWrap(Op: RBO);
3610 if (!NUW && !NSW)
3611 break;
3612 if (!NSW && ICmpInst::isSigned(Pred))
3613 break;
3614 if (Value *V = simplifyICmpInst(Predicate: Pred, LHS: LBO->getOperand(i_nocapture: 0),
3615 RHS: RBO->getOperand(i_nocapture: 0), Q, MaxRecurse: MaxRecurse - 1))
3616 return V;
3617 break;
3618 }
3619 }
3620 }
3621 return nullptr;
3622}
3623
3624/// simplify integer comparisons where at least one operand of the compare
3625/// matches an integer min/max idiom.
3626static Value *simplifyICmpWithMinMax(CmpPredicate Pred, Value *LHS, Value *RHS,
3627 const SimplifyQuery &Q,
3628 unsigned MaxRecurse) {
3629 // All folds below need a min/max operand.
3630 if (!isa<MinMaxIntrinsic>(Val: LHS) && !isa<MinMaxIntrinsic>(Val: RHS))
3631 return nullptr;
3632
3633 Type *ITy = getCompareTy(Op: LHS); // The return type.
3634 Value *A, *B;
3635 CmpInst::Predicate P = CmpInst::BAD_ICMP_PREDICATE;
3636 CmpInst::Predicate EqP; // Chosen so that "A == max/min(A,B)" iff "A EqP B".
3637
3638 // Signed variants on "max(a,b)>=a -> true".
3639 if (match(V: LHS, P: m_SMax(Op0: m_Value(V&: A), Op1: m_Value(V&: B))) && (A == RHS || B == RHS)) {
3640 if (A != RHS)
3641 std::swap(a&: A, b&: B); // smax(A, B) pred A.
3642 EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B".
3643 // We analyze this as smax(A, B) pred A.
3644 P = Pred;
3645 } else if (match(V: RHS, P: m_SMax(Op0: m_Value(V&: A), Op1: m_Value(V&: B))) &&
3646 (A == LHS || B == LHS)) {
3647 if (A != LHS)
3648 std::swap(a&: A, b&: B); // A pred smax(A, B).
3649 EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B".
3650 // We analyze this as smax(A, B) swapped-pred A.
3651 P = CmpInst::getSwappedPredicate(pred: Pred);
3652 } else if (match(V: LHS, P: m_SMin(Op0: m_Value(V&: A), Op1: m_Value(V&: B))) &&
3653 (A == RHS || B == RHS)) {
3654 if (A != RHS)
3655 std::swap(a&: A, b&: B); // smin(A, B) pred A.
3656 EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B".
3657 // We analyze this as smax(-A, -B) swapped-pred -A.
3658 // Note that we do not need to actually form -A or -B thanks to EqP.
3659 P = CmpInst::getSwappedPredicate(pred: Pred);
3660 } else if (match(V: RHS, P: m_SMin(Op0: m_Value(V&: A), Op1: m_Value(V&: B))) &&
3661 (A == LHS || B == LHS)) {
3662 if (A != LHS)
3663 std::swap(a&: A, b&: B); // A pred smin(A, B).
3664 EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B".
3665 // We analyze this as smax(-A, -B) pred -A.
3666 // Note that we do not need to actually form -A or -B thanks to EqP.
3667 P = Pred;
3668 }
3669 if (P != CmpInst::BAD_ICMP_PREDICATE) {
3670 // Cases correspond to "max(A, B) p A".
3671 switch (P) {
3672 default:
3673 break;
3674 case CmpInst::ICMP_EQ:
3675 case CmpInst::ICMP_SLE:
3676 // Equivalent to "A EqP B". This may be the same as the condition tested
3677 // in the max/min; if so, we can just return that.
3678 if (Value *V = extractEquivalentCondition(V: LHS, Pred: EqP, LHS: A, RHS: B))
3679 return V;
3680 if (Value *V = extractEquivalentCondition(V: RHS, Pred: EqP, LHS: A, RHS: B))
3681 return V;
3682 // Otherwise, see if "A EqP B" simplifies.
3683 if (MaxRecurse)
3684 if (Value *V = simplifyICmpInst(Predicate: EqP, LHS: A, RHS: B, Q, MaxRecurse: MaxRecurse - 1))
3685 return V;
3686 break;
3687 case CmpInst::ICMP_NE:
3688 case CmpInst::ICMP_SGT: {
3689 CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(pred: EqP);
3690 // Equivalent to "A InvEqP B". This may be the same as the condition
3691 // tested in the max/min; if so, we can just return that.
3692 if (Value *V = extractEquivalentCondition(V: LHS, Pred: InvEqP, LHS: A, RHS: B))
3693 return V;
3694 if (Value *V = extractEquivalentCondition(V: RHS, Pred: InvEqP, LHS: A, RHS: B))
3695 return V;
3696 // Otherwise, see if "A InvEqP B" simplifies.
3697 if (MaxRecurse)
3698 if (Value *V = simplifyICmpInst(Predicate: InvEqP, LHS: A, RHS: B, Q, MaxRecurse: MaxRecurse - 1))
3699 return V;
3700 break;
3701 }
3702 case CmpInst::ICMP_SGE:
3703 // Always true.
3704 return getTrue(Ty: ITy);
3705 case CmpInst::ICMP_SLT:
3706 // Always false.
3707 return getFalse(Ty: ITy);
3708 }
3709 }
3710
3711 // Unsigned variants on "max(a,b)>=a -> true".
3712 P = CmpInst::BAD_ICMP_PREDICATE;
3713 if (match(V: LHS, P: m_UMax(Op0: m_Value(V&: A), Op1: m_Value(V&: B))) && (A == RHS || B == RHS)) {
3714 if (A != RHS)
3715 std::swap(a&: A, b&: B); // umax(A, B) pred A.
3716 EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B".
3717 // We analyze this as umax(A, B) pred A.
3718 P = Pred;
3719 } else if (match(V: RHS, P: m_UMax(Op0: m_Value(V&: A), Op1: m_Value(V&: B))) &&
3720 (A == LHS || B == LHS)) {
3721 if (A != LHS)
3722 std::swap(a&: A, b&: B); // A pred umax(A, B).
3723 EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B".
3724 // We analyze this as umax(A, B) swapped-pred A.
3725 P = CmpInst::getSwappedPredicate(pred: Pred);
3726 } else if (match(V: LHS, P: m_UMin(Op0: m_Value(V&: A), Op1: m_Value(V&: B))) &&
3727 (A == RHS || B == RHS)) {
3728 if (A != RHS)
3729 std::swap(a&: A, b&: B); // umin(A, B) pred A.
3730 EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B".
3731 // We analyze this as umax(-A, -B) swapped-pred -A.
3732 // Note that we do not need to actually form -A or -B thanks to EqP.
3733 P = CmpInst::getSwappedPredicate(pred: Pred);
3734 } else if (match(V: RHS, P: m_UMin(Op0: m_Value(V&: A), Op1: m_Value(V&: B))) &&
3735 (A == LHS || B == LHS)) {
3736 if (A != LHS)
3737 std::swap(a&: A, b&: B); // A pred umin(A, B).
3738 EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B".
3739 // We analyze this as umax(-A, -B) pred -A.
3740 // Note that we do not need to actually form -A or -B thanks to EqP.
3741 P = Pred;
3742 }
3743 if (P != CmpInst::BAD_ICMP_PREDICATE) {
3744 // Cases correspond to "max(A, B) p A".
3745 switch (P) {
3746 default:
3747 break;
3748 case CmpInst::ICMP_EQ:
3749 case CmpInst::ICMP_ULE:
3750 // Equivalent to "A EqP B". This may be the same as the condition tested
3751 // in the max/min; if so, we can just return that.
3752 if (Value *V = extractEquivalentCondition(V: LHS, Pred: EqP, LHS: A, RHS: B))
3753 return V;
3754 if (Value *V = extractEquivalentCondition(V: RHS, Pred: EqP, LHS: A, RHS: B))
3755 return V;
3756 // Otherwise, see if "A EqP B" simplifies.
3757 if (MaxRecurse)
3758 if (Value *V = simplifyICmpInst(Predicate: EqP, LHS: A, RHS: B, Q, MaxRecurse: MaxRecurse - 1))
3759 return V;
3760 break;
3761 case CmpInst::ICMP_NE:
3762 case CmpInst::ICMP_UGT: {
3763 CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(pred: EqP);
3764 // Equivalent to "A InvEqP B". This may be the same as the condition
3765 // tested in the max/min; if so, we can just return that.
3766 if (Value *V = extractEquivalentCondition(V: LHS, Pred: InvEqP, LHS: A, RHS: B))
3767 return V;
3768 if (Value *V = extractEquivalentCondition(V: RHS, Pred: InvEqP, LHS: A, RHS: B))
3769 return V;
3770 // Otherwise, see if "A InvEqP B" simplifies.
3771 if (MaxRecurse)
3772 if (Value *V = simplifyICmpInst(Predicate: InvEqP, LHS: A, RHS: B, Q, MaxRecurse: MaxRecurse - 1))
3773 return V;
3774 break;
3775 }
3776 case CmpInst::ICMP_UGE:
3777 return getTrue(Ty: ITy);
3778 case CmpInst::ICMP_ULT:
3779 return getFalse(Ty: ITy);
3780 }
3781 }
3782
3783 // Comparing 1 each of min/max with a common operand?
3784 // Canonicalize min operand to RHS.
3785 if (match(V: LHS, P: m_UMin(Op0: m_Value(), Op1: m_Value())) ||
3786 match(V: LHS, P: m_SMin(Op0: m_Value(), Op1: m_Value()))) {
3787 std::swap(a&: LHS, b&: RHS);
3788 Pred = ICmpInst::getSwappedPredicate(pred: Pred);
3789 }
3790
3791 Value *C, *D;
3792 if (match(V: LHS, P: m_SMax(Op0: m_Value(V&: A), Op1: m_Value(V&: B))) &&
3793 match(V: RHS, P: m_SMin(Op0: m_Value(V&: C), Op1: m_Value(V&: D))) &&
3794 (A == C || A == D || B == C || B == D)) {
3795 // smax(A, B) >=s smin(A, D) --> true
3796 if (Pred == CmpInst::ICMP_SGE)
3797 return getTrue(Ty: ITy);
3798 // smax(A, B) <s smin(A, D) --> false
3799 if (Pred == CmpInst::ICMP_SLT)
3800 return getFalse(Ty: ITy);
3801 } else if (match(V: LHS, P: m_UMax(Op0: m_Value(V&: A), Op1: m_Value(V&: B))) &&
3802 match(V: RHS, P: m_UMin(Op0: m_Value(V&: C), Op1: m_Value(V&: D))) &&
3803 (A == C || A == D || B == C || B == D)) {
3804 // umax(A, B) >=u umin(A, D) --> true
3805 if (Pred == CmpInst::ICMP_UGE)
3806 return getTrue(Ty: ITy);
3807 // umax(A, B) <u umin(A, D) --> false
3808 if (Pred == CmpInst::ICMP_ULT)
3809 return getFalse(Ty: ITy);
3810 }
3811
3812 return nullptr;
3813}
3814
3815static Value *simplifyICmpWithDominatingAssume(CmpPredicate Predicate,
3816 Value *LHS, Value *RHS,
3817 const SimplifyQuery &Q) {
3818 // Gracefully handle instructions that have not been inserted yet.
3819 if (!Q.AC || !Q.CtxI)
3820 return nullptr;
3821
3822 for (Value *AssumeBaseOp : {LHS, RHS}) {
3823 for (auto &AssumeVH : Q.AC->assumptionsFor(V: AssumeBaseOp)) {
3824 if (!AssumeVH)
3825 continue;
3826
3827 CallInst *Assume = cast<CallInst>(Val&: AssumeVH);
3828 if (std::optional<bool> Imp = isImpliedCondition(
3829 LHS: Assume->getArgOperand(i: 0), RHSPred: Predicate, RHSOp0: LHS, RHSOp1: RHS, DL: Q.DL))
3830 if (isValidAssumeForContext(I: Assume, Q))
3831 return ConstantInt::get(Ty: getCompareTy(Op: LHS), V: *Imp);
3832 }
3833 }
3834
3835 return nullptr;
3836}
3837
3838static Value *simplifyICmpWithIntrinsicOnLHS(CmpPredicate Pred, Value *LHS,
3839 Value *RHS) {
3840 auto *II = dyn_cast<IntrinsicInst>(Val: LHS);
3841 if (!II)
3842 return nullptr;
3843
3844 switch (II->getIntrinsicID()) {
3845 case Intrinsic::uadd_sat:
3846 // uadd.sat(X, Y) uge X + Y
3847 if (match(V: RHS, P: m_c_Add(L: m_Specific(V: II->getArgOperand(i: 0)),
3848 R: m_Specific(V: II->getArgOperand(i: 1))))) {
3849 if (Pred == ICmpInst::ICMP_UGE)
3850 return ConstantInt::getTrue(Ty: getCompareTy(Op: II));
3851 if (Pred == ICmpInst::ICMP_ULT)
3852 return ConstantInt::getFalse(Ty: getCompareTy(Op: II));
3853 }
3854 return nullptr;
3855 case Intrinsic::usub_sat:
3856 // usub.sat(X, Y) ule X - Y
3857 if (match(V: RHS, P: m_Sub(L: m_Specific(V: II->getArgOperand(i: 0)),
3858 R: m_Specific(V: II->getArgOperand(i: 1))))) {
3859 if (Pred == ICmpInst::ICMP_ULE)
3860 return ConstantInt::getTrue(Ty: getCompareTy(Op: II));
3861 if (Pred == ICmpInst::ICMP_UGT)
3862 return ConstantInt::getFalse(Ty: getCompareTy(Op: II));
3863 }
3864 return nullptr;
3865 default:
3866 return nullptr;
3867 }
3868}
3869
3870/// Helper method to get range from metadata or attribute.
3871static std::optional<ConstantRange> getRange(Value *V,
3872 const InstrInfoQuery &IIQ) {
3873 if (Instruction *I = dyn_cast<Instruction>(Val: V))
3874 if (MDNode *MD = IIQ.getMetadata(I, KindID: LLVMContext::MD_range))
3875 return getConstantRangeFromMetadata(RangeMD: *MD);
3876
3877 if (const Argument *A = dyn_cast<Argument>(Val: V))
3878 return A->getRange();
3879 else if (const CallBase *CB = dyn_cast<CallBase>(Val: V))
3880 return CB->getRange();
3881
3882 return std::nullopt;
3883}
3884
3885/// Given operands for an ICmpInst, see if we can fold the result.
3886/// If not, this returns null.
3887static Value *simplifyICmpInst(CmpPredicate Pred, Value *LHS, Value *RHS,
3888 const SimplifyQuery &Q, unsigned MaxRecurse) {
3889 assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!");
3890
3891 if (Constant *CLHS = dyn_cast<Constant>(Val: LHS)) {
3892 if (Constant *CRHS = dyn_cast<Constant>(Val: RHS)) {
3893 if (Constant *C =
3894 ConstantFoldCompareInstOperands(Predicate: Pred, LHS: CLHS, RHS: CRHS, DL: Q.DL, TLI: Q.TLI))
3895 return C;
3896 // Constant folding cannot always decide a comparison between constants.
3897 // Fall through to the generic logic below, which can still reason about
3898 // them with value tracking.
3899 } else {
3900 // If we have a constant, make sure it is on the RHS.
3901 std::swap(a&: LHS, b&: RHS);
3902 Pred = CmpInst::getSwappedPredicate(pred: Pred);
3903 }
3904 }
3905 assert(!isa<UndefValue>(LHS) && "Unexpected icmp undef,%X");
3906
3907 Type *ITy = getCompareTy(Op: LHS); // The return type.
3908
3909 // icmp poison, X -> poison
3910 if (isa<PoisonValue>(Val: RHS))
3911 return PoisonValue::get(T: ITy);
3912
3913 // For EQ and NE, we can always pick a value for the undef to make the
3914 // predicate pass or fail, so we can return undef.
3915 // Matches behavior in llvm::ConstantFoldCompareInstruction.
3916 if (Q.isUndefValue(V: RHS) && ICmpInst::isEquality(P: Pred))
3917 return UndefValue::get(T: ITy);
3918
3919 // icmp X, X -> true/false
3920 // icmp X, undef -> true/false because undef could be X.
3921 if (LHS == RHS || Q.isUndefValue(V: RHS))
3922 return ConstantInt::get(Ty: ITy, V: CmpInst::isTrueWhenEqual(predicate: Pred));
3923
3924 if (Value *V = simplifyICmpOfBools(Pred, LHS, RHS, Q))
3925 return V;
3926
3927 // TODO: Sink/common this with other potentially expensive calls that use
3928 // ValueTracking? See comment below for isKnownNonEqual().
3929 if (Value *V = simplifyICmpWithZero(Pred, LHS, RHS, Q))
3930 return V;
3931
3932 if (Value *V = simplifyICmpWithConstant(Pred, LHS, RHS, Q))
3933 return V;
3934
3935 // If both operands have range metadata, use the metadata
3936 // to simplify the comparison.
3937 if (std::optional<ConstantRange> RhsCr = getRange(V: RHS, IIQ: Q.IIQ))
3938 if (std::optional<ConstantRange> LhsCr = getRange(V: LHS, IIQ: Q.IIQ)) {
3939 if (LhsCr->icmp(Pred, Other: *RhsCr))
3940 return ConstantInt::getTrue(Ty: ITy);
3941
3942 if (LhsCr->icmp(Pred: CmpInst::getInversePredicate(pred: Pred), Other: *RhsCr))
3943 return ConstantInt::getFalse(Ty: ITy);
3944 }
3945
3946 // Compare of cast, for example (zext X) != 0 -> X != 0
3947 if (isa<CastInst>(Val: LHS) && (isa<Constant>(Val: RHS) || isa<CastInst>(Val: RHS))) {
3948 Instruction *LI = cast<CastInst>(Val: LHS);
3949 Value *SrcOp = LI->getOperand(i: 0);
3950 Type *SrcTy = SrcOp->getType();
3951 Type *DstTy = LI->getType();
3952
3953 // Turn icmp (ptrtoint/ptrtoaddr x), (ptrtoint/ptrtoaddr/constant) into a
3954 // compare of the input if the integer type is the same size as the
3955 // pointer address type (icmp only compares the address of the pointer).
3956 if (MaxRecurse && (isa<PtrToIntInst, PtrToAddrInst>(Val: LI)) &&
3957 Q.DL.getAddressType(PtrTy: SrcTy) == DstTy) {
3958 if (Constant *RHSC = dyn_cast<Constant>(Val: RHS)) {
3959 // Transfer the cast to the constant.
3960 if (Value *V = simplifyICmpInst(Pred, LHS: SrcOp,
3961 RHS: ConstantExpr::getIntToPtr(C: RHSC, Ty: SrcTy),
3962 Q, MaxRecurse: MaxRecurse - 1))
3963 return V;
3964 } else if (isa<PtrToIntInst, PtrToAddrInst>(Val: RHS)) {
3965 auto *RI = cast<CastInst>(Val: RHS);
3966 if (RI->getOperand(i_nocapture: 0)->getType() == SrcTy)
3967 // Compare without the cast.
3968 if (Value *V = simplifyICmpInst(Pred, LHS: SrcOp, RHS: RI->getOperand(i_nocapture: 0), Q,
3969 MaxRecurse: MaxRecurse - 1))
3970 return V;
3971 }
3972 }
3973
3974 if (isa<ZExtInst>(Val: LHS)) {
3975 // Turn icmp (zext X), (zext Y) into a compare of X and Y if they have the
3976 // same type.
3977 if (ZExtInst *RI = dyn_cast<ZExtInst>(Val: RHS)) {
3978 if (MaxRecurse && SrcTy == RI->getOperand(i_nocapture: 0)->getType())
3979 // Compare X and Y. Note that signed predicates become unsigned.
3980 if (Value *V =
3981 simplifyICmpInst(Pred: ICmpInst::getUnsignedPredicate(Pred), LHS: SrcOp,
3982 RHS: RI->getOperand(i_nocapture: 0), Q, MaxRecurse: MaxRecurse - 1))
3983 return V;
3984 }
3985 // Fold (zext X) ule (sext X), (zext X) sge (sext X) to true.
3986 else if (SExtInst *RI = dyn_cast<SExtInst>(Val: RHS)) {
3987 if (SrcOp == RI->getOperand(i_nocapture: 0)) {
3988 if (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_SGE)
3989 return ConstantInt::getTrue(Ty: ITy);
3990 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_SLT)
3991 return ConstantInt::getFalse(Ty: ITy);
3992 }
3993 }
3994 // Turn icmp (zext X), Cst into a compare of X and Cst if Cst is extended
3995 // too. If not, then try to deduce the result of the comparison.
3996 else if (match(V: RHS, P: m_ImmConstant())) {
3997 Constant *C = dyn_cast<Constant>(Val: RHS);
3998 assert(C != nullptr);
3999
4000 // Compute the constant that would happen if we truncated to SrcTy then
4001 // reextended to DstTy.
4002 Constant *Trunc =
4003 ConstantFoldCastOperand(Opcode: Instruction::Trunc, C, DestTy: SrcTy, DL: Q.DL);
4004 assert(Trunc && "Constant-fold of ImmConstant should not fail");
4005 Constant *RExt =
4006 ConstantFoldCastOperand(Opcode: CastInst::ZExt, C: Trunc, DestTy: DstTy, DL: Q.DL);
4007 assert(RExt && "Constant-fold of ImmConstant should not fail");
4008 Constant *AnyEq =
4009 ConstantFoldCompareInstOperands(Predicate: ICmpInst::ICMP_EQ, LHS: RExt, RHS: C, DL: Q.DL);
4010 assert(AnyEq && "Constant-fold of ImmConstant should not fail");
4011
4012 // If the re-extended constant didn't change any of the elements then
4013 // this is effectively also a case of comparing two zero-extended
4014 // values.
4015 if (AnyEq->isAllOnesValue() && MaxRecurse)
4016 if (Value *V = simplifyICmpInst(Pred: ICmpInst::getUnsignedPredicate(Pred),
4017 LHS: SrcOp, RHS: Trunc, Q, MaxRecurse: MaxRecurse - 1))
4018 return V;
4019
4020 // Otherwise the upper bits of LHS are zero while RHS has a non-zero bit
4021 // there. Use this to work out the result of the comparison.
4022 if (AnyEq->isNullValue()) {
4023 switch (Pred) {
4024 default:
4025 llvm_unreachable("Unknown ICmp predicate!");
4026 // LHS <u RHS.
4027 case ICmpInst::ICMP_EQ:
4028 case ICmpInst::ICMP_UGT:
4029 case ICmpInst::ICMP_UGE:
4030 return Constant::getNullValue(Ty: ITy);
4031
4032 case ICmpInst::ICMP_NE:
4033 case ICmpInst::ICMP_ULT:
4034 case ICmpInst::ICMP_ULE:
4035 return Constant::getAllOnesValue(Ty: ITy);
4036
4037 // LHS is non-negative. If RHS is negative then LHS >s LHS. If RHS
4038 // is non-negative then LHS <s RHS.
4039 case ICmpInst::ICMP_SGT:
4040 case ICmpInst::ICMP_SGE:
4041 return ConstantFoldCompareInstOperands(
4042 Predicate: ICmpInst::ICMP_SLT, LHS: C, RHS: Constant::getNullValue(Ty: C->getType()),
4043 DL: Q.DL);
4044 case ICmpInst::ICMP_SLT:
4045 case ICmpInst::ICMP_SLE:
4046 return ConstantFoldCompareInstOperands(
4047 Predicate: ICmpInst::ICMP_SGE, LHS: C, RHS: Constant::getNullValue(Ty: C->getType()),
4048 DL: Q.DL);
4049 }
4050 }
4051 }
4052 }
4053
4054 if (isa<SExtInst>(Val: LHS)) {
4055 // Turn icmp (sext X), (sext Y) into a compare of X and Y if they have the
4056 // same type.
4057 if (SExtInst *RI = dyn_cast<SExtInst>(Val: RHS)) {
4058 if (MaxRecurse && SrcTy == RI->getOperand(i_nocapture: 0)->getType())
4059 // Compare X and Y. Note that the predicate does not change.
4060 if (Value *V = simplifyICmpInst(Pred, LHS: SrcOp, RHS: RI->getOperand(i_nocapture: 0), Q,
4061 MaxRecurse: MaxRecurse - 1))
4062 return V;
4063 }
4064 // Fold (sext X) uge (zext X), (sext X) sle (zext X) to true.
4065 else if (ZExtInst *RI = dyn_cast<ZExtInst>(Val: RHS)) {
4066 if (SrcOp == RI->getOperand(i_nocapture: 0)) {
4067 if (Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_SLE)
4068 return ConstantInt::getTrue(Ty: ITy);
4069 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_SGT)
4070 return ConstantInt::getFalse(Ty: ITy);
4071 }
4072 }
4073 // Turn icmp (sext X), Cst into a compare of X and Cst if Cst is extended
4074 // too. If not, then try to deduce the result of the comparison.
4075 else if (match(V: RHS, P: m_ImmConstant())) {
4076 Constant *C = cast<Constant>(Val: RHS);
4077
4078 // Compute the constant that would happen if we truncated to SrcTy then
4079 // reextended to DstTy.
4080 Constant *Trunc =
4081 ConstantFoldCastOperand(Opcode: Instruction::Trunc, C, DestTy: SrcTy, DL: Q.DL);
4082 assert(Trunc && "Constant-fold of ImmConstant should not fail");
4083 Constant *RExt =
4084 ConstantFoldCastOperand(Opcode: CastInst::SExt, C: Trunc, DestTy: DstTy, DL: Q.DL);
4085 assert(RExt && "Constant-fold of ImmConstant should not fail");
4086 Constant *AnyEq =
4087 ConstantFoldCompareInstOperands(Predicate: ICmpInst::ICMP_EQ, LHS: RExt, RHS: C, DL: Q.DL);
4088 assert(AnyEq && "Constant-fold of ImmConstant should not fail");
4089
4090 // If the re-extended constant didn't change then this is effectively
4091 // also a case of comparing two sign-extended values.
4092 if (AnyEq->isAllOnesValue() && MaxRecurse)
4093 if (Value *V =
4094 simplifyICmpInst(Pred, LHS: SrcOp, RHS: Trunc, Q, MaxRecurse: MaxRecurse - 1))
4095 return V;
4096
4097 // Otherwise the upper bits of LHS are all equal, while RHS has varying
4098 // bits there. Use this to work out the result of the comparison.
4099 if (AnyEq->isNullValue()) {
4100 switch (Pred.getPreferredSignedPredicate()) {
4101 default:
4102 llvm_unreachable("Unknown ICmp predicate!");
4103 case ICmpInst::ICMP_EQ:
4104 return Constant::getNullValue(Ty: ITy);
4105 case ICmpInst::ICMP_NE:
4106 return Constant::getAllOnesValue(Ty: ITy);
4107
4108 // If RHS is non-negative then LHS <s RHS. If RHS is negative then
4109 // LHS >s RHS.
4110 case ICmpInst::ICMP_SGT:
4111 case ICmpInst::ICMP_SGE:
4112 return ConstantFoldCompareInstOperands(
4113 Predicate: ICmpInst::ICMP_SLT, LHS: C, RHS: Constant::getNullValue(Ty: C->getType()),
4114 DL: Q.DL);
4115 case ICmpInst::ICMP_SLT:
4116 case ICmpInst::ICMP_SLE:
4117 return ConstantFoldCompareInstOperands(
4118 Predicate: ICmpInst::ICMP_SGE, LHS: C, RHS: Constant::getNullValue(Ty: C->getType()),
4119 DL: Q.DL);
4120
4121 // If LHS is non-negative then LHS <u RHS. If LHS is negative then
4122 // LHS >u RHS.
4123 case ICmpInst::ICMP_UGT:
4124 case ICmpInst::ICMP_UGE:
4125 // Comparison is true iff the LHS <s 0.
4126 if (MaxRecurse)
4127 if (Value *V = simplifyICmpInst(Pred: ICmpInst::ICMP_SLT, LHS: SrcOp,
4128 RHS: Constant::getNullValue(Ty: SrcTy), Q,
4129 MaxRecurse: MaxRecurse - 1))
4130 return V;
4131 break;
4132 case ICmpInst::ICMP_ULT:
4133 case ICmpInst::ICMP_ULE:
4134 // Comparison is true iff the LHS >=s 0.
4135 if (MaxRecurse)
4136 if (Value *V = simplifyICmpInst(Pred: ICmpInst::ICMP_SGE, LHS: SrcOp,
4137 RHS: Constant::getNullValue(Ty: SrcTy), Q,
4138 MaxRecurse: MaxRecurse - 1))
4139 return V;
4140 break;
4141 }
4142 }
4143 }
4144 }
4145 }
4146
4147 // icmp eq|ne X, Y -> false|true if X != Y
4148 // This is potentially expensive, and we have already computedKnownBits for
4149 // compares with 0 above here, so only try this for a non-zero compare.
4150 if (ICmpInst::isEquality(P: Pred) && !match(V: RHS, P: m_Zero()) &&
4151 isKnownNonEqual(V1: LHS, V2: RHS, SQ: Q)) {
4152 return Pred == ICmpInst::ICMP_NE ? getTrue(Ty: ITy) : getFalse(Ty: ITy);
4153 }
4154
4155 if (Value *V = simplifyICmpWithBinOp(Pred, LHS, RHS, Q, MaxRecurse))
4156 return V;
4157
4158 if (Value *V = simplifyICmpWithMinMax(Pred, LHS, RHS, Q, MaxRecurse))
4159 return V;
4160
4161 if (Value *V = simplifyICmpWithIntrinsicOnLHS(Pred, LHS, RHS))
4162 return V;
4163 if (Value *V = simplifyICmpWithIntrinsicOnLHS(
4164 Pred: ICmpInst::getSwappedPredicate(pred: Pred), LHS: RHS, RHS: LHS))
4165 return V;
4166
4167 if (Value *V = simplifyICmpUsingMonotonicValues(Pred, LHS, RHS, Q))
4168 return V;
4169 if (Value *V = simplifyICmpUsingMonotonicValues(
4170 Pred: ICmpInst::getSwappedPredicate(pred: Pred), LHS: RHS, RHS: LHS, Q))
4171 return V;
4172
4173 if (Value *V = simplifyICmpWithDominatingAssume(Predicate: Pred, LHS, RHS, Q))
4174 return V;
4175
4176 if (std::optional<bool> Res =
4177 isImpliedByDomCondition(Pred, LHS, RHS, ContextI: Q.CtxI, DL: Q.DL))
4178 return ConstantInt::getBool(Ty: ITy, V: *Res);
4179
4180 // Simplify comparisons of related pointers using a powerful, recursive
4181 // GEP-walk when we have target data available..
4182 if (LHS->getType()->isPointerTy())
4183 if (auto *C = computePointerICmp(Pred, LHS, RHS, Q))
4184 return C;
4185
4186 // If the comparison is with the result of a select instruction, check whether
4187 // comparing with either branch of the select always yields the same value.
4188 if (isa<SelectInst>(Val: LHS) || isa<SelectInst>(Val: RHS))
4189 if (Value *V = threadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse))
4190 return V;
4191
4192 // If the comparison is with the result of a phi instruction, check whether
4193 // doing the compare with each incoming phi value yields a common result.
4194 if (isa<PHINode>(Val: LHS) || isa<PHINode>(Val: RHS))
4195 if (Value *V = threadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse))
4196 return V;
4197
4198 return nullptr;
4199}
4200
4201Value *llvm::simplifyICmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS,
4202 const SimplifyQuery &Q) {
4203 return ::simplifyICmpInst(Pred: Predicate, LHS, RHS, Q, MaxRecurse: RecursionLimit);
4204}
4205
4206/// Given operands for an FCmpInst, see if we can fold the result.
4207/// If not, this returns null.
4208static Value *simplifyFCmpInst(CmpPredicate Pred, Value *LHS, Value *RHS,
4209 FastMathFlags FMF, const SimplifyQuery &Q,
4210 unsigned MaxRecurse) {
4211 assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!");
4212
4213 if (Constant *CLHS = dyn_cast<Constant>(Val: LHS)) {
4214 if (Constant *CRHS = dyn_cast<Constant>(Val: RHS)) {
4215 // if the folding isn't successfull, fall back to the rest of the logic
4216 if (auto *Result = ConstantFoldCompareInstOperands(
4217 Predicate: Pred, LHS: CLHS, RHS: CRHS, DL: Q.DL, TLI: Q.TLI, CtxF: Q.getFunction()))
4218 return Result;
4219 } else {
4220 // If we have a constant, make sure it is on the RHS.
4221 std::swap(a&: LHS, b&: RHS);
4222 Pred = CmpInst::getSwappedPredicate(pred: Pred);
4223 }
4224 }
4225
4226 // Fold trivial predicates.
4227 Type *RetTy = getCompareTy(Op: LHS);
4228 if (Pred == FCmpInst::FCMP_FALSE)
4229 return getFalse(Ty: RetTy);
4230 if (Pred == FCmpInst::FCMP_TRUE)
4231 return getTrue(Ty: RetTy);
4232
4233 // fcmp pred x, poison and fcmp pred poison, x
4234 // fold to poison
4235 if (isa<PoisonValue>(Val: LHS) || isa<PoisonValue>(Val: RHS))
4236 return PoisonValue::get(T: RetTy);
4237
4238 // fcmp pred x, undef and fcmp pred undef, x
4239 // fold to true if unordered, false if ordered
4240 if (Q.isUndefValue(V: LHS) || Q.isUndefValue(V: RHS)) {
4241 // Choosing NaN for the undef will always make unordered comparison succeed
4242 // and ordered comparison fail.
4243 return ConstantInt::get(Ty: RetTy, V: CmpInst::isUnordered(predicate: Pred));
4244 }
4245
4246 // fcmp x,x -> true/false. Not all compares are foldable.
4247 if (LHS == RHS) {
4248 if (CmpInst::isTrueWhenEqual(predicate: Pred))
4249 return getTrue(Ty: RetTy);
4250 if (CmpInst::isFalseWhenEqual(predicate: Pred))
4251 return getFalse(Ty: RetTy);
4252 }
4253
4254 // Fold (un)ordered comparison if we can determine there are no NaNs.
4255 //
4256 // This catches the 2 variable input case, constants are handled below as a
4257 // class-like compare.
4258 if (Pred == FCmpInst::FCMP_ORD || Pred == FCmpInst::FCMP_UNO) {
4259 KnownFPClass RHSClass = computeKnownFPClass(V: RHS, InterestedClasses: fcAllFlags, SQ: Q);
4260 KnownFPClass LHSClass = computeKnownFPClass(V: LHS, InterestedClasses: fcAllFlags, SQ: Q);
4261
4262 if (FMF.noNaNs() ||
4263 (RHSClass.isKnownNeverNaN() && LHSClass.isKnownNeverNaN()))
4264 return ConstantInt::get(Ty: RetTy, V: Pred == FCmpInst::FCMP_ORD);
4265
4266 if (RHSClass.isKnownAlwaysNaN() || LHSClass.isKnownAlwaysNaN())
4267 return ConstantInt::get(Ty: RetTy, V: Pred == CmpInst::FCMP_UNO);
4268 }
4269
4270 if (std::optional<bool> Res =
4271 isImpliedByDomCondition(Pred, LHS, RHS, ContextI: Q.CtxI, DL: Q.DL))
4272 return ConstantInt::getBool(Ty: RetTy, V: *Res);
4273
4274 const APFloat *C = nullptr;
4275 match(V: RHS, P: m_APFloatAllowPoison(Res&: C));
4276 std::optional<KnownFPClass> FullKnownClassLHS;
4277
4278 // Lazily compute the possible classes for LHS. Avoid computing it twice if
4279 // RHS is a 0.
4280 auto computeLHSClass = [=, &FullKnownClassLHS](FPClassTest InterestedFlags =
4281 fcAllFlags) {
4282 if (FullKnownClassLHS)
4283 return *FullKnownClassLHS;
4284 return computeKnownFPClass(V: LHS, FMF, InterestedClasses: InterestedFlags, SQ: Q);
4285 };
4286
4287 if (C && Q.getFunction()) {
4288 // Fold out compares that express a class test.
4289 auto [ClassVal, ClassTest] =
4290 fcmpToClassTest(Pred, F: *Q.getFunction(), LHS, ConstRHS: C);
4291 if (ClassVal) {
4292 FullKnownClassLHS = computeLHSClass();
4293 if ((FullKnownClassLHS->getKnownFPClasses() & ClassTest) == fcNone)
4294 return getFalse(Ty: RetTy);
4295 if ((FullKnownClassLHS->getKnownFPClasses() & ~ClassTest) == fcNone)
4296 return getTrue(Ty: RetTy);
4297 }
4298 }
4299
4300 // Handle fcmp with constant RHS.
4301 if (C) {
4302 // TODO: If we always required a context function, we wouldn't need to
4303 // special case nans.
4304 if (C->isNaN())
4305 return ConstantInt::get(Ty: RetTy, V: CmpInst::isUnordered(predicate: Pred));
4306
4307 // TODO: Need version fcmpToClassTest which returns implied class when the
4308 // compare isn't a complete class test. e.g. > 1.0 implies fcPositive, but
4309 // isn't implementable as a class call.
4310 if (C->isNegative() && !C->isNegZero()) {
4311 FPClassTest Interested = KnownFPClass::OrderedLessThanZeroMask;
4312
4313 // TODO: We can catch more cases by using a range check rather than
4314 // relying on CannotBeOrderedLessThanZero.
4315 switch (Pred) {
4316 case FCmpInst::FCMP_UGE:
4317 case FCmpInst::FCMP_UGT:
4318 case FCmpInst::FCMP_UNE: {
4319 KnownFPClass KnownClass = computeLHSClass(Interested);
4320
4321 // (X >= 0) implies (X > C) when (C < 0)
4322 if (KnownClass.cannotBeOrderedLessThanZero())
4323 return getTrue(Ty: RetTy);
4324 break;
4325 }
4326 case FCmpInst::FCMP_OEQ:
4327 case FCmpInst::FCMP_OLE:
4328 case FCmpInst::FCMP_OLT: {
4329 KnownFPClass KnownClass = computeLHSClass(Interested);
4330
4331 // (X >= 0) implies !(X < C) when (C < 0)
4332 if (KnownClass.cannotBeOrderedLessThanZero())
4333 return getFalse(Ty: RetTy);
4334 break;
4335 }
4336 default:
4337 break;
4338 }
4339 }
4340 // Check FCmp of [min/maxnum or min/maximumnum with const] with other const.
4341 const APFloat *C2;
4342 bool IsMax = match(V: LHS, P: m_FMaxNum_or_FMaximumNum(Op0: m_Value(), Op1: m_APFloat(Res&: C2)));
4343 bool IsMin = match(V: LHS, P: m_FMinNum_or_FMinimumNum(Op0: m_Value(), Op1: m_APFloat(Res&: C2)));
4344 if ((IsMax && *C2 > *C) || (IsMin && *C2 < *C)) {
4345 // The ordered relationship and min/maxnum or min/maximumnum guarantee
4346 // that we do not have NaN constants, so ordered/unordered preds are
4347 // handled the same.
4348 switch (Pred) {
4349 case FCmpInst::FCMP_OEQ:
4350 case FCmpInst::FCMP_UEQ:
4351 // minnum(X, LesserC) == C --> false
4352 // maxnum(X, GreaterC) == C --> false
4353 return getFalse(Ty: RetTy);
4354 case FCmpInst::FCMP_ONE:
4355 case FCmpInst::FCMP_UNE:
4356 // minnum(X, LesserC) != C --> true
4357 // maxnum(X, GreaterC) != C --> true
4358 return getTrue(Ty: RetTy);
4359 case FCmpInst::FCMP_OGE:
4360 case FCmpInst::FCMP_UGE:
4361 case FCmpInst::FCMP_OGT:
4362 case FCmpInst::FCMP_UGT:
4363 // minnum(X, LesserC) >= C --> false
4364 // minnum(X, LesserC) > C --> false
4365 // maxnum(X, GreaterC) >= C --> true
4366 // maxnum(X, GreaterC) > C --> true
4367 return ConstantInt::get(Ty: RetTy, V: IsMax);
4368 case FCmpInst::FCMP_OLE:
4369 case FCmpInst::FCMP_ULE:
4370 case FCmpInst::FCMP_OLT:
4371 case FCmpInst::FCMP_ULT:
4372 // minnum(X, LesserC) <= C --> true
4373 // minnum(X, LesserC) < C --> true
4374 // maxnum(X, GreaterC) <= C --> false
4375 // maxnum(X, GreaterC) < C --> false
4376 return ConstantInt::get(Ty: RetTy, V: !IsMax);
4377 default:
4378 // TRUE/FALSE/ORD/UNO should be handled before this.
4379 llvm_unreachable("Unexpected fcmp predicate");
4380 }
4381 }
4382 }
4383
4384 // TODO: Could fold this with above if there were a matcher which returned all
4385 // classes in a non-splat vector.
4386 if (match(V: RHS, P: m_AnyZeroFP())) {
4387 switch (Pred) {
4388 case FCmpInst::FCMP_OGE:
4389 case FCmpInst::FCMP_ULT: {
4390 FPClassTest Interested = KnownFPClass::OrderedLessThanZeroMask;
4391 if (!FMF.noNaNs())
4392 Interested |= fcNan;
4393
4394 KnownFPClass Known = computeLHSClass(Interested);
4395
4396 // Positive or zero X >= 0.0 --> true
4397 // Positive or zero X < 0.0 --> false
4398 if ((FMF.noNaNs() || Known.isKnownNeverNaN()) &&
4399 Known.cannotBeOrderedLessThanZero())
4400 return Pred == FCmpInst::FCMP_OGE ? getTrue(Ty: RetTy) : getFalse(Ty: RetTy);
4401 break;
4402 }
4403 case FCmpInst::FCMP_UGE:
4404 case FCmpInst::FCMP_OLT: {
4405 FPClassTest Interested = KnownFPClass::OrderedLessThanZeroMask;
4406 KnownFPClass Known = computeLHSClass(Interested);
4407
4408 // Positive or zero or nan X >= 0.0 --> true
4409 // Positive or zero or nan X < 0.0 --> false
4410 if (Known.cannotBeOrderedLessThanZero())
4411 return Pred == FCmpInst::FCMP_UGE ? getTrue(Ty: RetTy) : getFalse(Ty: RetTy);
4412 break;
4413 }
4414 default:
4415 break;
4416 }
4417 }
4418
4419 // If the comparison is with the result of a select instruction, check whether
4420 // comparing with either branch of the select always yields the same value.
4421 if (isa<SelectInst>(Val: LHS) || isa<SelectInst>(Val: RHS))
4422 if (Value *V = threadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse))
4423 return V;
4424
4425 // If the comparison is with the result of a phi instruction, check whether
4426 // doing the compare with each incoming phi value yields a common result.
4427 if (isa<PHINode>(Val: LHS) || isa<PHINode>(Val: RHS))
4428 if (Value *V = threadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse))
4429 return V;
4430
4431 return nullptr;
4432}
4433
4434Value *llvm::simplifyFCmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS,
4435 FastMathFlags FMF, const SimplifyQuery &Q) {
4436 return ::simplifyFCmpInst(Pred: Predicate, LHS, RHS, FMF, Q, MaxRecurse: RecursionLimit);
4437}
4438
4439static Value *simplifyWithOpsReplaced(Value *V,
4440 ArrayRef<std::pair<Value *, Value *>> Ops,
4441 const SimplifyQuery &Q,
4442 bool AllowRefinement,
4443 SmallVectorImpl<Instruction *> *DropFlags,
4444 unsigned MaxRecurse) {
4445 assert((AllowRefinement || !Q.CanUseUndef) &&
4446 "If AllowRefinement=false then CanUseUndef=false");
4447 for (const auto &OpAndRepOp : Ops) {
4448 // We cannot replace a constant, and shouldn't even try.
4449 if (isa<Constant>(Val: OpAndRepOp.first))
4450 return nullptr;
4451
4452 // Trivial replacement.
4453 if (V == OpAndRepOp.first)
4454 return OpAndRepOp.second;
4455 }
4456
4457 if (!MaxRecurse--)
4458 return nullptr;
4459
4460 auto *I = dyn_cast<Instruction>(Val: V);
4461 if (!I)
4462 return nullptr;
4463
4464 // The arguments of a phi node might refer to a value from a previous
4465 // cycle iteration.
4466 if (isa<PHINode>(Val: I))
4467 return nullptr;
4468
4469 // Don't fold away llvm.is.constant checks based on assumptions.
4470 if (match(V: I, P: m_Intrinsic<Intrinsic::is_constant>()))
4471 return nullptr;
4472
4473 // Don't simplify freeze.
4474 if (isa<FreezeInst>(Val: I))
4475 return nullptr;
4476
4477 for (const auto &OpAndRepOp : Ops) {
4478 // For vector types, the simplification must hold per-lane, so forbid
4479 // potentially cross-lane operations like shufflevector.
4480 if (OpAndRepOp.first->getType()->isVectorTy() &&
4481 !isNotCrossLaneOperation(I))
4482 return nullptr;
4483 }
4484
4485 // Replace Op with RepOp in instruction operands.
4486 SmallVector<Value *, 8> NewOps;
4487 bool AnyReplaced = false;
4488 for (Value *InstOp : I->operands()) {
4489 if (Value *NewInstOp = simplifyWithOpsReplaced(
4490 V: InstOp, Ops, Q, AllowRefinement, DropFlags, MaxRecurse)) {
4491 NewOps.push_back(Elt: NewInstOp);
4492 AnyReplaced = InstOp != NewInstOp;
4493 } else {
4494 NewOps.push_back(Elt: InstOp);
4495 }
4496
4497 // Bail out if any operand is undef and SimplifyQuery disables undef
4498 // simplification. Constant folding currently doesn't respect this option.
4499 if (isa<UndefValue>(Val: NewOps.back()) && !Q.CanUseUndef)
4500 return nullptr;
4501 }
4502
4503 if (!AnyReplaced)
4504 return nullptr;
4505
4506 if (!AllowRefinement) {
4507 // General InstSimplify functions may refine the result, e.g. by returning
4508 // a constant for a potentially poison value. To avoid this, implement only
4509 // a few non-refining but profitable transforms here.
4510
4511 if (auto *BO = dyn_cast<BinaryOperator>(Val: I)) {
4512 unsigned Opcode = BO->getOpcode();
4513 // id op x -> x, x op id -> x
4514 // Exclude floats, because x op id may produce a different NaN value.
4515 if (!BO->getType()->isFPOrFPVectorTy()) {
4516 if (NewOps[0] == ConstantExpr::getBinOpIdentity(Opcode, Ty: I->getType()))
4517 return NewOps[1];
4518 if (NewOps[1] == ConstantExpr::getBinOpIdentity(Opcode, Ty: I->getType(),
4519 /* RHS */ AllowRHSConstant: true))
4520 return NewOps[0];
4521 }
4522
4523 // x & x -> x, x | x -> x
4524 if ((Opcode == Instruction::And || Opcode == Instruction::Or) &&
4525 NewOps[0] == NewOps[1]) {
4526 // or disjoint x, x results in poison.
4527 if (auto *PDI = dyn_cast<PossiblyDisjointInst>(Val: BO)) {
4528 if (PDI->isDisjoint()) {
4529 if (!DropFlags)
4530 return nullptr;
4531 DropFlags->push_back(Elt: BO);
4532 }
4533 }
4534 return NewOps[0];
4535 }
4536
4537 // x - x -> 0, x ^ x -> 0. This is non-refining, because x is non-poison
4538 // by assumption and this case never wraps, so nowrap flags can be
4539 // ignored.
4540 if ((Opcode == Instruction::Sub || Opcode == Instruction::Xor) &&
4541 NewOps[0] == NewOps[1] &&
4542 any_of(Range&: Ops, P: [=](const auto &Rep) { return NewOps[0] == Rep.second; }))
4543 return Constant::getNullValue(Ty: I->getType());
4544
4545 // If we are substituting an absorber constant into a binop and extra
4546 // poison can't leak if we remove the select -- because both operands of
4547 // the binop are based on the same value -- then it may be safe to replace
4548 // the value with the absorber constant. Examples:
4549 // (Op == 0) ? 0 : (Op & -Op) --> Op & -Op
4550 // (Op == 0) ? 0 : (Op * (binop Op, C)) --> Op * (binop Op, C)
4551 // (Op == -1) ? -1 : (Op | (binop C, Op) --> Op | (binop C, Op)
4552 Constant *Absorber = ConstantExpr::getBinOpAbsorber(Opcode, Ty: I->getType());
4553 if ((NewOps[0] == Absorber || NewOps[1] == Absorber) &&
4554 any_of(Range&: Ops,
4555 P: [=](const auto &Rep) { return impliesPoison(BO, Rep.first); }))
4556 return Absorber;
4557 }
4558
4559 if (auto *II = dyn_cast<IntrinsicInst>(Val: I)) {
4560 // `x == y ? 0 : ucmp(x, y)` where under the replacement y -> x,
4561 // `ucmp(x, x)` becomes `0`.
4562 if ((II->getIntrinsicID() == Intrinsic::scmp ||
4563 II->getIntrinsicID() == Intrinsic::ucmp) &&
4564 NewOps[0] == NewOps[1]) {
4565 if (II->hasPoisonGeneratingAnnotations()) {
4566 if (!DropFlags)
4567 return nullptr;
4568
4569 DropFlags->push_back(Elt: II);
4570 }
4571
4572 return ConstantInt::get(Ty: I->getType(), V: 0);
4573 }
4574
4575 if (auto *MMI = dyn_cast<MinMaxIntrinsic>(Val: II)) {
4576 const APInt Identity = MMI->getIdentity();
4577
4578 Value *Result = nullptr;
4579 if (match(V: NewOps[0], P: m_SpecificInt(V: Identity)))
4580 Result = NewOps[1];
4581 else if (match(V: NewOps[1], P: m_SpecificInt(V: Identity)))
4582 Result = NewOps[0];
4583
4584 if (Result) {
4585 if (II->hasPoisonGeneratingAnnotations()) {
4586 if (!DropFlags)
4587 return nullptr;
4588
4589 DropFlags->push_back(Elt: II);
4590 }
4591
4592 return Result;
4593 }
4594 }
4595 }
4596
4597 if (isa<GetElementPtrInst>(Val: I)) {
4598 // getelementptr x, 0 -> x.
4599 // This never returns poison, even if inbounds is set.
4600 if (NewOps.size() == 2 && match(V: NewOps[1], P: m_Zero()))
4601 return NewOps[0];
4602 }
4603 } else {
4604 // The simplification queries below may return the original value. Consider:
4605 // %div = udiv i32 %arg, %arg2
4606 // %mul = mul nsw i32 %div, %arg2
4607 // %cmp = icmp eq i32 %mul, %arg
4608 // %sel = select i1 %cmp, i32 %div, i32 undef
4609 // Replacing %arg by %mul, %div becomes "udiv i32 %mul, %arg2", which
4610 // simplifies back to %arg. This can only happen because %mul does not
4611 // dominate %div. To ensure a consistent return value contract, we make sure
4612 // that this case returns nullptr as well.
4613 auto PreventSelfSimplify = [V](Value *Simplified) {
4614 return Simplified != V ? Simplified : nullptr;
4615 };
4616
4617 return PreventSelfSimplify(
4618 ::simplifyInstructionWithOperands(I, NewOps, SQ: Q, MaxRecurse));
4619 }
4620
4621 // If all operands are constant after substituting Op for RepOp then we can
4622 // constant fold the instruction.
4623 SmallVector<Constant *, 8> ConstOps;
4624 for (Value *NewOp : NewOps) {
4625 if (Constant *ConstOp = dyn_cast<Constant>(Val: NewOp))
4626 ConstOps.push_back(Elt: ConstOp);
4627 else
4628 return nullptr;
4629 }
4630
4631 // Consider:
4632 // %cmp = icmp eq i32 %x, 2147483647
4633 // %add = add nsw i32 %x, 1
4634 // %sel = select i1 %cmp, i32 -2147483648, i32 %add
4635 //
4636 // We can't replace %sel with %add unless we strip away the flags (which
4637 // will be done in InstCombine).
4638 // TODO: This may be unsound, because it only catches some forms of
4639 // refinement.
4640 if (!AllowRefinement) {
4641 auto *II = dyn_cast<IntrinsicInst>(Val: I);
4642 if (canCreatePoison(Op: cast<Operator>(Val: I), ConsiderFlagsAndMetadata: !DropFlags)) {
4643 // abs cannot create poison if the value is known to never be int_min.
4644 if (II && II->getIntrinsicID() == Intrinsic::abs) {
4645 if (!ConstOps[0]->isNotMinSignedValue())
4646 return nullptr;
4647 } else
4648 return nullptr;
4649 }
4650
4651 if (DropFlags && II) {
4652 // If we're going to change the poison flag of abs/ctz to false, also
4653 // perform constant folding that way, so we get an integer instead of a
4654 // poison value here.
4655 switch (II->getIntrinsicID()) {
4656 case Intrinsic::abs:
4657 case Intrinsic::ctlz:
4658 case Intrinsic::cttz:
4659 ConstOps[1] = ConstantInt::getFalse(Context&: I->getContext());
4660 break;
4661 default:
4662 break;
4663 }
4664 }
4665
4666 Constant *Res = ConstantFoldInstOperands(I, Ops: ConstOps, DL: Q.DL, TLI: Q.TLI,
4667 /*AllowNonDeterministic=*/false);
4668 if (DropFlags && Res && I->hasPoisonGeneratingAnnotations())
4669 DropFlags->push_back(Elt: I);
4670 return Res;
4671 }
4672
4673 return ConstantFoldInstOperands(I, Ops: ConstOps, DL: Q.DL, TLI: Q.TLI,
4674 /*AllowNonDeterministic=*/false);
4675}
4676
4677static Value *simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,
4678 const SimplifyQuery &Q,
4679 bool AllowRefinement,
4680 SmallVectorImpl<Instruction *> *DropFlags,
4681 unsigned MaxRecurse) {
4682 return simplifyWithOpsReplaced(V, Ops: {{Op, RepOp}}, Q, AllowRefinement,
4683 DropFlags, MaxRecurse);
4684}
4685
4686Value *llvm::simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,
4687 const SimplifyQuery &Q,
4688 bool AllowRefinement,
4689 SmallVectorImpl<Instruction *> *DropFlags) {
4690 // If refinement is disabled, also disable undef simplifications (which are
4691 // always refinements) in SimplifyQuery.
4692 if (!AllowRefinement)
4693 return ::simplifyWithOpReplaced(V, Op, RepOp, Q: Q.getWithoutUndef(),
4694 AllowRefinement, DropFlags, MaxRecurse: RecursionLimit);
4695 return ::simplifyWithOpReplaced(V, Op, RepOp, Q, AllowRefinement, DropFlags,
4696 MaxRecurse: RecursionLimit);
4697}
4698
4699/// Try to simplify a select instruction when its condition operand is an
4700/// integer comparison where one operand of the compare is a constant.
4701static Value *simplifySelectBitTest(Value *TrueVal, Value *FalseVal, Value *X,
4702 const APInt *Y, bool TrueWhenUnset) {
4703 const APInt *C;
4704
4705 // (X & Y) == 0 ? X & ~Y : X --> X
4706 // (X & Y) != 0 ? X & ~Y : X --> X & ~Y
4707 if (FalseVal == X && match(V: TrueVal, P: m_And(L: m_Specific(V: X), R: m_APInt(Res&: C))) &&
4708 *Y == ~*C)
4709 return TrueWhenUnset ? FalseVal : TrueVal;
4710
4711 // (X & Y) == 0 ? X : X & ~Y --> X & ~Y
4712 // (X & Y) != 0 ? X : X & ~Y --> X
4713 if (TrueVal == X && match(V: FalseVal, P: m_And(L: m_Specific(V: X), R: m_APInt(Res&: C))) &&
4714 *Y == ~*C)
4715 return TrueWhenUnset ? FalseVal : TrueVal;
4716
4717 if (Y->isPowerOf2()) {
4718 // (X & Y) == 0 ? X | Y : X --> X | Y
4719 // (X & Y) != 0 ? X | Y : X --> X
4720 if (FalseVal == X && match(V: TrueVal, P: m_Or(L: m_Specific(V: X), R: m_APInt(Res&: C))) &&
4721 *Y == *C) {
4722 // We can't return the or if it has the disjoint flag.
4723 if (TrueWhenUnset && cast<PossiblyDisjointInst>(Val: TrueVal)->isDisjoint())
4724 return nullptr;
4725 return TrueWhenUnset ? TrueVal : FalseVal;
4726 }
4727
4728 // (X & Y) == 0 ? X : X | Y --> X
4729 // (X & Y) != 0 ? X : X | Y --> X | Y
4730 if (TrueVal == X && match(V: FalseVal, P: m_Or(L: m_Specific(V: X), R: m_APInt(Res&: C))) &&
4731 *Y == *C) {
4732 // We can't return the or if it has the disjoint flag.
4733 if (!TrueWhenUnset && cast<PossiblyDisjointInst>(Val: FalseVal)->isDisjoint())
4734 return nullptr;
4735 return TrueWhenUnset ? TrueVal : FalseVal;
4736 }
4737 }
4738
4739 return nullptr;
4740}
4741
4742static Value *simplifyCmpSelOfMaxMin(Value *CmpLHS, Value *CmpRHS,
4743 CmpPredicate Pred, Value *TVal,
4744 Value *FVal) {
4745 // Canonicalize common cmp+sel operand as CmpLHS.
4746 if (CmpRHS == TVal || CmpRHS == FVal) {
4747 std::swap(a&: CmpLHS, b&: CmpRHS);
4748 Pred = ICmpInst::getSwappedPredicate(pred: Pred);
4749 }
4750
4751 // Canonicalize common cmp+sel operand as TVal.
4752 if (CmpLHS == FVal) {
4753 std::swap(a&: TVal, b&: FVal);
4754 Pred = ICmpInst::getInversePredicate(pred: Pred);
4755 }
4756
4757 // A vector select may be shuffling together elements that are equivalent
4758 // based on the max/min/select relationship.
4759 Value *X = CmpLHS, *Y = CmpRHS;
4760 bool PeekedThroughSelectShuffle = false;
4761 auto *Shuf = dyn_cast<ShuffleVectorInst>(Val: FVal);
4762 if (Shuf && Shuf->isSelect()) {
4763 if (Shuf->getOperand(i_nocapture: 0) == Y)
4764 FVal = Shuf->getOperand(i_nocapture: 1);
4765 else if (Shuf->getOperand(i_nocapture: 1) == Y)
4766 FVal = Shuf->getOperand(i_nocapture: 0);
4767 else
4768 return nullptr;
4769 PeekedThroughSelectShuffle = true;
4770 }
4771
4772 // (X pred Y) ? X : max/min(X, Y)
4773 auto *MMI = dyn_cast<MinMaxIntrinsic>(Val: FVal);
4774 if (!MMI || TVal != X ||
4775 !match(V: FVal, P: m_c_MaxOrMin(L: m_Specific(V: X), R: m_Specific(V: Y))))
4776 return nullptr;
4777
4778 // (X > Y) ? X : max(X, Y) --> max(X, Y)
4779 // (X >= Y) ? X : max(X, Y) --> max(X, Y)
4780 // (X < Y) ? X : min(X, Y) --> min(X, Y)
4781 // (X <= Y) ? X : min(X, Y) --> min(X, Y)
4782 //
4783 // The equivalence allows a vector select (shuffle) of max/min and Y. Ex:
4784 // (X > Y) ? X : (Z ? max(X, Y) : Y)
4785 // If Z is true, this reduces as above, and if Z is false:
4786 // (X > Y) ? X : Y --> max(X, Y)
4787 ICmpInst::Predicate MMPred = MMI->getPredicate();
4788 if (MMPred == CmpInst::getStrictPredicate(pred: Pred))
4789 return MMI;
4790
4791 // Other transforms are not valid with a shuffle.
4792 if (PeekedThroughSelectShuffle)
4793 return nullptr;
4794
4795 // (X == Y) ? X : max/min(X, Y) --> max/min(X, Y)
4796 if (Pred == CmpInst::ICMP_EQ)
4797 return MMI;
4798
4799 // (X != Y) ? X : max/min(X, Y) --> X
4800 if (Pred == CmpInst::ICMP_NE)
4801 return X;
4802
4803 // (X < Y) ? X : max(X, Y) --> X
4804 // (X <= Y) ? X : max(X, Y) --> X
4805 // (X > Y) ? X : min(X, Y) --> X
4806 // (X >= Y) ? X : min(X, Y) --> X
4807 ICmpInst::Predicate InvPred = CmpInst::getInversePredicate(pred: Pred);
4808 if (MMPred == CmpInst::getStrictPredicate(pred: InvPred))
4809 return X;
4810
4811 return nullptr;
4812}
4813
4814/// An alternative way to test if a bit is set or not.
4815/// uses e.g. sgt/slt or trunc instead of eq/ne.
4816static Value *simplifySelectWithBitTest(Value *CondVal, Value *TrueVal,
4817 Value *FalseVal) {
4818 if (auto Res = decomposeBitTest(Cond: CondVal))
4819 return simplifySelectBitTest(TrueVal, FalseVal, X: Res->X, Y: &Res->Mask,
4820 TrueWhenUnset: Res->Pred == ICmpInst::ICMP_EQ);
4821
4822 return nullptr;
4823}
4824
4825/// Try to simplify a select instruction when its condition operand is an
4826/// integer equality or floating-point equivalence comparison.
4827static Value *simplifySelectWithEquivalence(
4828 ArrayRef<std::pair<Value *, Value *>> Replacements, Value *TrueVal,
4829 Value *FalseVal, const SimplifyQuery &Q, unsigned MaxRecurse) {
4830 Value *SimplifiedFalseVal =
4831 simplifyWithOpsReplaced(V: FalseVal, Ops: Replacements, Q: Q.getWithoutUndef(),
4832 /* AllowRefinement */ false,
4833 /* DropFlags */ nullptr, MaxRecurse);
4834 if (!SimplifiedFalseVal)
4835 SimplifiedFalseVal = FalseVal;
4836
4837 Value *SimplifiedTrueVal =
4838 simplifyWithOpsReplaced(V: TrueVal, Ops: Replacements, Q,
4839 /* AllowRefinement */ true,
4840 /* DropFlags */ nullptr, MaxRecurse);
4841 if (!SimplifiedTrueVal)
4842 SimplifiedTrueVal = TrueVal;
4843
4844 if (SimplifiedFalseVal == SimplifiedTrueVal)
4845 return FalseVal;
4846
4847 return nullptr;
4848}
4849
4850/// Try to simplify a select instruction when its condition operand is an
4851/// integer comparison.
4852static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal,
4853 Value *FalseVal,
4854 const SimplifyQuery &Q,
4855 unsigned MaxRecurse) {
4856 CmpPredicate Pred;
4857 Value *CmpLHS, *CmpRHS;
4858 if (!match(V: CondVal, P: m_ICmp(Pred, L: m_Value(V&: CmpLHS), R: m_Value(V&: CmpRHS))))
4859 return nullptr;
4860
4861 if (Value *V = simplifyCmpSelOfMaxMin(CmpLHS, CmpRHS, Pred, TVal: TrueVal, FVal: FalseVal))
4862 return V;
4863
4864 // Canonicalize ne to eq predicate.
4865 if (Pred == ICmpInst::ICMP_NE) {
4866 Pred = ICmpInst::ICMP_EQ;
4867 std::swap(a&: TrueVal, b&: FalseVal);
4868 }
4869
4870 // Check for integer min/max with a limit constant:
4871 // X > MIN_INT ? X : MIN_INT --> X
4872 // X < MAX_INT ? X : MAX_INT --> X
4873 if (TrueVal->getType()->isIntOrIntVectorTy()) {
4874 Value *X, *Y;
4875 SelectPatternFlavor SPF =
4876 matchDecomposedSelectPattern(CmpI: cast<ICmpInst>(Val: CondVal), TrueVal, FalseVal,
4877 LHS&: X, RHS&: Y)
4878 .Flavor;
4879 if (SelectPatternResult::isMinOrMax(SPF) && Pred == getMinMaxPred(SPF)) {
4880 APInt LimitC = getMinMaxLimit(SPF: getInverseMinMaxFlavor(SPF),
4881 BitWidth: X->getType()->getScalarSizeInBits());
4882 if (match(V: Y, P: m_SpecificInt(V: LimitC)))
4883 return X;
4884 }
4885 }
4886
4887 if (Pred == ICmpInst::ICMP_EQ && match(V: CmpRHS, P: m_Zero())) {
4888 Value *X;
4889 const APInt *Y;
4890 if (match(V: CmpLHS, P: m_And(L: m_Value(V&: X), R: m_APInt(Res&: Y))))
4891 if (Value *V = simplifySelectBitTest(TrueVal, FalseVal, X, Y,
4892 /*TrueWhenUnset=*/true))
4893 return V;
4894
4895 // Test for a bogus zero-shift-guard-op around funnel-shift or rotate.
4896 Value *ShAmt;
4897 auto isFsh = m_CombineOr(Ps: m_FShl(Op0: m_Value(V&: X), Op1: m_Value(), Op2: m_Value(V&: ShAmt)),
4898 Ps: m_FShr(Op0: m_Value(), Op1: m_Value(V&: X), Op2: m_Value(V&: ShAmt)));
4899 // (ShAmt == 0) ? fshl(X, *, ShAmt) : X --> X
4900 // (ShAmt == 0) ? fshr(*, X, ShAmt) : X --> X
4901 if (match(V: TrueVal, P: isFsh) && FalseVal == X && CmpLHS == ShAmt)
4902 return X;
4903
4904 // Test for a zero-shift-guard-op around rotates. These are used to
4905 // avoid UB from oversized shifts in raw IR rotate patterns, but the
4906 // intrinsics do not have that problem.
4907 // We do not allow this transform for the general funnel shift case because
4908 // that would not preserve the poison safety of the original code.
4909 auto isRotate =
4910 m_CombineOr(Ps: m_FShl(Op0: m_Value(V&: X), Op1: m_Deferred(V: X), Op2: m_Value(V&: ShAmt)),
4911 Ps: m_FShr(Op0: m_Value(V&: X), Op1: m_Deferred(V: X), Op2: m_Value(V&: ShAmt)));
4912 if (match(V: FalseVal, P: isRotate) && TrueVal == X) {
4913 // (ShAmt == 0) ? X : fshl(X, X, ShAmt) --> fshl(X, X, ShAmt)
4914 // (ShAmt == 0) ? X : fshr(X, X, ShAmt) --> fshr(X, X, ShAmt)
4915 if (CmpLHS == ShAmt)
4916 return FalseVal;
4917 // Compute the bitwidth of the value being rotated.
4918 unsigned BW = X->getType()->getScalarSizeInBits();
4919 // Handle the cases where the expression to be checked for zero is not the
4920 // shift amount but the `shAmt % bitwidth` which is equivalent to `shAmt &
4921 // (bitwidth - 1)` provided the bitwidth is a power of 2.
4922 //
4923 // ((ShAmt & (BW-1)) == 0) ? X : fshl(X, X, ShAmt) --> fshl(X, X, ShAmt)
4924 // ((ShAmt & (BW-1)) == 0) ? X : fshr(X, X, ShAmt) --> fshr(X, X, ShAmt)
4925 if (isPowerOf2_32(Value: BW) &&
4926 match(V: CmpLHS, P: m_c_And(L: m_Specific(V: ShAmt), R: m_SpecificInt(V: BW - 1))))
4927 return FalseVal;
4928 // (ShAmt % BW == 0) ? X : fshl(X, X, ShAmt) --> fshl(X, X, ShAmt)
4929 // (ShAmt % BW == 0) ? X : fshr(X, X, ShAmt) --> fshr(X, X, ShAmt)
4930 if (match(V: CmpLHS, P: m_URem(L: m_Specific(V: ShAmt), R: m_SpecificInt(V: BW))))
4931 return FalseVal;
4932 }
4933
4934 // X == 0 ? abs(X) : -abs(X) --> -abs(X)
4935 // X == 0 ? -abs(X) : abs(X) --> abs(X)
4936 if (match(V: TrueVal, P: m_Intrinsic<Intrinsic::abs>(Ops: m_Specific(V: CmpLHS))) &&
4937 match(V: FalseVal, P: m_Neg(V: m_Intrinsic<Intrinsic::abs>(Ops: m_Specific(V: CmpLHS)))))
4938 return FalseVal;
4939 if (match(V: TrueVal,
4940 P: m_Neg(V: m_Intrinsic<Intrinsic::abs>(Ops: m_Specific(V: CmpLHS)))) &&
4941 match(V: FalseVal, P: m_Intrinsic<Intrinsic::abs>(Ops: m_Specific(V: CmpLHS))))
4942 return FalseVal;
4943 }
4944
4945 // If we have a scalar equality comparison, then we know the value in one of
4946 // the arms of the select. See if substituting this value into the arm and
4947 // simplifying the result yields the same value as the other arm.
4948 if (Pred == ICmpInst::ICMP_EQ) {
4949 if (CmpLHS->getType()->isIntOrIntVectorTy() ||
4950 canReplacePointersIfEqual(From: CmpLHS, To: CmpRHS, DL: Q.DL))
4951 if (Value *V = simplifySelectWithEquivalence(Replacements: {{CmpLHS, CmpRHS}}, TrueVal,
4952 FalseVal, Q, MaxRecurse))
4953 return V;
4954 if (CmpLHS->getType()->isIntOrIntVectorTy() ||
4955 canReplacePointersIfEqual(From: CmpRHS, To: CmpLHS, DL: Q.DL))
4956 if (Value *V = simplifySelectWithEquivalence(Replacements: {{CmpRHS, CmpLHS}}, TrueVal,
4957 FalseVal, Q, MaxRecurse))
4958 return V;
4959
4960 Value *X;
4961 Value *Y;
4962 // select((X | Y) == 0 ? X : 0) --> 0 (commuted 2 ways)
4963 if (match(V: CmpLHS, P: m_Or(L: m_Value(V&: X), R: m_Value(V&: Y))) &&
4964 match(V: CmpRHS, P: m_Zero())) {
4965 // (X | Y) == 0 implies X == 0 and Y == 0.
4966 if (Value *V = simplifySelectWithEquivalence(
4967 Replacements: {{X, CmpRHS}, {Y, CmpRHS}}, TrueVal, FalseVal, Q, MaxRecurse))
4968 return V;
4969 }
4970
4971 // select((X & Y) == -1 ? X : -1) --> -1 (commuted 2 ways)
4972 if (match(V: CmpLHS, P: m_And(L: m_Value(V&: X), R: m_Value(V&: Y))) &&
4973 match(V: CmpRHS, P: m_AllOnes())) {
4974 // (X & Y) == -1 implies X == -1 and Y == -1.
4975 if (Value *V = simplifySelectWithEquivalence(
4976 Replacements: {{X, CmpRHS}, {Y, CmpRHS}}, TrueVal, FalseVal, Q, MaxRecurse))
4977 return V;
4978 }
4979 }
4980
4981 return nullptr;
4982}
4983
4984/// Try to simplify a select instruction when its condition operand is a
4985/// floating-point comparison.
4986static Value *simplifySelectWithFCmp(Value *Cond, Value *T, Value *F,
4987 FastMathFlags FMF, const SimplifyQuery &Q,
4988 unsigned MaxRecurse) {
4989 CmpPredicate Pred;
4990 Value *CmpLHS, *CmpRHS;
4991 if (!match(V: Cond, P: m_FCmp(Pred, L: m_Value(V&: CmpLHS), R: m_Value(V&: CmpRHS))))
4992 return nullptr;
4993 FCmpInst *I = cast<FCmpInst>(Val: Cond);
4994
4995 bool IsEquiv = I->isEquivalence();
4996 if (I->isEquivalence(/*Invert=*/true)) {
4997 std::swap(a&: T, b&: F);
4998 Pred = FCmpInst::getInversePredicate(pred: Pred);
4999 IsEquiv = true;
5000 }
5001
5002 // This transforms is safe if at least one operand is known to not be zero.
5003 // Otherwise, the select can change the sign of a zero operand.
5004 if (IsEquiv) {
5005 if (Value *V = simplifySelectWithEquivalence(Replacements: {{CmpLHS, CmpRHS}}, TrueVal: T, FalseVal: F, Q,
5006 MaxRecurse))
5007 return V;
5008 if (Value *V = simplifySelectWithEquivalence(Replacements: {{CmpRHS, CmpLHS}}, TrueVal: T, FalseVal: F, Q,
5009 MaxRecurse))
5010 return V;
5011 }
5012
5013 // Canonicalize CmpLHS to be T, and CmpRHS to be F, if they're swapped.
5014 if (CmpLHS == F && CmpRHS == T)
5015 std::swap(a&: CmpLHS, b&: CmpRHS);
5016
5017 if (CmpLHS != T || CmpRHS != F)
5018 return nullptr;
5019
5020 // This transform is also safe if we do not have (do not care about) -0.0.
5021 if (FMF.noSignedZeros()) {
5022 // (T == F) ? T : F --> F
5023 if (Pred == FCmpInst::FCMP_OEQ)
5024 return F;
5025
5026 // (T != F) ? T : F --> T
5027 if (Pred == FCmpInst::FCMP_UNE)
5028 return T;
5029 }
5030
5031 return nullptr;
5032}
5033
5034/// Look for the following pattern and simplify %to_fold to %identicalPhi.
5035/// Here %phi, %to_fold and %phi.next perform the same functionality as
5036/// %identicalPhi and hence the select instruction %to_fold can be folded
5037/// into %identicalPhi.
5038///
5039/// BB1:
5040/// %identicalPhi = phi [ X, %BB0 ], [ %identicalPhi.next, %BB1 ]
5041/// %phi = phi [ X, %BB0 ], [ %phi.next, %BB1 ]
5042/// ...
5043/// %identicalPhi.next = select %cmp, %val, %identicalPhi
5044/// (or select %cmp, %identicalPhi, %val)
5045/// %to_fold = select %cmp2, %identicalPhi, %phi
5046/// %phi.next = select %cmp, %val, %to_fold
5047/// (or select %cmp, %to_fold, %val)
5048///
5049/// Prove that %phi and %identicalPhi are the same by induction:
5050///
5051/// Base case: Both %phi and %identicalPhi are equal on entry to the loop.
5052/// Inductive case:
5053/// Suppose %phi and %identicalPhi are equal at iteration i.
5054/// We look at their values at iteration i+1 which are %phi.next and
5055/// %identicalPhi.next. They would have become different only when %cmp is
5056/// false and the corresponding values %to_fold and %identicalPhi differ
5057/// (similar reason for the other "or" case in the bracket).
5058///
5059/// The only condition when %to_fold and %identicalPh could differ is when %cmp2
5060/// is false and %to_fold is %phi, which contradicts our inductive hypothesis
5061/// that %phi and %identicalPhi are equal. Thus %phi and %identicalPhi are
5062/// always equal at iteration i+1.
5063bool isSelectWithIdenticalPHI(PHINode &PN, PHINode &IdenticalPN) {
5064 if (PN.getParent() != IdenticalPN.getParent())
5065 return false;
5066 if (PN.getNumIncomingValues() != 2)
5067 return false;
5068
5069 // Check that only the backedge incoming value is different.
5070 unsigned DiffVals = 0;
5071 BasicBlock *DiffValBB = nullptr;
5072 for (unsigned i = 0; i < 2; i++) {
5073 BasicBlock *PredBB = PN.getIncomingBlock(i);
5074 if (PN.getIncomingValue(i) !=
5075 IdenticalPN.getIncomingValueForBlock(BB: PredBB)) {
5076 DiffVals++;
5077 DiffValBB = PredBB;
5078 }
5079 }
5080 if (DiffVals != 1)
5081 return false;
5082 // Now check that the backedge incoming values are two select
5083 // instructions with the same condition. Either their true
5084 // values are the same, or their false values are the same.
5085 auto *SI = dyn_cast<SelectInst>(Val: PN.getIncomingValueForBlock(BB: DiffValBB));
5086 auto *IdenticalSI =
5087 dyn_cast<SelectInst>(Val: IdenticalPN.getIncomingValueForBlock(BB: DiffValBB));
5088 if (!SI || !IdenticalSI)
5089 return false;
5090 if (SI->getCondition() != IdenticalSI->getCondition())
5091 return false;
5092
5093 SelectInst *SIOtherVal = nullptr;
5094 Value *IdenticalSIOtherVal = nullptr;
5095 if (SI->getTrueValue() == IdenticalSI->getTrueValue()) {
5096 SIOtherVal = dyn_cast<SelectInst>(Val: SI->getFalseValue());
5097 IdenticalSIOtherVal = IdenticalSI->getFalseValue();
5098 } else if (SI->getFalseValue() == IdenticalSI->getFalseValue()) {
5099 SIOtherVal = dyn_cast<SelectInst>(Val: SI->getTrueValue());
5100 IdenticalSIOtherVal = IdenticalSI->getTrueValue();
5101 } else {
5102 return false;
5103 }
5104
5105 // Now check that the other values in select, i.e., %to_fold and
5106 // %identicalPhi, are essentially the same value.
5107 if (!SIOtherVal || IdenticalSIOtherVal != &IdenticalPN)
5108 return false;
5109 if (!(SIOtherVal->getTrueValue() == &IdenticalPN &&
5110 SIOtherVal->getFalseValue() == &PN) &&
5111 !(SIOtherVal->getTrueValue() == &PN &&
5112 SIOtherVal->getFalseValue() == &IdenticalPN))
5113 return false;
5114 return true;
5115}
5116
5117/// Given operands for a SelectInst, see if we can fold the result.
5118/// If not, this returns null.
5119static Value *simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
5120 FastMathFlags FMF, const SimplifyQuery &Q,
5121 unsigned MaxRecurse) {
5122 if (auto *CondC = dyn_cast<Constant>(Val: Cond)) {
5123 if (auto *TrueC = dyn_cast<Constant>(Val: TrueVal))
5124 if (auto *FalseC = dyn_cast<Constant>(Val: FalseVal))
5125 if (Constant *C = ConstantFoldSelectInstruction(Cond: CondC, V1: TrueC, V2: FalseC))
5126 return C;
5127
5128 // select poison, X, Y -> poison
5129 if (isa<PoisonValue>(Val: CondC))
5130 return PoisonValue::get(T: TrueVal->getType());
5131
5132 // select undef, X, Y -> X or Y
5133 if (Q.isUndefValue(V: CondC))
5134 return isa<Constant>(Val: FalseVal) ? FalseVal : TrueVal;
5135
5136 // select true, X, Y --> X
5137 // select false, X, Y --> Y
5138 // For vectors, allow undef/poison elements in the condition to match the
5139 // defined elements, so we can eliminate the select.
5140 if (match(V: CondC, P: m_One()))
5141 return TrueVal;
5142 if (match(V: CondC, P: m_Zero()))
5143 return FalseVal;
5144 }
5145
5146 assert(Cond->getType()->isIntOrIntVectorTy(1) &&
5147 "Select must have bool or bool vector condition");
5148 assert(TrueVal->getType() == FalseVal->getType() &&
5149 "Select must have same types for true/false ops");
5150
5151 if (Cond->getType() == TrueVal->getType()) {
5152 // select i1 Cond, i1 true, i1 false --> i1 Cond
5153 if (match(V: TrueVal, P: m_One()) && match(V: FalseVal, P: m_ZeroInt()))
5154 return Cond;
5155
5156 // (X && Y) ? X : Y --> Y (commuted 2 ways)
5157 if (match(V: Cond, P: m_c_LogicalAnd(L: m_Specific(V: TrueVal), R: m_Specific(V: FalseVal))))
5158 return FalseVal;
5159
5160 // (X || Y) ? X : Y --> X (commuted 2 ways)
5161 if (match(V: Cond, P: m_c_LogicalOr(L: m_Specific(V: TrueVal), R: m_Specific(V: FalseVal))))
5162 return TrueVal;
5163
5164 // (X || Y) ? false : X --> false (commuted 2 ways)
5165 if (match(V: Cond, P: m_c_LogicalOr(L: m_Specific(V: FalseVal), R: m_Value())) &&
5166 match(V: TrueVal, P: m_ZeroInt()))
5167 return ConstantInt::getFalse(Ty: Cond->getType());
5168
5169 // Match patterns that end in logical-and.
5170 if (match(V: FalseVal, P: m_ZeroInt())) {
5171 // !(X || Y) && X --> false (commuted 2 ways)
5172 if (match(V: Cond, P: m_Not(V: m_c_LogicalOr(L: m_Specific(V: TrueVal), R: m_Value()))))
5173 return ConstantInt::getFalse(Ty: Cond->getType());
5174 // X && !(X || Y) --> false (commuted 2 ways)
5175 if (match(V: TrueVal, P: m_Not(V: m_c_LogicalOr(L: m_Specific(V: Cond), R: m_Value()))))
5176 return ConstantInt::getFalse(Ty: Cond->getType());
5177
5178 // (X || Y) && Y --> Y (commuted 2 ways)
5179 if (match(V: Cond, P: m_c_LogicalOr(L: m_Specific(V: TrueVal), R: m_Value())))
5180 return TrueVal;
5181 // Y && (X || Y) --> Y (commuted 2 ways)
5182 if (match(V: TrueVal, P: m_c_LogicalOr(L: m_Specific(V: Cond), R: m_Value())))
5183 return Cond;
5184
5185 // (X || Y) && (X || !Y) --> X (commuted 8 ways)
5186 Value *X, *Y;
5187 if (match(V: Cond, P: m_c_LogicalOr(L: m_Value(V&: X), R: m_Not(V: m_Value(V&: Y)))) &&
5188 match(V: TrueVal, P: m_c_LogicalOr(L: m_Specific(V: X), R: m_Specific(V: Y))))
5189 return X;
5190 if (match(V: TrueVal, P: m_c_LogicalOr(L: m_Value(V&: X), R: m_Not(V: m_Value(V&: Y)))) &&
5191 match(V: Cond, P: m_c_LogicalOr(L: m_Specific(V: X), R: m_Specific(V: Y))))
5192 return X;
5193 }
5194
5195 // Match patterns that end in logical-or.
5196 if (match(V: TrueVal, P: m_One())) {
5197 // !(X && Y) || X --> true (commuted 2 ways)
5198 if (match(V: Cond, P: m_Not(V: m_c_LogicalAnd(L: m_Specific(V: FalseVal), R: m_Value()))))
5199 return ConstantInt::getTrue(Ty: Cond->getType());
5200 // X || !(X && Y) --> true (commuted 2 ways)
5201 if (match(V: FalseVal, P: m_Not(V: m_c_LogicalAnd(L: m_Specific(V: Cond), R: m_Value()))))
5202 return ConstantInt::getTrue(Ty: Cond->getType());
5203
5204 // (X && Y) || Y --> Y (commuted 2 ways)
5205 if (match(V: Cond, P: m_c_LogicalAnd(L: m_Specific(V: FalseVal), R: m_Value())))
5206 return FalseVal;
5207 // Y || (X && Y) --> Y (commuted 2 ways)
5208 if (match(V: FalseVal, P: m_c_LogicalAnd(L: m_Specific(V: Cond), R: m_Value())))
5209 return Cond;
5210 }
5211 }
5212
5213 // select ?, X, X -> X
5214 if (TrueVal == FalseVal)
5215 return TrueVal;
5216
5217 if (Cond == TrueVal) {
5218 // select i1 X, i1 X, i1 false --> X (logical-and)
5219 if (match(V: FalseVal, P: m_ZeroInt()))
5220 return Cond;
5221 // select i1 X, i1 X, i1 true --> true
5222 if (match(V: FalseVal, P: m_One()))
5223 return ConstantInt::getTrue(Ty: Cond->getType());
5224 }
5225 if (Cond == FalseVal) {
5226 // select i1 X, i1 true, i1 X --> X (logical-or)
5227 if (match(V: TrueVal, P: m_One()))
5228 return Cond;
5229 // select i1 X, i1 false, i1 X --> false
5230 if (match(V: TrueVal, P: m_ZeroInt()))
5231 return ConstantInt::getFalse(Ty: Cond->getType());
5232 }
5233
5234 // If the true or false value is poison, we can fold to the other value.
5235 // If the true or false value is undef, we can fold to the other value as
5236 // long as the other value isn't poison.
5237 // select ?, poison, X -> X
5238 // select ?, undef, X -> X
5239 if (isa<PoisonValue>(Val: TrueVal) ||
5240 (Q.isUndefValue(V: TrueVal) && impliesPoison(ValAssumedPoison: FalseVal, V: Cond)))
5241 return FalseVal;
5242 // select ?, X, poison -> X
5243 // select ?, X, undef -> X
5244 if (isa<PoisonValue>(Val: FalseVal) ||
5245 (Q.isUndefValue(V: FalseVal) && impliesPoison(ValAssumedPoison: TrueVal, V: Cond)))
5246 return TrueVal;
5247
5248 // Deal with partial undef vector constants: select ?, VecC, VecC' --> VecC''
5249 Constant *TrueC, *FalseC;
5250 if (isa<FixedVectorType>(Val: TrueVal->getType()) &&
5251 match(V: TrueVal, P: m_Constant(C&: TrueC)) &&
5252 match(V: FalseVal, P: m_Constant(C&: FalseC))) {
5253 unsigned NumElts =
5254 cast<FixedVectorType>(Val: TrueC->getType())->getNumElements();
5255 SmallVector<Constant *, 16> NewC;
5256 for (unsigned i = 0; i != NumElts; ++i) {
5257 // Bail out on incomplete vector constants.
5258 Constant *TEltC = TrueC->getAggregateElement(Elt: i);
5259 Constant *FEltC = FalseC->getAggregateElement(Elt: i);
5260 if (!TEltC || !FEltC)
5261 break;
5262
5263 // If the elements match (undef or not), that value is the result. If only
5264 // one element is undef, choose the defined element as the safe result.
5265 if (TEltC == FEltC)
5266 NewC.push_back(Elt: TEltC);
5267 else if (isa<PoisonValue>(Val: TEltC) ||
5268 (Q.isUndefValue(V: TEltC) && isGuaranteedNotToBePoison(V: FEltC)))
5269 NewC.push_back(Elt: FEltC);
5270 else if (isa<PoisonValue>(Val: FEltC) ||
5271 (Q.isUndefValue(V: FEltC) && isGuaranteedNotToBePoison(V: TEltC)))
5272 NewC.push_back(Elt: TEltC);
5273 else
5274 break;
5275 }
5276 if (NewC.size() == NumElts)
5277 return ConstantVector::get(V: NewC);
5278 }
5279
5280 if (Value *V =
5281 simplifySelectWithICmpCond(CondVal: Cond, TrueVal, FalseVal, Q, MaxRecurse))
5282 return V;
5283
5284 if (Value *V = simplifySelectWithBitTest(CondVal: Cond, TrueVal, FalseVal))
5285 return V;
5286
5287 if (Value *V =
5288 simplifySelectWithFCmp(Cond, T: TrueVal, F: FalseVal, FMF, Q, MaxRecurse))
5289 return V;
5290
5291 std::optional<bool> Imp = isImpliedByDomCondition(Cond, ContextI: Q.CtxI, DL: Q.DL);
5292 if (Imp)
5293 return *Imp ? TrueVal : FalseVal;
5294 // Look for same PHIs in the true and false values.
5295 if (auto *TruePHI = dyn_cast<PHINode>(Val: TrueVal))
5296 if (auto *FalsePHI = dyn_cast<PHINode>(Val: FalseVal)) {
5297 if (isSelectWithIdenticalPHI(PN&: *TruePHI, IdenticalPN&: *FalsePHI))
5298 return FalseVal;
5299 if (isSelectWithIdenticalPHI(PN&: *FalsePHI, IdenticalPN&: *TruePHI))
5300 return TrueVal;
5301 }
5302 return nullptr;
5303}
5304
5305Value *llvm::simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
5306 FastMathFlags FMF, const SimplifyQuery &Q) {
5307 return ::simplifySelectInst(Cond, TrueVal, FalseVal, FMF, Q, MaxRecurse: RecursionLimit);
5308}
5309
5310/// Given operands for an GetElementPtrInst, see if we can fold the result.
5311/// If not, this returns null.
5312static Value *simplifyGEPInst(Type *SrcTy, Value *Ptr,
5313 ArrayRef<Value *> Indices, GEPNoWrapFlags NW,
5314 const SimplifyQuery &Q, unsigned) {
5315 // The type of the GEP pointer operand.
5316 unsigned AS =
5317 cast<PointerType>(Val: Ptr->getType()->getScalarType())->getAddressSpace();
5318
5319 // getelementptr P -> P.
5320 if (Indices.empty())
5321 return Ptr;
5322
5323 // Compute the (pointer) type returned by the GEP instruction.
5324 Type *LastType = GetElementPtrInst::getIndexedType(Ty: SrcTy, IdxList: Indices);
5325 Type *GEPTy = Ptr->getType();
5326 if (!GEPTy->isVectorTy()) {
5327 for (Value *Op : Indices) {
5328 // If one of the operands is a vector, the result type is a vector of
5329 // pointers. All vector operands must have the same number of elements.
5330 if (VectorType *VT = dyn_cast<VectorType>(Val: Op->getType())) {
5331 GEPTy = VectorType::get(ElementType: GEPTy, EC: VT->getElementCount());
5332 break;
5333 }
5334 }
5335 }
5336
5337 // All-zero GEP is a no-op, unless it performs a vector splat.
5338 if (Ptr->getType() == GEPTy && all_of(Range&: Indices, P: match_fn(P: m_Zero())))
5339 return Ptr;
5340
5341 // getelementptr poison, idx -> poison
5342 // getelementptr baseptr, poison -> poison
5343 if (isa<PoisonValue>(Val: Ptr) || any_of(Range&: Indices, P: IsaPred<PoisonValue>))
5344 return PoisonValue::get(T: GEPTy);
5345
5346 // getelementptr undef, idx -> undef
5347 if (Q.isUndefValue(V: Ptr))
5348 return UndefValue::get(T: GEPTy);
5349
5350 bool IsScalableVec =
5351 SrcTy->isScalableTy() || any_of(Range&: Indices, P: [](const Value *V) {
5352 return isa<ScalableVectorType>(Val: V->getType());
5353 });
5354
5355 if (Indices.size() == 1) {
5356 Type *Ty = SrcTy;
5357 if (!IsScalableVec && Ty->isSized()) {
5358 Value *P;
5359 uint64_t C;
5360 uint64_t TyAllocSize = Q.DL.getTypeAllocSize(Ty);
5361 // getelementptr P, N -> P if P points to a type of zero size.
5362 if (TyAllocSize == 0 && Ptr->getType() == GEPTy)
5363 return Ptr;
5364
5365 // The following transforms are only safe if the ptrtoint cast
5366 // doesn't truncate the address of the pointers. The non-address bits
5367 // must be the same, as the underlying objects are the same.
5368 if (Indices[0]->getType()->getScalarSizeInBits() >=
5369 Q.DL.getAddressSizeInBits(AS)) {
5370 auto CanSimplify = [GEPTy, &P, Ptr]() -> bool {
5371 return P->getType() == GEPTy &&
5372 getUnderlyingObject(V: P) == getUnderlyingObject(V: Ptr);
5373 };
5374 // getelementptr V, (sub P, V) -> P if P points to a type of size 1.
5375 if (TyAllocSize == 1 &&
5376 match(V: Indices[0], P: m_Sub(L: m_PtrToIntOrAddr(Op: m_Value(V&: P)),
5377 R: m_PtrToIntOrAddr(Op: m_Specific(V: Ptr)))) &&
5378 CanSimplify())
5379 return P;
5380
5381 // getelementptr V, (ashr exact (sub P, V), C) -> P if P points to a
5382 // type of size 1 << C.
5383 if (match(V: Indices[0],
5384 P: m_Exact(SubPattern: m_AShr(L: m_Sub(L: m_PtrToIntOrAddr(Op: m_Value(V&: P)),
5385 R: m_PtrToIntOrAddr(Op: m_Specific(V: Ptr))),
5386 R: m_ConstantInt(V&: C)))) &&
5387 TyAllocSize == 1ULL << C && CanSimplify())
5388 return P;
5389
5390 // getelementptr V, (sdiv exact (sub P, V), C) -> P if P points to a
5391 // type of size C.
5392 if (match(V: Indices[0],
5393 P: m_Exact(SubPattern: m_SDiv(L: m_Sub(L: m_PtrToIntOrAddr(Op: m_Value(V&: P)),
5394 R: m_PtrToIntOrAddr(Op: m_Specific(V: Ptr))),
5395 R: m_SpecificInt(V: TyAllocSize)))) &&
5396 CanSimplify())
5397 return P;
5398 }
5399 }
5400 }
5401
5402 // Match the index before stripping the base below, which is more expensive.
5403 Value *PtrOp;
5404 bool IsNeg = match(V: Indices.back(), P: m_Neg(V: m_PtrToInt(Op: m_Value(V&: PtrOp))));
5405 if ((IsNeg || match(V: Indices.back(), P: m_Not(V: m_PtrToInt(Op: m_Value(V&: PtrOp))))) &&
5406 !IsScalableVec && Q.DL.getTypeAllocSize(Ty: LastType) == 1 &&
5407 all_of(Range: Indices.drop_back(N: 1), P: match_fn(P: m_Zero()))) {
5408 unsigned IdxWidth =
5409 Q.DL.getIndexSizeInBits(AS: Ptr->getType()->getPointerAddressSpace());
5410 if (Q.DL.getTypeSizeInBits(Ty: Indices.back()->getType()) == IdxWidth) {
5411 APInt BasePtrOffset(IdxWidth, 0);
5412 Value *StrippedBasePtr =
5413 Ptr->stripAndAccumulateInBoundsConstantOffsets(DL: Q.DL, Offset&: BasePtrOffset);
5414
5415 // Avoid creating inttoptr of zero here: While LLVMs treatment of
5416 // inttoptr is generally conservative, this particular case is folded to
5417 // a null pointer, which will have incorrect provenance.
5418
5419 // gep (gep V, C), (sub 0, V) -> C
5420 // gep (gep V, C), (xor V, -1) -> C-1
5421 APInt Offset = IsNeg ? BasePtrOffset : BasePtrOffset - 1;
5422 if (PtrOp == StrippedBasePtr && !Offset.isZero()) {
5423 auto *CI = ConstantInt::get(Context&: GEPTy->getContext(), V: Offset);
5424 return ConstantExpr::getIntToPtr(C: CI, Ty: GEPTy);
5425 }
5426 }
5427 }
5428
5429 // Check to see if this is constant foldable.
5430 if (!isa<Constant>(Val: Ptr) || !all_of(Range&: Indices, P: IsaPred<Constant>))
5431 return nullptr;
5432
5433 ArrayRef<Constant *> ConstIdxs =
5434 ArrayRef((Constant *const *)Indices.data(), Indices.size());
5435 auto *ConstGEP = ConstantExpr::getGetElementPtr(
5436 DL: Q.DL, Ty: SrcTy, C: cast<Constant>(Val: Ptr), IdxList: ConstIdxs, NW);
5437 if (!ConstGEP)
5438 return ConstantFoldGetElementPtr(Ty: SrcTy, C: cast<Constant>(Val: Ptr), InRange: std::nullopt,
5439 Idxs: Indices);
5440
5441 return ConstantFoldConstant(C: ConstGEP, DL: Q.DL);
5442}
5443
5444Value *llvm::simplifyGEPInst(Type *SrcTy, Value *Ptr, ArrayRef<Value *> Indices,
5445 GEPNoWrapFlags NW, const SimplifyQuery &Q) {
5446 return ::simplifyGEPInst(SrcTy, Ptr, Indices, NW, Q, RecursionLimit);
5447}
5448
5449/// Given operands for an InsertValueInst, see if we can fold the result.
5450/// If not, this returns null.
5451static Value *simplifyInsertValueInst(Value *Agg, Value *Val,
5452 ArrayRef<unsigned> Idxs,
5453 const SimplifyQuery &Q, unsigned) {
5454 if (Constant *CAgg = dyn_cast<Constant>(Val: Agg))
5455 if (Constant *CVal = dyn_cast<Constant>(Val))
5456 return ConstantFoldInsertValueInstruction(Agg: CAgg, Val: CVal, Idxs);
5457
5458 // insertvalue x, poison, n -> x
5459 // insertvalue x, undef, n -> x if x cannot be poison
5460 if (isa<PoisonValue>(Val) ||
5461 (Q.isUndefValue(V: Val) && isGuaranteedNotToBePoison(V: Agg)))
5462 return Agg;
5463
5464 // insertvalue x, (extractvalue y, n), n
5465 if (ExtractValueInst *EV = dyn_cast<ExtractValueInst>(Val))
5466 if (EV->getAggregateOperand()->getType() == Agg->getType() &&
5467 EV->getIndices() == Idxs) {
5468 // insertvalue poison, (extractvalue y, n), n -> y
5469 // insertvalue undef, (extractvalue y, n), n -> y if y cannot be poison
5470 if (isa<PoisonValue>(Val: Agg) ||
5471 (Q.isUndefValue(V: Agg) &&
5472 isGuaranteedNotToBePoison(V: EV->getAggregateOperand())))
5473 return EV->getAggregateOperand();
5474
5475 // insertvalue y, (extractvalue y, n), n -> y
5476 if (Agg == EV->getAggregateOperand())
5477 return Agg;
5478 }
5479
5480 return nullptr;
5481}
5482
5483Value *llvm::simplifyInsertValueInst(Value *Agg, Value *Val,
5484 ArrayRef<unsigned> Idxs,
5485 const SimplifyQuery &Q) {
5486 return ::simplifyInsertValueInst(Agg, Val, Idxs, Q, RecursionLimit);
5487}
5488
5489Value *llvm::simplifyInsertElementInst(Value *Vec, Value *Val, Value *Idx,
5490 const SimplifyQuery &Q) {
5491 // Try to constant fold.
5492 auto *VecC = dyn_cast<Constant>(Val: Vec);
5493 auto *ValC = dyn_cast<Constant>(Val);
5494 auto *IdxC = dyn_cast<Constant>(Val: Idx);
5495 if (VecC && ValC && IdxC)
5496 return ConstantExpr::getInsertElement(Vec: VecC, Elt: ValC, Idx: IdxC);
5497
5498 // For fixed-length vector, fold into poison if index is out of bounds.
5499 if (auto *CI = dyn_cast<ConstantInt>(Val: Idx)) {
5500 if (isa<FixedVectorType>(Val: Vec->getType()) &&
5501 CI->uge(Num: cast<FixedVectorType>(Val: Vec->getType())->getNumElements()))
5502 return PoisonValue::get(T: Vec->getType());
5503 }
5504
5505 // If index is undef, it might be out of bounds (see above case)
5506 if (Q.isUndefValue(V: Idx))
5507 return PoisonValue::get(T: Vec->getType());
5508
5509 // If the scalar is poison, or it is undef and there is no risk of
5510 // propagating poison from the vector value, simplify to the vector value.
5511 if (isa<PoisonValue>(Val) ||
5512 (Q.isUndefValue(V: Val) && isGuaranteedNotToBePoison(V: Vec)))
5513 return Vec;
5514
5515 // Inserting the splatted value into a constant splat does nothing.
5516 if (VecC && ValC && VecC->getSplatValue() == ValC)
5517 return Vec;
5518
5519 // If we are extracting a value from a vector, then inserting it into the same
5520 // place, that's the input vector:
5521 // insertelt Vec, (extractelt Vec, Idx), Idx --> Vec
5522 if (match(V: Val, P: m_ExtractElt(Val: m_Specific(V: Vec), Idx: m_Specific(V: Idx))))
5523 return Vec;
5524
5525 return nullptr;
5526}
5527
5528/// Given operands for an ExtractValueInst, see if we can fold the result.
5529/// If not, this returns null.
5530static Value *simplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs,
5531 const SimplifyQuery &, unsigned) {
5532 if (auto *CAgg = dyn_cast<Constant>(Val: Agg))
5533 return ConstantFoldExtractValueInstruction(Agg: CAgg, Idxs);
5534
5535 // extractvalue x, (insertvalue y, elt, n), n -> elt
5536 unsigned NumIdxs = Idxs.size();
5537 SmallPtrSet<InsertValueInst *, 8> VisitedSet;
5538 for (auto *IVI = dyn_cast<InsertValueInst>(Val: Agg); IVI != nullptr;
5539 IVI = dyn_cast<InsertValueInst>(Val: IVI->getAggregateOperand())) {
5540 // Protect against insertvalue cycles in unreachable code.
5541 if (!VisitedSet.insert(Ptr: IVI).second)
5542 break;
5543
5544 ArrayRef<unsigned> InsertValueIdxs = IVI->getIndices();
5545 unsigned NumInsertValueIdxs = InsertValueIdxs.size();
5546 unsigned NumCommonIdxs = std::min(a: NumInsertValueIdxs, b: NumIdxs);
5547 if (InsertValueIdxs.slice(N: 0, M: NumCommonIdxs) ==
5548 Idxs.slice(N: 0, M: NumCommonIdxs)) {
5549 if (NumIdxs == NumInsertValueIdxs)
5550 return IVI->getInsertedValueOperand();
5551 break;
5552 }
5553 }
5554
5555 // Simplify umul_with_overflow where one operand is 1.
5556 Value *V;
5557 if (Idxs.size() == 1 &&
5558 (match(V: Agg,
5559 P: m_Intrinsic<Intrinsic::umul_with_overflow>(Ops: m_Value(V), Ops: m_One())) ||
5560 match(V: Agg, P: m_Intrinsic<Intrinsic::umul_with_overflow>(Ops: m_One(),
5561 Ops: m_Value(V))))) {
5562 if (Idxs[0] == 0)
5563 return V;
5564 assert(Idxs[0] == 1 && "invalid index");
5565 return getFalse(Ty: CmpInst::makeCmpResultType(opnd_type: V->getType()));
5566 }
5567
5568 return nullptr;
5569}
5570
5571Value *llvm::simplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs,
5572 const SimplifyQuery &Q) {
5573 return ::simplifyExtractValueInst(Agg, Idxs, Q, RecursionLimit);
5574}
5575
5576/// Given operands for an ExtractElementInst, see if we can fold the result.
5577/// If not, this returns null.
5578static Value *simplifyExtractElementInst(Value *Vec, Value *Idx,
5579 const SimplifyQuery &Q, unsigned) {
5580 auto *VecVTy = cast<VectorType>(Val: Vec->getType());
5581 if (auto *CVec = dyn_cast<Constant>(Val: Vec)) {
5582 if (auto *CIdx = dyn_cast<Constant>(Val: Idx))
5583 return ConstantExpr::getExtractElement(Vec: CVec, Idx: CIdx);
5584
5585 if (Q.isUndefValue(V: Vec))
5586 return UndefValue::get(T: VecVTy->getElementType());
5587 }
5588
5589 // An undef extract index can be arbitrarily chosen to be an out-of-range
5590 // index value, which would result in the instruction being poison.
5591 if (Q.isUndefValue(V: Idx))
5592 return PoisonValue::get(T: VecVTy->getElementType());
5593
5594 // If extracting a specified index from the vector, see if we can recursively
5595 // find a previously computed scalar that was inserted into the vector.
5596 if (auto *IdxC = dyn_cast<ConstantInt>(Val: Idx)) {
5597 // For fixed-length vector, fold into undef if index is out of bounds.
5598 unsigned MinNumElts = VecVTy->getElementCount().getKnownMinValue();
5599 if (isa<FixedVectorType>(Val: VecVTy) && IdxC->getValue().uge(RHS: MinNumElts))
5600 return PoisonValue::get(T: VecVTy->getElementType());
5601 // Handle case where an element is extracted from a splat.
5602 if (IdxC->getValue().ult(RHS: MinNumElts))
5603 if (auto *Splat = getSplatValue(V: Vec))
5604 return Splat;
5605 if (Value *Elt = findScalarElement(V: Vec, EltNo: IdxC->getZExtValue()))
5606 return Elt;
5607 } else {
5608 // extractelt x, (insertelt y, elt, n), n -> elt
5609 // If the possibly-variable indices are trivially known to be equal
5610 // (because they are the same operand) then use the value that was
5611 // inserted directly.
5612 auto *IE = dyn_cast<InsertElementInst>(Val: Vec);
5613 if (IE && IE->getOperand(i_nocapture: 2) == Idx)
5614 return IE->getOperand(i_nocapture: 1);
5615
5616 // The index is not relevant if our vector is a splat.
5617 if (Value *Splat = getSplatValue(V: Vec))
5618 return Splat;
5619 }
5620 return nullptr;
5621}
5622
5623Value *llvm::simplifyExtractElementInst(Value *Vec, Value *Idx,
5624 const SimplifyQuery &Q) {
5625 return ::simplifyExtractElementInst(Vec, Idx, Q, RecursionLimit);
5626}
5627
5628/// Return true if accessing the bit range of type Ty at Offset in a value of
5629/// type SrcTy is poison.
5630static bool isPoisonBitRange(Value *Offset, Type *Ty, Type *SrcTy,
5631 const SimplifyQuery &Q) {
5632 Constant *C = dyn_cast<Constant>(Val: Offset);
5633 if (!C)
5634 return false;
5635
5636 // A poison offset makes the result poison. An undef offset can be chosen to
5637 // be out of range, which also makes the result poison.
5638 if (isa<PoisonValue>(Val: C) || Q.isUndefValue(V: C))
5639 return true;
5640
5641 // Accessing bits past the end of the value is poison. This covers scalars and
5642 // fixed/scalable vectors with splat constants.
5643 uint64_t Off;
5644 if (match(V: C, P: m_ConstantInt(V&: Off)) &&
5645 Off + Q.DL.getTypeSizeInBits(Ty: Ty->getScalarType()).getFixedValue() >
5646 Q.DL.getTypeSizeInBits(Ty: SrcTy->getScalarType()).getFixedValue())
5647 return true;
5648
5649 // Try harder for fixed-length vectors:
5650 // If every lane's offset is out of range or poison, the result is poison.
5651 if (isa<ConstantVector, ConstantDataVector>(Val: C)) {
5652 for (unsigned I = 0,
5653 E = cast<FixedVectorType>(Val: C->getType())->getNumElements();
5654 I != E; ++I)
5655 if (!isPoisonBitRange(Offset: C->getAggregateElement(Elt: I), Ty, SrcTy, Q))
5656 return false;
5657 return true;
5658 }
5659
5660 return false;
5661}
5662
5663/// Given operands for a BitInsertInst, see if we can fold the result.
5664/// If not, this returns null.
5665static Value *simplifyBitInsertInst(Value *Base, Value *Val, Value *Offset,
5666 const SimplifyQuery &Q, unsigned) {
5667 auto *CBase = dyn_cast<Constant>(Val: Base);
5668 auto *CVal = dyn_cast<Constant>(Val);
5669 auto *COffset = dyn_cast<Constant>(Val: Offset);
5670 if (CBase && CVal && COffset)
5671 if (Constant *C = ConstantFoldBitInsertOperands(Base: CBase, Val: CVal, Offset: COffset, DL: Q.DL))
5672 return C;
5673
5674 // bitinsert x, y, poison/undef/out_of_range -> poison
5675 if (isPoisonBitRange(Offset, Ty: Val->getType(), SrcTy: Base->getType(), Q))
5676 return PoisonValue::get(T: Base->getType());
5677
5678 // bitinsert x, (bN y), n -> y, since any n != 0 is poison
5679 if (Val->getType() == Base->getType())
5680 return Val;
5681
5682 // bitinsert x, (bitextract ty, x, n), n -> x
5683 if (match(V: Val, P: m_BitExtract(Src: m_Specific(V: Base), Off: m_Specific(V: Offset))))
5684 return Base;
5685
5686 return nullptr;
5687}
5688
5689Value *llvm::simplifyBitInsertInst(Value *Base, Value *Val, Value *Offset,
5690 const SimplifyQuery &Q) {
5691 return ::simplifyBitInsertInst(Base, Val, Offset, Q, RecursionLimit);
5692}
5693
5694/// Given operands for a BitExtractInst, see if we can fold the result.
5695/// If not, this returns null.
5696static Value *simplifyBitExtractInst(Type *Ty, Value *Src, Value *Offset,
5697 const SimplifyQuery &Q, unsigned) {
5698 auto *CSrc = dyn_cast<Constant>(Val: Src);
5699 auto *COffset = dyn_cast<Constant>(Val: Offset);
5700 if (CSrc && COffset)
5701 if (Constant *C = ConstantFoldBitExtractOperands(Ty, Src: CSrc, Offset: COffset, DL: Q.DL))
5702 return C;
5703
5704 // bitextract ty, x, poison/undef/out_of_range -> poison
5705 if (isPoisonBitRange(Offset, Ty, SrcTy: Src->getType(), Q))
5706 return PoisonValue::get(T: Ty);
5707
5708 // bitextract bN, (bN x), n -> x, since any n != 0 is poison
5709 if (Src->getType() == Ty)
5710 return Src;
5711
5712 // bitextract ty, (bitinsert x, (ty y), n), n -> y
5713 Value *Val;
5714 if (match(V: Src, P: m_BitInsert(Base: m_Value(), Val: m_Value(V&: Val), Off: m_Specific(V: Offset))) &&
5715 Val->getType() == Ty)
5716 return Val;
5717
5718 return nullptr;
5719}
5720
5721Value *llvm::simplifyBitExtractInst(Type *Ty, Value *Src, Value *Offset,
5722 const SimplifyQuery &Q) {
5723 return ::simplifyBitExtractInst(Ty, Src, Offset, Q, RecursionLimit);
5724}
5725
5726/// See if we can fold the given phi. If not, returns null.
5727static Value *simplifyPHINode(PHINode *PN, ArrayRef<Value *> IncomingValues,
5728 const SimplifyQuery &Q) {
5729 // WARNING: no matter how worthwhile it may seem, we can not perform PHI CSE
5730 // here, because the PHI we may succeed simplifying to was not
5731 // def-reachable from the original PHI!
5732
5733 // If all of the PHI's incoming values are the same then replace the PHI node
5734 // with the common value.
5735 Value *CommonValue = nullptr;
5736 bool HasPoisonInput = false;
5737 bool HasUndefInput = false;
5738 for (Value *Incoming : IncomingValues) {
5739 // If the incoming value is the phi node itself, it can safely be skipped.
5740 if (Incoming == PN)
5741 continue;
5742 if (isa<PoisonValue>(Val: Incoming)) {
5743 HasPoisonInput = true;
5744 continue;
5745 }
5746 if (Q.isUndefValue(V: Incoming)) {
5747 // Remember that we saw an undef value, but otherwise ignore them.
5748 HasUndefInput = true;
5749 continue;
5750 }
5751 if (CommonValue && Incoming != CommonValue)
5752 return nullptr; // Not the same, bail out.
5753 CommonValue = Incoming;
5754 }
5755
5756 // If CommonValue is null then all of the incoming values were either undef,
5757 // poison or equal to the phi node itself.
5758 if (!CommonValue)
5759 return HasUndefInput ? UndefValue::get(T: PN->getType())
5760 : PoisonValue::get(T: PN->getType());
5761
5762 if (HasPoisonInput || HasUndefInput) {
5763 // If we have a PHI node like phi(X, undef, X), where X is defined by some
5764 // instruction, we cannot return X as the result of the PHI node unless it
5765 // dominates the PHI block.
5766 if (!valueDominatesPHI(V: CommonValue, P: PN, DT: Q.DT))
5767 return nullptr;
5768
5769 // Make sure we do not replace an undef value with poison.
5770 if (HasUndefInput &&
5771 !isGuaranteedNotToBePoison(V: CommonValue, AC: Q.AC, CtxI: Q.CtxI, DT: Q.DT))
5772 return nullptr;
5773 return CommonValue;
5774 }
5775
5776 return CommonValue;
5777}
5778
5779static Value *simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
5780 const SimplifyQuery &Q, unsigned MaxRecurse) {
5781 if (auto *C = dyn_cast<Constant>(Val: Op))
5782 return ConstantFoldCastOperand(Opcode: CastOpc, C, DestTy: Ty, DL: Q.DL);
5783
5784 if (auto *CI = dyn_cast<CastInst>(Val: Op)) {
5785 auto *Src = CI->getOperand(i_nocapture: 0);
5786 Type *SrcTy = Src->getType();
5787 Type *MidTy = CI->getType();
5788 Type *DstTy = Ty;
5789 if (Src->getType() == Ty) {
5790 auto FirstOp = CI->getOpcode();
5791 auto SecondOp = static_cast<Instruction::CastOps>(CastOpc);
5792 if (CastInst::isEliminableCastPair(firstOpcode: FirstOp, secondOpcode: SecondOp, SrcTy, MidTy, DstTy,
5793 DL: &Q.DL) == Instruction::BitCast)
5794 return Src;
5795 }
5796 }
5797
5798 // bitcast x -> x
5799 if (CastOpc == Instruction::BitCast)
5800 if (Op->getType() == Ty)
5801 return Op;
5802
5803 // ptrtoaddr (ptradd (Ptr, X - ptrtoint/ptrtoaddr(Ptr))) -> X
5804 // This is also valid for ptrtoint, but only if the (now unused) ptrtoint
5805 // instruction is preserved for its provenance exposure side effect. As this
5806 // is currently not the case, only fold ptrtoaddr, which does not expose
5807 // provenance.
5808 Value *Ptr, *X;
5809 if (CastOpc == Instruction::PtrToAddr &&
5810 match(V: Op, P: m_PtrAdd(DL: Q.DL, PointerOp: m_Value(V&: Ptr),
5811 OffsetOp: m_Sub(L: m_Value(V&: X), R: m_PtrToIntOrAddr(Op: m_Deferred(V: Ptr))))))
5812 return X;
5813
5814 // Fold a value-preserving zext/sext of a trunc back to the original value.
5815 if (CastOpc == Instruction::ZExt || CastOpc == Instruction::SExt) {
5816 if (auto *Trunc = dyn_cast<TruncInst>(Val: Op)) {
5817 Value *Src = Trunc->getOperand(i_nocapture: 0);
5818 bool NoWrap = CastOpc == Instruction::ZExt ? Trunc->hasNoUnsignedWrap()
5819 : Trunc->hasNoSignedWrap();
5820 if (Src->getType() == Ty && NoWrap)
5821 return Src;
5822 }
5823 }
5824
5825 return nullptr;
5826}
5827
5828Value *llvm::simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
5829 const SimplifyQuery &Q) {
5830 return ::simplifyCastInst(CastOpc, Op, Ty, Q, MaxRecurse: RecursionLimit);
5831}
5832
5833static Value *simplifyAddrSpaceCastInst(Value *Op, Type *Ty, bool IsNonNull,
5834 const SimplifyQuery &Q,
5835 unsigned MaxRecurse) {
5836 if (IsNonNull && isa<ConstantPointerNull>(Val: Op) && Q.getFunction() &&
5837 !NullPointerIsDefined(F: Q.getFunction(),
5838 AS: Op->getType()->getPointerAddressSpace()))
5839 return PoisonValue::get(T: Ty);
5840
5841 return ::simplifyCastInst(CastOpc: Instruction::AddrSpaceCast, Op, Ty, Q, MaxRecurse);
5842}
5843
5844Value *llvm::simplifyAddrSpaceCastInst(Value *Op, Type *Ty, bool IsNonNull,
5845 const SimplifyQuery &Q) {
5846 return ::simplifyAddrSpaceCastInst(Op, Ty, IsNonNull, Q, MaxRecurse: RecursionLimit);
5847}
5848
5849/// For the given destination element of a shuffle, peek through shuffles to
5850/// match a root vector source operand that contains that element in the same
5851/// vector lane (ie, the same mask index), so we can eliminate the shuffle(s).
5852static Value *foldIdentityShuffles(int DestElt, Value *Op0, Value *Op1,
5853 int MaskVal, Value *RootVec,
5854 unsigned MaxRecurse) {
5855 if (!MaxRecurse--)
5856 return nullptr;
5857
5858 // Bail out if any mask value is undefined. That kind of shuffle may be
5859 // simplified further based on demanded bits or other folds.
5860 if (MaskVal == -1)
5861 return nullptr;
5862
5863 // The mask value chooses which source operand we need to look at next.
5864 int InVecNumElts = cast<FixedVectorType>(Val: Op0->getType())->getNumElements();
5865 int RootElt = MaskVal;
5866 Value *SourceOp = Op0;
5867 if (MaskVal >= InVecNumElts) {
5868 RootElt = MaskVal - InVecNumElts;
5869 SourceOp = Op1;
5870 }
5871
5872 // If the source operand is a shuffle itself, look through it to find the
5873 // matching root vector.
5874 if (auto *SourceShuf = dyn_cast<ShuffleVectorInst>(Val: SourceOp)) {
5875 return foldIdentityShuffles(
5876 DestElt, Op0: SourceShuf->getOperand(i_nocapture: 0), Op1: SourceShuf->getOperand(i_nocapture: 1),
5877 MaskVal: SourceShuf->getMaskValue(Elt: RootElt), RootVec, MaxRecurse);
5878 }
5879
5880 // The source operand is not a shuffle. Initialize the root vector value for
5881 // this shuffle if that has not been done yet.
5882 if (!RootVec)
5883 RootVec = SourceOp;
5884
5885 // Give up as soon as a source operand does not match the existing root value.
5886 if (RootVec != SourceOp)
5887 return nullptr;
5888
5889 // The element must be coming from the same lane in the source vector
5890 // (although it may have crossed lanes in intermediate shuffles).
5891 if (RootElt != DestElt)
5892 return nullptr;
5893
5894 return RootVec;
5895}
5896
5897static Value *simplifyShuffleVectorInst(Value *Op0, Value *Op1,
5898 ArrayRef<int> Mask, Type *RetTy,
5899 const SimplifyQuery &Q,
5900 unsigned MaxRecurse) {
5901 if (all_of(Range&: Mask, P: equal_to(Arg: PoisonMaskElem)))
5902 return PoisonValue::get(T: RetTy);
5903
5904 auto *InVecTy = cast<VectorType>(Val: Op0->getType());
5905 unsigned MaskNumElts = Mask.size();
5906 ElementCount InVecEltCount = InVecTy->getElementCount();
5907
5908 bool Scalable = InVecEltCount.isScalable();
5909
5910 SmallVector<int, 32> Indices;
5911 Indices.assign(in_start: Mask.begin(), in_end: Mask.end());
5912
5913 // Canonicalization: If mask does not select elements from an input vector,
5914 // replace that input vector with poison.
5915 if (!Scalable) {
5916 bool MaskSelects0 = false, MaskSelects1 = false;
5917 unsigned InVecNumElts = InVecEltCount.getKnownMinValue();
5918 for (unsigned i = 0; i != MaskNumElts; ++i) {
5919 if (Indices[i] == -1)
5920 continue;
5921 if ((unsigned)Indices[i] < InVecNumElts)
5922 MaskSelects0 = true;
5923 else
5924 MaskSelects1 = true;
5925 }
5926 if (!MaskSelects0)
5927 Op0 = PoisonValue::get(T: InVecTy);
5928 if (!MaskSelects1)
5929 Op1 = PoisonValue::get(T: InVecTy);
5930 }
5931
5932 auto *Op0Const = dyn_cast<Constant>(Val: Op0);
5933 auto *Op1Const = dyn_cast<Constant>(Val: Op1);
5934
5935 // If all operands are constant, constant fold the shuffle. This
5936 // transformation depends on the value of the mask which is not known at
5937 // compile time for scalable vectors
5938 if (Op0Const && Op1Const)
5939 return ConstantExpr::getShuffleVector(V1: Op0Const, V2: Op1Const, Mask);
5940
5941 // Canonicalization: if only one input vector is constant, it shall be the
5942 // second one. This transformation depends on the value of the mask which
5943 // is not known at compile time for scalable vectors
5944 if (!Scalable && Op0Const && !Op1Const) {
5945 std::swap(a&: Op0, b&: Op1);
5946 ShuffleVectorInst::commuteShuffleMask(Mask: Indices,
5947 InVecNumElts: InVecEltCount.getKnownMinValue());
5948 }
5949
5950 // A splat of an inserted scalar constant becomes a vector constant:
5951 // shuf (inselt ?, C, IndexC), undef, <IndexC, IndexC...> --> <C, C...>
5952 // NOTE: We may have commuted above, so analyze the updated Indices, not the
5953 // original mask constant.
5954 // NOTE: This transformation depends on the value of the mask which is not
5955 // known at compile time for scalable vectors
5956 Constant *C;
5957 ConstantInt *IndexC;
5958 if (!Scalable && match(V: Op0, P: m_InsertElt(Val: m_Value(), Elt: m_Constant(C),
5959 Idx: m_ConstantInt(CI&: IndexC)))) {
5960 // Match a splat shuffle mask of the insert index allowing undef elements.
5961 int InsertIndex = IndexC->getZExtValue();
5962 if (all_of(Range&: Indices, P: [InsertIndex](int MaskElt) {
5963 return MaskElt == InsertIndex || MaskElt == -1;
5964 })) {
5965 assert(isa<UndefValue>(Op1) && "Expected undef operand 1 for splat");
5966
5967 // Shuffle mask poisons become poison constant result elements.
5968 SmallVector<Constant *, 16> VecC(MaskNumElts, C);
5969 for (unsigned i = 0; i != MaskNumElts; ++i)
5970 if (Indices[i] == -1)
5971 VecC[i] = PoisonValue::get(T: C->getType());
5972 return ConstantVector::get(V: VecC);
5973 }
5974 }
5975
5976 // A shuffle of a splat is always the splat itself. Legal if the shuffle's
5977 // value type is same as the input vectors' type.
5978 if (auto *OpShuf = dyn_cast<ShuffleVectorInst>(Val: Op0))
5979 if (Q.isUndefValue(V: Op1) && RetTy == InVecTy &&
5980 all_equal(Range: OpShuf->getShuffleMask()))
5981 return Op0;
5982
5983 // All remaining transformation depend on the value of the mask, which is
5984 // not known at compile time for scalable vectors.
5985 if (Scalable)
5986 return nullptr;
5987
5988 // Don't fold a shuffle with undef mask elements. This may get folded in a
5989 // better way using demanded bits or other analysis.
5990 // TODO: Should we allow this?
5991 if (is_contained(Range&: Indices, Element: -1))
5992 return nullptr;
5993
5994 // Check if every element of this shuffle can be mapped back to the
5995 // corresponding element of a single root vector. If so, we don't need this
5996 // shuffle. This handles simple identity shuffles as well as chains of
5997 // shuffles that may widen/narrow and/or move elements across lanes and back.
5998 Value *RootVec = nullptr;
5999 for (unsigned i = 0; i != MaskNumElts; ++i) {
6000 // Note that recursion is limited for each vector element, so if any element
6001 // exceeds the limit, this will fail to simplify.
6002 RootVec =
6003 foldIdentityShuffles(DestElt: i, Op0, Op1, MaskVal: Indices[i], RootVec, MaxRecurse);
6004
6005 // We can't replace a widening/narrowing shuffle with one of its operands.
6006 if (!RootVec || RootVec->getType() != RetTy)
6007 return nullptr;
6008 }
6009 return RootVec;
6010}
6011
6012/// Given operands for a ShuffleVectorInst, fold the result or return null.
6013Value *llvm::simplifyShuffleVectorInst(Value *Op0, Value *Op1,
6014 ArrayRef<int> Mask, Type *RetTy,
6015 const SimplifyQuery &Q) {
6016 return ::simplifyShuffleVectorInst(Op0, Op1, Mask, RetTy, Q, MaxRecurse: RecursionLimit);
6017}
6018
6019static Constant *foldConstant(Instruction::UnaryOps Opcode, Value *&Op,
6020 const SimplifyQuery &Q) {
6021 if (auto *C = dyn_cast<Constant>(Val: Op))
6022 return ConstantFoldUnaryOpOperand(Opcode, Op: C, DL: Q.DL);
6023 return nullptr;
6024}
6025
6026/// Given the operand for an FNeg, see if we can fold the result. If not, this
6027/// returns null.
6028static Value *simplifyFNegInst(Value *Op, FastMathFlags FMF,
6029 const SimplifyQuery &Q, unsigned MaxRecurse) {
6030 if (Constant *C = foldConstant(Opcode: Instruction::FNeg, Op, Q))
6031 return C;
6032
6033 Value *X;
6034 // fneg (fneg X) ==> X
6035 // TODO: Confirm whether m_FNegOrFSubNegZero is correct.
6036 if (match(V: Op, P: m_FNegOrFSubNegZero(X: m_Value(V&: X))))
6037 return X;
6038
6039 return nullptr;
6040}
6041
6042Value *llvm::simplifyFNegInst(Value *Op, FastMathFlags FMF,
6043 const SimplifyQuery &Q) {
6044 return ::simplifyFNegInst(Op, FMF, Q, MaxRecurse: RecursionLimit);
6045}
6046
6047/// Try to propagate existing NaN values when possible. If not, replace the
6048/// constant or elements in the constant with a canonical NaN.
6049static Constant *propagateNaN(Constant *In) {
6050 Type *Ty = In->getType();
6051 if (auto *VecTy = dyn_cast<FixedVectorType>(Val: Ty)) {
6052 unsigned NumElts = VecTy->getNumElements();
6053 SmallVector<Constant *, 32> NewC(NumElts);
6054 for (unsigned i = 0; i != NumElts; ++i) {
6055 Constant *EltC = In->getAggregateElement(Elt: i);
6056 // Poison elements propagate. NaN propagates except signaling is quieted.
6057 // Replace unknown or undef elements with canonical NaN.
6058 if (EltC && isa<PoisonValue>(Val: EltC))
6059 NewC[i] = EltC;
6060 else if (EltC && EltC->isNaN())
6061 NewC[i] = ConstantFP::get(
6062 Ty: EltC->getType(), V: cast<ConstantFP>(Val: EltC)->getValue().makeQuiet());
6063 else
6064 NewC[i] = ConstantFP::getNaN(Ty: VecTy->getElementType());
6065 }
6066 return ConstantVector::get(V: NewC);
6067 }
6068
6069 // If it is not a fixed vector, but not a simple NaN either, return a
6070 // canonical NaN.
6071 if (!In->isNaN())
6072 return ConstantFP::getNaN(Ty);
6073
6074 // If we known this is a NaN, and it's scalable vector, we must have a splat
6075 // on our hands. Grab that before splatting a QNaN constant.
6076 if (isa<ScalableVectorType>(Val: Ty)) {
6077 auto *Splat = In->getSplatValue();
6078 assert(Splat && Splat->isNaN() &&
6079 "Found a scalable-vector NaN but not a splat");
6080 In = Splat;
6081 }
6082
6083 // Propagate an existing QNaN constant. If it is an SNaN, make it quiet, but
6084 // preserve the sign/payload.
6085 return ConstantFP::get(Ty, V: cast<ConstantFP>(Val: In)->getValue().makeQuiet());
6086}
6087
6088/// Perform folds that are common to any floating-point operation. This implies
6089/// transforms based on poison/undef/NaN because the operation itself makes no
6090/// difference to the result.
6091static Constant *simplifyFPOp(ArrayRef<Value *> Ops, FastMathFlags FMF,
6092 const SimplifyQuery &Q,
6093 fp::ExceptionBehavior ExBehavior,
6094 RoundingMode Rounding) {
6095 // Poison is independent of anything else. It always propagates from an
6096 // operand to a math result.
6097 if (any_of(Range&: Ops, P: IsaPred<PoisonValue>))
6098 return PoisonValue::get(T: Ops[0]->getType());
6099
6100 for (Value *V : Ops) {
6101 bool IsNan = match(V, P: m_NaN());
6102 bool IsInf = match(V, P: m_Inf());
6103 bool IsUndef = Q.isUndefValue(V);
6104
6105 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
6106 // (an undef operand can be chosen to be Nan/Inf), then the result of
6107 // this operation is poison.
6108 if (FMF.noNaNs() && (IsNan || IsUndef))
6109 return PoisonValue::get(T: V->getType());
6110 if (FMF.noInfs() && (IsInf || IsUndef))
6111 return PoisonValue::get(T: V->getType());
6112
6113 if (isDefaultFPEnvironment(EB: ExBehavior, RM: Rounding)) {
6114 // Undef does not propagate because undef means that all bits can take on
6115 // any value. If this is undef * NaN for example, then the result values
6116 // (at least the exponent bits) are limited. Assume the undef is a
6117 // canonical NaN and propagate that.
6118 if (IsUndef)
6119 return ConstantFP::getNaN(Ty: V->getType());
6120 if (IsNan)
6121 return propagateNaN(In: cast<Constant>(Val: V));
6122 } else if (ExBehavior != fp::ebStrict) {
6123 if (IsNan)
6124 return propagateNaN(In: cast<Constant>(Val: V));
6125 }
6126 }
6127 return nullptr;
6128}
6129
6130/// Given operands for an FAdd, see if we can fold the result. If not, this
6131/// returns null.
6132static Value *
6133simplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF,
6134 const SimplifyQuery &Q, unsigned MaxRecurse,
6135 fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
6136 RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
6137 if (isDefaultFPEnvironment(EB: ExBehavior, RM: Rounding))
6138 if (Constant *C = foldOrCommuteConstant(Opcode: Instruction::FAdd, Op0, Op1, Q))
6139 return C;
6140
6141 if (Constant *C = simplifyFPOp(Ops: {Op0, Op1}, FMF, Q, ExBehavior, Rounding))
6142 return C;
6143
6144 // fadd X, -0 ==> X
6145 // With strict/constrained FP, we have these possible edge cases that do
6146 // not simplify to Op0:
6147 // fadd SNaN, -0.0 --> QNaN
6148 // fadd +0.0, -0.0 --> -0.0 (but only with round toward negative)
6149 if (canIgnoreSNaN(EB: ExBehavior, FMF) &&
6150 (!canRoundingModeBe(RM: Rounding, QRM: RoundingMode::TowardNegative) ||
6151 FMF.noSignedZeros()))
6152 if (match(V: Op1, P: m_NegZeroFP()))
6153 return Op0;
6154
6155 // fadd X, 0 ==> X, when we know X is not -0
6156 if (canIgnoreSNaN(EB: ExBehavior, FMF))
6157 if (match(V: Op1, P: m_PosZeroFP()) &&
6158 (FMF.noSignedZeros() || cannotBeNegativeZero(V: Op0, SQ: Q)))
6159 return Op0;
6160
6161 if (!isDefaultFPEnvironment(EB: ExBehavior, RM: Rounding))
6162 return nullptr;
6163
6164 if (FMF.noNaNs()) {
6165 // With nnan: X + {+/-}Inf --> {+/-}Inf
6166 if (match(V: Op1, P: m_Inf()))
6167 return Op1;
6168
6169 // With nnan: -X + X --> 0.0 (and commuted variant)
6170 // We don't have to explicitly exclude infinities (ninf): INF + -INF == NaN.
6171 // Negative zeros are allowed because we always end up with positive zero:
6172 // X = -0.0: (-0.0 - (-0.0)) + (-0.0) == ( 0.0) + (-0.0) == 0.0
6173 // X = -0.0: ( 0.0 - (-0.0)) + (-0.0) == ( 0.0) + (-0.0) == 0.0
6174 // X = 0.0: (-0.0 - ( 0.0)) + ( 0.0) == (-0.0) + ( 0.0) == 0.0
6175 // X = 0.0: ( 0.0 - ( 0.0)) + ( 0.0) == ( 0.0) + ( 0.0) == 0.0
6176 if (match(V: Op0, P: m_FSub(L: m_AnyZeroFP(), R: m_Specific(V: Op1))) ||
6177 match(V: Op1, P: m_FSub(L: m_AnyZeroFP(), R: m_Specific(V: Op0))))
6178 return ConstantFP::getZero(Ty: Op0->getType());
6179
6180 // TODO: Confirm whether m_FNegOrFSubNegZero is correct.
6181 if (match(V: Op0, P: m_FNegOrFSubNegZero(X: m_Specific(V: Op1))) ||
6182 match(V: Op1, P: m_FNegOrFSubNegZero(X: m_Specific(V: Op0))))
6183 return ConstantFP::getZero(Ty: Op0->getType());
6184 }
6185
6186 // (X - Y) + Y --> X
6187 // Y + (X - Y) --> X
6188 Value *X;
6189 if (FMF.noSignedZeros() && FMF.allowReassoc() &&
6190 (match(V: Op0, P: m_FSub(L: m_Value(V&: X), R: m_Specific(V: Op1))) ||
6191 match(V: Op1, P: m_FSub(L: m_Value(V&: X), R: m_Specific(V: Op0)))))
6192 return X;
6193
6194 return nullptr;
6195}
6196
6197/// Given operands for an FSub, see if we can fold the result. If not, this
6198/// returns null.
6199static Value *
6200simplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF,
6201 const SimplifyQuery &Q, unsigned MaxRecurse,
6202 fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
6203 RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
6204 if (isDefaultFPEnvironment(EB: ExBehavior, RM: Rounding))
6205 if (Constant *C = foldOrCommuteConstant(Opcode: Instruction::FSub, Op0, Op1, Q))
6206 return C;
6207
6208 if (Constant *C = simplifyFPOp(Ops: {Op0, Op1}, FMF, Q, ExBehavior, Rounding))
6209 return C;
6210
6211 // fsub X, +0 ==> X
6212 if (canIgnoreSNaN(EB: ExBehavior, FMF) &&
6213 (!canRoundingModeBe(RM: Rounding, QRM: RoundingMode::TowardNegative) ||
6214 FMF.noSignedZeros()))
6215 if (match(V: Op1, P: m_PosZeroFP()))
6216 return Op0;
6217
6218 // fsub X, -0 ==> X, when we know X is not -0
6219 if (canIgnoreSNaN(EB: ExBehavior, FMF))
6220 if (match(V: Op1, P: m_NegZeroFP()) &&
6221 (FMF.noSignedZeros() || cannotBeNegativeZero(V: Op0, SQ: Q)))
6222 return Op0;
6223
6224 // fsub -0.0, (fsub -0.0, X) ==> X
6225 // fsub -0.0, (fneg X) ==> X
6226 Value *X;
6227 if (canIgnoreSNaN(EB: ExBehavior, FMF))
6228 // TODO: Confirm whether m_FNegOrFSubNegZero is correct.
6229 if (match(V: Op0, P: m_NegZeroFP()) &&
6230 match(V: Op1, P: m_FNegOrFSubNegZero(X: m_Value(V&: X))))
6231 return X;
6232
6233 // fsub 0.0, (fsub 0.0, X) ==> X if signed zeros are ignored.
6234 // fsub 0.0, (fneg X) ==> X if signed zeros are ignored.
6235 if (canIgnoreSNaN(EB: ExBehavior, FMF))
6236 if (FMF.noSignedZeros() && match(V: Op0, P: m_AnyZeroFP()) &&
6237 (match(V: Op1, P: m_FSub(L: m_AnyZeroFP(), R: m_Value(V&: X))) ||
6238 match(V: Op1, P: m_FNeg(X: m_Value(V&: X)))))
6239 return X;
6240
6241 if (!isDefaultFPEnvironment(EB: ExBehavior, RM: Rounding))
6242 return nullptr;
6243
6244 if (FMF.noNaNs()) {
6245 // fsub nnan x, x ==> 0.0
6246 if (Op0 == Op1)
6247 return Constant::getNullValue(Ty: Op0->getType());
6248
6249 // With nnan: {+/-}Inf - X --> {+/-}Inf
6250 if (match(V: Op0, P: m_Inf()))
6251 return Op0;
6252
6253 // With nnan: X - {+/-}Inf --> {-/+}Inf
6254 if (match(V: Op1, P: m_Inf()))
6255 return foldConstant(Opcode: Instruction::FNeg, Op&: Op1, Q);
6256 }
6257
6258 // Y - (Y - X) --> X
6259 // (X + Y) - Y --> X
6260 if (FMF.noSignedZeros() && FMF.allowReassoc() &&
6261 (match(V: Op1, P: m_FSub(L: m_Specific(V: Op0), R: m_Value(V&: X))) ||
6262 match(V: Op0, P: m_c_FAdd(L: m_Specific(V: Op1), R: m_Value(V&: X)))))
6263 return X;
6264
6265 return nullptr;
6266}
6267
6268static Value *simplifyFMAFMul(Value *Op0, Value *Op1, FastMathFlags FMF,
6269 const SimplifyQuery &Q, unsigned MaxRecurse,
6270 fp::ExceptionBehavior ExBehavior,
6271 RoundingMode Rounding) {
6272 if (Constant *C = simplifyFPOp(Ops: {Op0, Op1}, FMF, Q, ExBehavior, Rounding))
6273 return C;
6274
6275 if (!isDefaultFPEnvironment(EB: ExBehavior, RM: Rounding))
6276 return nullptr;
6277
6278 // Canonicalize special constants as operand 1.
6279 if (match(V: Op0, P: m_FPOne()) || match(V: Op0, P: m_AnyZeroFP()))
6280 std::swap(a&: Op0, b&: Op1);
6281
6282 // X * 1.0 --> X
6283 if (match(V: Op1, P: m_FPOne()))
6284 return Op0;
6285
6286 if (match(V: Op1, P: m_AnyZeroFP())) {
6287 // X * 0.0 --> 0.0 (with nnan and nsz)
6288 if (FMF.noNaNs() && FMF.noSignedZeros())
6289 return ConstantFP::getZero(Ty: Op0->getType());
6290
6291 KnownFPClass Known = computeKnownFPClass(V: Op0, FMF, InterestedClasses: fcInf | fcNan, SQ: Q);
6292 if (Known.isKnownNever(Mask: fcInf | fcNan)) {
6293 // if nsz is set, return 0.0
6294 if (FMF.noSignedZeros())
6295 return ConstantFP::getZero(Ty: Op0->getType());
6296 // +normal number * (-)0.0 --> (-)0.0
6297 if (Known.getSignBit() == false)
6298 return Op1;
6299 // -normal number * (-)0.0 --> -(-)0.0
6300 if (Known.getSignBit() == true)
6301 return foldConstant(Opcode: Instruction::FNeg, Op&: Op1, Q);
6302 }
6303 }
6304
6305 // sqrt(X) * sqrt(X) --> X, if we can:
6306 // 1. Remove the intermediate rounding (reassociate).
6307 // 2. Ignore non-zero negative numbers because sqrt would produce NAN.
6308 // 3. Ignore -0.0 because sqrt(-0.0) == -0.0, but -0.0 * -0.0 == 0.0.
6309 Value *X;
6310 if (Op0 == Op1 && match(V: Op0, P: m_Sqrt(Op0: m_Value(V&: X))) && FMF.allowReassoc() &&
6311 FMF.noNaNs() && FMF.noSignedZeros())
6312 return X;
6313
6314 return nullptr;
6315}
6316
6317/// Given the operands for an FMul, see if we can fold the result
6318static Value *
6319simplifyFMulInst(Value *Op0, Value *Op1, FastMathFlags FMF,
6320 const SimplifyQuery &Q, unsigned MaxRecurse,
6321 fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
6322 RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
6323 if (isDefaultFPEnvironment(EB: ExBehavior, RM: Rounding))
6324 if (Constant *C = foldOrCommuteConstant(Opcode: Instruction::FMul, Op0, Op1, Q))
6325 return C;
6326
6327 // Now apply simplifications that do not require rounding.
6328 return simplifyFMAFMul(Op0, Op1, FMF, Q, MaxRecurse, ExBehavior, Rounding);
6329}
6330
6331Value *llvm::simplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF,
6332 const SimplifyQuery &Q,
6333 fp::ExceptionBehavior ExBehavior,
6334 RoundingMode Rounding) {
6335 return ::simplifyFAddInst(Op0, Op1, FMF, Q, MaxRecurse: RecursionLimit, ExBehavior,
6336 Rounding);
6337}
6338
6339Value *llvm::simplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF,
6340 const SimplifyQuery &Q,
6341 fp::ExceptionBehavior ExBehavior,
6342 RoundingMode Rounding) {
6343 return ::simplifyFSubInst(Op0, Op1, FMF, Q, MaxRecurse: RecursionLimit, ExBehavior,
6344 Rounding);
6345}
6346
6347Value *llvm::simplifyFMulInst(Value *Op0, Value *Op1, FastMathFlags FMF,
6348 const SimplifyQuery &Q,
6349 fp::ExceptionBehavior ExBehavior,
6350 RoundingMode Rounding) {
6351 return ::simplifyFMulInst(Op0, Op1, FMF, Q, MaxRecurse: RecursionLimit, ExBehavior,
6352 Rounding);
6353}
6354
6355Value *llvm::simplifyFMAFMul(Value *Op0, Value *Op1, FastMathFlags FMF,
6356 const SimplifyQuery &Q,
6357 fp::ExceptionBehavior ExBehavior,
6358 RoundingMode Rounding) {
6359 return ::simplifyFMAFMul(Op0, Op1, FMF, Q, MaxRecurse: RecursionLimit, ExBehavior,
6360 Rounding);
6361}
6362
6363static Value *
6364simplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF,
6365 const SimplifyQuery &Q, unsigned,
6366 fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
6367 RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
6368 if (isDefaultFPEnvironment(EB: ExBehavior, RM: Rounding))
6369 if (Constant *C = foldOrCommuteConstant(Opcode: Instruction::FDiv, Op0, Op1, Q))
6370 return C;
6371
6372 if (Constant *C = simplifyFPOp(Ops: {Op0, Op1}, FMF, Q, ExBehavior, Rounding))
6373 return C;
6374
6375 if (!isDefaultFPEnvironment(EB: ExBehavior, RM: Rounding))
6376 return nullptr;
6377
6378 // X / 1.0 -> X
6379 if (match(V: Op1, P: m_FPOne()))
6380 return Op0;
6381
6382 // 0 / X -> 0
6383 // Requires that NaNs are off (X could be zero) and signed zeroes are
6384 // ignored (X could be positive or negative, so the output sign is unknown).
6385 if (FMF.noNaNs() && FMF.noSignedZeros() && match(V: Op0, P: m_AnyZeroFP()))
6386 return ConstantFP::getZero(Ty: Op0->getType());
6387
6388 if (FMF.noNaNs()) {
6389 // X / X -> 1.0 is legal when NaNs are ignored.
6390 // We can ignore infinities because INF/INF is NaN.
6391 if (Op0 == Op1)
6392 return ConstantFP::get(Ty: Op0->getType(), V: 1.0);
6393
6394 // (X * Y) / Y --> X if we can reassociate to the above form.
6395 Value *X;
6396 if (FMF.allowReassoc() && match(V: Op0, P: m_c_FMul(L: m_Value(V&: X), R: m_Specific(V: Op1))))
6397 return X;
6398
6399 // -X / X -> -1.0 and
6400 // X / -X -> -1.0 are legal when NaNs are ignored.
6401 // We can ignore signed zeros because +-0.0/+-0.0 is NaN and ignored.
6402 if (match(V: Op0, P: m_FNegNSZ(X: m_Specific(V: Op1))) ||
6403 match(V: Op1, P: m_FNegNSZ(X: m_Specific(V: Op0))))
6404 return ConstantFP::get(Ty: Op0->getType(), V: -1.0);
6405
6406 // nnan ninf X / [-]0.0 -> poison
6407 if (FMF.noInfs() && match(V: Op1, P: m_AnyZeroFP()))
6408 return PoisonValue::get(T: Op1->getType());
6409 }
6410
6411 return nullptr;
6412}
6413
6414Value *llvm::simplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF,
6415 const SimplifyQuery &Q,
6416 fp::ExceptionBehavior ExBehavior,
6417 RoundingMode Rounding) {
6418 return ::simplifyFDivInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
6419 Rounding);
6420}
6421
6422static Value *
6423simplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF,
6424 const SimplifyQuery &Q, unsigned,
6425 fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
6426 RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
6427 if (isDefaultFPEnvironment(EB: ExBehavior, RM: Rounding))
6428 if (Constant *C = foldOrCommuteConstant(Opcode: Instruction::FRem, Op0, Op1, Q))
6429 return C;
6430
6431 if (Constant *C = simplifyFPOp(Ops: {Op0, Op1}, FMF, Q, ExBehavior, Rounding))
6432 return C;
6433
6434 if (!isDefaultFPEnvironment(EB: ExBehavior, RM: Rounding))
6435 return nullptr;
6436
6437 // Unlike fdiv, the result of frem always matches the sign of the dividend.
6438 // The constant match may include undef elements in a vector, so return a full
6439 // zero constant as the result.
6440 if (FMF.noNaNs()) {
6441 // +0 % X -> 0
6442 if (match(V: Op0, P: m_PosZeroFP()))
6443 return ConstantFP::getZero(Ty: Op0->getType());
6444 // -0 % X -> -0
6445 if (match(V: Op0, P: m_NegZeroFP()))
6446 return ConstantFP::getNegativeZero(Ty: Op0->getType());
6447 }
6448
6449 return nullptr;
6450}
6451
6452Value *llvm::simplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF,
6453 const SimplifyQuery &Q,
6454 fp::ExceptionBehavior ExBehavior,
6455 RoundingMode Rounding) {
6456 return ::simplifyFRemInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
6457 Rounding);
6458}
6459
6460//=== Helper functions for higher up the class hierarchy.
6461
6462/// Given the operand for a UnaryOperator, see if we can fold the result.
6463/// If not, this returns null.
6464static Value *simplifyUnOp(unsigned Opcode, Value *Op, const SimplifyQuery &Q,
6465 unsigned MaxRecurse) {
6466 switch (Opcode) {
6467 case Instruction::FNeg:
6468 return simplifyFNegInst(Op, FMF: FastMathFlags(), Q, MaxRecurse);
6469 default:
6470 llvm_unreachable("Unexpected opcode");
6471 }
6472}
6473
6474/// Given the operand for a UnaryOperator, see if we can fold the result.
6475/// If not, this returns null.
6476/// Try to use FastMathFlags when folding the result.
6477static Value *simplifyFPUnOp(unsigned Opcode, Value *Op,
6478 const FastMathFlags &FMF, const SimplifyQuery &Q,
6479 unsigned MaxRecurse) {
6480 switch (Opcode) {
6481 case Instruction::FNeg:
6482 return simplifyFNegInst(Op, FMF, Q, MaxRecurse);
6483 default:
6484 return simplifyUnOp(Opcode, Op, Q, MaxRecurse);
6485 }
6486}
6487
6488Value *llvm::simplifyUnOp(unsigned Opcode, Value *Op, const SimplifyQuery &Q) {
6489 return ::simplifyUnOp(Opcode, Op, Q, MaxRecurse: RecursionLimit);
6490}
6491
6492Value *llvm::simplifyUnOp(unsigned Opcode, Value *Op, FastMathFlags FMF,
6493 const SimplifyQuery &Q) {
6494 return ::simplifyFPUnOp(Opcode, Op, FMF, Q, MaxRecurse: RecursionLimit);
6495}
6496
6497/// Given operands for a BinaryOperator, see if we can fold the result.
6498/// If not, this returns null.
6499static Value *simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
6500 const SimplifyQuery &Q, unsigned MaxRecurse) {
6501 switch (Opcode) {
6502 case Instruction::Add:
6503 return simplifyAddInst(Op0: LHS, Op1: RHS, /* IsNSW */ false, /* IsNUW */ false, Q,
6504 MaxRecurse);
6505 case Instruction::Sub:
6506 return simplifySubInst(Op0: LHS, Op1: RHS, /* IsNSW */ false, /* IsNUW */ false, Q,
6507 MaxRecurse);
6508 case Instruction::Mul:
6509 return simplifyMulInst(Op0: LHS, Op1: RHS, /* IsNSW */ false, /* IsNUW */ false, Q,
6510 MaxRecurse);
6511 case Instruction::SDiv:
6512 return simplifySDivInst(Op0: LHS, Op1: RHS, /* IsExact */ false, Q, MaxRecurse);
6513 case Instruction::UDiv:
6514 return simplifyUDivInst(Op0: LHS, Op1: RHS, /* IsExact */ false, Q, MaxRecurse);
6515 case Instruction::SRem:
6516 return simplifySRemInst(Op0: LHS, Op1: RHS, Q, MaxRecurse);
6517 case Instruction::URem:
6518 return simplifyURemInst(Op0: LHS, Op1: RHS, Q, MaxRecurse);
6519 case Instruction::Shl:
6520 return simplifyShlInst(Op0: LHS, Op1: RHS, /* IsNSW */ false, /* IsNUW */ false, Q,
6521 MaxRecurse);
6522 case Instruction::LShr:
6523 return simplifyLShrInst(Op0: LHS, Op1: RHS, /* IsExact */ false, Q, MaxRecurse);
6524 case Instruction::AShr:
6525 return simplifyAShrInst(Op0: LHS, Op1: RHS, /* IsExact */ false, Q, MaxRecurse);
6526 case Instruction::And:
6527 return simplifyAndInst(Op0: LHS, Op1: RHS, Q, MaxRecurse);
6528 case Instruction::Or:
6529 return simplifyOrInst(Op0: LHS, Op1: RHS, Q, MaxRecurse);
6530 case Instruction::Xor:
6531 return simplifyXorInst(Op0: LHS, Op1: RHS, Q, MaxRecurse);
6532 case Instruction::FAdd:
6533 return simplifyFAddInst(Op0: LHS, Op1: RHS, FMF: FastMathFlags(), Q, MaxRecurse);
6534 case Instruction::FSub:
6535 return simplifyFSubInst(Op0: LHS, Op1: RHS, FMF: FastMathFlags(), Q, MaxRecurse);
6536 case Instruction::FMul:
6537 return simplifyFMulInst(Op0: LHS, Op1: RHS, FMF: FastMathFlags(), Q, MaxRecurse);
6538 case Instruction::FDiv:
6539 return simplifyFDivInst(Op0: LHS, Op1: RHS, FMF: FastMathFlags(), Q, MaxRecurse);
6540 case Instruction::FRem:
6541 return simplifyFRemInst(Op0: LHS, Op1: RHS, FMF: FastMathFlags(), Q, MaxRecurse);
6542 default:
6543 llvm_unreachable("Unexpected opcode");
6544 }
6545}
6546
6547/// Given operands for a BinaryOperator, see if we can fold the result.
6548/// If not, this returns null.
6549/// Try to use FastMathFlags when folding the result.
6550static Value *simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
6551 const FastMathFlags &FMF, const SimplifyQuery &Q,
6552 unsigned MaxRecurse) {
6553 switch (Opcode) {
6554 case Instruction::FAdd:
6555 return simplifyFAddInst(Op0: LHS, Op1: RHS, FMF, Q, MaxRecurse);
6556 case Instruction::FSub:
6557 return simplifyFSubInst(Op0: LHS, Op1: RHS, FMF, Q, MaxRecurse);
6558 case Instruction::FMul:
6559 return simplifyFMulInst(Op0: LHS, Op1: RHS, FMF, Q, MaxRecurse);
6560 case Instruction::FDiv:
6561 return simplifyFDivInst(Op0: LHS, Op1: RHS, FMF, Q, MaxRecurse);
6562 case Instruction::FRem:
6563 return simplifyFRemInst(Op0: LHS, Op1: RHS, FMF, Q, MaxRecurse);
6564 default:
6565 return simplifyBinOp(Opcode, LHS, RHS, Q, MaxRecurse);
6566 }
6567}
6568
6569Value *llvm::simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
6570 const SimplifyQuery &Q) {
6571 return ::simplifyBinOp(Opcode, LHS, RHS, Q, MaxRecurse: RecursionLimit);
6572}
6573
6574Value *llvm::simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
6575 FastMathFlags FMF, const SimplifyQuery &Q) {
6576 return ::simplifyBinOp(Opcode, LHS, RHS, FMF, Q, MaxRecurse: RecursionLimit);
6577}
6578
6579/// Given operands for a CmpInst, see if we can fold the result.
6580static Value *simplifyCmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS,
6581 const SimplifyQuery &Q, unsigned MaxRecurse) {
6582 if (CmpInst::isIntPredicate(P: Predicate))
6583 return simplifyICmpInst(Pred: Predicate, LHS, RHS, Q, MaxRecurse);
6584 return simplifyFCmpInst(Pred: Predicate, LHS, RHS, FMF: FastMathFlags(), Q, MaxRecurse);
6585}
6586
6587Value *llvm::simplifyCmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS,
6588 const SimplifyQuery &Q) {
6589 return ::simplifyCmpInst(Predicate, LHS, RHS, Q, MaxRecurse: RecursionLimit);
6590}
6591
6592static bool isIdempotent(Intrinsic::ID ID) {
6593 switch (ID) {
6594 default:
6595 return false;
6596
6597 // Unary idempotent: f(f(x)) = f(x)
6598 case Intrinsic::fabs:
6599 case Intrinsic::floor:
6600 case Intrinsic::ceil:
6601 case Intrinsic::trunc:
6602 case Intrinsic::rint:
6603 case Intrinsic::nearbyint:
6604 case Intrinsic::round:
6605 case Intrinsic::roundeven:
6606 case Intrinsic::canonicalize:
6607 case Intrinsic::arithmetic_fence:
6608 return true;
6609 }
6610}
6611
6612/// Return true if the intrinsic rounds a floating-point value to an integral
6613/// floating-point value (not an integer type).
6614static bool removesFPFraction(Intrinsic::ID ID) {
6615 switch (ID) {
6616 default:
6617 return false;
6618
6619 case Intrinsic::floor:
6620 case Intrinsic::ceil:
6621 case Intrinsic::trunc:
6622 case Intrinsic::rint:
6623 case Intrinsic::nearbyint:
6624 case Intrinsic::round:
6625 case Intrinsic::roundeven:
6626 return true;
6627 }
6628}
6629
6630static Value *simplifyRelativeLoad(Constant *Ptr, Constant *Offset,
6631 const DataLayout &DL) {
6632 GlobalValue *PtrSym;
6633 APInt PtrOffset;
6634 if (!IsConstantOffsetFromGlobal(C: Ptr, GV&: PtrSym, Offset&: PtrOffset, DL))
6635 return nullptr;
6636
6637 Type *Int32Ty = Type::getInt32Ty(C&: Ptr->getContext());
6638
6639 auto *OffsetConstInt = dyn_cast<ConstantInt>(Val: Offset);
6640 if (!OffsetConstInt || OffsetConstInt->getBitWidth() > 64)
6641 return nullptr;
6642
6643 APInt OffsetInt = OffsetConstInt->getValue().sextOrTrunc(
6644 width: DL.getIndexTypeSizeInBits(Ty: Ptr->getType()));
6645 if (OffsetInt.srem(RHS: 4) != 0)
6646 return nullptr;
6647
6648 Constant *Loaded =
6649 ConstantFoldLoadFromConstPtr(C: Ptr, Ty: Int32Ty, Offset: std::move(OffsetInt), DL);
6650 if (!Loaded)
6651 return nullptr;
6652
6653 auto *LoadedCE = dyn_cast<ConstantExpr>(Val: Loaded);
6654 if (!LoadedCE)
6655 return nullptr;
6656
6657 if (LoadedCE->getOpcode() == Instruction::Trunc) {
6658 LoadedCE = dyn_cast<ConstantExpr>(Val: LoadedCE->getOperand(i_nocapture: 0));
6659 if (!LoadedCE)
6660 return nullptr;
6661 }
6662
6663 if (LoadedCE->getOpcode() != Instruction::Sub)
6664 return nullptr;
6665
6666 auto *LoadedLHS = dyn_cast<ConstantExpr>(Val: LoadedCE->getOperand(i_nocapture: 0));
6667 if (!LoadedLHS || LoadedLHS->getOpcode() != Instruction::PtrToInt)
6668 return nullptr;
6669 auto *LoadedLHSPtr = LoadedLHS->getOperand(i_nocapture: 0);
6670
6671 Constant *LoadedRHS = LoadedCE->getOperand(i_nocapture: 1);
6672 GlobalValue *LoadedRHSSym;
6673 APInt LoadedRHSOffset;
6674 if (!IsConstantOffsetFromGlobal(C: LoadedRHS, GV&: LoadedRHSSym, Offset&: LoadedRHSOffset,
6675 DL) ||
6676 PtrSym != LoadedRHSSym || PtrOffset != LoadedRHSOffset)
6677 return nullptr;
6678
6679 return LoadedLHSPtr;
6680}
6681
6682// TODO: Need to pass in FastMathFlags
6683static Value *simplifyLdexp(Value *Op0, Value *Op1, const SimplifyQuery &Q,
6684 bool IsStrict) {
6685 // ldexp(undef, x) -> nan
6686 if (Q.isUndefValue(V: Op0))
6687 return ConstantFP::getNaN(Ty: Op0->getType());
6688
6689 if (!IsStrict) {
6690 // TODO: Could insert a canonicalize for strict
6691
6692 // ldexp(x, undef) -> x
6693 if (Q.isUndefValue(V: Op1))
6694 return Op0;
6695 }
6696
6697 const APFloat *C = nullptr;
6698 match(V: Op0, P: PatternMatch::m_APFloat(Res&: C));
6699
6700 // These cases should be safe, even with strictfp.
6701 // ldexp(0.0, x) -> 0.0
6702 // ldexp(-0.0, x) -> -0.0
6703 // ldexp(inf, x) -> inf
6704 // ldexp(-inf, x) -> -inf
6705 if (C && (C->isZero() || C->isInfinity()))
6706 return Op0;
6707
6708 // These are canonicalization dropping, could do it if we knew how we could
6709 // ignore denormal flushes and target handling of nan payload bits.
6710 if (IsStrict)
6711 return nullptr;
6712
6713 // TODO: Could quiet this with strictfp if the exception mode isn't strict.
6714 if (C && C->isNaN())
6715 return ConstantFP::get(Ty: Op0->getType(), V: C->makeQuiet());
6716
6717 // ldexp(x, 0) -> x
6718
6719 // TODO: Could fold this if we know the exception mode isn't
6720 // strict, we know the denormal mode and other target modes.
6721 if (match(V: Op1, P: PatternMatch::m_ZeroInt()))
6722 return Op0;
6723
6724 return nullptr;
6725}
6726
6727static Value *simplifyUnaryIntrinsic(Intrinsic::ID IID, Value *Op0,
6728 FastMathFlags FMF,
6729 const SimplifyQuery &Q) {
6730 // Idempotent functions return the same result when called repeatedly.
6731 if (isIdempotent(ID: IID))
6732 if (auto *II = dyn_cast<IntrinsicInst>(Val: Op0))
6733 if (II->getIntrinsicID() == IID)
6734 return II;
6735
6736 if (removesFPFraction(ID: IID)) {
6737 // Converting from int or calling a rounding function always results in a
6738 // finite integral number or infinity. For those inputs, rounding functions
6739 // always return the same value, so the (2nd) rounding is eliminated. Ex:
6740 // floor (sitofp x) -> sitofp x
6741 // round (ceil x) -> ceil x
6742 auto *II = dyn_cast<IntrinsicInst>(Val: Op0);
6743 if ((II && removesFPFraction(ID: II->getIntrinsicID())) ||
6744 match(V: Op0, P: m_IToFP(Op: m_Value())))
6745 return Op0;
6746 }
6747
6748 Value *X;
6749 switch (IID) {
6750 case Intrinsic::fabs: {
6751 KnownFPClass KnownClass = computeKnownFPClass(V: Op0, InterestedClasses: fcAllFlags, SQ: Q);
6752 if (KnownClass.getSignBit() == false)
6753 return Op0;
6754
6755 if (KnownClass.cannotBeOrderedLessThanZero() &&
6756 KnownClass.isKnownNeverNaN() && FMF.noSignedZeros())
6757 return Op0;
6758
6759 break;
6760 }
6761 case Intrinsic::bswap:
6762 // bswap(bswap(x)) -> x
6763 if (match(V: Op0, P: m_BSwap(Op0: m_Value(V&: X))))
6764 return X;
6765 break;
6766 case Intrinsic::bitreverse:
6767 // bitreverse(bitreverse(x)) -> x
6768 if (match(V: Op0, P: m_BitReverse(Op0: m_Value(V&: X))))
6769 return X;
6770 break;
6771 case Intrinsic::ctpop: {
6772 // ctpop(X) -> 1 iff X is non-zero power of 2.
6773 if (isKnownToBeAPowerOfTwo(V: Op0, DL: Q.DL, /*OrZero*/ false, AC: Q.AC, CtxI: Q.CtxI, DT: Q.DT))
6774 return ConstantInt::get(Ty: Op0->getType(), V: 1);
6775 // If everything but the lowest bit is zero, that bit is the pop-count. Ex:
6776 // ctpop(and X, 1) --> and X, 1
6777 unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
6778 if (MaskedValueIsZero(V: Op0, Mask: APInt::getHighBitsSet(numBits: BitWidth, hiBitsSet: BitWidth - 1),
6779 SQ: Q))
6780 return Op0;
6781 break;
6782 }
6783 case Intrinsic::exp:
6784 // exp(log(x)) -> x
6785 if (FMF.allowReassoc() &&
6786 match(V: Op0, P: m_Intrinsic<Intrinsic::log>(Ops: m_Value(V&: X))))
6787 return X;
6788 break;
6789 case Intrinsic::exp2:
6790 // exp2(log2(x)) -> x
6791 if (FMF.allowReassoc() &&
6792 match(V: Op0, P: m_Intrinsic<Intrinsic::log2>(Ops: m_Value(V&: X))))
6793 return X;
6794 break;
6795 case Intrinsic::exp10:
6796 // exp10(log10(x)) -> x
6797 if (FMF.allowReassoc() &&
6798 match(V: Op0, P: m_Intrinsic<Intrinsic::log10>(Ops: m_Value(V&: X))))
6799 return X;
6800 break;
6801 case Intrinsic::log:
6802 // log(exp(x)) -> x
6803 if (FMF.allowReassoc() &&
6804 match(V: Op0, P: m_Intrinsic<Intrinsic::exp>(Ops: m_Value(V&: X))))
6805 return X;
6806 break;
6807 case Intrinsic::log2:
6808 // log2(exp2(x)) -> x
6809 if (FMF.allowReassoc() &&
6810 (match(V: Op0, P: m_Intrinsic<Intrinsic::exp2>(Ops: m_Value(V&: X))) ||
6811 match(V: Op0,
6812 P: m_Intrinsic<Intrinsic::pow>(Ops: m_SpecificFP(V: 2.0), Ops: m_Value(V&: X)))))
6813 return X;
6814 break;
6815 case Intrinsic::log10:
6816 // log10(pow(10.0, x)) -> x
6817 // log10(exp10(x)) -> x
6818 if (FMF.allowReassoc() &&
6819 (match(V: Op0, P: m_Intrinsic<Intrinsic::exp10>(Ops: m_Value(V&: X))) ||
6820 match(V: Op0,
6821 P: m_Intrinsic<Intrinsic::pow>(Ops: m_SpecificFP(V: 10.0), Ops: m_Value(V&: X)))))
6822 return X;
6823 break;
6824 case Intrinsic::vector_reverse:
6825 // vector.reverse(vector.reverse(x)) -> x
6826 if (match(V: Op0, P: m_VecReverse(Op0: m_Value(V&: X))))
6827 return X;
6828 // vector.reverse(splat(X)) -> splat(X)
6829 if (isSplatValue(V: Op0))
6830 return Op0;
6831 break;
6832 case Intrinsic::structured_gep:
6833 return Op0;
6834 default:
6835 break;
6836 }
6837
6838 return nullptr;
6839}
6840
6841/// Given a min/max intrinsic, see if it can be removed based on having an
6842/// operand that is another min/max intrinsic with shared operand(s). The caller
6843/// is expected to swap the operand arguments to handle commutation.
6844static Value *foldMinMaxSharedOp(Intrinsic::ID IID, Value *Op0, Value *Op1) {
6845 Value *X, *Y;
6846 if (!match(V: Op0, P: m_MaxOrMin(Op0: m_Value(V&: X), Op1: m_Value(V&: Y))))
6847 return nullptr;
6848
6849 auto *MM0 = dyn_cast<IntrinsicInst>(Val: Op0);
6850 if (!MM0)
6851 return nullptr;
6852 Intrinsic::ID IID0 = MM0->getIntrinsicID();
6853
6854 if (Op1 == X || Op1 == Y ||
6855 match(V: Op1, P: m_c_MaxOrMin(L: m_Specific(V: X), R: m_Specific(V: Y)))) {
6856 // max (max X, Y), X --> max X, Y
6857 if (IID0 == IID)
6858 return MM0;
6859 // max (min X, Y), X --> X
6860 if (IID0 == getInverseMinMaxIntrinsic(MinMaxID: IID))
6861 return Op1;
6862 }
6863 return nullptr;
6864}
6865
6866/// Given a min/max intrinsic, see if it can be removed based on having an
6867/// operand that is another min/max intrinsic with shared operand(s). The caller
6868/// is expected to swap the operand arguments to handle commutation.
6869static Value *foldMinimumMaximumSharedOp(Intrinsic::ID IID, Value *Op0,
6870 Value *Op1, unsigned MaxRecurse) {
6871 auto IsMinimumMaximumIntrinsic = [](Intrinsic::ID ID) {
6872 switch (ID) {
6873 case Intrinsic::maxnum:
6874 case Intrinsic::minnum:
6875 case Intrinsic::maximum:
6876 case Intrinsic::minimum:
6877 case Intrinsic::maximumnum:
6878 case Intrinsic::minimumnum:
6879 return true;
6880 default:
6881 return false;
6882 }
6883 };
6884
6885 assert(IsMinimumMaximumIntrinsic(IID) && "Unsupported intrinsic");
6886
6887 auto *M0 = dyn_cast<IntrinsicInst>(Val: Op0);
6888 // If Op0 is not the same intrinsic as IID, do not process.
6889 // This is a difference with integer min/max handling. We do not process the
6890 // case like max(min(X,Y),min(X,Y)) => min(X,Y). But it can be handled by GVN.
6891 if (!M0 || M0->getIntrinsicID() != IID)
6892 return nullptr;
6893 Value *X0 = M0->getOperand(i_nocapture: 0);
6894 Value *Y0 = M0->getOperand(i_nocapture: 1);
6895 // Simple case, m(m(X,Y), X) => m(X, Y)
6896 // m(m(X,Y), Y) => m(X, Y)
6897 // For minimum/maximum, X is NaN => m(NaN, Y) == NaN and m(NaN, NaN) == NaN.
6898 // For minimum/maximum, Y is NaN => m(X, NaN) == NaN and m(NaN, NaN) == NaN.
6899 // For minnum/maxnum, X is NaN => m(NaN, Y) == Y and m(Y, Y) == Y.
6900 // For minnum/maxnum, Y is NaN => m(X, NaN) == X and m(X, NaN) == X.
6901 if (X0 == Op1 || Y0 == Op1)
6902 return M0;
6903
6904 // m(m(X, Y), Z) --> m(X, Y) if m(X, Z) == X or m(Y, Z) == Y.
6905 if (MaxRecurse--) {
6906 if (foldMinimumMaximumSharedOp(IID, Op0: X0, Op1, MaxRecurse) == X0 ||
6907 foldMinimumMaximumSharedOp(IID, Op0: Y0, Op1, MaxRecurse) == Y0)
6908 return M0;
6909 }
6910
6911 auto *M1 = dyn_cast<IntrinsicInst>(Val: Op1);
6912 if (!M1 || !IsMinimumMaximumIntrinsic(M1->getIntrinsicID()))
6913 return nullptr;
6914 Value *X1 = M1->getOperand(i_nocapture: 0);
6915 Value *Y1 = M1->getOperand(i_nocapture: 1);
6916 Intrinsic::ID IID1 = M1->getIntrinsicID();
6917 // we have a case m(m(X,Y),m'(X,Y)) taking into account m' is commutative.
6918 // if m' is m or inversion of m => m(m(X,Y),m'(X,Y)) == m(X,Y).
6919 // For minimum/maximum, X is NaN => m(NaN,Y) == m'(NaN, Y) == NaN.
6920 // For minimum/maximum, Y is NaN => m(X,NaN) == m'(X, NaN) == NaN.
6921 // For minnum/maxnum, X is NaN => m(NaN,Y) == m'(NaN, Y) == Y.
6922 // For minnum/maxnum, Y is NaN => m(X,NaN) == m'(X, NaN) == X.
6923 if ((X0 == X1 && Y0 == Y1) || (X0 == Y1 && Y0 == X1))
6924 if (IID1 == IID || getInverseMinMaxIntrinsic(MinMaxID: IID1) == IID)
6925 return M0;
6926
6927 return nullptr;
6928}
6929
6930enum class MinMaxOptResult {
6931 CannotOptimize = 0,
6932 UseNewConstVal = 1,
6933 UseOtherVal = 2,
6934 // For undef/poison, we can choose to either propgate undef/poison or
6935 // use the LHS value depending on what will allow more optimization.
6936 UseEither = 3
6937};
6938// Get the optimized value for a min/max instruction with a single constant
6939// input (either undef or scalar constantFP). The result may indicate to
6940// use the non-const LHS value, use a new constant value instead (with NaNs
6941// quieted), or to choose either option in the case of undef/poison.
6942static MinMaxOptResult OptimizeConstMinMax(const Constant *RHSConst,
6943 const Intrinsic::ID IID,
6944 FastMathFlags FMF,
6945 Constant **OutNewConstVal) {
6946 assert(OutNewConstVal != nullptr);
6947
6948 bool PropagateNaN = IID == Intrinsic::minimum || IID == Intrinsic::maximum;
6949 bool PropagateSNaN = IID == Intrinsic::minnum || IID == Intrinsic::maxnum;
6950 bool IsMin = IID == Intrinsic::minimum || IID == Intrinsic::minnum ||
6951 IID == Intrinsic::minimumnum;
6952
6953 // min/max(x, poison) -> either x or poison
6954 if (isa<UndefValue>(Val: RHSConst)) {
6955 *OutNewConstVal = const_cast<Constant *>(RHSConst);
6956 return MinMaxOptResult::UseEither;
6957 }
6958
6959 const ConstantFP *CFP = dyn_cast<ConstantFP>(Val: RHSConst);
6960 if (!CFP)
6961 return MinMaxOptResult::CannotOptimize;
6962 APFloat CAPF = CFP->getValueAPF();
6963
6964 // minnum(x, qnan) -> x
6965 // maxnum(x, qnan) -> x
6966 // minnum(x, snan) -> qnan
6967 // maxnum(x, snan) -> qnan
6968 // minimum(X, nan) -> qnan
6969 // maximum(X, nan) -> qnan
6970 // minimumnum(X, nan) -> x
6971 // maximumnum(X, nan) -> x
6972 if (CAPF.isNaN()) {
6973 if (PropagateNaN || (PropagateSNaN && CAPF.isSignaling())) {
6974 *OutNewConstVal = ConstantFP::get(Ty: CFP->getType(), V: CAPF.makeQuiet());
6975 return MinMaxOptResult::UseNewConstVal;
6976 }
6977 return MinMaxOptResult::UseOtherVal;
6978 }
6979
6980 if (CAPF.isInfinity() || (FMF.noInfs() && CAPF.isLargest())) {
6981 // minnum(X, -inf) -> -inf (ignoring sNaN -> qNaN propagation)
6982 // maxnum(X, +inf) -> +inf (ignoring sNaN -> qNaN propagation)
6983 // minimum(X, -inf) -> -inf if nnan
6984 // maximum(X, +inf) -> +inf if nnan
6985 // minimumnum(X, -inf) -> -inf
6986 // maximumnum(X, +inf) -> +inf
6987 if (CAPF.isNegative() == IsMin && (!PropagateNaN || FMF.noNaNs())) {
6988 *OutNewConstVal = const_cast<Constant *>(RHSConst);
6989 return MinMaxOptResult::UseNewConstVal;
6990 }
6991
6992 // minnum(X, +inf) -> X if nnan
6993 // maxnum(X, -inf) -> X if nnan
6994 // minimum(X, +inf) -> X (ignoring quieting of sNaNs)
6995 // maximum(X, -inf) -> X (ignoring quieting of sNaNs)
6996 // minimumnum(X, +inf) -> X if nnan
6997 // maximumnum(X, -inf) -> X if nnan
6998 if (CAPF.isNegative() != IsMin && (PropagateNaN || FMF.noNaNs()))
6999 return MinMaxOptResult::UseOtherVal;
7000 }
7001 return MinMaxOptResult::CannotOptimize;
7002}
7003
7004static Value *simplifySVEIntReduction(Intrinsic::ID IID, Type *ReturnType,
7005 Value *Op0, Value *Op1) {
7006 Constant *C0 = dyn_cast<Constant>(Val: Op0);
7007 Constant *C1 = dyn_cast<Constant>(Val: Op1);
7008 unsigned Width = ReturnType->getPrimitiveSizeInBits();
7009
7010 // All false predicate or reduction of neutral values ==> neutral result.
7011 switch (IID) {
7012 case Intrinsic::aarch64_sve_eorv:
7013 case Intrinsic::aarch64_sve_orv:
7014 case Intrinsic::aarch64_sve_saddv:
7015 case Intrinsic::aarch64_sve_uaddv:
7016 case Intrinsic::aarch64_sve_umaxv:
7017 if ((C0 && C0->isNullValue()) || (C1 && C1->isNullValue()))
7018 return ConstantInt::get(Ty: ReturnType, V: 0);
7019 break;
7020 case Intrinsic::aarch64_sve_andv:
7021 case Intrinsic::aarch64_sve_uminv:
7022 if ((C0 && C0->isNullValue()) || (C1 && C1->isAllOnesValue()))
7023 return ConstantInt::get(Ty: ReturnType, V: APInt::getMaxValue(numBits: Width));
7024 break;
7025 case Intrinsic::aarch64_sve_smaxv:
7026 if ((C0 && C0->isNullValue()) || (C1 && C1->isMinSignedValue()))
7027 return ConstantInt::get(Ty: ReturnType, V: APInt::getSignedMinValue(numBits: Width));
7028 break;
7029 case Intrinsic::aarch64_sve_sminv:
7030 if ((C0 && C0->isNullValue()) || (C1 && C1->isMaxSignedValue()))
7031 return ConstantInt::get(Ty: ReturnType, V: APInt::getSignedMaxValue(numBits: Width));
7032 break;
7033 }
7034
7035 switch (IID) {
7036 case Intrinsic::aarch64_sve_andv:
7037 case Intrinsic::aarch64_sve_orv:
7038 case Intrinsic::aarch64_sve_smaxv:
7039 case Intrinsic::aarch64_sve_sminv:
7040 case Intrinsic::aarch64_sve_umaxv:
7041 case Intrinsic::aarch64_sve_uminv:
7042 // sve_reduce_##(all, splat(X)) ==> X
7043 if (C0 && C0->isAllOnesValue()) {
7044 if (Value *SplatVal = getSplatValue(V: Op1)) {
7045 assert(SplatVal->getType() == ReturnType && "Unexpected result type!");
7046 return SplatVal;
7047 }
7048 }
7049 break;
7050 case Intrinsic::aarch64_sve_eorv:
7051 // sve_reduce_xor(all, splat(X)) ==> 0
7052 if (C0 && C0->isAllOnesValue())
7053 if (getSplatValue(V: Op1))
7054 return ConstantInt::get(Ty: ReturnType, V: 0);
7055 break;
7056 }
7057
7058 return nullptr;
7059}
7060
7061static Value *simplifyBinaryIntrinsic(Intrinsic::ID IID, Type *ReturnType,
7062 Value *Op0, Value *Op1, FastMathFlags FMF,
7063 const SimplifyQuery &Q) {
7064 unsigned BitWidth = ReturnType->getScalarSizeInBits();
7065 switch (IID) {
7066 case Intrinsic::vector_shuffle:
7067 // A poison mask selects poison in every lane.
7068 if (isa<PoisonValue>(Val: Op1))
7069 return PoisonValue::get(T: ReturnType);
7070 // The only in-range index into a one-element vector is 0, and other
7071 // indices select poison, so the shuffle is its source.
7072 if (cast<VectorType>(Val: ReturnType)->getElementCount().isScalar())
7073 return Op0;
7074 break;
7075 case Intrinsic::get_active_lane_mask: {
7076 if (match(V: Op1, P: m_Zero()))
7077 return ConstantInt::getFalse(Ty: ReturnType);
7078
7079 const Function *F = Q.getFunction();
7080 if (!F)
7081 break;
7082
7083 auto *ScalableTy = dyn_cast<ScalableVectorType>(Val: ReturnType);
7084 Attribute Attr = F->getFnAttribute(Kind: Attribute::VScaleRange);
7085 if (ScalableTy && Attr.isValid()) {
7086 std::optional<unsigned> VScaleMax = Attr.getVScaleRangeMax();
7087 if (!VScaleMax)
7088 break;
7089 uint64_t MaxPossibleMaskElements =
7090 (uint64_t)ScalableTy->getMinNumElements() * (*VScaleMax);
7091
7092 const APInt *Op1Val;
7093 if (match(V: Op0, P: m_Zero()) && match(V: Op1, P: m_APInt(Res&: Op1Val)) &&
7094 Op1Val->uge(RHS: MaxPossibleMaskElements))
7095 return ConstantInt::getAllOnesValue(Ty: ReturnType);
7096 }
7097 break;
7098 }
7099 case Intrinsic::abs:
7100 // abs(abs(x)) -> abs(x). We don't need to worry about the nsw arg here.
7101 // It is always ok to pick the earlier abs. We'll just lose nsw if its only
7102 // on the outer abs.
7103 if (match(V: Op0, P: m_Intrinsic<Intrinsic::abs>(Ops: m_Value(), Ops: m_Value())))
7104 return Op0;
7105 break;
7106
7107 case Intrinsic::cttz: {
7108 Value *X;
7109 if (match(V: Op0, P: m_Shl(L: m_One(), R: m_Value(V&: X))))
7110 return X;
7111 break;
7112 }
7113 case Intrinsic::ctlz: {
7114 Value *X;
7115 if (match(V: Op0, P: m_LShr(L: m_Negative(), R: m_Value(V&: X))))
7116 return X;
7117 if (match(V: Op0, P: m_AShr(L: m_Negative(), R: m_Value())))
7118 return Constant::getNullValue(Ty: ReturnType);
7119 break;
7120 }
7121 case Intrinsic::pdep: {
7122 if (match(V: Op0, P: m_Zero()))
7123 return Constant::getNullValue(Ty: ReturnType);
7124 if (match(V: Op1, P: m_Zero()))
7125 return Constant::getNullValue(Ty: ReturnType);
7126 if (match(V: Op1, P: m_AllOnes()))
7127 return Op0;
7128 break;
7129 }
7130 case Intrinsic::pext: {
7131 if (match(V: Op0, P: m_Zero()))
7132 return Constant::getNullValue(Ty: ReturnType);
7133 if (match(V: Op1, P: m_Zero()))
7134 return Constant::getNullValue(Ty: ReturnType);
7135 if (match(V: Op1, P: m_AllOnes()))
7136 return Op0;
7137 break;
7138 }
7139 case Intrinsic::ptrmask: {
7140 // NOTE: We can't apply this simplifications based on the value of Op1
7141 // because we need to preserve provenance.
7142 if (Q.isUndefValue(V: Op0) || match(V: Op0, P: m_Zero()))
7143 return Constant::getNullValue(Ty: Op0->getType());
7144
7145 assert(Op1->getType()->getScalarSizeInBits() ==
7146 Q.DL.getIndexTypeSizeInBits(Op0->getType()) &&
7147 "Invalid mask width");
7148 // If index-width (mask size) is less than pointer-size then mask is
7149 // 1-extended.
7150 if (match(V: Op1, P: m_PtrToIntOrAddr(Op: m_Specific(V: Op0))))
7151 return Op0;
7152
7153 // NOTE: We may have attributes associated with the return value of the
7154 // llvm.ptrmask intrinsic that will be lost when we just return the
7155 // operand. We should try to preserve them.
7156 if (match(V: Op1, P: m_AllOnes()) || Q.isUndefValue(V: Op1))
7157 return Op0;
7158
7159 Constant *C;
7160 if (match(V: Op1, P: m_ImmConstant(C))) {
7161 KnownBits PtrKnown = computeKnownBits(V: Op0, Q);
7162 // See if we only masking off bits we know are already zero due to
7163 // alignment.
7164 APInt IrrelevantPtrBits =
7165 PtrKnown.Zero.zextOrTrunc(width: C->getType()->getScalarSizeInBits());
7166 C = ConstantFoldBinaryOpOperands(
7167 Opcode: Instruction::Or, LHS: C, RHS: ConstantInt::get(Ty: C->getType(), V: IrrelevantPtrBits),
7168 DL: Q.DL);
7169 if (C != nullptr && C->isAllOnesValue())
7170 return Op0;
7171 }
7172 break;
7173 }
7174 case Intrinsic::smax:
7175 case Intrinsic::smin:
7176 case Intrinsic::umax:
7177 case Intrinsic::umin: {
7178 // If the arguments are the same, this is a no-op.
7179 if (Op0 == Op1)
7180 return Op0;
7181
7182 // Canonicalize immediate constant operand as Op1.
7183 if (match(V: Op0, P: m_ImmConstant()))
7184 std::swap(a&: Op0, b&: Op1);
7185
7186 // Assume undef is the limit value.
7187 if (Q.isUndefValue(V: Op1))
7188 return ConstantInt::get(
7189 Ty: ReturnType, V: MinMaxIntrinsic::getSaturationPoint(ID: IID, numBits: BitWidth));
7190
7191 const APInt *C;
7192 if (match(V: Op1, P: m_APIntAllowPoison(Res&: C))) {
7193 // Clamp to limit value. For example:
7194 // umax(i8 %x, i8 255) --> 255
7195 if (*C == MinMaxIntrinsic::getSaturationPoint(ID: IID, numBits: BitWidth))
7196 return ConstantInt::get(Ty: ReturnType, V: *C);
7197
7198 // If the constant op is the opposite of the limit value, the other must
7199 // be larger/smaller or equal. For example:
7200 // umin(i8 %x, i8 255) --> %x
7201 if (*C == MinMaxIntrinsic::getSaturationPoint(
7202 ID: getInverseMinMaxIntrinsic(MinMaxID: IID), numBits: BitWidth))
7203 return Op0;
7204
7205 // Remove nested call if constant operands allow it. Example:
7206 // max (max X, 7), 5 -> max X, 7
7207 auto *MinMax0 = dyn_cast<IntrinsicInst>(Val: Op0);
7208 if (MinMax0 && MinMax0->getIntrinsicID() == IID) {
7209 // TODO: loosen undef/splat restrictions for vector constants.
7210 Value *M00 = MinMax0->getOperand(i_nocapture: 0), *M01 = MinMax0->getOperand(i_nocapture: 1);
7211 const APInt *InnerC;
7212 if ((match(V: M00, P: m_APInt(Res&: InnerC)) || match(V: M01, P: m_APInt(Res&: InnerC))) &&
7213 ICmpInst::compare(LHS: *InnerC, RHS: *C,
7214 Pred: ICmpInst::getNonStrictPredicate(
7215 pred: MinMaxIntrinsic::getPredicate(ID: IID))))
7216 return Op0;
7217 }
7218 }
7219
7220 if (Value *V = foldMinMaxSharedOp(IID, Op0, Op1))
7221 return V;
7222 if (Value *V = foldMinMaxSharedOp(IID, Op0: Op1, Op1: Op0))
7223 return V;
7224
7225 ICmpInst::Predicate Pred =
7226 ICmpInst::getNonStrictPredicate(pred: MinMaxIntrinsic::getPredicate(ID: IID));
7227 if (isICmpTrue(Pred, LHS: Op0, RHS: Op1, Q: Q.getWithoutUndef(), MaxRecurse: RecursionLimit))
7228 return Op0;
7229 if (isICmpTrue(Pred, LHS: Op1, RHS: Op0, Q: Q.getWithoutUndef(), MaxRecurse: RecursionLimit))
7230 return Op1;
7231
7232 break;
7233 }
7234 case Intrinsic::scmp:
7235 case Intrinsic::ucmp: {
7236 // Fold to a constant if the relationship between operands can be
7237 // established with certainty
7238 if (isICmpTrue(Pred: CmpInst::ICMP_EQ, LHS: Op0, RHS: Op1, Q, MaxRecurse: RecursionLimit))
7239 return Constant::getNullValue(Ty: ReturnType);
7240
7241 ICmpInst::Predicate PredGT =
7242 IID == Intrinsic::scmp ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
7243 if (isICmpTrue(Pred: PredGT, LHS: Op0, RHS: Op1, Q, MaxRecurse: RecursionLimit))
7244 return ConstantInt::get(Ty: ReturnType, V: 1);
7245
7246 ICmpInst::Predicate PredLT =
7247 IID == Intrinsic::scmp ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
7248 if (isICmpTrue(Pred: PredLT, LHS: Op0, RHS: Op1, Q, MaxRecurse: RecursionLimit))
7249 return ConstantInt::getSigned(Ty: ReturnType, V: -1);
7250
7251 break;
7252 }
7253 case Intrinsic::umulh:
7254 case Intrinsic::smulh:
7255 // Multiply by one.
7256 if (IID == Intrinsic::umulh || BitWidth == 1)
7257 if (match(V: Op0, P: m_One()) || match(V: Op1, P: m_One()))
7258 return Constant::getNullValue(Ty: ReturnType);
7259 // Multiply by undef -> zero (NOT undef!) as other arg could still be
7260 // zero.
7261 if (Q.isUndefValue(V: Op0) || Q.isUndefValue(V: Op1))
7262 return Constant::getNullValue(Ty: ReturnType);
7263
7264 // Multiply by zero.
7265 if (match(V: Op0, P: m_Zero()) || match(V: Op1, P: m_Zero()))
7266 return Constant::getNullValue(Ty: ReturnType);
7267 break;
7268 case Intrinsic::usub_with_overflow:
7269 case Intrinsic::ssub_with_overflow:
7270 // X - X -> { 0, false }
7271 // X - undef -> { 0, false }
7272 // undef - X -> { 0, false }
7273 if (Op0 == Op1 || Q.isUndefValue(V: Op0) || Q.isUndefValue(V: Op1))
7274 return Constant::getNullValue(Ty: ReturnType);
7275 break;
7276 case Intrinsic::uadd_with_overflow:
7277 case Intrinsic::sadd_with_overflow:
7278 // X + undef -> { -1, false }
7279 // undef + x -> { -1, false }
7280 if (Q.isUndefValue(V: Op0) || Q.isUndefValue(V: Op1)) {
7281 return ConstantStruct::get(
7282 T: cast<StructType>(Val: ReturnType),
7283 V: {Constant::getAllOnesValue(Ty: ReturnType->getStructElementType(N: 0)),
7284 Constant::getNullValue(Ty: ReturnType->getStructElementType(N: 1))});
7285 }
7286 break;
7287 case Intrinsic::umul_with_overflow:
7288 case Intrinsic::smul_with_overflow:
7289 // 0 * X -> { 0, false }
7290 // X * 0 -> { 0, false }
7291 if (match(V: Op0, P: m_Zero()) || match(V: Op1, P: m_Zero()))
7292 return Constant::getNullValue(Ty: ReturnType);
7293 // undef * X -> { 0, false }
7294 // X * undef -> { 0, false }
7295 if (Q.isUndefValue(V: Op0) || Q.isUndefValue(V: Op1))
7296 return Constant::getNullValue(Ty: ReturnType);
7297 break;
7298 case Intrinsic::uadd_sat:
7299 // sat(MAX + X) -> MAX
7300 // sat(X + MAX) -> MAX
7301 if (match(V: Op0, P: m_AllOnes()) || match(V: Op1, P: m_AllOnes()))
7302 return Constant::getAllOnesValue(Ty: ReturnType);
7303 [[fallthrough]];
7304 case Intrinsic::sadd_sat:
7305 // sat(X + undef) -> -1
7306 // sat(undef + X) -> -1
7307 // For unsigned: Assume undef is MAX, thus we saturate to MAX (-1).
7308 // For signed: Assume undef is ~X, in which case X + ~X = -1.
7309 if (Q.isUndefValue(V: Op0) || Q.isUndefValue(V: Op1))
7310 return Constant::getAllOnesValue(Ty: ReturnType);
7311
7312 // X + 0 -> X
7313 if (match(V: Op1, P: m_Zero()))
7314 return Op0;
7315 // 0 + X -> X
7316 if (match(V: Op0, P: m_Zero()))
7317 return Op1;
7318 break;
7319 case Intrinsic::usub_sat:
7320 // sat(0 - X) -> 0, sat(X - MAX) -> 0
7321 if (match(V: Op0, P: m_Zero()) || match(V: Op1, P: m_AllOnes()))
7322 return Constant::getNullValue(Ty: ReturnType);
7323 [[fallthrough]];
7324 case Intrinsic::ssub_sat:
7325 // X - X -> 0, X - undef -> 0, undef - X -> 0
7326 if (Op0 == Op1 || Q.isUndefValue(V: Op0) || Q.isUndefValue(V: Op1))
7327 return Constant::getNullValue(Ty: ReturnType);
7328 // X - 0 -> X
7329 if (match(V: Op1, P: m_Zero()))
7330 return Op0;
7331 break;
7332 case Intrinsic::ushl_sat:
7333 case Intrinsic::sshl_sat:
7334 if (isPoisonShift(Amount: Op1, Q))
7335 return PoisonValue::get(T: ReturnType);
7336 // X << 0 -> X
7337 if (match(V: Op1, P: m_Zero()))
7338 return Op0;
7339 // 0 << Y -> 0
7340 if (match(V: Op0, P: m_Zero()))
7341 return Op0;
7342 if (IID == Intrinsic::ushl_sat) {
7343 // -1 u<< Y -> -1
7344 if (match(V: Op0, P: m_AllOnes()))
7345 return Op0;
7346 } else {
7347 // SINT_MAX s<< Y -> SINT_MAX, SINT_MIN s<< Y -> SINT_MIN
7348 if (match(V: Op0, P: m_MaxSignedValue()) || match(V: Op0, P: m_SignMask()))
7349 return Op0;
7350 }
7351 break;
7352 case Intrinsic::load_relative:
7353 if (auto *C0 = dyn_cast<Constant>(Val: Op0))
7354 if (auto *C1 = dyn_cast<Constant>(Val: Op1))
7355 return simplifyRelativeLoad(Ptr: C0, Offset: C1, DL: Q.DL);
7356 break;
7357 case Intrinsic::powi:
7358 if (auto *Power = dyn_cast<ConstantInt>(Val: Op1)) {
7359 // powi(x, 0) -> 1.0
7360 if (Power->isZero())
7361 return ConstantFP::get(Ty: Op0->getType(), V: 1.0);
7362 // powi(x, 1) -> x
7363 if (Power->isOne())
7364 return Op0;
7365 }
7366 break;
7367 case Intrinsic::ldexp:
7368 return simplifyLdexp(Op0, Op1, Q, IsStrict: false);
7369 case Intrinsic::copysign:
7370 // copysign X, X --> X
7371 if (Op0 == Op1)
7372 return Op0;
7373 // copysign -X, X --> X
7374 // copysign X, -X --> -X
7375 if (match(V: Op0, P: m_FNeg(X: m_Specific(V: Op1))) ||
7376 match(V: Op1, P: m_FNeg(X: m_Specific(V: Op0))))
7377 return Op1;
7378 break;
7379 case Intrinsic::is_fpclass: {
7380 uint64_t Mask = cast<ConstantInt>(Val: Op1)->getZExtValue();
7381 // If all tests are made, it doesn't matter what the value is.
7382 if ((Mask & fcAllFlags) == fcAllFlags)
7383 return ConstantInt::get(Ty: ReturnType, V: true);
7384 if ((Mask & fcAllFlags) == 0)
7385 return ConstantInt::get(Ty: ReturnType, V: false);
7386 if (Q.isUndefValue(V: Op0))
7387 return UndefValue::get(T: ReturnType);
7388 break;
7389 }
7390 case Intrinsic::maxnum:
7391 case Intrinsic::minnum:
7392 case Intrinsic::maximum:
7393 case Intrinsic::minimum:
7394 case Intrinsic::maximumnum:
7395 case Intrinsic::minimumnum: {
7396 // In several cases here, we deviate from exact IEEE 754 semantics
7397 // to enable optimizations (as allowed by the LLVM IR spec).
7398 //
7399 // For instance, we may return one of the arguments unmodified instead of
7400 // inserting an llvm.canonicalize to transform input sNaNs into qNaNs,
7401 // or may assume all NaN inputs are qNaNs.
7402
7403 // If the arguments are the same, this is a no-op (ignoring NaN quieting)
7404 if (Op0 == Op1)
7405 return Op0;
7406
7407 // Canonicalize constant operand as Op1.
7408 if (isa<Constant>(Val: Op0))
7409 std::swap(a&: Op0, b&: Op1);
7410
7411 if (Constant *C = dyn_cast<Constant>(Val: Op1)) {
7412 MinMaxOptResult OptResult = MinMaxOptResult::CannotOptimize;
7413 Constant *NewConst = nullptr;
7414
7415 if (VectorType *VTy = dyn_cast<VectorType>(Val: C->getType())) {
7416 ElementCount ElemCount = VTy->getElementCount();
7417
7418 if (Constant *SplatVal = C->getSplatValue()) {
7419 // Handle splat vectors (including scalable vectors)
7420 OptResult = OptimizeConstMinMax(RHSConst: SplatVal, IID, FMF, OutNewConstVal: &NewConst);
7421 if (OptResult == MinMaxOptResult::UseNewConstVal)
7422 NewConst = ConstantVector::getSplat(EC: ElemCount, Elt: NewConst);
7423
7424 } else if (ElemCount.isFixed()) {
7425 // Storage to build up new const return value (with NaNs quieted)
7426 SmallVector<Constant *, 16> NewC(ElemCount.getFixedValue());
7427
7428 // Check elementwise whether we can optimize to either a constant
7429 // value or return the LHS value. We cannot mix and match LHS +
7430 // constant elements, as this would require inserting a new
7431 // VectorShuffle instruction, which is not allowed in simplifyBinOp.
7432 OptResult = MinMaxOptResult::UseEither;
7433 for (unsigned i = 0; i != ElemCount.getFixedValue(); ++i) {
7434 auto *Elt = C->getAggregateElement(Elt: i);
7435 if (!Elt) {
7436 OptResult = MinMaxOptResult::CannotOptimize;
7437 break;
7438 }
7439 auto ElemResult = OptimizeConstMinMax(RHSConst: Elt, IID, FMF, OutNewConstVal: &NewConst);
7440 if (ElemResult == MinMaxOptResult::CannotOptimize ||
7441 (ElemResult != OptResult &&
7442 OptResult != MinMaxOptResult::UseEither &&
7443 ElemResult != MinMaxOptResult::UseEither)) {
7444 OptResult = MinMaxOptResult::CannotOptimize;
7445 break;
7446 }
7447 NewC[i] = NewConst;
7448 if (ElemResult != MinMaxOptResult::UseEither)
7449 OptResult = ElemResult;
7450 }
7451 if (OptResult == MinMaxOptResult::UseNewConstVal)
7452 NewConst = ConstantVector::get(V: NewC);
7453 }
7454 } else {
7455 // Handle scalar inputs
7456 OptResult = OptimizeConstMinMax(RHSConst: C, IID, FMF, OutNewConstVal: &NewConst);
7457 }
7458
7459 if (OptResult == MinMaxOptResult::UseOtherVal ||
7460 OptResult == MinMaxOptResult::UseEither)
7461 return Op0; // Return the other arg (ignoring NaN quieting)
7462 else if (OptResult == MinMaxOptResult::UseNewConstVal)
7463 return NewConst;
7464 }
7465
7466 // Min/max of the same operation with common operand:
7467 // m(m(X, Y), X) --> m(X, Y) (4 commuted variants), also through nested m,
7468 // e.g. m(m(m(X, Y), Z), X) --> m(m(X, Y), Z).
7469 if (Value *V = foldMinimumMaximumSharedOp(IID, Op0, Op1, MaxRecurse: RecursionLimit))
7470 return V;
7471 if (Value *V = foldMinimumMaximumSharedOp(IID, Op0: Op1, Op1: Op0, MaxRecurse: RecursionLimit))
7472 return V;
7473
7474 break;
7475 }
7476 case Intrinsic::vector_extract: {
7477 // (extract_vector (insert_vector _, X, 0), 0) -> X
7478 unsigned IdxN = cast<ConstantInt>(Val: Op1)->getZExtValue();
7479 Value *X = nullptr;
7480 if (match(V: Op0, P: m_Intrinsic<Intrinsic::vector_insert>(Ops: m_Value(), Ops: m_Value(V&: X),
7481 Ops: m_Zero())) &&
7482 IdxN == 0 && X->getType() == ReturnType)
7483 return X;
7484
7485 break;
7486 }
7487
7488 case Intrinsic::aarch64_sve_andv:
7489 case Intrinsic::aarch64_sve_eorv:
7490 case Intrinsic::aarch64_sve_orv:
7491 case Intrinsic::aarch64_sve_saddv:
7492 case Intrinsic::aarch64_sve_smaxv:
7493 case Intrinsic::aarch64_sve_sminv:
7494 case Intrinsic::aarch64_sve_uaddv:
7495 case Intrinsic::aarch64_sve_umaxv:
7496 case Intrinsic::aarch64_sve_uminv:
7497 return simplifySVEIntReduction(IID, ReturnType, Op0, Op1);
7498 default:
7499 break;
7500 }
7501
7502 return nullptr;
7503}
7504
7505/// interleaveN(extractvalue(deinterleaveN(x), 0), ...,
7506/// extractvalue(deinterleaveN(x), N-1)) --> x
7507static Value *simplifyIdentityInterleave(Intrinsic::ID IID,
7508 ArrayRef<Value *> Args) {
7509 unsigned Factor = getInterleaveIntrinsicFactor(ID: IID);
7510 if (!Factor || Factor != Args.size())
7511 return nullptr;
7512
7513 Intrinsic::ID DeinterleaveID = Intrinsic::getDeinterleaveIntrinsicID(Factor);
7514 IntrinsicInst *DI = nullptr;
7515 for (unsigned Idx = 0; Idx != Factor; ++Idx) {
7516 auto *EV = dyn_cast<ExtractValueInst>(Val: Args[Idx]);
7517 if (!EV || EV->getNumIndices() != 1 || *EV->idx_begin() != Idx)
7518 return nullptr;
7519
7520 auto *CurDI = dyn_cast<IntrinsicInst>(Val: EV->getAggregateOperand());
7521 if (!CurDI || CurDI->getIntrinsicID() != DeinterleaveID)
7522 return nullptr;
7523
7524 if (!DI)
7525 DI = CurDI;
7526 else if (DI != CurDI)
7527 return nullptr;
7528 }
7529
7530 return DI->getArgOperand(i: 0);
7531}
7532
7533Value *llvm::simplifyIntrinsic(Intrinsic::ID IID, Type *ReturnType,
7534 ArrayRef<Value *> Args, FastMathFlags FMF,
7535 const SimplifyQuery &Q,
7536 fp::ExceptionBehavior ExBehavior,
7537 RoundingMode Rounding) {
7538 unsigned NumOperands = Args.size();
7539 if (IID != Intrinsic::not_intrinsic && intrinsicPropagatesPoison(IID) &&
7540 any_of(Range&: Args, P: IsaPred<PoisonValue>))
7541 return PoisonValue::get(T: ReturnType);
7542
7543 // Defer to ConstantFolding if all args are constants.
7544 if (all_of(Range&: Args, P: IsaPred<Constant>))
7545 if (Constant *C = ConstantFoldIntrinsic(
7546 ID: IID, Ops: ArrayRef((Constant *const *)Args.data(), Args.size()),
7547 Ty: ReturnType, DL: Q.DL, CtxF: Q.getFunction()))
7548 return C;
7549
7550 // Most of the intrinsics with no operands have some kind of side effect.
7551 // Don't simplify.
7552 if (!NumOperands) {
7553 switch (IID) {
7554 case Intrinsic::vscale: {
7555 if (!Q.getFunction())
7556 return nullptr;
7557 ConstantRange CR = getVScaleRange(F: Q.getFunction(), BitWidth: 64);
7558 if (const APInt *C = CR.getSingleElement())
7559 return ConstantInt::get(Ty: ReturnType, V: C->getZExtValue());
7560 return nullptr;
7561 }
7562 default:
7563 return nullptr;
7564 }
7565 }
7566
7567 if (Value *V = simplifyIdentityInterleave(IID, Args))
7568 return V;
7569
7570 if (NumOperands == 1)
7571 return simplifyUnaryIntrinsic(IID, Op0: Args[0], FMF, Q);
7572
7573 if (NumOperands == 2)
7574 return simplifyBinaryIntrinsic(IID, ReturnType, Op0: Args[0], Op1: Args[1], FMF, Q);
7575
7576 // Handle intrinsics with 3 or more arguments.
7577 switch (IID) {
7578 case Intrinsic::masked_load:
7579 case Intrinsic::masked_gather: {
7580 Value *MaskArg = Args[1];
7581 Value *PassthruArg = Args[2];
7582 // If the mask is all zeros or poison, the "passthru" argument is the
7583 // result.
7584 if (match(V: MaskArg, P: m_ZeroOrPoison()))
7585 return PassthruArg;
7586 return nullptr;
7587 }
7588 case Intrinsic::fshl:
7589 case Intrinsic::fshr: {
7590 Value *Op0 = Args[0], *Op1 = Args[1], *ShAmtArg = Args[2];
7591
7592 // If both operands are undef, the result is undef.
7593 if (Q.isUndefValue(V: Op0) && Q.isUndefValue(V: Op1))
7594 return UndefValue::get(T: ReturnType);
7595
7596 // If shift amount is undef, assume it is zero.
7597 if (Q.isUndefValue(V: ShAmtArg))
7598 return Args[IID == Intrinsic::fshl ? 0 : 1];
7599
7600 const APInt *ShAmtC;
7601 if (match(V: ShAmtArg, P: m_APInt(Res&: ShAmtC))) {
7602 // If there's effectively no shift, return the 1st arg or 2nd arg.
7603 APInt BitWidth = APInt(ShAmtC->getBitWidth(), ShAmtC->getBitWidth());
7604 const APInt ShAmt = ShAmtC->urem(RHS: BitWidth);
7605 if (ShAmt.isZero())
7606 return Args[IID == Intrinsic::fshl ? 0 : 1];
7607
7608 // fshl (lshr X, C1), (shl X, C2), C1 -> X when C1 + C2 == BW
7609 // fshr (lshr X, C1), (shl X, C2), C2 -> X when C1 + C2 == BW
7610 const APInt *C1, *C2;
7611 Value *X;
7612 if (match(V: Op0, P: m_LShr(L: m_Value(V&: X), R: m_APInt(Res&: C1))) &&
7613 match(V: Op1, P: m_Shl(L: m_Specific(V: X), R: m_APInt(Res&: C2))) &&
7614 *C1 + *C2 == BitWidth && ShAmt == *(IID == Intrinsic::fshl ? C1 : C2))
7615 return X;
7616 }
7617
7618 // Rotating zero by anything is zero.
7619 if (match(V: Op0, P: m_Zero()) && match(V: Op1, P: m_Zero()))
7620 return ConstantInt::getNullValue(Ty: ReturnType);
7621
7622 // Rotating -1 by anything is -1.
7623 if (match(V: Op0, P: m_AllOnes()) && match(V: Op1, P: m_AllOnes()))
7624 return ConstantInt::getAllOnesValue(Ty: ReturnType);
7625
7626 return nullptr;
7627 }
7628 case Intrinsic::experimental_constrained_fma:
7629 return simplifyFPOp(Ops: Args, FMF: {}, Q, ExBehavior, Rounding);
7630 case Intrinsic::fma:
7631 case Intrinsic::fmuladd:
7632 return simplifyFPOp(Ops: Args, FMF: {}, Q, ExBehavior: fp::ebIgnore,
7633 Rounding: RoundingMode::NearestTiesToEven);
7634 case Intrinsic::smul_fix:
7635 case Intrinsic::smul_fix_sat: {
7636 Value *Op0 = Args[0];
7637 Value *Op1 = Args[1];
7638 Value *Op2 = Args[2];
7639
7640 // Canonicalize constant operand as Op1 (ConstantFolding handles the case
7641 // when both Op0 and Op1 are constant so we do not care about that special
7642 // case here).
7643 if (isa<Constant>(Val: Op0))
7644 std::swap(a&: Op0, b&: Op1);
7645
7646 // X * 0 -> 0
7647 if (match(V: Op1, P: m_Zero()))
7648 return Constant::getNullValue(Ty: ReturnType);
7649
7650 // X * undef -> 0
7651 if (Q.isUndefValue(V: Op1))
7652 return Constant::getNullValue(Ty: ReturnType);
7653
7654 // X * (1 << Scale) -> X
7655 APInt ScaledOne =
7656 APInt::getOneBitSet(numBits: ReturnType->getScalarSizeInBits(),
7657 BitNo: cast<ConstantInt>(Val: Op2)->getZExtValue());
7658 if (ScaledOne.isNonNegative() && match(V: Op1, P: m_SpecificInt(V: ScaledOne)))
7659 return Op0;
7660
7661 return nullptr;
7662 }
7663 case Intrinsic::vector_insert: {
7664 Value *Vec = Args[0];
7665 Value *SubVec = Args[1];
7666 Value *Idx = Args[2];
7667
7668 // (insert_vector Y, (extract_vector X, 0), 0) -> X
7669 // where: Y is X, or Y is undef
7670 unsigned IdxN = cast<ConstantInt>(Val: Idx)->getZExtValue();
7671 Value *X = nullptr;
7672 if (match(V: SubVec,
7673 P: m_Intrinsic<Intrinsic::vector_extract>(Ops: m_Value(V&: X), Ops: m_Zero())) &&
7674 (Q.isUndefValue(V: Vec) || Vec == X) && IdxN == 0 &&
7675 X->getType() == ReturnType)
7676 return X;
7677
7678 return nullptr;
7679 }
7680 case Intrinsic::vector_splice_right: {
7681 // splice.right(splice.left(poison, x, offset), poison, offset) -> x
7682 Value *X, *Offset = Args[2];
7683 if (match(V: Args[0], P: m_Intrinsic<Intrinsic::vector_splice_left>(
7684 Ops: m_Poison(), Ops: m_Value(V&: X), Ops: m_Specific(V: Offset))) &&
7685 isa<PoisonValue>(Val: Args[1]))
7686 return X;
7687 [[fallthrough]];
7688 }
7689 case Intrinsic::vector_splice_left: {
7690 Value *Offset = Args[2];
7691 auto *Ty = cast<VectorType>(Val: ReturnType);
7692 if (Q.isUndefValue(V: Offset))
7693 return PoisonValue::get(T: Ty);
7694
7695 unsigned BitWidth = Offset->getType()->getScalarSizeInBits();
7696 ConstantRange NumElts(
7697 APInt(BitWidth, Ty->getElementCount().getKnownMinValue()));
7698 if (Ty->isScalableTy())
7699 NumElts = NumElts.multiply(Other: Q.getFunction()
7700 ? getVScaleRange(F: Q.getFunction(), BitWidth)
7701 : ConstantRange::getFull(BitWidth));
7702
7703 // If we know Offset > NumElts, simplify to poison.
7704 ConstantRange CR = computeConstantRangeIncludingKnownBits(V: Offset, ForSigned: false, SQ: Q);
7705 if (CR.getUnsignedMin().ugt(RHS: NumElts.getUnsignedMax()))
7706 return PoisonValue::get(T: Ty);
7707
7708 // splice.left(a, b, 0) --> a, splice.right(a, b, 0) --> b
7709 if (CR.isSingleElement() && CR.getSingleElement()->isZero())
7710 return IID == Intrinsic::vector_splice_left ? Args[0] : Args[1];
7711
7712 return nullptr;
7713 }
7714 case Intrinsic::experimental_constrained_fadd:
7715 return simplifyFAddInst(Op0: Args[0], Op1: Args[1], FMF, Q, ExBehavior, Rounding);
7716 case Intrinsic::experimental_constrained_fsub:
7717 return simplifyFSubInst(Op0: Args[0], Op1: Args[1], FMF, Q, ExBehavior, Rounding);
7718 case Intrinsic::experimental_constrained_fmul:
7719 return simplifyFMulInst(Op0: Args[0], Op1: Args[1], FMF, Q, ExBehavior, Rounding);
7720 case Intrinsic::experimental_constrained_fdiv:
7721 return simplifyFDivInst(Op0: Args[0], Op1: Args[1], FMF, Q, ExBehavior, Rounding);
7722 case Intrinsic::experimental_constrained_frem:
7723 return simplifyFRemInst(Op0: Args[0], Op1: Args[1], FMF, Q, ExBehavior, Rounding);
7724 case Intrinsic::experimental_constrained_ldexp:
7725 return simplifyLdexp(Op0: Args[0], Op1: Args[1], Q, IsStrict: true);
7726 case Intrinsic::experimental_vp_reverse: {
7727 Value *Vec = Args[0];
7728 Value *EVL = Args[2];
7729
7730 Value *X;
7731 // vp.reverse(vp.reverse(X)) == X (mask doesn't matter)
7732 if (match(V: Vec, P: m_Intrinsic<Intrinsic::experimental_vp_reverse>(
7733 Ops: m_Value(V&: X), Ops: m_Value(), Ops: m_Specific(V: EVL))))
7734 return X;
7735
7736 // vp.reverse(splat(X)) -> splat(X) (regardless of mask and EVL)
7737 if (isSplatValue(V: Vec))
7738 return Vec;
7739 return nullptr;
7740 }
7741 default:
7742 return nullptr;
7743 }
7744}
7745
7746static Value *simplifyIntrinsic(CallBase *Call, ArrayRef<Value *> Args,
7747 const SimplifyQuery &Q) {
7748 // Operand bundles should not be in Args.
7749 assert(Call->arg_size() == Args.size());
7750 Intrinsic::ID IID = Call->getCalledFunction()->getIntrinsicID();
7751 Type *ReturnType = Call->getCalledFunction()->getReturnType();
7752
7753 switch (IID) {
7754 case Intrinsic::experimental_gc_relocate: {
7755 GCRelocateInst &GCR = *cast<GCRelocateInst>(Val: Call);
7756 Value *DerivedPtr = GCR.getDerivedPtr();
7757 Value *BasePtr = GCR.getBasePtr();
7758
7759 // Undef is undef, even after relocation.
7760 if (isa<UndefValue>(Val: DerivedPtr) || isa<UndefValue>(Val: BasePtr)) {
7761 return UndefValue::get(T: GCR.getType());
7762 }
7763
7764 if (auto *PT = dyn_cast<PointerType>(Val: GCR.getType())) {
7765 // For now, the assumption is that the relocation of null will be null
7766 // for most any collector. If this ever changes, a corresponding hook
7767 // should be added to GCStrategy and this code should check it first.
7768 if (isa<ConstantPointerNull>(Val: DerivedPtr)) {
7769 // Use null-pointer of gc_relocate's type to replace it.
7770 return ConstantPointerNull::get(T: PT);
7771 }
7772 }
7773 return nullptr;
7774 }
7775 default: {
7776 // Use the default FP environment if none is found.
7777 fp::ExceptionBehavior ExBehavior = fp::ebIgnore;
7778 RoundingMode Rounding = RoundingMode::NearestTiesToEven;
7779 if (auto *Constrained = dyn_cast<ConstrainedFPIntrinsic>(Val: Call)) {
7780 ExBehavior = Constrained->getExceptionBehavior().value_or(u&: ExBehavior);
7781 Rounding = Constrained->getRoundingMode().value_or(u&: Rounding);
7782 }
7783 return simplifyIntrinsic(
7784 IID, ReturnType, Args, FMF: Call->getFastMathFlagsOrNone(),
7785 Q: Q.getWithFunction(F: Call->getFunction()), ExBehavior, Rounding);
7786 }
7787 }
7788}
7789
7790static Value *tryConstantFoldCall(CallBase *Call, ArrayRef<Value *> Args,
7791 const SimplifyQuery &Q) {
7792 auto *F = Call->getCalledFunction();
7793 if (!F)
7794 return nullptr;
7795
7796 // Check for constant arguments before querying canConstantFoldCallTo.
7797 // Checking arguments is cheaper than TLI classification and attribute checks.
7798 SmallVector<Constant *, 4> ConstantArgs;
7799 ConstantArgs.reserve(N: Args.size());
7800 for (Value *Arg : Args) {
7801 Constant *C = dyn_cast<Constant>(Val: Arg);
7802 if (!C) {
7803 if (isa<MetadataAsValue>(Val: Arg))
7804 continue;
7805 return nullptr;
7806 }
7807 ConstantArgs.push_back(Elt: C);
7808 }
7809
7810 // Check if this is a known library function that we know how to fold.
7811 if (!canConstantFoldCallTo(Call, F, TLI: Q.TLI))
7812 return nullptr;
7813
7814 return ConstantFoldCall(Call, F, Operands: ConstantArgs, TLI: Q.TLI);
7815}
7816
7817Value *llvm::simplifyCall(CallBase *Call, Value *Callee, ArrayRef<Value *> Args,
7818 const SimplifyQuery &Q) {
7819 // Args should not contain operand bundle operands.
7820 assert(Call->arg_size() == Args.size());
7821
7822 // musttail calls can only be simplified if they are also DCEd.
7823 // As we can't guarantee this here, don't simplify them.
7824 if (Call->isMustTailCall())
7825 return nullptr;
7826
7827 // call undef -> poison
7828 // call null -> poison
7829 if (isa<UndefValue>(Val: Callee) || isa<ConstantPointerNull>(Val: Callee))
7830 return PoisonValue::get(T: Call->getType());
7831
7832 if (Value *V = tryConstantFoldCall(Call, Args, Q))
7833 return V;
7834
7835 auto *F = dyn_cast<Function>(Val: Callee);
7836 if (F && F->isIntrinsic())
7837 if (Value *Ret = ::simplifyIntrinsic(Call, Args, Q))
7838 return Ret;
7839
7840 return nullptr;
7841}
7842
7843Value *llvm::simplifyConstrainedFPCall(CallBase *Call, const SimplifyQuery &Q) {
7844 assert(isa<ConstrainedFPIntrinsic>(Call));
7845 SmallVector<Value *, 4> Args(Call->args());
7846 if (Value *V = tryConstantFoldCall(Call, Args, Q))
7847 return V;
7848 if (Value *Ret = ::simplifyIntrinsic(Call, Args, Q))
7849 return Ret;
7850 return nullptr;
7851}
7852
7853/// Given operands for a Freeze, see if we can fold the result.
7854static Value *simplifyFreezeInst(Value *Op0, const SimplifyQuery &Q) {
7855 // Use a utility function defined in ValueTracking.
7856 if (llvm::isGuaranteedNotToBeUndefOrPoison(V: Op0, AC: Q.AC, CtxI: Q.CtxI, DT: Q.DT))
7857 return Op0;
7858 // We have room for improvement.
7859 return nullptr;
7860}
7861
7862Value *llvm::simplifyFreezeInst(Value *Op0, const SimplifyQuery &Q) {
7863 return ::simplifyFreezeInst(Op0, Q);
7864}
7865
7866Value *llvm::simplifyLoadInst(LoadInst *LI, Value *PtrOp,
7867 const SimplifyQuery &Q) {
7868 if (LI->isVolatile())
7869 return nullptr;
7870
7871 if (auto *PtrOpC = dyn_cast<Constant>(Val: PtrOp))
7872 return ConstantFoldLoadFromConstPtr(C: PtrOpC, Ty: LI->getType(), DL: Q.DL);
7873
7874 // We can only fold the load if it is from a constant global with definitive
7875 // initializer. Skip expensive logic if this is not the case.
7876 auto *GV = dyn_cast<GlobalVariable>(Val: getUnderlyingObject(V: PtrOp));
7877 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
7878 return nullptr;
7879
7880 // If GlobalVariable's initializer is uniform, then return the constant
7881 // regardless of its offset.
7882 if (Constant *C = ConstantFoldLoadFromUniformValue(C: GV->getInitializer(),
7883 Ty: LI->getType(), DL: Q.DL))
7884 return C;
7885
7886 // Try to convert operand into a constant by stripping offsets while looking
7887 // through invariant.group intrinsics.
7888 APInt Offset(Q.DL.getIndexTypeSizeInBits(Ty: PtrOp->getType()), 0);
7889 PtrOp = PtrOp->stripAndAccumulateConstantOffsets(
7890 DL: Q.DL, Offset, /* AllowNonInbounts */ AllowNonInbounds: true,
7891 /* AllowInvariantGroup */ true);
7892 if (PtrOp == GV) {
7893 // Index size may have changed due to address space casts.
7894 Offset = Offset.sextOrTrunc(width: Q.DL.getIndexTypeSizeInBits(Ty: PtrOp->getType()));
7895 return ConstantFoldLoadFromConstPtr(C: GV, Ty: LI->getType(), Offset: std::move(Offset),
7896 DL: Q.DL);
7897 }
7898
7899 return nullptr;
7900}
7901
7902/// See if we can compute a simplified version of this instruction.
7903/// If not, this returns null.
7904
7905static Value *simplifyInstructionWithOperands(Instruction *I,
7906 ArrayRef<Value *> NewOps,
7907 const SimplifyQuery &SQ,
7908 unsigned MaxRecurse) {
7909 assert(I->getFunction() && "instruction should be inserted in a function");
7910 assert((!SQ.getFunction() || SQ.getFunction() == I->getFunction()) &&
7911 "context instruction should be in the same function");
7912
7913 const SimplifyQuery Q = SQ.CtxI ? SQ : SQ.getWithInstruction(I);
7914
7915 switch (I->getOpcode()) {
7916 default:
7917 if (all_of(Range&: NewOps, P: IsaPred<Constant>)) {
7918 SmallVector<Constant *, 8> NewConstOps(NewOps.size());
7919 transform(Range&: NewOps, d_first: NewConstOps.begin(),
7920 F: [](Value *V) { return cast<Constant>(Val: V); });
7921 return ConstantFoldInstOperands(I, Ops: NewConstOps, DL: Q.DL, TLI: Q.TLI);
7922 }
7923 return nullptr;
7924 case Instruction::FNeg:
7925 return simplifyFNegInst(Op: NewOps[0], FMF: I->getFastMathFlags(), Q, MaxRecurse);
7926 case Instruction::FAdd:
7927 return simplifyFAddInst(Op0: NewOps[0], Op1: NewOps[1], FMF: I->getFastMathFlags(), Q,
7928 MaxRecurse);
7929 case Instruction::Add:
7930 return simplifyAddInst(
7931 Op0: NewOps[0], Op1: NewOps[1], IsNSW: Q.IIQ.hasNoSignedWrap(Op: cast<BinaryOperator>(Val: I)),
7932 IsNUW: Q.IIQ.hasNoUnsignedWrap(Op: cast<BinaryOperator>(Val: I)), Q, MaxRecurse);
7933 case Instruction::FSub:
7934 return simplifyFSubInst(Op0: NewOps[0], Op1: NewOps[1], FMF: I->getFastMathFlags(), Q,
7935 MaxRecurse);
7936 case Instruction::Sub:
7937 return simplifySubInst(
7938 Op0: NewOps[0], Op1: NewOps[1], IsNSW: Q.IIQ.hasNoSignedWrap(Op: cast<BinaryOperator>(Val: I)),
7939 IsNUW: Q.IIQ.hasNoUnsignedWrap(Op: cast<BinaryOperator>(Val: I)), Q, MaxRecurse);
7940 case Instruction::FMul:
7941 return simplifyFMulInst(Op0: NewOps[0], Op1: NewOps[1], FMF: I->getFastMathFlags(), Q,
7942 MaxRecurse);
7943 case Instruction::Mul:
7944 return simplifyMulInst(
7945 Op0: NewOps[0], Op1: NewOps[1], IsNSW: Q.IIQ.hasNoSignedWrap(Op: cast<BinaryOperator>(Val: I)),
7946 IsNUW: Q.IIQ.hasNoUnsignedWrap(Op: cast<BinaryOperator>(Val: I)), Q, MaxRecurse);
7947 case Instruction::SDiv:
7948 return simplifySDivInst(Op0: NewOps[0], Op1: NewOps[1],
7949 IsExact: Q.IIQ.isExact(Op: cast<BinaryOperator>(Val: I)), Q,
7950 MaxRecurse);
7951 case Instruction::UDiv:
7952 return simplifyUDivInst(Op0: NewOps[0], Op1: NewOps[1],
7953 IsExact: Q.IIQ.isExact(Op: cast<BinaryOperator>(Val: I)), Q,
7954 MaxRecurse);
7955 case Instruction::FDiv:
7956 return simplifyFDivInst(Op0: NewOps[0], Op1: NewOps[1], FMF: I->getFastMathFlags(), Q,
7957 MaxRecurse);
7958 case Instruction::SRem:
7959 return simplifySRemInst(Op0: NewOps[0], Op1: NewOps[1], Q, MaxRecurse);
7960 case Instruction::URem:
7961 return simplifyURemInst(Op0: NewOps[0], Op1: NewOps[1], Q, MaxRecurse);
7962 case Instruction::FRem:
7963 return simplifyFRemInst(Op0: NewOps[0], Op1: NewOps[1], FMF: I->getFastMathFlags(), Q,
7964 MaxRecurse);
7965 case Instruction::Shl:
7966 return simplifyShlInst(
7967 Op0: NewOps[0], Op1: NewOps[1], IsNSW: Q.IIQ.hasNoSignedWrap(Op: cast<BinaryOperator>(Val: I)),
7968 IsNUW: Q.IIQ.hasNoUnsignedWrap(Op: cast<BinaryOperator>(Val: I)), Q, MaxRecurse);
7969 case Instruction::LShr:
7970 return simplifyLShrInst(Op0: NewOps[0], Op1: NewOps[1],
7971 IsExact: Q.IIQ.isExact(Op: cast<BinaryOperator>(Val: I)), Q,
7972 MaxRecurse);
7973 case Instruction::AShr:
7974 return simplifyAShrInst(Op0: NewOps[0], Op1: NewOps[1],
7975 IsExact: Q.IIQ.isExact(Op: cast<BinaryOperator>(Val: I)), Q,
7976 MaxRecurse);
7977 case Instruction::And:
7978 return simplifyAndInst(Op0: NewOps[0], Op1: NewOps[1], Q, MaxRecurse);
7979 case Instruction::Or:
7980 return simplifyOrInst(Op0: NewOps[0], Op1: NewOps[1], Q, MaxRecurse);
7981 case Instruction::Xor:
7982 return simplifyXorInst(Op0: NewOps[0], Op1: NewOps[1], Q, MaxRecurse);
7983 case Instruction::ICmp:
7984 return simplifyICmpInst(Pred: cast<ICmpInst>(Val: I)->getCmpPredicate(), LHS: NewOps[0],
7985 RHS: NewOps[1], Q, MaxRecurse);
7986 case Instruction::FCmp:
7987 return simplifyFCmpInst(Pred: cast<FCmpInst>(Val: I)->getPredicate(), LHS: NewOps[0],
7988 RHS: NewOps[1], FMF: I->getFastMathFlags(), Q, MaxRecurse);
7989 case Instruction::Select: {
7990 FastMathFlags FMF;
7991 if (auto *FPMO = dyn_cast<FPMathOperator>(Val: I))
7992 FMF = FPMO->getFastMathFlags();
7993 return simplifySelectInst(Cond: NewOps[0], TrueVal: NewOps[1], FalseVal: NewOps[2], FMF, Q,
7994 MaxRecurse);
7995 }
7996 case Instruction::GetElementPtr: {
7997 auto *GEPI = cast<GetElementPtrInst>(Val: I);
7998 return simplifyGEPInst(SrcTy: GEPI->getSourceElementType(), Ptr: NewOps[0],
7999 Indices: ArrayRef(NewOps).slice(N: 1), NW: GEPI->getNoWrapFlags(), Q,
8000 MaxRecurse);
8001 }
8002 case Instruction::InsertValue: {
8003 InsertValueInst *IV = cast<InsertValueInst>(Val: I);
8004 return simplifyInsertValueInst(Agg: NewOps[0], Val: NewOps[1], Idxs: IV->getIndices(), Q,
8005 MaxRecurse);
8006 }
8007 case Instruction::InsertElement:
8008 return simplifyInsertElementInst(Vec: NewOps[0], Val: NewOps[1], Idx: NewOps[2], Q);
8009 case Instruction::ExtractValue: {
8010 auto *EVI = cast<ExtractValueInst>(Val: I);
8011 return simplifyExtractValueInst(Agg: NewOps[0], Idxs: EVI->getIndices(), Q,
8012 MaxRecurse);
8013 }
8014 case Instruction::ExtractElement:
8015 return simplifyExtractElementInst(Vec: NewOps[0], Idx: NewOps[1], Q, MaxRecurse);
8016 case Instruction::BitInsert:
8017 return simplifyBitInsertInst(Base: NewOps[0], Val: NewOps[1], Offset: NewOps[2], Q,
8018 MaxRecurse);
8019 case Instruction::BitExtract:
8020 return simplifyBitExtractInst(Ty: I->getType(), Src: NewOps[0], Offset: NewOps[1], Q,
8021 MaxRecurse);
8022 case Instruction::ShuffleVector: {
8023 auto *SVI = cast<ShuffleVectorInst>(Val: I);
8024 return simplifyShuffleVectorInst(Op0: NewOps[0], Op1: NewOps[1],
8025 Mask: SVI->getShuffleMask(), RetTy: SVI->getType(), Q,
8026 MaxRecurse);
8027 }
8028 case Instruction::PHI:
8029 return simplifyPHINode(PN: cast<PHINode>(Val: I), IncomingValues: NewOps, Q);
8030 case Instruction::Call:
8031 return simplifyCall(
8032 Call: cast<CallInst>(Val: I), Callee: NewOps.back(),
8033 Args: NewOps.drop_back(N: 1 + cast<CallInst>(Val: I)->getNumTotalBundleOperands()), Q);
8034 case Instruction::Freeze:
8035 return llvm::simplifyFreezeInst(Op0: NewOps[0], Q);
8036#define HANDLE_CAST_INST(num, opc, clas) case Instruction::opc:
8037#include "llvm/IR/Instruction.def"
8038#undef HANDLE_CAST_INST
8039 if (I->getOpcode() == Instruction::AddrSpaceCast) {
8040 return simplifyAddrSpaceCastInst(
8041 Op: NewOps[0], Ty: I->getType(),
8042 IsNonNull: Q.IIQ.UseInstrInfo && cast<AddrSpaceCastInst>(Val: I)->hasNonNull(), Q,
8043 MaxRecurse);
8044 }
8045
8046 return simplifyCastInst(CastOpc: I->getOpcode(), Op: NewOps[0], Ty: I->getType(), Q,
8047 MaxRecurse);
8048 case Instruction::Alloca:
8049 // No simplifications for Alloca and it can't be constant folded.
8050 return nullptr;
8051 case Instruction::Load:
8052 return simplifyLoadInst(LI: cast<LoadInst>(Val: I), PtrOp: NewOps[0], Q);
8053 }
8054}
8055
8056Value *llvm::simplifyInstructionWithOperands(Instruction *I,
8057 ArrayRef<Value *> NewOps,
8058 const SimplifyQuery &SQ) {
8059 assert(NewOps.size() == I->getNumOperands() &&
8060 "Number of operands should match the instruction!");
8061 return ::simplifyInstructionWithOperands(I, NewOps, SQ, MaxRecurse: RecursionLimit);
8062}
8063
8064Value *llvm::simplifyInstruction(Instruction *I, const SimplifyQuery &SQ) {
8065 SmallVector<Value *, 8> Ops(I->operands());
8066 Value *Result = ::simplifyInstructionWithOperands(I, NewOps: Ops, SQ, MaxRecurse: RecursionLimit);
8067
8068 /// If called on unreachable code, the instruction may simplify to itself.
8069 /// Make life easier for users by detecting that case here, and returning a
8070 /// safe value instead.
8071 return Result == I ? PoisonValue::get(T: I->getType()) : Result;
8072}
8073
8074/// Implementation of recursive simplification through an instruction's
8075/// uses.
8076///
8077/// This is the common implementation of the recursive simplification routines.
8078/// If we have a pre-simplified value in 'SimpleV', that is forcibly used to
8079/// replace the instruction 'I'. Otherwise, we simply add 'I' to the list of
8080/// instructions to process and attempt to simplify it using
8081/// InstructionSimplify. Recursively visited users which could not be
8082/// simplified themselves are to the optional UnsimplifiedUsers set for
8083/// further processing by the caller.
8084///
8085/// This routine returns 'true' only when *it* simplifies something. The passed
8086/// in simplified value does not count toward this.
8087static bool replaceAndRecursivelySimplifyImpl(
8088 Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI,
8089 const DominatorTree *DT, AssumptionCache *AC,
8090 SmallSetVector<Instruction *, 8> *UnsimplifiedUsers = nullptr) {
8091 bool Simplified = false;
8092 SmallSetVector<Instruction *, 8> Worklist;
8093 const DataLayout &DL = I->getDataLayout();
8094
8095 // If we have an explicit value to collapse to, do that round of the
8096 // simplification loop by hand initially.
8097 if (SimpleV) {
8098 for (User *U : I->users())
8099 if (U != I)
8100 Worklist.insert(X: cast<Instruction>(Val: U));
8101
8102 // Replace the instruction with its simplified value.
8103 I->replaceAllUsesWith(V: SimpleV);
8104
8105 if (!I->isEHPad() && !I->isTerminator() && !I->mayHaveSideEffects())
8106 I->eraseFromParent();
8107 } else {
8108 Worklist.insert(X: I);
8109 }
8110
8111 // Note that we must test the size on each iteration, the worklist can grow.
8112 for (unsigned Idx = 0; Idx != Worklist.size(); ++Idx) {
8113 I = Worklist[Idx];
8114
8115 // See if this instruction simplifies.
8116 SimpleV = simplifyInstruction(I, SQ: {DL, TLI, DT, AC});
8117 if (!SimpleV) {
8118 if (UnsimplifiedUsers)
8119 UnsimplifiedUsers->insert(X: I);
8120 continue;
8121 }
8122
8123 Simplified = true;
8124
8125 // Stash away all the uses of the old instruction so we can check them for
8126 // recursive simplifications after a RAUW. This is cheaper than checking all
8127 // uses of To on the recursive step in most cases.
8128 for (User *U : I->users())
8129 Worklist.insert(X: cast<Instruction>(Val: U));
8130
8131 // Replace the instruction with its simplified value.
8132 I->replaceAllUsesWith(V: SimpleV);
8133
8134 if (!I->isEHPad() && !I->isTerminator() && !I->mayHaveSideEffects())
8135 I->eraseFromParent();
8136 }
8137 return Simplified;
8138}
8139
8140bool llvm::replaceAndRecursivelySimplify(
8141 Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI,
8142 const DominatorTree *DT, AssumptionCache *AC,
8143 SmallSetVector<Instruction *, 8> *UnsimplifiedUsers) {
8144 assert(I != SimpleV && "replaceAndRecursivelySimplify(X,X) is not valid!");
8145 assert(SimpleV && "Must provide a simplified value.");
8146 return replaceAndRecursivelySimplifyImpl(I, SimpleV, TLI, DT, AC,
8147 UnsimplifiedUsers);
8148}
8149
8150namespace llvm {
8151const SimplifyQuery getBestSimplifyQuery(Pass &P, Function &F) {
8152 auto *DTWP = P.getAnalysisIfAvailable<DominatorTreeWrapperPass>();
8153 auto *DT = DTWP ? &DTWP->getDomTree() : nullptr;
8154 auto *TLIWP = P.getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
8155 auto *TLI = TLIWP ? &TLIWP->getTLI(F) : nullptr;
8156 auto *ACWP = P.getAnalysisIfAvailable<AssumptionCacheTracker>();
8157 auto *AC = ACWP ? &ACWP->getAssumptionCache(F) : nullptr;
8158 return {F.getDataLayout(), TLI, DT, AC};
8159}
8160
8161const SimplifyQuery getBestSimplifyQuery(LoopStandardAnalysisResults &AR,
8162 const DataLayout &DL) {
8163 return {DL, &AR.TLI, &AR.DT, &AR.AC};
8164}
8165
8166template <class T, class... TArgs>
8167const SimplifyQuery getBestSimplifyQuery(AnalysisManager<T, TArgs...> &AM,
8168 Function &F) {
8169 auto *DT = AM.template getCachedResult<DominatorTreeAnalysis>(F);
8170 auto *TLI = AM.template getCachedResult<TargetLibraryAnalysis>(F);
8171 auto *AC = AM.template getCachedResult<AssumptionAnalysis>(F);
8172 return {F.getDataLayout(), TLI, DT, AC};
8173}
8174template const SimplifyQuery getBestSimplifyQuery(AnalysisManager<Function> &,
8175 Function &);
8176
8177bool SimplifyQuery::isUndefValue(Value *V) const {
8178 if (!CanUseUndef)
8179 return false;
8180
8181 return match(V, P: m_Undef());
8182}
8183
8184} // namespace llvm
8185
8186void InstSimplifyFolder::anchor() {}
8187