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