LLVM 23.0.0git
ValueTracking.cpp
Go to the documentation of this file.
1//===- ValueTracking.cpp - Walk computations to compute properties --------===//
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 contains routines that help analyze properties that chains of
10// computations have.
11//
12//===----------------------------------------------------------------------===//
13
15#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/ScopeExit.h"
23#include "llvm/ADT/StringRef.h"
33#include "llvm/Analysis/Loads.h"
38#include "llvm/IR/Argument.h"
39#include "llvm/IR/Attributes.h"
40#include "llvm/IR/BasicBlock.h"
42#include "llvm/IR/Constant.h"
45#include "llvm/IR/Constants.h"
48#include "llvm/IR/Dominators.h"
50#include "llvm/IR/Function.h"
52#include "llvm/IR/GlobalAlias.h"
53#include "llvm/IR/GlobalValue.h"
55#include "llvm/IR/InstrTypes.h"
56#include "llvm/IR/Instruction.h"
59#include "llvm/IR/Intrinsics.h"
60#include "llvm/IR/IntrinsicsAArch64.h"
61#include "llvm/IR/IntrinsicsAMDGPU.h"
62#include "llvm/IR/IntrinsicsRISCV.h"
63#include "llvm/IR/IntrinsicsX86.h"
64#include "llvm/IR/LLVMContext.h"
65#include "llvm/IR/Metadata.h"
66#include "llvm/IR/Module.h"
67#include "llvm/IR/Operator.h"
69#include "llvm/IR/Type.h"
70#include "llvm/IR/User.h"
71#include "llvm/IR/Value.h"
81#include <algorithm>
82#include <cassert>
83#include <cstdint>
84#include <optional>
85#include <utility>
86
87using namespace llvm;
88using namespace llvm::PatternMatch;
89
90// Controls the number of uses of the value searched for possible
91// dominating comparisons.
92static cl::opt<unsigned> DomConditionsMaxUses("dom-conditions-max-uses",
93 cl::Hidden, cl::init(20));
94
95/// Maximum number of instructions to check between assume and context
96/// instruction.
97static constexpr unsigned MaxInstrsToCheckForFree = 32;
98
99/// Returns the bitwidth of the given scalar or pointer type. For vector types,
100/// returns the element type's bitwidth.
101static unsigned getBitWidth(Type *Ty, const DataLayout &DL) {
102 if (unsigned BitWidth = Ty->getScalarSizeInBits())
103 return BitWidth;
104
105 return DL.getPointerTypeSizeInBits(Ty);
106}
107
108// Given the provided Value and, potentially, a context instruction, return
109// the preferred context instruction (if any).
110static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI) {
111 // If we've been provided with a context instruction, then use that (provided
112 // it has been inserted).
113 if (CxtI && CxtI->getParent())
114 return CxtI;
115
116 // If the value is really an already-inserted instruction, then use that.
117 CxtI = dyn_cast<Instruction>(V);
118 if (CxtI && CxtI->getParent())
119 return CxtI;
120
121 return nullptr;
122}
123
125 const APInt &DemandedElts,
126 APInt &DemandedLHS, APInt &DemandedRHS) {
127 if (isa<ScalableVectorType>(Shuf->getType())) {
128 assert(DemandedElts == APInt(1,1));
129 DemandedLHS = DemandedRHS = DemandedElts;
130 return true;
131 }
132
133 int NumElts =
134 cast<FixedVectorType>(Shuf->getOperand(0)->getType())->getNumElements();
135 return llvm::getShuffleDemandedElts(NumElts, Shuf->getShuffleMask(),
136 DemandedElts, DemandedLHS, DemandedRHS);
137}
138
139static void computeKnownBits(const Value *V, const APInt &DemandedElts,
140 KnownBits &Known, const SimplifyQuery &Q,
141 unsigned Depth);
142
144 const SimplifyQuery &Q, unsigned Depth) {
145 // Since the number of lanes in a scalable vector is unknown at compile time,
146 // we track one bit which is implicitly broadcast to all lanes. This means
147 // that all lanes in a scalable vector are considered demanded.
148 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
149 APInt DemandedElts =
150 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
151 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);
152}
153
155 const DataLayout &DL, AssumptionCache *AC,
156 const Instruction *CxtI, const DominatorTree *DT,
157 bool UseInstrInfo, unsigned Depth) {
159 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo),
160 Depth);
161}
162
164 AssumptionCache *AC, const Instruction *CxtI,
165 const DominatorTree *DT, bool UseInstrInfo,
166 unsigned Depth) {
167 return computeKnownBits(
168 V, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
169}
170
171KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
172 const DataLayout &DL, AssumptionCache *AC,
173 const Instruction *CxtI,
174 const DominatorTree *DT, bool UseInstrInfo,
175 unsigned Depth) {
176 return computeKnownBits(
177 V, DemandedElts,
178 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
179}
180
183 const SimplifyQuery &SQ) {
184 // Look for an inverted mask: (X & ~M) op (Y & M).
185 {
186 Value *M;
187 if (match(LHS, m_c_And(m_Not(m_Value(M)), m_Value())) &&
189 return isGuaranteedNotToBeUndef(M, SQ.AC, SQ.CxtI, SQ.DT)
192 }
193
194 // X op (Y & ~X)
196 return isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT)
199
200 // X op ((X & Y) ^ Y) -- this is the canonical form of the previous pattern
201 // for constant Y.
202 Value *Y;
203 if (match(RHS,
205 bool IsNoUndef = isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT) &&
206 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT);
207 return IsNoUndef ? NoCommonBitsSetResult::Known
209 }
210
211 // Peek through extends to find a 'not' of the other side:
212 // (ext Y) op ext(~Y)
213 if (match(LHS, m_ZExtOrSExt(m_Value(Y))) &&
215 return isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT)
218
219 // Look for: (A & B) op ~(A | B)
220 {
221 Value *A, *B;
222 if (match(LHS, m_And(m_Value(A), m_Value(B))) &&
224 bool IsNoUndef = isGuaranteedNotToBeUndef(A, SQ.AC, SQ.CxtI, SQ.DT) &&
225 isGuaranteedNotToBeUndef(B, SQ.AC, SQ.CxtI, SQ.DT);
226 return IsNoUndef ? NoCommonBitsSetResult::Known
228 }
229 }
230
231 // Look for: (X << V) op (Y >> (BitWidth - V))
232 // or (X >> V) op (Y << (BitWidth - V))
233 {
234 const Value *V;
235 const APInt *R;
236 if (((match(RHS, m_Shl(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&
237 match(LHS, m_LShr(m_Value(), m_Specific(V)))) ||
238 (match(RHS, m_LShr(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&
239 match(LHS, m_Shl(m_Value(), m_Specific(V))))) &&
240 R->uge(LHS->getType()->getScalarSizeInBits()))
242 }
243
245}
246
249 const WithCache<const Value *> &RHSCache,
250 const SimplifyQuery &SQ) {
251 const Value *LHS = LHSCache.getValue();
252 const Value *RHS = RHSCache.getValue();
253
254 assert(LHS->getType() == RHS->getType() &&
255 "LHS and RHS should have the same type");
256 assert(LHS->getType()->isIntOrIntVectorTy() &&
257 "LHS and RHS should be integers");
258
260 if (Result == NoCommonBitsSetResult::Known)
262
263 NoCommonBitsSetResult CommuteResult =
265 if (CommuteResult == NoCommonBitsSetResult::Known)
267
269 RHSCache.getKnownBits(SQ)))
271
275
277}
278
280 const WithCache<const Value *> &RHSCache,
281 const SimplifyQuery &SQ) {
282 NoCommonBitsSetResult Result =
283 getNoCommonBitsSetResult(LHSCache, RHSCache, SQ);
284 return Result == NoCommonBitsSetResult::Known;
285}
286
288 return !I->user_empty() &&
289 all_of(I->users(), match_fn(m_ICmp(m_Value(), m_Zero())));
290}
291
293 return !I->user_empty() && all_of(I->users(), [](const User *U) {
294 CmpPredicate P;
295 return match(U, m_ICmp(P, m_Value(), m_Zero())) && ICmpInst::isEquality(P);
296 });
297}
298
300 bool OrZero, AssumptionCache *AC,
301 const Instruction *CxtI,
302 const DominatorTree *DT, bool UseInstrInfo,
303 unsigned Depth) {
304 return ::isKnownToBeAPowerOfTwo(
305 V, OrZero, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo),
306 Depth);
307}
308
309static bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
310 const SimplifyQuery &Q, unsigned Depth);
311
313 unsigned Depth) {
314 return computeKnownBits(V, SQ, Depth).isNonNegative();
315}
316
318 unsigned Depth) {
319 if (auto *CI = dyn_cast<ConstantInt>(V))
320 return CI->getValue().isStrictlyPositive();
321
322 // If `isKnownNonNegative` ever becomes more sophisticated, make sure to keep
323 // this updated.
325 return Known.isNonNegative() &&
326 (Known.isNonZero() || isKnownNonZero(V, SQ, Depth));
327}
328
330 unsigned Depth) {
331 return computeKnownBits(V, SQ, Depth).isNegative();
332}
333
334static bool isKnownNonEqual(const Value *V1, const Value *V2,
335 const APInt &DemandedElts, const SimplifyQuery &Q,
336 unsigned Depth);
337
338bool llvm::isKnownNonEqual(const Value *V1, const Value *V2,
339 const SimplifyQuery &Q, unsigned Depth) {
340 // We don't support looking through casts.
341 if (V1 == V2 || V1->getType() != V2->getType())
342 return false;
343 auto *FVTy = dyn_cast<FixedVectorType>(V1->getType());
344 APInt DemandedElts =
345 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
346 return ::isKnownNonEqual(V1, V2, DemandedElts, Q, Depth);
347}
348
349bool llvm::MaskedValueIsZero(const Value *V, const APInt &Mask,
350 const SimplifyQuery &SQ, unsigned Depth) {
351 KnownBits Known(Mask.getBitWidth());
353 return Mask.isSubsetOf(Known.Zero);
354}
355
356static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
357 const SimplifyQuery &Q, unsigned Depth);
358
359static unsigned ComputeNumSignBits(const Value *V, const SimplifyQuery &Q,
360 unsigned Depth = 0) {
361 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
362 APInt DemandedElts =
363 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
364 return ComputeNumSignBits(V, DemandedElts, Q, Depth);
365}
366
367unsigned llvm::ComputeNumSignBits(const Value *V, const DataLayout &DL,
368 AssumptionCache *AC, const Instruction *CxtI,
369 const DominatorTree *DT, bool UseInstrInfo,
370 unsigned Depth) {
371 return ::ComputeNumSignBits(
372 V, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
373}
374
376 AssumptionCache *AC,
377 const Instruction *CxtI,
378 const DominatorTree *DT,
379 unsigned Depth) {
380 unsigned SignBits = ComputeNumSignBits(V, DL, AC, CxtI, DT, Depth);
381 return V->getType()->getScalarSizeInBits() - SignBits + 1;
382}
383
384/// Try to detect the lerp pattern: a * (b - c) + c * d
385/// where a >= 0, b >= 0, c >= 0, d >= 0, and b >= c.
386///
387/// In that particular case, we can use the following chain of reasoning:
388///
389/// a * (b - c) + c * d <= a' * (b - c) + a' * c = a' * b where a' = max(a, d)
390///
391/// Since that is true for arbitrary a, b, c and d within our constraints, we
392/// can conclude that:
393///
394/// max(a * (b - c) + c * d) <= max(max(a), max(d)) * max(b) = U
395///
396/// Considering that any result of the lerp would be less or equal to U, it
397/// would have at least the number of leading 0s as in U.
398///
399/// While being quite a specific situation, it is fairly common in computer
400/// graphics in the shape of alpha blending.
401///
402/// Modifies given KnownOut in-place with the inferred information.
403static void computeKnownBitsFromLerpPattern(const Value *Op0, const Value *Op1,
404 const APInt &DemandedElts,
405 KnownBits &KnownOut,
406 const SimplifyQuery &Q,
407 unsigned Depth) {
408
409 Type *Ty = Op0->getType();
410 const unsigned BitWidth = Ty->getScalarSizeInBits();
411
412 // Only handle scalar types for now
413 if (Ty->isVectorTy())
414 return;
415
416 // Try to match: a * (b - c) + c * d.
417 // When a == 1 => A == nullptr, the same applies to d/D as well.
418 const Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
419 const Instruction *SubBC = nullptr;
420
421 const auto MatchSubBC = [&]() {
422 // (b - c) can have two forms that interest us:
423 //
424 // 1. sub nuw %b, %c
425 // 2. xor %c, %b
426 //
427 // For the first case, nuw flag guarantees our requirement b >= c.
428 //
429 // The second case might happen when the analysis can infer that b is a mask
430 // for c and we can transform sub operation into xor (that is usually true
431 // for constant b's). Even though xor is symmetrical, canonicalization
432 // ensures that the constant will be the RHS. We have additional checks
433 // later on to ensure that this xor operation is equivalent to subtraction.
435 m_Xor(m_Value(C), m_Value(B))));
436 };
437
438 const auto MatchASubBC = [&]() {
439 // Cases:
440 // - a * (b - c)
441 // - (b - c) * a
442 // - (b - c) <- a implicitly equals 1
443 return m_CombineOr(m_c_Mul(m_Value(A), MatchSubBC()), MatchSubBC());
444 };
445
446 const auto MatchCD = [&]() {
447 // Cases:
448 // - d * c
449 // - c * d
450 // - c <- d implicitly equals 1
452 };
453
454 const auto Match = [&](const Value *LHS, const Value *RHS) {
455 // We do use m_Specific(C) in MatchCD, so we have to make sure that
456 // it's bound to anything and match(LHS, MatchASubBC()) absolutely
457 // has to evaluate first and return true.
458 //
459 // If Match returns true, it is guaranteed that B != nullptr, C != nullptr.
460 return match(LHS, MatchASubBC()) && match(RHS, MatchCD());
461 };
462
463 if (!Match(Op0, Op1) && !Match(Op1, Op0))
464 return;
465
466 const auto ComputeKnownBitsOrOne = [&](const Value *V) {
467 // For some of the values we use the convention of leaving
468 // it nullptr to signify an implicit constant 1.
469 return V ? computeKnownBits(V, DemandedElts, Q, Depth + 1)
471 };
472
473 // Check that all operands are non-negative
474 const KnownBits KnownA = ComputeKnownBitsOrOne(A);
475 if (!KnownA.isNonNegative())
476 return;
477
478 const KnownBits KnownD = ComputeKnownBitsOrOne(D);
479 if (!KnownD.isNonNegative())
480 return;
481
482 const KnownBits KnownB = computeKnownBits(B, DemandedElts, Q, Depth + 1);
483 if (!KnownB.isNonNegative())
484 return;
485
486 const KnownBits KnownC = computeKnownBits(C, DemandedElts, Q, Depth + 1);
487 if (!KnownC.isNonNegative())
488 return;
489
490 // If we matched subtraction as xor, we need to actually check that xor
491 // is semantically equivalent to subtraction.
492 //
493 // For that to be true, b has to be a mask for c or that b's known
494 // ones cover all known and possible ones of c.
495 if (SubBC->getOpcode() == Instruction::Xor &&
496 !KnownC.getMaxValue().isSubsetOf(KnownB.getMinValue()))
497 return;
498
499 const APInt MaxA = KnownA.getMaxValue();
500 const APInt MaxD = KnownD.getMaxValue();
501 const APInt MaxAD = APIntOps::umax(MaxA, MaxD);
502 const APInt MaxB = KnownB.getMaxValue();
503
504 // We can't infer leading zeros info if the upper-bound estimate wraps.
505 bool Overflow;
506 const APInt UpperBound = MaxAD.umul_ov(MaxB, Overflow);
507
508 if (Overflow)
509 return;
510
511 // If we know that x <= y and both are positive than x has at least the same
512 // number of leading zeros as y.
513 const unsigned MinimumNumberOfLeadingZeros = UpperBound.countl_zero();
514 KnownOut.Zero.setHighBits(MinimumNumberOfLeadingZeros);
515}
516
517static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1,
518 bool NSW, bool NUW,
519 const APInt &DemandedElts,
520 KnownBits &KnownOut, KnownBits &Known2,
521 const SimplifyQuery &Q, unsigned Depth) {
522 computeKnownBits(Op1, DemandedElts, KnownOut, Q, Depth + 1);
523
524 // If one operand is unknown and we have no nowrap information,
525 // the result will be unknown independently of the second operand.
526 if (KnownOut.isUnknown() && !NSW && !NUW)
527 return;
528
529 computeKnownBits(Op0, DemandedElts, Known2, Q, Depth + 1);
530 KnownOut = KnownBits::computeForAddSub(Add, NSW, NUW, Known2, KnownOut);
531
532 if (!Add && NSW && !KnownOut.isNonNegative() &&
534 .value_or(false) ||
535 match(Op1, m_c_SMin(m_Specific(Op0), m_Value()))))
536 KnownOut.makeNonNegative();
537
538 if (Add)
539 // Try to match lerp pattern and combine results
540 computeKnownBitsFromLerpPattern(Op0, Op1, DemandedElts, KnownOut, Q, Depth);
541}
542
543static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,
544 bool NUW, const APInt &DemandedElts,
545 KnownBits &Known, KnownBits &Known2,
546 const SimplifyQuery &Q, unsigned Depth) {
547 computeKnownBits(Op1, DemandedElts, Known, Q, Depth + 1);
548 computeKnownBits(Op0, DemandedElts, Known2, Q, Depth + 1);
549
550 bool isKnownNegative = false;
551 bool isKnownNonNegative = false;
552 // If the multiplication is known not to overflow, compute the sign bit.
553 if (NSW) {
554 if (Op0 == Op1) {
555 // The product of a number with itself is non-negative.
556 isKnownNonNegative = true;
557 } else {
558 bool isKnownNonNegativeOp1 = Known.isNonNegative();
559 bool isKnownNonNegativeOp0 = Known2.isNonNegative();
560 bool isKnownNegativeOp1 = Known.isNegative();
561 bool isKnownNegativeOp0 = Known2.isNegative();
562 // The product of two numbers with the same sign is non-negative.
563 isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) ||
564 (isKnownNonNegativeOp1 && isKnownNonNegativeOp0);
565 if (!isKnownNonNegative && NUW) {
566 // mul nuw nsw with a factor > 1 is non-negative.
567 KnownBits One = KnownBits::makeConstant(APInt(Known.getBitWidth(), 1));
568 isKnownNonNegative = KnownBits::sgt(Known, One).value_or(false) ||
569 KnownBits::sgt(Known2, One).value_or(false);
570 }
571
572 // The product of a negative number and a non-negative number is either
573 // negative or zero.
576 (isKnownNegativeOp1 && isKnownNonNegativeOp0 &&
577 Known2.isNonZero()) ||
578 (isKnownNegativeOp0 && isKnownNonNegativeOp1 && Known.isNonZero());
579 }
580 }
581
582 bool SelfMultiply = Op0 == Op1;
583 if (SelfMultiply)
584 SelfMultiply &=
585 isGuaranteedNotToBeUndef(Op0, Q.AC, Q.CxtI, Q.DT, Depth + 1);
586 Known = KnownBits::mul(Known, Known2, SelfMultiply);
587
588 if (SelfMultiply) {
589 unsigned SignBits = ComputeNumSignBits(Op0, DemandedElts, Q, Depth + 1);
590 unsigned TyBits = Op0->getType()->getScalarSizeInBits();
591 unsigned OutValidBits = 2 * (TyBits - SignBits + 1);
592
593 if (OutValidBits < TyBits) {
594 APInt KnownZeroMask =
595 APInt::getHighBitsSet(TyBits, TyBits - OutValidBits + 1);
596 Known.Zero |= KnownZeroMask;
597 }
598 }
599
600 // Only make use of no-wrap flags if we failed to compute the sign bit
601 // directly. This matters if the multiplication always overflows, in
602 // which case we prefer to follow the result of the direct computation,
603 // though as the program is invoking undefined behaviour we can choose
604 // whatever we like here.
605 if (isKnownNonNegative && !Known.isNegative())
606 Known.makeNonNegative();
607 else if (isKnownNegative && !Known.isNonNegative())
608 Known.makeNegative();
609}
610
612 KnownBits &Known) {
613 unsigned BitWidth = Known.getBitWidth();
614 unsigned NumRanges = Ranges.getNumOperands() / 2;
615 assert(NumRanges >= 1);
616
617 Known.setAllConflict();
618
619 for (unsigned i = 0; i < NumRanges; ++i) {
621 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
623 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
624 ConstantRange Range(Lower->getValue(), Upper->getValue());
625 // BitWidth must equal the Ranges BitWidth for the correct number of high
626 // bits to be set.
627 assert(BitWidth == Range.getBitWidth() &&
628 "Known bit width must match range bit width!");
629
630 // The first CommonPrefixBits of all values in Range are equal.
631 unsigned CommonPrefixBits =
632 (Range.getUnsignedMax() ^ Range.getUnsignedMin()).countl_zero();
633 APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits);
634 APInt UnsignedMax = Range.getUnsignedMax().zextOrTrunc(BitWidth);
635 Known.One &= UnsignedMax & Mask;
636 Known.Zero &= ~UnsignedMax & Mask;
637 }
638}
639
640static bool isEphemeralValueOf(const Instruction *I, const Value *E) {
641 // The instruction defining an assumption's condition itself is always
642 // considered ephemeral to that assumption (even if it has other
643 // non-ephemeral users). See r246696's test case for an example.
644 if (is_contained(I->operands(), E))
645 return true;
646
647 const auto *EI = dyn_cast<Instruction>(E);
648 if (!EI)
649 return false;
650
651 if (EI == I)
652 return true;
653
656 Visited.insert(EI);
657 WorkList.push_back(EI);
658 bool ReachesI = false;
659 while (!WorkList.empty()) {
660 const Instruction *V = WorkList.pop_back_val();
661 for (const User *U : V->users()) {
662 const auto *UI = cast<Instruction>(U);
663 if (UI == I) {
664 ReachesI = true;
665 continue;
666 }
667 if (UI->mayHaveSideEffects() || UI->isTerminator())
668 return false;
669 if (Visited.insert(UI).second)
670 WorkList.push_back(UI);
671 }
672 }
673 return ReachesI;
674}
675
676// Is this an intrinsic that cannot be speculated but also cannot trap?
678 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(I))
679 return CI->isAssumeLikeIntrinsic();
680
681 return false;
682}
683
685 const Instruction *CxtI,
686 const DominatorTree *DT,
687 bool AllowEphemerals) {
688 // There are two restrictions on the use of an assume:
689 // 1. The assume must dominate the context (or the control flow must
690 // reach the assume whenever it reaches the context).
691 // 2. The context must not be in the assume's set of ephemeral values
692 // (otherwise we will use the assume to prove that the condition
693 // feeding the assume is trivially true, thus causing the removal of
694 // the assume).
695
696 if (Inv->getParent() == CxtI->getParent()) {
697 // If Inv and CtxI are in the same block, check if the assume (Inv) is first
698 // in the BB.
699 if (Inv->comesBefore(CxtI))
700 return true;
701
702 // Don't let an assume affect itself - this would cause the problems
703 // `isEphemeralValueOf` is trying to prevent, and it would also make
704 // the loop below go out of bounds.
705 if (!AllowEphemerals && Inv == CxtI)
706 return false;
707
708 // The context comes first, but they're both in the same block.
709 // Make sure there is nothing in between that might interrupt
710 // the control flow, not even CxtI itself.
711 // We limit the scan distance between the assume and its context instruction
712 // to avoid a compile-time explosion. This limit is chosen arbitrarily, so
713 // it can be adjusted if needed (could be turned into a cl::opt).
714 auto Range = make_range(CxtI->getIterator(), Inv->getIterator());
716 return false;
717
718 return AllowEphemerals || !isEphemeralValueOf(Inv, CxtI);
719 }
720
721 // Inv and CxtI are in different blocks.
722 if (DT) {
723 if (DT->dominates(Inv, CxtI))
724 return true;
725 } else if (Inv->getParent() == CxtI->getParent()->getSinglePredecessor() ||
726 Inv->getParent()->isEntryBlock()) {
727 // We don't have a DT, but this trivially dominates.
728 return true;
729 }
730
731 return false;
732}
733
735 const Instruction *CtxI) {
736 // Helper to check if there are any calls in the range that may free memory.
737 unsigned NumChecked = 0;
738 auto hasNoFreeInRange = [&NumChecked](auto Range) {
739 for (const Instruction &I : Range) {
740 if (NumChecked++ > MaxInstrsToCheckForFree)
741 return false;
742
743 if (auto *CB = dyn_cast<CallBase>(&I)) {
744 if (!CB->hasFnAttr(Attribute::NoFree))
745 return false;
746 } else if (I.maySynchronize())
747 return false;
748 }
749 return true;
750 };
751
752 const BasicBlock *CtxBB = CtxI->getParent();
753 const BasicBlock *AssumeBB = Assume->getParent();
754 BasicBlock::const_iterator CtxIter = CtxI->getIterator();
755 if (CtxBB == AssumeBB) {
756 // Same block case: check that Assume comes before CtxI.
757 if (Assume != CtxI && !Assume->comesBefore(CtxI))
758 return false;
759 return hasNoFreeInRange(make_range(Assume->getIterator(), CtxIter));
760 }
761
762 // Handle chain of single-predecessor blocks.
763 const BasicBlock *CurBB = CtxBB;
764 while (true) {
765 if (CurBB == AssumeBB)
766 return hasNoFreeInRange(
767 make_range(Assume->getIterator(), AssumeBB->end()));
768
769 const BasicBlock *PredBB = CurBB->getSinglePredecessor();
770 if (!PredBB)
771 return false;
772
773 if (!hasNoFreeInRange(make_range(CurBB->begin(),
774 CurBB == CtxBB ? CtxIter : CurBB->end())))
775 return false;
776 CurBB = PredBB;
777 }
778}
779
780// TODO: cmpExcludesZero misses many cases where `RHS` is non-constant but
781// we still have enough information about `RHS` to conclude non-zero. For
782// example Pred=EQ, RHS=isKnownNonZero. cmpExcludesZero is called in loops
783// so the extra compile time may not be worth it, but possibly a second API
784// should be created for use outside of loops.
785static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS) {
786 // v u> y implies v != 0.
787 if (Pred == ICmpInst::ICMP_UGT)
788 return true;
789
790 // Special-case v != 0 to also handle v != null.
791 if (Pred == ICmpInst::ICMP_NE)
792 return match(RHS, m_Zero());
793
794 // All other predicates - rely on generic ConstantRange handling.
795 const APInt *C;
796 auto Zero = APInt::getZero(RHS->getType()->getScalarSizeInBits());
797 if (match(RHS, m_APInt(C))) {
799 return !TrueValues.contains(Zero);
800 }
801
803 if (VC == nullptr)
804 return false;
805
806 for (unsigned ElemIdx = 0, NElem = VC->getNumElements(); ElemIdx < NElem;
807 ++ElemIdx) {
809 Pred, VC->getElementAsAPInt(ElemIdx));
810 if (TrueValues.contains(Zero))
811 return false;
812 }
813 return true;
814}
815
816static void breakSelfRecursivePHI(const Use *U, const PHINode *PHI,
817 Value *&ValOut, Instruction *&CtxIOut,
818 const PHINode **PhiOut = nullptr) {
819 ValOut = U->get();
820 if (ValOut == PHI)
821 return;
822 CtxIOut = PHI->getIncomingBlock(*U)->getTerminator();
823 if (PhiOut)
824 *PhiOut = PHI;
825 Value *V;
826 // If the Use is a select of this phi, compute analysis on other arm to break
827 // recursion.
828 // TODO: Min/Max
829 if (match(ValOut, m_Select(m_Value(), m_Specific(PHI), m_Value(V))) ||
830 match(ValOut, m_Select(m_Value(), m_Value(V), m_Specific(PHI))))
831 ValOut = V;
832
833 // Same for select, if this phi is 2-operand phi, compute analysis on other
834 // incoming value to break recursion.
835 // TODO: We could handle any number of incoming edges as long as we only have
836 // two unique values.
837 if (auto *IncPhi = dyn_cast<PHINode>(ValOut);
838 IncPhi && IncPhi->getNumIncomingValues() == 2) {
839 for (int Idx = 0; Idx < 2; ++Idx) {
840 if (IncPhi->getIncomingValue(Idx) == PHI) {
841 ValOut = IncPhi->getIncomingValue(1 - Idx);
842 if (PhiOut)
843 *PhiOut = IncPhi;
844 CtxIOut = IncPhi->getIncomingBlock(1 - Idx)->getTerminator();
845 break;
846 }
847 }
848 }
849}
850
851static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q) {
852 // Use of assumptions is context-sensitive. If we don't have a context, we
853 // cannot use them!
854 if (!Q.AC || !Q.CxtI)
855 return false;
856
857 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
858 if (!Elem.Assume)
859 continue;
860
861 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
862 assert(I->getFunction() == Q.CxtI->getFunction() &&
863 "Got assumption for the wrong function!");
864
865 if (Elem.Index != AssumptionCache::ExprResultIdx) {
867 I->getOperandBundleAt(Elem.Index)) &&
869 return true;
870 continue;
871 }
872
873 // Warning: This loop can end up being somewhat performance sensitive.
874 // We're running this loop for once for each value queried resulting in a
875 // runtime of ~O(#assumes * #values).
876
877 Value *RHS;
878 CmpPredicate Pred;
879 auto m_V = m_CombineOr(m_Specific(V), m_PtrToInt(m_Specific(V)));
880 if (!match(I->getArgOperand(0), m_c_ICmp(Pred, m_V, m_Value(RHS))))
881 continue;
882
884 return true;
885 }
886
887 return false;
888}
889
892 const SimplifyQuery &Q) {
893 if (RHS->getType()->isPointerTy()) {
894 // Handle comparison of pointer to null explicitly, as it will not be
895 // covered by the m_APInt() logic below.
896 if (LHS == V && match(RHS, m_Zero())) {
897 switch (Pred) {
899 Known.setAllZero();
900 break;
903 Known.makeNonNegative();
904 break;
906 Known.makeNegative();
907 break;
908 default:
909 break;
910 }
911 }
912 return;
913 }
914
915 unsigned BitWidth = Known.getBitWidth();
916 auto m_V =
918
919 Value *Y;
920 const APInt *Mask, *C;
921 if (!match(RHS, m_APInt(C)))
922 return;
923
924 uint64_t ShAmt;
925 switch (Pred) {
927 // assume(V = C)
928 if (match(LHS, m_V)) {
929 Known = Known.unionWith(KnownBits::makeConstant(*C));
930 // assume(V & Mask = C)
931 } else if (match(LHS, m_c_And(m_V, m_Value(Y)))) {
932 // For one bits in Mask, we can propagate bits from C to V.
933 Known.One |= *C;
934 if (match(Y, m_APInt(Mask)))
935 Known.Zero |= ~*C & *Mask;
936 // assume(V | Mask = C)
937 } else if (match(LHS, m_c_Or(m_V, m_Value(Y)))) {
938 // For zero bits in Mask, we can propagate bits from C to V.
939 Known.Zero |= ~*C;
940 if (match(Y, m_APInt(Mask)))
941 Known.One |= *C & ~*Mask;
942 // assume(V << ShAmt = C)
943 } else if (match(LHS, m_Shl(m_V, m_ConstantInt(ShAmt))) &&
944 ShAmt < BitWidth) {
945 // For those bits in C that are known, we can propagate them to known
946 // bits in V shifted to the right by ShAmt.
948 RHSKnown >>= ShAmt;
949 Known = Known.unionWith(RHSKnown);
950 // assume(V >> ShAmt = C)
951 } else if (match(LHS, m_Shr(m_V, m_ConstantInt(ShAmt))) &&
952 ShAmt < BitWidth) {
953 // For those bits in RHS that are known, we can propagate them to known
954 // bits in V shifted to the right by C.
956 RHSKnown <<= ShAmt;
957 Known = Known.unionWith(RHSKnown);
958 }
959 break;
960 case ICmpInst::ICMP_NE: {
961 // assume (V & B != 0) where B is a power of 2
962 const APInt *BPow2;
963 if (C->isZero() && match(LHS, m_And(m_V, m_Power2(BPow2))))
964 Known.One |= *BPow2;
965 break;
966 }
967 default: {
968 const APInt *Offset = nullptr;
969 if (match(LHS, m_CombineOr(m_V, m_AddLike(m_V, m_APInt(Offset))))) {
971 if (Offset)
972 LHSRange = LHSRange.sub(*Offset);
973 Known = Known.unionWith(LHSRange.toKnownBits());
974 }
975 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
976 // X & Y u> C -> X u> C && Y u> C
977 // X nuw- Y u> C -> X u> C
978 if (match(LHS, m_c_And(m_V, m_Value())) ||
979 match(LHS, m_NUWSub(m_V, m_Value())))
980 Known.One.setHighBits(
981 (*C + (Pred == ICmpInst::ICMP_UGT)).countLeadingOnes());
982 }
983 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
984 // X | Y u< C -> X u< C && Y u< C
985 // X nuw+ Y u< C -> X u< C && Y u< C
986 if (match(LHS, m_c_Or(m_V, m_Value())) ||
987 match(LHS, m_c_NUWAdd(m_V, m_Value()))) {
988 Known.Zero.setHighBits(
989 (*C - (Pred == ICmpInst::ICMP_ULT)).countLeadingZeros());
990 }
991 }
992 } break;
993 }
994}
995
996static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp,
998 const SimplifyQuery &SQ, bool Invert) {
1000 Invert ? Cmp->getInversePredicate() : Cmp->getPredicate();
1001 Value *LHS = Cmp->getOperand(0);
1002 Value *RHS = Cmp->getOperand(1);
1003
1004 // Handle icmp pred (trunc V), C
1005 if (match(LHS, m_Trunc(m_Specific(V)))) {
1006 KnownBits DstKnown(LHS->getType()->getScalarSizeInBits());
1007 computeKnownBitsFromCmp(LHS, Pred, LHS, RHS, DstKnown, SQ);
1009 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
1010 else
1011 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
1012 return;
1013 }
1014
1015 computeKnownBitsFromCmp(V, Pred, LHS, RHS, Known, SQ);
1016}
1017
1019 KnownBits &Known, const SimplifyQuery &SQ,
1020 bool Invert, unsigned Depth) {
1021 Value *A, *B;
1024 KnownBits Known2(Known.getBitWidth());
1025 KnownBits Known3(Known.getBitWidth());
1026 computeKnownBitsFromCond(V, A, Known2, SQ, Invert, Depth + 1);
1027 computeKnownBitsFromCond(V, B, Known3, SQ, Invert, Depth + 1);
1028 if (Invert ? match(Cond, m_LogicalOr(m_Value(), m_Value()))
1030 Known2 = Known2.unionWith(Known3);
1031 else
1032 Known2 = Known2.intersectWith(Known3);
1033 Known = Known.unionWith(Known2);
1034 return;
1035 }
1036
1037 if (auto *Cmp = dyn_cast<ICmpInst>(Cond)) {
1038 computeKnownBitsFromICmpCond(V, Cmp, Known, SQ, Invert);
1039 return;
1040 }
1041
1042 if (match(Cond, m_Trunc(m_Specific(V)))) {
1043 KnownBits DstKnown(1);
1044 if (Invert) {
1045 DstKnown.setAllZero();
1046 } else {
1047 DstKnown.setAllOnes();
1048 }
1050 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
1051 return;
1052 }
1053 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
1054 return;
1055 }
1056
1058 computeKnownBitsFromCond(V, A, Known, SQ, !Invert, Depth + 1);
1059}
1060
1062 const SimplifyQuery &Q, unsigned Depth) {
1063 // Handle injected condition.
1064 if (Q.CC && Q.CC->AffectedValues.contains(V))
1066
1067 if (!Q.CxtI)
1068 return;
1069
1070 if (Q.DC && Q.DT) {
1071 // Handle dominating conditions.
1072 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
1073 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
1074 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
1075 computeKnownBitsFromCond(V, BI->getCondition(), Known, Q,
1076 /*Invert*/ false, Depth);
1077
1078 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
1079 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
1080 computeKnownBitsFromCond(V, BI->getCondition(), Known, Q,
1081 /*Invert*/ true, Depth);
1082 }
1083
1084 if (Known.hasConflict())
1085 Known.resetAll();
1086 }
1087
1088 if (!Q.AC)
1089 return;
1090
1091 unsigned BitWidth = Known.getBitWidth();
1092
1093 // Note that the patterns below need to be kept in sync with the code
1094 // in AssumptionCache::updateAffectedValues.
1095
1096 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
1097 if (!Elem.Assume)
1098 continue;
1099
1100 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
1101 assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() &&
1102 "Got assumption for the wrong function!");
1103
1104 if (Elem.Index != AssumptionCache::ExprResultIdx) {
1105 if (auto OBU = I->getOperandBundleAt(Elem.Index);
1106 getBundleAttrFromOBU(OBU) == BundleAttr::Align) {
1107 auto [Ptr, _, _2, Alignment, Offset] = getAssumeAlignInfo(OBU);
1108 if (Ptr == V && Alignment && Offset && isPowerOf2_64(*Alignment) &&
1110 Known.Zero |= (*Alignment - 1) & ~*Offset;
1111 Known.One |= (*Alignment - 1) & *Offset;
1112 }
1113 }
1114 continue;
1115 }
1116
1117 // Warning: This loop can end up being somewhat performance sensitive.
1118 // We're running this loop for once for each value queried resulting in a
1119 // runtime of ~O(#assumes * #values).
1120
1121 Value *Arg = I->getArgOperand(0);
1122
1123 if (Arg == V && isValidAssumeForContext(I, Q)) {
1124 assert(BitWidth == 1 && "assume operand is not i1?");
1125 (void)BitWidth;
1126 Known.setAllOnes();
1127 return;
1128 }
1129 if (match(Arg, m_Not(m_Specific(V))) &&
1131 assert(BitWidth == 1 && "assume operand is not i1?");
1132 (void)BitWidth;
1133 Known.setAllZero();
1134 return;
1135 }
1136 auto *Trunc = dyn_cast<TruncInst>(Arg);
1137 if (Trunc && Trunc->getOperand(0) == V &&
1139 if (Trunc->hasNoUnsignedWrap()) {
1141 return;
1142 }
1143 Known.One.setBit(0);
1144 return;
1145 }
1146
1147 // The remaining tests are all recursive, so bail out if we hit the limit.
1149 continue;
1150
1151 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
1152 if (!Cmp)
1153 continue;
1154
1155 if (!isValidAssumeForContext(I, Q))
1156 continue;
1157
1158 computeKnownBitsFromICmpCond(V, Cmp, Known, Q, /*Invert=*/false);
1159 }
1160
1161 // Conflicting assumption: Undefined behavior will occur on this execution
1162 // path.
1163 if (Known.hasConflict())
1164 Known.resetAll();
1165}
1166
1167/// Compute known bits from a shift operator, including those with a
1168/// non-constant shift amount. Known is the output of this function. Known2 is a
1169/// pre-allocated temporary with the same bit width as Known and on return
1170/// contains the known bit of the shift value source. KF is an
1171/// operator-specific function that, given the known-bits and a shift amount,
1172/// compute the implied known-bits of the shift operator's result respectively
1173/// for that shift amount. The results from calling KF are conservatively
1174/// combined for all permitted shift amounts.
1176 const Operator *I, const APInt &DemandedElts, KnownBits &Known,
1177 KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth,
1178 function_ref<KnownBits(const KnownBits &, const KnownBits &, bool)> KF) {
1179 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1180 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1181 // To limit compile-time impact, only query isKnownNonZero() if we know at
1182 // least something about the shift amount.
1183 bool ShAmtNonZero =
1184 Known.isNonZero() ||
1185 (Known.getMaxValue().ult(Known.getBitWidth()) &&
1186 isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth + 1));
1187 Known = KF(Known2, Known, ShAmtNonZero);
1188}
1189
1190static KnownBits
1191getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts,
1192 const KnownBits &KnownLHS, const KnownBits &KnownRHS,
1193 const SimplifyQuery &Q, unsigned Depth) {
1194 unsigned BitWidth = KnownLHS.getBitWidth();
1195 KnownBits KnownOut(BitWidth);
1196 bool IsAnd = false;
1197 bool HasKnownOne = !KnownLHS.One.isZero() || !KnownRHS.One.isZero();
1198 Value *X = nullptr, *Y = nullptr;
1199
1200 switch (I->getOpcode()) {
1201 case Instruction::And:
1202 KnownOut = KnownLHS & KnownRHS;
1203 IsAnd = true;
1204 // and(x, -x) is common idioms that will clear all but lowest set
1205 // bit. If we have a single known bit in x, we can clear all bits
1206 // above it.
1207 // TODO: instcombine often reassociates independent `and` which can hide
1208 // this pattern. Try to match and(x, and(-x, y)) / and(and(x, y), -x).
1209 if (HasKnownOne && match(I, m_c_And(m_Value(X), m_Neg(m_Deferred(X))))) {
1210 // -(-x) == x so using whichever (LHS/RHS) gets us a better result.
1211 if (KnownLHS.countMaxTrailingZeros() <= KnownRHS.countMaxTrailingZeros())
1212 KnownOut = KnownLHS.blsi();
1213 else
1214 KnownOut = KnownRHS.blsi();
1215 }
1216 break;
1217 case Instruction::Or:
1218 KnownOut = KnownLHS | KnownRHS;
1219 break;
1220 case Instruction::Xor:
1221 KnownOut = KnownLHS ^ KnownRHS;
1222 // xor(x, x-1) is common idioms that will clear all but lowest set
1223 // bit. If we have a single known bit in x, we can clear all bits
1224 // above it.
1225 // TODO: xor(x, x-1) is often rewritting as xor(x, x-C) where C !=
1226 // -1 but for the purpose of demanded bits (xor(x, x-C) &
1227 // Demanded) == (xor(x, x-1) & Demanded). Extend the xor pattern
1228 // to use arbitrary C if xor(x, x-C) as the same as xor(x, x-1).
1229 if (HasKnownOne &&
1231 const KnownBits &XBits = I->getOperand(0) == X ? KnownLHS : KnownRHS;
1232 KnownOut = XBits.blsmsk();
1233 }
1234 break;
1235 default:
1236 llvm_unreachable("Invalid Op used in 'analyzeKnownBitsFromAndXorOr'");
1237 }
1238
1239 // and(x, add (x, -1)) is a common idiom that always clears the low bit;
1240 // xor/or(x, add (x, -1)) is an idiom that will always set the low bit.
1241 // here we handle the more general case of adding any odd number by
1242 // matching the form and/xor/or(x, add(x, y)) where y is odd.
1243 // TODO: This could be generalized to clearing any bit set in y where the
1244 // following bit is known to be unset in y.
1245 if (!KnownOut.Zero[0] && !KnownOut.One[0] &&
1249 KnownBits KnownY(BitWidth);
1250 computeKnownBits(Y, DemandedElts, KnownY, Q, Depth + 1);
1251 if (KnownY.countMinTrailingOnes() > 0) {
1252 if (IsAnd)
1253 KnownOut.Zero.setBit(0);
1254 else
1255 KnownOut.One.setBit(0);
1256 }
1257 }
1258 return KnownOut;
1259}
1260
1262 const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q,
1263 unsigned Depth,
1264 const function_ref<KnownBits(const KnownBits &, const KnownBits &)>
1265 KnownBitsFunc) {
1266 APInt DemandedEltsLHS, DemandedEltsRHS;
1268 DemandedElts, DemandedEltsLHS,
1269 DemandedEltsRHS);
1270
1271 const auto ComputeForSingleOpFunc =
1272 [Depth, &Q, KnownBitsFunc](const Value *Op, APInt &DemandedEltsOp) {
1273 return KnownBitsFunc(
1274 computeKnownBits(Op, DemandedEltsOp, Q, Depth + 1),
1275 computeKnownBits(Op, DemandedEltsOp << 1, Q, Depth + 1));
1276 };
1277
1278 if (DemandedEltsRHS.isZero())
1279 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS);
1280 if (DemandedEltsLHS.isZero())
1281 return ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS);
1282
1283 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS)
1284 .intersectWith(ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS));
1285}
1286
1287// Public so this can be used in `SimplifyDemandedUseBits`.
1289 const KnownBits &KnownLHS,
1290 const KnownBits &KnownRHS,
1291 const SimplifyQuery &SQ,
1292 unsigned Depth) {
1293 auto *FVTy = dyn_cast<FixedVectorType>(I->getType());
1294 APInt DemandedElts =
1295 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
1296
1297 return getKnownBitsFromAndXorOr(I, DemandedElts, KnownLHS, KnownRHS, SQ,
1298 Depth);
1299}
1300
1302 Attribute Attr = F->getFnAttribute(Attribute::VScaleRange);
1303 // Without vscale_range, we only know that vscale is non-zero.
1304 if (!Attr.isValid())
1306
1307 unsigned AttrMin = Attr.getVScaleRangeMin();
1308 // Minimum is larger than vscale width, result is always poison.
1309 if ((unsigned)llvm::bit_width(AttrMin) > BitWidth)
1310 return ConstantRange::getEmpty(BitWidth);
1311
1312 APInt Min(BitWidth, AttrMin);
1313 std::optional<unsigned> AttrMax = Attr.getVScaleRangeMax();
1314 if (!AttrMax || (unsigned)llvm::bit_width(*AttrMax) > BitWidth)
1316
1317 return ConstantRange(Min, APInt(BitWidth, *AttrMax) + 1);
1318}
1319
1321 Value *Arm, bool Invert,
1322 const SimplifyQuery &Q, unsigned Depth) {
1323 // If we have a constant arm, we are done.
1324 if (Known.isConstant())
1325 return;
1326
1327 // See what condition implies about the bits of the select arm.
1328 KnownBits CondRes(Known.getBitWidth());
1329 computeKnownBitsFromCond(Arm, Cond, CondRes, Q, Invert, Depth + 1);
1330 // If we don't get any information from the condition, no reason to
1331 // proceed.
1332 if (CondRes.isUnknown())
1333 return;
1334
1335 // We can have conflict if the condition is dead. I.e if we have
1336 // (x | 64) < 32 ? (x | 64) : y
1337 // we will have conflict at bit 6 from the condition/the `or`.
1338 // In that case just return. Its not particularly important
1339 // what we do, as this select is going to be simplified soon.
1340 CondRes = CondRes.unionWith(Known);
1341 if (CondRes.hasConflict())
1342 return;
1343
1344 // Finally make sure the information we found is valid. This is relatively
1345 // expensive so it's left for the very end.
1346 if (!isGuaranteedNotToBeUndef(Arm, Q.AC, Q.CxtI, Q.DT, Depth + 1))
1347 return;
1348
1349 // Finally, we know we get information from the condition and its valid,
1350 // so return it.
1351 Known = std::move(CondRes);
1352}
1353
1354// Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow).
1355// Returns the input and lower/upper bounds.
1356static bool isSignedMinMaxClamp(const Value *Select, const Value *&In,
1357 const APInt *&CLow, const APInt *&CHigh) {
1359 cast<Operator>(Select)->getOpcode() == Instruction::Select &&
1360 "Input should be a Select!");
1361
1362 const Value *LHS = nullptr, *RHS = nullptr;
1364 if (SPF != SPF_SMAX && SPF != SPF_SMIN)
1365 return false;
1366
1367 if (!match(RHS, m_APInt(CLow)))
1368 return false;
1369
1370 const Value *LHS2 = nullptr, *RHS2 = nullptr;
1372 if (getInverseMinMaxFlavor(SPF) != SPF2)
1373 return false;
1374
1375 if (!match(RHS2, m_APInt(CHigh)))
1376 return false;
1377
1378 if (SPF == SPF_SMIN)
1379 std::swap(CLow, CHigh);
1380
1381 In = LHS2;
1382 return CLow->sle(*CHigh);
1383}
1384
1386 const APInt *&CLow,
1387 const APInt *&CHigh) {
1388 assert((II->getIntrinsicID() == Intrinsic::smin ||
1389 II->getIntrinsicID() == Intrinsic::smax) &&
1390 "Must be smin/smax");
1391
1392 Intrinsic::ID InverseID = getInverseMinMaxIntrinsic(II->getIntrinsicID());
1393 auto *InnerII = dyn_cast<IntrinsicInst>(II->getArgOperand(0));
1394 if (!InnerII || InnerII->getIntrinsicID() != InverseID ||
1395 !match(II->getArgOperand(1), m_APInt(CLow)) ||
1396 !match(InnerII->getArgOperand(1), m_APInt(CHigh)))
1397 return false;
1398
1399 if (II->getIntrinsicID() == Intrinsic::smin)
1400 std::swap(CLow, CHigh);
1401 return CLow->sle(*CHigh);
1402}
1403
1405 KnownBits &Known) {
1406 const APInt *CLow, *CHigh;
1407 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
1408 Known = Known.unionWith(
1409 ConstantRange::getNonEmpty(*CLow, *CHigh + 1).toKnownBits());
1410}
1411
1413 const APInt &DemandedElts,
1415 const SimplifyQuery &Q,
1416 unsigned Depth) {
1417 unsigned BitWidth = Known.getBitWidth();
1418
1419 KnownBits Known2(BitWidth);
1420 switch (I->getOpcode()) {
1421 default: break;
1422 case Instruction::Load:
1423 if (MDNode *MD =
1424 Q.IIQ.getMetadata(cast<LoadInst>(I), LLVMContext::MD_range))
1426 break;
1427 case Instruction::And:
1428 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1429 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1430
1431 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1432 break;
1433 case Instruction::Or:
1434 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1435 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1436
1437 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1438 break;
1439 case Instruction::Xor:
1440 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1441 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1442
1443 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1444 break;
1445 case Instruction::Mul: {
1448 computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, NUW,
1449 DemandedElts, Known, Known2, Q, Depth);
1450 break;
1451 }
1452 case Instruction::UDiv: {
1453 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1454 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1455 Known =
1457 break;
1458 }
1459 case Instruction::SDiv: {
1460 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1461 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1462 Known =
1464 break;
1465 }
1466 case Instruction::Select: {
1467 auto ComputeForArm = [&](Value *Arm, bool Invert) {
1468 KnownBits Res(Known.getBitWidth());
1469 computeKnownBits(Arm, DemandedElts, Res, Q, Depth + 1);
1470 adjustKnownBitsForSelectArm(Res, I->getOperand(0), Arm, Invert, Q, Depth);
1471 return Res;
1472 };
1473 // Only known if known in both the LHS and RHS.
1474 Known =
1475 ComputeForArm(I->getOperand(1), /*Invert=*/false)
1476 .intersectWith(ComputeForArm(I->getOperand(2), /*Invert=*/true));
1477 break;
1478 }
1479 case Instruction::FPTrunc:
1480 case Instruction::FPExt:
1481 case Instruction::FPToUI:
1482 case Instruction::FPToSI:
1483 case Instruction::SIToFP:
1484 case Instruction::UIToFP:
1485 break; // Can't work with floating point.
1486 case Instruction::PtrToInt:
1487 case Instruction::PtrToAddr:
1488 case Instruction::IntToPtr:
1489 // Fall through and handle them the same as zext/trunc.
1490 [[fallthrough]];
1491 case Instruction::ZExt:
1492 case Instruction::Trunc: {
1493 Type *SrcTy = I->getOperand(0)->getType();
1494
1495 unsigned SrcBitWidth;
1496 // Note that we handle pointer operands here because of inttoptr/ptrtoint
1497 // which fall through here.
1498 Type *ScalarTy = SrcTy->getScalarType();
1499 SrcBitWidth = ScalarTy->isPointerTy() ?
1500 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
1501 Q.DL.getTypeSizeInBits(ScalarTy);
1502
1503 assert(SrcBitWidth && "SrcBitWidth can't be zero");
1504 Known = Known.anyextOrTrunc(SrcBitWidth);
1505 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1506 if (auto *Inst = dyn_cast<PossiblyNonNegInst>(I);
1507 Inst && Inst->hasNonNeg() && !Known.isNegative())
1508 Known.makeNonNegative();
1509 Known = Known.zextOrTrunc(BitWidth);
1510 break;
1511 }
1512 case Instruction::BitCast: {
1513 Type *SrcTy = I->getOperand(0)->getType();
1514 if (SrcTy->isIntOrPtrTy() &&
1515 // TODO: For now, not handling conversions like:
1516 // (bitcast i64 %x to <2 x i32>)
1517 !I->getType()->isVectorTy()) {
1518 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1519 break;
1520 }
1521
1522 const Value *V;
1523 // Handle bitcast from floating point to integer.
1524 if (match(I, m_ElementWiseBitCast(m_Value(V))) &&
1525 V->getType()->isFPOrFPVectorTy()) {
1526 Type *FPType = V->getType()->getScalarType();
1527 KnownFPClass Result =
1528 computeKnownFPClass(V, DemandedElts, fcAllFlags, Q, Depth + 1);
1529 FPClassTest FPClasses = Result.KnownFPClasses;
1530
1531 // TODO: Treat it as zero/poison if the use of I is unreachable.
1532 if (FPClasses == fcNone)
1533 break;
1534
1535 if (Result.isKnownNever(fcNormal | fcSubnormal | fcNan)) {
1536 Known.setAllConflict();
1537
1538 if (FPClasses & fcInf)
1539 Known = Known.intersectWith(KnownBits::makeConstant(
1540 APFloat::getInf(FPType->getFltSemantics()).bitcastToAPInt()));
1541
1542 if (FPClasses & fcZero)
1543 Known = Known.intersectWith(KnownBits::makeConstant(
1544 APInt::getZero(FPType->getScalarSizeInBits())));
1545
1546 Known.Zero.clearSignBit();
1547 Known.One.clearSignBit();
1548 }
1549
1550 if (Result.SignBit) {
1551 if (*Result.SignBit)
1552 Known.makeNegative();
1553 else
1554 Known.makeNonNegative();
1555 }
1556
1557 break;
1558 }
1559
1560 // Handle cast from vector integer type to scalar or vector integer.
1561 auto *SrcVecTy = dyn_cast<FixedVectorType>(SrcTy);
1562 if (!SrcVecTy || !SrcVecTy->getElementType()->isIntegerTy() ||
1563 !I->getType()->isIntOrIntVectorTy() ||
1564 isa<ScalableVectorType>(I->getType()))
1565 break;
1566
1567 unsigned NumElts = DemandedElts.getBitWidth();
1568 bool IsLE = Q.DL.isLittleEndian();
1569 // Look through a cast from narrow vector elements to wider type.
1570 // Examples: v4i32 -> v2i64, v3i8 -> v24
1571 unsigned SubBitWidth = SrcVecTy->getScalarSizeInBits();
1572 if (BitWidth % SubBitWidth == 0) {
1573 // Known bits are automatically intersected across demanded elements of a
1574 // vector. So for example, if a bit is computed as known zero, it must be
1575 // zero across all demanded elements of the vector.
1576 //
1577 // For this bitcast, each demanded element of the output is sub-divided
1578 // across a set of smaller vector elements in the source vector. To get
1579 // the known bits for an entire element of the output, compute the known
1580 // bits for each sub-element sequentially. This is done by shifting the
1581 // one-set-bit demanded elements parameter across the sub-elements for
1582 // consecutive calls to computeKnownBits. We are using the demanded
1583 // elements parameter as a mask operator.
1584 //
1585 // The known bits of each sub-element are then inserted into place
1586 // (dependent on endian) to form the full result of known bits.
1587 unsigned SubScale = BitWidth / SubBitWidth;
1588 APInt SubDemandedElts = APInt::getZero(NumElts * SubScale);
1589 for (unsigned i = 0; i != NumElts; ++i) {
1590 if (DemandedElts[i])
1591 SubDemandedElts.setBit(i * SubScale);
1592 }
1593
1594 KnownBits KnownSrc(SubBitWidth);
1595 for (unsigned i = 0; i != SubScale; ++i) {
1596 computeKnownBits(I->getOperand(0), SubDemandedElts.shl(i), KnownSrc, Q,
1597 Depth + 1);
1598 unsigned ShiftElt = IsLE ? i : SubScale - 1 - i;
1599 Known.insertBits(KnownSrc, ShiftElt * SubBitWidth);
1600 }
1601 }
1602 // Look through a cast from wider vector elements to narrow type.
1603 // Examples: v2i64 -> v4i32
1604 if (SubBitWidth % BitWidth == 0) {
1605 unsigned SubScale = SubBitWidth / BitWidth;
1606 KnownBits KnownSrc(SubBitWidth);
1607 APInt SubDemandedElts =
1608 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
1609 computeKnownBits(I->getOperand(0), SubDemandedElts, KnownSrc, Q,
1610 Depth + 1);
1611
1612 Known.setAllConflict();
1613 for (unsigned i = 0; i != NumElts; ++i) {
1614 if (DemandedElts[i]) {
1615 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
1616 unsigned Offset = (Shifts % SubScale) * BitWidth;
1617 Known = Known.intersectWith(KnownSrc.extractBits(BitWidth, Offset));
1618 if (Known.isUnknown())
1619 break;
1620 }
1621 }
1622 }
1623 break;
1624 }
1625 case Instruction::SExt: {
1626 // Compute the bits in the result that are not present in the input.
1627 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
1628
1629 Known = Known.trunc(SrcBitWidth);
1630 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1631 // If the sign bit of the input is known set or clear, then we know the
1632 // top bits of the result.
1633 Known = Known.sext(BitWidth);
1634 break;
1635 }
1636 case Instruction::Shl: {
1639 auto KF = [NUW, NSW](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1640 bool ShAmtNonZero) {
1641 return KnownBits::shl(KnownVal, KnownAmt, NUW, NSW, ShAmtNonZero);
1642 };
1643 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1644 KF);
1645 // Trailing zeros of a right-shifted constant never decrease.
1646 const APInt *C;
1647 if (match(I->getOperand(0), m_APInt(C)))
1648 Known.Zero.setLowBits(C->countr_zero());
1649
1650 // shl X, sub(Y, xor(ctlz(X, true), BitWidth-1)) shifts X so that its MSB
1651 // lands at bit Y, when BitWidth is a power of 2.
1652 const APInt *YC;
1653 Value *X = I->getOperand(0);
1654 if (isPowerOf2_32(BitWidth) &&
1655 match(I->getOperand(1),
1657 m_SpecificInt(BitWidth - 1)))) &&
1658 YC->ult(BitWidth - 1)) {
1659 unsigned Y = YC->getZExtValue();
1660 Known.One.setBit(Y);
1661 Known.Zero.setBitsFrom(Y + 1);
1662 }
1663 break;
1664 }
1665 case Instruction::LShr: {
1666 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1667 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1668 bool ShAmtNonZero) {
1669 return KnownBits::lshr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1670 };
1671 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1672 KF);
1673 // Leading zeros of a left-shifted constant never decrease.
1674 const APInt *C;
1675 if (match(I->getOperand(0), m_APInt(C)))
1676 Known.Zero.setHighBits(C->countl_zero());
1677 break;
1678 }
1679 case Instruction::AShr: {
1680 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1681 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1682 bool ShAmtNonZero) {
1683 return KnownBits::ashr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1684 };
1685 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1686 KF);
1687 break;
1688 }
1689 case Instruction::Sub: {
1692 computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW, NUW,
1693 DemandedElts, Known, Known2, Q, Depth);
1694 break;
1695 }
1696 case Instruction::Add: {
1699 computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW, NUW,
1700 DemandedElts, Known, Known2, Q, Depth);
1701 break;
1702 }
1703 case Instruction::SRem:
1704 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1705 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1706 Known = KnownBits::srem(Known, Known2);
1707 break;
1708
1709 case Instruction::URem:
1710 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1711 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1712 Known = KnownBits::urem(Known, Known2);
1713 break;
1714 case Instruction::Alloca:
1715 Known.Zero.setLowBits(Log2(cast<AllocaInst>(I)->getAlign()));
1716 break;
1717 case Instruction::GetElementPtr: {
1718 // Analyze all of the subscripts of this getelementptr instruction
1719 // to determine if we can prove known low zero bits.
1720 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1721 // Accumulate the constant indices in a separate variable
1722 // to minimize the number of calls to computeForAddSub.
1723 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(I->getType());
1724 APInt AccConstIndices(IndexWidth, 0);
1725
1726 auto AddIndexToKnown = [&](KnownBits IndexBits) {
1727 if (IndexWidth == BitWidth) {
1728 // Note that inbounds does *not* guarantee nsw for the addition, as only
1729 // the offset is signed, while the base address is unsigned.
1730 Known = KnownBits::add(Known, IndexBits);
1731 } else {
1732 // If the index width is smaller than the pointer width, only add the
1733 // value to the low bits.
1734 assert(IndexWidth < BitWidth &&
1735 "Index width can't be larger than pointer width");
1736 Known.insertBits(KnownBits::add(Known.trunc(IndexWidth), IndexBits), 0);
1737 }
1738 };
1739
1741 for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
1742 // TrailZ can only become smaller, short-circuit if we hit zero.
1743 if (Known.isUnknown())
1744 break;
1745
1746 Value *Index = I->getOperand(i);
1747
1748 // Handle case when index is zero.
1749 Constant *CIndex = dyn_cast<Constant>(Index);
1750 if (CIndex && CIndex->isNullValue())
1751 continue;
1752
1753 if (StructType *STy = GTI.getStructTypeOrNull()) {
1754 // Handle struct member offset arithmetic.
1755
1756 assert(CIndex &&
1757 "Access to structure field must be known at compile time");
1758
1759 if (CIndex->getType()->isVectorTy())
1760 Index = CIndex->getSplatValue();
1761
1762 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
1763 const StructLayout *SL = Q.DL.getStructLayout(STy);
1764 uint64_t Offset = SL->getElementOffset(Idx);
1765 AccConstIndices += Offset;
1766 continue;
1767 }
1768
1769 // Handle array index arithmetic.
1770 Type *IndexedTy = GTI.getIndexedType();
1771 if (!IndexedTy->isSized()) {
1772 Known.resetAll();
1773 break;
1774 }
1775
1776 TypeSize Stride = GTI.getSequentialElementStride(Q.DL);
1777 uint64_t StrideInBytes = Stride.getKnownMinValue();
1778 if (!Stride.isScalable()) {
1779 // Fast path for constant offset.
1780 if (auto *CI = dyn_cast<ConstantInt>(Index)) {
1781 AccConstIndices +=
1782 CI->getValue().sextOrTrunc(IndexWidth) * StrideInBytes;
1783 continue;
1784 }
1785 }
1786
1787 KnownBits IndexBits =
1788 computeKnownBits(Index, Q, Depth + 1).sextOrTrunc(IndexWidth);
1789 KnownBits ScalingFactor(IndexWidth);
1790 // Multiply by current sizeof type.
1791 // &A[i] == A + i * sizeof(*A[i]).
1792 if (Stride.isScalable()) {
1793 // For scalable types the only thing we know about sizeof is
1794 // that this is a multiple of the minimum size.
1795 ScalingFactor.Zero.setLowBits(llvm::countr_zero(StrideInBytes));
1796 } else {
1797 ScalingFactor =
1798 KnownBits::makeConstant(APInt(IndexWidth, StrideInBytes));
1799 }
1800 AddIndexToKnown(KnownBits::mul(IndexBits, ScalingFactor));
1801 }
1802 if (!Known.isUnknown() && !AccConstIndices.isZero())
1803 AddIndexToKnown(KnownBits::makeConstant(AccConstIndices));
1804 break;
1805 }
1806 case Instruction::PHI: {
1807 const PHINode *P = cast<PHINode>(I);
1808 BinaryOperator *BO = nullptr;
1809 Value *R = nullptr, *L = nullptr;
1810 if (matchSimpleRecurrence(P, BO, R, L)) {
1811 // Handle the case of a simple two-predecessor recurrence PHI.
1812 // There's a lot more that could theoretically be done here, but
1813 // this is sufficient to catch some interesting cases.
1814 unsigned Opcode = BO->getOpcode();
1815
1816 switch (Opcode) {
1817 // If this is a shift recurrence, we know the bits being shifted in. We
1818 // can combine that with information about the start value of the
1819 // recurrence to conclude facts about the result. If this is a udiv
1820 // recurrence, we know that the result can never exceed either the
1821 // numerator or the start value, whichever is greater.
1822 case Instruction::LShr:
1823 case Instruction::AShr:
1824 case Instruction::Shl:
1825 case Instruction::UDiv:
1826 if (BO->getOperand(0) != I)
1827 break;
1828 [[fallthrough]];
1829
1830 // For a urem recurrence, the result can never exceed the start value. The
1831 // phi could either be the numerator or the denominator.
1832 case Instruction::URem: {
1833 // We have matched a recurrence of the form:
1834 // %iv = [R, %entry], [%iv.next, %backedge]
1835 // %iv.next = shift_op %iv, L
1836
1837 // Recurse with the phi context to avoid concern about whether facts
1838 // inferred hold at original context instruction. TODO: It may be
1839 // correct to use the original context. IF warranted, explore and
1840 // add sufficient tests to cover.
1842 RecQ.CxtI = P;
1843 computeKnownBits(R, DemandedElts, Known2, RecQ, Depth + 1);
1844 switch (Opcode) {
1845 case Instruction::Shl:
1846 // A shl recurrence will only increase the tailing zeros
1847 Known.Zero.setLowBits(Known2.countMinTrailingZeros());
1848 break;
1849 case Instruction::LShr:
1850 case Instruction::UDiv:
1851 case Instruction::URem:
1852 // lshr, udiv, and urem recurrences will preserve the leading zeros of
1853 // the start value.
1854 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1855 break;
1856 case Instruction::AShr:
1857 // An ashr recurrence will extend the initial sign bit
1858 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1859 Known.One.setHighBits(Known2.countMinLeadingOnes());
1860 break;
1861 }
1862 break;
1863 }
1864
1865 // Check for operations that have the property that if
1866 // both their operands have low zero bits, the result
1867 // will have low zero bits.
1868 case Instruction::Add:
1869 case Instruction::Sub:
1870 case Instruction::And:
1871 case Instruction::Or:
1872 case Instruction::Mul: {
1873 // Change the context instruction to the "edge" that flows into the
1874 // phi. This is important because that is where the value is actually
1875 // "evaluated" even though it is used later somewhere else. (see also
1876 // D69571).
1878
1879 unsigned OpNum = P->getOperand(0) == R ? 0 : 1;
1880 Instruction *RInst = P->getIncomingBlock(OpNum)->getTerminator();
1881 Instruction *LInst = P->getIncomingBlock(1 - OpNum)->getTerminator();
1882
1883 // Ok, we have a PHI of the form L op= R. Check for low
1884 // zero bits.
1885 RecQ.CxtI = RInst;
1886 computeKnownBits(R, DemandedElts, Known2, RecQ, Depth + 1);
1887
1888 // We need to take the minimum number of known bits
1889 KnownBits Known3(BitWidth);
1890 RecQ.CxtI = LInst;
1891 computeKnownBits(L, DemandedElts, Known3, RecQ, Depth + 1);
1892
1893 Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(),
1894 Known3.countMinTrailingZeros()));
1895
1896 auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(BO);
1897 if (!OverflowOp || !Q.IIQ.hasNoSignedWrap(OverflowOp))
1898 break;
1899
1900 switch (Opcode) {
1901 // If initial value of recurrence is nonnegative, and we are adding
1902 // a nonnegative number with nsw, the result can only be nonnegative
1903 // or poison value regardless of the number of times we execute the
1904 // add in phi recurrence. If initial value is negative and we are
1905 // adding a negative number with nsw, the result can only be
1906 // negative or poison value. Similar arguments apply to sub and mul.
1907 //
1908 // (add non-negative, non-negative) --> non-negative
1909 // (add negative, negative) --> negative
1910 case Instruction::Add: {
1911 if (Known2.isNonNegative() && Known3.isNonNegative())
1912 Known.makeNonNegative();
1913 else if (Known2.isNegative() && Known3.isNegative())
1914 Known.makeNegative();
1915 break;
1916 }
1917
1918 // (sub nsw non-negative, negative) --> non-negative
1919 // (sub nsw negative, non-negative) --> negative
1920 case Instruction::Sub: {
1921 if (BO->getOperand(0) != I)
1922 break;
1923 if (Known2.isNonNegative() && Known3.isNegative())
1924 Known.makeNonNegative();
1925 else if (Known2.isNegative() && Known3.isNonNegative())
1926 Known.makeNegative();
1927 break;
1928 }
1929
1930 // (mul nsw non-negative, non-negative) --> non-negative
1931 case Instruction::Mul:
1932 if (Known2.isNonNegative() && Known3.isNonNegative())
1933 Known.makeNonNegative();
1934 break;
1935
1936 default:
1937 break;
1938 }
1939 break;
1940 }
1941
1942 default:
1943 break;
1944 }
1945 }
1946
1947 // Unreachable blocks may have zero-operand PHI nodes.
1948 if (P->getNumIncomingValues() == 0)
1949 break;
1950
1951 // Otherwise take the unions of the known bit sets of the operands,
1952 // taking conservative care to avoid excessive recursion.
1953 if (Depth < MaxAnalysisRecursionDepth - 1 && Known.isUnknown()) {
1954 // Skip if every incoming value references to ourself.
1955 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
1956 break;
1957
1958 Known.setAllConflict();
1959 for (const Use &U : P->operands()) {
1960 Value *IncValue;
1961 const PHINode *CxtPhi;
1962 Instruction *CxtI;
1963 breakSelfRecursivePHI(&U, P, IncValue, CxtI, &CxtPhi);
1964 // Skip direct self references.
1965 if (IncValue == P)
1966 continue;
1967
1968 // Change the context instruction to the "edge" that flows into the
1969 // phi. This is important because that is where the value is actually
1970 // "evaluated" even though it is used later somewhere else. (see also
1971 // D69571).
1973
1974 Known2 = KnownBits(BitWidth);
1975
1976 // Recurse, but cap the recursion to one level, because we don't
1977 // want to waste time spinning around in loops.
1978 // TODO: See if we can base recursion limiter on number of incoming phi
1979 // edges so we don't overly clamp analysis.
1980 computeKnownBits(IncValue, DemandedElts, Known2, RecQ,
1982
1983 // See if we can further use a conditional branch into the phi
1984 // to help us determine the range of the value.
1985 if (!Known2.isConstant()) {
1986 CmpPredicate Pred;
1987 const APInt *RHSC;
1988 BasicBlock *TrueSucc, *FalseSucc;
1989 // TODO: Use RHS Value and compute range from its known bits.
1990 if (match(RecQ.CxtI,
1991 m_Br(m_c_ICmp(Pred, m_Specific(IncValue), m_APInt(RHSC)),
1992 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
1993 // Check for cases of duplicate successors.
1994 if ((TrueSucc == CxtPhi->getParent()) !=
1995 (FalseSucc == CxtPhi->getParent())) {
1996 // If we're using the false successor, invert the predicate.
1997 if (FalseSucc == CxtPhi->getParent())
1998 Pred = CmpInst::getInversePredicate(Pred);
1999 // Get the knownbits implied by the incoming phi condition.
2000 auto CR = ConstantRange::makeExactICmpRegion(Pred, *RHSC);
2001 KnownBits KnownUnion = Known2.unionWith(CR.toKnownBits());
2002 // We can have conflicts here if we are analyzing deadcode (its
2003 // impossible for us reach this BB based the icmp).
2004 if (KnownUnion.hasConflict()) {
2005 // No reason to continue analyzing in a known dead region, so
2006 // just resetAll and break. This will cause us to also exit the
2007 // outer loop.
2008 Known.resetAll();
2009 break;
2010 }
2011 Known2 = KnownUnion;
2012 }
2013 }
2014 }
2015
2016 Known = Known.intersectWith(Known2);
2017 // If all bits have been ruled out, there's no need to check
2018 // more operands.
2019 if (Known.isUnknown())
2020 break;
2021 }
2022 }
2023 break;
2024 }
2025 case Instruction::Call:
2026 case Instruction::Invoke: {
2027 // If range metadata is attached to this call, set known bits from that,
2028 // and then intersect with known bits based on other properties of the
2029 // function.
2030 if (MDNode *MD =
2031 Q.IIQ.getMetadata(cast<Instruction>(I), LLVMContext::MD_range))
2033
2034 const auto *CB = cast<CallBase>(I);
2035
2036 if (std::optional<ConstantRange> Range = CB->getRange())
2037 Known = Known.unionWith(Range->toKnownBits());
2038
2039 if (const Value *RV = CB->getReturnedArgOperand()) {
2040 if (RV->getType() == I->getType()) {
2041 computeKnownBits(RV, Known2, Q, Depth + 1);
2042 Known = Known.unionWith(Known2);
2043 // If the function doesn't return properly for all input values
2044 // (e.g. unreachable exits) then there might be conflicts between the
2045 // argument value and the range metadata. Simply discard the known bits
2046 // in case of conflicts.
2047 if (Known.hasConflict())
2048 Known.resetAll();
2049 }
2050 }
2051 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
2052 switch (II->getIntrinsicID()) {
2053 default:
2054 break;
2055 case Intrinsic::abs: {
2056 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2057 bool IntMinIsPoison = match(II->getArgOperand(1), m_One());
2058 Known = Known.unionWith(Known2.abs(IntMinIsPoison));
2059 break;
2060 }
2061 case Intrinsic::bitreverse:
2062 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2063 Known = Known.unionWith(Known2.reverseBits());
2064 break;
2065 case Intrinsic::bswap:
2066 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2067 Known = Known.unionWith(Known2.byteSwap());
2068 break;
2069 case Intrinsic::ctlz: {
2070 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2071 // If we have a known 1, its position is our upper bound.
2072 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
2073 // If this call is poison for 0 input, the result will be less than 2^n.
2074 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
2075 PossibleLZ = std::min(PossibleLZ, BitWidth - 1);
2076 unsigned LowBits = llvm::bit_width(PossibleLZ);
2077 Known.Zero.setBitsFrom(LowBits);
2078 break;
2079 }
2080 case Intrinsic::cttz: {
2081 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2082 // If we have a known 1, its position is our upper bound.
2083 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
2084 // If this call is poison for 0 input, the result will be less than 2^n.
2085 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
2086 PossibleTZ = std::min(PossibleTZ, BitWidth - 1);
2087 unsigned LowBits = llvm::bit_width(PossibleTZ);
2088 Known.Zero.setBitsFrom(LowBits);
2089 break;
2090 }
2091 case Intrinsic::ctpop: {
2092 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2093 // We can bound the space the count needs. Also, bits known to be zero
2094 // can't contribute to the population.
2095 unsigned BitsPossiblySet = Known2.countMaxPopulation();
2096 unsigned LowBits = llvm::bit_width(BitsPossiblySet);
2097 Known.Zero.setBitsFrom(LowBits);
2098 // TODO: we could bound KnownOne using the lower bound on the number
2099 // of bits which might be set provided by popcnt KnownOne2.
2100 break;
2101 }
2102 case Intrinsic::fshr:
2103 case Intrinsic::fshl: {
2104 const APInt *SA;
2105 if (!match(I->getOperand(2), m_APInt(SA)))
2106 break;
2107
2108 KnownBits Known3(BitWidth);
2109 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2110 computeKnownBits(I->getOperand(1), DemandedElts, Known3, Q, Depth + 1);
2111 Known = II->getIntrinsicID() == Intrinsic::fshl
2112 ? KnownBits::fshl(Known2, Known3, *SA)
2113 : KnownBits::fshr(Known2, Known3, *SA);
2114 break;
2115 }
2116 case Intrinsic::clmul:
2117 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2118 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2119 Known = KnownBits::clmul(Known, Known2);
2120 break;
2121 case Intrinsic::pext:
2122 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2123 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2124 Known = KnownBits::pext(Known, Known2);
2125 break;
2126 case Intrinsic::pdep:
2127 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2128 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2129 Known = KnownBits::pdep(Known, Known2);
2130 break;
2131 case Intrinsic::uadd_sat:
2132 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2133 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2134 Known = KnownBits::uadd_sat(Known, Known2);
2135 break;
2136 case Intrinsic::usub_sat:
2137 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2138 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2139 Known = KnownBits::usub_sat(Known, Known2);
2140 break;
2141 case Intrinsic::sadd_sat:
2142 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2143 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2144 Known = KnownBits::sadd_sat(Known, Known2);
2145 break;
2146 case Intrinsic::ssub_sat:
2147 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2148 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2149 Known = KnownBits::ssub_sat(Known, Known2);
2150 break;
2151 // Vec reverse preserves bits from input vec.
2152 case Intrinsic::vector_reverse:
2153 computeKnownBits(I->getOperand(0), DemandedElts.reverseBits(), Known, Q,
2154 Depth + 1);
2155 break;
2156 // for min/max/and/or reduce, any bit common to each element in the
2157 // input vec is set in the output.
2158 case Intrinsic::vector_reduce_and:
2159 case Intrinsic::vector_reduce_or:
2160 case Intrinsic::vector_reduce_umax:
2161 case Intrinsic::vector_reduce_umin:
2162 case Intrinsic::vector_reduce_smax:
2163 case Intrinsic::vector_reduce_smin:
2164 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2165 break;
2166 case Intrinsic::vector_reduce_xor: {
2167 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2168 // The zeros common to all vecs are zero in the output.
2169 // If the number of elements is odd, then the common ones remain. If the
2170 // number of elements is even, then the common ones becomes zeros.
2171 auto *VecTy = cast<VectorType>(I->getOperand(0)->getType());
2172 // Even, so the ones become zeros.
2173 bool EvenCnt = VecTy->getElementCount().isKnownEven();
2174 if (EvenCnt)
2175 Known.Zero |= Known.One;
2176 // Maybe even element count so need to clear ones.
2177 if (VecTy->isScalableTy() || EvenCnt)
2178 Known.One.clearAllBits();
2179 break;
2180 }
2181 case Intrinsic::vector_reduce_add: {
2182 auto *VecTy = dyn_cast<FixedVectorType>(I->getOperand(0)->getType());
2183 if (!VecTy)
2184 break;
2185 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2186 Known = Known.reduceAdd(VecTy->getNumElements());
2187 break;
2188 }
2189 case Intrinsic::umin:
2190 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2191 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2192 Known = KnownBits::umin(Known, Known2);
2193 break;
2194 case Intrinsic::umax:
2195 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2196 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2197 Known = KnownBits::umax(Known, Known2);
2198 break;
2199 case Intrinsic::smin:
2200 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2201 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2202 Known = KnownBits::smin(Known, Known2);
2204 break;
2205 case Intrinsic::smax:
2206 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2207 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2208 Known = KnownBits::smax(Known, Known2);
2210 break;
2211 case Intrinsic::ptrmask: {
2212 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2213
2214 const Value *Mask = I->getOperand(1);
2215 Known2 = KnownBits(Mask->getType()->getScalarSizeInBits());
2216 computeKnownBits(Mask, DemandedElts, Known2, Q, Depth + 1);
2217 // TODO: 1-extend would be more precise.
2218 Known &= Known2.anyextOrTrunc(BitWidth);
2219 break;
2220 }
2221 case Intrinsic::x86_sse2_pmulh_w:
2222 case Intrinsic::x86_avx2_pmulh_w:
2223 case Intrinsic::x86_avx512_pmulh_w_512:
2224 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2225 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2226 Known = KnownBits::mulhs(Known, Known2);
2227 break;
2228 case Intrinsic::x86_sse2_pmulhu_w:
2229 case Intrinsic::x86_avx2_pmulhu_w:
2230 case Intrinsic::x86_avx512_pmulhu_w_512:
2231 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2232 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2233 Known = KnownBits::mulhu(Known, Known2);
2234 break;
2235 case Intrinsic::x86_sse42_crc32_64_64:
2236 Known.Zero.setBitsFrom(32);
2237 break;
2238 case Intrinsic::x86_ssse3_phadd_d_128:
2239 case Intrinsic::x86_ssse3_phadd_w_128:
2240 case Intrinsic::x86_avx2_phadd_d:
2241 case Intrinsic::x86_avx2_phadd_w: {
2243 I, DemandedElts, Q, Depth,
2244 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2245 return KnownBits::add(KnownLHS, KnownRHS);
2246 });
2247 break;
2248 }
2249 case Intrinsic::x86_ssse3_phadd_sw_128:
2250 case Intrinsic::x86_avx2_phadd_sw: {
2252 I, DemandedElts, Q, Depth, KnownBits::sadd_sat);
2253 break;
2254 }
2255 case Intrinsic::x86_ssse3_phsub_d_128:
2256 case Intrinsic::x86_ssse3_phsub_w_128:
2257 case Intrinsic::x86_avx2_phsub_d:
2258 case Intrinsic::x86_avx2_phsub_w: {
2260 I, DemandedElts, Q, Depth,
2261 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2262 return KnownBits::sub(KnownLHS, KnownRHS);
2263 });
2264 break;
2265 }
2266 case Intrinsic::x86_ssse3_phsub_sw_128:
2267 case Intrinsic::x86_avx2_phsub_sw: {
2269 I, DemandedElts, Q, Depth, KnownBits::ssub_sat);
2270 break;
2271 }
2272 case Intrinsic::riscv_vsetvli:
2273 case Intrinsic::riscv_vsetvlimax: {
2274 bool HasAVL = II->getIntrinsicID() == Intrinsic::riscv_vsetvli;
2275 const ConstantRange Range = getVScaleRange(II->getFunction(), BitWidth);
2277 cast<ConstantInt>(II->getArgOperand(HasAVL))->getZExtValue());
2278 RISCVVType::VLMUL VLMUL = static_cast<RISCVVType::VLMUL>(
2279 cast<ConstantInt>(II->getArgOperand(1 + HasAVL))->getZExtValue());
2280 uint64_t MaxVLEN =
2281 Range.getUnsignedMax().getZExtValue() * RISCV::RVVBitsPerBlock;
2282 uint64_t MaxVL = MaxVLEN / RISCVVType::getSEWLMULRatio(SEW, VLMUL);
2283
2284 // Result of vsetvli must be not larger than AVL.
2285 if (HasAVL)
2286 if (auto *CI = dyn_cast<ConstantInt>(II->getArgOperand(0)))
2287 MaxVL = std::min(MaxVL, CI->getZExtValue());
2288
2289 unsigned KnownZeroFirstBit = Log2_32(MaxVL) + 1;
2290 if (BitWidth > KnownZeroFirstBit)
2291 Known.Zero.setBitsFrom(KnownZeroFirstBit);
2292 break;
2293 }
2294 case Intrinsic::amdgcn_mbcnt_hi:
2295 case Intrinsic::amdgcn_mbcnt_lo: {
2296 // Wave64 mbcnt_lo returns at most 32 + src1. Otherwise these return at
2297 // most 31 + src1.
2298 Known.Zero.setBitsFrom(
2299 II->getIntrinsicID() == Intrinsic::amdgcn_mbcnt_lo ? 6 : 5);
2300 computeKnownBits(I->getOperand(1), Known2, Q, Depth + 1);
2301 Known = KnownBits::add(Known, Known2);
2302 break;
2303 }
2304 case Intrinsic::vscale: {
2305 if (!II->getParent() || !II->getFunction())
2306 break;
2307
2308 Known = getVScaleRange(II->getFunction(), BitWidth).toKnownBits();
2309 break;
2310 }
2311 }
2312 }
2313 break;
2314 }
2315 case Instruction::ShuffleVector: {
2316 if (auto *Splat = getSplatValue(I)) {
2318 break;
2319 }
2320
2321 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
2322 // FIXME: Do we need to handle ConstantExpr involving shufflevectors?
2323 if (!Shuf) {
2324 Known.resetAll();
2325 return;
2326 }
2327 // For undef elements, we don't know anything about the common state of
2328 // the shuffle result.
2329 APInt DemandedLHS, DemandedRHS;
2330 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) {
2331 Known.resetAll();
2332 return;
2333 }
2334 Known.setAllConflict();
2335 if (!!DemandedLHS) {
2336 const Value *LHS = Shuf->getOperand(0);
2337 computeKnownBits(LHS, DemandedLHS, Known, Q, Depth + 1);
2338 // If we don't know any bits, early out.
2339 if (Known.isUnknown())
2340 break;
2341 }
2342 if (!!DemandedRHS) {
2343 const Value *RHS = Shuf->getOperand(1);
2344 computeKnownBits(RHS, DemandedRHS, Known2, Q, Depth + 1);
2345 Known = Known.intersectWith(Known2);
2346 }
2347 break;
2348 }
2349 case Instruction::InsertElement: {
2350 if (isa<ScalableVectorType>(I->getType())) {
2351 Known.resetAll();
2352 return;
2353 }
2354 const Value *Vec = I->getOperand(0);
2355 const Value *Elt = I->getOperand(1);
2356 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
2357 unsigned NumElts = DemandedElts.getBitWidth();
2358 APInt DemandedVecElts = DemandedElts;
2359 bool NeedsElt = true;
2360 // If we know the index we are inserting too, clear it from Vec check.
2361 if (CIdx && CIdx->getValue().ult(NumElts)) {
2362 DemandedVecElts.clearBit(CIdx->getZExtValue());
2363 NeedsElt = DemandedElts[CIdx->getZExtValue()];
2364 }
2365
2366 Known.setAllConflict();
2367 if (NeedsElt) {
2368 computeKnownBits(Elt, Known, Q, Depth + 1);
2369 // If we don't know any bits, early out.
2370 if (Known.isUnknown())
2371 break;
2372 }
2373
2374 if (!DemandedVecElts.isZero()) {
2375 computeKnownBits(Vec, DemandedVecElts, Known2, Q, Depth + 1);
2376 Known = Known.intersectWith(Known2);
2377 }
2378 break;
2379 }
2380 case Instruction::ExtractElement: {
2381 // Look through extract element. If the index is non-constant or
2382 // out-of-range demand all elements, otherwise just the extracted element.
2383 const Value *Vec = I->getOperand(0);
2384 const Value *Idx = I->getOperand(1);
2385 auto *CIdx = dyn_cast<ConstantInt>(Idx);
2386 if (isa<ScalableVectorType>(Vec->getType())) {
2387 // FIXME: there's probably *something* we can do with scalable vectors
2388 Known.resetAll();
2389 break;
2390 }
2391 unsigned NumElts = cast<FixedVectorType>(Vec->getType())->getNumElements();
2392 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
2393 if (CIdx && CIdx->getValue().ult(NumElts))
2394 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
2395 computeKnownBits(Vec, DemandedVecElts, Known, Q, Depth + 1);
2396 break;
2397 }
2398 case Instruction::ExtractValue:
2399 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
2401 if (EVI->getNumIndices() != 1) break;
2402 if (EVI->getIndices()[0] == 0) {
2403 switch (II->getIntrinsicID()) {
2404 default: break;
2405 case Intrinsic::uadd_with_overflow:
2406 case Intrinsic::sadd_with_overflow:
2408 true, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2409 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2410 break;
2411 case Intrinsic::usub_with_overflow:
2412 case Intrinsic::ssub_with_overflow:
2414 false, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2415 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2416 break;
2417 case Intrinsic::umul_with_overflow:
2418 case Intrinsic::smul_with_overflow:
2419 computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false,
2420 false, DemandedElts, Known, Known2, Q, Depth);
2421 break;
2422 }
2423 }
2424 }
2425 break;
2426 case Instruction::Freeze:
2427 if (isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
2428 Depth + 1))
2429 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2430 break;
2431 }
2432}
2433
2434/// Determine which bits of V are known to be either zero or one and return
2435/// them.
2436KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
2437 const SimplifyQuery &Q, unsigned Depth) {
2438 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2439 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);
2440 return Known;
2441}
2442
2443/// Determine which bits of V are known to be either zero or one and return
2444/// them.
2446 unsigned Depth) {
2447 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2449 return Known;
2450}
2451
2452/// Determine which bits of V are known to be either zero or one and return
2453/// them in the Known bit set.
2454///
2455/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that
2456/// we cannot optimize based on the assumption that it is zero without changing
2457/// it to be an explicit zero. If we don't change it to zero, other code could
2458/// optimized based on the contradictory assumption that it is non-zero.
2459/// Because instcombine aggressively folds operations with undef args anyway,
2460/// this won't lose us code quality.
2461///
2462/// This function is defined on values with integer type, values with pointer
2463/// type, and vectors of integers. In the case
2464/// where V is a vector, known zero, and known one values are the
2465/// same width as the vector element, and the bit is set only if it is true
2466/// for all of the demanded elements in the vector specified by DemandedElts.
2467void computeKnownBits(const Value *V, const APInt &DemandedElts,
2468 KnownBits &Known, const SimplifyQuery &Q,
2469 unsigned Depth) {
2470 if (!DemandedElts) {
2471 // No demanded elts, better to assume we don't know anything.
2472 Known.resetAll();
2473 return;
2474 }
2475
2476 assert(V && "No Value?");
2477 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2478
2479#ifndef NDEBUG
2480 Type *Ty = V->getType();
2481 unsigned BitWidth = Known.getBitWidth();
2482
2483 assert((Ty->isIntOrIntVectorTy(BitWidth) || Ty->isPtrOrPtrVectorTy()) &&
2484 "Not integer or pointer type!");
2485
2486 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
2487 assert(
2488 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
2489 "DemandedElt width should equal the fixed vector number of elements");
2490 } else {
2491 assert(DemandedElts == APInt(1, 1) &&
2492 "DemandedElt width should be 1 for scalars or scalable vectors");
2493 }
2494
2495 Type *ScalarTy = Ty->getScalarType();
2496 if (ScalarTy->isPointerTy()) {
2497 assert(BitWidth == Q.DL.getPointerTypeSizeInBits(ScalarTy) &&
2498 "V and Known should have same BitWidth");
2499 } else {
2500 assert(BitWidth == Q.DL.getTypeSizeInBits(ScalarTy) &&
2501 "V and Known should have same BitWidth");
2502 }
2503#endif
2504
2505 const APInt *C;
2506 if (match(V, m_APInt(C))) {
2507 // We know all of the bits for a scalar constant or a splat vector constant!
2509 return;
2510 }
2511 // Null and aggregate-zero are all-zeros.
2513 Known.setAllZero();
2514 return;
2515 }
2516 // Handle a constant vector by taking the intersection of the known bits of
2517 // each element.
2519 assert(!isa<ScalableVectorType>(V->getType()));
2520 // We know that CDV must be a vector of integers. Take the intersection of
2521 // each element.
2522 Known.setAllConflict();
2523 for (unsigned i = 0, e = CDV->getNumElements(); i != e; ++i) {
2524 if (!DemandedElts[i])
2525 continue;
2526 APInt Elt = CDV->getElementAsAPInt(i);
2527 Known.Zero &= ~Elt;
2528 Known.One &= Elt;
2529 }
2530 if (Known.hasConflict())
2531 Known.resetAll();
2532 return;
2533 }
2534
2535 if (const auto *CV = dyn_cast<ConstantVector>(V)) {
2536 assert(!isa<ScalableVectorType>(V->getType()));
2537 // We know that CV must be a vector of integers. Take the intersection of
2538 // each element.
2539 Known.setAllConflict();
2540 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
2541 if (!DemandedElts[i])
2542 continue;
2543 Constant *Element = CV->getAggregateElement(i);
2544 if (isa<PoisonValue>(Element))
2545 continue;
2546 auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
2547 if (!ElementCI) {
2548 Known.resetAll();
2549 return;
2550 }
2551 const APInt &Elt = ElementCI->getValue();
2552 Known.Zero &= ~Elt;
2553 Known.One &= Elt;
2554 }
2555 if (Known.hasConflict())
2556 Known.resetAll();
2557 return;
2558 }
2559
2560 // Start out not knowing anything.
2561 Known.resetAll();
2562
2563 // We can't imply anything about undefs.
2564 if (isa<UndefValue>(V))
2565 return;
2566
2567 // There's no point in looking through other users of ConstantData for
2568 // assumptions. Confirm that we've handled them all.
2569 assert(!isa<ConstantData>(V) && "Unhandled constant data!");
2570
2571 if (const auto *A = dyn_cast<Argument>(V))
2572 if (std::optional<ConstantRange> Range = A->getRange())
2573 Known = Range->toKnownBits();
2574
2575 // All recursive calls that increase depth must come after this.
2577 return;
2578
2579 // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
2580 // the bits of its aliasee.
2581 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
2582 if (!GA->isInterposable())
2583 computeKnownBits(GA->getAliasee(), Known, Q, Depth + 1);
2584 return;
2585 }
2586
2587 if (const Operator *I = dyn_cast<Operator>(V))
2588 computeKnownBitsFromOperator(I, DemandedElts, Known, Q, Depth);
2589 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2590 if (std::optional<ConstantRange> CR = GV->getAbsoluteSymbolRange())
2591 Known = CR->toKnownBits();
2592 }
2593
2594 // Aligned pointers have trailing zeros - refine Known.Zero set
2595 if (isa<PointerType>(V->getType())) {
2596 Align Alignment = V->getPointerAlignment(Q.DL);
2597 Known.Zero.setLowBits(Log2(Alignment));
2598 }
2599
2600 // computeKnownBitsFromContext strictly refines Known.
2601 // Therefore, we run them after computeKnownBitsFromOperator.
2602
2603 // Check whether we can determine known bits from context such as assumes.
2605}
2606
2607/// Try to detect a recurrence that the value of the induction variable is
2608/// always a power of two (or zero).
2609static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero,
2610 SimplifyQuery &Q, unsigned Depth) {
2611 BinaryOperator *BO = nullptr;
2612 Value *Start = nullptr, *Step = nullptr;
2613 if (!matchSimpleRecurrence(PN, BO, Start, Step))
2614 return false;
2615
2616 // Initial value must be a power of two.
2617 for (const Use &U : PN->operands()) {
2618 if (U.get() == Start) {
2619 // Initial value comes from a different BB, need to adjust context
2620 // instruction for analysis.
2621 Q.CxtI = PN->getIncomingBlock(U)->getTerminator();
2622 if (!isKnownToBeAPowerOfTwo(Start, OrZero, Q, Depth))
2623 return false;
2624 }
2625 }
2626
2627 // Except for Mul, the induction variable must be on the left side of the
2628 // increment expression, otherwise its value can be arbitrary.
2629 if (BO->getOpcode() != Instruction::Mul && BO->getOperand(1) != Step)
2630 return false;
2631
2632 Q.CxtI = BO->getParent()->getTerminator();
2633 switch (BO->getOpcode()) {
2634 case Instruction::Mul:
2635 // Power of two is closed under multiplication.
2636 return (OrZero || Q.IIQ.hasNoUnsignedWrap(BO) ||
2637 Q.IIQ.hasNoSignedWrap(BO)) &&
2638 isKnownToBeAPowerOfTwo(Step, OrZero, Q, Depth);
2639 case Instruction::SDiv:
2640 // Start value must not be signmask for signed division, so simply being a
2641 // power of two is not sufficient, and it has to be a constant.
2642 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2643 return false;
2644 [[fallthrough]];
2645 case Instruction::UDiv:
2646 // Divisor must be a power of two.
2647 // If OrZero is false, cannot guarantee induction variable is non-zero after
2648 // division, same for Shr, unless it is exact division.
2649 return (OrZero || Q.IIQ.isExact(BO)) &&
2650 isKnownToBeAPowerOfTwo(Step, false, Q, Depth);
2651 case Instruction::Shl:
2652 return OrZero || Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO);
2653 case Instruction::AShr:
2654 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2655 return false;
2656 [[fallthrough]];
2657 case Instruction::LShr:
2658 return OrZero || Q.IIQ.isExact(BO);
2659 default:
2660 return false;
2661 }
2662}
2663
2664/// Return true if we can infer that \p V is known to be a power of 2 from
2665/// dominating condition \p Cond (e.g., ctpop(V) == 1).
2666static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero,
2667 const Value *Cond,
2668 bool CondIsTrue) {
2669 CmpPredicate Pred;
2670 const APInt *RHSC;
2671 if (!match(Cond, m_ICmp(Pred, m_Ctpop(m_Specific(V)), m_APInt(RHSC))))
2672 return false;
2673 if (!CondIsTrue)
2674 Pred = ICmpInst::getInversePredicate(Pred);
2675 // ctpop(V) u< 2
2676 if (OrZero && Pred == ICmpInst::ICMP_ULT && *RHSC == 2)
2677 return true;
2678 // ctpop(V) == 1
2679 return Pred == ICmpInst::ICMP_EQ && *RHSC == 1;
2680}
2681
2682/// Return true if the given value is known to have exactly one
2683/// bit set when defined. For vectors return true if every element is known to
2684/// be a power of two when defined. Supports values with integer or pointer
2685/// types and vectors of integers.
2686bool llvm::isKnownToBeAPowerOfTwo(const Value *V, bool OrZero,
2687 const SimplifyQuery &Q, unsigned Depth) {
2688 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2689
2690 if (isa<Constant>(V))
2691 return OrZero ? match(V, m_Power2OrZero()) : match(V, m_Power2());
2692
2693 // i1 is by definition a power of 2 or zero.
2694 if (OrZero && V->getType()->getScalarSizeInBits() == 1)
2695 return true;
2696
2697 // Try to infer from assumptions.
2698 if (Q.AC && Q.CxtI) {
2699 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
2700 if (!AssumeVH)
2701 continue;
2702 CallInst *I = cast<CallInst>(AssumeVH);
2703 if (isImpliedToBeAPowerOfTwoFromCond(V, OrZero, I->getArgOperand(0),
2704 /*CondIsTrue=*/true) &&
2706 return true;
2707 }
2708 }
2709
2710 // Handle dominating conditions.
2711 if (Q.DC && Q.CxtI && Q.DT) {
2712 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
2713 Value *Cond = BI->getCondition();
2714
2715 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
2717 /*CondIsTrue=*/true) &&
2718 Q.DT->dominates(Edge0, Q.CxtI->getParent()))
2719 return true;
2720
2721 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
2723 /*CondIsTrue=*/false) &&
2724 Q.DT->dominates(Edge1, Q.CxtI->getParent()))
2725 return true;
2726 }
2727 }
2728
2729 auto *I = dyn_cast<Instruction>(V);
2730 if (!I)
2731 return false;
2732
2733 if (Q.CxtI && match(V, m_VScale())) {
2734 const Function *F = Q.CxtI->getFunction();
2735 // The vscale_range indicates vscale is a power-of-two.
2736 return F->hasFnAttribute(Attribute::VScaleRange);
2737 }
2738
2739 // 1 << X is clearly a power of two if the one is not shifted off the end. If
2740 // it is shifted off the end then the result is undefined.
2741 if (match(I, m_Shl(m_One(), m_Value())))
2742 return true;
2743
2744 // (signmask) >>l X is clearly a power of two if the one is not shifted off
2745 // the bottom. If it is shifted off the bottom then the result is undefined.
2746 if (match(I, m_LShr(m_SignMask(), m_Value())))
2747 return true;
2748
2749 // The remaining tests are all recursive, so bail out if we hit the limit.
2751 return false;
2752
2753 switch (I->getOpcode()) {
2754 case Instruction::ZExt:
2755 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2756 case Instruction::Trunc:
2757 return OrZero && isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2758 case Instruction::Shl:
2759 if (OrZero || Q.IIQ.hasNoUnsignedWrap(I) || Q.IIQ.hasNoSignedWrap(I))
2760 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2761 return false;
2762 case Instruction::LShr:
2763 if (OrZero || Q.IIQ.isExact(cast<BinaryOperator>(I)))
2764 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2765 return false;
2766 case Instruction::UDiv:
2768 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2769 return false;
2770 case Instruction::Mul:
2771 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
2772 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth) &&
2773 (OrZero || isKnownNonZero(I, Q, Depth));
2774 case Instruction::And:
2775 // A power of two and'd with anything is a power of two or zero.
2776 if (OrZero &&
2777 (isKnownToBeAPowerOfTwo(I->getOperand(1), /*OrZero*/ true, Q, Depth) ||
2778 isKnownToBeAPowerOfTwo(I->getOperand(0), /*OrZero*/ true, Q, Depth)))
2779 return true;
2780 // X & (-X) is always a power of two or zero.
2781 if (match(I->getOperand(0), m_Neg(m_Specific(I->getOperand(1)))) ||
2782 match(I->getOperand(1), m_Neg(m_Specific(I->getOperand(0)))))
2783 return OrZero || isKnownNonZero(I->getOperand(0), Q, Depth);
2784 return false;
2785 case Instruction::Add: {
2786 // Adding a power-of-two or zero to the same power-of-two or zero yields
2787 // either the original power-of-two, a larger power-of-two or zero.
2789 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) ||
2790 Q.IIQ.hasNoSignedWrap(VOBO)) {
2791 if (match(I->getOperand(0),
2792 m_c_And(m_Specific(I->getOperand(1)), m_Value())) &&
2793 isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth))
2794 return true;
2795 if (match(I->getOperand(1),
2796 m_c_And(m_Specific(I->getOperand(0)), m_Value())) &&
2797 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth))
2798 return true;
2799
2800 unsigned BitWidth = V->getType()->getScalarSizeInBits();
2801 KnownBits LHSBits(BitWidth);
2802 computeKnownBits(I->getOperand(0), LHSBits, Q, Depth);
2803
2804 KnownBits RHSBits(BitWidth);
2805 computeKnownBits(I->getOperand(1), RHSBits, Q, Depth);
2806 // If i8 V is a power of two or zero:
2807 // ZeroBits: 1 1 1 0 1 1 1 1
2808 // ~ZeroBits: 0 0 0 1 0 0 0 0
2809 if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2())
2810 // If OrZero isn't set, we cannot give back a zero result.
2811 // Make sure either the LHS or RHS has a bit set.
2812 if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue())
2813 return true;
2814 }
2815
2816 // LShr(UINT_MAX, Y) + 1 is a power of two (if add is nuw) or zero.
2817 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO))
2818 if (match(I, m_Add(m_LShr(m_AllOnes(), m_Value()), m_One())))
2819 return true;
2820 return false;
2821 }
2822 case Instruction::Select:
2823 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
2824 isKnownToBeAPowerOfTwo(I->getOperand(2), OrZero, Q, Depth);
2825 case Instruction::PHI: {
2826 // A PHI node is power of two if all incoming values are power of two, or if
2827 // it is an induction variable where in each step its value is a power of
2828 // two.
2829 auto *PN = cast<PHINode>(I);
2831
2832 // Check if it is an induction variable and always power of two.
2833 if (isPowerOfTwoRecurrence(PN, OrZero, RecQ, Depth))
2834 return true;
2835
2836 // Recursively check all incoming values. Limit recursion to 2 levels, so
2837 // that search complexity is limited to number of operands^2.
2838 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
2839 return llvm::all_of(PN->operands(), [&](const Use &U) {
2840 // Value is power of 2 if it is coming from PHI node itself by induction.
2841 if (U.get() == PN)
2842 return true;
2843
2844 // Change the context instruction to the incoming block where it is
2845 // evaluated.
2846 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
2847 return isKnownToBeAPowerOfTwo(U.get(), OrZero, RecQ, NewDepth);
2848 });
2849 }
2850 case Instruction::Invoke:
2851 case Instruction::Call: {
2852 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
2853 switch (II->getIntrinsicID()) {
2854 case Intrinsic::umax:
2855 case Intrinsic::smax:
2856 case Intrinsic::umin:
2857 case Intrinsic::smin:
2858 return isKnownToBeAPowerOfTwo(II->getArgOperand(1), OrZero, Q, Depth) &&
2859 isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2860 // bswap/bitreverse just move around bits, but don't change any 1s/0s
2861 // thus dont change pow2/non-pow2 status.
2862 case Intrinsic::bitreverse:
2863 case Intrinsic::bswap:
2864 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2865 case Intrinsic::fshr:
2866 case Intrinsic::fshl:
2867 // If Op0 == Op1, this is a rotate. is_pow2(rotate(x, y)) == is_pow2(x)
2868 if (II->getArgOperand(0) == II->getArgOperand(1))
2869 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2870 break;
2871 default:
2872 break;
2873 }
2874 }
2875 return false;
2876 }
2877 default:
2878 return false;
2879 }
2880}
2881
2882/// Test whether a GEP's result is known to be non-null.
2883///
2884/// Uses properties inherent in a GEP to try to determine whether it is known
2885/// to be non-null.
2886///
2887/// Currently this routine does not support vector GEPs.
2888static bool isGEPKnownNonNull(const GEPOperator *GEP, const SimplifyQuery &Q,
2889 unsigned Depth) {
2890 const Function *F = nullptr;
2891 if (const Instruction *I = dyn_cast<Instruction>(GEP))
2892 F = I->getFunction();
2893
2894 // If the gep is nuw or inbounds with invalid null pointer, then the GEP
2895 // may be null iff the base pointer is null and the offset is zero.
2896 if (!GEP->hasNoUnsignedWrap() &&
2897 !(GEP->isInBounds() &&
2898 !NullPointerIsDefined(F, GEP->getPointerAddressSpace())))
2899 return false;
2900
2901 // FIXME: Support vector-GEPs.
2902 assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP");
2903
2904 // If the base pointer is non-null, we cannot walk to a null address with an
2905 // inbounds GEP in address space zero.
2906 if (isKnownNonZero(GEP->getPointerOperand(), Q, Depth))
2907 return true;
2908
2909 // Walk the GEP operands and see if any operand introduces a non-zero offset.
2910 // If so, then the GEP cannot produce a null pointer, as doing so would
2911 // inherently violate the inbounds contract within address space zero.
2913 GTI != GTE; ++GTI) {
2914 // Struct types are easy -- they must always be indexed by a constant.
2915 if (StructType *STy = GTI.getStructTypeOrNull()) {
2916 ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
2917 unsigned ElementIdx = OpC->getZExtValue();
2918 const StructLayout *SL = Q.DL.getStructLayout(STy);
2919 uint64_t ElementOffset = SL->getElementOffset(ElementIdx);
2920 if (ElementOffset > 0)
2921 return true;
2922 continue;
2923 }
2924
2925 // If we have a zero-sized type, the index doesn't matter. Keep looping.
2926 if (GTI.getSequentialElementStride(Q.DL).isZero())
2927 continue;
2928
2929 // Fast path the constant operand case both for efficiency and so we don't
2930 // increment Depth when just zipping down an all-constant GEP.
2931 if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
2932 if (!OpC->isZero())
2933 return true;
2934 continue;
2935 }
2936
2937 // We post-increment Depth here because while isKnownNonZero increments it
2938 // as well, when we pop back up that increment won't persist. We don't want
2939 // to recurse 10k times just because we have 10k GEP operands. We don't
2940 // bail completely out because we want to handle constant GEPs regardless
2941 // of depth.
2943 continue;
2944
2945 if (isKnownNonZero(GTI.getOperand(), Q, Depth))
2946 return true;
2947 }
2948
2949 return false;
2950}
2951
2953 const Instruction *CtxI,
2954 const DominatorTree *DT) {
2955 assert(!isa<Constant>(V) && "Called for constant?");
2956
2957 if (!CtxI || !DT)
2958 return false;
2959
2960 unsigned NumUsesExplored = 0;
2961 for (auto &U : V->uses()) {
2962 // Avoid massive lists
2963 if (NumUsesExplored >= DomConditionsMaxUses)
2964 break;
2965 NumUsesExplored++;
2966
2967 const Instruction *UI = cast<Instruction>(U.getUser());
2968 // If the value is used as an argument to a call or invoke, then argument
2969 // attributes may provide an answer about null-ness.
2970 if (V->getType()->isPointerTy()) {
2971 if (const auto *CB = dyn_cast<CallBase>(UI)) {
2972 if (CB->isArgOperand(&U) &&
2973 CB->paramHasNonNullAttr(CB->getArgOperandNo(&U),
2974 /*AllowUndefOrPoison=*/false) &&
2975 DT->dominates(CB, CtxI))
2976 return true;
2977 }
2978 }
2979
2980 // If the value is used as a load/store, then the pointer must be non null.
2981 if (V == getLoadStorePointerOperand(UI)) {
2984 DT->dominates(UI, CtxI))
2985 return true;
2986 }
2987
2988 if ((match(UI, m_IDiv(m_Value(), m_Specific(V))) ||
2989 match(UI, m_IRem(m_Value(), m_Specific(V)))) &&
2990 isValidAssumeForContext(UI, CtxI, DT))
2991 return true;
2992
2993 // Consider only compare instructions uniquely controlling a branch
2994 Value *RHS;
2995 CmpPredicate Pred;
2996 if (!match(UI, m_c_ICmp(Pred, m_Specific(V), m_Value(RHS))))
2997 continue;
2998
2999 bool NonNullIfTrue;
3000 if (cmpExcludesZero(Pred, RHS))
3001 NonNullIfTrue = true;
3003 NonNullIfTrue = false;
3004 else
3005 continue;
3006
3009 for (const auto *CmpU : UI->users()) {
3010 assert(WorkList.empty() && "Should be!");
3011 if (Visited.insert(CmpU).second)
3012 WorkList.push_back(CmpU);
3013
3014 while (!WorkList.empty()) {
3015 auto *Curr = WorkList.pop_back_val();
3016
3017 // If a user is an AND, add all its users to the work list. We only
3018 // propagate "pred != null" condition through AND because it is only
3019 // correct to assume that all conditions of AND are met in true branch.
3020 // TODO: Support similar logic of OR and EQ predicate?
3021 if (NonNullIfTrue)
3022 if (match(Curr, m_LogicalAnd(m_Value(), m_Value()))) {
3023 for (const auto *CurrU : Curr->users())
3024 if (Visited.insert(CurrU).second)
3025 WorkList.push_back(CurrU);
3026 continue;
3027 }
3028
3029 if (const CondBrInst *BI = dyn_cast<CondBrInst>(Curr)) {
3030 BasicBlock *NonNullSuccessor =
3031 BI->getSuccessor(NonNullIfTrue ? 0 : 1);
3032 BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor);
3033 if (DT->dominates(Edge, CtxI->getParent()))
3034 return true;
3035 } else if (NonNullIfTrue && isGuard(Curr) &&
3036 DT->dominates(cast<Instruction>(Curr), CtxI)) {
3037 return true;
3038 }
3039 }
3040 }
3041 }
3042
3043 return false;
3044}
3045
3046/// Does the 'Range' metadata (which must be a valid MD_range operand list)
3047/// ensure that the value it's attached to is never Value? 'RangeType' is
3048/// is the type of the value described by the range.
3049static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) {
3050 const unsigned NumRanges = Ranges->getNumOperands() / 2;
3051 assert(NumRanges >= 1);
3052 for (unsigned i = 0; i < NumRanges; ++i) {
3054 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
3056 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
3057 ConstantRange Range(Lower->getValue(), Upper->getValue());
3058 if (Range.contains(Value))
3059 return false;
3060 }
3061 return true;
3062}
3063
3064/// Try to detect a recurrence that monotonically increases/decreases from a
3065/// non-zero starting value. These are common as induction variables.
3066static bool isNonZeroRecurrence(const PHINode *PN) {
3067 BinaryOperator *BO = nullptr;
3068 Value *Start = nullptr, *Step = nullptr;
3069 const APInt *StartC, *StepC;
3070 if (!matchSimpleRecurrence(PN, BO, Start, Step) ||
3071 !match(Start, m_APInt(StartC)) || StartC->isZero())
3072 return false;
3073
3074 switch (BO->getOpcode()) {
3075 case Instruction::Add:
3076 // Starting from non-zero and stepping away from zero can never wrap back
3077 // to zero.
3078 return BO->hasNoUnsignedWrap() ||
3079 (BO->hasNoSignedWrap() && match(Step, m_APInt(StepC)) &&
3080 StartC->isNegative() == StepC->isNegative());
3081 case Instruction::Mul:
3082 return (BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap()) &&
3083 match(Step, m_APInt(StepC)) && !StepC->isZero();
3084 case Instruction::Shl:
3085 return BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap();
3086 case Instruction::AShr:
3087 case Instruction::LShr:
3088 return BO->isExact();
3089 default:
3090 return false;
3091 }
3092}
3093
3094static bool matchOpWithOpEqZero(Value *Op0, Value *Op1) {
3096 m_Specific(Op1), m_Zero()))) ||
3098 m_Specific(Op0), m_Zero())));
3099}
3100
3101static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q,
3102 unsigned BitWidth, Value *X, Value *Y, bool NSW,
3103 bool NUW, unsigned Depth) {
3104 // (X + (X != 0)) is non zero
3105 if (matchOpWithOpEqZero(X, Y))
3106 return true;
3107
3108 if (NUW)
3109 return isKnownNonZero(Y, DemandedElts, Q, Depth) ||
3110 isKnownNonZero(X, DemandedElts, Q, Depth);
3111
3112 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
3113 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
3114
3115 // If X and Y are both non-negative (as signed values) then their sum is not
3116 // zero unless both X and Y are zero.
3117 if (XKnown.isNonNegative() && YKnown.isNonNegative())
3118 if (isKnownNonZero(Y, DemandedElts, Q, Depth) ||
3119 isKnownNonZero(X, DemandedElts, Q, Depth))
3120 return true;
3121
3122 // If X and Y are both negative (as signed values) then their sum is not
3123 // zero unless both X and Y equal INT_MIN.
3124 if (XKnown.isNegative() && YKnown.isNegative()) {
3126 // The sign bit of X is set. If some other bit is set then X is not equal
3127 // to INT_MIN.
3128 if (XKnown.One.intersects(Mask))
3129 return true;
3130 // The sign bit of Y is set. If some other bit is set then Y is not equal
3131 // to INT_MIN.
3132 if (YKnown.One.intersects(Mask))
3133 return true;
3134 }
3135
3136 // The sum of a non-negative number and a power of two is not zero.
3137 if (XKnown.isNonNegative() &&
3138 isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Q, Depth))
3139 return true;
3140 if (YKnown.isNonNegative() &&
3141 isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Q, Depth))
3142 return true;
3143
3144 return KnownBits::add(XKnown, YKnown, NSW, NUW).isNonZero();
3145}
3146
3147static bool isNonZeroSub(const APInt &DemandedElts, const SimplifyQuery &Q,
3148 unsigned BitWidth, Value *X, Value *Y,
3149 unsigned Depth) {
3150 // (X - (X != 0)) is non zero
3151 // ((X != 0) - X) is non zero
3152 if (matchOpWithOpEqZero(X, Y))
3153 return true;
3154
3155 // TODO: Move this case into isKnownNonEqual().
3156 if (auto *C = dyn_cast<Constant>(X))
3157 if (C->isNullValue() && isKnownNonZero(Y, DemandedElts, Q, Depth))
3158 return true;
3159
3160 return ::isKnownNonEqual(X, Y, DemandedElts, Q, Depth);
3161}
3162
3163static bool isNonZeroMul(const APInt &DemandedElts, const SimplifyQuery &Q,
3164 unsigned BitWidth, Value *X, Value *Y, bool NSW,
3165 bool NUW, unsigned Depth) {
3166 // If X and Y are non-zero then so is X * Y as long as the multiplication
3167 // does not overflow.
3168 if (NSW || NUW)
3169 return isKnownNonZero(X, DemandedElts, Q, Depth) &&
3170 isKnownNonZero(Y, DemandedElts, Q, Depth);
3171
3172 // If either X or Y is odd, then if the other is non-zero the result can't
3173 // be zero.
3174 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
3175 if (XKnown.One[0])
3176 return isKnownNonZero(Y, DemandedElts, Q, Depth);
3177
3178 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
3179 if (YKnown.One[0])
3180 return XKnown.isNonZero() || isKnownNonZero(X, DemandedElts, Q, Depth);
3181
3182 // If there exists any subset of X (sX) and subset of Y (sY) s.t sX * sY is
3183 // non-zero, then X * Y is non-zero. We can find sX and sY by just taking
3184 // the lowest known One of X and Y. If they are non-zero, the result
3185 // must be non-zero. We can check if LSB(X) * LSB(Y) != 0 by doing
3186 // X.CountLeadingZeros + Y.CountLeadingZeros < BitWidth.
3187 return (XKnown.countMaxTrailingZeros() + YKnown.countMaxTrailingZeros()) <
3188 BitWidth;
3189}
3190
3191static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts,
3192 const SimplifyQuery &Q, const KnownBits &KnownVal,
3193 unsigned Depth) {
3194 auto ShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
3195 switch (I->getOpcode()) {
3196 case Instruction::Shl:
3197 return Lhs.shl(Rhs);
3198 case Instruction::LShr:
3199 return Lhs.lshr(Rhs);
3200 case Instruction::AShr:
3201 return Lhs.ashr(Rhs);
3202 default:
3203 llvm_unreachable("Unknown Shift Opcode");
3204 }
3205 };
3206
3207 auto InvShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
3208 switch (I->getOpcode()) {
3209 case Instruction::Shl:
3210 return Lhs.lshr(Rhs);
3211 case Instruction::LShr:
3212 case Instruction::AShr:
3213 return Lhs.shl(Rhs);
3214 default:
3215 llvm_unreachable("Unknown Shift Opcode");
3216 }
3217 };
3218
3219 if (KnownVal.isUnknown())
3220 return false;
3221
3222 KnownBits KnownCnt =
3223 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
3224 APInt MaxShift = KnownCnt.getMaxValue();
3225 unsigned NumBits = KnownVal.getBitWidth();
3226 if (MaxShift.uge(NumBits))
3227 return false;
3228
3229 if (!ShiftOp(KnownVal.One, MaxShift).isZero())
3230 return true;
3231
3232 // If all of the bits shifted out are known to be zero, and Val is known
3233 // non-zero then at least one non-zero bit must remain.
3234 if (InvShiftOp(KnownVal.Zero, NumBits - MaxShift)
3235 .eq(InvShiftOp(APInt::getAllOnes(NumBits), NumBits - MaxShift)) &&
3236 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth))
3237 return true;
3238
3239 return false;
3240}
3241
3243 const APInt &DemandedElts,
3244 const SimplifyQuery &Q, unsigned Depth) {
3245 unsigned BitWidth = getBitWidth(I->getType()->getScalarType(), Q.DL);
3246 switch (I->getOpcode()) {
3247 case Instruction::Alloca:
3248 // Alloca never returns null, malloc might.
3249 return I->getType()->getPointerAddressSpace() == 0;
3250 case Instruction::GetElementPtr:
3251 if (I->getType()->isPointerTy())
3253 break;
3254 case Instruction::BitCast: {
3255 // We need to be a bit careful here. We can only peek through the bitcast
3256 // if the scalar size of elements in the operand are smaller than and a
3257 // multiple of the size they are casting too. Take three cases:
3258 //
3259 // 1) Unsafe:
3260 // bitcast <2 x i16> %NonZero to <4 x i8>
3261 //
3262 // %NonZero can have 2 non-zero i16 elements, but isKnownNonZero on a
3263 // <4 x i8> requires that all 4 i8 elements be non-zero which isn't
3264 // guranteed (imagine just sign bit set in the 2 i16 elements).
3265 //
3266 // 2) Unsafe:
3267 // bitcast <4 x i3> %NonZero to <3 x i4>
3268 //
3269 // Even though the scalar size of the src (`i3`) is smaller than the
3270 // scalar size of the dst `i4`, because `i3` is not a multiple of `i4`
3271 // its possible for the `3 x i4` elements to be zero because there are
3272 // some elements in the destination that don't contain any full src
3273 // element.
3274 //
3275 // 3) Safe:
3276 // bitcast <4 x i8> %NonZero to <2 x i16>
3277 //
3278 // This is always safe as non-zero in the 4 i8 elements implies
3279 // non-zero in the combination of any two adjacent ones. Since i8 is a
3280 // multiple of i16, each i16 is guranteed to have 2 full i8 elements.
3281 // This all implies the 2 i16 elements are non-zero.
3282 Type *FromTy = I->getOperand(0)->getType();
3283 if ((FromTy->isIntOrIntVectorTy() || FromTy->isPtrOrPtrVectorTy()) &&
3284 (BitWidth % getBitWidth(FromTy->getScalarType(), Q.DL)) == 0)
3285 return isKnownNonZero(I->getOperand(0), Q, Depth);
3286 } break;
3287 case Instruction::IntToPtr:
3288 // Note that we have to take special care to avoid looking through
3289 // truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well
3290 // as casts that can alter the value, e.g., AddrSpaceCasts.
3291 if (!isa<ScalableVectorType>(I->getType()) &&
3292 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
3293 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
3294 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3295 break;
3296 case Instruction::PtrToAddr:
3297 // isKnownNonZero() for pointers refers to the address bits being non-zero,
3298 // so we can directly forward.
3299 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3300 case Instruction::PtrToInt:
3301 // For inttoptr, make sure the result size is >= the address size. If the
3302 // address is non-zero, any larger value is also non-zero.
3303 if (Q.DL.getAddressSizeInBits(I->getOperand(0)->getType()) <=
3304 I->getType()->getScalarSizeInBits())
3305 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3306 break;
3307 case Instruction::Trunc:
3308 // nuw/nsw trunc preserves zero/non-zero status of input.
3309 if (auto *TI = dyn_cast<TruncInst>(I))
3310 if (TI->hasNoSignedWrap() || TI->hasNoUnsignedWrap())
3311 return isKnownNonZero(TI->getOperand(0), DemandedElts, Q, Depth);
3312 break;
3313
3314 // Iff x - y != 0, then x ^ y != 0
3315 // Therefore we can do the same exact checks
3316 case Instruction::Xor:
3317 case Instruction::Sub:
3318 return isNonZeroSub(DemandedElts, Q, BitWidth, I->getOperand(0),
3319 I->getOperand(1), Depth);
3320 case Instruction::Or:
3321 // (X | (X != 0)) is non zero
3322 if (matchOpWithOpEqZero(I->getOperand(0), I->getOperand(1)))
3323 return true;
3324 // X | Y != 0 if X != Y.
3325 if (isKnownNonEqual(I->getOperand(0), I->getOperand(1), DemandedElts, Q,
3326 Depth))
3327 return true;
3328 // X | Y != 0 if X != 0 or Y != 0.
3329 return isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth) ||
3330 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3331 case Instruction::SExt:
3332 case Instruction::ZExt:
3333 // ext X != 0 if X != 0.
3334 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3335
3336 case Instruction::Shl: {
3337 // shl nsw/nuw can't remove any non-zero bits.
3339 if (Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO))
3340 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3341
3342 // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
3343 // if the lowest bit is shifted off the end.
3345 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth);
3346 if (Known.One[0])
3347 return true;
3348
3349 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3350 }
3351 case Instruction::LShr:
3352 case Instruction::AShr: {
3353 // shr exact can only shift out zero bits.
3355 if (BO->isExact())
3356 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3357
3358 // shr X, Y != 0 if X is negative. Note that the value of the shift is not
3359 // defined if the sign bit is shifted off the end.
3361 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3362 if (Known.isNegative())
3363 return true;
3364
3365 // shr (add nuw A, B), C is non-zero if A or B has a known-one bit at
3366 // position >= C, because the sum >= max(A, B).
3367 Value *A, *B;
3368 const APInt *C;
3369 if (Depth + 1 < MaxAnalysisRecursionDepth &&
3370 match(I->getOperand(0), m_NUWAdd(m_Value(A), m_Value(B))) &&
3371 match(I->getOperand(1), m_APInt(C)) && C->ult(BitWidth)) {
3372 KnownBits KnownA = computeKnownBits(A, DemandedElts, Q, Depth + 1);
3373 if (!KnownA.One.lshr(*C).isZero())
3374 return true;
3375 KnownBits KnownB = computeKnownBits(B, DemandedElts, Q, Depth + 1);
3376 if (!KnownB.One.lshr(*C).isZero())
3377 return true;
3378 }
3379
3380 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3381 }
3382 case Instruction::UDiv:
3383 case Instruction::SDiv: {
3384 // X / Y
3385 // div exact can only produce a zero if the dividend is zero.
3386 if (cast<PossiblyExactOperator>(I)->isExact())
3387 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3388
3389 KnownBits XKnown =
3390 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3391 // If X is fully unknown we won't be able to figure anything out so don't
3392 // both computing knownbits for Y.
3393 if (XKnown.isUnknown())
3394 return false;
3395
3396 KnownBits YKnown =
3397 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
3398 if (I->getOpcode() == Instruction::SDiv) {
3399 // For signed division need to compare abs value of the operands.
3400 XKnown = XKnown.abs(/*IntMinIsPoison*/ false);
3401 YKnown = YKnown.abs(/*IntMinIsPoison*/ false);
3402 }
3403 // If X u>= Y then div is non zero (0/0 is UB).
3404 std::optional<bool> XUgeY = KnownBits::uge(XKnown, YKnown);
3405 // If X is total unknown or X u< Y we won't be able to prove non-zero
3406 // with compute known bits so just return early.
3407 return XUgeY && *XUgeY;
3408 }
3409 case Instruction::Add: {
3410 // X + Y.
3411
3412 // If Add has nuw wrap flag, then if either X or Y is non-zero the result is
3413 // non-zero.
3415 return isNonZeroAdd(DemandedElts, Q, BitWidth, I->getOperand(0),
3416 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3417 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3418 }
3419 case Instruction::Mul: {
3421 return isNonZeroMul(DemandedElts, Q, BitWidth, I->getOperand(0),
3422 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3423 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3424 }
3425 case Instruction::Select: {
3426 // (C ? X : Y) != 0 if X != 0 and Y != 0.
3427
3428 // First check if the arm is non-zero using `isKnownNonZero`. If that fails,
3429 // then see if the select condition implies the arm is non-zero. For example
3430 // (X != 0 ? X : Y), we know the true arm is non-zero as the `X` "return" is
3431 // dominated by `X != 0`.
3432 auto SelectArmIsNonZero = [&](bool IsTrueArm) {
3433 Value *Op;
3434 Op = IsTrueArm ? I->getOperand(1) : I->getOperand(2);
3435 // Op is trivially non-zero.
3436 if (isKnownNonZero(Op, DemandedElts, Q, Depth))
3437 return true;
3438
3439 // The condition of the select dominates the true/false arm. Check if the
3440 // condition implies that a given arm is non-zero.
3441 Value *X;
3442 CmpPredicate Pred;
3443 if (!match(I->getOperand(0), m_c_ICmp(Pred, m_Specific(Op), m_Value(X))))
3444 return false;
3445
3446 if (!IsTrueArm)
3447 Pred = ICmpInst::getInversePredicate(Pred);
3448
3449 return cmpExcludesZero(Pred, X);
3450 };
3451
3452 if (SelectArmIsNonZero(/* IsTrueArm */ true) &&
3453 SelectArmIsNonZero(/* IsTrueArm */ false))
3454 return true;
3455 break;
3456 }
3457 case Instruction::PHI: {
3458 auto *PN = cast<PHINode>(I);
3460 return true;
3461
3462 // Check if all incoming values are non-zero using recursion.
3464 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
3465 return llvm::all_of(PN->operands(), [&](const Use &U) {
3466 if (U.get() == PN)
3467 return true;
3468 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
3469 // Check if the branch on the phi excludes zero.
3470 CmpPredicate Pred;
3471 Value *X;
3472 BasicBlock *TrueSucc, *FalseSucc;
3473 if (match(RecQ.CxtI,
3474 m_Br(m_c_ICmp(Pred, m_Specific(U.get()), m_Value(X)),
3475 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
3476 // Check for cases of duplicate successors.
3477 if ((TrueSucc == PN->getParent()) != (FalseSucc == PN->getParent())) {
3478 // If we're using the false successor, invert the predicate.
3479 if (FalseSucc == PN->getParent())
3480 Pred = CmpInst::getInversePredicate(Pred);
3481 if (cmpExcludesZero(Pred, X))
3482 return true;
3483 }
3484 }
3485 // Finally recurse on the edge and check it directly.
3486 return isKnownNonZero(U.get(), DemandedElts, RecQ, NewDepth);
3487 });
3488 }
3489 case Instruction::InsertElement: {
3490 if (isa<ScalableVectorType>(I->getType()))
3491 break;
3492
3493 const Value *Vec = I->getOperand(0);
3494 const Value *Elt = I->getOperand(1);
3495 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
3496
3497 unsigned NumElts = DemandedElts.getBitWidth();
3498 APInt DemandedVecElts = DemandedElts;
3499 bool SkipElt = false;
3500 // If we know the index we are inserting too, clear it from Vec check.
3501 if (CIdx && CIdx->getValue().ult(NumElts)) {
3502 DemandedVecElts.clearBit(CIdx->getZExtValue());
3503 SkipElt = !DemandedElts[CIdx->getZExtValue()];
3504 }
3505
3506 // Result is zero if Elt is non-zero and rest of the demanded elts in Vec
3507 // are non-zero.
3508 return (SkipElt || isKnownNonZero(Elt, Q, Depth)) &&
3509 (DemandedVecElts.isZero() ||
3510 isKnownNonZero(Vec, DemandedVecElts, Q, Depth));
3511 }
3512 case Instruction::ExtractElement:
3513 if (const auto *EEI = dyn_cast<ExtractElementInst>(I)) {
3514 const Value *Vec = EEI->getVectorOperand();
3515 const Value *Idx = EEI->getIndexOperand();
3516 auto *CIdx = dyn_cast<ConstantInt>(Idx);
3517 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
3518 unsigned NumElts = VecTy->getNumElements();
3519 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
3520 if (CIdx && CIdx->getValue().ult(NumElts))
3521 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
3522 return isKnownNonZero(Vec, DemandedVecElts, Q, Depth);
3523 }
3524 }
3525 break;
3526 case Instruction::ShuffleVector: {
3527 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
3528 if (!Shuf)
3529 break;
3530 APInt DemandedLHS, DemandedRHS;
3531 // For undef elements, we don't know anything about the common state of
3532 // the shuffle result.
3533 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
3534 break;
3535 // If demanded elements for both vecs are non-zero, the shuffle is non-zero.
3536 return (DemandedRHS.isZero() ||
3537 isKnownNonZero(Shuf->getOperand(1), DemandedRHS, Q, Depth)) &&
3538 (DemandedLHS.isZero() ||
3539 isKnownNonZero(Shuf->getOperand(0), DemandedLHS, Q, Depth));
3540 }
3541 case Instruction::Freeze:
3542 return isKnownNonZero(I->getOperand(0), Q, Depth) &&
3543 isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
3544 Depth);
3545 case Instruction::Load: {
3546 auto *LI = cast<LoadInst>(I);
3547 // A Load tagged with nonnull or dereferenceable with null pointer undefined
3548 // is never null.
3549 if (auto *PtrT = dyn_cast<PointerType>(I->getType())) {
3550 if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull) ||
3551 (Q.IIQ.getMetadata(LI, LLVMContext::MD_dereferenceable) &&
3552 !NullPointerIsDefined(LI->getFunction(), PtrT->getAddressSpace())))
3553 return true;
3554 } else if (MDNode *Ranges = Q.IIQ.getMetadata(LI, LLVMContext::MD_range)) {
3556 }
3557
3558 // No need to fall through to computeKnownBits as range metadata is already
3559 // handled in isKnownNonZero.
3560 return false;
3561 }
3562 case Instruction::ExtractValue: {
3563 const WithOverflowInst *WO;
3565 switch (WO->getBinaryOp()) {
3566 default:
3567 break;
3568 case Instruction::Add:
3569 return isNonZeroAdd(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3570 WO->getArgOperand(1),
3571 /*NSW=*/false,
3572 /*NUW=*/false, Depth);
3573 case Instruction::Sub:
3574 return isNonZeroSub(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3575 WO->getArgOperand(1), Depth);
3576 case Instruction::Mul:
3577 return isNonZeroMul(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3578 WO->getArgOperand(1),
3579 /*NSW=*/false, /*NUW=*/false, Depth);
3580 break;
3581 }
3582 }
3583 break;
3584 }
3585 case Instruction::Call:
3586 case Instruction::Invoke: {
3587 const auto *Call = cast<CallBase>(I);
3588 if (I->getType()->isPointerTy()) {
3589 if (Call->isReturnNonNull())
3590 return true;
3591 if (const auto *RP = getArgumentAliasingToReturnedPointer(
3592 Call, /*MustPreserveOffset=*/true))
3593 return isKnownNonZero(RP, Q, Depth);
3594 } else {
3595 if (MDNode *Ranges = Q.IIQ.getMetadata(Call, LLVMContext::MD_range))
3597 if (std::optional<ConstantRange> Range = Call->getRange()) {
3598 const APInt ZeroValue(Range->getBitWidth(), 0);
3599 if (!Range->contains(ZeroValue))
3600 return true;
3601 }
3602 if (const Value *RV = Call->getReturnedArgOperand())
3603 if (RV->getType() == I->getType() && isKnownNonZero(RV, Q, Depth))
3604 return true;
3605 }
3606
3607 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
3608 switch (II->getIntrinsicID()) {
3609 case Intrinsic::sshl_sat:
3610 case Intrinsic::ushl_sat:
3611 case Intrinsic::abs:
3612 case Intrinsic::bitreverse:
3613 case Intrinsic::bswap:
3614 case Intrinsic::ctpop:
3615 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3616 // NB: We don't do usub_sat here as in any case we can prove its
3617 // non-zero, we will fold it to `sub nuw` in InstCombine.
3618 case Intrinsic::ssub_sat:
3619 // For most types, if x != y then ssub.sat x, y != 0. But
3620 // ssub.sat.i1 0, -1 = 0, because 1 saturates to 0. This means
3621 // isNonZeroSub will do the wrong thing for ssub.sat.i1.
3622 if (BitWidth == 1)
3623 return false;
3624 return isNonZeroSub(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3625 II->getArgOperand(1), Depth);
3626 case Intrinsic::sadd_sat:
3627 return isNonZeroAdd(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3628 II->getArgOperand(1),
3629 /*NSW=*/true, /* NUW=*/false, Depth);
3630 // Vec reverse preserves zero/non-zero status from input vec.
3631 case Intrinsic::vector_reverse:
3632 return isKnownNonZero(II->getArgOperand(0), DemandedElts.reverseBits(),
3633 Q, Depth);
3634 // umin/smin/smax/smin/or of all non-zero elements is always non-zero.
3635 case Intrinsic::vector_reduce_or:
3636 case Intrinsic::vector_reduce_umax:
3637 case Intrinsic::vector_reduce_umin:
3638 case Intrinsic::vector_reduce_smax:
3639 case Intrinsic::vector_reduce_smin:
3640 return isKnownNonZero(II->getArgOperand(0), Q, Depth);
3641 case Intrinsic::umax:
3642 case Intrinsic::uadd_sat:
3643 // umax(X, (X != 0)) is non zero
3644 // X +usat (X != 0) is non zero
3645 if (matchOpWithOpEqZero(II->getArgOperand(0), II->getArgOperand(1)))
3646 return true;
3647
3648 return isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth) ||
3649 isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3650 case Intrinsic::smax: {
3651 // If either arg is strictly positive the result is non-zero. Otherwise
3652 // the result is non-zero if both ops are non-zero.
3653 auto IsNonZero = [&](Value *Op, std::optional<bool> &OpNonZero,
3654 const KnownBits &OpKnown) {
3655 if (!OpNonZero.has_value())
3656 OpNonZero = OpKnown.isNonZero() ||
3657 isKnownNonZero(Op, DemandedElts, Q, Depth);
3658 return *OpNonZero;
3659 };
3660 // Avoid re-computing isKnownNonZero.
3661 std::optional<bool> Op0NonZero, Op1NonZero;
3662 KnownBits Op1Known =
3663 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3664 if (Op1Known.isNonNegative() &&
3665 IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known))
3666 return true;
3667 KnownBits Op0Known =
3668 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3669 if (Op0Known.isNonNegative() &&
3670 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known))
3671 return true;
3672 return IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known) &&
3673 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known);
3674 }
3675 case Intrinsic::smin: {
3676 // If either arg is negative the result is non-zero. Otherwise
3677 // the result is non-zero if both ops are non-zero.
3678 KnownBits Op1Known =
3679 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3680 if (Op1Known.isNegative())
3681 return true;
3682 KnownBits Op0Known =
3683 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3684 if (Op0Known.isNegative())
3685 return true;
3686
3687 if (Op1Known.isNonZero() && Op0Known.isNonZero())
3688 return true;
3689 }
3690 [[fallthrough]];
3691 case Intrinsic::umin:
3692 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth) &&
3693 isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth);
3694 case Intrinsic::cttz:
3695 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3696 .Zero[0];
3697 case Intrinsic::ctlz:
3698 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3699 .isNonNegative();
3700 case Intrinsic::fshr:
3701 case Intrinsic::fshl:
3702 // If Op0 == Op1, this is a rotate. rotate(x, y) != 0 iff x != 0.
3703 if (II->getArgOperand(0) == II->getArgOperand(1))
3704 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3705 break;
3706 case Intrinsic::vscale:
3707 return true;
3708 case Intrinsic::experimental_get_vector_length:
3709 return isKnownNonZero(I->getOperand(0), Q, Depth);
3710 default:
3711 break;
3712 }
3713 break;
3714 }
3715
3716 return false;
3717 }
3718 }
3719
3721 computeKnownBits(I, DemandedElts, Known, Q, Depth);
3722 return Known.One != 0;
3723}
3724
3725/// Return true if the given value is known to be non-zero when defined. For
3726/// vectors, return true if every demanded element is known to be non-zero when
3727/// defined. For pointers, if the context instruction and dominator tree are
3728/// specified, perform context-sensitive analysis and return true if the
3729/// pointer couldn't possibly be null at the specified instruction.
3730/// Supports values with integer or pointer type and vectors of integers.
3731bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
3732 const SimplifyQuery &Q, unsigned Depth) {
3733 Type *Ty = V->getType();
3734
3735#ifndef NDEBUG
3736 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
3737
3738 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3739 assert(
3740 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
3741 "DemandedElt width should equal the fixed vector number of elements");
3742 } else {
3743 assert(DemandedElts == APInt(1, 1) &&
3744 "DemandedElt width should be 1 for scalars");
3745 }
3746#endif
3747
3748 if (auto *C = dyn_cast<Constant>(V)) {
3749 if (C->isNullValue())
3750 return false;
3751 if (isa<ConstantInt>(C))
3752 // Must be non-zero due to null test above.
3753 return true;
3754
3755 // For constant vectors, check that all elements are poison or known
3756 // non-zero to determine that the whole vector is known non-zero.
3757 if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
3758 for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
3759 if (!DemandedElts[i])
3760 continue;
3761 Constant *Elt = C->getAggregateElement(i);
3762 if (!Elt || Elt->isNullValue())
3763 return false;
3764 if (!isa<PoisonValue>(Elt) && !isa<ConstantInt>(Elt))
3765 return false;
3766 }
3767 return true;
3768 }
3769
3770 // Constant ptrauth can be null, iff the base pointer can be.
3771 if (auto *CPA = dyn_cast<ConstantPtrAuth>(V))
3772 return isKnownNonZero(CPA->getPointer(), DemandedElts, Q, Depth);
3773
3774 // A global variable in address space 0 is non null unless extern weak
3775 // or an absolute symbol reference. Other address spaces may have null as a
3776 // valid address for a global, so we can't assume anything.
3777 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
3778 if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&
3779 GV->getType()->getAddressSpace() == 0)
3780 return true;
3781 }
3782
3783 // For constant expressions, fall through to the Operator code below.
3784 if (!isa<ConstantExpr>(V))
3785 return false;
3786 }
3787
3788 if (const auto *A = dyn_cast<Argument>(V))
3789 if (std::optional<ConstantRange> Range = A->getRange()) {
3790 const APInt ZeroValue(Range->getBitWidth(), 0);
3791 if (!Range->contains(ZeroValue))
3792 return true;
3793 }
3794
3795 if (!isa<Constant>(V) && isKnownNonZeroFromAssume(V, Q))
3796 return true;
3797
3798 // Some of the tests below are recursive, so bail out if we hit the limit.
3800 return false;
3801
3802 // Check for pointer simplifications.
3803
3804 if (PointerType *PtrTy = dyn_cast<PointerType>(Ty)) {
3805 // A byval, inalloca may not be null in a non-default addres space. A
3806 // nonnull argument is assumed never 0.
3807 if (const Argument *A = dyn_cast<Argument>(V)) {
3808 if (((A->hasPassPointeeByValueCopyAttr() &&
3809 !NullPointerIsDefined(A->getParent(), PtrTy->getAddressSpace())) ||
3810 A->hasNonNullAttr()))
3811 return true;
3812 }
3813 }
3814
3815 if (const auto *I = dyn_cast<Operator>(V))
3816 if (isKnownNonZeroFromOperator(I, DemandedElts, Q, Depth))
3817 return true;
3818
3819 if (!isa<Constant>(V) &&
3821 return true;
3822
3823 if (const Value *Stripped = stripNullTest(V))
3824 return isKnownNonZero(Stripped, DemandedElts, Q, Depth);
3825
3826 return false;
3827}
3828
3830 unsigned Depth) {
3831 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
3832 APInt DemandedElts =
3833 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
3834 return ::isKnownNonZero(V, DemandedElts, Q, Depth);
3835}
3836
3837/// If the pair of operators are the same invertible function, return the
3838/// the operands of the function corresponding to each input. Otherwise,
3839/// return std::nullopt. An invertible function is one that is 1-to-1 and maps
3840/// every input value to exactly one output value. This is equivalent to
3841/// saying that Op1 and Op2 are equal exactly when the specified pair of
3842/// operands are equal, (except that Op1 and Op2 may be poison more often.)
3843static std::optional<std::pair<Value*, Value*>>
3845 const Operator *Op2) {
3846 if (Op1->getOpcode() != Op2->getOpcode())
3847 return std::nullopt;
3848
3849 auto getOperands = [&](unsigned OpNum) -> auto {
3850 return std::make_pair(Op1->getOperand(OpNum), Op2->getOperand(OpNum));
3851 };
3852
3853 switch (Op1->getOpcode()) {
3854 default:
3855 break;
3856 case Instruction::Or:
3857 if (!cast<PossiblyDisjointInst>(Op1)->isDisjoint() ||
3858 !cast<PossiblyDisjointInst>(Op2)->isDisjoint())
3859 break;
3860 [[fallthrough]];
3861 case Instruction::Xor:
3862 case Instruction::Add: {
3863 Value *Other;
3864 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(0)), m_Value(Other))))
3865 return std::make_pair(Op1->getOperand(1), Other);
3866 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(1)), m_Value(Other))))
3867 return std::make_pair(Op1->getOperand(0), Other);
3868 break;
3869 }
3870 case Instruction::Sub:
3871 if (Op1->getOperand(0) == Op2->getOperand(0))
3872 return getOperands(1);
3873 if (Op1->getOperand(1) == Op2->getOperand(1))
3874 return getOperands(0);
3875 break;
3876 case Instruction::Mul: {
3877 // invertible if A * B == (A * B) mod 2^N where A, and B are integers
3878 // and N is the bitwdith. The nsw case is non-obvious, but proven by
3879 // alive2: https://alive2.llvm.org/ce/z/Z6D5qK
3880 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3881 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3882 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3883 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3884 break;
3885
3886 // Assume operand order has been canonicalized
3887 if (Op1->getOperand(1) == Op2->getOperand(1) &&
3888 isa<ConstantInt>(Op1->getOperand(1)) &&
3889 !cast<ConstantInt>(Op1->getOperand(1))->isZero())
3890 return getOperands(0);
3891 break;
3892 }
3893 case Instruction::Shl: {
3894 // Same as multiplies, with the difference that we don't need to check
3895 // for a non-zero multiply. Shifts always multiply by non-zero.
3896 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3897 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3898 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3899 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3900 break;
3901
3902 if (Op1->getOperand(1) == Op2->getOperand(1))
3903 return getOperands(0);
3904 break;
3905 }
3906 case Instruction::AShr:
3907 case Instruction::LShr: {
3908 auto *PEO1 = cast<PossiblyExactOperator>(Op1);
3909 auto *PEO2 = cast<PossiblyExactOperator>(Op2);
3910 if (!PEO1->isExact() || !PEO2->isExact())
3911 break;
3912
3913 if (Op1->getOperand(1) == Op2->getOperand(1))
3914 return getOperands(0);
3915 break;
3916 }
3917 case Instruction::SExt:
3918 case Instruction::ZExt:
3919 if (Op1->getOperand(0)->getType() == Op2->getOperand(0)->getType())
3920 return getOperands(0);
3921 break;
3922 case Instruction::PHI: {
3923 const PHINode *PN1 = cast<PHINode>(Op1);
3924 const PHINode *PN2 = cast<PHINode>(Op2);
3925
3926 // If PN1 and PN2 are both recurrences, can we prove the entire recurrences
3927 // are a single invertible function of the start values? Note that repeated
3928 // application of an invertible function is also invertible
3929 BinaryOperator *BO1 = nullptr;
3930 Value *Start1 = nullptr, *Step1 = nullptr;
3931 BinaryOperator *BO2 = nullptr;
3932 Value *Start2 = nullptr, *Step2 = nullptr;
3933 if (PN1->getParent() != PN2->getParent() ||
3934 !matchSimpleRecurrence(PN1, BO1, Start1, Step1) ||
3935 !matchSimpleRecurrence(PN2, BO2, Start2, Step2))
3936 break;
3937
3939 cast<Operator>(BO2));
3940 if (!Values)
3941 break;
3942
3943 // We have to be careful of mutually defined recurrences here. Ex:
3944 // * X_i = X_(i-1) OP Y_(i-1), and Y_i = X_(i-1) OP V
3945 // * X_i = Y_i = X_(i-1) OP Y_(i-1)
3946 // The invertibility of these is complicated, and not worth reasoning
3947 // about (yet?).
3948 if (Values->first != PN1 || Values->second != PN2)
3949 break;
3950
3951 return std::make_pair(Start1, Start2);
3952 }
3953 }
3954 return std::nullopt;
3955}
3956
3957/// Return true if V1 == (binop V2, X), where X is known non-zero.
3958/// Only handle a small subset of binops where (binop V2, X) with non-zero X
3959/// implies V2 != V1.
3960static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2,
3961 const APInt &DemandedElts,
3962 const SimplifyQuery &Q, unsigned Depth) {
3964 if (!BO)
3965 return false;
3966 switch (BO->getOpcode()) {
3967 default:
3968 break;
3969 case Instruction::Or:
3970 if (!cast<PossiblyDisjointInst>(V1)->isDisjoint())
3971 break;
3972 [[fallthrough]];
3973 case Instruction::Xor:
3974 case Instruction::Add:
3975 Value *Op = nullptr;
3976 if (V2 == BO->getOperand(0))
3977 Op = BO->getOperand(1);
3978 else if (V2 == BO->getOperand(1))
3979 Op = BO->getOperand(0);
3980 else
3981 return false;
3982 return isKnownNonZero(Op, DemandedElts, Q, Depth + 1);
3983 }
3984 return false;
3985}
3986
3987/// Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and
3988/// the multiplication is nuw or nsw.
3989static bool isNonEqualMul(const Value *V1, const Value *V2,
3990 const APInt &DemandedElts, const SimplifyQuery &Q,
3991 unsigned Depth) {
3992 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3993 const APInt *C;
3994 return match(OBO, m_Mul(m_Specific(V1), m_APInt(C))) &&
3995 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3996 !C->isZero() && !C->isOne() &&
3997 isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3998 }
3999 return false;
4000}
4001
4002/// Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and
4003/// the shift is nuw or nsw.
4004static bool isNonEqualShl(const Value *V1, const Value *V2,
4005 const APInt &DemandedElts, const SimplifyQuery &Q,
4006 unsigned Depth) {
4007 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
4008 const APInt *C;
4009 return match(OBO, m_Shl(m_Specific(V1), m_APInt(C))) &&
4010 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
4011 !C->isZero() && isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
4012 }
4013 return false;
4014}
4015
4016static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2,
4017 const APInt &DemandedElts, const SimplifyQuery &Q,
4018 unsigned Depth) {
4019 // Check two PHIs are in same block.
4020 if (PN1->getParent() != PN2->getParent())
4021 return false;
4022
4024 bool UsedFullRecursion = false;
4025 for (const BasicBlock *IncomBB : PN1->blocks()) {
4026 if (!VisitedBBs.insert(IncomBB).second)
4027 continue; // Don't reprocess blocks that we have dealt with already.
4028 const Value *IV1 = PN1->getIncomingValueForBlock(IncomBB);
4029 const Value *IV2 = PN2->getIncomingValueForBlock(IncomBB);
4030 const APInt *C1, *C2;
4031 if (match(IV1, m_APInt(C1)) && match(IV2, m_APInt(C2)) && *C1 != *C2)
4032 continue;
4033
4034 // Only one pair of phi operands is allowed for full recursion.
4035 if (UsedFullRecursion)
4036 return false;
4037
4039 RecQ.CxtI = IncomBB->getTerminator();
4040 if (!isKnownNonEqual(IV1, IV2, DemandedElts, RecQ, Depth + 1))
4041 return false;
4042 UsedFullRecursion = true;
4043 }
4044 return true;
4045}
4046
4047static bool isNonEqualSelect(const Value *V1, const Value *V2,
4048 const APInt &DemandedElts, const SimplifyQuery &Q,
4049 unsigned Depth) {
4050 const SelectInst *SI1 = dyn_cast<SelectInst>(V1);
4051 if (!SI1)
4052 return false;
4053
4054 if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2)) {
4055 const Value *Cond1 = SI1->getCondition();
4056 const Value *Cond2 = SI2->getCondition();
4057 if (Cond1 == Cond2)
4058 return isKnownNonEqual(SI1->getTrueValue(), SI2->getTrueValue(),
4059 DemandedElts, Q, Depth + 1) &&
4060 isKnownNonEqual(SI1->getFalseValue(), SI2->getFalseValue(),
4061 DemandedElts, Q, Depth + 1);
4062 }
4063 return isKnownNonEqual(SI1->getTrueValue(), V2, DemandedElts, Q, Depth + 1) &&
4064 isKnownNonEqual(SI1->getFalseValue(), V2, DemandedElts, Q, Depth + 1);
4065}
4066
4067// Check to see if A is both a GEP and is the incoming value for a PHI in the
4068// loop, and B is either a ptr or another GEP. If the PHI has 2 incoming values,
4069// one of them being the recursive GEP A and the other a ptr at same base and at
4070// the same/higher offset than B we are only incrementing the pointer further in
4071// loop if offset of recursive GEP is greater than 0.
4073 const SimplifyQuery &Q) {
4074 if (!A->getType()->isPointerTy() || !B->getType()->isPointerTy())
4075 return false;
4076
4077 auto *GEPA = dyn_cast<GEPOperator>(A);
4078 if (!GEPA || GEPA->getNumIndices() != 1 || !isa<Constant>(GEPA->idx_begin()))
4079 return false;
4080
4081 // Handle 2 incoming PHI values with one being a recursive GEP.
4082 auto *PN = dyn_cast<PHINode>(GEPA->getPointerOperand());
4083 if (!PN || PN->getNumIncomingValues() != 2)
4084 return false;
4085
4086 // Search for the recursive GEP as an incoming operand, and record that as
4087 // Step.
4088 Value *Start = nullptr;
4089 Value *Step = const_cast<Value *>(A);
4090 if (PN->getIncomingValue(0) == Step)
4091 Start = PN->getIncomingValue(1);
4092 else if (PN->getIncomingValue(1) == Step)
4093 Start = PN->getIncomingValue(0);
4094 else
4095 return false;
4096
4097 // Other incoming node base should match the B base.
4098 // StartOffset >= OffsetB && StepOffset > 0?
4099 // StartOffset <= OffsetB && StepOffset < 0?
4100 // Is non-equal if above are true.
4101 // We use stripAndAccumulateInBoundsConstantOffsets to restrict the
4102 // optimisation to inbounds GEPs only.
4103 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(Start->getType());
4104 APInt StartOffset(IndexWidth, 0);
4105 Start = Start->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StartOffset);
4106 APInt StepOffset(IndexWidth, 0);
4107 Step = Step->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StepOffset);
4108
4109 // Check if Base Pointer of Step matches the PHI.
4110 if (Step != PN)
4111 return false;
4112 APInt OffsetB(IndexWidth, 0);
4113 B = B->stripAndAccumulateInBoundsConstantOffsets(Q.DL, OffsetB);
4114 return Start == B &&
4115 ((StartOffset.sge(OffsetB) && StepOffset.isStrictlyPositive()) ||
4116 (StartOffset.sle(OffsetB) && StepOffset.isNegative()));
4117}
4118
4119static bool isKnownNonEqualFromContext(const Value *V1, const Value *V2,
4120 const SimplifyQuery &Q, unsigned Depth) {
4121 if (!Q.CxtI)
4122 return false;
4123
4124 // Try to infer NonEqual based on information from dominating conditions.
4125 if (Q.DC && Q.DT) {
4126 auto IsKnownNonEqualFromDominatingCondition = [&](const Value *V) {
4127 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
4128 Value *Cond = BI->getCondition();
4129 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4130 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()) &&
4132 /*LHSIsTrue=*/true, Depth)
4133 .value_or(false))
4134 return true;
4135
4136 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4137 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()) &&
4139 /*LHSIsTrue=*/false, Depth)
4140 .value_or(false))
4141 return true;
4142 }
4143
4144 return false;
4145 };
4146
4147 if (IsKnownNonEqualFromDominatingCondition(V1) ||
4148 IsKnownNonEqualFromDominatingCondition(V2))
4149 return true;
4150 }
4151
4152 if (!Q.AC)
4153 return false;
4154
4155 // Try to infer NonEqual based on information from assumptions.
4156 for (auto &AssumeVH : Q.AC->assumptionsFor(V1)) {
4157 if (!AssumeVH)
4158 continue;
4159 CallInst *I = cast<CallInst>(AssumeVH);
4160
4161 assert(I->getFunction() == Q.CxtI->getFunction() &&
4162 "Got assumption for the wrong function!");
4163 assert(I->getIntrinsicID() == Intrinsic::assume &&
4164 "must be an assume intrinsic");
4165
4166 if (isImpliedCondition(I->getArgOperand(0), ICmpInst::ICMP_NE, V1, V2, Q.DL,
4167 /*LHSIsTrue=*/true, Depth)
4168 .value_or(false) &&
4170 return true;
4171 }
4172
4173 return false;
4174}
4175
4176/// Return true if it is known that V1 != V2.
4177static bool isKnownNonEqual(const Value *V1, const Value *V2,
4178 const APInt &DemandedElts, const SimplifyQuery &Q,
4179 unsigned Depth) {
4180 if (V1 == V2)
4181 return false;
4182 if (V1->getType() != V2->getType())
4183 // We can't look through casts yet.
4184 return false;
4185
4187 return false;
4188
4189 // See if we can recurse through (exactly one of) our operands. This
4190 // requires our operation be 1-to-1 and map every input value to exactly
4191 // one output value. Such an operation is invertible.
4192 auto *O1 = dyn_cast<Operator>(V1);
4193 auto *O2 = dyn_cast<Operator>(V2);
4194 if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) {
4195 if (auto Values = getInvertibleOperands(O1, O2))
4196 return isKnownNonEqual(Values->first, Values->second, DemandedElts, Q,
4197 Depth + 1);
4198
4199 if (const PHINode *PN1 = dyn_cast<PHINode>(V1)) {
4200 const PHINode *PN2 = cast<PHINode>(V2);
4201 // FIXME: This is missing a generalization to handle the case where one is
4202 // a PHI and another one isn't.
4203 if (isNonEqualPHIs(PN1, PN2, DemandedElts, Q, Depth))
4204 return true;
4205 };
4206 }
4207
4208 if (isModifyingBinopOfNonZero(V1, V2, DemandedElts, Q, Depth) ||
4209 isModifyingBinopOfNonZero(V2, V1, DemandedElts, Q, Depth))
4210 return true;
4211
4212 if (isNonEqualMul(V1, V2, DemandedElts, Q, Depth) ||
4213 isNonEqualMul(V2, V1, DemandedElts, Q, Depth))
4214 return true;
4215
4216 if (isNonEqualShl(V1, V2, DemandedElts, Q, Depth) ||
4217 isNonEqualShl(V2, V1, DemandedElts, Q, Depth))
4218 return true;
4219
4220 if (V1->getType()->isIntOrIntVectorTy()) {
4221 // Are any known bits in V1 contradictory to known bits in V2? If V1
4222 // has a known zero where V2 has a known one, they must not be equal.
4223 KnownBits Known1 = computeKnownBits(V1, DemandedElts, Q, Depth);
4224 if (!Known1.isUnknown()) {
4225 KnownBits Known2 = computeKnownBits(V2, DemandedElts, Q, Depth);
4226 if (Known1.Zero.intersects(Known2.One) ||
4227 Known2.Zero.intersects(Known1.One))
4228 return true;
4229 }
4230 }
4231
4232 if (isNonEqualSelect(V1, V2, DemandedElts, Q, Depth) ||
4233 isNonEqualSelect(V2, V1, DemandedElts, Q, Depth))
4234 return true;
4235
4238 return true;
4239
4240 Value *A, *B;
4241 // PtrToInts are NonEqual if their Ptrs are NonEqual.
4242 // Check PtrToInt type matches the pointer size.
4243 if (match(V1, m_PtrToIntSameSize(Q.DL, m_Value(A))) &&
4245 return isKnownNonEqual(A, B, DemandedElts, Q, Depth + 1);
4246
4247 if (isKnownNonEqualFromContext(V1, V2, Q, Depth))
4248 return true;
4249
4250 return false;
4251}
4252
4253/// For vector constants, loop over the elements and find the constant with the
4254/// minimum number of sign bits. Return 0 if the value is not a vector constant
4255/// or if any element was not analyzed; otherwise, return the count for the
4256/// element with the minimum number of sign bits.
4258 const APInt &DemandedElts,
4259 unsigned TyBits) {
4260 const auto *CV = dyn_cast<Constant>(V);
4261 if (!CV || !isa<FixedVectorType>(CV->getType()))
4262 return 0;
4263
4264 unsigned MinSignBits = TyBits;
4265 unsigned NumElts = cast<FixedVectorType>(CV->getType())->getNumElements();
4266 for (unsigned i = 0; i != NumElts; ++i) {
4267 if (!DemandedElts[i])
4268 continue;
4269 // If we find a non-ConstantInt, bail out.
4270 auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));
4271 if (!Elt)
4272 return 0;
4273
4274 MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());
4275 }
4276
4277 return MinSignBits;
4278}
4279
4280static unsigned ComputeNumSignBitsImpl(const Value *V,
4281 const APInt &DemandedElts,
4282 const SimplifyQuery &Q, unsigned Depth);
4283
4284static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
4285 const SimplifyQuery &Q, unsigned Depth) {
4286 unsigned Result = ComputeNumSignBitsImpl(V, DemandedElts, Q, Depth);
4287 assert(Result > 0 && "At least one sign bit needs to be present!");
4288 return Result;
4289}
4290
4291/// Return the number of times the sign bit of the register is replicated into
4292/// the other bits. We know that at least 1 bit is always equal to the sign bit
4293/// (itself), but other cases can give us information. For example, immediately
4294/// after an "ashr X, 2", we know that the top 3 bits are all equal to each
4295/// other, so we return 3. For vectors, return the number of sign bits for the
4296/// vector element with the minimum number of known sign bits of the demanded
4297/// elements in the vector specified by DemandedElts.
4298static unsigned ComputeNumSignBitsImpl(const Value *V,
4299 const APInt &DemandedElts,
4300 const SimplifyQuery &Q, unsigned Depth) {
4301 Type *Ty = V->getType();
4302#ifndef NDEBUG
4303 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4304
4305 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
4306 assert(
4307 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
4308 "DemandedElt width should equal the fixed vector number of elements");
4309 } else {
4310 assert(DemandedElts == APInt(1, 1) &&
4311 "DemandedElt width should be 1 for scalars");
4312 }
4313#endif
4314
4315 // We return the minimum number of sign bits that are guaranteed to be present
4316 // in V, so for undef we have to conservatively return 1. We don't have the
4317 // same behavior for poison though -- that's a FIXME today.
4318
4319 Type *ScalarTy = Ty->getScalarType();
4320 unsigned TyBits = ScalarTy->isPointerTy() ?
4321 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
4322 Q.DL.getTypeSizeInBits(ScalarTy);
4323
4324 unsigned Tmp, Tmp2;
4325 unsigned FirstAnswer = 1;
4326
4327 // Note that ConstantInt is handled by the general computeKnownBits case
4328 // below.
4329
4331 return 1;
4332
4333 if (auto *U = dyn_cast<Operator>(V)) {
4334 switch (Operator::getOpcode(V)) {
4335 default: break;
4336 case Instruction::BitCast: {
4337 Value *Src = U->getOperand(0);
4338 Type *SrcTy = Src->getType();
4339
4340 // Skip if the source type is not an integer or integer vector type
4341 // This ensures we only process integer-like types
4342 if (!SrcTy->isIntOrIntVectorTy())
4343 break;
4344
4345 unsigned SrcBits = SrcTy->getScalarSizeInBits();
4346
4347 // Bitcast 'large element' scalar/vector to 'small element' vector.
4348 if ((SrcBits % TyBits) != 0)
4349 break;
4350
4351 // Only proceed if the destination type is a fixed-size vector
4352 if (isa<FixedVectorType>(Ty)) {
4353 // Fast case - sign splat can be simply split across the small elements.
4354 // This works for both vector and scalar sources
4355 Tmp = ComputeNumSignBits(Src, Q, Depth + 1);
4356 if (Tmp == SrcBits)
4357 return TyBits;
4358 }
4359 break;
4360 }
4361 case Instruction::SExt:
4362 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
4363 return ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1) +
4364 Tmp;
4365
4366 case Instruction::SDiv: {
4367 const APInt *Denominator;
4368 // sdiv X, C -> adds log(C) sign bits.
4369 if (match(U->getOperand(1), m_APInt(Denominator))) {
4370
4371 // Ignore non-positive denominator.
4372 if (!Denominator->isStrictlyPositive())
4373 break;
4374
4375 // Calculate the incoming numerator bits.
4376 unsigned NumBits =
4377 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4378
4379 // Add floor(log(C)) bits to the numerator bits.
4380 return std::min(TyBits, NumBits + Denominator->logBase2());
4381 }
4382 break;
4383 }
4384
4385 case Instruction::SRem: {
4386 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4387
4388 const APInt *Denominator;
4389 // srem X, C -> we know that the result is within [-C+1,C) when C is a
4390 // positive constant. This let us put a lower bound on the number of sign
4391 // bits.
4392 if (match(U->getOperand(1), m_APInt(Denominator))) {
4393
4394 // Ignore non-positive denominator.
4395 if (Denominator->isStrictlyPositive()) {
4396 // Calculate the leading sign bit constraints by examining the
4397 // denominator. Given that the denominator is positive, there are two
4398 // cases:
4399 //
4400 // 1. The numerator is positive. The result range is [0,C) and
4401 // [0,C) u< (1 << ceilLogBase2(C)).
4402 //
4403 // 2. The numerator is negative. Then the result range is (-C,0] and
4404 // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
4405 //
4406 // Thus a lower bound on the number of sign bits is `TyBits -
4407 // ceilLogBase2(C)`.
4408
4409 unsigned ResBits = TyBits - Denominator->ceilLogBase2();
4410 Tmp = std::max(Tmp, ResBits);
4411 }
4412 }
4413 return Tmp;
4414 }
4415
4416 case Instruction::AShr: {
4417 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4418 // ashr X, C -> adds C sign bits. Vectors too.
4419 const APInt *ShAmt;
4420 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4421 if (ShAmt->uge(TyBits))
4422 break; // Bad shift.
4423 unsigned ShAmtLimited = ShAmt->getZExtValue();
4424 Tmp += ShAmtLimited;
4425 if (Tmp > TyBits) Tmp = TyBits;
4426 }
4427 return Tmp;
4428 }
4429 case Instruction::Shl: {
4430 const APInt *ShAmt;
4431 Value *X = nullptr;
4432 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4433 // shl destroys sign bits.
4434 if (ShAmt->uge(TyBits))
4435 break; // Bad shift.
4436 // We can look through a zext (more or less treating it as a sext) if
4437 // all extended bits are shifted out.
4438 if (match(U->getOperand(0), m_ZExt(m_Value(X))) &&
4439 ShAmt->uge(TyBits - X->getType()->getScalarSizeInBits())) {
4440 Tmp = ComputeNumSignBits(X, DemandedElts, Q, Depth + 1);
4441 Tmp += TyBits - X->getType()->getScalarSizeInBits();
4442 } else
4443 Tmp =
4444 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4445 if (ShAmt->uge(Tmp))
4446 break; // Shifted all sign bits out.
4447 Tmp2 = ShAmt->getZExtValue();
4448 return Tmp - Tmp2;
4449 }
4450 break;
4451 }
4452 case Instruction::And:
4453 case Instruction::Or:
4454 case Instruction::Xor: // NOT is handled here.
4455 // Logical binary ops preserve the number of sign bits at the worst.
4456 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4457 if (Tmp != 1) {
4458 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4459 FirstAnswer = std::min(Tmp, Tmp2);
4460 // We computed what we know about the sign bits as our first
4461 // answer. Now proceed to the generic code that uses
4462 // computeKnownBits, and pick whichever answer is better.
4463 }
4464 break;
4465
4466 case Instruction::Select: {
4467 // If we have a clamp pattern, we know that the number of sign bits will
4468 // be the minimum of the clamp min/max range.
4469 const Value *X;
4470 const APInt *CLow, *CHigh;
4471 if (isSignedMinMaxClamp(U, X, CLow, CHigh))
4472 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4473
4474 Tmp = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4475 if (Tmp == 1)
4476 break;
4477 Tmp2 = ComputeNumSignBits(U->getOperand(2), DemandedElts, Q, Depth + 1);
4478 return std::min(Tmp, Tmp2);
4479 }
4480
4481 case Instruction::Add:
4482 // Add can have at most one carry bit. Thus we know that the output
4483 // is, at worst, one more bit than the inputs.
4484 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4485 if (Tmp == 1) break;
4486
4487 // Special case decrementing a value (ADD X, -1):
4488 if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
4489 if (CRHS->isAllOnesValue()) {
4490 KnownBits Known(TyBits);
4491 computeKnownBits(U->getOperand(0), DemandedElts, Known, Q, Depth + 1);
4492
4493 // If the input is known to be 0 or 1, the output is 0/-1, which is
4494 // all sign bits set.
4495 if ((Known.Zero | 1).isAllOnes())
4496 return TyBits;
4497
4498 // If we are subtracting one from a positive number, there is no carry
4499 // out of the result.
4500 if (Known.isNonNegative())
4501 return Tmp;
4502 }
4503
4504 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4505 if (Tmp2 == 1)
4506 break;
4507 return std::min(Tmp, Tmp2) - 1;
4508
4509 case Instruction::Sub:
4510 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4511 if (Tmp2 == 1)
4512 break;
4513
4514 // Handle NEG.
4515 if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
4516 if (CLHS->isNullValue()) {
4517 KnownBits Known(TyBits);
4518 computeKnownBits(U->getOperand(1), DemandedElts, Known, Q, Depth + 1);
4519 // If the input is known to be 0 or 1, the output is 0/-1, which is
4520 // all sign bits set.
4521 if ((Known.Zero | 1).isAllOnes())
4522 return TyBits;
4523
4524 // If the input is known to be positive (the sign bit is known clear),
4525 // the output of the NEG has the same number of sign bits as the
4526 // input.
4527 if (Known.isNonNegative())
4528 return Tmp2;
4529
4530 // Otherwise, we treat this like a SUB.
4531 }
4532
4533 // Sub can have at most one carry bit. Thus we know that the output
4534 // is, at worst, one more bit than the inputs.
4535 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4536 if (Tmp == 1)
4537 break;
4538 return std::min(Tmp, Tmp2) - 1;
4539
4540 case Instruction::Mul: {
4541 // The output of the Mul can be at most twice the valid bits in the
4542 // inputs.
4543 unsigned SignBitsOp0 =
4544 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4545 if (SignBitsOp0 == 1)
4546 break;
4547 unsigned SignBitsOp1 =
4548 ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4549 if (SignBitsOp1 == 1)
4550 break;
4551 unsigned OutValidBits =
4552 (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);
4553 return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
4554 }
4555
4556 case Instruction::PHI: {
4557 const PHINode *PN = cast<PHINode>(U);
4558 unsigned NumIncomingValues = PN->getNumIncomingValues();
4559 // Don't analyze large in-degree PHIs.
4560 if (NumIncomingValues > 4) break;
4561 // Unreachable blocks may have zero-operand PHI nodes.
4562 if (NumIncomingValues == 0) break;
4563
4564 // Take the minimum of all incoming values. This can't infinitely loop
4565 // because of our depth threshold.
4567 Tmp = TyBits;
4568 for (unsigned i = 0, e = NumIncomingValues; i != e; ++i) {
4569 if (Tmp == 1) return Tmp;
4570 RecQ.CxtI = PN->getIncomingBlock(i)->getTerminator();
4571 Tmp = std::min(Tmp, ComputeNumSignBits(PN->getIncomingValue(i),
4572 DemandedElts, RecQ, Depth + 1));
4573 }
4574 return Tmp;
4575 }
4576
4577 case Instruction::Trunc: {
4578 // If the input contained enough sign bits that some remain after the
4579 // truncation, then we can make use of that. Otherwise we don't know
4580 // anything.
4581 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4582 unsigned OperandTyBits = U->getOperand(0)->getType()->getScalarSizeInBits();
4583 if (Tmp > (OperandTyBits - TyBits))
4584 return Tmp - (OperandTyBits - TyBits);
4585
4586 return 1;
4587 }
4588
4589 case Instruction::ExtractElement:
4590 // Look through extract element. At the moment we keep this simple and
4591 // skip tracking the specific element. But at least we might find
4592 // information valid for all elements of the vector (for example if vector
4593 // is sign extended, shifted, etc).
4594 return ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4595
4596 case Instruction::ShuffleVector: {
4597 // Collect the minimum number of sign bits that are shared by every vector
4598 // element referenced by the shuffle.
4599 auto *Shuf = dyn_cast<ShuffleVectorInst>(U);
4600 if (!Shuf) {
4601 // FIXME: Add support for shufflevector constant expressions.
4602 return 1;
4603 }
4604 APInt DemandedLHS, DemandedRHS;
4605 // For undef elements, we don't know anything about the common state of
4606 // the shuffle result.
4607 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
4608 return 1;
4609 Tmp = std::numeric_limits<unsigned>::max();
4610 if (!!DemandedLHS) {
4611 const Value *LHS = Shuf->getOperand(0);
4612 Tmp = ComputeNumSignBits(LHS, DemandedLHS, Q, Depth + 1);
4613 }
4614 // If we don't know anything, early out and try computeKnownBits
4615 // fall-back.
4616 if (Tmp == 1)
4617 break;
4618 if (!!DemandedRHS) {
4619 const Value *RHS = Shuf->getOperand(1);
4620 Tmp2 = ComputeNumSignBits(RHS, DemandedRHS, Q, Depth + 1);
4621 Tmp = std::min(Tmp, Tmp2);
4622 }
4623 // If we don't know anything, early out and try computeKnownBits
4624 // fall-back.
4625 if (Tmp == 1)
4626 break;
4627 assert(Tmp <= TyBits && "Failed to determine minimum sign bits");
4628 return Tmp;
4629 }
4630 case Instruction::Call: {
4631 if (const auto *II = dyn_cast<IntrinsicInst>(U)) {
4632 switch (II->getIntrinsicID()) {
4633 default:
4634 break;
4635 case Intrinsic::abs:
4636 Tmp =
4637 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4638 if (Tmp == 1)
4639 break;
4640
4641 // Absolute value reduces number of sign bits by at most 1.
4642 return Tmp - 1;
4643 case Intrinsic::smin:
4644 case Intrinsic::smax: {
4645 const APInt *CLow, *CHigh;
4646 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
4647 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4648 }
4649 }
4650 }
4651 }
4652 }
4653 }
4654
4655 // Finally, if we can prove that the top bits of the result are 0's or 1's,
4656 // use this information.
4657
4658 // If we can examine all elements of a vector constant successfully, we're
4659 // done (we can't do any better than that). If not, keep trying.
4660 if (unsigned VecSignBits =
4661 computeNumSignBitsVectorConstant(V, DemandedElts, TyBits))
4662 return VecSignBits;
4663
4664 KnownBits Known(TyBits);
4665 computeKnownBits(V, DemandedElts, Known, Q, Depth);
4666
4667 // If we know that the sign bit is either zero or one, determine the number of
4668 // identical bits in the top of the input value.
4669 return std::max(FirstAnswer, Known.countMinSignBits());
4670}
4671
4673 const TargetLibraryInfo *TLI) {
4674 const Function *F = CB.getCalledFunction();
4675 if (!F)
4677
4678 if (F->isIntrinsic())
4679 return F->getIntrinsicID();
4680
4681 // We are going to infer semantics of a library function based on mapping it
4682 // to an LLVM intrinsic. Check that the library function is available from
4683 // this callbase and in this environment.
4684 LibFunc Func;
4685 if (F->hasLocalLinkage() || !TLI || !TLI->getLibFunc(CB, Func) ||
4686 !CB.onlyReadsMemory())
4688
4689 switch (Func) {
4690 default:
4691 break;
4692 case LibFunc_sin:
4693 case LibFunc_sinf:
4694 case LibFunc_sinl:
4695 return Intrinsic::sin;
4696 case LibFunc_cos:
4697 case LibFunc_cosf:
4698 case LibFunc_cosl:
4699 return Intrinsic::cos;
4700 case LibFunc_tan:
4701 case LibFunc_tanf:
4702 case LibFunc_tanl:
4703 return Intrinsic::tan;
4704 case LibFunc_asin:
4705 case LibFunc_asinf:
4706 case LibFunc_asinl:
4707 return Intrinsic::asin;
4708 case LibFunc_acos:
4709 case LibFunc_acosf:
4710 case LibFunc_acosl:
4711 return Intrinsic::acos;
4712 case LibFunc_atan:
4713 case LibFunc_atanf:
4714 case LibFunc_atanl:
4715 return Intrinsic::atan;
4716 case LibFunc_atan2:
4717 case LibFunc_atan2f:
4718 case LibFunc_atan2l:
4719 return Intrinsic::atan2;
4720 case LibFunc_sinh:
4721 case LibFunc_sinhf:
4722 case LibFunc_sinhl:
4723 return Intrinsic::sinh;
4724 case LibFunc_cosh:
4725 case LibFunc_coshf:
4726 case LibFunc_coshl:
4727 return Intrinsic::cosh;
4728 case LibFunc_tanh:
4729 case LibFunc_tanhf:
4730 case LibFunc_tanhl:
4731 return Intrinsic::tanh;
4732 case LibFunc_exp:
4733 case LibFunc_expf:
4734 case LibFunc_expl:
4735 return Intrinsic::exp;
4736 case LibFunc_exp2:
4737 case LibFunc_exp2f:
4738 case LibFunc_exp2l:
4739 return Intrinsic::exp2;
4740 case LibFunc_exp10:
4741 case LibFunc_exp10f:
4742 case LibFunc_exp10l:
4743 return Intrinsic::exp10;
4744 case LibFunc_log:
4745 case LibFunc_logf:
4746 case LibFunc_logl:
4747 return Intrinsic::log;
4748 case LibFunc_log10:
4749 case LibFunc_log10f:
4750 case LibFunc_log10l:
4751 return Intrinsic::log10;
4752 case LibFunc_log2:
4753 case LibFunc_log2f:
4754 case LibFunc_log2l:
4755 return Intrinsic::log2;
4756 case LibFunc_fabs:
4757 case LibFunc_fabsf:
4758 case LibFunc_fabsl:
4759 return Intrinsic::fabs;
4760 case LibFunc_fmin:
4761 case LibFunc_fminf:
4762 case LibFunc_fminl:
4763 return Intrinsic::minnum;
4764 case LibFunc_fmax:
4765 case LibFunc_fmaxf:
4766 case LibFunc_fmaxl:
4767 return Intrinsic::maxnum;
4768 case LibFunc_copysign:
4769 case LibFunc_copysignf:
4770 case LibFunc_copysignl:
4771 return Intrinsic::copysign;
4772 case LibFunc_floor:
4773 case LibFunc_floorf:
4774 case LibFunc_floorl:
4775 return Intrinsic::floor;
4776 case LibFunc_ceil:
4777 case LibFunc_ceilf:
4778 case LibFunc_ceill:
4779 return Intrinsic::ceil;
4780 case LibFunc_trunc:
4781 case LibFunc_truncf:
4782 case LibFunc_truncl:
4783 return Intrinsic::trunc;
4784 case LibFunc_rint:
4785 case LibFunc_rintf:
4786 case LibFunc_rintl:
4787 return Intrinsic::rint;
4788 case LibFunc_nearbyint:
4789 case LibFunc_nearbyintf:
4790 case LibFunc_nearbyintl:
4791 return Intrinsic::nearbyint;
4792 case LibFunc_round:
4793 case LibFunc_roundf:
4794 case LibFunc_roundl:
4795 return Intrinsic::round;
4796 case LibFunc_roundeven:
4797 case LibFunc_roundevenf:
4798 case LibFunc_roundevenl:
4799 return Intrinsic::roundeven;
4800 case LibFunc_pow:
4801 case LibFunc_powf:
4802 case LibFunc_powl:
4803 return Intrinsic::pow;
4804 case LibFunc_sqrt:
4805 case LibFunc_sqrtf:
4806 case LibFunc_sqrtl:
4807 return Intrinsic::sqrt;
4808 }
4809
4811}
4812
4813/// Given an exploded icmp instruction, return true if the comparison only
4814/// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if
4815/// the result of the comparison is true when the input value is signed.
4817 bool &TrueIfSigned) {
4818 switch (Pred) {
4819 case ICmpInst::ICMP_SLT: // True if LHS s< 0
4820 TrueIfSigned = true;
4821 return RHS.isZero();
4822 case ICmpInst::ICMP_SLE: // True if LHS s<= -1
4823 TrueIfSigned = true;
4824 return RHS.isAllOnes();
4825 case ICmpInst::ICMP_SGT: // True if LHS s> -1
4826 TrueIfSigned = false;
4827 return RHS.isAllOnes();
4828 case ICmpInst::ICMP_SGE: // True if LHS s>= 0
4829 TrueIfSigned = false;
4830 return RHS.isZero();
4831 case ICmpInst::ICMP_UGT:
4832 // True if LHS u> RHS and RHS == sign-bit-mask - 1
4833 TrueIfSigned = true;
4834 return RHS.isMaxSignedValue();
4835 case ICmpInst::ICMP_UGE:
4836 // True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4837 TrueIfSigned = true;
4838 return RHS.isMinSignedValue();
4839 case ICmpInst::ICMP_ULT:
4840 // True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4841 TrueIfSigned = false;
4842 return RHS.isMinSignedValue();
4843 case ICmpInst::ICMP_ULE:
4844 // True if LHS u<= RHS and RHS == sign-bit-mask - 1
4845 TrueIfSigned = false;
4846 return RHS.isMaxSignedValue();
4847 default:
4848 return false;
4849 }
4850}
4851
4853 bool CondIsTrue,
4854 const Instruction *CxtI,
4855 KnownFPClass &KnownFromContext,
4856 unsigned Depth = 0) {
4857 Value *A, *B;
4859 (CondIsTrue ? match(Cond, m_LogicalAnd(m_Value(A), m_Value(B)))
4860 : match(Cond, m_LogicalOr(m_Value(A), m_Value(B))))) {
4861 computeKnownFPClassFromCond(V, A, CondIsTrue, CxtI, KnownFromContext,
4862 Depth + 1);
4863 computeKnownFPClassFromCond(V, B, CondIsTrue, CxtI, KnownFromContext,
4864 Depth + 1);
4865 return;
4866 }
4868 computeKnownFPClassFromCond(V, A, !CondIsTrue, CxtI, KnownFromContext,
4869 Depth + 1);
4870 return;
4871 }
4872 CmpPredicate Pred;
4873 Value *LHS;
4874 uint64_t ClassVal = 0;
4875 const APFloat *CRHS;
4876 const APInt *RHS;
4877 if (match(Cond, m_FCmp(Pred, m_Value(LHS), m_APFloat(CRHS)))) {
4878 auto [CmpVal, MaskIfTrue, MaskIfFalse] = fcmpImpliesClass(
4879 Pred, *cast<Instruction>(Cond)->getParent()->getParent(), LHS, *CRHS,
4880 LHS != V);
4881 if (CmpVal == V)
4882 KnownFromContext.knownNot(~(CondIsTrue ? MaskIfTrue : MaskIfFalse));
4884 m_Specific(V), m_ConstantInt(ClassVal)))) {
4885 FPClassTest Mask = static_cast<FPClassTest>(ClassVal);
4886 KnownFromContext.knownNot(CondIsTrue ? ~Mask : Mask);
4887 } else if (match(Cond, m_ICmp(Pred, m_ElementWiseBitCast(m_Specific(V)),
4888 m_APInt(RHS)))) {
4889 bool TrueIfSigned;
4890 if (!isSignBitCheck(Pred, *RHS, TrueIfSigned))
4891 return;
4892 if (TrueIfSigned == CondIsTrue)
4893 KnownFromContext.signBitMustBeOne();
4894 else
4895 KnownFromContext.signBitMustBeZero();
4896 }
4897}
4898
4899/// Compute the minimum and maximum values (inclusive) for the exponent of \p V,
4900/// assuming it is not nan. Returns {min, max, max-assuming-nonzero}. A value
4901/// frexp(0) = 0, so the tighter max-assuming-nonzero bound is only usable when
4902/// \p V is known not to be a logical zero (e.g., for fabs(x) < 0.25, the non-0
4903/// exponent range is [-149, -2], but the 0 edge case is above this range).
4904static std::tuple<int, int, int>
4906 if (!Q.CxtI || !Q.DC || !Q.DT)
4908
4909 // Intersect the bounds implied by every dominating condition, keeping the
4910 // tightest maximum. A value may participate in multiple compares
4911 // (e.g. fabs(x) < 2.0 and fabs(x) < 1.0), and the tighter one wins.
4912 int MaxExp = APFloat::IEK_Inf;
4913 int MaxExpNonZero = APFloat::IEK_Inf;
4914
4915 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
4916 CmpPredicate Pred;
4917 const APFloat *LimitC;
4918 if (!match(BI->getCondition(),
4919 m_FCmp(Pred, m_FAbs(m_Specific(V)), m_Finite(LimitC))))
4920 continue;
4921
4922 if (Pred == FCmpInst::FCMP_ORD || Pred == FCmpInst::FCMP_UNO ||
4923 Pred == FCmpInst::FCMP_TRUE || Pred == FCmpInst::FCMP_FALSE)
4924 continue;
4925
4926 // If fabs(x) <= K, implies the exponent min exp range.
4927 // if fabs(x) >= K, swap the successor
4928 bool IsLessEqual =
4929 Pred == FCmpInst::FCMP_OLT || Pred == FCmpInst::FCMP_OLE ||
4930 Pred == FCmpInst::FCMP_ULT || Pred == FCmpInst::FCMP_ULE ||
4931 Pred == FCmpInst::FCMP_OEQ || Pred == FCmpInst::FCMP_UEQ;
4932
4933 bool KnownStrictlyLess =
4934 Pred == FCmpInst::FCMP_OLT || Pred == FCmpInst::FCMP_ULT ||
4935 Pred == FCmpInst::FCMP_OGE || Pred == FCmpInst::FCMP_UGE;
4936
4937 BasicBlockEdge Edge1(BI->getParent(),
4938 BI->getSuccessor(IsLessEqual ? 0 : 1));
4939 if (Q.DT->dominates(Edge1, Q.CxtI->getParent())) {
4940 // frexp returns an exponent one greater than ilogb.
4941 int Exp = ilogb(*LimitC) + 1;
4942
4943 // A strict bound fabs(V) < 2^n forces ilogb(V) <= n - 1, so the max frexp
4944 // exponent drops by one when K is exact power of two.
4945 if (KnownStrictlyLess && LimitC->getExactLog2Abs() != INT_MIN)
4946 --Exp;
4947
4948 // frexp(0) = 0, which the bound above (assuming a normal nonzero value)
4949 // may exclude.
4950
4951 // TODO: Figure out lower bound to detect no-underflow.
4952 MaxExpNonZero = std::min(MaxExpNonZero, Exp);
4953 MaxExp = std::min(MaxExp, std::max(Exp, 0));
4954 }
4955 }
4956
4957 return {APFloat::IEK_NaN, MaxExp, MaxExpNonZero};
4958}
4959
4961 const SimplifyQuery &Q) {
4962 KnownFPClass KnownFromContext;
4963
4964 if (Q.CC && Q.CC->AffectedValues.contains(V))
4966 KnownFromContext);
4967
4968 if (!Q.CxtI)
4969 return KnownFromContext;
4970
4971 if (Q.DC && Q.DT) {
4972 // Handle dominating conditions.
4973 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
4974 Value *Cond = BI->getCondition();
4975
4976 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4977 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
4978 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/true, Q.CxtI,
4979 KnownFromContext);
4980
4981 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4982 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
4983 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/false, Q.CxtI,
4984 KnownFromContext);
4985 }
4986 }
4987
4988 if (!Q.AC)
4989 return KnownFromContext;
4990
4991 // Try to restrict the floating-point classes based on information from
4992 // assumptions.
4993 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
4994 if (!AssumeVH)
4995 continue;
4996 CallInst *I = cast<CallInst>(AssumeVH);
4997
4998 assert(I->getFunction() == Q.CxtI->getParent()->getParent() &&
4999 "Got assumption for the wrong function!");
5000 assert(I->getIntrinsicID() == Intrinsic::assume &&
5001 "must be an assume intrinsic");
5002
5003 if (!isValidAssumeForContext(I, Q))
5004 continue;
5005
5006 computeKnownFPClassFromCond(V, I->getArgOperand(0),
5007 /*CondIsTrue=*/true, Q.CxtI, KnownFromContext);
5008 }
5009
5010 return KnownFromContext;
5011}
5012
5014 Value *Arm, bool Invert,
5015 const SimplifyQuery &SQ,
5016 unsigned Depth) {
5017
5018 KnownFPClass KnownSrc;
5020 /*CondIsTrue=*/!Invert, SQ.CxtI, KnownSrc,
5021 Depth + 1);
5022 KnownSrc = KnownSrc.unionWith(Known);
5023 if (KnownSrc.isUnknown())
5024 return;
5025
5026 if (isGuaranteedNotToBeUndef(Arm, SQ.AC, SQ.CxtI, SQ.DT, Depth + 1))
5027 Known = KnownSrc;
5028}
5029
5030void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
5031 FPClassTest InterestedClasses, KnownFPClass &Known,
5032 const SimplifyQuery &Q, unsigned Depth);
5033
5035 FPClassTest InterestedClasses,
5036 const SimplifyQuery &Q, unsigned Depth) {
5037 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
5038 APInt DemandedElts =
5039 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
5040 computeKnownFPClass(V, DemandedElts, InterestedClasses, Known, Q, Depth);
5041}
5042
5044 const APInt &DemandedElts,
5045 FPClassTest InterestedClasses,
5047 const SimplifyQuery &Q,
5048 unsigned Depth) {
5049 if ((InterestedClasses &
5051 return;
5052
5053 KnownFPClass KnownSrc;
5054 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5055 KnownSrc, Q, Depth + 1);
5056 Known = KnownFPClass::fptrunc(KnownSrc);
5057}
5058
5060 switch (IID) {
5061 case Intrinsic::minimum:
5063 case Intrinsic::maximum:
5065 case Intrinsic::minimumnum:
5067 case Intrinsic::maximumnum:
5069 case Intrinsic::minnum:
5071 case Intrinsic::maxnum:
5073 default:
5074 llvm_unreachable("not a floating-point min-max intrinsic");
5075 }
5076}
5077
5078/// \return true if this is a floating point value that is known to have a
5079/// magnitude smaller than 1. i.e., fabs(X) <= 1.0 or is nan.
5080static bool isAbsoluteValueULEOne(const Value *V) {
5081 // TODO: Handle frexp
5082 // TODO: Other rounding intrinsics?
5083 // TODO: Try computeKnownExponentRangeFromContext
5084
5085 // fabs(x - floor(x)) <= 1
5086 const Value *SubFloorX;
5087 if (match(V, m_FSub(m_Value(SubFloorX),
5089 return true;
5090
5093}
5094
5095void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
5096 FPClassTest InterestedClasses, KnownFPClass &Known,
5097 const SimplifyQuery &Q, unsigned Depth) {
5098 assert(Known.isUnknown() && "should not be called with known information");
5099
5100 if (!DemandedElts) {
5101 // No demanded elts, better to assume we don't know anything.
5102 Known.resetAll();
5103 return;
5104 }
5105
5106 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
5107
5108 if (auto *CFP = dyn_cast<ConstantFP>(V)) {
5109 Known = KnownFPClass(CFP->getValueAPF());
5110 return;
5111 }
5112
5114 Known.KnownFPClasses = fcPosZero;
5115 Known.SignBit = false;
5116 return;
5117 }
5118
5119 if (isa<PoisonValue>(V)) {
5120 Known.KnownFPClasses = fcNone;
5121 Known.SignBit = false;
5122 return;
5123 }
5124
5125 // Try to handle fixed width vector constants
5126 auto *VFVTy = dyn_cast<FixedVectorType>(V->getType());
5127 const Constant *CV = dyn_cast<Constant>(V);
5128 if (VFVTy && CV) {
5129 Known.KnownFPClasses = fcNone;
5130 bool SignBitAllZero = true;
5131 bool SignBitAllOne = true;
5132
5133 // For vectors, verify that each element is not NaN.
5134 unsigned NumElts = VFVTy->getNumElements();
5135 for (unsigned i = 0; i != NumElts; ++i) {
5136 if (!DemandedElts[i])
5137 continue;
5138
5139 Constant *Elt = CV->getAggregateElement(i);
5140 if (!Elt) {
5141 Known = KnownFPClass();
5142 return;
5143 }
5144 if (isa<PoisonValue>(Elt))
5145 continue;
5146 auto *CElt = dyn_cast<ConstantFP>(Elt);
5147 if (!CElt) {
5148 Known = KnownFPClass();
5149 return;
5150 }
5151
5152 const APFloat &C = CElt->getValueAPF();
5153 Known.KnownFPClasses |= C.classify();
5154 if (C.isNegative())
5155 SignBitAllZero = false;
5156 else
5157 SignBitAllOne = false;
5158 }
5159 if (SignBitAllOne != SignBitAllZero)
5160 Known.SignBit = SignBitAllOne;
5161 return;
5162 }
5163
5164 if (const auto *CDS = dyn_cast<ConstantDataSequential>(V)) {
5165 Known.KnownFPClasses = fcNone;
5166 for (size_t I = 0, E = CDS->getNumElements(); I != E; ++I)
5167 Known |= CDS->getElementAsAPFloat(I).classify();
5168 return;
5169 }
5170
5171 if (const auto *CA = dyn_cast<ConstantAggregate>(V)) {
5172 // TODO: Handle complex aggregates
5173 Known.KnownFPClasses = fcNone;
5174 for (const Use &Op : CA->operands()) {
5175 auto *CFP = dyn_cast<ConstantFP>(Op.get());
5176 if (!CFP) {
5177 Known = KnownFPClass();
5178 return;
5179 }
5180
5181 Known |= CFP->getValueAPF().classify();
5182 }
5183
5184 return;
5185 }
5186
5187 FPClassTest KnownNotFromFlags = fcNone;
5188 if (const auto *CB = dyn_cast<CallBase>(V))
5189 KnownNotFromFlags |= CB->getRetNoFPClass();
5190 else if (const auto *Arg = dyn_cast<Argument>(V))
5191 KnownNotFromFlags |= Arg->getNoFPClass();
5192
5193 const Operator *Op = dyn_cast<Operator>(V);
5195 if (FPOp->hasNoNaNs())
5196 KnownNotFromFlags |= fcNan;
5197 if (FPOp->hasNoInfs())
5198 KnownNotFromFlags |= fcInf;
5199 }
5200
5201 KnownFPClass AssumedClasses = computeKnownFPClassFromContext(V, Q);
5202 KnownNotFromFlags |= ~AssumedClasses.KnownFPClasses;
5203
5204 // We no longer need to find out about these bits from inputs if we can
5205 // assume this from flags/attributes.
5206 InterestedClasses &= ~KnownNotFromFlags;
5207
5208 llvm::scope_exit ClearClassesFromFlags([=, &Known] {
5209 Known.knownNot(KnownNotFromFlags);
5210 if (!Known.SignBit && AssumedClasses.SignBit) {
5211 if (*AssumedClasses.SignBit)
5212 Known.signBitMustBeOne();
5213 else
5214 Known.signBitMustBeZero();
5215 }
5216 });
5217
5218 if (!Op)
5219 return;
5220
5221 // All recursive calls that increase depth must come after this.
5223 return;
5224
5225 const unsigned Opc = Op->getOpcode();
5226 switch (Opc) {
5227 case Instruction::FNeg: {
5228 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5229 Known, Q, Depth + 1);
5230 Known.fneg();
5231 break;
5232 }
5233 case Instruction::Select: {
5234 auto ComputeForArm = [&](Value *Arm, bool Invert) {
5235 KnownFPClass Res;
5236 computeKnownFPClass(Arm, DemandedElts, InterestedClasses, Res, Q,
5237 Depth + 1);
5238 adjustKnownFPClassForSelectArm(Res, Op->getOperand(0), Arm, Invert, Q,
5239 Depth);
5240 return Res;
5241 };
5242 // Only known if known in both the LHS and RHS.
5243 Known =
5244 ComputeForArm(Op->getOperand(1), /*Invert=*/false)
5245 .intersectWith(ComputeForArm(Op->getOperand(2), /*Invert=*/true));
5246 break;
5247 }
5248 case Instruction::Load: {
5249 const MDNode *NoFPClass =
5250 cast<LoadInst>(Op)->getMetadata(LLVMContext::MD_nofpclass);
5251 if (!NoFPClass)
5252 break;
5253
5254 ConstantInt *MaskVal =
5256 Known.knownNot(static_cast<FPClassTest>(MaskVal->getZExtValue()));
5257 break;
5258 }
5259 case Instruction::Call: {
5260 const CallInst *II = cast<CallInst>(Op);
5261 const Intrinsic::ID IID = II->getIntrinsicID();
5262 switch (IID) {
5263 case Intrinsic::fabs: {
5264 if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) {
5265 // If we only care about the sign bit we don't need to inspect the
5266 // operand.
5267 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5268 InterestedClasses, Known, Q, Depth + 1);
5269 }
5270
5271 Known.fabs();
5272 break;
5273 }
5274 case Intrinsic::copysign: {
5275 KnownFPClass KnownSign;
5276
5277 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5278 Known, Q, Depth + 1);
5279 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5280 KnownSign, Q, Depth + 1);
5281 Known.copysign(KnownSign);
5282 break;
5283 }
5284 case Intrinsic::fma:
5285 case Intrinsic::fmuladd: {
5286 if ((InterestedClasses & fcNegative) == fcNone)
5287 break;
5288
5289 // FIXME: This should check isGuaranteedNotToBeUndef
5290 if (II->getArgOperand(0) == II->getArgOperand(1)) {
5291 KnownFPClass KnownSrc, KnownAddend;
5292 computeKnownFPClass(II->getArgOperand(2), DemandedElts,
5293 InterestedClasses, KnownAddend, Q, Depth + 1);
5294 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5295 InterestedClasses, KnownSrc, Q, Depth + 1);
5296
5297 const Function *F = II->getFunction();
5298 const fltSemantics &FltSem =
5299 II->getType()->getScalarType()->getFltSemantics();
5301 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5302
5303 if (KnownNotFromFlags & fcNan) {
5304 KnownSrc.knownNot(fcNan);
5305 KnownAddend.knownNot(fcNan);
5306 }
5307
5308 if (KnownNotFromFlags & fcInf) {
5309 KnownSrc.knownNot(fcInf);
5310 KnownAddend.knownNot(fcInf);
5311 }
5312
5313 Known = KnownFPClass::fma_square(KnownSrc, KnownAddend, Mode);
5314 break;
5315 }
5316
5317 KnownFPClass KnownSrc[3];
5318 for (int I = 0; I != 3; ++I) {
5319 computeKnownFPClass(II->getArgOperand(I), DemandedElts,
5320 InterestedClasses, KnownSrc[I], Q, Depth + 1);
5321 if (KnownSrc[I].isUnknown())
5322 return;
5323
5324 if (KnownNotFromFlags & fcNan)
5325 KnownSrc[I].knownNot(fcNan);
5326 if (KnownNotFromFlags & fcInf)
5327 KnownSrc[I].knownNot(fcInf);
5328 }
5329
5330 const Function *F = II->getFunction();
5331 const fltSemantics &FltSem =
5332 II->getType()->getScalarType()->getFltSemantics();
5334 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5335 Known = KnownFPClass::fma(KnownSrc[0], KnownSrc[1], KnownSrc[2], Mode);
5336 break;
5337 }
5338 case Intrinsic::sqrt:
5339 case Intrinsic::experimental_constrained_sqrt: {
5340 KnownFPClass KnownSrc;
5341 FPClassTest InterestedSrcs = InterestedClasses;
5342 if (InterestedClasses & fcNan)
5343 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5344
5345 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5346 KnownSrc, Q, Depth + 1);
5347
5349
5350 bool HasNSZ = Q.IIQ.hasNoSignedZeros(II);
5351 if (!HasNSZ) {
5352 const Function *F = II->getFunction();
5353 const fltSemantics &FltSem =
5354 II->getType()->getScalarType()->getFltSemantics();
5355 Mode = F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5356 }
5357
5358 Known = KnownFPClass::sqrt(KnownSrc, Mode);
5359 if (HasNSZ)
5360 Known.knownNot(fcNegZero);
5361
5362 break;
5363 }
5364 case Intrinsic::sin: {
5365 KnownFPClass KnownSrc;
5366 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5367 KnownSrc, Q, Depth + 1);
5368 Known = KnownFPClass::sin(KnownSrc);
5369 break;
5370 }
5371 case Intrinsic::cos: {
5372 KnownFPClass KnownSrc;
5373 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5374 KnownSrc, Q, Depth + 1);
5375 Known = KnownFPClass::cos(KnownSrc);
5376 break;
5377 }
5378 case Intrinsic::tan: {
5379 KnownFPClass KnownSrc;
5380 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5381 KnownSrc, Q, Depth + 1);
5382 Known = KnownFPClass::tan(KnownSrc);
5383 break;
5384 }
5385 case Intrinsic::sinh: {
5386 KnownFPClass KnownSrc;
5387 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5388 KnownSrc, Q, Depth + 1);
5389 Known = KnownFPClass::sinh(KnownSrc);
5390 break;
5391 }
5392 case Intrinsic::cosh: {
5393 KnownFPClass KnownSrc;
5394 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5395 KnownSrc, Q, Depth + 1);
5396 Known = KnownFPClass::cosh(KnownSrc);
5397 break;
5398 }
5399 case Intrinsic::tanh: {
5400 KnownFPClass KnownSrc;
5401 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5402 KnownSrc, Q, Depth + 1);
5403 Known = KnownFPClass::tanh(KnownSrc);
5404 break;
5405 }
5406 case Intrinsic::asin: {
5407 KnownFPClass KnownSrc;
5408 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5409 KnownSrc, Q, Depth + 1);
5410 Known = KnownFPClass::asin(KnownSrc);
5411 break;
5412 }
5413 case Intrinsic::acos: {
5414 KnownFPClass KnownSrc;
5415 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5416 KnownSrc, Q, Depth + 1);
5417 Known = KnownFPClass::acos(KnownSrc);
5418 break;
5419 }
5420 case Intrinsic::atan: {
5421 KnownFPClass KnownSrc;
5422 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5423 KnownSrc, Q, Depth + 1);
5424 Known = KnownFPClass::atan(KnownSrc);
5425 break;
5426 }
5427 case Intrinsic::atan2: {
5428 KnownFPClass KnownLHS, KnownRHS;
5429 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5430 KnownLHS, Q, Depth + 1);
5431 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5432 KnownRHS, Q, Depth + 1);
5433 Known = KnownFPClass::atan2(KnownLHS, KnownRHS);
5434 break;
5435 }
5436 case Intrinsic::maxnum:
5437 case Intrinsic::minnum:
5438 case Intrinsic::minimum:
5439 case Intrinsic::maximum:
5440 case Intrinsic::minimumnum:
5441 case Intrinsic::maximumnum: {
5442 KnownFPClass KnownLHS, KnownRHS;
5443 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5444 KnownLHS, Q, Depth + 1);
5445 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5446 KnownRHS, Q, Depth + 1);
5447
5448 const Function *F = II->getFunction();
5449
5451 F ? F->getDenormalMode(
5452 II->getType()->getScalarType()->getFltSemantics())
5454
5455 Known = KnownFPClass::minMaxLike(KnownLHS, KnownRHS, getMinMaxKind(IID),
5456 Mode);
5457 break;
5458 }
5459 case Intrinsic::canonicalize: {
5460 KnownFPClass KnownSrc;
5461 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5462 KnownSrc, Q, Depth + 1);
5463
5464 const Function *F = II->getFunction();
5465 DenormalMode DenormMode =
5466 F ? F->getDenormalMode(
5467 II->getType()->getScalarType()->getFltSemantics())
5469 Known = KnownFPClass::canonicalize(KnownSrc, DenormMode);
5470 break;
5471 }
5472 case Intrinsic::vector_reduce_fmax:
5473 case Intrinsic::vector_reduce_fmin:
5474 case Intrinsic::vector_reduce_fmaximum:
5475 case Intrinsic::vector_reduce_fminimum: {
5476 // reduce min/max will choose an element from one of the vector elements,
5477 // so we can infer and class information that is common to all elements.
5478 Known = computeKnownFPClass(II->getArgOperand(0), II->getFastMathFlags(),
5479 InterestedClasses, Q, Depth + 1);
5480 // Can only propagate sign if output is never NaN.
5481 if (!Known.isKnownNeverNaN())
5482 Known.SignBit.reset();
5483 break;
5484 }
5485 // reverse preserves all characteristics of the input vec's element.
5486 case Intrinsic::vector_reverse:
5488 II->getArgOperand(0), DemandedElts.reverseBits(),
5489 II->getFastMathFlags(), InterestedClasses, Q, Depth + 1);
5490 break;
5491 case Intrinsic::trunc:
5492 case Intrinsic::floor:
5493 case Intrinsic::ceil:
5494 case Intrinsic::rint:
5495 case Intrinsic::nearbyint:
5496 case Intrinsic::round:
5497 case Intrinsic::roundeven: {
5498 KnownFPClass KnownSrc;
5499 FPClassTest InterestedSrcs = InterestedClasses;
5500 if (InterestedSrcs & fcPosFinite)
5501 InterestedSrcs |= fcPosFinite;
5502 if (InterestedSrcs & fcNegFinite)
5503 InterestedSrcs |= fcNegFinite;
5504 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5505 KnownSrc, Q, Depth + 1);
5506
5508 KnownSrc, IID == Intrinsic::trunc,
5509 V->getType()->getScalarType()->isMultiUnitFPType());
5510 break;
5511 }
5512 case Intrinsic::exp:
5513 case Intrinsic::exp2:
5514 case Intrinsic::exp10:
5515 case Intrinsic::amdgcn_exp2: {
5516 KnownFPClass KnownSrc;
5517 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5518 KnownSrc, Q, Depth + 1);
5519
5520 Known = KnownFPClass::exp(KnownSrc);
5521
5522 Type *EltTy = II->getType()->getScalarType();
5523 if (IID == Intrinsic::amdgcn_exp2 && EltTy->isFloatTy())
5524 Known.knownNot(fcSubnormal);
5525
5526 break;
5527 }
5528 case Intrinsic::fptrunc_round: {
5529 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,
5530 Q, Depth);
5531 break;
5532 }
5533 case Intrinsic::log:
5534 case Intrinsic::log10:
5535 case Intrinsic::log2:
5536 case Intrinsic::experimental_constrained_log:
5537 case Intrinsic::experimental_constrained_log10:
5538 case Intrinsic::experimental_constrained_log2:
5539 case Intrinsic::amdgcn_log: {
5540 Type *EltTy = II->getType()->getScalarType();
5541
5542 // log(+inf) -> +inf
5543 // log([+-]0.0) -> -inf
5544 // log(-inf) -> nan
5545 // log(-x) -> nan
5546 if ((InterestedClasses & (fcNan | fcInf)) != fcNone) {
5547 FPClassTest InterestedSrcs = InterestedClasses;
5548 if ((InterestedClasses & fcNegInf) != fcNone)
5549 InterestedSrcs |= fcZero | fcSubnormal;
5550 if ((InterestedClasses & fcNan) != fcNone)
5551 InterestedSrcs |= fcNan | fcNegative;
5552
5553 KnownFPClass KnownSrc;
5554 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5555 KnownSrc, Q, Depth + 1);
5556
5557 const Function *F = II->getFunction();
5558 DenormalMode Mode = F ? F->getDenormalMode(EltTy->getFltSemantics())
5560 Known = KnownFPClass::log(KnownSrc, Mode);
5561 }
5562
5563 break;
5564 }
5565 case Intrinsic::powi: {
5566 if ((InterestedClasses & (fcNan | fcInf | fcNegative)) == fcNone)
5567 break;
5568
5569 const Value *Exp = II->getArgOperand(1);
5570 Type *ExpTy = Exp->getType();
5571 unsigned BitWidth = ExpTy->getScalarType()->getIntegerBitWidth();
5572 KnownBits ExponentKnownBits(BitWidth);
5573 computeKnownBits(Exp, isa<VectorType>(ExpTy) ? DemandedElts : APInt(1, 1),
5574 ExponentKnownBits, Q, Depth + 1);
5575
5576 FPClassTest InterestedSrcs = fcNone;
5577 if (InterestedClasses & fcNan)
5578 InterestedSrcs |= fcNan;
5579 if (!ExponentKnownBits.isZero()) {
5580 if (InterestedClasses & fcInf)
5581 InterestedSrcs |= fcFinite | fcInf;
5582 if ((InterestedClasses & fcNegative) && !ExponentKnownBits.isEven())
5583 InterestedSrcs |= fcNegative;
5584 }
5585
5586 KnownFPClass KnownSrc;
5587 if (InterestedSrcs != fcNone)
5588 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5589 KnownSrc, Q, Depth + 1);
5590
5591 Known = KnownFPClass::powi(KnownSrc, ExponentKnownBits);
5592 break;
5593 }
5594 case Intrinsic::ldexp: {
5595 KnownFPClass KnownSrc;
5596 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5597 KnownSrc, Q, Depth + 1);
5598 // Can refine inf/zero handling based on the exponent operand.
5599 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;
5600
5601 const Value *ExpArg = II->getArgOperand(1);
5602 ConstantRange ExpKnownRange =
5603 ((KnownSrc.KnownFPClasses & ExpInfoMask) != fcNone)
5604 ? computeConstantRange(ExpArg, /*ForSigned=*/true, Q, Depth + 1)
5605 : ConstantRange::getFull(
5606 ExpArg->getType()->getScalarSizeInBits());
5607
5608 const fltSemantics &Flt =
5609 II->getType()->getScalarType()->getFltSemantics();
5610
5611 const Function *F = II->getFunction();
5613 F ? F->getDenormalMode(Flt) : DenormalMode::getDynamic();
5614
5615 Known = KnownFPClass::ldexp(KnownSrc, ExpKnownRange.getSignedMin(),
5616 ExpKnownRange.getSignedMax(), Flt, Mode);
5617 break;
5618 }
5619 case Intrinsic::arithmetic_fence: {
5620 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5621 Known, Q, Depth + 1);
5622 break;
5623 }
5624 case Intrinsic::experimental_constrained_sitofp:
5625 case Intrinsic::experimental_constrained_uitofp:
5626 // Cannot produce nan
5627 Known.knownNot(fcNan);
5628
5629 // sitofp and uitofp turn into +0.0 for zero.
5630 Known.knownNot(fcNegZero);
5631
5632 // Integers cannot be subnormal
5633 Known.knownNot(fcSubnormal);
5634
5635 if (IID == Intrinsic::experimental_constrained_uitofp)
5636 Known.signBitMustBeZero();
5637
5638 // TODO: Copy inf handling from instructions
5639 break;
5640
5641 case Intrinsic::amdgcn_fract: {
5642 Known.knownNot(fcInf);
5643
5644 if (InterestedClasses & fcNan) {
5645 KnownFPClass KnownSrc;
5646 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5647 InterestedClasses, KnownSrc, Q, Depth + 1);
5648
5649 if (KnownSrc.isKnownNeverInfOrNaN())
5650 Known.knownNot(fcNan);
5651 else if (KnownSrc.isKnownNever(fcSNan))
5652 Known.knownNot(fcSNan);
5653 }
5654
5655 break;
5656 }
5657 case Intrinsic::amdgcn_rcp: {
5658 KnownFPClass KnownSrc;
5659 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5660 KnownSrc, Q, Depth + 1);
5661
5662 Known.propagateNaN(KnownSrc);
5663
5664 Type *EltTy = II->getType()->getScalarType();
5665
5666 // f32 denormal always flushed.
5667 if (EltTy->isFloatTy()) {
5668 Known.knownNot(fcSubnormal);
5669 KnownSrc.knownNot(fcSubnormal);
5670 }
5671
5672 if (KnownSrc.isKnownNever(fcNegative))
5673 Known.knownNot(fcNegative);
5674 if (KnownSrc.isKnownNever(fcPositive))
5675 Known.knownNot(fcPositive);
5676
5677 if (const Function *F = II->getFunction()) {
5678 DenormalMode Mode = F->getDenormalMode(EltTy->getFltSemantics());
5679 if (KnownSrc.isKnownNeverLogicalPosZero(Mode))
5680 Known.knownNot(fcPosInf);
5681 if (KnownSrc.isKnownNeverLogicalNegZero(Mode))
5682 Known.knownNot(fcNegInf);
5683 }
5684
5685 break;
5686 }
5687 case Intrinsic::amdgcn_rsq: {
5688 KnownFPClass KnownSrc;
5689 // The only negative value that can be returned is -inf for -0 inputs.
5691
5692 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5693 KnownSrc, Q, Depth + 1);
5694
5695 // Negative -> nan
5696 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
5697 Known.knownNot(fcNan);
5698 else if (KnownSrc.isKnownNever(fcSNan))
5699 Known.knownNot(fcSNan);
5700
5701 // +inf -> +0
5702 if (KnownSrc.isKnownNeverPosInfinity())
5703 Known.knownNot(fcPosZero);
5704
5705 Type *EltTy = II->getType()->getScalarType();
5706
5707 // f32 denormal always flushed.
5708 if (EltTy->isFloatTy())
5709 Known.knownNot(fcPosSubnormal);
5710
5711 if (const Function *F = II->getFunction()) {
5712 DenormalMode Mode = F->getDenormalMode(EltTy->getFltSemantics());
5713
5714 // -0 -> -inf
5715 if (KnownSrc.isKnownNeverLogicalNegZero(Mode))
5716 Known.knownNot(fcNegInf);
5717
5718 // +0 -> +inf
5719 if (KnownSrc.isKnownNeverLogicalPosZero(Mode))
5720 Known.knownNot(fcPosInf);
5721 }
5722
5723 break;
5724 }
5725 case Intrinsic::amdgcn_trig_preop: {
5726 // Always returns a value [0, 1)
5727 Known.knownNot(fcNan | fcInf | fcNegative);
5728 break;
5729 }
5730 default:
5731 break;
5732 }
5733
5734 break;
5735 }
5736 case Instruction::FAdd:
5737 case Instruction::FSub: {
5738 KnownFPClass KnownLHS, KnownRHS;
5739 bool WantNegative =
5740 Op->getOpcode() == Instruction::FAdd &&
5741 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;
5742 bool WantNaN = (InterestedClasses & fcNan) != fcNone;
5743 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;
5744
5745 if (!WantNaN && !WantNegative && !WantNegZero)
5746 break;
5747
5748 FPClassTest InterestedSrcs = InterestedClasses;
5749 if (WantNegative)
5750 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5751 if (InterestedClasses & fcNan)
5752 InterestedSrcs |= fcInf;
5753 computeKnownFPClass(Op->getOperand(1), DemandedElts, InterestedSrcs,
5754 KnownRHS, Q, Depth + 1);
5755
5756 // Special case fadd x, x, which is the canonical form of fmul x, 2.
5757 bool Self = Op->getOperand(0) == Op->getOperand(1) &&
5758 isGuaranteedNotToBeUndef(Op->getOperand(0), Q.AC, Q.CxtI, Q.DT,
5759 Depth + 1);
5760 if (Self)
5761 KnownLHS = KnownRHS;
5762
5763 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||
5764 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) ||
5765 WantNegZero || Opc == Instruction::FSub) {
5766
5767 // FIXME: Context function should always be passed in separately
5768 const Function *F = cast<Instruction>(Op)->getFunction();
5769 const fltSemantics &FltSem =
5770 Op->getType()->getScalarType()->getFltSemantics();
5772 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5773
5774 if (Self && Opc == Instruction::FAdd) {
5775 Known = KnownFPClass::fadd_self(KnownLHS, Mode);
5776 } else {
5777 // RHS is canonically cheaper to compute. Skip inspecting the LHS if
5778 // there's no point.
5779
5780 if (!Self) {
5781 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedSrcs,
5782 KnownLHS, Q, Depth + 1);
5783 }
5784
5785 Known = Opc == Instruction::FAdd
5786 ? KnownFPClass::fadd(KnownLHS, KnownRHS, Mode)
5787 : KnownFPClass::fsub(KnownLHS, KnownRHS, Mode);
5788 }
5789 }
5790
5791 break;
5792 }
5793 case Instruction::FMul: {
5794 const Function *F = cast<Instruction>(Op)->getFunction();
5796 F ? F->getDenormalMode(
5797 Op->getType()->getScalarType()->getFltSemantics())
5799
5800 Value *LHS = Op->getOperand(0);
5801 Value *RHS = Op->getOperand(1);
5802 // X * X is always non-negative or a NaN.
5803 // FIXME: Should check isGuaranteedNotToBeUndef
5804 if (LHS == RHS) {
5805 KnownFPClass KnownSrc;
5806 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownSrc, Q,
5807 Depth + 1);
5808 Known = KnownFPClass::square(KnownSrc, Mode);
5809 break;
5810 }
5811
5812 KnownFPClass KnownLHS, KnownRHS;
5813
5814 const APFloat *CRHS;
5815 if (match(RHS, m_APFloat(CRHS))) {
5816 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Q,
5817 Depth + 1);
5818 Known = KnownFPClass::fmul(KnownLHS, *CRHS, Mode);
5819 } else {
5820 computeKnownFPClass(RHS, DemandedElts, fcAllFlags, KnownRHS, Q,
5821 Depth + 1);
5822 // TODO: Improve accuracy in unfused FMA pattern. We can prove an
5823 // additional not-nan if the addend is known-not negative infinity if the
5824 // multiply is known-not infinity.
5825
5826 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Q,
5827 Depth + 1);
5828 Known = KnownFPClass::fmul(KnownLHS, KnownRHS, Mode);
5829 }
5830
5831 /// Propgate no-infs if the other source is known smaller than one, such
5832 /// that this cannot introduce overflow.
5833 if (KnownLHS.isKnownNever(fcInf) && isAbsoluteValueULEOne(RHS))
5834 Known.knownNot(fcInf);
5835 else if (KnownRHS.isKnownNever(fcInf) && isAbsoluteValueULEOne(LHS))
5836 Known.knownNot(fcInf);
5837
5838 break;
5839 }
5840 case Instruction::FDiv:
5841 case Instruction::FRem: {
5842 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
5843
5844 if (Op->getOpcode() == Instruction::FRem)
5845 Known.knownNot(fcInf);
5846
5847 if (Op->getOperand(0) == Op->getOperand(1) &&
5848 isGuaranteedNotToBeUndef(Op->getOperand(0), Q.AC, Q.CxtI, Q.DT)) {
5849 if (Op->getOpcode() == Instruction::FDiv) {
5850 // X / X is always exactly 1.0 or a NaN.
5851 Known.KnownFPClasses = fcNan | fcPosNormal;
5852 } else {
5853 // X % X is always exactly [+-]0.0 or a NaN.
5854 Known.KnownFPClasses = fcNan | fcZero;
5855 }
5856
5857 if (!WantNan)
5858 break;
5859
5860 KnownFPClass KnownSrc;
5861 computeKnownFPClass(Op->getOperand(0), DemandedElts,
5862 fcNan | fcInf | fcZero | fcSubnormal, KnownSrc, Q,
5863 Depth + 1);
5864 const Function *F = cast<Instruction>(Op)->getFunction();
5865 const fltSemantics &FltSem =
5866 Op->getType()->getScalarType()->getFltSemantics();
5867
5869 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5870
5871 Known = Op->getOpcode() == Instruction::FDiv
5872 ? KnownFPClass::fdiv_self(KnownSrc, Mode)
5873 : KnownFPClass::frem_self(KnownSrc, Mode);
5874 break;
5875 }
5876
5877 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
5878 const bool WantPositive =
5879 Opc == Instruction::FRem && (InterestedClasses & fcPositive) != fcNone;
5880 if (!WantNan && !WantNegative && !WantPositive)
5881 break;
5882
5883 KnownFPClass KnownLHS, KnownRHS;
5884
5885 computeKnownFPClass(Op->getOperand(1), DemandedElts,
5886 fcNan | fcInf | fcZero | fcNegative, KnownRHS, Q,
5887 Depth + 1);
5888
5889 bool KnowSomethingUseful = KnownRHS.isKnownNeverNaN() ||
5890 KnownRHS.isKnownNever(fcNegative) ||
5891 KnownRHS.isKnownNever(fcPositive);
5892
5893 if (KnowSomethingUseful || WantPositive) {
5894 computeKnownFPClass(Op->getOperand(0), DemandedElts, fcAllFlags, KnownLHS,
5895 Q, Depth + 1);
5896 }
5897
5898 const Function *F = cast<Instruction>(Op)->getFunction();
5899 const fltSemantics &FltSem =
5900 Op->getType()->getScalarType()->getFltSemantics();
5901
5902 if (Op->getOpcode() == Instruction::FDiv) {
5904 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5905 Known = KnownFPClass::fdiv(KnownLHS, KnownRHS, Mode);
5906 } else {
5907 // Inf REM x and x REM 0 produce NaN.
5908 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5909 KnownLHS.isKnownNeverInfinity() && F &&
5910 KnownRHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))) {
5911 Known.knownNot(fcNan);
5912 }
5913
5914 // The sign for frem is the same as the first operand.
5915 if (KnownLHS.cannotBeOrderedLessThanZero())
5917 if (KnownLHS.cannotBeOrderedGreaterThanZero())
5919
5920 // See if we can be more aggressive about the sign of 0.
5921 if (KnownLHS.isKnownNever(fcNegative))
5922 Known.knownNot(fcNegative);
5923 if (KnownLHS.isKnownNever(fcPositive))
5924 Known.knownNot(fcPositive);
5925 }
5926
5927 break;
5928 }
5929 case Instruction::FPExt: {
5930 KnownFPClass KnownSrc;
5931 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5932 KnownSrc, Q, Depth + 1);
5933
5934 const fltSemantics &DstTy =
5935 Op->getType()->getScalarType()->getFltSemantics();
5936 const fltSemantics &SrcTy =
5937 Op->getOperand(0)->getType()->getScalarType()->getFltSemantics();
5938
5939 Known = KnownFPClass::fpext(KnownSrc, DstTy, SrcTy);
5940 break;
5941 }
5942 case Instruction::FPTrunc: {
5943 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known, Q,
5944 Depth);
5945 break;
5946 }
5947 case Instruction::SIToFP:
5948 case Instruction::UIToFP: {
5949 // Cannot produce nan
5950 Known.knownNot(fcNan);
5951
5952 // Integers cannot be subnormal
5953 Known.knownNot(fcSubnormal);
5954
5955 // sitofp and uitofp turn into +0.0 for zero.
5956 Known.knownNot(fcNegZero);
5957
5958 // UIToFP is always non-negative regardless of known bits.
5959 if (Op->getOpcode() == Instruction::UIToFP)
5960 Known.signBitMustBeZero();
5961
5962 // Only compute known bits if we can learn something useful from them.
5963 if (!(InterestedClasses & (fcPosZero | fcNormal | fcInf)))
5964 break;
5965
5966 KnownBits IntKnown =
5967 computeKnownBits(Op->getOperand(0), DemandedElts, Q, Depth + 1);
5968
5969 // If the integer is non-zero, the result cannot be +0.0
5970 if (IntKnown.isNonZero())
5971 Known.knownNot(fcPosZero);
5972
5973 if (Op->getOpcode() == Instruction::SIToFP) {
5974 // If the signed integer is known non-negative, the result is
5975 // non-negative. If the signed integer is known negative, the result is
5976 // negative.
5977 if (IntKnown.isNonNegative()) {
5978 Known.signBitMustBeZero();
5979 } else if (IntKnown.isNegative()) {
5980 Known.signBitMustBeOne();
5981 }
5982 }
5983
5984 // Guard kept for ilogb()
5985 if (InterestedClasses & fcInf) {
5986 // Get width of largest magnitude integer known.
5987 // This still works for a signed minimum value because the largest FP
5988 // value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx).
5989 int IntSize = IntKnown.getBitWidth();
5990 if (Op->getOpcode() == Instruction::UIToFP)
5991 IntSize -= IntKnown.countMinLeadingZeros();
5992 else if (Op->getOpcode() == Instruction::SIToFP)
5993 IntSize -= IntKnown.countMinSignBits();
5994
5995 // If the exponent of the largest finite FP value can hold the largest
5996 // integer, the result of the cast must be finite.
5997 Type *FPTy = Op->getType()->getScalarType();
5998 if (ilogb(APFloat::getLargest(FPTy->getFltSemantics())) >= IntSize)
5999 Known.knownNot(fcInf);
6000 }
6001
6002 break;
6003 }
6004 case Instruction::ExtractElement: {
6005 // Look through extract element. If the index is non-constant or
6006 // out-of-range demand all elements, otherwise just the extracted element.
6007 const Value *Vec = Op->getOperand(0);
6008
6009 APInt DemandedVecElts;
6010 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
6011 unsigned NumElts = VecTy->getNumElements();
6012 DemandedVecElts = APInt::getAllOnes(NumElts);
6013 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(1));
6014 if (CIdx && CIdx->getValue().ult(NumElts))
6015 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
6016 } else {
6017 DemandedVecElts = APInt(1, 1);
6018 }
6019
6020 return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,
6021 Q, Depth + 1);
6022 }
6023 case Instruction::InsertElement: {
6024 if (isa<ScalableVectorType>(Op->getType()))
6025 return;
6026
6027 const Value *Vec = Op->getOperand(0);
6028 const Value *Elt = Op->getOperand(1);
6029 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(2));
6030 unsigned NumElts = DemandedElts.getBitWidth();
6031 APInt DemandedVecElts = DemandedElts;
6032 bool NeedsElt = true;
6033 // If we know the index we are inserting to, clear it from Vec check.
6034 if (CIdx && CIdx->getValue().ult(NumElts)) {
6035 DemandedVecElts.clearBit(CIdx->getZExtValue());
6036 NeedsElt = DemandedElts[CIdx->getZExtValue()];
6037 }
6038
6039 // Do we demand the inserted element?
6040 if (NeedsElt) {
6041 computeKnownFPClass(Elt, Known, InterestedClasses, Q, Depth + 1);
6042 // If we don't know any bits, early out.
6043 if (Known.isUnknown())
6044 break;
6045 } else {
6046 Known.KnownFPClasses = fcNone;
6047 }
6048
6049 // Do we need anymore elements from Vec?
6050 if (!DemandedVecElts.isZero()) {
6051 KnownFPClass Known2;
6052 computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2, Q,
6053 Depth + 1);
6054 Known |= Known2;
6055 }
6056
6057 break;
6058 }
6059 case Instruction::ShuffleVector: {
6060 // Handle vector splat idiom
6061 if (Value *Splat = getSplatValue(V)) {
6062 computeKnownFPClass(Splat, Known, InterestedClasses, Q, Depth + 1);
6063 break;
6064 }
6065
6066 // For undef elements, we don't know anything about the common state of
6067 // the shuffle result.
6068 APInt DemandedLHS, DemandedRHS;
6069 auto *Shuf = dyn_cast<ShuffleVectorInst>(Op);
6070 if (!Shuf || !getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
6071 return;
6072
6073 if (!!DemandedLHS) {
6074 const Value *LHS = Shuf->getOperand(0);
6075 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known, Q,
6076 Depth + 1);
6077
6078 // If we don't know any bits, early out.
6079 if (Known.isUnknown())
6080 break;
6081 } else {
6082 Known.KnownFPClasses = fcNone;
6083 }
6084
6085 if (!!DemandedRHS) {
6086 KnownFPClass Known2;
6087 const Value *RHS = Shuf->getOperand(1);
6088 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2, Q,
6089 Depth + 1);
6090 Known |= Known2;
6091 }
6092
6093 break;
6094 }
6095 case Instruction::ExtractValue: {
6096 const ExtractValueInst *Extract = cast<ExtractValueInst>(Op);
6097 ArrayRef<unsigned> Indices = Extract->getIndices();
6098 const Value *Src = Extract->getAggregateOperand();
6099 if (isa<StructType>(Src->getType()) && Indices.size() == 1 &&
6100 Indices[0] == 0) {
6101 if (const auto *II = dyn_cast<IntrinsicInst>(Src)) {
6102 switch (II->getIntrinsicID()) {
6103 case Intrinsic::frexp: {
6104 Known.knownNot(fcSubnormal);
6105
6106 KnownFPClass KnownSrc;
6107 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
6108 InterestedClasses, KnownSrc, Q, Depth + 1);
6109
6110 const Function *F = cast<Instruction>(Op)->getFunction();
6111 const fltSemantics &FltSem =
6112 Op->getType()->getScalarType()->getFltSemantics();
6113
6115 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
6116 Known = KnownFPClass::frexp_mant(KnownSrc, Mode);
6117 return;
6118 }
6119 default:
6120 break;
6121 }
6122 }
6123 }
6124
6125 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Q,
6126 Depth + 1);
6127 break;
6128 }
6129 case Instruction::PHI: {
6130 const PHINode *P = cast<PHINode>(Op);
6131 // Unreachable blocks may have zero-operand PHI nodes.
6132 if (P->getNumIncomingValues() == 0)
6133 break;
6134
6135 // Otherwise take the unions of the known bit sets of the operands,
6136 // taking conservative care to avoid excessive recursion.
6137 const unsigned PhiRecursionLimit = MaxAnalysisRecursionDepth - 2;
6138
6139 if (Depth < PhiRecursionLimit) {
6140 // Skip if every incoming value references to ourself.
6141 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
6142 break;
6143
6144 bool First = true;
6145
6146 for (const Use &U : P->operands()) {
6147 Value *IncValue;
6148 Instruction *CxtI;
6149 breakSelfRecursivePHI(&U, P, IncValue, CxtI);
6150 // Skip direct self references.
6151 if (IncValue == P)
6152 continue;
6153
6154 KnownFPClass KnownSrc;
6155 // Recurse, but cap the recursion to two levels, because we don't want
6156 // to waste time spinning around in loops. We need at least depth 2 to
6157 // detect known sign bits.
6158 computeKnownFPClass(IncValue, DemandedElts, InterestedClasses, KnownSrc,
6160 PhiRecursionLimit);
6161
6162 if (First) {
6163 Known = KnownSrc;
6164 First = false;
6165 } else {
6166 Known |= KnownSrc;
6167 }
6168
6169 if (Known.KnownFPClasses == fcAllFlags)
6170 break;
6171 }
6172 }
6173
6174 // Look for the case of a for loop which has a positive
6175 // initial value and is incremented by a squared value.
6176 // This will propagate sign information out of such loops.
6177 if (P->getNumIncomingValues() != 2 || Known.cannotBeOrderedLessThanZero())
6178 break;
6179 for (unsigned I = 0; I < 2; I++) {
6180 Value *RecurValue = P->getIncomingValue(1 - I);
6182 if (!II)
6183 continue;
6184 Value *R, *L, *Init;
6185 PHINode *PN;
6187 PN == P) {
6188 switch (II->getIntrinsicID()) {
6189 case Intrinsic::fma:
6190 case Intrinsic::fmuladd: {
6191 KnownFPClass KnownStart;
6192 computeKnownFPClass(Init, DemandedElts, InterestedClasses, KnownStart,
6193 Q, Depth + 1);
6194 if (KnownStart.cannotBeOrderedLessThanZero() && L == R &&
6195 isGuaranteedNotToBeUndef(L, Q.AC, Q.CxtI, Q.DT, Depth + 1))
6197 break;
6198 }
6199 }
6200 }
6201 }
6202 break;
6203 }
6204 case Instruction::BitCast: {
6205 const Value *Src;
6206 if (!match(Op, m_ElementWiseBitCast(m_Value(Src))) ||
6207 !Src->getType()->isIntOrIntVectorTy())
6208 break;
6209
6210 const Type *Ty = Op->getType();
6211
6212 Value *CastLHS, *CastRHS;
6213
6214 // Match bitcast(umax(bitcast(a), bitcast(b)))
6215 if (match(Src, m_c_MaxOrMin(m_BitCast(m_Value(CastLHS)),
6216 m_BitCast(m_Value(CastRHS)))) &&
6217 CastLHS->getType() == Ty && CastRHS->getType() == Ty) {
6218 KnownFPClass KnownLHS, KnownRHS;
6219 computeKnownFPClass(CastRHS, DemandedElts, InterestedClasses, KnownRHS, Q,
6220 Depth + 1);
6221 if (!KnownRHS.isUnknown()) {
6222 computeKnownFPClass(CastLHS, DemandedElts, InterestedClasses, KnownLHS,
6223 Q, Depth + 1);
6224 Known = KnownLHS | KnownRHS;
6225 }
6226
6227 return;
6228 }
6229
6230 const Type *EltTy = Ty->getScalarType();
6231 KnownBits Bits(EltTy->getPrimitiveSizeInBits());
6232 computeKnownBits(Src, DemandedElts, Bits, Q, Depth + 1);
6233
6235 break;
6236 }
6237 default:
6238 break;
6239 }
6240}
6241
6243 const APInt &DemandedElts,
6244 FPClassTest InterestedClasses,
6245 const SimplifyQuery &SQ,
6246 unsigned Depth) {
6247 KnownFPClass KnownClasses;
6248 ::computeKnownFPClass(V, DemandedElts, InterestedClasses, KnownClasses, SQ,
6249 Depth);
6250 return KnownClasses;
6251}
6252
6254 FPClassTest InterestedClasses,
6255 const SimplifyQuery &SQ,
6256 unsigned Depth) {
6258 ::computeKnownFPClass(V, Known, InterestedClasses, SQ, Depth);
6259 return Known;
6260}
6261
6263 const Value *V, const DataLayout &DL, FPClassTest InterestedClasses,
6264 const TargetLibraryInfo *TLI, AssumptionCache *AC, const Instruction *CxtI,
6265 const DominatorTree *DT, bool UseInstrInfo, unsigned Depth) {
6266 return computeKnownFPClass(V, InterestedClasses,
6267 SimplifyQuery(DL, TLI, DT, AC, CxtI, UseInstrInfo),
6268 Depth);
6269}
6270
6272llvm::computeKnownFPClass(const Value *V, const APInt &DemandedElts,
6273 FastMathFlags FMF, FPClassTest InterestedClasses,
6274 const SimplifyQuery &SQ, unsigned Depth) {
6275 if (FMF.noNaNs())
6276 InterestedClasses &= ~fcNan;
6277 if (FMF.noInfs())
6278 InterestedClasses &= ~fcInf;
6279
6280 KnownFPClass Result =
6281 computeKnownFPClass(V, DemandedElts, InterestedClasses, SQ, Depth);
6282
6283 if (FMF.noNaNs())
6284 Result.KnownFPClasses &= ~fcNan;
6285 if (FMF.noInfs())
6286 Result.KnownFPClasses &= ~fcInf;
6287 return Result;
6288}
6289
6291 FPClassTest InterestedClasses,
6292 const SimplifyQuery &SQ,
6293 unsigned Depth) {
6294 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
6295 APInt DemandedElts =
6296 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
6297 return computeKnownFPClass(V, DemandedElts, FMF, InterestedClasses, SQ,
6298 Depth);
6299}
6300
6302 unsigned Depth) {
6304 return Known.isKnownNeverNegZero();
6305}
6306
6308 unsigned Depth) {
6311 return Known.cannotBeOrderedLessThanZero();
6312}
6313
6315 unsigned Depth) {
6317 return Known.isKnownNeverInfinity();
6318}
6319
6320/// Return true if the floating-point value can never contain a NaN or infinity.
6322 unsigned Depth) {
6324 return Known.isKnownNeverNaN() && Known.isKnownNeverInfinity();
6325}
6326
6327/// Return true if the floating-point scalar value is not a NaN or if the
6328/// floating-point vector value has no NaN elements. Return false if a value
6329/// could ever be NaN.
6331 unsigned Depth) {
6333 return Known.isKnownNeverNaN();
6334}
6335
6336/// Return false if we can prove that the specified FP value's sign bit is 0.
6337/// Return true if we can prove that the specified FP value's sign bit is 1.
6338/// Otherwise return std::nullopt.
6339std::optional<bool> llvm::computeKnownFPSignBit(const Value *V,
6340 const SimplifyQuery &SQ,
6341 unsigned Depth) {
6343 return Known.SignBit;
6344}
6345
6347 auto *User = cast<Instruction>(U.getUser());
6348 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6349 if (FPOp->hasNoSignedZeros())
6350 return true;
6351 }
6352
6353 switch (User->getOpcode()) {
6354 case Instruction::FPToSI:
6355 case Instruction::FPToUI:
6356 return true;
6357 case Instruction::FCmp:
6358 // fcmp treats both positive and negative zero as equal.
6359 return true;
6360 case Instruction::Call:
6361 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6362 switch (II->getIntrinsicID()) {
6363 case Intrinsic::fabs:
6364 return true;
6365 case Intrinsic::copysign:
6366 return U.getOperandNo() == 0;
6367 case Intrinsic::is_fpclass:
6368 case Intrinsic::vp_is_fpclass: {
6369 auto Test =
6370 static_cast<FPClassTest>(
6371 cast<ConstantInt>(II->getArgOperand(1))->getZExtValue()) &
6374 }
6375 default:
6376 return false;
6377 }
6378 }
6379 return false;
6380 default:
6381 return false;
6382 }
6383}
6384
6386 auto *User = cast<Instruction>(U.getUser());
6387 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6388 if (FPOp->hasNoNaNs())
6389 return true;
6390 }
6391
6392 switch (User->getOpcode()) {
6393 case Instruction::FPToSI:
6394 case Instruction::FPToUI:
6395 return true;
6396 // Proper FP math operations ignore the sign bit of NaN.
6397 case Instruction::FAdd:
6398 case Instruction::FSub:
6399 case Instruction::FMul:
6400 case Instruction::FDiv:
6401 case Instruction::FRem:
6402 case Instruction::FPTrunc:
6403 case Instruction::FPExt:
6404 case Instruction::FCmp:
6405 return true;
6406 // Bitwise FP operations should preserve the sign bit of NaN.
6407 case Instruction::FNeg:
6408 case Instruction::Select:
6409 case Instruction::PHI:
6410 return false;
6411 case Instruction::Ret:
6412 return User->getFunction()->getAttributes().getRetNoFPClass() &
6414 case Instruction::Call:
6415 case Instruction::Invoke: {
6416 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6417 switch (II->getIntrinsicID()) {
6418 case Intrinsic::fabs:
6419 return true;
6420 case Intrinsic::copysign:
6421 return U.getOperandNo() == 0;
6422 // Other proper FP math intrinsics ignore the sign bit of NaN.
6423 case Intrinsic::maxnum:
6424 case Intrinsic::minnum:
6425 case Intrinsic::maximum:
6426 case Intrinsic::minimum:
6427 case Intrinsic::maximumnum:
6428 case Intrinsic::minimumnum:
6429 case Intrinsic::canonicalize:
6430 case Intrinsic::fma:
6431 case Intrinsic::fmuladd:
6432 case Intrinsic::sqrt:
6433 case Intrinsic::pow:
6434 case Intrinsic::powi:
6435 case Intrinsic::fptoui_sat:
6436 case Intrinsic::fptosi_sat:
6437 case Intrinsic::is_fpclass:
6438 case Intrinsic::vp_is_fpclass:
6439 return true;
6440 default:
6441 return false;
6442 }
6443 }
6444
6445 FPClassTest NoFPClass =
6446 cast<CallBase>(User)->getParamNoFPClass(U.getOperandNo());
6447 return NoFPClass & FPClassTest::fcNan;
6448 }
6449 default:
6450 return false;
6451 }
6452}
6453
6455 FastMathFlags FMF) {
6456 if (isa<PoisonValue>(V))
6457 return true;
6458 if (isa<UndefValue>(V))
6459 return false;
6460
6461 if (match(V, m_CheckedFp([](const APFloat &Val) { return Val.isInteger(); })))
6462 return true;
6463
6465 if (!I)
6466 return false;
6467
6468 switch (I->getOpcode()) {
6469 case Instruction::SIToFP:
6470 case Instruction::UIToFP:
6471 // TODO: Could check nofpclass(inf) on incoming argument
6472 if (FMF.noInfs())
6473 return true;
6474
6475 // Need to check int size cannot produce infinity, which computeKnownFPClass
6476 // knows how to do already.
6477 return isKnownNeverInfinity(I, SQ);
6478 case Instruction::Call: {
6479 const CallInst *CI = cast<CallInst>(I);
6480 switch (CI->getIntrinsicID()) {
6481 case Intrinsic::trunc:
6482 case Intrinsic::floor:
6483 case Intrinsic::ceil:
6484 case Intrinsic::rint:
6485 case Intrinsic::nearbyint:
6486 case Intrinsic::round:
6487 case Intrinsic::roundeven:
6488 return (FMF.noInfs() && FMF.noNaNs()) || isKnownNeverInfOrNaN(I, SQ);
6489 default:
6490 break;
6491 }
6492
6493 break;
6494 }
6495 default:
6496 break;
6497 }
6498
6499 return false;
6500}
6501
6503
6504 // All byte-wide stores are splatable, even of arbitrary variables.
6505 if (V->getType()->isIntegerTy(8))
6506 return V;
6507
6508 LLVMContext &Ctx = V->getContext();
6509
6510 // Undef don't care.
6511 auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
6512 if (isa<UndefValue>(V))
6513 return UndefInt8;
6514
6515 // Return poison for zero-sized type.
6516 if (DL.getTypeStoreSize(V->getType()).isZero())
6517 return PoisonValue::get(Type::getInt8Ty(Ctx));
6518
6520 if (!C) {
6521 // Conceptually, we could handle things like:
6522 // %a = zext i8 %X to i16
6523 // %b = shl i16 %a, 8
6524 // %c = or i16 %a, %b
6525 // but until there is an example that actually needs this, it doesn't seem
6526 // worth worrying about.
6527 return nullptr;
6528 }
6529
6530 // Handle 'null' ConstantArrayZero etc.
6531 if (C->isNullValue())
6533
6534 // Constant floating-point values can be handled as integer values if the
6535 // corresponding integer value is "byteable". An important case is 0.0.
6536 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
6537 Type *ScalarTy = CFP->getType()->getScalarType();
6538 if (ScalarTy->isHalfTy() || ScalarTy->isFloatTy() || ScalarTy->isDoubleTy())
6539 return isBytewiseValue(
6540 ConstantInt::get(Ctx, CFP->getValue().bitcastToAPInt()), DL);
6541
6542 // Don't handle long double formats, which have strange constraints.
6543 return nullptr;
6544 }
6545
6546 // We can handle constant integers that are multiple of 8 bits.
6547 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
6548 if (CI->getBitWidth() % 8 == 0) {
6549 if (!CI->getValue().isSplat(8))
6550 return nullptr;
6551 return ConstantInt::get(Ctx, CI->getValue().trunc(8));
6552 }
6553 }
6554
6555 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
6556 if (CE->getOpcode() == Instruction::IntToPtr) {
6557 if (auto *PtrTy = dyn_cast<PointerType>(CE->getType())) {
6558 unsigned BitWidth = DL.getPointerSizeInBits(PtrTy->getAddressSpace());
6560 CE->getOperand(0), Type::getIntNTy(Ctx, BitWidth), false, DL))
6561 return isBytewiseValue(Op, DL);
6562 }
6563 }
6564 }
6565
6566 auto Merge = [&](Value *LHS, Value *RHS) -> Value * {
6567 if (LHS == RHS)
6568 return LHS;
6569 if (!LHS || !RHS)
6570 return nullptr;
6571 if (LHS == UndefInt8)
6572 return RHS;
6573 if (RHS == UndefInt8)
6574 return LHS;
6575 return nullptr;
6576 };
6577
6579 Value *Val = UndefInt8;
6580 for (uint64_t I = 0, E = CA->getNumElements(); I != E; ++I)
6581 if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))
6582 return nullptr;
6583 return Val;
6584 }
6585
6587 Value *Val = UndefInt8;
6588 for (Value *Op : C->operands())
6589 if (!(Val = Merge(Val, isBytewiseValue(Op, DL))))
6590 return nullptr;
6591 return Val;
6592 }
6593
6594 // Don't try to handle the handful of other constants.
6595 return nullptr;
6596}
6597
6598// This is the recursive version of BuildSubAggregate. It takes a few different
6599// arguments. Idxs is the index within the nested struct From that we are
6600// looking at now (which is of type IndexedType). IdxSkip is the number of
6601// indices from Idxs that should be left out when inserting into the resulting
6602// struct. To is the result struct built so far, new insertvalue instructions
6603// build on that.
6604static Value *BuildSubAggregate(Value *From, Value *To, Type *IndexedType,
6606 unsigned IdxSkip,
6607 BasicBlock::iterator InsertBefore) {
6608 StructType *STy = dyn_cast<StructType>(IndexedType);
6609 if (STy) {
6610 // Save the original To argument so we can modify it
6611 Value *OrigTo = To;
6612 // General case, the type indexed by Idxs is a struct
6613 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
6614 // Process each struct element recursively
6615 Idxs.push_back(i);
6616 Value *PrevTo = To;
6617 To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
6618 InsertBefore);
6619 Idxs.pop_back();
6620 if (!To) {
6621 // Couldn't find any inserted value for this index? Cleanup
6622 while (PrevTo != OrigTo) {
6624 PrevTo = Del->getAggregateOperand();
6625 Del->eraseFromParent();
6626 }
6627 // Stop processing elements
6628 break;
6629 }
6630 }
6631 // If we successfully found a value for each of our subaggregates
6632 if (To)
6633 return To;
6634 }
6635 // Base case, the type indexed by SourceIdxs is not a struct, or not all of
6636 // the struct's elements had a value that was inserted directly. In the latter
6637 // case, perhaps we can't determine each of the subelements individually, but
6638 // we might be able to find the complete struct somewhere.
6639
6640 // Find the value that is at that particular spot
6641 Value *V = FindInsertedValue(From, Idxs);
6642
6643 if (!V)
6644 return nullptr;
6645
6646 // Insert the value in the new (sub) aggregate
6647 return InsertValueInst::Create(To, V, ArrayRef(Idxs).slice(IdxSkip), "tmp",
6648 InsertBefore);
6649}
6650
6651// This helper takes a nested struct and extracts a part of it (which is again a
6652// struct) into a new value. For example, given the struct:
6653// { a, { b, { c, d }, e } }
6654// and the indices "1, 1" this returns
6655// { c, d }.
6656//
6657// It does this by inserting an insertvalue for each element in the resulting
6658// struct, as opposed to just inserting a single struct. This will only work if
6659// each of the elements of the substruct are known (ie, inserted into From by an
6660// insertvalue instruction somewhere).
6661//
6662// All inserted insertvalue instructions are inserted before InsertBefore
6664 BasicBlock::iterator InsertBefore) {
6665 Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
6666 idx_range);
6667 Value *To = PoisonValue::get(IndexedType);
6668 SmallVector<unsigned, 10> Idxs(idx_range);
6669 unsigned IdxSkip = Idxs.size();
6670
6671 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
6672}
6673
6674/// Given an aggregate and a sequence of indices, see if the scalar value
6675/// indexed is already around as a register, for example if it was inserted
6676/// directly into the aggregate.
6677///
6678/// If InsertBefore is not null, this function will duplicate (modified)
6679/// insertvalues when a part of a nested struct is extracted.
6680Value *
6682 std::optional<BasicBlock::iterator> InsertBefore) {
6683 // Nothing to index? Just return V then (this is useful at the end of our
6684 // recursion).
6685 if (idx_range.empty())
6686 return V;
6687 // We have indices, so V should have an indexable type.
6688 assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
6689 "Not looking at a struct or array?");
6690 assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
6691 "Invalid indices for type?");
6692
6693 if (Constant *C = dyn_cast<Constant>(V)) {
6694 C = C->getAggregateElement(idx_range[0]);
6695 if (!C) return nullptr;
6696 return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
6697 }
6698
6700 // Loop the indices for the insertvalue instruction in parallel with the
6701 // requested indices
6702 const unsigned *req_idx = idx_range.begin();
6703 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
6704 i != e; ++i, ++req_idx) {
6705 if (req_idx == idx_range.end()) {
6706 // We can't handle this without inserting insertvalues
6707 if (!InsertBefore)
6708 return nullptr;
6709
6710 // The requested index identifies a part of a nested aggregate. Handle
6711 // this specially. For example,
6712 // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
6713 // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
6714 // %C = extractvalue {i32, { i32, i32 } } %B, 1
6715 // This can be changed into
6716 // %A = insertvalue {i32, i32 } undef, i32 10, 0
6717 // %C = insertvalue {i32, i32 } %A, i32 11, 1
6718 // which allows the unused 0,0 element from the nested struct to be
6719 // removed.
6720 return BuildSubAggregate(V, ArrayRef(idx_range.begin(), req_idx),
6721 *InsertBefore);
6722 }
6723
6724 // This insert value inserts something else than what we are looking for.
6725 // See if the (aggregate) value inserted into has the value we are
6726 // looking for, then.
6727 if (*req_idx != *i)
6728 return FindInsertedValue(I->getAggregateOperand(), idx_range,
6729 InsertBefore);
6730 }
6731 // If we end up here, the indices of the insertvalue match with those
6732 // requested (though possibly only partially). Now we recursively look at
6733 // the inserted value, passing any remaining indices.
6734 return FindInsertedValue(I->getInsertedValueOperand(),
6735 ArrayRef(req_idx, idx_range.end()), InsertBefore);
6736 }
6737
6739 // If we're extracting a value from an aggregate that was extracted from
6740 // something else, we can extract from that something else directly instead.
6741 // However, we will need to chain I's indices with the requested indices.
6742
6743 // Calculate the number of indices required
6744 unsigned size = I->getNumIndices() + idx_range.size();
6745 // Allocate some space to put the new indices in
6747 Idxs.reserve(size);
6748 // Add indices from the extract value instruction
6749 Idxs.append(I->idx_begin(), I->idx_end());
6750
6751 // Add requested indices
6752 Idxs.append(idx_range.begin(), idx_range.end());
6753
6754 assert(Idxs.size() == size
6755 && "Number of indices added not correct?");
6756
6757 return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
6758 }
6759 // Otherwise, we don't know (such as, extracting from a function return value
6760 // or load instruction)
6761 return nullptr;
6762}
6763
6764// If V refers to an initialized global constant, set Slice either to
6765// its initializer if the size of its elements equals ElementSize, or,
6766// for ElementSize == 8, to its representation as an array of unsiged
6767// char. Return true on success.
6768// Offset is in the unit "nr of ElementSize sized elements".
6771 unsigned ElementSize, uint64_t Offset) {
6772 assert(V && "V should not be null.");
6773 assert((ElementSize % 8) == 0 &&
6774 "ElementSize expected to be a multiple of the size of a byte.");
6775 unsigned ElementSizeInBytes = ElementSize / 8;
6776
6777 // Drill down into the pointer expression V, ignoring any intervening
6778 // casts, and determine the identity of the object it references along
6779 // with the cumulative byte offset into it.
6780 const GlobalVariable *GV =
6782 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
6783 // Fail if V is not based on constant global object.
6784 return false;
6785
6786 const DataLayout &DL = GV->getDataLayout();
6787 APInt Off(DL.getIndexTypeSizeInBits(V->getType()), 0);
6788
6789 if (GV != V->stripAndAccumulateConstantOffsets(DL, Off,
6790 /*AllowNonInbounds*/ true))
6791 // Fail if a constant offset could not be determined.
6792 return false;
6793
6794 uint64_t StartIdx = Off.getLimitedValue();
6795 if (StartIdx == UINT64_MAX)
6796 // Fail if the constant offset is excessive.
6797 return false;
6798
6799 // Off/StartIdx is in the unit of bytes. So we need to convert to number of
6800 // elements. Simply bail out if that isn't possible.
6801 if ((StartIdx % ElementSizeInBytes) != 0)
6802 return false;
6803
6804 Offset += StartIdx / ElementSizeInBytes;
6805 ConstantDataArray *Array = nullptr;
6806 ArrayType *ArrayTy = nullptr;
6807
6808 if (GV->getInitializer()->isNullValue()) {
6809 Type *GVTy = GV->getValueType();
6810 uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy).getFixedValue();
6811 uint64_t Length = SizeInBytes / ElementSizeInBytes;
6812
6813 Slice.Array = nullptr;
6814 Slice.Offset = 0;
6815 // Return an empty Slice for undersized constants to let callers
6816 // transform even undefined library calls into simpler, well-defined
6817 // expressions. This is preferable to making the calls although it
6818 // prevents sanitizers from detecting such calls.
6819 Slice.Length = Length < Offset ? 0 : Length - Offset;
6820 return true;
6821 }
6822
6823 auto *Init = const_cast<Constant *>(GV->getInitializer());
6824 if (auto *ArrayInit = dyn_cast<ConstantDataArray>(Init)) {
6825 Type *InitElTy = ArrayInit->getElementType();
6826 if (InitElTy->isIntegerTy(ElementSize)) {
6827 // If Init is an initializer for an array of the expected type
6828 // and size, use it as is.
6829 Array = ArrayInit;
6830 ArrayTy = ArrayInit->getType();
6831 }
6832 }
6833
6834 if (!Array) {
6835 if (ElementSize != 8)
6836 // TODO: Handle conversions to larger integral types.
6837 return false;
6838
6839 // Otherwise extract the portion of the initializer starting
6840 // at Offset as an array of bytes, and reset Offset.
6842 if (!Init)
6843 return false;
6844
6845 Offset = 0;
6847 ArrayTy = dyn_cast<ArrayType>(Init->getType());
6848 }
6849
6850 uint64_t NumElts = ArrayTy->getArrayNumElements();
6851 if (Offset > NumElts)
6852 return false;
6853
6854 Slice.Array = Array;
6855 Slice.Offset = Offset;
6856 Slice.Length = NumElts - Offset;
6857 return true;
6858}
6859
6860/// Extract bytes from the initializer of the constant array V, which need
6861/// not be a nul-terminated string. On success, store the bytes in Str and
6862/// return true. When TrimAtNul is set, Str will contain only the bytes up
6863/// to but not including the first nul. Return false on failure.
6865 bool TrimAtNul) {
6867 if (!getConstantDataArrayInfo(V, Slice, 8))
6868 return false;
6869
6870 if (Slice.Array == nullptr) {
6871 if (TrimAtNul) {
6872 // Return a nul-terminated string even for an empty Slice. This is
6873 // safe because all existing SimplifyLibcalls callers require string
6874 // arguments and the behavior of the functions they fold is undefined
6875 // otherwise. Folding the calls this way is preferable to making
6876 // the undefined library calls, even though it prevents sanitizers
6877 // from reporting such calls.
6878 Str = StringRef();
6879 return true;
6880 }
6881 if (Slice.Length == 1) {
6882 Str = StringRef("", 1);
6883 return true;
6884 }
6885 // We cannot instantiate a StringRef as we do not have an appropriate string
6886 // of 0s at hand.
6887 return false;
6888 }
6889
6890 // Start out with the entire array in the StringRef.
6891 Str = Slice.Array->getAsString();
6892 // Skip over 'offset' bytes.
6893 Str = Str.substr(Slice.Offset);
6894
6895 if (TrimAtNul) {
6896 // Trim off the \0 and anything after it. If the array is not nul
6897 // terminated, we just return the whole end of string. The client may know
6898 // some other way that the string is length-bound.
6899 Str = Str.substr(0, Str.find('\0'));
6900 }
6901 return true;
6902}
6903
6904// These next two are very similar to the above, but also look through PHI
6905// nodes.
6906// TODO: See if we can integrate these two together.
6907
6908/// If we can compute the length of the string pointed to by
6909/// the specified pointer, return 'len+1'. If we can't, return 0.
6912 unsigned CharSize) {
6913 // Look through noop bitcast instructions.
6914 V = V->stripPointerCasts();
6915
6916 // If this is a PHI node, there are two cases: either we have already seen it
6917 // or we haven't.
6918 if (const PHINode *PN = dyn_cast<PHINode>(V)) {
6919 if (!PHIs.insert(PN).second)
6920 return ~0ULL; // already in the set.
6921
6922 // If it was new, see if all the input strings are the same length.
6923 uint64_t LenSoFar = ~0ULL;
6924 for (Value *IncValue : PN->incoming_values()) {
6925 uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize);
6926 if (Len == 0) return 0; // Unknown length -> unknown.
6927
6928 if (Len == ~0ULL) continue;
6929
6930 if (Len != LenSoFar && LenSoFar != ~0ULL)
6931 return 0; // Disagree -> unknown.
6932 LenSoFar = Len;
6933 }
6934
6935 // Success, all agree.
6936 return LenSoFar;
6937 }
6938
6939 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
6940 if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
6941 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);
6942 if (Len1 == 0) return 0;
6943 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);
6944 if (Len2 == 0) return 0;
6945 if (Len1 == ~0ULL) return Len2;
6946 if (Len2 == ~0ULL) return Len1;
6947 if (Len1 != Len2) return 0;
6948 return Len1;
6949 }
6950
6951 // Otherwise, see if we can read the string.
6953 if (!getConstantDataArrayInfo(V, Slice, CharSize))
6954 return 0;
6955
6956 if (Slice.Array == nullptr)
6957 // Zeroinitializer (including an empty one).
6958 return 1;
6959
6960 // Search for the first nul character. Return a conservative result even
6961 // when there is no nul. This is safe since otherwise the string function
6962 // being folded such as strlen is undefined, and can be preferable to
6963 // making the undefined library call.
6964 unsigned NullIndex = 0;
6965 for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) {
6966 if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0)
6967 break;
6968 }
6969
6970 return NullIndex + 1;
6971}
6972
6973/// If we can compute the length of the string pointed to by
6974/// the specified pointer, return 'len+1'. If we can't, return 0.
6975uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) {
6976 if (!V->getType()->isPointerTy())
6977 return 0;
6978
6980 uint64_t Len = GetStringLengthH(V, PHIs, CharSize);
6981 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
6982 // an empty string as a length.
6983 return Len == ~0ULL ? 1 : Len;
6984}
6985
6986const Value *
6988 bool MustPreserveOffset) {
6989 assert(Call &&
6990 "getArgumentAliasingToReturnedPointer only works on nonnull calls");
6991 if (const Value *RV = Call->getReturnedArgOperand())
6992 return RV;
6993 // This can be used only as a aliasing property.
6995 Call, MustPreserveOffset))
6996 return Call->getArgOperand(0);
6997 return nullptr;
6998}
6999
7001 const CallBase *Call, bool MustPreserveOffset) {
7002 switch (Call->getIntrinsicID()) {
7003 case Intrinsic::launder_invariant_group:
7004 case Intrinsic::strip_invariant_group:
7005 case Intrinsic::aarch64_irg:
7006 case Intrinsic::aarch64_tagp:
7007 // The amdgcn_make_buffer_rsrc function does not alter the address of the
7008 // input pointer (and thus preserves the byte offset, which is the property
7009 // the MustPreserveOffset flag selects). However, it will not necessarily
7010 // map ptr addrspace(N) null to ptr addrspace(8) null, aka the "null
7011 // descriptor", which has "all loads return 0, all stores are dropped"
7012 // semantics. Given the context of this intrinsic list, no one should be
7013 // relying on such a strict bit-exact null mapping (and, at time of
7014 // writing, they are not), but we document this fact out of an abundance
7015 // of caution.
7016 case Intrinsic::amdgcn_make_buffer_rsrc:
7017 return true;
7018 case Intrinsic::ptrmask:
7019 return !MustPreserveOffset;
7020 case Intrinsic::threadlocal_address:
7021 // The underlying variable changes with thread ID. The Thread ID may change
7022 // at coroutine suspend points.
7023 return !Call->getParent()->getParent()->isPresplitCoroutine();
7024 default:
7025 return false;
7026 }
7027}
7028
7029/// \p PN defines a loop-variant pointer to an object. Check if the
7030/// previous iteration of the loop was referring to the same object as \p PN.
7032 const LoopInfo *LI) {
7033 // Find the loop-defined value.
7034 Loop *L = LI->getLoopFor(PN->getParent());
7035 if (PN->getNumIncomingValues() != 2)
7036 return true;
7037
7038 // Find the value from previous iteration.
7039 auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
7040 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
7041 PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
7042 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
7043 return true;
7044
7045 // If a new pointer is loaded in the loop, the pointer references a different
7046 // object in every iteration. E.g.:
7047 // for (i)
7048 // int *p = a[i];
7049 // ...
7050 if (auto *Load = dyn_cast<LoadInst>(PrevValue))
7051 if (!L->isLoopInvariant(Load->getPointerOperand()))
7052 return false;
7053 return true;
7054}
7055
7056const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup) {
7057 for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
7058 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
7059 const Value *PtrOp = GEP->getPointerOperand();
7060 if (!PtrOp->getType()->isPointerTy()) // Only handle scalar pointer base.
7061 return V;
7062 V = PtrOp;
7063 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
7064 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
7065 Value *NewV = cast<Operator>(V)->getOperand(0);
7066 if (!NewV->getType()->isPointerTy())
7067 return V;
7068 V = NewV;
7069 } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
7070 if (GA->isInterposable())
7071 return V;
7072 V = GA->getAliasee();
7073 } else {
7074 if (auto *PHI = dyn_cast<PHINode>(V)) {
7075 // Look through single-arg phi nodes created by LCSSA.
7076 if (PHI->getNumIncomingValues() == 1) {
7077 V = PHI->getIncomingValue(0);
7078 continue;
7079 }
7080 } else if (auto *Call = dyn_cast<CallBase>(V)) {
7081 // CaptureTracking can know about special capturing properties of some
7082 // intrinsics like launder.invariant.group, that can't be expressed with
7083 // the attributes, but have properties like returning aliasing pointer.
7084 // Because some analysis may assume that nocaptured pointer is not
7085 // returned from some special intrinsic (because function would have to
7086 // be marked with returns attribute), it is crucial to use this function
7087 // because it should be in sync with CaptureTracking. Not using it may
7088 // cause weird miscompilations where 2 aliasing pointers are assumed to
7089 // noalias.
7091 Call, /*MustPreserveOffset=*/false)) {
7092 V = RP;
7093 continue;
7094 }
7095 }
7096
7097 return V;
7098 }
7099 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
7100 }
7101 return V;
7102}
7103
7106 const LoopInfo *LI, unsigned MaxLookup) {
7109 Worklist.push_back(V);
7110 do {
7111 const Value *P = Worklist.pop_back_val();
7112 P = getUnderlyingObject(P, MaxLookup);
7113
7114 if (!Visited.insert(P).second)
7115 continue;
7116
7117 if (auto *SI = dyn_cast<SelectInst>(P)) {
7118 Worklist.push_back(SI->getTrueValue());
7119 Worklist.push_back(SI->getFalseValue());
7120 continue;
7121 }
7122
7123 if (auto *PN = dyn_cast<PHINode>(P)) {
7124 // If this PHI changes the underlying object in every iteration of the
7125 // loop, don't look through it. Consider:
7126 // int **A;
7127 // for (i) {
7128 // Prev = Curr; // Prev = PHI (Prev_0, Curr)
7129 // Curr = A[i];
7130 // *Prev, *Curr;
7131 //
7132 // Prev is tracking Curr one iteration behind so they refer to different
7133 // underlying objects.
7134 if (!LI || !LI->isLoopHeader(PN->getParent()) ||
7136 append_range(Worklist, PN->incoming_values());
7137 else
7138 Objects.push_back(P);
7139 continue;
7140 }
7141
7142 Objects.push_back(P);
7143 } while (!Worklist.empty());
7144}
7145
7147 const unsigned MaxVisited = 8;
7148
7151 Worklist.push_back(V);
7152 const Value *Object = nullptr;
7153 // Used as fallback if we can't find a common underlying object through
7154 // recursion.
7155 bool First = true;
7156 const Value *FirstObject = getUnderlyingObject(V);
7157 do {
7158 const Value *P = Worklist.pop_back_val();
7159 P = First ? FirstObject : getUnderlyingObject(P);
7160 First = false;
7161
7162 if (!Visited.insert(P).second)
7163 continue;
7164
7165 if (Visited.size() == MaxVisited)
7166 return FirstObject;
7167
7168 if (auto *SI = dyn_cast<SelectInst>(P)) {
7169 Worklist.push_back(SI->getTrueValue());
7170 Worklist.push_back(SI->getFalseValue());
7171 continue;
7172 }
7173
7174 if (auto *PN = dyn_cast<PHINode>(P)) {
7175 append_range(Worklist, PN->incoming_values());
7176 continue;
7177 }
7178
7179 if (!Object)
7180 Object = P;
7181 else if (Object != P)
7182 return FirstObject;
7183 } while (!Worklist.empty());
7184
7185 return Object ? Object : FirstObject;
7186}
7187
7188/// This is the function that does the work of looking through basic
7189/// ptrtoint+arithmetic+inttoptr sequences.
7190static const Value *getUnderlyingObjectFromInt(const Value *V) {
7191 do {
7192 if (const Operator *U = dyn_cast<Operator>(V)) {
7193 // If we find a ptrtoint, we can transfer control back to the
7194 // regular getUnderlyingObjectFromInt.
7195 if (U->getOpcode() == Instruction::PtrToInt)
7196 return U->getOperand(0);
7197 // If we find an add of a constant, a multiplied value, or a phi, it's
7198 // likely that the other operand will lead us to the base
7199 // object. We don't have to worry about the case where the
7200 // object address is somehow being computed by the multiply,
7201 // because our callers only care when the result is an
7202 // identifiable object.
7203 if (U->getOpcode() != Instruction::Add ||
7204 (!isa<ConstantInt>(U->getOperand(1)) &&
7205 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
7206 !isa<PHINode>(U->getOperand(1))))
7207 return V;
7208 V = U->getOperand(0);
7209 } else {
7210 return V;
7211 }
7212 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
7213 } while (true);
7214}
7215
7216/// This is a wrapper around getUnderlyingObjects and adds support for basic
7217/// ptrtoint+arithmetic+inttoptr sequences.
7218/// It returns false if unidentified object is found in getUnderlyingObjects.
7220 SmallVectorImpl<Value *> &Objects) {
7222 SmallVector<const Value *, 4> Working(1, V);
7223 do {
7224 V = Working.pop_back_val();
7225
7227 getUnderlyingObjects(V, Objs);
7228
7229 for (const Value *V : Objs) {
7230 if (!Visited.insert(V).second)
7231 continue;
7232 if (Operator::getOpcode(V) == Instruction::IntToPtr) {
7233 const Value *O =
7234 getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
7235 if (O->getType()->isPointerTy()) {
7236 Working.push_back(O);
7237 continue;
7238 }
7239 }
7240 // If getUnderlyingObjects fails to find an identifiable object,
7241 // getUnderlyingObjectsForCodeGen also fails for safety.
7242 if (!isIdentifiedObject(V)) {
7243 Objects.clear();
7244 return false;
7245 }
7246 Objects.push_back(const_cast<Value *>(V));
7247 }
7248 } while (!Working.empty());
7249 return true;
7250}
7251
7253 AllocaInst *Result = nullptr;
7255 SmallVector<Value *, 4> Worklist;
7256
7257 auto AddWork = [&](Value *V) {
7258 if (Visited.insert(V).second)
7259 Worklist.push_back(V);
7260 };
7261
7262 AddWork(V);
7263 do {
7264 V = Worklist.pop_back_val();
7265 assert(Visited.count(V));
7266
7267 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
7268 if (Result && Result != AI)
7269 return nullptr;
7270 Result = AI;
7271 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
7272 AddWork(CI->getOperand(0));
7273 } else if (PHINode *PN = dyn_cast<PHINode>(V)) {
7274 for (Value *IncValue : PN->incoming_values())
7275 AddWork(IncValue);
7276 } else if (auto *SI = dyn_cast<SelectInst>(V)) {
7277 AddWork(SI->getTrueValue());
7278 AddWork(SI->getFalseValue());
7280 if (OffsetZero && !GEP->hasAllZeroIndices())
7281 return nullptr;
7282 AddWork(GEP->getPointerOperand());
7283 } else if (CallBase *CB = dyn_cast<CallBase>(V)) {
7284 Value *Returned = CB->getReturnedArgOperand();
7285 if (Returned)
7286 AddWork(Returned);
7287 else
7288 return nullptr;
7289 } else {
7290 return nullptr;
7291 }
7292 } while (!Worklist.empty());
7293
7294 return Result;
7295}
7296
7298 const Value *V, bool AllowLifetime, bool AllowDroppable) {
7299 for (const User *U : V->users()) {
7301 if (!II)
7302 return false;
7303
7304 if (AllowLifetime && II->isLifetimeStartOrEnd())
7305 continue;
7306
7307 if (AllowDroppable && II->isDroppable())
7308 continue;
7309
7310 return false;
7311 }
7312 return true;
7313}
7314
7317 V, /* AllowLifetime */ true, /* AllowDroppable */ false);
7318}
7321 V, /* AllowLifetime */ true, /* AllowDroppable */ true);
7322}
7323
7325 if (auto *II = dyn_cast<IntrinsicInst>(I))
7326 return isTriviallyVectorizable(II->getIntrinsicID());
7327 auto *Shuffle = dyn_cast<ShuffleVectorInst>(I);
7328 return (!Shuffle || Shuffle->isSelect()) &&
7330}
7331
7333 const Instruction *Inst, const Instruction *CtxI, AssumptionCache *AC,
7334 const DominatorTree *DT, const TargetLibraryInfo *TLI, bool UseVariableInfo,
7335 bool IgnoreUBImplyingAttrs) {
7336 return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI,
7337 AC, DT, TLI, UseVariableInfo,
7338 IgnoreUBImplyingAttrs);
7339}
7340
7342 unsigned Opcode, const Instruction *Inst, const Instruction *CtxI,
7343 AssumptionCache *AC, const DominatorTree *DT, const TargetLibraryInfo *TLI,
7344 bool UseVariableInfo, bool IgnoreUBImplyingAttrs) {
7345#ifndef NDEBUG
7346 if (Inst->getOpcode() != Opcode) {
7347 // Check that the operands are actually compatible with the Opcode override.
7348 auto hasEqualReturnAndLeadingOperandTypes =
7349 [](const Instruction *Inst, unsigned NumLeadingOperands) {
7350 if (Inst->getNumOperands() < NumLeadingOperands)
7351 return false;
7352 const Type *ExpectedType = Inst->getType();
7353 for (unsigned ItOp = 0; ItOp < NumLeadingOperands; ++ItOp)
7354 if (Inst->getOperand(ItOp)->getType() != ExpectedType)
7355 return false;
7356 return true;
7357 };
7359 hasEqualReturnAndLeadingOperandTypes(Inst, 2));
7360 assert(!Instruction::isUnaryOp(Opcode) ||
7361 hasEqualReturnAndLeadingOperandTypes(Inst, 1));
7362 }
7363#endif
7364
7365 switch (Opcode) {
7366 default:
7367 return true;
7368 case Instruction::UDiv:
7369 case Instruction::URem: {
7370 // x / y is undefined if y == 0.
7371 const APInt *V;
7372 if (match(Inst->getOperand(1), m_APInt(V)))
7373 return *V != 0;
7374 return false;
7375 }
7376 case Instruction::SDiv:
7377 case Instruction::SRem: {
7378 // x / y is undefined if y == 0 or x == INT_MIN and y == -1
7379 const APInt *Numerator, *Denominator;
7380 if (!match(Inst->getOperand(1), m_APInt(Denominator)))
7381 return false;
7382 // We cannot hoist this division if the denominator is 0.
7383 if (*Denominator == 0)
7384 return false;
7385 // It's safe to hoist if the denominator is not 0 or -1.
7386 if (!Denominator->isAllOnes())
7387 return true;
7388 // At this point we know that the denominator is -1. It is safe to hoist as
7389 // long we know that the numerator is not INT_MIN.
7390 if (match(Inst->getOperand(0), m_APInt(Numerator)))
7391 return !Numerator->isMinSignedValue();
7392 // The numerator *might* be MinSignedValue.
7393 return false;
7394 }
7395 case Instruction::Load: {
7396 if (!UseVariableInfo)
7397 return false;
7398
7399 const LoadInst *LI = dyn_cast<LoadInst>(Inst);
7400 if (!LI)
7401 return false;
7402 if (mustSuppressSpeculation(*LI))
7403 return false;
7404 const DataLayout &DL = LI->getDataLayout();
7406 LI->getPointerOperand(), LI->getType(), LI->getAlign(),
7407 SimplifyQuery(DL, TLI, DT, AC, CtxI));
7408 }
7409 case Instruction::Call: {
7410 auto *CI = dyn_cast<const CallInst>(Inst);
7411 if (!CI)
7412 return false;
7413 const Function *Callee = CI->getCalledFunction();
7414
7415 // The called function could have undefined behavior or side-effects, even
7416 // if marked readnone nounwind.
7417 if (!Callee || !Callee->isSpeculatable())
7418 return false;
7419 // Since the operands may be changed after hoisting, undefined behavior may
7420 // be triggered by some UB-implying attributes.
7421 return IgnoreUBImplyingAttrs || !CI->hasUBImplyingAttrs();
7422 }
7423 case Instruction::VAArg:
7424 case Instruction::Alloca:
7425 case Instruction::Invoke:
7426 case Instruction::CallBr:
7427 case Instruction::PHI:
7428 case Instruction::Store:
7429 case Instruction::Ret:
7430 case Instruction::UncondBr:
7431 case Instruction::CondBr:
7432 case Instruction::IndirectBr:
7433 case Instruction::Switch:
7434 case Instruction::Unreachable:
7435 case Instruction::Fence:
7436 case Instruction::AtomicRMW:
7437 case Instruction::AtomicCmpXchg:
7438 case Instruction::LandingPad:
7439 case Instruction::Resume:
7440 case Instruction::CatchSwitch:
7441 case Instruction::CatchPad:
7442 case Instruction::CatchRet:
7443 case Instruction::CleanupPad:
7444 case Instruction::CleanupRet:
7445 return false; // Misc instructions which have effects
7446 }
7447}
7448
7450 if (I.mayReadOrWriteMemory())
7451 // Memory dependency possible
7452 return true;
7454 // Can't move above a maythrow call or infinite loop. Or if an
7455 // inalloca alloca, above a stacksave call.
7456 return true;
7458 // 1) Can't reorder two inf-loop calls, even if readonly
7459 // 2) Also can't reorder an inf-loop call below a instruction which isn't
7460 // safe to speculative execute. (Inverse of above)
7461 return true;
7462 return false;
7463}
7464
7465/// Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
7479
7480/// Combine constant ranges from computeConstantRange() and computeKnownBits().
7483 bool ForSigned,
7484 const SimplifyQuery &SQ) {
7485 ConstantRange CR1 =
7486 ConstantRange::fromKnownBits(V.getKnownBits(SQ), ForSigned);
7487 ConstantRange CR2 = computeConstantRange(V, ForSigned, SQ);
7490 return CR1.intersectWith(CR2, RangeType);
7491}
7492
7494 const Value *RHS,
7495 const SimplifyQuery &SQ,
7496 bool IsNSW) {
7497 ConstantRange LHSRange =
7498 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7499 ConstantRange RHSRange =
7500 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7501
7502 // mul nsw of two non-negative numbers is also nuw.
7503 if (IsNSW && LHSRange.isAllNonNegative() && RHSRange.isAllNonNegative())
7505
7506 return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange));
7507}
7508
7510 const Value *RHS,
7511 const SimplifyQuery &SQ) {
7512 // Multiplying n * m significant bits yields a result of n + m significant
7513 // bits. If the total number of significant bits does not exceed the
7514 // result bit width (minus 1), there is no overflow.
7515 // This means if we have enough leading sign bits in the operands
7516 // we can guarantee that the result does not overflow.
7517 // Ref: "Hacker's Delight" by Henry Warren
7518 unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
7519
7520 // Note that underestimating the number of sign bits gives a more
7521 // conservative answer.
7522 unsigned SignBits =
7523 ::ComputeNumSignBits(LHS, SQ) + ::ComputeNumSignBits(RHS, SQ);
7524
7525 // First handle the easy case: if we have enough sign bits there's
7526 // definitely no overflow.
7527 if (SignBits > BitWidth + 1)
7529
7530 // There are two ambiguous cases where there can be no overflow:
7531 // SignBits == BitWidth + 1 and
7532 // SignBits == BitWidth
7533 // The second case is difficult to check, therefore we only handle the
7534 // first case.
7535 if (SignBits == BitWidth + 1) {
7536 // It overflows only when both arguments are negative and the true
7537 // product is exactly the minimum negative number.
7538 // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
7539 // For simplicity we just check if at least one side is not negative.
7540 KnownBits LHSKnown = computeKnownBits(LHS, SQ);
7541 KnownBits RHSKnown = computeKnownBits(RHS, SQ);
7542 if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative())
7544 }
7546}
7547
7550 const WithCache<const Value *> &RHS,
7551 const SimplifyQuery &SQ) {
7552 ConstantRange LHSRange =
7553 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7554 ConstantRange RHSRange =
7555 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7556 return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange));
7557}
7558
7559static OverflowResult
7562 const AddOperator *Add, const SimplifyQuery &SQ) {
7563 if (Add && Add->hasNoSignedWrap()) {
7565 }
7566
7567 // If LHS and RHS each have at least two sign bits, the addition will look
7568 // like
7569 //
7570 // XX..... +
7571 // YY.....
7572 //
7573 // If the carry into the most significant position is 0, X and Y can't both
7574 // be 1 and therefore the carry out of the addition is also 0.
7575 //
7576 // If the carry into the most significant position is 1, X and Y can't both
7577 // be 0 and therefore the carry out of the addition is also 1.
7578 //
7579 // Since the carry into the most significant position is always equal to
7580 // the carry out of the addition, there is no signed overflow.
7581 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
7583
7584 ConstantRange LHSRange =
7585 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7586 ConstantRange RHSRange =
7587 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7588 OverflowResult OR =
7589 mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange));
7591 return OR;
7592
7593 // The remaining code needs Add to be available. Early returns if not so.
7594 if (!Add)
7596
7597 // If the sign of Add is the same as at least one of the operands, this add
7598 // CANNOT overflow. If this can be determined from the known bits of the
7599 // operands the above signedAddMayOverflow() check will have already done so.
7600 // The only other way to improve on the known bits is from an assumption, so
7601 // call computeKnownBitsFromContext() directly.
7602 bool LHSOrRHSKnownNonNegative =
7603 (LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative());
7604 bool LHSOrRHSKnownNegative =
7605 (LHSRange.isAllNegative() || RHSRange.isAllNegative());
7606 if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
7607 KnownBits AddKnown(LHSRange.getBitWidth());
7608 computeKnownBitsFromContext(Add, AddKnown, SQ);
7609 if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||
7610 (AddKnown.isNegative() && LHSOrRHSKnownNegative))
7612 }
7613
7615}
7616
7618 const Value *RHS,
7619 const SimplifyQuery &SQ) {
7620 // X - (X % ?)
7621 // The remainder of a value can't have greater magnitude than itself,
7622 // so the subtraction can't overflow.
7623
7624 // X - (X -nuw ?)
7625 // In the minimal case, this would simplify to "?", so there's no subtract
7626 // at all. But if this analysis is used to peek through casts, for example,
7627 // then determining no-overflow may allow other transforms.
7628
7629 // TODO: There are other patterns like this.
7630 // See simplifyICmpWithBinOpOnLHS() for candidates.
7631 if (match(RHS, m_URem(m_Specific(LHS), m_Value())) ||
7632 match(RHS, m_NUWSub(m_Specific(LHS), m_Value())))
7633 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7635
7636 if (auto C = isImpliedByDomCondition(CmpInst::ICMP_UGE, LHS, RHS, SQ.CxtI,
7637 SQ.DL)) {
7638 if (*C)
7641 }
7642
7643 ConstantRange LHSRange =
7644 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7645 ConstantRange RHSRange =
7646 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7647 return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange));
7648}
7649
7651 const Value *RHS,
7652 const SimplifyQuery &SQ) {
7653 // X - (X % ?)
7654 // The remainder of a value can't have greater magnitude than itself,
7655 // so the subtraction can't overflow.
7656
7657 // X - (X -nsw ?)
7658 // In the minimal case, this would simplify to "?", so there's no subtract
7659 // at all. But if this analysis is used to peek through casts, for example,
7660 // then determining no-overflow may allow other transforms.
7661 if (match(RHS, m_SRem(m_Specific(LHS), m_Value())) ||
7662 match(RHS, m_NSWSub(m_Specific(LHS), m_Value())))
7663 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7665
7666 // If LHS and RHS each have at least two sign bits, the subtraction
7667 // cannot overflow.
7668 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
7670
7671 ConstantRange LHSRange =
7672 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7673 ConstantRange RHSRange =
7674 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7675 return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange));
7676}
7677
7679 const DominatorTree &DT) {
7680 SmallVector<const CondBrInst *, 2> GuardingBranches;
7682
7683 for (const User *U : WO->users()) {
7684 if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) {
7685 assert(EVI->getNumIndices() == 1 && "Obvious from CI's type");
7686
7687 if (EVI->getIndices()[0] == 0)
7688 Results.push_back(EVI);
7689 else {
7690 assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type");
7691
7692 for (const auto *U : EVI->users())
7693 if (const auto *B = dyn_cast<CondBrInst>(U))
7694 GuardingBranches.push_back(B);
7695 }
7696 } else {
7697 // We are using the aggregate directly in a way we don't want to analyze
7698 // here (storing it to a global, say).
7699 return false;
7700 }
7701 }
7702
7703 auto AllUsesGuardedByBranch = [&](const CondBrInst *BI) {
7704 BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1));
7705
7706 // Check if all users of the add are provably no-wrap.
7707 for (const auto *Result : Results) {
7708 // If the extractvalue itself is not executed on overflow, the we don't
7709 // need to check each use separately, since domination is transitive.
7710 if (DT.dominates(NoWrapEdge, Result->getParent()))
7711 continue;
7712
7713 for (const auto &RU : Result->uses())
7714 if (!DT.dominates(NoWrapEdge, RU))
7715 return false;
7716 }
7717
7718 return true;
7719 };
7720
7721 return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
7722}
7723
7724/// Shifts return poison if shiftwidth is larger than the bitwidth.
7725static bool shiftAmountKnownInRange(const Value *ShiftAmount) {
7726 auto *C = dyn_cast<Constant>(ShiftAmount);
7727 if (!C)
7728 return false;
7729
7730 // Shifts return poison if shiftwidth is larger than the bitwidth.
7732 if (auto *FVTy = dyn_cast<FixedVectorType>(C->getType())) {
7733 unsigned NumElts = FVTy->getNumElements();
7734 for (unsigned i = 0; i < NumElts; ++i)
7735 ShiftAmounts.push_back(C->getAggregateElement(i));
7736 } else if (isa<ScalableVectorType>(C->getType()))
7737 return false; // Can't tell, just return false to be safe
7738 else
7739 ShiftAmounts.push_back(C);
7740
7741 bool Safe = llvm::all_of(ShiftAmounts, [](const Constant *C) {
7742 auto *CI = dyn_cast_or_null<ConstantInt>(C);
7743 return CI && CI->getValue().ult(C->getType()->getIntegerBitWidth());
7744 });
7745
7746 return Safe;
7747}
7748
7750 bool ConsiderFlagsAndMetadata) {
7751
7752 if (ConsiderFlagsAndMetadata && includesPoison(Kind) &&
7753 Op->hasPoisonGeneratingAnnotations())
7754 return true;
7755
7756 unsigned Opcode = Op->getOpcode();
7757
7758 // Check whether opcode is a poison/undef-generating operation
7759 switch (Opcode) {
7760 case Instruction::Shl:
7761 case Instruction::AShr:
7762 case Instruction::LShr:
7763 return includesPoison(Kind) && !shiftAmountKnownInRange(Op->getOperand(1));
7764 case Instruction::FPToSI:
7765 case Instruction::FPToUI:
7766 // fptosi/ui yields poison if the resulting value does not fit in the
7767 // destination type.
7768 return true;
7769 case Instruction::Call:
7770 if (auto *II = dyn_cast<IntrinsicInst>(Op)) {
7771 switch (II->getIntrinsicID()) {
7772 // NOTE: Use IntrNoCreateUndefOrPoison when possible.
7773 case Intrinsic::ctlz:
7774 case Intrinsic::cttz:
7775 case Intrinsic::abs:
7776 // We're not considering flags so it is safe to just return false.
7777 return false;
7778 case Intrinsic::sshl_sat:
7779 case Intrinsic::ushl_sat:
7780 if (!includesPoison(Kind) ||
7781 shiftAmountKnownInRange(II->getArgOperand(1)))
7782 return false;
7783 break;
7784 }
7785 }
7786 [[fallthrough]];
7787 case Instruction::CallBr:
7788 case Instruction::Invoke: {
7789 const auto *CB = cast<CallBase>(Op);
7790 return !CB->hasRetAttr(Attribute::NoUndef) &&
7791 !CB->hasFnAttr(Attribute::NoCreateUndefOrPoison);
7792 }
7793 case Instruction::InsertElement:
7794 case Instruction::ExtractElement: {
7795 // If index exceeds the length of the vector, it returns poison
7796 auto *VTy = cast<VectorType>(Op->getOperand(0)->getType());
7797 unsigned IdxOp = Op->getOpcode() == Instruction::InsertElement ? 2 : 1;
7798 auto *Idx = dyn_cast<ConstantInt>(Op->getOperand(IdxOp));
7799 if (includesPoison(Kind))
7800 return !Idx ||
7801 Idx->getValue().uge(VTy->getElementCount().getKnownMinValue());
7802 return false;
7803 }
7804 case Instruction::ShuffleVector: {
7806 ? cast<ConstantExpr>(Op)->getShuffleMask()
7807 : cast<ShuffleVectorInst>(Op)->getShuffleMask();
7808 return includesPoison(Kind) && is_contained(Mask, PoisonMaskElem);
7809 }
7810 case Instruction::FNeg:
7811 case Instruction::PHI:
7812 case Instruction::Select:
7813 case Instruction::ExtractValue:
7814 case Instruction::InsertValue:
7815 case Instruction::Freeze:
7816 case Instruction::ICmp:
7817 case Instruction::FCmp:
7818 case Instruction::GetElementPtr:
7819 return false;
7820 case Instruction::AddrSpaceCast:
7821 return true;
7822 default: {
7823 const auto *CE = dyn_cast<ConstantExpr>(Op);
7824 if (isa<CastInst>(Op) || (CE && CE->isCast()))
7825 return false;
7826 else if (Instruction::isBinaryOp(Opcode))
7827 return false;
7828 // Be conservative and return true.
7829 return true;
7830 }
7831 }
7832}
7833
7835 bool ConsiderFlagsAndMetadata) {
7836 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::UndefOrPoison,
7837 ConsiderFlagsAndMetadata);
7838}
7839
7840bool llvm::canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata) {
7841 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::PoisonOnly,
7842 ConsiderFlagsAndMetadata);
7843}
7844
7845static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V,
7846 unsigned Depth) {
7847 if (ValAssumedPoison == V)
7848 return true;
7849
7850 const unsigned MaxDepth = 2;
7851 if (Depth >= MaxDepth)
7852 return false;
7853
7854 if (const auto *I = dyn_cast<Instruction>(V)) {
7855 if (any_of(I->operands(), [=](const Use &Op) {
7856 return propagatesPoison(Op) &&
7857 directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1);
7858 }))
7859 return true;
7860
7861 // V = extractvalue V0, idx
7862 // V2 = extractvalue V0, idx2
7863 // V0's elements are all poison or not. (e.g., add_with_overflow)
7864 const WithOverflowInst *II;
7866 (match(ValAssumedPoison, m_ExtractValue(m_Specific(II))) ||
7867 llvm::is_contained(II->args(), ValAssumedPoison)))
7868 return true;
7869 }
7870 return false;
7871}
7872
7873static bool impliesPoison(const Value *ValAssumedPoison, const Value *V,
7874 unsigned Depth) {
7875 if (isGuaranteedNotToBePoison(ValAssumedPoison))
7876 return true;
7877
7878 if (directlyImpliesPoison(ValAssumedPoison, V, /* Depth */ 0))
7879 return true;
7880
7881 const unsigned MaxDepth = 2;
7882 if (Depth >= MaxDepth)
7883 return false;
7884
7885 const auto *I = dyn_cast<Instruction>(ValAssumedPoison);
7886 if (I && !canCreatePoison(cast<Operator>(I))) {
7887 return all_of(I->operands(), [=](const Value *Op) {
7888 return impliesPoison(Op, V, Depth + 1);
7889 });
7890 }
7891 return false;
7892}
7893
7894bool llvm::impliesPoison(const Value *ValAssumedPoison, const Value *V) {
7895 return ::impliesPoison(ValAssumedPoison, V, /* Depth */ 0);
7896}
7897
7898static bool programUndefinedIfUndefOrPoison(const Value *V, bool PoisonOnly);
7899
7901 const Value *V, AssumptionCache *AC, const Instruction *CtxI,
7902 const DominatorTree *DT, unsigned Depth, UndefPoisonKind Kind) {
7904 return false;
7905
7906 if (isa<MetadataAsValue>(V))
7907 return false;
7908
7909 if (const auto *A = dyn_cast<Argument>(V)) {
7910 if (A->hasAttribute(Attribute::NoUndef) ||
7911 A->hasAttribute(Attribute::Dereferenceable) ||
7912 A->hasAttribute(Attribute::DereferenceableOrNull))
7913 return true;
7914 }
7915
7916 if (auto *C = dyn_cast<Constant>(V)) {
7917 if (isa<PoisonValue>(C))
7918 return !includesPoison(Kind);
7919
7920 if (isa<UndefValue>(C))
7921 return !includesUndef(Kind);
7922
7925 return true;
7926
7927 if (C->getType()->isVectorTy()) {
7928 if (isa<ConstantExpr>(C)) {
7929 // Scalable vectors can use a ConstantExpr to build a splat.
7930 if (Constant *SplatC = C->getSplatValue())
7931 if (isa<ConstantInt>(SplatC) || isa<ConstantFP>(SplatC))
7932 return true;
7933 } else {
7934 if (includesUndef(Kind) && C->containsUndefElement())
7935 return false;
7936 if (includesPoison(Kind) && C->containsPoisonElement())
7937 return false;
7938 return !C->containsConstantExpression();
7939 }
7940 }
7941 }
7942
7943 // Strip cast operations from a pointer value.
7944 // Note that stripPointerCastsSameRepresentation can strip off getelementptr
7945 // inbounds with zero offset. To guarantee that the result isn't poison, the
7946 // stripped pointer is checked as it has to be pointing into an allocated
7947 // object or be null `null` to ensure `inbounds` getelement pointers with a
7948 // zero offset could not produce poison.
7949 // It can strip off addrspacecast that do not change bit representation as
7950 // well. We believe that such addrspacecast is equivalent to no-op.
7951 auto *StrippedV = V->stripPointerCastsSameRepresentation();
7952 if (isa<AllocaInst>(StrippedV) || isa<GlobalVariable>(StrippedV) ||
7953 isa<Function>(StrippedV) || isa<ConstantPointerNull>(StrippedV))
7954 return true;
7955
7956 auto OpCheck = [&](const Value *V) {
7957 return isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth + 1, Kind);
7958 };
7959
7960 if (auto *Opr = dyn_cast<Operator>(V)) {
7961 // If the value is a freeze instruction, then it can never
7962 // be undef or poison.
7963 if (isa<FreezeInst>(V))
7964 return true;
7965
7966 if (const auto *CB = dyn_cast<CallBase>(V)) {
7967 if (CB->hasRetAttr(Attribute::NoUndef) ||
7968 CB->hasRetAttr(Attribute::Dereferenceable) ||
7969 CB->hasRetAttr(Attribute::DereferenceableOrNull))
7970 return true;
7971 }
7972
7973 if (!::canCreateUndefOrPoison(Opr, Kind,
7974 /*ConsiderFlagsAndMetadata=*/true)) {
7975 if (const auto *PN = dyn_cast<PHINode>(V)) {
7976 unsigned Num = PN->getNumIncomingValues();
7977 bool IsWellDefined = true;
7978 for (unsigned i = 0; i < Num; ++i) {
7979 if (PN == PN->getIncomingValue(i))
7980 continue;
7981 auto *TI = PN->getIncomingBlock(i)->getTerminator();
7982 if (!isGuaranteedNotToBeUndefOrPoison(PN->getIncomingValue(i), AC, TI,
7983 DT, Depth + 1, Kind)) {
7984 IsWellDefined = false;
7985 break;
7986 }
7987 }
7988 if (IsWellDefined)
7989 return true;
7990 } else if (auto *Splat = isa<ShuffleVectorInst>(Opr) ? getSplatValue(Opr)
7991 : nullptr) {
7992 // For splats we only need to check the value being splatted.
7993 if (OpCheck(Splat))
7994 return true;
7995 } else if (all_of(Opr->operands(), OpCheck))
7996 return true;
7997 }
7998 }
7999
8000 if (auto *I = dyn_cast<LoadInst>(V))
8001 if (I->hasMetadata(LLVMContext::MD_noundef) ||
8002 I->hasMetadata(LLVMContext::MD_dereferenceable) ||
8003 I->hasMetadata(LLVMContext::MD_dereferenceable_or_null))
8004 return true;
8005
8007 return true;
8008
8009 // CxtI may be null or a cloned instruction.
8010 if (!CtxI || !CtxI->getParent() || !DT)
8011 return false;
8012
8013 auto *DNode = DT->getNode(CtxI->getParent());
8014 if (!DNode)
8015 // Unreachable block
8016 return false;
8017
8018 // If V is used as a branch condition before reaching CtxI, V cannot be
8019 // undef or poison.
8020 // br V, BB1, BB2
8021 // BB1:
8022 // CtxI ; V cannot be undef or poison here
8023 auto *Dominator = DNode->getIDom();
8024 // This check is purely for compile time reasons: we can skip the IDom walk
8025 // if what we are checking for includes undef and the value is not an integer.
8026 if (!includesUndef(Kind) || V->getType()->isIntegerTy())
8027 while (Dominator) {
8028 auto *TI = Dominator->getBlock()->getTerminatorOrNull();
8029
8030 Value *Cond = nullptr;
8031 if (auto BI = dyn_cast_or_null<CondBrInst>(TI)) {
8032 Cond = BI->getCondition();
8033 } else if (auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
8034 Cond = SI->getCondition();
8035 }
8036
8037 if (Cond) {
8038 if (Cond == V)
8039 return true;
8040 else if (!includesUndef(Kind) && isa<Operator>(Cond)) {
8041 // For poison, we can analyze further
8042 auto *Opr = cast<Operator>(Cond);
8043 if (any_of(Opr->operands(), [V](const Use &U) {
8044 return V == U && propagatesPoison(U);
8045 }))
8046 return true;
8047 }
8048 }
8049
8050 Dominator = Dominator->getIDom();
8051 }
8052
8053 if (AC && getKnowledgeValidInContext(V, {Attribute::NoUndef}, *AC, CtxI, DT))
8054 return true;
8055
8056 return false;
8057}
8058
8060 const Instruction *CtxI,
8061 const DominatorTree *DT,
8062 unsigned Depth) {
8063 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
8065}
8066
8068 const Instruction *CtxI,
8069 const DominatorTree *DT, unsigned Depth) {
8070 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
8072}
8073
8075 const Instruction *CtxI,
8076 const DominatorTree *DT, unsigned Depth) {
8077 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
8079}
8080
8081/// Return true if undefined behavior would provably be executed on the path to
8082/// OnPathTo if Root produced a posion result. Note that this doesn't say
8083/// anything about whether OnPathTo is actually executed or whether Root is
8084/// actually poison. This can be used to assess whether a new use of Root can
8085/// be added at a location which is control equivalent with OnPathTo (such as
8086/// immediately before it) without introducing UB which didn't previously
8087/// exist. Note that a false result conveys no information.
8089 Instruction *OnPathTo,
8090 DominatorTree *DT) {
8091 // Basic approach is to assume Root is poison, propagate poison forward
8092 // through all users we can easily track, and then check whether any of those
8093 // users are provable UB and must execute before out exiting block might
8094 // exit.
8095
8096 // The set of all recursive users we've visited (which are assumed to all be
8097 // poison because of said visit)
8100 Worklist.push_back(Root);
8101 while (!Worklist.empty()) {
8102 const Instruction *I = Worklist.pop_back_val();
8103
8104 // If we know this must trigger UB on a path leading our target.
8105 if (mustTriggerUB(I, KnownPoison) && DT->dominates(I, OnPathTo))
8106 return true;
8107
8108 // If we can't analyze propagation through this instruction, just skip it
8109 // and transitive users. Safe as false is a conservative result.
8110 if (I != Root && !any_of(I->operands(), [&KnownPoison](const Use &U) {
8111 return KnownPoison.contains(U) && propagatesPoison(U);
8112 }))
8113 continue;
8114
8115 if (KnownPoison.insert(I).second)
8116 for (const User *User : I->users())
8117 Worklist.push_back(cast<Instruction>(User));
8118 }
8119
8120 // Might be non-UB, or might have a path we couldn't prove must execute on
8121 // way to exiting bb.
8122 return false;
8123}
8124
8126 const SimplifyQuery &SQ) {
8127 return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1),
8128 Add, SQ);
8129}
8130
8133 const WithCache<const Value *> &RHS,
8134 const SimplifyQuery &SQ) {
8135 return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, SQ);
8136}
8137
8139 // Note: An atomic operation isn't guaranteed to return in a reasonable amount
8140 // of time because it's possible for another thread to interfere with it for an
8141 // arbitrary length of time, but programs aren't allowed to rely on that.
8142
8143 // If there is no successor, then execution can't transfer to it.
8144 if (isa<ReturnInst>(I))
8145 return false;
8147 return false;
8148
8149 // Note: Do not add new checks here; instead, change Instruction::mayThrow or
8150 // Instruction::willReturn.
8151 //
8152 // FIXME: Move this check into Instruction::willReturn.
8153 if (isa<CatchPadInst>(I)) {
8154 switch (classifyEHPersonality(I->getFunction()->getPersonalityFn())) {
8155 default:
8156 // A catchpad may invoke exception object constructors and such, which
8157 // in some languages can be arbitrary code, so be conservative by default.
8158 return false;
8160 // For CoreCLR, it just involves a type test.
8161 return true;
8162 }
8163 }
8164
8165 // An instruction that returns without throwing must transfer control flow
8166 // to a successor.
8167 return !I->mayThrow() && I->willReturn();
8168}
8169
8171 // TODO: This is slightly conservative for invoke instruction since exiting
8172 // via an exception *is* normal control for them.
8173 for (const Instruction &I : *BB)
8175 return false;
8176 return true;
8177}
8178
8185
8188 assert(ScanLimit && "scan limit must be non-zero");
8189 for (const Instruction &I : Range) {
8190 if (--ScanLimit == 0)
8191 return false;
8193 return false;
8194 }
8195 return true;
8196}
8197
8199 const Loop *L) {
8200 // The loop header is guaranteed to be executed for every iteration.
8201 //
8202 // FIXME: Relax this constraint to cover all basic blocks that are
8203 // guaranteed to be executed at every iteration.
8204 if (I->getParent() != L->getHeader()) return false;
8205
8206 for (const Instruction &LI : *L->getHeader()) {
8207 if (&LI == I) return true;
8208 if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;
8209 }
8210 llvm_unreachable("Instruction not contained in its own parent basic block.");
8211}
8212
8214 switch (IID) {
8215 // TODO: Add more intrinsics.
8216 case Intrinsic::sadd_with_overflow:
8217 case Intrinsic::ssub_with_overflow:
8218 case Intrinsic::smul_with_overflow:
8219 case Intrinsic::uadd_with_overflow:
8220 case Intrinsic::usub_with_overflow:
8221 case Intrinsic::umul_with_overflow:
8222 // If an input is a vector containing a poison element, the
8223 // two output vectors (calculated results, overflow bits)'
8224 // corresponding lanes are poison.
8225 return true;
8226 case Intrinsic::ctpop:
8227 case Intrinsic::ctlz:
8228 case Intrinsic::cttz:
8229 case Intrinsic::abs:
8230 case Intrinsic::smax:
8231 case Intrinsic::smin:
8232 case Intrinsic::umax:
8233 case Intrinsic::umin:
8234 case Intrinsic::scmp:
8235 case Intrinsic::is_fpclass:
8236 case Intrinsic::ptrmask:
8237 case Intrinsic::ucmp:
8238 case Intrinsic::bitreverse:
8239 case Intrinsic::bswap:
8240 case Intrinsic::sadd_sat:
8241 case Intrinsic::ssub_sat:
8242 case Intrinsic::sshl_sat:
8243 case Intrinsic::uadd_sat:
8244 case Intrinsic::usub_sat:
8245 case Intrinsic::ushl_sat:
8246 case Intrinsic::smul_fix:
8247 case Intrinsic::smul_fix_sat:
8248 case Intrinsic::umul_fix:
8249 case Intrinsic::umul_fix_sat:
8250 case Intrinsic::pow:
8251 case Intrinsic::powi:
8252 case Intrinsic::sin:
8253 case Intrinsic::sinh:
8254 case Intrinsic::cos:
8255 case Intrinsic::cosh:
8256 case Intrinsic::sincos:
8257 case Intrinsic::sincospi:
8258 case Intrinsic::tan:
8259 case Intrinsic::tanh:
8260 case Intrinsic::asin:
8261 case Intrinsic::acos:
8262 case Intrinsic::atan:
8263 case Intrinsic::atan2:
8264 case Intrinsic::canonicalize:
8265 case Intrinsic::sqrt:
8266 case Intrinsic::exp:
8267 case Intrinsic::exp2:
8268 case Intrinsic::exp10:
8269 case Intrinsic::log:
8270 case Intrinsic::log2:
8271 case Intrinsic::log10:
8272 case Intrinsic::modf:
8273 case Intrinsic::floor:
8274 case Intrinsic::ceil:
8275 case Intrinsic::trunc:
8276 case Intrinsic::rint:
8277 case Intrinsic::nearbyint:
8278 case Intrinsic::round:
8279 case Intrinsic::roundeven:
8280 case Intrinsic::lrint:
8281 case Intrinsic::llrint:
8282 case Intrinsic::fshl:
8283 case Intrinsic::fshr:
8284 case Intrinsic::frexp:
8285 case Intrinsic::get_active_lane_mask:
8286 return true;
8287 default:
8288 return false;
8289 }
8290}
8291
8292bool llvm::propagatesPoison(const Use &PoisonOp) {
8293 const Operator *I = cast<Operator>(PoisonOp.getUser());
8294 switch (I->getOpcode()) {
8295 case Instruction::Freeze:
8296 case Instruction::PHI:
8297 case Instruction::Invoke:
8298 return false;
8299 case Instruction::Select:
8300 return PoisonOp.getOperandNo() == 0;
8301 case Instruction::Call:
8302 if (auto *II = dyn_cast<IntrinsicInst>(I))
8303 return intrinsicPropagatesPoison(II->getIntrinsicID());
8304 return false;
8305 case Instruction::ICmp:
8306 case Instruction::FCmp:
8307 case Instruction::GetElementPtr:
8308 return true;
8309 default:
8311 return true;
8312
8313 // Be conservative and return false.
8314 return false;
8315 }
8316}
8317
8318/// Enumerates all operands of \p I that are guaranteed to not be undef or
8319/// poison. If the callback \p Handle returns true, stop processing and return
8320/// true. Otherwise, return false.
8321template <typename CallableT>
8323 const CallableT &Handle) {
8324 switch (I->getOpcode()) {
8325 case Instruction::Store:
8326 if (Handle(cast<StoreInst>(I)->getPointerOperand()))
8327 return true;
8328 break;
8329
8330 case Instruction::Load:
8331 if (Handle(cast<LoadInst>(I)->getPointerOperand()))
8332 return true;
8333 break;
8334
8335 // Since dereferenceable attribute imply noundef, atomic operations
8336 // also implicitly have noundef pointers too
8337 case Instruction::AtomicCmpXchg:
8339 return true;
8340 break;
8341
8342 case Instruction::AtomicRMW:
8343 if (Handle(cast<AtomicRMWInst>(I)->getPointerOperand()))
8344 return true;
8345 break;
8346
8347 case Instruction::Call:
8348 case Instruction::Invoke: {
8349 const CallBase *CB = cast<CallBase>(I);
8350 if (CB->isIndirectCall() && Handle(CB->getCalledOperand()))
8351 return true;
8352 for (unsigned i = 0; i < CB->arg_size(); ++i)
8353 if ((CB->paramHasAttr(i, Attribute::NoUndef) ||
8354 CB->paramHasAttr(i, Attribute::Dereferenceable) ||
8355 CB->paramHasAttr(i, Attribute::DereferenceableOrNull)) &&
8356 Handle(CB->getArgOperand(i)))
8357 return true;
8358 break;
8359 }
8360 case Instruction::Ret:
8361 if (I->getFunction()->hasRetAttribute(Attribute::NoUndef) &&
8362 Handle(I->getOperand(0)))
8363 return true;
8364 break;
8365 case Instruction::Switch:
8366 if (Handle(cast<SwitchInst>(I)->getCondition()))
8367 return true;
8368 break;
8369 case Instruction::CondBr:
8370 if (Handle(cast<CondBrInst>(I)->getCondition()))
8371 return true;
8372 break;
8373 default:
8374 break;
8375 }
8376
8377 return false;
8378}
8379
8380/// Enumerates all operands of \p I that are guaranteed to not be poison.
8381template <typename CallableT>
8383 const CallableT &Handle) {
8384 if (handleGuaranteedWellDefinedOps(I, Handle))
8385 return true;
8386 switch (I->getOpcode()) {
8387 // Divisors of these operations are allowed to be partially undef.
8388 case Instruction::UDiv:
8389 case Instruction::SDiv:
8390 case Instruction::URem:
8391 case Instruction::SRem:
8392 return Handle(I->getOperand(1));
8393 default:
8394 return false;
8395 }
8396}
8397
8399 const SmallPtrSetImpl<const Value *> &KnownPoison) {
8401 I, [&](const Value *V) { return KnownPoison.count(V); });
8402}
8403
8405 bool PoisonOnly) {
8406 // We currently only look for uses of values within the same basic
8407 // block, as that makes it easier to guarantee that the uses will be
8408 // executed given that Inst is executed.
8409 //
8410 // FIXME: Expand this to consider uses beyond the same basic block. To do
8411 // this, look out for the distinction between post-dominance and strong
8412 // post-dominance.
8413 const BasicBlock *BB = nullptr;
8415 if (const auto *Inst = dyn_cast<Instruction>(V)) {
8416 BB = Inst->getParent();
8417 Begin = Inst->getIterator();
8418 Begin++;
8419 } else if (const auto *Arg = dyn_cast<Argument>(V)) {
8420 if (Arg->getParent()->isDeclaration())
8421 return false;
8422 BB = &Arg->getParent()->getEntryBlock();
8423 Begin = BB->begin();
8424 } else {
8425 return false;
8426 }
8427
8428 // Limit number of instructions we look at, to avoid scanning through large
8429 // blocks. The current limit is chosen arbitrarily.
8430 unsigned ScanLimit = 32;
8431 BasicBlock::const_iterator End = BB->end();
8432
8433 if (!PoisonOnly) {
8434 // Since undef does not propagate eagerly, be conservative & just check
8435 // whether a value is directly passed to an instruction that must take
8436 // well-defined operands.
8437
8438 for (const auto &I : make_range(Begin, End)) {
8439 if (--ScanLimit == 0)
8440 break;
8441
8442 if (handleGuaranteedWellDefinedOps(&I, [V](const Value *WellDefinedOp) {
8443 return WellDefinedOp == V;
8444 }))
8445 return true;
8446
8448 break;
8449 }
8450 return false;
8451 }
8452
8453 // Set of instructions that we have proved will yield poison if Inst
8454 // does.
8455 SmallPtrSet<const Value *, 16> YieldsPoison;
8457
8458 YieldsPoison.insert(V);
8459 Visited.insert(BB);
8460
8461 while (true) {
8462 for (const auto &I : make_range(Begin, End)) {
8463 if (--ScanLimit == 0)
8464 return false;
8465 if (mustTriggerUB(&I, YieldsPoison))
8466 return true;
8468 return false;
8469
8470 // If an operand is poison and propagates it, mark I as yielding poison.
8471 for (const Use &Op : I.operands()) {
8472 if (YieldsPoison.count(Op) && propagatesPoison(Op)) {
8473 YieldsPoison.insert(&I);
8474 break;
8475 }
8476 }
8477
8478 // Special handling for select, which returns poison if its operand 0 is
8479 // poison (handled in the loop above) *or* if both its true/false operands
8480 // are poison (handled here).
8481 if (I.getOpcode() == Instruction::Select &&
8482 YieldsPoison.count(I.getOperand(1)) &&
8483 YieldsPoison.count(I.getOperand(2))) {
8484 YieldsPoison.insert(&I);
8485 }
8486 }
8487
8488 BB = BB->getSingleSuccessor();
8489 if (!BB || !Visited.insert(BB).second)
8490 break;
8491
8492 Begin = BB->getFirstNonPHIIt();
8493 End = BB->end();
8494 }
8495 return false;
8496}
8497
8499 return ::programUndefinedIfUndefOrPoison(Inst, false);
8500}
8501
8503 return ::programUndefinedIfUndefOrPoison(Inst, true);
8504}
8505
8506static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) {
8507 if (FMF.noNaNs())
8508 return true;
8509
8510 if (auto *C = dyn_cast<ConstantFP>(V))
8511 return !C->isNaN();
8512
8513 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8514 if (!C->getElementType()->isFloatingPointTy())
8515 return false;
8516 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8517 if (C->getElementAsAPFloat(I).isNaN())
8518 return false;
8519 }
8520 return true;
8521 }
8522
8524 return true;
8525
8526 return false;
8527}
8528
8529static bool isKnownNonZero(const Value *V) {
8530 if (auto *C = dyn_cast<ConstantFP>(V))
8531 return !C->isZero();
8532
8533 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8534 if (!C->getElementType()->isFloatingPointTy())
8535 return false;
8536 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8537 if (C->getElementAsAPFloat(I).isZero())
8538 return false;
8539 }
8540 return true;
8541 }
8542
8543 return false;
8544}
8545
8546/// Match clamp pattern for float types without care about NaNs or signed zeros.
8547/// Given non-min/max outer cmp/select from the clamp pattern this
8548/// function recognizes if it can be substitued by a "canonical" min/max
8549/// pattern.
8551 Value *CmpLHS, Value *CmpRHS,
8552 Value *TrueVal, Value *FalseVal,
8553 Value *&LHS, Value *&RHS) {
8554 // Try to match
8555 // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2))
8556 // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2))
8557 // and return description of the outer Max/Min.
8558
8559 // First, check if select has inverse order:
8560 if (CmpRHS == FalseVal) {
8561 std::swap(TrueVal, FalseVal);
8562 Pred = CmpInst::getInversePredicate(Pred);
8563 }
8564
8565 // Assume success now. If there's no match, callers should not use these anyway.
8566 LHS = TrueVal;
8567 RHS = FalseVal;
8568
8569 const APFloat *FC1;
8570 if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite())
8571 return {SPF_UNKNOWN, SPNB_NA, false};
8572
8573 const APFloat *FC2;
8574 switch (Pred) {
8575 case CmpInst::FCMP_OLT:
8576 case CmpInst::FCMP_OLE:
8577 case CmpInst::FCMP_ULT:
8578 case CmpInst::FCMP_ULE:
8579 if (match(FalseVal, m_OrdOrUnordFMin(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8580 *FC1 < *FC2)
8581 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
8582 if (match(FalseVal, m_FMinNum(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8583 *FC1 < *FC2)
8584 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
8585 break;
8586 case CmpInst::FCMP_OGT:
8587 case CmpInst::FCMP_OGE:
8588 case CmpInst::FCMP_UGT:
8589 case CmpInst::FCMP_UGE:
8590 if (match(FalseVal, m_OrdOrUnordFMax(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8591 *FC1 > *FC2)
8592 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
8593 if (match(FalseVal, m_FMaxNum(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8594 *FC1 > *FC2)
8595 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
8596 break;
8597 default:
8598 break;
8599 }
8600
8601 return {SPF_UNKNOWN, SPNB_NA, false};
8602}
8603
8604/// Recognize variations of:
8605/// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
8607 Value *CmpLHS, Value *CmpRHS,
8608 Value *TrueVal, Value *FalseVal) {
8609 // Swap the select operands and predicate to match the patterns below.
8610 if (CmpRHS != TrueVal) {
8611 Pred = ICmpInst::getSwappedPredicate(Pred);
8612 std::swap(TrueVal, FalseVal);
8613 }
8614 const APInt *C1;
8615 if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) {
8616 const APInt *C2;
8617 // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
8618 if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8619 C1->slt(*C2) && Pred == CmpInst::ICMP_SLT)
8620 return {SPF_SMAX, SPNB_NA, false};
8621
8622 // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
8623 if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8624 C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT)
8625 return {SPF_SMIN, SPNB_NA, false};
8626
8627 // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
8628 if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8629 C1->ult(*C2) && Pred == CmpInst::ICMP_ULT)
8630 return {SPF_UMAX, SPNB_NA, false};
8631
8632 // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
8633 if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8634 C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT)
8635 return {SPF_UMIN, SPNB_NA, false};
8636 }
8637 return {SPF_UNKNOWN, SPNB_NA, false};
8638}
8639
8640/// Recognize variations of:
8641/// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c))
8643 Value *CmpLHS, Value *CmpRHS,
8644 Value *TVal, Value *FVal,
8645 unsigned Depth) {
8646 // TODO: Allow FP min/max with nnan/nsz.
8647 assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison");
8648
8649 Value *A = nullptr, *B = nullptr;
8650 SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1);
8651 if (!SelectPatternResult::isMinOrMax(L.Flavor))
8652 return {SPF_UNKNOWN, SPNB_NA, false};
8653
8654 Value *C = nullptr, *D = nullptr;
8655 SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1);
8656 if (L.Flavor != R.Flavor)
8657 return {SPF_UNKNOWN, SPNB_NA, false};
8658
8659 // We have something like: x Pred y ? min(a, b) : min(c, d).
8660 // Try to match the compare to the min/max operations of the select operands.
8661 // First, make sure we have the right compare predicate.
8662 switch (L.Flavor) {
8663 case SPF_SMIN:
8664 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) {
8665 Pred = ICmpInst::getSwappedPredicate(Pred);
8666 std::swap(CmpLHS, CmpRHS);
8667 }
8668 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
8669 break;
8670 return {SPF_UNKNOWN, SPNB_NA, false};
8671 case SPF_SMAX:
8672 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) {
8673 Pred = ICmpInst::getSwappedPredicate(Pred);
8674 std::swap(CmpLHS, CmpRHS);
8675 }
8676 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE)
8677 break;
8678 return {SPF_UNKNOWN, SPNB_NA, false};
8679 case SPF_UMIN:
8680 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
8681 Pred = ICmpInst::getSwappedPredicate(Pred);
8682 std::swap(CmpLHS, CmpRHS);
8683 }
8684 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE)
8685 break;
8686 return {SPF_UNKNOWN, SPNB_NA, false};
8687 case SPF_UMAX:
8688 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
8689 Pred = ICmpInst::getSwappedPredicate(Pred);
8690 std::swap(CmpLHS, CmpRHS);
8691 }
8692 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
8693 break;
8694 return {SPF_UNKNOWN, SPNB_NA, false};
8695 default:
8696 return {SPF_UNKNOWN, SPNB_NA, false};
8697 }
8698
8699 // If there is a common operand in the already matched min/max and the other
8700 // min/max operands match the compare operands (either directly or inverted),
8701 // then this is min/max of the same flavor.
8702
8703 // a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8704 // ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8705 if (D == B) {
8706 if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8707 match(A, m_Not(m_Specific(CmpRHS)))))
8708 return {L.Flavor, SPNB_NA, false};
8709 }
8710 // a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8711 // ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8712 if (C == B) {
8713 if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8714 match(A, m_Not(m_Specific(CmpRHS)))))
8715 return {L.Flavor, SPNB_NA, false};
8716 }
8717 // b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8718 // ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8719 if (D == A) {
8720 if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8721 match(B, m_Not(m_Specific(CmpRHS)))))
8722 return {L.Flavor, SPNB_NA, false};
8723 }
8724 // b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8725 // ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8726 if (C == A) {
8727 if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8728 match(B, m_Not(m_Specific(CmpRHS)))))
8729 return {L.Flavor, SPNB_NA, false};
8730 }
8731
8732 return {SPF_UNKNOWN, SPNB_NA, false};
8733}
8734
8735/// If the input value is the result of a 'not' op, constant integer, or vector
8736/// splat of a constant integer, return the bitwise-not source value.
8737/// TODO: This could be extended to handle non-splat vector integer constants.
8739 Value *NotV;
8740 if (match(V, m_Not(m_Value(NotV))))
8741 return NotV;
8742
8743 const APInt *C;
8744 if (match(V, m_APInt(C)))
8745 return ConstantInt::get(V->getType(), ~(*C));
8746
8747 return nullptr;
8748}
8749
8750/// Match non-obvious integer minimum and maximum sequences.
8752 Value *CmpLHS, Value *CmpRHS,
8753 Value *TrueVal, Value *FalseVal,
8754 Value *&LHS, Value *&RHS,
8755 unsigned Depth) {
8756 // Assume success. If there's no match, callers should not use these anyway.
8757 LHS = TrueVal;
8758 RHS = FalseVal;
8759
8760 SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);
8762 return SPR;
8763
8764 SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth);
8766 return SPR;
8767
8768 // Look through 'not' ops to find disguised min/max.
8769 // (X > Y) ? ~X : ~Y ==> (~X < ~Y) ? ~X : ~Y ==> MIN(~X, ~Y)
8770 // (X < Y) ? ~X : ~Y ==> (~X > ~Y) ? ~X : ~Y ==> MAX(~X, ~Y)
8771 if (CmpLHS == getNotValue(TrueVal) && CmpRHS == getNotValue(FalseVal)) {
8772 switch (Pred) {
8773 case CmpInst::ICMP_SGT: return {SPF_SMIN, SPNB_NA, false};
8774 case CmpInst::ICMP_SLT: return {SPF_SMAX, SPNB_NA, false};
8775 case CmpInst::ICMP_UGT: return {SPF_UMIN, SPNB_NA, false};
8776 case CmpInst::ICMP_ULT: return {SPF_UMAX, SPNB_NA, false};
8777 default: break;
8778 }
8779 }
8780
8781 // (X > Y) ? ~Y : ~X ==> (~X < ~Y) ? ~Y : ~X ==> MAX(~Y, ~X)
8782 // (X < Y) ? ~Y : ~X ==> (~X > ~Y) ? ~Y : ~X ==> MIN(~Y, ~X)
8783 if (CmpLHS == getNotValue(FalseVal) && CmpRHS == getNotValue(TrueVal)) {
8784 switch (Pred) {
8785 case CmpInst::ICMP_SGT: return {SPF_SMAX, SPNB_NA, false};
8786 case CmpInst::ICMP_SLT: return {SPF_SMIN, SPNB_NA, false};
8787 case CmpInst::ICMP_UGT: return {SPF_UMAX, SPNB_NA, false};
8788 case CmpInst::ICMP_ULT: return {SPF_UMIN, SPNB_NA, false};
8789 default: break;
8790 }
8791 }
8792
8793 if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT)
8794 return {SPF_UNKNOWN, SPNB_NA, false};
8795
8796 const APInt *C1;
8797 if (!match(CmpRHS, m_APInt(C1)))
8798 return {SPF_UNKNOWN, SPNB_NA, false};
8799
8800 // An unsigned min/max can be written with a signed compare.
8801 const APInt *C2;
8802 if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) ||
8803 (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) {
8804 // Is the sign bit set?
8805 // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX
8806 // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN
8807 if (Pred == CmpInst::ICMP_SLT && C1->isZero() && C2->isMaxSignedValue())
8808 return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8809
8810 // Is the sign bit clear?
8811 // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX
8812 // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN
8813 if (Pred == CmpInst::ICMP_SGT && C1->isAllOnes() && C2->isMinSignedValue())
8814 return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8815 }
8816
8817 return {SPF_UNKNOWN, SPNB_NA, false};
8818}
8819
8820bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW,
8821 bool AllowPoison) {
8822 assert(X && Y && "Invalid operand");
8823
8824 auto IsNegationOf = [&](const Value *X, const Value *Y) {
8825 if (!match(X, m_Neg(m_Specific(Y))))
8826 return false;
8827
8828 auto *BO = cast<BinaryOperator>(X);
8829 if (NeedNSW && !BO->hasNoSignedWrap())
8830 return false;
8831
8832 auto *Zero = cast<Constant>(BO->getOperand(0));
8833 if (!AllowPoison && !Zero->isNullValue())
8834 return false;
8835
8836 return true;
8837 };
8838
8839 // X = -Y or Y = -X
8840 if (IsNegationOf(X, Y) || IsNegationOf(Y, X))
8841 return true;
8842
8843 // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A)
8844 Value *A, *B;
8845 return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) &&
8846 match(Y, m_Sub(m_Specific(B), m_Specific(A))))) ||
8847 (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) &&
8849}
8850
8851bool llvm::isKnownInversion(const Value *X, const Value *Y) {
8852 // Handle X = icmp pred A, B, Y = icmp pred A, C.
8853 Value *A, *B, *C;
8854 CmpPredicate Pred1, Pred2;
8855 if (!match(X, m_ICmp(Pred1, m_Value(A), m_Value(B))) ||
8856 !match(Y, m_c_ICmp(Pred2, m_Specific(A), m_Value(C))))
8857 return false;
8858
8859 // They must both have samesign flag or not.
8860 if (Pred1.hasSameSign() != Pred2.hasSameSign())
8861 return false;
8862
8863 if (B == C)
8864 return Pred1 == ICmpInst::getInversePredicate(Pred2);
8865
8866 // Try to infer the relationship from constant ranges.
8867 const APInt *RHSC1, *RHSC2;
8868 if (!match(B, m_APInt(RHSC1)) || !match(C, m_APInt(RHSC2)))
8869 return false;
8870
8871 // Sign bits of two RHSCs should match.
8872 if (Pred1.hasSameSign() && RHSC1->isNonNegative() != RHSC2->isNonNegative())
8873 return false;
8874
8875 const auto CR1 = ConstantRange::makeExactICmpRegion(Pred1, *RHSC1);
8876 const auto CR2 = ConstantRange::makeExactICmpRegion(Pred2, *RHSC2);
8877
8878 return CR1.inverse() == CR2;
8879}
8880
8882 SelectPatternNaNBehavior NaNBehavior,
8883 bool Ordered) {
8884 switch (Pred) {
8885 default:
8886 return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.
8887 case ICmpInst::ICMP_UGT:
8888 case ICmpInst::ICMP_UGE:
8889 return {SPF_UMAX, SPNB_NA, false};
8890 case ICmpInst::ICMP_SGT:
8891 case ICmpInst::ICMP_SGE:
8892 return {SPF_SMAX, SPNB_NA, false};
8893 case ICmpInst::ICMP_ULT:
8894 case ICmpInst::ICMP_ULE:
8895 return {SPF_UMIN, SPNB_NA, false};
8896 case ICmpInst::ICMP_SLT:
8897 case ICmpInst::ICMP_SLE:
8898 return {SPF_SMIN, SPNB_NA, false};
8899 case FCmpInst::FCMP_UGT:
8900 case FCmpInst::FCMP_UGE:
8901 case FCmpInst::FCMP_OGT:
8902 case FCmpInst::FCMP_OGE:
8903 return {SPF_FMAXNUM, NaNBehavior, Ordered};
8904 case FCmpInst::FCMP_ULT:
8905 case FCmpInst::FCMP_ULE:
8906 case FCmpInst::FCMP_OLT:
8907 case FCmpInst::FCMP_OLE:
8908 return {SPF_FMINNUM, NaNBehavior, Ordered};
8909 }
8910}
8911
8912std::optional<std::pair<CmpPredicate, Constant *>>
8915 "Only for relational integer predicates.");
8916 if (isa<UndefValue>(C))
8917 return std::nullopt;
8918
8919 Type *Type = C->getType();
8920 bool IsSigned = ICmpInst::isSigned(Pred);
8921
8923 bool WillIncrement =
8924 UnsignedPred == ICmpInst::ICMP_ULE || UnsignedPred == ICmpInst::ICMP_UGT;
8925
8926 // Check if the constant operand can be safely incremented/decremented
8927 // without overflowing/underflowing.
8928 auto ConstantIsOk = [WillIncrement, IsSigned](ConstantInt *C) {
8929 return WillIncrement ? !C->isMaxValue(IsSigned) : !C->isMinValue(IsSigned);
8930 };
8931
8932 Constant *SafeReplacementConstant = nullptr;
8933 if (auto *CI = dyn_cast<ConstantInt>(C)) {
8934 // Bail out if the constant can't be safely incremented/decremented.
8935 if (!ConstantIsOk(CI))
8936 return std::nullopt;
8937 } else if (auto *FVTy = dyn_cast<FixedVectorType>(Type)) {
8938 unsigned NumElts = FVTy->getNumElements();
8939 for (unsigned i = 0; i != NumElts; ++i) {
8940 Constant *Elt = C->getAggregateElement(i);
8941 if (!Elt)
8942 return std::nullopt;
8943
8944 if (isa<UndefValue>(Elt))
8945 continue;
8946
8947 // Bail out if we can't determine if this constant is min/max or if we
8948 // know that this constant is min/max.
8949 auto *CI = dyn_cast<ConstantInt>(Elt);
8950 if (!CI || !ConstantIsOk(CI))
8951 return std::nullopt;
8952
8953 if (!SafeReplacementConstant)
8954 SafeReplacementConstant = CI;
8955 }
8956 } else if (isa<VectorType>(C->getType())) {
8957 // Handle scalable splat
8958 Value *SplatC = C->getSplatValue();
8959 auto *CI = dyn_cast_or_null<ConstantInt>(SplatC);
8960 // Bail out if the constant can't be safely incremented/decremented.
8961 if (!CI || !ConstantIsOk(CI))
8962 return std::nullopt;
8963 } else {
8964 // ConstantExpr?
8965 return std::nullopt;
8966 }
8967
8968 // It may not be safe to change a compare predicate in the presence of
8969 // undefined elements, so replace those elements with the first safe constant
8970 // that we found.
8971 // TODO: in case of poison, it is safe; let's replace undefs only.
8972 if (C->containsUndefOrPoisonElement()) {
8973 assert(SafeReplacementConstant && "Replacement constant not set");
8974 C = Constant::replaceUndefsWith(C, SafeReplacementConstant);
8975 }
8976
8978
8979 // Increment or decrement the constant.
8980 Constant *OneOrNegOne = ConstantInt::get(Type, WillIncrement ? 1 : -1, true);
8981 Constant *NewC = ConstantExpr::getAdd(C, OneOrNegOne);
8982
8983 return std::make_pair(NewPred, NewC);
8984}
8985
8987 FastMathFlags FMF,
8988 Value *CmpLHS, Value *CmpRHS,
8989 Value *TrueVal, Value *FalseVal,
8990 Value *&LHS, Value *&RHS,
8991 unsigned Depth) {
8992 bool HasMismatchedZeros = false;
8993 if (CmpInst::isFPPredicate(Pred)) {
8994 // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one
8995 // 0.0 operand, set the compare's 0.0 operands to that same value for the
8996 // purpose of identifying min/max. Disregard vector constants with undefined
8997 // elements because those can not be back-propagated for analysis.
8998 Value *OutputZeroVal = nullptr;
8999 if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) &&
9000 !cast<Constant>(TrueVal)->containsUndefOrPoisonElement())
9001 OutputZeroVal = TrueVal;
9002 else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) &&
9003 !cast<Constant>(FalseVal)->containsUndefOrPoisonElement())
9004 OutputZeroVal = FalseVal;
9005
9006 if (OutputZeroVal) {
9007 if (match(CmpLHS, m_AnyZeroFP()) && CmpLHS != OutputZeroVal) {
9008 HasMismatchedZeros = true;
9009 CmpLHS = OutputZeroVal;
9010 }
9011 if (match(CmpRHS, m_AnyZeroFP()) && CmpRHS != OutputZeroVal) {
9012 HasMismatchedZeros = true;
9013 CmpRHS = OutputZeroVal;
9014 }
9015 }
9016 }
9017
9018 LHS = CmpLHS;
9019 RHS = CmpRHS;
9020
9021 // Signed zero may return inconsistent results between implementations.
9022 // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0
9023 // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)
9024 // Therefore, we behave conservatively and only proceed if at least one of the
9025 // operands is known to not be zero or if we don't care about signed zero.
9026 switch (Pred) {
9027 default: break;
9030 if (!HasMismatchedZeros)
9031 break;
9032 [[fallthrough]];
9035 if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
9036 !isKnownNonZero(CmpRHS))
9037 return {SPF_UNKNOWN, SPNB_NA, false};
9038 }
9039
9040 SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
9041 bool Ordered = false;
9042
9043 // When given one NaN and one non-NaN input:
9044 // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
9045 // - A simple C99 (a < b ? a : b) construction will return 'b' (as the
9046 // ordered comparison fails), which could be NaN or non-NaN.
9047 // so here we discover exactly what NaN behavior is required/accepted.
9048 if (CmpInst::isFPPredicate(Pred)) {
9049 bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
9050 bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
9051
9052 if (LHSSafe && RHSSafe) {
9053 // Both operands are known non-NaN.
9054 NaNBehavior = SPNB_RETURNS_ANY;
9055 Ordered = CmpInst::isOrdered(Pred);
9056 } else if (CmpInst::isOrdered(Pred)) {
9057 // An ordered comparison will return false when given a NaN, so it
9058 // returns the RHS.
9059 Ordered = true;
9060 if (LHSSafe)
9061 // LHS is non-NaN, so if RHS is NaN then NaN will be returned.
9062 NaNBehavior = SPNB_RETURNS_NAN;
9063 else if (RHSSafe)
9064 NaNBehavior = SPNB_RETURNS_OTHER;
9065 else
9066 // Completely unsafe.
9067 return {SPF_UNKNOWN, SPNB_NA, false};
9068 } else {
9069 Ordered = false;
9070 // An unordered comparison will return true when given a NaN, so it
9071 // returns the LHS.
9072 if (LHSSafe)
9073 // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
9074 NaNBehavior = SPNB_RETURNS_OTHER;
9075 else if (RHSSafe)
9076 NaNBehavior = SPNB_RETURNS_NAN;
9077 else
9078 // Completely unsafe.
9079 return {SPF_UNKNOWN, SPNB_NA, false};
9080 }
9081 }
9082
9083 if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
9084 std::swap(CmpLHS, CmpRHS);
9085 Pred = CmpInst::getSwappedPredicate(Pred);
9086 if (NaNBehavior == SPNB_RETURNS_NAN)
9087 NaNBehavior = SPNB_RETURNS_OTHER;
9088 else if (NaNBehavior == SPNB_RETURNS_OTHER)
9089 NaNBehavior = SPNB_RETURNS_NAN;
9090 Ordered = !Ordered;
9091 }
9092
9093 // ([if]cmp X, Y) ? X : Y
9094 if (TrueVal == CmpLHS && FalseVal == CmpRHS)
9095 return getSelectPattern(Pred, NaNBehavior, Ordered);
9096
9097 if (isKnownNegation(TrueVal, FalseVal)) {
9098 // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can
9099 // match against either LHS or sign-preserving operations on LHS, like
9100 // sext(LHS), or binary ops that do not wrap in signed sense.
9101 auto CmpLHSOrSExt =
9102 m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS)));
9103 auto MaybeSExtOrMulCmpLHS =
9104 m_CombineOr(CmpLHSOrSExt, m_NSWMul(CmpLHSOrSExt, m_StrictlyPositive()),
9105 m_NSWShl(CmpLHSOrSExt, m_Value()));
9106 auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes());
9107 auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One());
9108 if (match(TrueVal, MaybeSExtOrMulCmpLHS)) {
9109 // Set the return values. If the compare uses the negated value (-X >s 0),
9110 // swap the return values because the negated value is always 'RHS'.
9111 LHS = TrueVal;
9112 RHS = FalseVal;
9113 if (match(CmpLHS, m_Neg(m_Specific(FalseVal))))
9114 std::swap(LHS, RHS);
9115
9116 // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X)
9117 // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X)
9118 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
9119 return {SPF_ABS, SPNB_NA, false};
9120
9121 // (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X)
9122 if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne))
9123 return {SPF_ABS, SPNB_NA, false};
9124
9125 // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X)
9126 // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X)
9127 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
9128 return {SPF_NABS, SPNB_NA, false};
9129 } else if (match(FalseVal, MaybeSExtOrMulCmpLHS)) {
9130 // Set the return values. If the compare uses the negated value (-X >s 0),
9131 // swap the return values because the negated value is always 'RHS'.
9132 LHS = FalseVal;
9133 RHS = TrueVal;
9134 if (match(CmpLHS, m_Neg(m_Specific(TrueVal))))
9135 std::swap(LHS, RHS);
9136
9137 // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X)
9138 // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X)
9139 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
9140 return {SPF_NABS, SPNB_NA, false};
9141
9142 // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X)
9143 // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X)
9144 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
9145 return {SPF_ABS, SPNB_NA, false};
9146 }
9147 }
9148
9149 if (CmpInst::isIntPredicate(Pred))
9150 return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth);
9151
9152 // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar
9153 // may return either -0.0 or 0.0, so fcmp/select pair has stricter
9154 // semantics than minNum. Be conservative in such case.
9155 if (NaNBehavior != SPNB_RETURNS_ANY ||
9156 (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
9157 !isKnownNonZero(CmpRHS)))
9158 return {SPF_UNKNOWN, SPNB_NA, false};
9159
9160 return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
9161}
9162
9164 Instruction::CastOps *CastOp) {
9165 const DataLayout &DL = CmpI->getDataLayout();
9166
9167 Constant *CastedTo = nullptr;
9168 switch (*CastOp) {
9169 case Instruction::ZExt:
9170 if (CmpI->isUnsigned())
9171 CastedTo = ConstantExpr::getTrunc(C, SrcTy);
9172 break;
9173 case Instruction::SExt:
9174 if (CmpI->isSigned())
9175 CastedTo = ConstantExpr::getTrunc(C, SrcTy, true);
9176 break;
9177 case Instruction::Trunc:
9178 Constant *CmpConst;
9179 if (match(CmpI->getOperand(1), m_Constant(CmpConst)) &&
9180 CmpConst->getType() == SrcTy) {
9181 // Here we have the following case:
9182 //
9183 // %cond = cmp iN %x, CmpConst
9184 // %tr = trunc iN %x to iK
9185 // %narrowsel = select i1 %cond, iK %t, iK C
9186 //
9187 // We can always move trunc after select operation:
9188 //
9189 // %cond = cmp iN %x, CmpConst
9190 // %widesel = select i1 %cond, iN %x, iN CmpConst
9191 // %tr = trunc iN %widesel to iK
9192 //
9193 // Note that C could be extended in any way because we don't care about
9194 // upper bits after truncation. It can't be abs pattern, because it would
9195 // look like:
9196 //
9197 // select i1 %cond, x, -x.
9198 //
9199 // So only min/max pattern could be matched. Such match requires widened C
9200 // == CmpConst. That is why set widened C = CmpConst, condition trunc
9201 // CmpConst == C is checked below.
9202 CastedTo = CmpConst;
9203 } else {
9204 unsigned ExtOp = CmpI->isSigned() ? Instruction::SExt : Instruction::ZExt;
9205 CastedTo = ConstantFoldCastOperand(ExtOp, C, SrcTy, DL);
9206 }
9207 break;
9208 case Instruction::FPTrunc:
9209 CastedTo = ConstantFoldCastOperand(Instruction::FPExt, C, SrcTy, DL);
9210 break;
9211 case Instruction::FPExt:
9212 CastedTo = ConstantFoldCastOperand(Instruction::FPTrunc, C, SrcTy, DL);
9213 break;
9214 case Instruction::FPToUI:
9215 CastedTo = ConstantFoldCastOperand(Instruction::UIToFP, C, SrcTy, DL);
9216 break;
9217 case Instruction::FPToSI:
9218 CastedTo = ConstantFoldCastOperand(Instruction::SIToFP, C, SrcTy, DL);
9219 break;
9220 case Instruction::UIToFP:
9221 CastedTo = ConstantFoldCastOperand(Instruction::FPToUI, C, SrcTy, DL);
9222 break;
9223 case Instruction::SIToFP:
9224 CastedTo = ConstantFoldCastOperand(Instruction::FPToSI, C, SrcTy, DL);
9225 break;
9226 default:
9227 break;
9228 }
9229
9230 if (!CastedTo)
9231 return nullptr;
9232
9233 // Make sure the cast doesn't lose any information.
9234 Constant *CastedBack =
9235 ConstantFoldCastOperand(*CastOp, CastedTo, C->getType(), DL);
9236 if (CastedBack && CastedBack != C)
9237 return nullptr;
9238
9239 return CastedTo;
9240}
9241
9242/// Helps to match a select pattern in case of a type mismatch.
9243///
9244/// The function processes the case when type of true and false values of a
9245/// select instruction differs from type of the cmp instruction operands because
9246/// of a cast instruction. The function checks if it is legal to move the cast
9247/// operation after "select". If yes, it returns the new second value of
9248/// "select" (with the assumption that cast is moved):
9249/// 1. As operand of cast instruction when both values of "select" are same cast
9250/// instructions.
9251/// 2. As restored constant (by applying reverse cast operation) when the first
9252/// value of the "select" is a cast operation and the second value is a
9253/// constant. It is implemented in lookThroughCastConst().
9254/// 3. As one operand is cast instruction and the other is not. The operands in
9255/// sel(cmp) are in different type integer.
9256/// NOTE: We return only the new second value because the first value could be
9257/// accessed as operand of cast instruction.
9259 Instruction::CastOps *CastOp) {
9260 auto *Cast1 = dyn_cast<CastInst>(V1);
9261 if (!Cast1)
9262 return nullptr;
9263
9264 *CastOp = Cast1->getOpcode();
9265 Type *SrcTy = Cast1->getSrcTy();
9266 if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
9267 // If V1 and V2 are both the same cast from the same type, look through V1.
9268 if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
9269 return Cast2->getOperand(0);
9270 return nullptr;
9271 }
9272
9273 auto *C = dyn_cast<Constant>(V2);
9274 if (C)
9275 return lookThroughCastConst(CmpI, SrcTy, C, CastOp);
9276
9277 Value *CastedTo = nullptr;
9278 if (*CastOp == Instruction::Trunc) {
9279 if (match(CmpI->getOperand(1), m_ZExtOrSExt(m_Specific(V2)))) {
9280 // Here we have the following case:
9281 // %y_ext = sext iK %y to iN
9282 // %cond = cmp iN %x, %y_ext
9283 // %tr = trunc iN %x to iK
9284 // %narrowsel = select i1 %cond, iK %tr, iK %y
9285 //
9286 // We can always move trunc after select operation:
9287 // %y_ext = sext iK %y to iN
9288 // %cond = cmp iN %x, %y_ext
9289 // %widesel = select i1 %cond, iN %x, iN %y_ext
9290 // %tr = trunc iN %widesel to iK
9291 assert(V2->getType() == Cast1->getType() &&
9292 "V2 and Cast1 should be the same type.");
9293 CastedTo = CmpI->getOperand(1);
9294 }
9295 }
9296
9297 return CastedTo;
9298}
9300 Instruction::CastOps *CastOp,
9301 unsigned Depth) {
9303 return {SPF_UNKNOWN, SPNB_NA, false};
9304
9306 if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
9307
9308 CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
9309 if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};
9310
9311 Value *TrueVal = SI->getTrueValue();
9312 Value *FalseVal = SI->getFalseValue();
9313
9314 return llvm::matchDecomposedSelectPattern(CmpI, TrueVal, FalseVal, LHS, RHS,
9315 SI->getFastMathFlagsOrNone(),
9316 CastOp, Depth);
9317}
9318
9320 CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS,
9321 FastMathFlags FMF, Instruction::CastOps *CastOp, unsigned Depth) {
9322 CmpInst::Predicate Pred = CmpI->getPredicate();
9323 Value *CmpLHS = CmpI->getOperand(0);
9324 Value *CmpRHS = CmpI->getOperand(1);
9325 if (isa<FPMathOperator>(CmpI) && CmpI->hasNoNaNs())
9326 FMF.setNoNaNs();
9327
9328 // Bail out early.
9329 if (CmpI->isEquality())
9330 return {SPF_UNKNOWN, SPNB_NA, false};
9331
9332 // Deal with type mismatches.
9333 if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
9334 if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) {
9335 // If this is a potential fmin/fmax with a cast to integer, then ignore
9336 // -0.0 because there is no corresponding integer value.
9337 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9338 FMF.setNoSignedZeros();
9339 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9340 cast<CastInst>(TrueVal)->getOperand(0), C,
9341 LHS, RHS, Depth);
9342 }
9343 if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) {
9344 // If this is a potential fmin/fmax with a cast to integer, then ignore
9345 // -0.0 because there is no corresponding integer value.
9346 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9347 FMF.setNoSignedZeros();
9348 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9349 C, cast<CastInst>(FalseVal)->getOperand(0),
9350 LHS, RHS, Depth);
9351 }
9352 }
9353 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
9354 LHS, RHS, Depth);
9355}
9356
9358 if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT;
9359 if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT;
9360 if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT;
9361 if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT;
9362 if (SPF == SPF_FMINNUM)
9363 return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT;
9364 if (SPF == SPF_FMAXNUM)
9365 return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT;
9366 llvm_unreachable("unhandled!");
9367}
9368
9370 switch (SPF) {
9372 return Intrinsic::umin;
9374 return Intrinsic::umax;
9376 return Intrinsic::smin;
9378 return Intrinsic::smax;
9379 default:
9380 llvm_unreachable("Unexpected SPF");
9381 }
9382}
9383
9385 if (SPF == SPF_SMIN) return SPF_SMAX;
9386 if (SPF == SPF_UMIN) return SPF_UMAX;
9387 if (SPF == SPF_SMAX) return SPF_SMIN;
9388 if (SPF == SPF_UMAX) return SPF_UMIN;
9389 llvm_unreachable("unhandled!");
9390}
9391
9393 switch (MinMaxID) {
9394 case Intrinsic::smax: return Intrinsic::smin;
9395 case Intrinsic::smin: return Intrinsic::smax;
9396 case Intrinsic::umax: return Intrinsic::umin;
9397 case Intrinsic::umin: return Intrinsic::umax;
9398 // Please note that next four intrinsics may produce the same result for
9399 // original and inverted case even if X != Y due to NaN is handled specially.
9400 case Intrinsic::maximum: return Intrinsic::minimum;
9401 case Intrinsic::minimum: return Intrinsic::maximum;
9402 case Intrinsic::maxnum: return Intrinsic::minnum;
9403 case Intrinsic::minnum: return Intrinsic::maxnum;
9404 case Intrinsic::maximumnum:
9405 return Intrinsic::minimumnum;
9406 case Intrinsic::minimumnum:
9407 return Intrinsic::maximumnum;
9408 default: llvm_unreachable("Unexpected intrinsic");
9409 }
9410}
9411
9413 switch (SPF) {
9416 case SPF_UMAX: return APInt::getMaxValue(BitWidth);
9417 case SPF_UMIN: return APInt::getMinValue(BitWidth);
9418 default: llvm_unreachable("Unexpected flavor");
9419 }
9420}
9421
9422std::pair<Intrinsic::ID, bool>
9424 // Check if VL contains select instructions that can be folded into a min/max
9425 // vector intrinsic and return the intrinsic if it is possible.
9426 // TODO: Support floating point min/max.
9427 bool AllCmpSingleUse = true;
9428 SelectPatternResult SelectPattern;
9429 SelectPattern.Flavor = SPF_UNKNOWN;
9430 if (all_of(VL, [&SelectPattern, &AllCmpSingleUse](Value *I) {
9431 Value *LHS, *RHS;
9432 auto CurrentPattern = matchSelectPattern(I, LHS, RHS);
9433 if (!SelectPatternResult::isMinOrMax(CurrentPattern.Flavor))
9434 return false;
9435 if (SelectPattern.Flavor != SPF_UNKNOWN &&
9436 SelectPattern.Flavor != CurrentPattern.Flavor)
9437 return false;
9438 SelectPattern = CurrentPattern;
9439 AllCmpSingleUse &=
9441 return true;
9442 })) {
9443 switch (SelectPattern.Flavor) {
9444 case SPF_SMIN:
9445 return {Intrinsic::smin, AllCmpSingleUse};
9446 case SPF_UMIN:
9447 return {Intrinsic::umin, AllCmpSingleUse};
9448 case SPF_SMAX:
9449 return {Intrinsic::smax, AllCmpSingleUse};
9450 case SPF_UMAX:
9451 return {Intrinsic::umax, AllCmpSingleUse};
9452 case SPF_FMAXNUM:
9453 return {Intrinsic::maxnum, AllCmpSingleUse};
9454 case SPF_FMINNUM:
9455 return {Intrinsic::minnum, AllCmpSingleUse};
9456 default:
9457 llvm_unreachable("unexpected select pattern flavor");
9458 }
9459 }
9460 return {Intrinsic::not_intrinsic, false};
9461}
9462
9463template <typename InstTy>
9464static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst,
9465 Value *&Init, Value *&OtherOp) {
9466 // Handle the case of a simple two-predecessor recurrence PHI.
9467 // There's a lot more that could theoretically be done here, but
9468 // this is sufficient to catch some interesting cases.
9469 // TODO: Expand list -- gep, uadd.sat etc.
9470 if (PN->getNumIncomingValues() != 2)
9471 return false;
9472
9473 for (unsigned I = 0; I != 2; ++I) {
9474 if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
9475 Operation && Operation->getNumOperands() >= 2) {
9476 Value *LHS = Operation->getOperand(0);
9477 Value *RHS = Operation->getOperand(1);
9478 if (LHS != PN && RHS != PN)
9479 continue;
9480
9481 Inst = Operation;
9482 Init = PN->getIncomingValue(!I);
9483 OtherOp = (LHS == PN) ? RHS : LHS;
9484 return true;
9485 }
9486 }
9487 return false;
9488}
9489
9490template <typename InstTy>
9491static bool matchThreeInputRecurrence(const PHINode *PN, InstTy *&Inst,
9492 Value *&Init, Value *&OtherOp0,
9493 Value *&OtherOp1) {
9494 if (PN->getNumIncomingValues() != 2)
9495 return false;
9496
9497 for (unsigned I = 0; I != 2; ++I) {
9498 if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
9499 Operation && Operation->getNumOperands() >= 3) {
9500 Value *Op0 = Operation->getOperand(0);
9501 Value *Op1 = Operation->getOperand(1);
9502 Value *Op2 = Operation->getOperand(2);
9503
9504 if (Op0 != PN && Op1 != PN && Op2 != PN)
9505 continue;
9506
9507 Inst = Operation;
9508 Init = PN->getIncomingValue(!I);
9509 if (Op0 == PN) {
9510 OtherOp0 = Op1;
9511 OtherOp1 = Op2;
9512 } else if (Op1 == PN) {
9513 OtherOp0 = Op0;
9514 OtherOp1 = Op2;
9515 } else {
9516 OtherOp0 = Op0;
9517 OtherOp1 = Op1;
9518 }
9519 return true;
9520 }
9521 }
9522 return false;
9523}
9525 Value *&Start, Value *&Step) {
9526 // We try to match a recurrence of the form:
9527 // %iv = [Start, %entry], [%iv.next, %backedge]
9528 // %iv.next = binop %iv, Step
9529 // Or:
9530 // %iv = [Start, %entry], [%iv.next, %backedge]
9531 // %iv.next = binop Step, %iv
9532 return matchTwoInputRecurrence(P, BO, Start, Step);
9533}
9534
9536 Value *&Start, Value *&Step) {
9537 BinaryOperator *BO = nullptr;
9538 return match(I, m_c_BinOp(m_Phi(P), m_Value())) &&
9539 matchSimpleRecurrence(P, BO, Start, Step) && BO == I;
9540}
9541
9543 PHINode *&P, Value *&Init,
9544 Value *&OtherOp) {
9545 // Binary intrinsics only supported for now.
9546 if (I->arg_size() != 2 || I->getType() != I->getArgOperand(0)->getType() ||
9547 I->getType() != I->getArgOperand(1)->getType())
9548 return false;
9549
9550 IntrinsicInst *II = nullptr;
9551 P = dyn_cast<PHINode>(I->getArgOperand(0));
9552 if (!P)
9553 P = dyn_cast<PHINode>(I->getArgOperand(1));
9554
9555 return P && matchTwoInputRecurrence(P, II, Init, OtherOp) && II == I;
9556}
9557
9559 PHINode *&P, Value *&Init,
9560 Value *&OtherOp0,
9561 Value *&OtherOp1) {
9562 if (I->arg_size() != 3 || I->getType() != I->getArgOperand(0)->getType() ||
9563 I->getType() != I->getArgOperand(1)->getType() ||
9564 I->getType() != I->getArgOperand(2)->getType())
9565 return false;
9566 IntrinsicInst *II = nullptr;
9567 P = dyn_cast<PHINode>(I->getArgOperand(0));
9568 if (!P) {
9569 P = dyn_cast<PHINode>(I->getArgOperand(1));
9570 if (!P)
9571 P = dyn_cast<PHINode>(I->getArgOperand(2));
9572 }
9573 return P && matchThreeInputRecurrence(P, II, Init, OtherOp0, OtherOp1) &&
9574 II == I;
9575}
9576
9577/// Return true if "icmp Pred LHS RHS" is always true.
9579 const Value *RHS) {
9580 if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS)
9581 return true;
9582
9583 switch (Pred) {
9584 default:
9585 return false;
9586
9587 case CmpInst::ICMP_SLE: {
9588 const APInt *C;
9589
9590 // LHS s<= LHS +_{nsw} C if C >= 0
9591 // LHS s<= LHS | C if C >= 0
9592 if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C))) ||
9594 return !C->isNegative();
9595
9596 // LHS s<= smax(LHS, V) for any V
9598 return true;
9599
9600 // smin(RHS, V) s<= RHS for any V
9602 return true;
9603
9604 // Match A to (X +_{nsw} CA) and B to (X +_{nsw} CB)
9605 const Value *X;
9606 const APInt *CLHS, *CRHS;
9607 if (match(LHS, m_NSWAddLike(m_Value(X), m_APInt(CLHS))) &&
9609 return CLHS->sle(*CRHS);
9610
9611 return false;
9612 }
9613
9614 case CmpInst::ICMP_ULE: {
9615 // LHS u<= LHS +_{nuw} V for any V
9616 if (match(RHS, m_c_Add(m_Specific(LHS), m_Value())) &&
9618 return true;
9619
9620 // LHS u<= LHS | V for any V
9621 if (match(RHS, m_c_Or(m_Specific(LHS), m_Value())))
9622 return true;
9623
9624 // LHS u<= umax(LHS, V) for any V
9626 return true;
9627
9628 // RHS >> V u<= RHS for any V
9629 if (match(LHS, m_LShr(m_Specific(RHS), m_Value())))
9630 return true;
9631
9632 // RHS u/ C_ugt_1 u<= RHS
9633 const APInt *C;
9634 if (match(LHS, m_UDiv(m_Specific(RHS), m_APInt(C))) && C->ugt(1))
9635 return true;
9636
9637 // RHS & V u<= RHS for any V
9639 return true;
9640
9641 // umin(RHS, V) u<= RHS for any V
9643 return true;
9644
9645 // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
9646 const Value *X;
9647 const APInt *CLHS, *CRHS;
9648 if (match(LHS, m_NUWAddLike(m_Value(X), m_APInt(CLHS))) &&
9650 return CLHS->ule(*CRHS);
9651
9652 return false;
9653 }
9654 }
9655}
9656
9657/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
9658/// ALHS ARHS" is true. Otherwise, return std::nullopt.
9659static std::optional<bool>
9661 const Value *ARHS, const Value *BLHS, const Value *BRHS) {
9662 switch (Pred) {
9663 default:
9664 return std::nullopt;
9665
9666 case CmpInst::ICMP_SLT:
9667 case CmpInst::ICMP_SLE:
9668 if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS) &&
9670 return true;
9671 return std::nullopt;
9672
9673 case CmpInst::ICMP_SGT:
9674 case CmpInst::ICMP_SGE:
9675 if (isTruePredicate(CmpInst::ICMP_SLE, ALHS, BLHS) &&
9677 return true;
9678 return std::nullopt;
9679
9680 case CmpInst::ICMP_ULT:
9681 case CmpInst::ICMP_ULE:
9682 if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS) &&
9684 return true;
9685 return std::nullopt;
9686
9687 case CmpInst::ICMP_UGT:
9688 case CmpInst::ICMP_UGE:
9689 if (isTruePredicate(CmpInst::ICMP_ULE, ALHS, BLHS) &&
9691 return true;
9692 return std::nullopt;
9693 }
9694}
9695
9696/// Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
9697/// Return false if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is false.
9698/// Otherwise, return std::nullopt if we can't infer anything.
9699static std::optional<bool>
9701 CmpPredicate RPred, const ConstantRange &RCR) {
9702 auto CRImpliesPred = [&](ConstantRange CR,
9703 CmpInst::Predicate Pred) -> std::optional<bool> {
9704 // If all true values for lhs and true for rhs, lhs implies rhs
9705 if (CR.icmp(Pred, RCR))
9706 return true;
9707
9708 // If there is no overlap, lhs implies not rhs
9709 if (CR.icmp(CmpInst::getInversePredicate(Pred), RCR))
9710 return false;
9711
9712 return std::nullopt;
9713 };
9714 if (auto Res = CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9715 RPred))
9716 return Res;
9717 if (LPred.hasSameSign() ^ RPred.hasSameSign()) {
9719 : LPred.dropSameSign();
9721 : RPred.dropSameSign();
9722 return CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9723 RPred);
9724 }
9725 return std::nullopt;
9726}
9727
9728/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
9729/// is true. Return false if LHS implies RHS is false. Otherwise, return
9730/// std::nullopt if we can't infer anything.
9731static std::optional<bool>
9732isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1,
9733 CmpPredicate RPred, const Value *R0, const Value *R1,
9734 const DataLayout &DL, bool LHSIsTrue) {
9735 // The rest of the logic assumes the LHS condition is true. If that's not the
9736 // case, invert the predicate to make it so.
9737 if (!LHSIsTrue)
9738 LPred = ICmpInst::getInverseCmpPredicate(LPred);
9739
9740 // We can have non-canonical operands, so try to normalize any common operand
9741 // to L0/R0.
9742 if (L0 == R1) {
9743 std::swap(R0, R1);
9744 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9745 }
9746 if (R0 == L1) {
9747 std::swap(L0, L1);
9748 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9749 }
9750 if (L1 == R1) {
9751 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
9752 if (L0 != R0 || match(L0, m_ImmConstant())) {
9753 std::swap(L0, L1);
9754 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9755 std::swap(R0, R1);
9756 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9757 }
9758 }
9759
9760 // See if we can infer anything if operand-0 matches and we have at least one
9761 // constant.
9762 const APInt *Unused;
9763 if (L0 == R0 && (match(L1, m_APInt(Unused)) || match(R1, m_APInt(Unused)))) {
9764 // Potential TODO: We could also further use the constant range of L0/R0 to
9765 // further constraint the constant ranges. At the moment this leads to
9766 // several regressions related to not transforming `multi_use(A + C0) eq/ne
9767 // C1` (see discussion: D58633).
9768 SimplifyQuery SQ(DL);
9773
9774 // Even if L1/R1 are not both constant, we can still sometimes deduce
9775 // relationship from a single constant. For example X u> Y implies X != 0.
9776 if (auto R = isImpliedCondCommonOperandWithCR(LPred, LCR, RPred, RCR))
9777 return R;
9778 // If both L1/R1 were exact constant ranges and we didn't get anything
9779 // here, we won't be able to deduce this.
9780 if (match(L1, m_APInt(Unused)) && match(R1, m_APInt(Unused)))
9781 return std::nullopt;
9782 }
9783
9784 // Can we infer anything when the two compares have matching operands?
9785 if (L0 == R0 && L1 == R1)
9786 return ICmpInst::isImpliedByMatchingCmp(LPred, RPred);
9787
9788 // It only really makes sense in the context of signed comparison for "X - Y
9789 // must be positive if X >= Y and no overflow".
9790 // Take SGT as an example: L0:x > L1:y and C >= 0
9791 // ==> R0:(x -nsw y) < R1:(-C) is false
9792 CmpInst::Predicate SignedLPred = LPred.getPreferredSignedPredicate();
9793 if ((SignedLPred == ICmpInst::ICMP_SGT ||
9794 SignedLPred == ICmpInst::ICMP_SGE) &&
9795 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9796 if (match(R1, m_NonPositive()) &&
9797 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == false)
9798 return false;
9799 }
9800
9801 // Take SLT as an example: L0:x < L1:y and C <= 0
9802 // ==> R0:(x -nsw y) < R1:(-C) is true
9803 if ((SignedLPred == ICmpInst::ICMP_SLT ||
9804 SignedLPred == ICmpInst::ICMP_SLE) &&
9805 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9806 if (match(R1, m_NonNegative()) &&
9807 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == true)
9808 return true;
9809 }
9810
9811 // a - b == NonZero -> a != b
9812 // ptrtoint(a) - ptrtoint(b) == NonZero -> a != b
9813 const APInt *L1C;
9814 Value *A, *B;
9815 if (LPred == ICmpInst::ICMP_EQ && ICmpInst::isEquality(RPred) &&
9816 match(L1, m_APInt(L1C)) && !L1C->isZero() &&
9817 match(L0, m_Sub(m_Value(A), m_Value(B))) &&
9818 ((A == R0 && B == R1) || (A == R1 && B == R0) ||
9823 return RPred.dropSameSign() == ICmpInst::ICMP_NE;
9824 }
9825
9826 // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1
9827 if (L0 == R0 &&
9828 (LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) &&
9829 (RPred == ICmpInst::ICMP_ULT || RPred == ICmpInst::ICMP_UGE) &&
9830 match(L0, m_c_Add(m_Specific(L1), m_Specific(R1))))
9831 return CmpPredicate::getMatching(LPred, RPred).has_value();
9832
9833 if (auto P = CmpPredicate::getMatching(LPred, RPred))
9834 return isImpliedCondOperands(*P, L0, L1, R0, R1);
9835
9836 return std::nullopt;
9837}
9838
9839/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
9840/// is true. Return false if LHS implies RHS is false. Otherwise, return
9841/// std::nullopt if we can't infer anything.
9842static std::optional<bool>
9844 FCmpInst::Predicate RPred, const Value *R0, const Value *R1,
9845 const DataLayout &DL, bool LHSIsTrue) {
9846 // The rest of the logic assumes the LHS condition is true. If that's not the
9847 // case, invert the predicate to make it so.
9848 if (!LHSIsTrue)
9849 LPred = FCmpInst::getInversePredicate(LPred);
9850
9851 // We can have non-canonical operands, so try to normalize any common operand
9852 // to L0/R0.
9853 if (L0 == R1) {
9854 std::swap(R0, R1);
9855 RPred = FCmpInst::getSwappedPredicate(RPred);
9856 }
9857 if (R0 == L1) {
9858 std::swap(L0, L1);
9859 LPred = FCmpInst::getSwappedPredicate(LPred);
9860 }
9861 if (L1 == R1) {
9862 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
9863 if (L0 != R0 || match(L0, m_ImmConstant())) {
9864 std::swap(L0, L1);
9865 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9866 std::swap(R0, R1);
9867 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9868 }
9869 }
9870
9871 // Can we infer anything when the two compares have matching operands?
9872 if (L0 == R0 && L1 == R1) {
9873 if ((LPred & RPred) == LPred)
9874 return true;
9875 if ((LPred & ~RPred) == LPred)
9876 return false;
9877 }
9878
9879 // See if we can infer anything if operand-0 matches and we have at least one
9880 // constant.
9881 const APFloat *L1C, *R1C;
9882 if (L0 == R0 && match(L1, m_APFloat(L1C)) && match(R1, m_APFloat(R1C))) {
9883 if (std::optional<ConstantFPRange> DomCR =
9885 if (std::optional<ConstantFPRange> ImpliedCR =
9887 if (ImpliedCR->contains(*DomCR))
9888 return true;
9889 }
9890 if (std::optional<ConstantFPRange> ImpliedCR =
9892 FCmpInst::getInversePredicate(RPred), *R1C)) {
9893 if (ImpliedCR->contains(*DomCR))
9894 return false;
9895 }
9896 }
9897 }
9898
9899 return std::nullopt;
9900}
9901
9902/// Return true if LHS implies RHS is true. Return false if LHS implies RHS is
9903/// false. Otherwise, return std::nullopt if we can't infer anything. We
9904/// expect the RHS to be an icmp and the LHS to be an 'and', 'or', or a 'select'
9905/// instruction.
9906static std::optional<bool>
9908 const Value *RHSOp0, const Value *RHSOp1,
9909 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9910 // The LHS must be an 'or', 'and', or a 'select' instruction.
9911 assert((LHS->getOpcode() == Instruction::And ||
9912 LHS->getOpcode() == Instruction::Or ||
9913 LHS->getOpcode() == Instruction::Select) &&
9914 "Expected LHS to be 'and', 'or', or 'select'.");
9915
9916 assert(Depth <= MaxAnalysisRecursionDepth && "Hit recursion limit");
9917
9918 // If the result of an 'or' is false, then we know both legs of the 'or' are
9919 // false. Similarly, if the result of an 'and' is true, then we know both
9920 // legs of the 'and' are true.
9921 const Value *ALHS, *ARHS;
9922 if ((!LHSIsTrue && match(LHS, m_LogicalOr(m_Value(ALHS), m_Value(ARHS)))) ||
9923 (LHSIsTrue && match(LHS, m_LogicalAnd(m_Value(ALHS), m_Value(ARHS))))) {
9924 // FIXME: Make this non-recursion.
9925 if (std::optional<bool> Implication = isImpliedCondition(
9926 ALHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9927 return Implication;
9928 if (std::optional<bool> Implication = isImpliedCondition(
9929 ARHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9930 return Implication;
9931 return std::nullopt;
9932 }
9933 return std::nullopt;
9934}
9935
9936std::optional<bool>
9938 const Value *RHSOp0, const Value *RHSOp1,
9939 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9940 // Bail out when we hit the limit.
9942 return std::nullopt;
9943
9944 // A mismatch occurs when we compare a scalar cmp to a vector cmp, for
9945 // example.
9946 if (RHSOp0->getType()->isVectorTy() != LHS->getType()->isVectorTy())
9947 return std::nullopt;
9948
9949 assert(LHS->getType()->isIntOrIntVectorTy(1) &&
9950 "Expected integer type only!");
9951
9952 // Match not
9953 if (match(LHS, m_Not(m_Value(LHS))))
9954 LHSIsTrue = !LHSIsTrue;
9955
9956 // Both LHS and RHS are icmps.
9957 if (RHSOp0->getType()->getScalarType()->isIntOrPtrTy()) {
9958 CmpPredicate LHSPred;
9959 Value *LHSOp0, *LHSOp1;
9960 if (match(LHS, m_ICmpLike(LHSPred, m_Value(LHSOp0), m_Value(LHSOp1))))
9961 return isImpliedCondICmps(LHSPred, LHSOp0, LHSOp1, RHSPred, RHSOp0,
9962 RHSOp1, DL, LHSIsTrue);
9963 } else {
9964 assert(RHSOp0->getType()->isFPOrFPVectorTy() &&
9965 "Expected floating point type only!");
9966 if (const auto *LHSCmp = dyn_cast<FCmpInst>(LHS))
9967 return isImpliedCondFCmps(LHSCmp->getPredicate(), LHSCmp->getOperand(0),
9968 LHSCmp->getOperand(1), RHSPred, RHSOp0, RHSOp1,
9969 DL, LHSIsTrue);
9970 }
9971
9972 /// The LHS should be an 'or', 'and', or a 'select' instruction. We expect
9973 /// the RHS to be an icmp.
9974 /// FIXME: Add support for and/or/select on the RHS.
9975 if (const Instruction *LHSI = dyn_cast<Instruction>(LHS)) {
9976 if ((LHSI->getOpcode() == Instruction::And ||
9977 LHSI->getOpcode() == Instruction::Or ||
9978 LHSI->getOpcode() == Instruction::Select))
9979 return isImpliedCondAndOr(LHSI, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue,
9980 Depth);
9981 }
9982 return std::nullopt;
9983}
9984
9985std::optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS,
9986 const DataLayout &DL,
9987 bool LHSIsTrue, unsigned Depth) {
9988 // LHS ==> RHS by definition
9989 if (LHS == RHS)
9990 return LHSIsTrue;
9991
9992 // Match not
9993 bool InvertRHS = false;
9994 if (match(RHS, m_Not(m_Value(RHS)))) {
9995 if (LHS == RHS)
9996 return !LHSIsTrue;
9997 InvertRHS = true;
9998 }
9999
10000 CmpPredicate RHSPred;
10001 Value *RHSOp0, *RHSOp1;
10002 if (match(RHS, m_ICmpLike(RHSPred, m_Value(RHSOp0), m_Value(RHSOp1)))) {
10003 if (auto Implied = isImpliedCondition(LHS, RHSPred, RHSOp0, RHSOp1, DL,
10004 LHSIsTrue, Depth))
10005 return InvertRHS ? !*Implied : *Implied;
10006 return std::nullopt;
10007 }
10008 if (const FCmpInst *RHSCmp = dyn_cast<FCmpInst>(RHS)) {
10009 if (auto Implied = isImpliedCondition(
10010 LHS, RHSCmp->getPredicate(), RHSCmp->getOperand(0),
10011 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))
10012 return InvertRHS ? !*Implied : *Implied;
10013 return std::nullopt;
10014 }
10015
10017 return std::nullopt;
10018
10019 // LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS2
10020 // LHS ==> !(RHS1 && RHS2) if LHS ==> !RHS1 or LHS ==> !RHS2
10021 const Value *RHS1, *RHS2;
10022 if (match(RHS, m_LogicalOr(m_Value(RHS1), m_Value(RHS2)))) {
10023 if (std::optional<bool> Imp =
10024 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
10025 if (*Imp == true)
10026 return !InvertRHS;
10027 if (std::optional<bool> Imp =
10028 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
10029 if (*Imp == true)
10030 return !InvertRHS;
10031 }
10032 if (match(RHS, m_LogicalAnd(m_Value(RHS1), m_Value(RHS2)))) {
10033 if (std::optional<bool> Imp =
10034 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
10035 if (*Imp == false)
10036 return InvertRHS;
10037 if (std::optional<bool> Imp =
10038 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
10039 if (*Imp == false)
10040 return InvertRHS;
10041 }
10042
10043 return std::nullopt;
10044}
10045
10046// Returns a pair (Condition, ConditionIsTrue), where Condition is a branch
10047// condition dominating ContextI or nullptr, if no condition is found.
10048static std::pair<Value *, bool>
10050 if (!ContextI || !ContextI->getParent())
10051 return {nullptr, false};
10052
10053 // TODO: This is a poor/cheap way to determine dominance. Should we use a
10054 // dominator tree (eg, from a SimplifyQuery) instead?
10055 const BasicBlock *ContextBB = ContextI->getParent();
10056 const BasicBlock *PredBB = ContextBB->getSinglePredecessor();
10057 if (!PredBB)
10058 return {nullptr, false};
10059
10060 // We need a conditional branch in the predecessor.
10061 Value *PredCond;
10062 BasicBlock *TrueBB, *FalseBB;
10063 if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))
10064 return {nullptr, false};
10065
10066 // The branch should get simplified. Don't bother simplifying this condition.
10067 if (TrueBB == FalseBB)
10068 return {nullptr, false};
10069
10070 assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
10071 "Predecessor block does not point to successor?");
10072
10073 // Is this condition implied by the predecessor condition?
10074 return {PredCond, TrueBB == ContextBB};
10075}
10076
10077std::optional<bool> llvm::isImpliedByDomCondition(const Value *Cond,
10078 const Instruction *ContextI,
10079 const DataLayout &DL) {
10080 assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool");
10081 auto PredCond = getDomPredecessorCondition(ContextI);
10082 if (PredCond.first)
10083 return isImpliedCondition(PredCond.first, Cond, DL, PredCond.second);
10084 return std::nullopt;
10085}
10086
10088 const Value *LHS,
10089 const Value *RHS,
10090 const Instruction *ContextI,
10091 const DataLayout &DL) {
10092 auto PredCond = getDomPredecessorCondition(ContextI);
10093 if (PredCond.first)
10094 return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL,
10095 PredCond.second);
10096 return std::nullopt;
10097}
10098
10100 APInt &Upper, const InstrInfoQuery &IIQ,
10101 bool PreferSignedRange) {
10102 unsigned Width = Lower.getBitWidth();
10103 const APInt *C;
10104 switch (BO.getOpcode()) {
10105 case Instruction::Sub:
10106 if (match(BO.getOperand(0), m_APInt(C))) {
10107 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
10108 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
10109
10110 // If the caller expects a signed compare, then try to use a signed range.
10111 // Otherwise if both no-wraps are set, use the unsigned range because it
10112 // is never larger than the signed range. Example:
10113 // "sub nuw nsw i8 -2, x" is unsigned [0, 254] vs. signed [-128, 126].
10114 // "sub nuw nsw i8 2, x" is unsigned [0, 2] vs. signed [-125, 127].
10115 if (PreferSignedRange && HasNSW && HasNUW)
10116 HasNUW = false;
10117
10118 if (HasNUW) {
10119 // 'sub nuw c, x' produces [0, C].
10120 Upper = *C + 1;
10121 } else if (HasNSW) {
10122 if (C->isNegative()) {
10123 // 'sub nsw -C, x' produces [SINT_MIN, -C - SINT_MIN].
10125 Upper = *C - APInt::getSignedMaxValue(Width);
10126 } else {
10127 // Note that sub 0, INT_MIN is not NSW. It techically is a signed wrap
10128 // 'sub nsw C, x' produces [C - SINT_MAX, SINT_MAX].
10129 Lower = *C - APInt::getSignedMaxValue(Width);
10131 }
10132 }
10133 }
10134 break;
10135 case Instruction::Add:
10136 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
10137 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
10138 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
10139
10140 // If the caller expects a signed compare, then try to use a signed
10141 // range. Otherwise if both no-wraps are set, use the unsigned range
10142 // because it is never larger than the signed range. Example: "add nuw
10143 // nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125].
10144 if (PreferSignedRange && HasNSW && HasNUW)
10145 HasNUW = false;
10146
10147 if (HasNUW) {
10148 // 'add nuw x, C' produces [C, UINT_MAX].
10149 Lower = *C;
10150 } else if (HasNSW) {
10151 if (C->isNegative()) {
10152 // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C].
10154 Upper = APInt::getSignedMaxValue(Width) + *C + 1;
10155 } else {
10156 // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX].
10157 Lower = APInt::getSignedMinValue(Width) + *C;
10158 Upper = APInt::getSignedMaxValue(Width) + 1;
10159 }
10160 }
10161 }
10162 break;
10163
10164 case Instruction::And:
10165 if (match(BO.getOperand(1), m_APInt(C)))
10166 // 'and x, C' produces [0, C].
10167 Upper = *C + 1;
10168 // X & -X is a power of two or zero. So we can cap the value at max power of
10169 // two.
10170 if (match(BO.getOperand(0), m_Neg(m_Specific(BO.getOperand(1)))) ||
10171 match(BO.getOperand(1), m_Neg(m_Specific(BO.getOperand(0)))))
10172 Upper = APInt::getSignedMinValue(Width) + 1;
10173 break;
10174
10175 case Instruction::Or:
10176 if (match(BO.getOperand(1), m_APInt(C)))
10177 // 'or x, C' produces [C, UINT_MAX].
10178 Lower = *C;
10179 break;
10180
10181 case Instruction::AShr:
10182 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10183 // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C].
10185 Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1;
10186 } else if (match(BO.getOperand(0), m_APInt(C))) {
10187 unsigned ShiftAmount = Width - 1;
10188 if (!C->isZero() && IIQ.isExact(&BO))
10189 ShiftAmount = C->countr_zero();
10190 if (C->isNegative()) {
10191 // 'ashr C, x' produces [C, C >> (Width-1)]
10192 Lower = *C;
10193 Upper = C->ashr(ShiftAmount) + 1;
10194 } else {
10195 // 'ashr C, x' produces [C >> (Width-1), C]
10196 Lower = C->ashr(ShiftAmount);
10197 Upper = *C + 1;
10198 }
10199 }
10200 break;
10201
10202 case Instruction::LShr:
10203 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10204 // 'lshr x, C' produces [0, UINT_MAX >> C].
10205 Upper = APInt::getAllOnes(Width).lshr(*C) + 1;
10206 } else if (match(BO.getOperand(0), m_APInt(C))) {
10207 // 'lshr C, x' produces [C >> (Width-1), C].
10208 unsigned ShiftAmount = Width - 1;
10209 if (!C->isZero() && IIQ.isExact(&BO))
10210 ShiftAmount = C->countr_zero();
10211 Lower = C->lshr(ShiftAmount);
10212 Upper = *C + 1;
10213 }
10214 break;
10215
10216 case Instruction::Shl:
10217 if (match(BO.getOperand(0), m_APInt(C))) {
10218 if (IIQ.hasNoUnsignedWrap(&BO)) {
10219 // 'shl nuw C, x' produces [C, C << CLZ(C)]
10220 Lower = *C;
10221 Upper = Lower.shl(Lower.countl_zero()) + 1;
10222 } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw?
10223 if (C->isNegative()) {
10224 // 'shl nsw C, x' produces [C << CLO(C)-1, C]
10225 unsigned ShiftAmount = C->countl_one() - 1;
10226 Lower = C->shl(ShiftAmount);
10227 Upper = *C + 1;
10228 } else {
10229 // 'shl nsw C, x' produces [C, C << CLZ(C)-1]
10230 unsigned ShiftAmount = C->countl_zero() - 1;
10231 Lower = *C;
10232 Upper = C->shl(ShiftAmount) + 1;
10233 }
10234 } else {
10235 // If lowbit is set, value can never be zero.
10236 if ((*C)[0])
10237 Lower = APInt::getOneBitSet(Width, 0);
10238 // If we are shifting a constant the largest it can be is if the longest
10239 // sequence of consecutive ones is shifted to the highbits (breaking
10240 // ties for which sequence is higher). At the moment we take a liberal
10241 // upper bound on this by just popcounting the constant.
10242 // TODO: There may be a bitwise trick for it longest/highest
10243 // consecutative sequence of ones (naive method is O(Width) loop).
10244 Upper = APInt::getHighBitsSet(Width, C->popcount()) + 1;
10245 }
10246 } else if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10247 Upper = APInt::getBitsSetFrom(Width, C->getZExtValue()) + 1;
10248 }
10249 break;
10250
10251 case Instruction::SDiv:
10252 if (match(BO.getOperand(1), m_APInt(C))) {
10253 APInt IntMin = APInt::getSignedMinValue(Width);
10254 APInt IntMax = APInt::getSignedMaxValue(Width);
10255 if (C->isAllOnes()) {
10256 // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX]
10257 // where C != -1 and C != 0 and C != 1
10258 Lower = IntMin + 1;
10259 Upper = IntMax + 1;
10260 } else if (C->countl_zero() < Width - 1) {
10261 // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C]
10262 // where C != -1 and C != 0 and C != 1
10263 Lower = IntMin.sdiv(*C);
10264 Upper = IntMax.sdiv(*C);
10265 if (Lower.sgt(Upper))
10267 Upper = Upper + 1;
10268 assert(Upper != Lower && "Upper part of range has wrapped!");
10269 }
10270 } else if (match(BO.getOperand(0), m_APInt(C))) {
10271 if (C->isMinSignedValue()) {
10272 // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2].
10273 Lower = *C;
10274 Upper = Lower.lshr(1) + 1;
10275 } else {
10276 // 'sdiv C, x' produces [-|C|, |C|].
10277 Upper = C->abs() + 1;
10278 Lower = (-Upper) + 1;
10279 }
10280 }
10281 break;
10282
10283 case Instruction::UDiv:
10284 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
10285 // 'udiv x, C' produces [0, UINT_MAX / C].
10286 Upper = APInt::getMaxValue(Width).udiv(*C) + 1;
10287 } else if (match(BO.getOperand(0), m_APInt(C))) {
10288 // 'udiv C, x' produces [0, C].
10289 Upper = *C + 1;
10290 }
10291 break;
10292
10293 case Instruction::SRem:
10294 if (match(BO.getOperand(1), m_APInt(C))) {
10295 // 'srem x, C' produces (-|C|, |C|).
10296 Upper = C->abs();
10297 Lower = (-Upper) + 1;
10298 } else if (match(BO.getOperand(0), m_APInt(C))) {
10299 if (C->isNegative()) {
10300 // 'srem -|C|, x' produces [-|C|, 0].
10301 Upper = 1;
10302 Lower = *C;
10303 } else {
10304 // 'srem |C|, x' produces [0, |C|].
10305 Upper = *C + 1;
10306 }
10307 }
10308 break;
10309
10310 case Instruction::URem:
10311 if (match(BO.getOperand(1), m_APInt(C)))
10312 // 'urem x, C' produces [0, C).
10313 Upper = *C;
10314 else if (match(BO.getOperand(0), m_APInt(C)))
10315 // 'urem C, x' produces [0, C].
10316 Upper = *C + 1;
10317 break;
10318
10319 default:
10320 break;
10321 }
10322}
10323
10325 bool UseInstrInfo) {
10326 unsigned Width = II.getType()->getScalarSizeInBits();
10327 const APInt *C;
10328 switch (II.getIntrinsicID()) {
10329 case Intrinsic::ctlz:
10330 case Intrinsic::cttz: {
10331 APInt Upper(Width, Width);
10332 if (!UseInstrInfo || !match(II.getArgOperand(1), m_One()))
10333 Upper += 1;
10334 // Maximum of set/clear bits is the bit width.
10336 }
10337 case Intrinsic::ctpop:
10338 // Maximum of set/clear bits is the bit width.
10340 APInt(Width, Width) + 1);
10341 case Intrinsic::uadd_sat:
10342 // uadd.sat(x, C) produces [C, UINT_MAX].
10343 if (match(II.getOperand(0), m_APInt(C)) ||
10344 match(II.getOperand(1), m_APInt(C)))
10346 break;
10347 case Intrinsic::sadd_sat:
10348 if (match(II.getOperand(0), m_APInt(C)) ||
10349 match(II.getOperand(1), m_APInt(C))) {
10350 if (C->isNegative())
10351 // sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)].
10353 APInt::getSignedMaxValue(Width) + *C +
10354 1);
10355
10356 // sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].
10358 APInt::getSignedMaxValue(Width) + 1);
10359 }
10360 break;
10361 case Intrinsic::usub_sat:
10362 // usub.sat(C, x) produces [0, C].
10363 if (match(II.getOperand(0), m_APInt(C)))
10364 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
10365
10366 // usub.sat(x, C) produces [0, UINT_MAX - C].
10367 if (match(II.getOperand(1), m_APInt(C)))
10369 APInt::getMaxValue(Width) - *C + 1);
10370 break;
10371 case Intrinsic::ssub_sat:
10372 if (match(II.getOperand(0), m_APInt(C))) {
10373 if (C->isNegative())
10374 // ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)].
10376 *C - APInt::getSignedMinValue(Width) +
10377 1);
10378
10379 // ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].
10381 APInt::getSignedMaxValue(Width) + 1);
10382 } else if (match(II.getOperand(1), m_APInt(C))) {
10383 if (C->isNegative())
10384 // ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]:
10386 APInt::getSignedMaxValue(Width) + 1);
10387
10388 // ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].
10390 APInt::getSignedMaxValue(Width) - *C +
10391 1);
10392 }
10393 break;
10394 case Intrinsic::umin:
10395 case Intrinsic::umax:
10396 case Intrinsic::smin:
10397 case Intrinsic::smax:
10398 if (!match(II.getOperand(0), m_APInt(C)) &&
10399 !match(II.getOperand(1), m_APInt(C)))
10400 break;
10401
10402 switch (II.getIntrinsicID()) {
10403 case Intrinsic::umin:
10404 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
10405 case Intrinsic::umax:
10407 case Intrinsic::smin:
10409 *C + 1);
10410 case Intrinsic::smax:
10412 APInt::getSignedMaxValue(Width) + 1);
10413 default:
10414 llvm_unreachable("Must be min/max intrinsic");
10415 }
10416 break;
10417 case Intrinsic::abs:
10418 // If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX],
10419 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10420 if (match(II.getOperand(1), m_One()))
10422 APInt::getSignedMaxValue(Width) + 1);
10423
10425 APInt::getSignedMinValue(Width) + 1);
10426 case Intrinsic::vscale:
10427 if (!II.getParent() || !II.getFunction())
10428 break;
10429 return getVScaleRange(II.getFunction(), Width);
10430 default:
10431 break;
10432 }
10433
10434 return ConstantRange::getFull(Width);
10435}
10436
10438 const InstrInfoQuery &IIQ) {
10439 unsigned BitWidth = SI.getType()->getScalarSizeInBits();
10440 const Value *LHS = nullptr, *RHS = nullptr;
10442 if (R.Flavor == SPF_UNKNOWN)
10443 return ConstantRange::getFull(BitWidth);
10444
10445 if (R.Flavor == SelectPatternFlavor::SPF_ABS) {
10446 // If the negation part of the abs (in RHS) has the NSW flag,
10447 // then the result of abs(X) is [0..SIGNED_MAX],
10448 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10449 if (match(RHS, m_Neg(m_Specific(LHS))) &&
10453
10456 }
10457
10458 if (R.Flavor == SelectPatternFlavor::SPF_NABS) {
10459 // The result of -abs(X) is <= 0.
10461 APInt(BitWidth, 1));
10462 }
10463
10464 const APInt *C;
10465 if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C)))
10466 return ConstantRange::getFull(BitWidth);
10467
10468 switch (R.Flavor) {
10469 case SPF_UMIN:
10471 case SPF_UMAX:
10473 case SPF_SMIN:
10475 *C + 1);
10476 case SPF_SMAX:
10479 default:
10480 return ConstantRange::getFull(BitWidth);
10481 }
10482}
10483
10485 // The maximum representable value of a half is 65504. For floats the maximum
10486 // value is 3.4e38 which requires roughly 129 bits.
10487 unsigned BitWidth = I->getType()->getScalarSizeInBits();
10488 if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy())
10489 return;
10490 if (isa<FPToSIInst>(I) && BitWidth >= 17) {
10491 Lower = APInt(BitWidth, -65504, true);
10492 Upper = APInt(BitWidth, 65505);
10493 }
10494
10495 if (isa<FPToUIInst>(I) && BitWidth >= 16) {
10496 // For a fptoui the lower limit is left as 0.
10497 Upper = APInt(BitWidth, 65505);
10498 }
10499}
10500
10502 const SimplifyQuery &SQ,
10503 unsigned Depth) {
10504 assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction");
10505
10507 return ConstantRange::getFull(V->getType()->getScalarSizeInBits());
10508
10509 if (auto *C = dyn_cast<Constant>(V))
10510 return C->toConstantRange();
10511
10512 unsigned BitWidth = V->getType()->getScalarSizeInBits();
10513 ConstantRange CR = ConstantRange::getFull(BitWidth);
10514 if (auto *BO = dyn_cast<BinaryOperator>(V)) {
10515 APInt Lower = APInt(BitWidth, 0);
10516 APInt Upper = APInt(BitWidth, 0);
10517 // TODO: Return ConstantRange.
10518 setLimitsForBinOp(*BO, Lower, Upper, SQ.IIQ, ForSigned);
10520 } else if (auto *II = dyn_cast<IntrinsicInst>(V))
10522 else if (auto *SI = dyn_cast<SelectInst>(V)) {
10523 ConstantRange CRTrue =
10524 computeConstantRange(SI->getTrueValue(), ForSigned, SQ, Depth + 1);
10525 ConstantRange CRFalse =
10526 computeConstantRange(SI->getFalseValue(), ForSigned, SQ, Depth + 1);
10527 CR = CRTrue.unionWith(CRFalse);
10529 } else if (auto *TI = dyn_cast<TruncInst>(V)) {
10530 ConstantRange SrcCR =
10531 computeConstantRange(TI->getOperand(0), ForSigned, SQ, Depth + 1);
10532 CR = SrcCR.truncate(BitWidth);
10533 } else if (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {
10534 APInt Lower = APInt(BitWidth, 0);
10535 APInt Upper = APInt(BitWidth, 0);
10536 // TODO: Return ConstantRange.
10539 } else if (const auto *A = dyn_cast<Argument>(V))
10540 if (std::optional<ConstantRange> Range = A->getRange())
10541 CR = *Range;
10542
10543 if (auto *I = dyn_cast<Instruction>(V)) {
10544 if (auto *Range = SQ.IIQ.getMetadata(I, LLVMContext::MD_range))
10546
10547 Value *FrexpSrc;
10548 if (const auto *CB = dyn_cast<CallBase>(V)) {
10549 if (std::optional<ConstantRange> Range = CB->getRange())
10550 CR = CR.intersectWith(*Range);
10552 m_Value(FrexpSrc))))) {
10553 const fltSemantics &FltSem =
10554 FrexpSrc->getType()->getScalarType()->getFltSemantics();
10555 // It should be possible to implement this for any type, but this logic
10556 // only computes the range assuming standard subnormal handling.
10557 if (APFloat::isIEEELikeFP(FltSem)) {
10559 FrexpSrc, fcSubnormal | fcZero | fcNan | fcInf, SQ, Depth + 1);
10560
10561 // The exponent of frexp(NaN) and frexp(Inf) is unspecified. Only
10562 // constrain its range when the source can be neither.
10563 if (KnownSrc.isKnownNeverInfOrNaN()) {
10564 int MinExp = APFloat::semanticsMinExponent(FltSem) + 1;
10565
10566 // Offset to find the true minimum exponent value for a denormal.
10567 if (!KnownSrc.isKnownNeverSubnormal())
10568 MinExp -= (APFloat::semanticsPrecision(FltSem) - 1);
10569
10570 int MaxExp = APFloat::semanticsMaxExponent(FltSem) + 1;
10571
10572 auto [AdjustedMin, AdjustedMax, AdjustedMaxNonZero] =
10574
10575 DenormalMode Mode = I->getFunction()->getDenormalMode(FltSem);
10576 bool NeverLogicalZero = KnownSrc.isKnownNeverLogicalZero(Mode);
10577
10578 MinExp = std::max(AdjustedMin, MinExp);
10579 MaxExp = std::min(NeverLogicalZero ? AdjustedMaxNonZero : AdjustedMax,
10580 MaxExp);
10581
10583 APInt(BitWidth, static_cast<int64_t>(MinExp), /*isSigned=*/true),
10584 APInt(BitWidth, static_cast<int64_t>(MaxExp) + 1,
10585 /*isSigned=*/true));
10586 }
10587 }
10588 }
10589 }
10590
10591 if (SQ.CxtI && SQ.AC) {
10592 // Try to restrict the range based on information from assumptions.
10593 for (auto &AssumeVH : SQ.AC->assumptionsFor(V)) {
10594 if (!AssumeVH)
10595 continue;
10596 CallInst *I = cast<CallInst>(AssumeVH);
10597 assert(I->getParent()->getParent() == SQ.CxtI->getParent()->getParent() &&
10598 "Got assumption for the wrong function!");
10599 assert(I->getIntrinsicID() == Intrinsic::assume &&
10600 "must be an assume intrinsic");
10601
10602 if (!isValidAssumeForContext(I, SQ))
10603 continue;
10604 Value *Arg = I->getArgOperand(0);
10605 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
10606 // Currently we just use information from comparisons.
10607 if (!Cmp || Cmp->getOperand(0) != V)
10608 continue;
10609 // TODO: Set "ForSigned" parameter via Cmp->isSigned()?
10610 ConstantRange RHS =
10611 computeConstantRange(Cmp->getOperand(1), /*ForSigned=*/false,
10612 SQ.getWithInstruction(I), Depth + 1);
10613 CR = CR.intersectWith(
10614 ConstantRange::makeAllowedICmpRegion(Cmp->getCmpPredicate(), RHS));
10615 }
10616 }
10617
10618 return CR;
10619}
10620
10621static void
10623 function_ref<void(Value *)> InsertAffected) {
10624 assert(V != nullptr);
10625 if (isa<Argument>(V) || isa<GlobalValue>(V)) {
10626 InsertAffected(V);
10627 } else if (auto *I = dyn_cast<Instruction>(V)) {
10628 InsertAffected(V);
10629
10630 // Peek through unary operators to find the source of the condition.
10631 Value *Op;
10633 m_Trunc(m_Value(Op))))) {
10635 InsertAffected(Op);
10636 }
10637 }
10638}
10639
10641 Value *Cond, bool IsAssume, function_ref<void(Value *)> InsertAffected) {
10642 auto AddAffected = [&InsertAffected](Value *V) {
10643 addValueAffectedByCondition(V, InsertAffected);
10644 };
10645
10646 auto AddCmpOperands = [&AddAffected, IsAssume](Value *LHS, Value *RHS) {
10647 if (IsAssume) {
10648 AddAffected(LHS);
10649 AddAffected(RHS);
10650 } else if (match(RHS, m_Constant()))
10651 AddAffected(LHS);
10652 };
10653
10654 SmallVector<Value *, 8> Worklist;
10656 Worklist.push_back(Cond);
10657 while (!Worklist.empty()) {
10658 Value *V = Worklist.pop_back_val();
10659 if (!Visited.insert(V).second)
10660 continue;
10661
10662 CmpPredicate Pred;
10663 Value *A, *B, *X;
10664
10665 if (IsAssume) {
10666 AddAffected(V);
10667 if (match(V, m_Not(m_Value(X))))
10668 AddAffected(X);
10669 }
10670
10671 if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
10672 // assume(A && B) is split to -> assume(A); assume(B);
10673 // assume(!(A || B)) is split to -> assume(!A); assume(!B);
10674 // Finally, assume(A || B) / assume(!(A && B)) generally don't provide
10675 // enough information to be worth handling (intersection of information as
10676 // opposed to union).
10677 if (!IsAssume) {
10678 Worklist.push_back(A);
10679 Worklist.push_back(B);
10680 }
10681 } else if (match(V, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
10682 bool HasRHSC = match(B, m_ConstantInt());
10683 if (ICmpInst::isEquality(Pred)) {
10684 AddAffected(A);
10685 if (IsAssume)
10686 AddAffected(B);
10687 if (HasRHSC) {
10688 Value *Y;
10689 // (X << C) or (X >>_s C) or (X >>_u C).
10690 if (match(A, m_Shift(m_Value(X), m_ConstantInt())))
10691 AddAffected(X);
10692 // (X & C) or (X | C).
10693 else if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10694 match(A, m_Or(m_Value(X), m_Value(Y)))) {
10695 AddAffected(X);
10696 AddAffected(Y);
10697 }
10698 // X - Y
10699 else if (match(A, m_Sub(m_Value(X), m_Value(Y)))) {
10700 AddAffected(X);
10701 AddAffected(Y);
10702 }
10703 }
10704 } else {
10705 AddCmpOperands(A, B);
10706 if (HasRHSC) {
10707 // Handle (A + C1) u< C2, which is the canonical form of
10708 // A > C3 && A < C4.
10710 AddAffected(X);
10711
10712 if (ICmpInst::isUnsigned(Pred)) {
10713 Value *Y;
10714 // X & Y u> C -> X >u C && Y >u C
10715 // X | Y u< C -> X u< C && Y u< C
10716 // X nuw+ Y u< C -> X u< C && Y u< C
10717 if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10718 match(A, m_Or(m_Value(X), m_Value(Y))) ||
10719 match(A, m_NUWAdd(m_Value(X), m_Value(Y)))) {
10720 AddAffected(X);
10721 AddAffected(Y);
10722 }
10723 // X nuw- Y u> C -> X u> C
10724 if (match(A, m_NUWSub(m_Value(X), m_Value())))
10725 AddAffected(X);
10726 }
10727 }
10728
10729 // Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported
10730 // by computeKnownFPClass().
10732 if (Pred == ICmpInst::ICMP_SLT && match(B, m_Zero()))
10733 InsertAffected(X);
10734 else if (Pred == ICmpInst::ICMP_SGT && match(B, m_AllOnes()))
10735 InsertAffected(X);
10736 }
10737 }
10738
10739 if (HasRHSC && match(A, m_Ctpop(m_Value(X))))
10740 AddAffected(X);
10741 } else if (match(V, m_FCmp(Pred, m_Value(A), m_Value(B)))) {
10742 AddCmpOperands(A, B);
10743
10744 // fcmp fneg(x), y
10745 // fcmp fabs(x), y
10746 // fcmp fneg(fabs(x)), y
10747 if (match(A, m_FNeg(m_Value(A))))
10748 AddAffected(A);
10749 if (match(A, m_FAbs(m_Value(A))))
10750 AddAffected(A);
10751
10753 m_Value()))) {
10754 // Handle patterns that computeKnownFPClass() support.
10755 AddAffected(A);
10756 } else if (!IsAssume && match(V, m_Trunc(m_Value(X)))) {
10757 // Assume is checked here as X is already added above for assumes in
10758 // addValueAffectedByCondition
10759 AddAffected(X);
10760 } else if (!IsAssume && match(V, m_Not(m_Value(X)))) {
10761 // Assume is checked here to avoid issues with ephemeral values
10762 Worklist.push_back(X);
10763 }
10764 }
10765}
10766
10768 // (X >> C) or/add (X & mask(C) != 0)
10769 if (const auto *BO = dyn_cast<BinaryOperator>(V)) {
10770 if (BO->getOpcode() == Instruction::Add ||
10771 BO->getOpcode() == Instruction::Or) {
10772 const Value *X;
10773 const APInt *C1, *C2;
10774 if (match(BO, m_c_BinOp(m_LShr(m_Value(X), m_APInt(C1)),
10778 m_Zero())))) &&
10779 C2->popcount() == C1->getZExtValue())
10780 return X;
10781 }
10782 }
10783 return nullptr;
10784}
10785
10787 return const_cast<Value *>(stripNullTest(const_cast<const Value *>(V)));
10788}
10789
10792 unsigned MaxCount, bool AllowUndefOrPoison) {
10795 auto Push = [&](const Value *V) -> bool {
10796 Constant *C;
10797 if (match(const_cast<Value *>(V), m_ImmConstant(C))) {
10798 if (!AllowUndefOrPoison && !isGuaranteedNotToBeUndefOrPoison(C))
10799 return false;
10800 // Check existence first to avoid unnecessary allocations.
10801 if (Constants.contains(C))
10802 return true;
10803 if (Constants.size() == MaxCount)
10804 return false;
10805 Constants.insert(C);
10806 return true;
10807 }
10808
10809 if (auto *Inst = dyn_cast<Instruction>(V)) {
10810 if (Visited.insert(Inst).second)
10811 Worklist.push_back(Inst);
10812 return true;
10813 }
10814 return false;
10815 };
10816 if (!Push(V))
10817 return false;
10818 while (!Worklist.empty()) {
10819 const Instruction *CurInst = Worklist.pop_back_val();
10820 switch (CurInst->getOpcode()) {
10821 case Instruction::Select:
10822 if (!Push(CurInst->getOperand(1)))
10823 return false;
10824 if (!Push(CurInst->getOperand(2)))
10825 return false;
10826 break;
10827 case Instruction::PHI:
10828 for (Value *IncomingValue : cast<PHINode>(CurInst)->incoming_values()) {
10829 // Fast path for recurrence PHI.
10830 if (IncomingValue == CurInst)
10831 continue;
10832 if (!Push(IncomingValue))
10833 return false;
10834 }
10835 break;
10836 default:
10837 return false;
10838 }
10839 }
10840 return true;
10841}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Register Bank Select
Rewrite undef for PHI
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis Results
This file contains the simple types necessary to represent the attributes associated with functions a...
static const Function * getParent(const Value *V)
#define X(NUM, ENUM, NAME)
Definition ELF.h:856
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")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Utilities for dealing with flags related to floating point properties and mode controls.
static Value * getCondition(Instruction *I)
Hexagon Common GEP
#define _
static MaybeAlign getAlign(Value *Ptr)
Module.h This file contains the declarations for the Module class.
static bool hasNoUnsignedWrap(BinaryOperator &I)
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
This file contains the declarations for metadata subclasses.
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
#define P(N)
PowerPC Reduce CR logical Operation
R600 Clause Merge
const SmallVectorImpl< MachineOperand > & Cond
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
std::pair< BasicBlock *, BasicBlock * > Edge
This file contains some templates that are useful if you are working with the STL at all.
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
This file contains the UndefPoisonKind enum and helper functions.
static void computeKnownFPClassFromCond(const Value *V, Value *Cond, bool CondIsTrue, const Instruction *CxtI, KnownFPClass &KnownFromContext, unsigned Depth=0)
static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero, SimplifyQuery &Q, unsigned Depth)
Try to detect a recurrence that the value of the induction variable is always a power of two (or zero...
static cl::opt< unsigned > DomConditionsMaxUses("dom-conditions-max-uses", cl::Hidden, cl::init(20))
static unsigned computeNumSignBitsVectorConstant(const Value *V, const APInt &DemandedElts, unsigned TyBits)
For vector constants, loop over the elements and find the constant with the minimum number of sign bi...
static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS, const Value *RHS)
Return true if "icmp Pred LHS RHS" is always true.
static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if V1 == (binop V2, X), where X is known non-zero.
static bool isGEPKnownNonNull(const GEPOperator *GEP, const SimplifyQuery &Q, unsigned Depth)
Test whether a GEP's result is known to be non-null.
static bool isNonEqualShl(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and the shift is nuw or nsw.
static bool isKnownNonNullFromDominatingCondition(const Value *V, const Instruction *CtxI, const DominatorTree *DT)
static const Value * getUnderlyingObjectFromInt(const Value *V)
This is the function that does the work of looking through basic ptrtoint+arithmetic+inttoptr sequenc...
static bool isNonZeroMul(const APInt &DemandedElts, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, bool NSW, bool NUW, unsigned Depth)
static bool rangeMetadataExcludesValue(const MDNode *Ranges, const APInt &Value)
Does the 'Range' metadata (which must be a valid MD_range operand list) ensure that the value it's at...
static KnownBits getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts, const KnownBits &KnownLHS, const KnownBits &KnownRHS, const SimplifyQuery &Q, unsigned Depth)
static void breakSelfRecursivePHI(const Use *U, const PHINode *PHI, Value *&ValOut, Instruction *&CtxIOut, const PHINode **PhiOut=nullptr)
static bool isNonZeroSub(const APInt &DemandedElts, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, unsigned Depth)
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR)
Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
static void addValueAffectedByCondition(Value *V, function_ref< void(Value *)> InsertAffected)
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
static void setLimitsForBinOp(const BinaryOperator &BO, APInt &Lower, APInt &Upper, const InstrInfoQuery &IIQ, bool PreferSignedRange)
static Value * lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2, Instruction::CastOps *CastOp)
Helps to match a select pattern in case of a type mismatch.
static std::pair< Value *, bool > getDomPredecessorCondition(const Instruction *ContextI)
static constexpr unsigned MaxInstrsToCheckForFree
Maximum number of instructions to check between assume and context instruction.
static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, const KnownBits &KnownVal, unsigned Depth)
static std::optional< bool > isImpliedCondFCmps(FCmpInst::Predicate LPred, const Value *L0, const Value *L1, FCmpInst::Predicate RPred, const Value *R0, const Value *R1, const DataLayout &DL, bool LHSIsTrue)
Return true if LHS implies RHS (expanded to its components as "R0 RPred R1") is true.
static bool isKnownNonEqualFromContext(const Value *V1, const Value *V2, const SimplifyQuery &Q, unsigned Depth)
static SelectPatternResult matchFastFloatClamp(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS)
Match clamp pattern for float types without care about NaNs or signed zeros.
static std::optional< bool > isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1, CmpPredicate RPred, const Value *R0, const Value *R1, const DataLayout &DL, bool LHSIsTrue)
Return true if LHS implies RHS (expanded to its components as "R0 RPred R1") is true.
static std::optional< bool > isImpliedCondCommonOperandWithCR(CmpPredicate LPred, const ConstantRange &LCR, CmpPredicate RPred, const ConstantRange &RCR)
Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
static ConstantRange getRangeForSelectPattern(const SelectInst &SI, const InstrInfoQuery &IIQ)
static void computeKnownBitsFromOperator(const Operator *I, const APInt &DemandedElts, KnownBits &Known, const SimplifyQuery &Q, unsigned Depth)
static uint64_t GetStringLengthH(const Value *V, SmallPtrSetImpl< const PHINode * > &PHIs, unsigned CharSize)
If we can compute the length of the string pointed to by the specified pointer, return 'len+1'.
static void computeKnownBitsFromShiftOperator(const Operator *I, const APInt &DemandedElts, KnownBits &Known, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth, function_ref< KnownBits(const KnownBits &, const KnownBits &, bool)> KF)
Compute known bits from a shift operator, including those with a non-constant shift amount.
static bool onlyUsedByLifetimeMarkersOrDroppableInstsHelper(const Value *V, bool AllowLifetime, bool AllowDroppable)
static std::optional< bool > isImpliedCondAndOr(const Instruction *LHS, CmpPredicate RHSPred, const Value *RHSOp0, const Value *RHSOp1, const DataLayout &DL, bool LHSIsTrue, unsigned Depth)
Return true if LHS implies RHS is true.
static std::tuple< int, int, int > computeKnownExponentRangeFromContext(const Value *V, const SimplifyQuery &Q)
Compute the minimum and maximum values (inclusive) for the exponent of V, assuming it is not nan.
static bool isSignedMinMaxClamp(const Value *Select, const Value *&In, const APInt *&CLow, const APInt *&CHigh)
static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, bool NSW, bool NUW, unsigned Depth)
static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V, unsigned Depth)
static bool isNonEqualSelect(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst, Value *&Init, Value *&OtherOp)
static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static void computeKnownBitsFromCmp(const Value *V, CmpInst::Predicate Pred, Value *LHS, Value *RHS, KnownBits &Known, const SimplifyQuery &Q)
static SelectPatternResult matchMinMaxOfMinMax(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TVal, Value *FVal, unsigned Depth)
Recognize variations of: a < c ?
static void unionWithMinMaxIntrinsicClamp(const IntrinsicInst *II, KnownBits &Known)
static void setLimitForFPToI(const Instruction *I, APInt &Lower, APInt &Upper)
static bool isSameUnderlyingObjectInLoop(const PHINode *PN, const LoopInfo *LI)
PN defines a loop-variant pointer to an object.
static bool isNonEqualPointersWithRecursiveGEP(const Value *A, const Value *B, const SimplifyQuery &Q)
static bool isSignedMinMaxIntrinsicClamp(const IntrinsicInst *II, const APInt *&CLow, const APInt *&CHigh)
static Value * lookThroughCastConst(CmpInst *CmpI, Type *SrcTy, Constant *C, Instruction::CastOps *CastOp)
static bool handleGuaranteedWellDefinedOps(const Instruction *I, const CallableT &Handle)
Enumerates all operands of I that are guaranteed to not be undef or poison.
static bool isAbsoluteValueULEOne(const Value *V)
static void computeKnownBitsFromLerpPattern(const Value *Op0, const Value *Op1, const APInt &DemandedElts, KnownBits &KnownOut, const SimplifyQuery &Q, unsigned Depth)
Try to detect the lerp pattern: a * (b - c) + c * d where a >= 0, b >= 0, c >= 0, d >= 0,...
static KnownFPClass computeKnownFPClassFromContext(const Value *V, const SimplifyQuery &Q)
static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1, bool NSW, bool NUW, const APInt &DemandedElts, KnownBits &KnownOut, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth)
static Value * getNotValue(Value *V)
If the input value is the result of a 'not' op, constant integer, or vector splat of a constant integ...
static constexpr KnownFPClass::MinMaxKind getMinMaxKind(Intrinsic::ID IID)
static unsigned ComputeNumSignBitsImpl(const Value *V, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return the number of times the sign bit of the register is replicated into the other bits.
static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp, KnownBits &Known, const SimplifyQuery &SQ, bool Invert)
static bool isKnownNonZeroFromOperator(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static bool matchOpWithOpEqZero(Value *Op0, Value *Op1)
static bool isNonZeroRecurrence(const PHINode *PN)
Try to detect a recurrence that monotonically increases/decreases from a non-zero starting value.
static SelectPatternResult matchClamp(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal)
Recognize variations of: CLAMP(v,l,h) ==> ((v) < (l) ?
static bool shiftAmountKnownInRange(const Value *ShiftAmount)
Shifts return poison if shiftwidth is larger than the bitwidth.
static bool isEphemeralValueOf(const Instruction *I, const Value *E)
static SelectPatternResult matchMinMax(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, unsigned Depth)
Match non-obvious integer minimum and maximum sequences.
static KnownBits computeKnownBitsForHorizontalOperation(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth, const function_ref< KnownBits(const KnownBits &, const KnownBits &)> KnownBitsFunc)
static bool handleGuaranteedNonPoisonOps(const Instruction *I, const CallableT &Handle)
Enumerates all operands of I that are guaranteed to not be poison.
static std::optional< std::pair< Value *, Value * > > getInvertibleOperands(const Operator *Op1, const Operator *Op2)
If the pair of operators are the same invertible function, return the the operands of the function co...
static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS)
static void computeKnownBitsFromCond(const Value *V, Value *Cond, KnownBits &Known, const SimplifyQuery &SQ, bool Invert, unsigned Depth)
static NoCommonBitsSetResult haveNoCommonBitsSetSpecialCases(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q)
static std::optional< bool > isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS, const Value *ARHS, const Value *BLHS, const Value *BRHS)
Return true if "icmp Pred BLHS BRHS" is true whenever "icmp PredALHS ARHS" is true.
static const Instruction * safeCxtI(const Value *V, const Instruction *CxtI)
static bool isNonEqualMul(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and the multiplication is nuw o...
static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero, const Value *Cond, bool CondIsTrue)
Return true if we can infer that V is known to be a power of 2 from dominating condition Cond (e....
static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW, bool NUW, const APInt &DemandedElts, KnownBits &Known, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth)
static bool matchThreeInputRecurrence(const PHINode *PN, InstTy *&Inst, Value *&Init, Value *&OtherOp0, Value *&OtherOp1)
static bool isKnownNonNaN(const Value *V, FastMathFlags FMF)
static ConstantRange getRangeForIntrinsic(const IntrinsicInst &II, bool UseInstrInfo)
static void computeKnownFPClassForFPTrunc(const Operator *Op, const APInt &DemandedElts, FPClassTest InterestedClasses, KnownFPClass &Known, const SimplifyQuery &Q, unsigned Depth)
static Value * BuildSubAggregate(Value *From, Value *To, Type *IndexedType, SmallVectorImpl< unsigned > &Idxs, unsigned IdxSkip, BasicBlock::iterator InsertBefore)
Value * RHS
Value * LHS
static LLVM_ABI ExponentType semanticsMinExponent(const fltSemantics &)
Definition APFloat.cpp:247
static LLVM_ABI ExponentType semanticsMaxExponent(const fltSemantics &)
Definition APFloat.cpp:243
static LLVM_ABI unsigned int semanticsPrecision(const fltSemantics &)
Definition APFloat.cpp:239
static LLVM_ABI bool isIEEELikeFP(const fltSemantics &)
Definition APFloat.cpp:280
LLVM_READONLY int getExactLog2Abs() const
Definition APFloat.h:1621
bool isFinite() const
Definition APFloat.h:1570
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1224
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1184
bool isInteger() const
Definition APFloat.h:1582
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:2006
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1599
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:235
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition APInt.h:1431
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition APInt.h:424
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1565
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition APInt.h:1416
unsigned popcount() const
Count the number of bits set.
Definition APInt.h:1695
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition APInt.h:207
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition APInt.h:1355
unsigned ceilLogBase2() const
Definition APInt.h:1789
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition APInt.h:1210
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition APInt.h:372
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1191
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:381
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1513
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1120
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:210
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition APInt.h:217
bool isNegative() const
Determine sign of this APInt.
Definition APInt.h:330
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:1258
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1670
LLVM_ABI APInt reverseBits() const
Definition APInt.cpp:790
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition APInt.h:1175
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1653
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition APInt.h:1623
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:220
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1084
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition APInt.h:357
unsigned logBase2() const
Definition APInt.h:1786
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:834
bool getBoolValue() const
Convert APInt to a boolean value.
Definition APInt.h:472
bool isMaxSignedValue() const
Determine if this is the largest signed value.
Definition APInt.h:406
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:335
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition APInt.h:1159
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:880
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition APInt.h:1266
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition APInt.h:1139
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition APInt.h:297
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:201
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition APInt.h:1413
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1246
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:287
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:240
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:858
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1230
an instruction to allocate memory on the stack
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator end() const
Definition ArrayRef.h:130
size_t size() const
Get the array size.
Definition ArrayRef.h:141
iterator begin() const
Definition ArrayRef.h:129
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
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:185
Class to represent array types.
This represents the llvm.assume intrinsic.
A cache of @llvm.assume calls within a function.
MutableArrayRef< ResultElem > assumptionsFor(const Value *V)
Access the list of assumptions which affect this value.
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:105
LLVM_ABI std::optional< unsigned > getVScaleRangeMax() const
Returns the maximum value for the vscale_range attribute or std::nullopt when unknown.
LLVM_ABI unsigned getVScaleRangeMin() const
Returns the minimum value for the vscale_range attribute.
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition Attributes.h:261
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator end()
Definition BasicBlock.h:474
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:461
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
InstListType::const_iterator const_iterator
Definition BasicBlock.h:171
LLVM_ABI const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
LLVM_ABI const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Definition BasicBlock.h:237
LLVM_ABI Instruction::BinaryOps getBinaryOp() const
Returns the binary operation underlying the intrinsic.
BinaryOps getOpcode() const
Definition InstrTypes.h:409
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
LLVM_ABI bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
LLVM_ABI bool isIndirectCall() const
Return true if the callsite is an indirect call.
bool onlyReadsMemory(unsigned OpNo) const
Value * getCalledOperand() const
Value * getArgOperand(unsigned i) const
LLVM_ABI Intrinsic::ID getIntrinsicID() const
Returns the intrinsic ID of the intrinsic called or Intrinsic::not_intrinsic if the called function i...
unsigned arg_size() const
This class represents a function call, abstracting a target machine's calling convention.
This is the base class for all instructions that perform data casts.
Definition InstrTypes.h:512
This class is the base class for the comparison instructions.
Definition InstrTypes.h:728
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:740
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition InstrTypes.h:743
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition InstrTypes.h:757
@ ICMP_SLT
signed less than
Definition InstrTypes.h:769
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:770
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:746
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition InstrTypes.h:755
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:744
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:745
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:764
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:763
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:767
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition InstrTypes.h:754
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition InstrTypes.h:751
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:765
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition InstrTypes.h:752
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:747
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition InstrTypes.h:749
@ ICMP_NE
not equal
Definition InstrTypes.h:762
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:768
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:766
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition InstrTypes.h:753
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition InstrTypes.h:742
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition InstrTypes.h:750
bool isSigned() const
Definition InstrTypes.h:993
static LLVM_ABI bool isEquality(Predicate pred)
Determine if this is an equals/not equals predicate.
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition InstrTypes.h:890
bool isTrueWhenEqual() const
This is just a convenience.
static bool isFPPredicate(Predicate P)
Definition InstrTypes.h:833
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition InstrTypes.h:852
Predicate getPredicate() const
Return the predicate for this instruction.
Definition InstrTypes.h:828
Predicate getFlippedStrictnessPredicate() const
For predicate of kind "is X or equal to 0" returns the predicate "is X".
Definition InstrTypes.h:956
static bool isIntPredicate(Predicate P)
Definition InstrTypes.h:839
static LLVM_ABI bool isOrdered(Predicate predicate)
Determine if the predicate is an ordered operation.
bool isUnsigned() const
Definition InstrTypes.h:999
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
static LLVM_ABI std::optional< CmpPredicate > getMatching(CmpPredicate A, CmpPredicate B)
Compares two CmpPredicates taking samesign into account and returns the canonicalized CmpPredicate if...
LLVM_ABI CmpInst::Predicate getPreferredSignedPredicate() const
Attempts to return a signed CmpInst::Predicate from the CmpPredicate.
CmpInst::Predicate dropSameSign() const
Drops samesign information.
bool hasSameSign() const
Query samesign information, for optimizations.
Conditional Branch instruction.
An array constant whose element type is a simple 1/2/4/8-byte integer, bytes or float/double,...
Definition Constants.h:865
ConstantDataSequential - A vector or array constant whose element type is a simple 1/2/4/8-byte integ...
Definition Constants.h:755
StringRef getAsString() const
If this array is isString(), then this method returns the array as a StringRef.
Definition Constants.h:831
A vector constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition Constants.h:951
static LLVM_ABI Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static LLVM_ABI Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static LLVM_ABI std::optional< ConstantFPRange > makeExactFCmpRegion(FCmpInst::Predicate Pred, const APFloat &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:420
This is the shared class of boolean and integer constants.
Definition Constants.h:87
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
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:168
This class represents a range of values.
PreferredRangeType
If represented precisely, the result of some range operations may consist of multiple disjoint ranges...
static LLVM_ABI ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
LLVM_ABI OverflowResult unsignedSubMayOverflow(const ConstantRange &Other) const
Return whether unsigned sub of the two ranges always/never overflows.
LLVM_ABI bool isAllNegative() const
Return true if all values in this range are negative.
LLVM_ABI OverflowResult unsignedAddMayOverflow(const ConstantRange &Other) const
Return whether unsigned add of the two ranges always/never overflows.
LLVM_ABI KnownBits toKnownBits() const
Return known bits for values in this range.
LLVM_ABI bool icmp(CmpInst::Predicate Pred, const ConstantRange &Other) const
Does the predicate Pred hold between ranges this and Other?
LLVM_ABI APInt getSignedMin() const
Return the smallest signed value contained in the ConstantRange.
LLVM_ABI OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const
Return whether unsigned mul of the two ranges always/never overflows.
LLVM_ABI ConstantRange truncate(uint32_t BitWidth, unsigned NoWrapKind=0) const
Return a new range in the specified integer type, which must be strictly smaller than the current typ...
LLVM_ABI bool isAllNonNegative() const
Return true if all values in this range are non-negative.
static LLVM_ABI ConstantRange makeAllowedICmpRegion(CmpInst::Predicate Pred, const ConstantRange &Other)
Produce the smallest range such that all values that may satisfy the given predicate with any value c...
LLVM_ABI ConstantRange unionWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the union of this range with another range.
static LLVM_ABI 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...
LLVM_ABI bool contains(const APInt &Val) const
Return true if the specified value is in the set.
LLVM_ABI OverflowResult signedAddMayOverflow(const ConstantRange &Other) const
Return whether signed add of the two ranges always/never overflows.
LLVM_ABI ConstantRange intersectWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the intersection of this range with another range.
LLVM_ABI APInt getSignedMax() const
Return the largest signed value contained in the ConstantRange.
OverflowResult
Represents whether an operation on the given constant range is known to always or never overflow.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
static ConstantRange getNonEmpty(APInt Lower, APInt Upper)
Create non-empty constant range with the given bounds.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
LLVM_ABI OverflowResult signedSubMayOverflow(const ConstantRange &Other) const
Return whether signed sub of the two ranges always/never overflows.
LLVM_ABI ConstantRange sub(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a subtraction of a value in this r...
This is an important base class in LLVM.
Definition Constant.h:43
static LLVM_ABI Constant * replaceUndefsWith(Constant *C, Constant *Replacement)
Try to replace undefined constant C or undefined elements in C with Replacement.
LLVM_ABI Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition Constant.h:64
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
LLVM_ABI Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
bool isLittleEndian() const
Layout endianness...
Definition DataLayout.h:217
unsigned getAddressSizeInBits(unsigned AS) const
The size in bits of an address in for the given AS.
Definition DataLayout.h:518
LLVM_ABI const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
LLVM_ABI unsigned getIndexTypeSizeInBits(Type *Ty) const
The size in bits of the index used in GEP calculation for this type.
LLVM_ABI unsigned getPointerTypeSizeInBits(Type *) const
The pointer representation size in bits for this type.
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
Definition DataLayout.h:791
ArrayRef< CondBrInst * > conditionsFor(const Value *V) const
Access the list of branches which affect this value.
DomTreeNodeBase * getIDom() const
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:151
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
This instruction extracts a struct member or array element value from an aggregate value.
ArrayRef< unsigned > getIndices() const
unsigned getNumIndices() const
static LLVM_ABI Type * getIndexedType(Type *Agg, ArrayRef< unsigned > Idxs)
Returns the type of the element that would be extracted with an extractvalue instruction with the spe...
This instruction compares its operands according to the predicate given to the constructor.
Utility class for floating point operations which can have information about relaxed accuracy require...
Definition Operator.h:202
Convenience struct for specifying and reasoning about fast-math flags.
Definition FMF.h:23
bool noSignedZeros() const
Definition FMF.h:67
bool noInfs() const
Definition FMF.h:66
void setNoSignedZeros(bool B=true)
Definition FMF.h:84
void setNoNaNs(bool B=true)
Definition FMF.h:78
bool noNaNs() const
Definition FMF.h:65
const BasicBlock & getEntryBlock() const
Definition Function.h:783
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
PointerType * getType() const
Global values are always pointers.
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this global belongs to.
Definition Globals.cpp:205
Type * getValueType() const
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
This instruction compares its operands according to the predicate given to the constructor.
CmpPredicate getSwappedCmpPredicate() const
CmpPredicate getInverseCmpPredicate() const
Predicate getFlippedSignednessPredicate() const
For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->EQ.
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
static LLVM_ABI std::optional< bool > isImpliedByMatchingCmp(CmpPredicate Pred1, CmpPredicate Pred2)
Determine if Pred1 implies Pred2 is true, false, or if nothing can be inferred about the implication,...
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.
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
LLVM_ABI bool hasNoNaNs() const LLVM_READONLY
Determine whether the no-NaNs flag is set.
LLVM_ABI bool hasNoUnsignedWrap() const LLVM_READONLY
Determine whether the no unsigned wrap flag is set.
LLVM_ABI bool hasNoSignedWrap() const LLVM_READONLY
Determine whether the no signed wrap flag is set.
bool isBinaryOp() const
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI bool isExact() const LLVM_READONLY
Determine whether the exact flag is set.
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
LLVM_ABI bool comesBefore(const Instruction *Other) const
Given an instruction Other in the same basic block as this instruction, return true if this instructi...
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
bool isUnaryOp() const
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
A wrapper class for inspecting calls to intrinsic functions.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
An instruction for reading from memory.
Value * getPointerOperand()
Align getAlign() const
Return the alignment of the access that is being performed.
bool isLoopHeader(const BlockT *BB) const
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
Metadata node.
Definition Metadata.h:1069
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1426
This is a utility class that provides an abstraction for the common functionality between Instruction...
Definition Operator.h:33
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
Definition Operator.h:43
Utility class for integer operators which may exhibit overflow - Add, Sub, Mul, and Shl.
Definition Operator.h:78
iterator_range< const_block_iterator > blocks() const
Value * getIncomingValueForBlock(const BasicBlock *BB) const
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.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A udiv, sdiv, lshr, or ashr instruction, which can be marked as "exact", indicating that no bits are ...
Definition Operator.h:156
bool isExact() const
Test whether this division is known to be exact, with zero remainder.
Definition Operator.h:175
This class represents the LLVM 'select' instruction.
const Value * getFalseValue() const
const Value * getCondition() const
const Value * getTrueValue() const
This instruction constructs a fixed permutation of two input vectors.
VectorType * getType() const
Overload to return most specific vector type.
static LLVM_ABI void getShuffleMask(const Constant *Mask, SmallVectorImpl< int > &Result)
Convert the input shuffle mask operand to a vector of integers.
size_type size() const
Definition SmallPtrSet.h:99
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void reserve(size_type N)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:597
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition DataLayout.h:743
TypeSize getElementOffset(unsigned Idx) const
Definition DataLayout.h:774
Class to represent struct types.
unsigned getNumElements() const
Random access to the elements.
Type * getElementType(unsigned N) const
Provides information about what library functions are available for the current target.
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM_ABI unsigned getIntegerBitWidth() const
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:288
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition Type.h:263
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:282
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
Definition Type.h:155
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
LLVM_ABI uint64_t getArrayNumElements() const
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:307
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:368
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:197
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition Type.h:326
bool isHalfTy() const
Return true if this is 'half', a 16-bit IEEE fp type.
Definition Type.h:144
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:232
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
Definition Type.h:158
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
Definition Type.h:285
bool isIntOrPtrTy() const
Return true if this is an integer type or a pointer type.
Definition Type.h:270
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:257
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition Type.cpp:313
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
Definition Type.h:227
LLVM_ABI const fltSemantics & getFltSemantics() const
Definition Type.cpp:106
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM_ABI unsigned getOperandNo() const
Return the operand # of this use in its User.
Definition Use.cpp:36
User * getUser() const
Returns the User that contains this Use.
Definition Use.h:61
op_range operands()
Definition User.h:267
Value * getOperand(unsigned i) const
Definition User.h:207
unsigned getNumOperands() const
Definition User.h:229
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
const Value * stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, APInt &Offset) const
This is a wrapper around stripAndAccumulateConstantOffsets with the in-bounds requirement set to fals...
Definition Value.h:727
iterator_range< user_iterator > users()
Definition Value.h:426
LLVM_ABI const Value * stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, bool AllowInvariantGroup=false, function_ref< bool(Value &Value, APInt &Offset)> ExternalAnalysis=nullptr, bool LookThroughIntToPtr=false) const
Accumulate the constant offset this value has compared to a base pointer.
const KnownBits & getKnownBits(const SimplifyQuery &Q) const
Definition WithCache.h:59
PointerType getValue() const
Definition WithCache.h:57
Represents an op.with.overflow intrinsic.
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:168
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:165
An efficient, type-erasing, non-owning reference to a callable.
TypeSize getSequentialElementStride(const DataLayout &DL) const
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:123
A range adaptor for a pair of iterators.
CallInst * Call
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
#define UINT64_MAX
Definition DataTypes.h:77
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI APInt ScaleBitMask(const APInt &A, unsigned NewBitWidth, bool MatchAllBits=false)
Splat/Merge neighboring bits to widen/narrow the bitmask represented by.
Definition APInt.cpp:3040
const APInt & umax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be unsigned.
Definition APInt.h:2294
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
SpecificConstantMatch m_ZeroInt()
Convenience matchers for specific integer values.
BinaryOp_match< SpecificConstantMatch, SrcTy, TargetOpcode::G_SUB > m_Neg(const SrcTy &&Src)
Matches a register negated by a G_SUB.
BinaryOp_match< SrcTy, SpecificConstantMatch, TargetOpcode::G_XOR, true > m_Not(const SrcTy &&Src)
Matches a register not-ed by a G_XOR.
OneUse_match< SubPat > m_OneUse(const SubPat &SP)
match_combine_or< Ty... > m_CombineOr(const Ty &...Ps)
Combine pattern matchers matching any of Ps patterns.
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
cst_pred_ty< is_lowbit_mask > m_LowBitMask()
Match an integer or vector with only the low bit(s) set.
match_bind< PHINode > m_Phi(PHINode *&PN)
Match a PHI node, capturing it if we match.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
PtrToIntSameSize_match< OpTy > m_PtrToIntSameSize(const DataLayout &DL, const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, FCmpInst > m_FCmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
auto m_c_UMax(const LHS &L, const RHS &R)
Matches a UMax 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.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWAdd(const LHS &L, const RHS &R)
auto m_PtrToIntOrAddr(const OpTy &Op)
Matches PtrToInt or PtrToAddr.
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.
BinaryOp_match< LHS, RHS, Instruction::URem > m_URem(const LHS &L, const RHS &R)
auto m_LogicalOp()
Matches either L && R or L || R where L and R are arbitrary values.
ap_match< APInt > m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
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.
cst_pred_ty< is_power2_or_zero > m_Power2OrZero()
Match an integer or vector of 0 or power-of-2 values.
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWSub(const LHS &L, const RHS &R)
specific_intval< false > m_SpecificInt(const APInt &V)
Match a specific integer value or vector with all elements equal to the value.
bool match(Val *V, const Pattern &P)
BinOpPred_match< LHS, RHS, is_idiv_op > m_IDiv(const LHS &L, const RHS &R)
Matches integer division operations.
match_bind< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
auto m_UMin(const Opnd0 &Op0, const Opnd1 &Op1)
match_deferred< 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()...
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
ap_match< APFloat > m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
CmpClass_match< LHS, RHS, ICmpInst, true > m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
auto match_fn(const Pattern &P)
A match functor that can be used as a UnaryPredicate in functional algorithms like all_of.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true > m_c_NUWAdd(const LHS &L, const RHS &R)
cstfp_pred_ty< is_finite > m_Finite()
Match a finite FP constant, i.e.
cst_pred_ty< is_nonnegative > m_NonNegative()
Match an integer or vector of non-negative values.
auto m_SMax(const Opnd0 &Op0, const Opnd1 &Op1)
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
auto m_UMax(const Opnd0 &Op0, const Opnd1 &Op1)
auto m_BasicBlock()
Match an arbitrary basic block value and ignore it.
ExtractValue_match< Ind, Val_t > m_ExtractValue(const Val_t &V)
Match a single index ExtractValue instruction.
ICmpLike_match< LHS, RHS > m_ICmpLike(CmpPredicate &Pred, const LHS &L, const RHS &R)
auto m_Value()
Match an arbitrary value and ignore it.
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.
auto m_Ctpop(const Opnd0 &Op0)
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
auto m_Constant()
Match an arbitrary Constant and ignore it.
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
cst_pred_ty< is_strictlypositive > m_StrictlyPositive()
Match an integer or vector of strictly positive values.
auto m_VScale()
Matches a call to llvm.vscale().
OverflowingBinaryOp_match< LHS, RHS, Instruction::Shl, OverflowingBinaryOperator::NoSignedWrap > m_NSWShl(const LHS &L, const RHS &R)
match_bind< WithOverflowInst > m_WithOverflowInst(WithOverflowInst *&I)
Match a with overflow intrinsic, capturing it if we match.
SpecificCmpClass_match< LHS, RHS, ICmpInst > m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
auto m_Ctlz(const Opnd0 &Op0, const Opnd1 &Op1)
match_combine_or< FMaxMin_match< LHS, RHS, ofmin_pred_ty >, FMaxMin_match< LHS, RHS, ufmin_pred_ty > > m_OrdOrUnordFMin(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point minimum function.
BinaryOp_match< LHS, RHS, Instruction::UDiv > m_UDiv(const LHS &L, const RHS &R)
match_immconstant_ty m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
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.
match_combine_or< BinaryOp_match< LHS, RHS, Instruction::Add >, DisjointOr_match< LHS, RHS > > m_AddLike(const LHS &L, const RHS &R)
Match either "add" or "or disjoint".
CastOperator_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
auto m_Intrinsic(const Ts &...Ops)
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
auto m_c_MaxOrMin(const LHS &L, const RHS &R)
cstfp_pred_ty< custom_checkfn< APFloat > > m_CheckedFp(function_ref< bool(const APFloat &)> CheckFn)
Match a float or vector where CheckFn(ele) for each element is true.
auto m_FMinNum(const Opnd0 &Op0, const Opnd1 &Op1)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWSub(const LHS &L, const RHS &R)
auto m_SMin(const Opnd0 &Op0, const Opnd1 &Op1)
auto m_FAbs(const Opnd0 &Op0)
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap >, DisjointOr_match< LHS, RHS > > m_NSWAddLike(const LHS &L, const RHS &R)
Match either "add nsw" or "or disjoint".
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
match_combine_or< FMaxMin_match< LHS, RHS, ofmax_pred_ty >, FMaxMin_match< LHS, RHS, ufmax_pred_ty > > m_OrdOrUnordFMax(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point maximum function.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap > m_NSWAdd(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, SExtInst > > m_ZExtOrSExt(const OpTy &Op)
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match 'fneg X' as 'fsub -0.0, X'.
BinOpPred_match< LHS, RHS, is_shift_op > m_Shift(const LHS &L, const RHS &R)
Matches shift operations.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
BinOpPred_match< LHS, RHS, is_irem_op > m_IRem(const LHS &L, const RHS &R)
Matches integer remainder operations.
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
brc_match< Cond_t, match_bind< BasicBlock >, match_bind< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
auto m_c_UMin(const LHS &L, const RHS &R)
Matches a UMin with LHS and RHS in either order.
auto m_c_SMax(const LHS &L, const RHS &R)
Matches an SMax with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
auto m_FMaxNum(const Opnd0 &Op0, const Opnd1 &Op1)
cst_pred_ty< is_nonpositive > m_NonPositive()
Match an integer or vector of non-positive values.
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
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.
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.
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap >, DisjointOr_match< LHS, RHS > > m_NUWAddLike(const LHS &L, const RHS &R)
Match either "add nuw" or "or disjoint".
auto m_c_SMin(const LHS &L, const RHS &R)
Matches an SMin with LHS and RHS in either order.
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)
auto m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
static unsigned decodeVSEW(unsigned VSEW)
LLVM_ABI unsigned getSEWLMULRatio(unsigned SEW, VLMUL VLMul)
static constexpr unsigned RVVBitsPerBlock
initializer< Ty > init(const Ty &Val)
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:668
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI bool haveNoCommonBitsSet(const WithCache< const Value * > &LHSCache, const WithCache< const Value * > &RHSCache, const SimplifyQuery &SQ)
Return true if LHS and RHS have no common bits set.
LLVM_ABI bool mustExecuteUBIfPoisonOnPathTo(Instruction *Root, Instruction *OnPathTo, DominatorTree *DT)
Return true if undefined behavior would provable be executed on the path to OnPathTo if Root produced...
LLVM_ABI Intrinsic::ID getInverseMinMaxIntrinsic(Intrinsic::ID MinMaxID)
LLVM_ABI bool willNotFreeBetween(const Instruction *Assume, const Instruction *CtxI)
Returns true, if no instruction between Assume and CtxI may free (including through synchronization).
@ Offset
Definition DWP.cpp:573
@ Length
Definition DWP.cpp:573
@ NeverOverflows
Never overflows.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
LLVM_ABI KnownFPClass computeKnownFPClass(const Value *V, const APInt &DemandedElts, FPClassTest InterestedClasses, const SimplifyQuery &SQ, unsigned Depth=0)
Determine which floating-point classes are valid for V, and return them in KnownFPClass bit sets.
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:1739
LLVM_ABI 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,...
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1669
LLVM_ABI bool canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
LLVM_ABI bool mustTriggerUB(const Instruction *I, const SmallPtrSetImpl< const Value * > &KnownPoison)
Return true if the given instruction must trigger undefined behavior when I is executed with any oper...
LLVM_ABI bool isKnownNeverInfinity(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point scalar value is not an infinity or if the floating-point vector val...
LLVM_ABI void computeKnownBitsFromContext(const Value *V, KnownBits &Known, const SimplifyQuery &Q, unsigned Depth=0)
Merge bits known from context-dependent facts into Known.
RelativeUniformCounterPtr Values
Definition InstrProf.h:91
BundleAttr getBundleAttrFromOBU(OperandBundleUse OBU)
LLVM_ABI bool isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI)
LLVM_ABI 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.
NoCommonBitsSetResult
@ Known
Known to have no common set bits.
@ Unknown
Not known to have no common set bits.
@ OnlyIfUndefIgnored
Known to have no common set bits only if undef values are ignored.
LLVM_ABI bool isAssumeLikeIntrinsic(const Instruction *I)
Return true if it is an intrinsic that cannot be speculated but also cannot trap.
LLVM_ABI AllocaInst * findAllocaForValue(Value *V, bool OffsetZero=false)
Returns unique alloca where the value comes from, or nullptr.
LLVM_ABI APInt getMinMaxLimit(SelectPatternFlavor SPF, unsigned BitWidth)
Return the minimum or maximum constant value for the specified integer min/max flavor and type.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI bool isOnlyUsedInZeroComparison(const Instruction *CxtI)
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
LLVM_ABI bool getConstantStringInfo(const Value *V, StringRef &Str, bool TrimAtNul=true)
This function computes the length of a null-terminated C string pointed to by V.
LLVM_ABI bool onlyUsedByLifetimeMarkersOrDroppableInsts(const Value *V)
Return true if the only users of this pointer are lifetime markers or droppable instructions.
LLVM_ABI Constant * ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset)
LLVM_ABI Value * stripNullTest(Value *V)
Returns the inner value X if the expression has the form f(X) where f(X) == 0 if and only if X == 0,...
LLVM_ABI bool getUnderlyingObjectsForCodeGen(const Value *V, SmallVectorImpl< Value * > &Objects)
This is a wrapper around getUnderlyingObjects and adds support for basic ptrtoint+arithmetic+inttoptr...
LLVM_ABI std::pair< Intrinsic::ID, bool > canConvertToMinOrMaxIntrinsic(ArrayRef< Value * > VL)
Check if the values in VL are select instructions that can be converted to a min or max (vector) intr...
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ABI bool getConstantDataArrayInfo(const Value *V, ConstantDataArraySlice &Slice, unsigned ElementSize, uint64_t Offset=0)
Returns true if the value V is a pointer into a ConstantDataArray.
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition bit.h:325
LLVM_ABI bool isGuaranteedToExecuteForEveryIteration(const Instruction *I, const Loop *L)
Return true if this function can prove that the instruction I is executed for every iteration of the ...
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2208
LLVM_ABI bool isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(const CallBase *Call, bool MustPreserveOffset)
{launder,strip}.invariant.group returns pointer that aliases its argument, and it only captures point...
LLVM_ABI bool assumeBundleImpliesNonNull(const Value *Val, const Function *Context, OperandBundleUse OBU)
LLVM_ABI bool mustSuppressSpeculation(const LoadInst &LI)
Return true if speculation of the given load must be suppressed to avoid ordering or interfering with...
Definition Loads.cpp:445
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition MathExtras.h:284
gep_type_iterator gep_type_end(const User *GEP)
LLVM_ABI const Value * getArgumentAliasingToReturnedPointer(const CallBase *Call, bool MustPreserveOffset)
This function returns call pointer argument that is considered the same by aliasing rules.
int ilogb(const APFloat &Arg)
Returns the exponent of the internal representation of the APFloat.
Definition APFloat.h:1674
LLVM_ABI bool isSafeToSpeculativelyExecute(const Instruction *I, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true, bool IgnoreUBImplyingAttrs=true)
Return true if the instruction does not have any effects besides calculating the result and does not ...
LLVM_ABI Value * getSplatValue(const Value *V)
Get splat value if the input is a splat vector or return nullptr.
LLVM_ABI CmpInst::Predicate getMinMaxPred(SelectPatternFlavor SPF, bool Ordered=false)
Return the canonical comparison predicate for the specified minimum/maximum flavor.
bool isa_and_nonnull(const Y &Val)
Definition Casting.h:676
LLVM_ABI bool canIgnoreSignBitOfZero(const Use &U)
Return true if the sign bit of the FP value can be ignored by the user when the value is zero.
LLVM_ABI bool isGuaranteedNotToBeUndef(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be undef, but may be poison.
LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
std::tuple< Value *, FPClassTest, FPClassTest > fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS, FPClassTest RHSClass, bool LookThroughSrc=true)
const Value * getPointerOperand(const Value *V)
A helper function that returns the pointer operand of a load, store or GEP instruction.
LLVM_ABI bool MaskedValueIsZero(const Value *V, const APInt &Mask, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if 'V & Mask' is known to be zero.
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:204
LLVM_ABI bool isOverflowIntrinsicNoWrap(const WithOverflowInst *WO, const DominatorTree &DT)
Returns true if the arithmetic part of the WO 's result is used only along the paths control dependen...
LLVM_ABI bool matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO, Value *&Start, Value *&Step)
Attempt to match a simple first order recurrence cycle of the form: iv = phi Ty [Start,...
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
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:1746
LLVM_ABI OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ, bool IsNSW=false)
LLVM_ABI bool getShuffleDemandedElts(int SrcWidth, ArrayRef< int > Mask, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS, bool AllowUndefElts=false)
Transform a shuffle mask's output demanded element mask into demanded element masks for the 2 operand...
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:331
LLVM_ABI bool isGuard(const User *U)
Returns true iff U has semantics of a guard expressed in a form of call of llvm.experimental....
LLVM_ABI SelectPatternFlavor getInverseMinMaxFlavor(SelectPatternFlavor SPF)
Return the inverse minimum/maximum flavor of the specified flavor.
constexpr unsigned MaxAnalysisRecursionDepth
LLVM_ABI void adjustKnownBitsForSelectArm(KnownBits &Known, Value *Cond, Value *Arm, bool Invert, const SimplifyQuery &Q, unsigned Depth=0)
Adjust Known for the given select Arm to include information from the select Cond.
LLVM_ABI bool isKnownNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be negative (i.e.
LLVM_ABI NoCommonBitsSetResult getNoCommonBitsSetResult(const WithCache< const Value * > &LHSCache, const WithCache< const Value * > &RHSCache, const SimplifyQuery &SQ)
Return how strongly LHS and RHS are known to have no common set bits.
LLVM_ABI OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
SelectPatternFlavor
Specific patterns of select instructions we can match.
@ SPF_ABS
Floating point maxnum.
@ SPF_NABS
Absolute value.
@ SPF_FMAXNUM
Floating point minnum.
@ SPF_UMIN
Signed minimum.
@ SPF_UMAX
Signed maximum.
@ SPF_SMAX
Unsigned minimum.
@ SPF_UNKNOWN
@ SPF_FMINNUM
Unsigned maximum.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:279
LLVM_ABI bool impliesPoison(const Value *ValAssumedPoison, const Value *V)
Return true if V is poison given that ValAssumedPoison is already poison.
LLVM_ABI void getHorizDemandedEltsForFirstOperand(unsigned VectorBitWidth, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS)
Compute the demanded elements mask of horizontal binary operations.
LLVM_ABI SelectPatternResult getSelectPattern(CmpInst::Predicate Pred, SelectPatternNaNBehavior NaNBehavior=SPNB_NA, bool Ordered=false)
Determine the pattern for predicate X Pred Y ? X : Y.
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI bool programUndefinedIfPoison(const Instruction *Inst)
LLVM_ABI SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
LLVM_ABI bool matchSimpleBinaryIntrinsicRecurrence(const IntrinsicInst *I, PHINode *&P, Value *&Init, Value *&OtherOp)
Attempt to match a simple value-accumulating recurrence of the form: llvm.intrinsic....
LLVM_ABI bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
LLVM_ABI bool cannotBeNegativeZero(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if we can prove that the specified FP value is never equal to -0.0.
LLVM_ABI bool programUndefinedIfUndefOrPoison(const Instruction *Inst)
Return true if this function can prove that if Inst is executed and yields a poison value or undef bi...
LLVM_ABI void adjustKnownFPClassForSelectArm(KnownFPClass &Known, Value *Cond, Value *Arm, bool Invert, const SimplifyQuery &Q, unsigned Depth=0)
Adjust Known for the given select Arm to include information from the select Cond.
generic_gep_type_iterator<> gep_type_iterator
LLVM_ABI bool collectPossibleValues(const Value *V, SmallPtrSetImpl< const Constant * > &Constants, unsigned MaxCount, bool AllowUndefOrPoison=true)
Enumerates all possible immediate values of V and inserts them into the set Constants.
LLVM_ABI uint64_t GetStringLength(const Value *V, unsigned CharSize=8)
If we can compute the length of the string pointed to by the specified pointer, return 'len+1'.
LLVM_ABI OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
LLVM_ABI 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...
LLVM_ABI Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
LLVM_ABI bool canCreateUndefOrPoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
canCreateUndefOrPoison returns true if Op can create undef or poison from non-undef & non-poison oper...
LLVM_ABI bool matchSimpleTernaryIntrinsicRecurrence(const IntrinsicInst *I, PHINode *&P, Value *&Init, Value *&OtherOp0, Value *&OtherOp1)
Attempt to match a simple value-accumulating recurrence of the form: llvm.intrinsic....
LLVM_ABI EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
LLVM_ABI bool isKnownInversion(const Value *X, const Value *Y)
Return true iff:
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI bool intrinsicPropagatesPoison(Intrinsic::ID IID)
Return whether this intrinsic propagates poison for all operands.
LLVM_ABI bool isNotCrossLaneOperation(const Instruction *I)
Return true if the instruction doesn't potentially cross vector lanes.
bool includesPoison(UndefPoisonKind Kind)
Returns true if Kind includes the Poison bit.
Definition UndefPoison.h:27
LLVM_ABI bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth=0)
Return true if the given value is known to be non-zero when defined.
constexpr int PoisonMaskElem
LLVM_ABI RetainedKnowledge getKnowledgeValidInContext(const Value *V, ArrayRef< Attribute::AttrKind > AttrKinds, AssumptionCache &AC, const Instruction *CtxI, const DominatorTree *DT=nullptr)
Return a valid Knowledge associated to the Value V if its Attribute kind is in AttrKinds and the know...
LLVM_ABI bool isSafeToSpeculativelyExecuteWithOpcode(unsigned Opcode, const Instruction *Inst, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true, bool IgnoreUBImplyingAttrs=true)
This returns the same result as isSafeToSpeculativelyExecute if Opcode is the actual opcode of Inst.
LLVM_ABI bool onlyUsedByLifetimeMarkers(const Value *V)
Return true if the only users of this pointer are lifetime markers.
LLVM_ABI Intrinsic::ID getIntrinsicForCallSite(const CallBase &CB, const TargetLibraryInfo *TLI)
Map a call instruction to an intrinsic ID.
@ Other
Any other memory.
Definition ModRef.h:68
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
LLVM_ABI const Value * getUnderlyingObjectAggressive(const Value *V)
Like getUnderlyingObject(), but will try harder to find a single underlying object.
LLVM_ABI Intrinsic::ID getMinMaxIntrinsic(SelectPatternFlavor SPF)
Convert given SPF to equivalent min/max intrinsic.
LLVM_ABI SelectPatternResult matchDecomposedSelectPattern(CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, FastMathFlags FMF=FastMathFlags(), 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...
bool includesUndef(UndefPoisonKind Kind)
Returns true if Kind includes the Undef bit.
Definition UndefPoison.h:33
LLVM_ABI OverflowResult computeOverflowForSignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
LLVM_ABI bool propagatesPoison(const Use &PoisonOp)
Return true if PoisonOp's user yields poison or raises UB if its operand PoisonOp is poison.
@ Add
Sum of integers.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
LLVM_ABI ConstantRange computeConstantRangeIncludingKnownBits(const WithCache< const Value * > &V, bool ForSigned, const SimplifyQuery &SQ)
Combine constant ranges from computeConstantRange() and computeKnownBits().
SelectPatternNaNBehavior
Behavior when a floating point min/max is given one NaN and one non-NaN as input.
@ SPNB_RETURNS_NAN
NaN behavior not applicable.
@ SPNB_RETURNS_OTHER
Given one NaN input, returns the NaN.
@ SPNB_RETURNS_ANY
Given one NaN input, returns the non-NaN.
LLVM_ABI bool isKnownNonEqual(const Value *V1, const Value *V2, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the given values are known to be non-equal when defined.
DWARFExpression::Operation Op
LLVM_ABI bool isDereferenceableAndAlignedPointer(const Value *V, Type *Ty, Align Alignment, const SimplifyQuery &Q, bool IgnoreFree=false)
Returns true if V is always a dereferenceable pointer with alignment greater or equal than requested.
Definition Loads.cpp:244
LLVM_ABI 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.
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Return the number of times the sign bit of the register is replicated into the other bits.
constexpr unsigned BitWidth
LLVM_ABI KnownBits analyzeKnownBitsFromAndXorOr(const Operator *I, const KnownBits &KnownLHS, const KnownBits &KnownRHS, const SimplifyQuery &SQ, unsigned Depth=0)
Using KnownBits LHS/RHS produce the known bits for logic op (and/xor/or).
LLVM_ABI OverflowResult computeOverflowForUnsignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
LLVM_ABI bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I)
Return true if this function can prove that the instruction I will always transfer execution to one o...
LLVM_ABI bool isKnownNeverInfOrNaN(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point value can never contain a NaN or infinity.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI bool isKnownNeverNaN(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point scalar value is not a NaN or if the floating-point vector value has...
gep_type_iterator gep_type_begin(const User *GEP)
UndefPoisonKind
Enumeration to track whether we are interested in Undef, Poison, or both.
Definition UndefPoison.h:20
LLVM_ABI Value * isBytewiseValue(Value *V, const DataLayout &DL)
If the specified value can be set by repeating the same byte in memory, return the i8 value that it i...
LLVM_ABI std::optional< std::pair< CmpPredicate, Constant * > > getFlippedStrictnessPredicateAndConstant(CmpPredicate Pred, Constant *C)
Convert an integer comparison with a constant RHS into an equivalent form with the strictness flipped...
LLVM_ABI unsigned ComputeMaxSignificantBits(const Value *Op, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Get the upper bound on bit size for this Value Op as a signed integer.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1947
LLVM_ABI bool isKnownIntegral(const Value *V, const SimplifyQuery &SQ, FastMathFlags FMF)
Return true if the floating-point value V is known to be an integer value.
LLVM_ABI AssumeAlignInfo getAssumeAlignInfo(OperandBundleUse)
LLVM_ABI OverflowResult computeOverflowForUnsignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:197
LLVM_ABI bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Return true if the given value is known to have exactly one bit set when defined.
LLVM_ABI 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...
LLVM_ABI 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.
LLVM_ABI void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, KnownBits &Known)
Compute known bits from the range metadata.
LLVM_ABI Value * FindInsertedValue(Value *V, ArrayRef< unsigned > idx_range, std::optional< BasicBlock::iterator > InsertBefore=std::nullopt)
Given an aggregate and an sequence of indices, see if the scalar value indexed is already around as a...
LLVM_ABI bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW=false, bool AllowPoison=true)
Return true if the two given values are negation.
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
LLVM_ABI bool isKnownPositive(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be positive (i.e.
LLVM_ABI 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...
LLVM_ABI bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the give value is known to be non-negative.
LLVM_ABI bool cannotBeOrderedLessThanZero(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if we can prove that the specified FP value is either NaN or never less than -0....
LLVM_ABI void getUnderlyingObjects(const Value *V, SmallVectorImpl< const Value * > &Objects, const LoopInfo *LI=nullptr, unsigned MaxLookup=MaxLookupSearchDepth)
This method is similar to getUnderlyingObject except that it can look through phi and select instruct...
LLVM_ABI bool mayHaveNonDefUseDependency(const Instruction &I)
Returns true if the result or effects of the given instructions I depend values not reachable through...
LLVM_ABI bool isTriviallyVectorizable(Intrinsic::ID ID)
Identify if the intrinsic is trivially vectorizable.
LLVM_ABI bool isIdentifiedObject(const Value *V)
Return true if this pointer refers to a distinct and identifiable object.
LLVM_ABI 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.
LLVM_ABI std::optional< bool > computeKnownFPSignBit(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return false if we can prove that the specified FP value's sign bit is 0.
LLVM_ABI bool canIgnoreSignBitOfNaN(const Use &U)
Return true if the sign bit of the FP value can be ignored by the user when the value is NaN.
LLVM_ABI ConstantRange computeConstantRange(const Value *V, bool ForSigned, const SimplifyQuery &SQ, unsigned Depth=0)
Determine the possible constant range of an integer or vector of integer value.
LLVM_ABI void findValuesAffectedByCondition(Value *Cond, bool IsAssume, function_ref< void(Value *)> InsertAffected)
Call InsertAffected on all Values whose known bits / value may be affected by the condition Cond.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:862
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
SmallPtrSet< Value *, 4 > AffectedValues
Represents offset+length into a ConstantDataArray.
const ConstantDataArray * Array
ConstantDataArray pointer.
Represent subnormal handling kind for floating point instruction inputs and outputs.
static constexpr DenormalMode getDynamic()
InstrInfoQuery provides an interface to query additional information for instructions like metadata o...
bool isExact(const BinaryOperator *Op) const
MDNode * getMetadata(const Instruction *I, unsigned KindID) const
bool hasNoSignedZeros(const InstT *Op) const
bool hasNoSignedWrap(const InstT *Op) const
bool hasNoUnsignedWrap(const InstT *Op) const
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition KnownBits.h:315
static LLVM_ABI KnownBits sadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)
KnownBits anyextOrTrunc(unsigned BitWidth) const
Return known bits for an "any" extension or truncation of the value we're tracking.
Definition KnownBits.h:190
static LLVM_ABI KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition KnownBits.h:269
static LLVM_ABI KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition KnownBits.h:106
bool isZero() const
Returns true if value is all zero.
Definition KnownBits.h:78
LLVM_ABI KnownBits blsi() const
Compute known bits for X & -X, which has only the lowest bit set of X set.
void makeNonNegative()
Make this value non-negative.
Definition KnownBits.h:125
static LLVM_ABI KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
unsigned countMinLeadingOnes() const
Returns the minimum number of leading one bits.
Definition KnownBits.h:265
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition KnownBits.h:256
static LLVM_ABI KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
static LLVM_ABI KnownBits ssub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.ssub.sat(LHS, RHS)
static LLVM_ABI KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
bool isUnknown() const
Returns true if we don't know any bits.
Definition KnownBits.h:64
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition KnownBits.h:288
LLVM_ABI KnownBits blsmsk() const
Compute known bits for X ^ (X - 1), which has all bits up to and including the lowest set bit of X se...
KnownBits byteSwap() const
Definition KnownBits.h:559
bool hasConflict() const
Returns true if there is conflicting information.
Definition KnownBits.h:51
static LLVM_ABI KnownBits fshl(const KnownBits &LHS, const KnownBits &RHS, const APInt &Amt)
Compute known bits for fshl(LHS, RHS, Amt).
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition KnownBits.h:303
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition KnownBits.h:84
KnownBits reverseBits() const
Definition KnownBits.h:563
unsigned getBitWidth() const
Get the bit width of this value.
Definition KnownBits.h:44
static LLVM_ABI KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition KnownBits.h:176
bool isConstant() const
Returns true if we know the value of all bits.
Definition KnownBits.h:54
static KnownBits add(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false, bool SelfAdd=false)
Compute knownbits resulting from addition of LHS and RHS.
Definition KnownBits.h:361
KnownBits unionWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for either this or RHS or both.
Definition KnownBits.h:335
static LLVM_ABI KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition KnownBits.h:109
bool isEven() const
Return if the value is known even (the low bit is 0).
Definition KnownBits.h:162
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition KnownBits.h:239
static LLVM_ABI KnownBits pdep(const KnownBits &Val, const KnownBits &Mask)
Compute known bits for pdep(Val, Mask).
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition KnownBits.h:325
unsigned countMinTrailingOnes() const
Returns the minimum number of trailing one bits.
Definition KnownBits.h:259
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition KnownBits.h:262
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:146
static LLVM_ABI KnownBits fshr(const KnownBits &LHS, const KnownBits &RHS, const APInt &Amt)
Compute known bits for fshr(LHS, RHS, Amt).
static LLVM_ABI KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
static LLVM_ABI KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
static LLVM_ABI KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
static LLVM_ABI KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
APInt getMinValue() const
Return the minimal unsigned value possible given these KnownBits.
Definition KnownBits.h:130
static LLVM_ABI KnownBits computeForAddSub(bool Add, bool NSW, bool NUW, const KnownBits &LHS, const KnownBits &RHS)
Compute known bits resulting from adding LHS and RHS.
Definition KnownBits.cpp:61
static LLVM_ABI KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition KnownBits.h:340
bool isNegative() const
Returns true if this value is known to be negative.
Definition KnownBits.h:103
static KnownBits sub(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from subtraction of LHS and RHS.
Definition KnownBits.h:376
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition KnownBits.h:294
void setAllOnes()
Make all bits known to be one and discard any previous information.
Definition KnownBits.h:90
static LLVM_ABI KnownBits uadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.uadd.sat(LHS, RHS)
static LLVM_ABI KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
KnownBits anyext(unsigned BitWidth) const
Return known bits for an "any" extension of the value we're tracking, where we don't know anything ab...
Definition KnownBits.h:171
static LLVM_ABI KnownBits clmul(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for clmul(LHS, RHS).
LLVM_ABI KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
static LLVM_ABI std::optional< bool > sgt(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SGT result.
static LLVM_ABI std::optional< bool > uge(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_UGE result.
static LLVM_ABI KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
static LLVM_ABI KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
static LLVM_ABI KnownBits pext(const KnownBits &Val, const KnownBits &Mask)
Compute known bits for pext(Val, Mask).
KnownBits sextOrTrunc(unsigned BitWidth) const
Return known bits for a sign extension or truncation of the value we're tracking.
Definition KnownBits.h:210
bool isKnownNeverInfOrNaN() const
Return true if it's known this can never be an infinity or nan.
FPClassTest KnownFPClasses
Floating-point classes the value could be one of.
bool isKnownNeverInfinity() const
Return true if it's known this can never be an infinity.
bool cannotBeOrderedGreaterThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never greater tha...
static LLVM_ABI KnownFPClass sin(const KnownFPClass &Src)
Report known values for sin.
static LLVM_ABI KnownFPClass fdiv_self(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fdiv x, x.
static constexpr FPClassTest OrderedGreaterThanZeroMask
static constexpr FPClassTest OrderedLessThanZeroMask
void knownNot(FPClassTest RuleOut)
static LLVM_ABI KnownFPClass fmul(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fmul.
static LLVM_ABI KnownFPClass fadd_self(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fadd x, x.
static KnownFPClass square(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
static LLVM_ABI KnownFPClass fsub(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fsub.
bool isKnownNeverSubnormal() const
Return true if it's known this can never be a subnormal.
KnownFPClass unionWith(const KnownFPClass &RHS) const
static LLVM_ABI KnownFPClass canonicalize(const KnownFPClass &Src, DenormalMode DenormMode=DenormalMode::getDynamic())
Apply the canonicalize intrinsic to this value.
LLVM_ABI bool isKnownNeverLogicalZero(DenormalMode Mode) const
Return true if it's known this can never be interpreted as a zero.
static LLVM_ABI KnownFPClass log(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for log/log2/log10.
static LLVM_ABI KnownFPClass atan(const KnownFPClass &Src)
Report known values for atan.
static LLVM_ABI KnownFPClass atan2(const KnownFPClass &LHS, const KnownFPClass &RHS)
Report known values for atan2.
static LLVM_ABI KnownFPClass fdiv(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fdiv.
static LLVM_ABI KnownFPClass roundToIntegral(const KnownFPClass &Src, bool IsTrunc, bool IsMultiUnitFPType)
Propagate known class for rounding intrinsics (trunc, floor, ceil, rint, nearbyint,...
static LLVM_ABI KnownFPClass cos(const KnownFPClass &Src)
Report known values for cos.
static LLVM_ABI KnownFPClass cosh(const KnownFPClass &Src)
Report known values for cosh.
static LLVM_ABI KnownFPClass minMaxLike(const KnownFPClass &LHS, const KnownFPClass &RHS, MinMaxKind Kind, DenormalMode DenormMode=DenormalMode::getDynamic())
bool isUnknown() const
static LLVM_ABI KnownFPClass exp(const KnownFPClass &Src)
Report known values for exp, exp2 and exp10.
static LLVM_ABI KnownFPClass frexp_mant(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for mantissa component of frexp.
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 ...
static LLVM_ABI KnownFPClass asin(const KnownFPClass &Src)
Report known values for asin.
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.
static LLVM_ABI KnownFPClass fpext(const KnownFPClass &KnownSrc, const fltSemantics &DstTy, const fltSemantics &SrcTy)
Propagate known class for fpext.
static LLVM_ABI KnownFPClass fma(const KnownFPClass &LHS, const KnownFPClass &RHS, const KnownFPClass &Addend, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fma.
static LLVM_ABI KnownFPClass tan(const KnownFPClass &Src)
Report known values for tan.
static LLVM_ABI KnownFPClass fptrunc(const KnownFPClass &KnownSrc)
Propagate known class for fptrunc.
bool cannotBeOrderedLessThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never less than -...
void signBitMustBeOne()
Assume the sign bit is one.
void signBitMustBeZero()
Assume the sign bit is zero.
static LLVM_ABI KnownFPClass sqrt(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for sqrt.
LLVM_ABI bool isKnownNeverLogicalPosZero(DenormalMode Mode) const
Return true if it's known this can never be interpreted as a positive zero.
bool isKnownNeverPosInfinity() const
Return true if it's known this can never be +infinity.
static LLVM_ABI KnownFPClass fadd(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fadd.
LLVM_ABI bool isKnownNeverLogicalNegZero(DenormalMode Mode) const
Return true if it's known this can never be interpreted as a negative zero.
static LLVM_ABI KnownFPClass bitcast(const fltSemantics &FltSemantics, const KnownBits &Bits)
Report known values for a bitcast into a float with provided semantics.
static LLVM_ABI KnownFPClass fma_square(const KnownFPClass &Squared, const KnownFPClass &Addend, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fma squared, squared, addend.
static LLVM_ABI KnownFPClass acos(const KnownFPClass &Src)
Report known values for acos.
static LLVM_ABI KnownFPClass frem_self(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for frem.
static LLVM_ABI KnownFPClass powi(const KnownFPClass &Src, const KnownBits &N)
Propagate known class for powi.
static LLVM_ABI KnownFPClass ldexp(const KnownFPClass &Src, const APInt &ConstantRangeMin, const APInt &ConstantRangeMax, const fltSemantics &Flt, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for ldexp, assuming the exponent is known to be within [ConstantRangeMin,...
static LLVM_ABI KnownFPClass sinh(const KnownFPClass &Src)
Report known values for sinh.
static LLVM_ABI KnownFPClass tanh(const KnownFPClass &Src)
Report known values for tanh.
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
SimplifyQuery getWithoutCondContext() const
const Instruction * CxtI
const DominatorTree * DT
SimplifyQuery getWithInstruction(const Instruction *I) const
AssumptionCache * AC
const DomConditionCache * DC
const InstrInfoQuery IIQ
const CondContext * CC