LLVM 24.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/APInt.h"
16#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/ScopeExit.h"
22#include "llvm/ADT/StringRef.h"
32#include "llvm/Analysis/Loads.h"
37#include "llvm/IR/Argument.h"
38#include "llvm/IR/Attributes.h"
39#include "llvm/IR/BasicBlock.h"
41#include "llvm/IR/Constant.h"
44#include "llvm/IR/Constants.h"
47#include "llvm/IR/Dominators.h"
49#include "llvm/IR/Function.h"
51#include "llvm/IR/GlobalAlias.h"
52#include "llvm/IR/GlobalValue.h"
54#include "llvm/IR/InstrTypes.h"
55#include "llvm/IR/Instruction.h"
58#include "llvm/IR/Intrinsics.h"
59#include "llvm/IR/IntrinsicsAArch64.h"
60#include "llvm/IR/IntrinsicsAMDGPU.h"
61#include "llvm/IR/IntrinsicsRISCV.h"
62#include "llvm/IR/IntrinsicsX86.h"
63#include "llvm/IR/LLVMContext.h"
64#include "llvm/IR/Metadata.h"
65#include "llvm/IR/Module.h"
66#include "llvm/IR/Operator.h"
68#include "llvm/IR/Type.h"
69#include "llvm/IR/User.h"
70#include "llvm/IR/Value.h"
80#include <algorithm>
81#include <cassert>
82#include <cstdint>
83#include <optional>
84#include <utility>
85
86using namespace llvm;
87using namespace llvm::PatternMatch;
88
89// Controls the number of uses of the value searched for possible
90// dominating comparisons.
91static cl::opt<unsigned> DomConditionsMaxUses("dom-conditions-max-uses",
92 cl::Hidden, cl::init(20));
93
94/// Maximum number of instructions to check between assume and context
95/// instruction.
96static constexpr unsigned MaxInstrsToCheckForFree = 32;
97
98template <typename InstTy>
99static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst,
100 Value *&Init, Value *&OtherOp);
101
102/// Returns the bitwidth of the given scalar or pointer type. For vector types,
103/// returns the element type's bitwidth.
104static unsigned getBitWidth(Type *Ty, const DataLayout &DL) {
105 if (unsigned BitWidth = Ty->getScalarSizeInBits())
106 return BitWidth;
107
108 return DL.getPointerTypeSizeInBits(Ty);
109}
110
111// Given the provided Value and, potentially, a context instruction, return
112// the preferred context instruction (if any).
113static const Instruction *safeCtxI(const Value *V, const Instruction *CtxI) {
114 // If we've been provided with a context instruction, then use that (provided
115 // it has been inserted).
116 if (CtxI && CtxI->getParent())
117 return CtxI;
118
119 // If the value is really an already-inserted instruction, then use that.
120 CtxI = dyn_cast<Instruction>(V);
121 if (CtxI && CtxI->getParent())
122 return CtxI;
123
124 return nullptr;
125}
126
128 const APInt &DemandedElts,
129 APInt &DemandedLHS, APInt &DemandedRHS) {
130 if (isa<ScalableVectorType>(Shuf->getType())) {
131 assert(DemandedElts == APInt(1,1));
132 DemandedLHS = DemandedRHS = DemandedElts;
133 return true;
134 }
135
136 int NumElts =
137 cast<FixedVectorType>(Shuf->getOperand(0)->getType())->getNumElements();
138 return llvm::getShuffleDemandedElts(NumElts, Shuf->getShuffleMask(),
139 DemandedElts, DemandedLHS, DemandedRHS);
140}
141
142static void computeKnownBits(const Value *V, const APInt &DemandedElts,
143 KnownBits &Known, const SimplifyQuery &Q,
144 unsigned Depth);
145
147 const SimplifyQuery &Q, unsigned Depth) {
148 // Since the number of lanes in a scalable vector is unknown at compile time,
149 // we track one bit which is implicitly broadcast to all lanes. This means
150 // that all lanes in a scalable vector are considered demanded.
151 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
152 APInt DemandedElts =
153 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
154 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);
155}
156
158 const DataLayout &DL, AssumptionCache *AC,
159 const Instruction *CtxI, const DominatorTree *DT,
160 bool UseInstrInfo, unsigned Depth) {
162 SimplifyQuery(DL, DT, AC, safeCtxI(V, CtxI), UseInstrInfo),
163 Depth);
164}
165
167 AssumptionCache *AC, const Instruction *CtxI,
168 const DominatorTree *DT, bool UseInstrInfo,
169 unsigned Depth) {
170 return computeKnownBits(
171 V, SimplifyQuery(DL, DT, AC, safeCtxI(V, CtxI), UseInstrInfo), Depth);
172}
173
176 const SimplifyQuery &SQ) {
177 // Look for an inverted mask: (X & ~M) op (Y & M).
178 {
179 Value *M;
180 if (match(LHS, m_c_And(m_Not(m_Value(M)), m_Value())) &&
182 return isGuaranteedNotToBeUndef(M, SQ.AC, SQ.CtxI, SQ.DT)
185 }
186
187 // X op (Y & ~X)
189 return isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CtxI, SQ.DT)
192
193 // X op ((X & Y) ^ Y) -- this is the canonical form of the previous pattern
194 // for constant Y.
195 Value *Y;
196 if (match(RHS,
198 bool IsNoUndef = isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CtxI, SQ.DT) &&
199 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CtxI, SQ.DT);
200 return IsNoUndef ? NoCommonBitsSetResult::Known
202 }
203
204 // Peek through extends to find a 'not' of the other side:
205 // (ext Y) op ext(~Y)
206 if (match(LHS, m_ZExtOrSExt(m_Value(Y))) &&
208 return isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CtxI, SQ.DT)
211
212 // Look for: (A & B) op ~(A | B)
213 {
214 Value *A, *B;
215 if (match(LHS, m_And(m_Value(A), m_Value(B))) &&
217 bool IsNoUndef = isGuaranteedNotToBeUndef(A, SQ.AC, SQ.CtxI, SQ.DT) &&
218 isGuaranteedNotToBeUndef(B, SQ.AC, SQ.CtxI, SQ.DT);
219 return IsNoUndef ? NoCommonBitsSetResult::Known
221 }
222 }
223
224 // Look for: (X << V) op (Y >> (BitWidth - V))
225 // or (X >> V) op (Y << (BitWidth - V))
226 {
227 const Value *V;
228 const APInt *R;
229 if (((match(RHS, m_Shl(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&
231 (match(RHS, m_LShr(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&
232 match(LHS, m_Shl(m_Value(), m_Specific(V))))) &&
233 R->uge(LHS->getType()->getScalarSizeInBits()))
235 }
236
238}
239
242 const WithCache<const Value *> &RHSCache,
243 const SimplifyQuery &SQ) {
244 const Value *LHS = LHSCache.getValue();
245 const Value *RHS = RHSCache.getValue();
246
247 assert(LHS->getType() == RHS->getType() &&
248 "LHS and RHS should have the same type");
249 assert(LHS->getType()->isIntOrIntVectorTy() &&
250 "LHS and RHS should be integers");
251
253 if (Result == NoCommonBitsSetResult::Known)
255
256 NoCommonBitsSetResult CommuteResult =
258 if (CommuteResult == NoCommonBitsSetResult::Known)
260
262 RHSCache.getKnownBits(SQ)))
264
268
270}
271
273 const WithCache<const Value *> &RHSCache,
274 const SimplifyQuery &SQ) {
275 NoCommonBitsSetResult Result =
276 getNoCommonBitsSetResult(LHSCache, RHSCache, SQ);
277 return Result == NoCommonBitsSetResult::Known;
278}
279
281 return !I->user_empty() &&
282 all_of(I->users(), match_fn(m_ICmp(m_Value(), m_Zero())));
283}
284
286 return !I->user_empty() && all_of(I->users(), [](const User *U) {
287 CmpPredicate P;
288 return match(U, m_ICmp(P, m_Value(), m_Zero())) && ICmpInst::isEquality(P);
289 });
290}
291
293 bool OrZero, AssumptionCache *AC,
294 const Instruction *CtxI,
295 const DominatorTree *DT, bool UseInstrInfo,
296 unsigned Depth) {
297 return ::isKnownToBeAPowerOfTwo(
298 V, OrZero, SimplifyQuery(DL, DT, AC, safeCtxI(V, CtxI), UseInstrInfo),
299 Depth);
300}
301
302static bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
303 const SimplifyQuery &Q, unsigned Depth);
304
306 unsigned Depth) {
307 return computeKnownBits(V, SQ, Depth).isNonNegative();
308}
309
311 const SimplifyQuery &SQ, unsigned Depth) {
312 if (auto *CI = dyn_cast<ConstantInt>(V.getValue()))
313 return CI->getValue().isStrictlyPositive();
314
315 // If `isKnownNonNegative` ever becomes more sophisticated, make sure to keep
316 // this updated.
317 KnownBits Known = Depth == 0 ? V.getKnownBits(SQ)
318 : computeKnownBits(V.getValue(), SQ, Depth);
319 return Known.isNonNegative() &&
320 (Known.isNonZero() || isKnownNonZero(V.getValue(), SQ, Depth));
321}
322
324 const SimplifyQuery &SQ, unsigned Depth) {
325 if (Depth == 0)
326 return V.getKnownBits(SQ).isNegative();
327 return computeKnownBits(V.getValue(), SQ, Depth).isNegative();
328}
329
330static bool isKnownNonEqual(const Value *V1, const Value *V2,
331 const APInt &DemandedElts, const SimplifyQuery &Q,
332 unsigned Depth);
333
335 const Value *RHS);
336
337bool llvm::isKnownNonEqual(const Value *V1, const Value *V2,
338 const SimplifyQuery &Q, unsigned Depth) {
339 // We don't support looking through casts.
340 if (V1 == V2 || V1->getType() != V2->getType())
341 return false;
342 auto *FVTy = dyn_cast<FixedVectorType>(V1->getType());
343 APInt DemandedElts =
344 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
345 return ::isKnownNonEqual(V1, V2, DemandedElts, Q, Depth);
346}
347
348bool llvm::MaskedValueIsZero(const Value *V, const APInt &Mask,
349 const SimplifyQuery &SQ, unsigned Depth) {
350 KnownBits Known(Mask.getBitWidth());
352 return Mask.isSubsetOf(Known.Zero);
353}
354
355static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
356 const SimplifyQuery &Q, unsigned Depth);
357
358static unsigned ComputeNumSignBits(const Value *V, const SimplifyQuery &Q,
359 unsigned Depth = 0) {
360 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
361 APInt DemandedElts =
362 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
363 return ComputeNumSignBits(V, DemandedElts, Q, Depth);
364}
365
366unsigned llvm::ComputeNumSignBits(const Value *V, const DataLayout &DL,
367 AssumptionCache *AC, const Instruction *CtxI,
368 const DominatorTree *DT, bool UseInstrInfo,
369 unsigned Depth) {
370 return ::ComputeNumSignBits(
371 V, SimplifyQuery(DL, DT, AC, safeCtxI(V, CtxI), UseInstrInfo), Depth);
372}
373
375 AssumptionCache *AC,
376 const Instruction *CtxI,
377 const DominatorTree *DT,
378 unsigned Depth) {
379 unsigned SignBits = ComputeNumSignBits(V, DL, AC, CtxI, DT, Depth);
380 return V->getType()->getScalarSizeInBits() - SignBits + 1;
381}
382
383/// Try to detect the lerp pattern: a * (b - c) + c * d
384/// where a >= 0, b >= 0, c >= 0, d >= 0, and b >= c.
385///
386/// In that particular case, we can use the following chain of reasoning:
387///
388/// a * (b - c) + c * d <= a' * (b - c) + a' * c = a' * b where a' = max(a, d)
389///
390/// Since that is true for arbitrary a, b, c and d within our constraints, we
391/// can conclude that:
392///
393/// max(a * (b - c) + c * d) <= max(max(a), max(d)) * max(b) = U
394///
395/// Considering that any result of the lerp would be less or equal to U, it
396/// would have at least the number of leading 0s as in U.
397///
398/// While being quite a specific situation, it is fairly common in computer
399/// graphics in the shape of alpha blending.
400///
401/// Modifies given KnownOut in-place with the inferred information.
402static void computeKnownBitsFromLerpPattern(const Value *Op0, const Value *Op1,
403 const APInt &DemandedElts,
404 KnownBits &KnownOut,
405 const SimplifyQuery &Q,
406 unsigned Depth) {
407
408 Type *Ty = Op0->getType();
409 const unsigned BitWidth = Ty->getScalarSizeInBits();
410
411 // Only handle scalar types for now
412 if (Ty->isVectorTy())
413 return;
414
415 // Try to match: a * (b - c) + c * d.
416 // When a == 1 => A == nullptr, the same applies to d/D as well.
417 const Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
418 const Instruction *SubBC = nullptr;
419
420 const auto MatchSubBC = [&]() {
421 // (b - c) can have two forms that interest us:
422 //
423 // 1. sub nuw %b, %c
424 // 2. xor %c, %b
425 //
426 // For the first case, nuw flag guarantees our requirement b >= c.
427 //
428 // The second case might happen when the analysis can infer that b is a mask
429 // for c and we can transform sub operation into xor (that is usually true
430 // for constant b's). Even though xor is symmetrical, canonicalization
431 // ensures that the constant will be the RHS. We have additional checks
432 // later on to ensure that this xor operation is equivalent to subtraction.
434 m_Xor(m_Value(C), m_Value(B))));
435 };
436
437 const auto MatchASubBC = [&]() {
438 // Cases:
439 // - a * (b - c)
440 // - (b - c) * a
441 // - (b - c) <- a implicitly equals 1
442 return m_CombineOr(m_c_Mul(m_Value(A), MatchSubBC()), MatchSubBC());
443 };
444
445 const auto MatchCD = [&]() {
446 // Cases:
447 // - d * c
448 // - c * d
449 // - c <- d implicitly equals 1
451 };
452
453 const auto Match = [&](const Value *LHS, const Value *RHS) {
454 // We do use m_Specific(C) in MatchCD, so we have to make sure that
455 // it's bound to anything and match(LHS, MatchASubBC()) absolutely
456 // has to evaluate first and return true.
457 //
458 // If Match returns true, it is guaranteed that B != nullptr, C != nullptr.
459 return match(LHS, MatchASubBC()) && match(RHS, MatchCD());
460 };
461
462 if (!Match(Op0, Op1) && !Match(Op1, Op0))
463 return;
464
465 const auto ComputeKnownBitsOrOne = [&](const Value *V) {
466 // For some of the values we use the convention of leaving
467 // it nullptr to signify an implicit constant 1.
468 return V ? computeKnownBits(V, DemandedElts, Q, Depth + 1)
470 };
471
472 // Check that all operands are non-negative
473 const KnownBits KnownA = ComputeKnownBitsOrOne(A);
474 if (!KnownA.isNonNegative())
475 return;
476
477 const KnownBits KnownD = ComputeKnownBitsOrOne(D);
478 if (!KnownD.isNonNegative())
479 return;
480
481 const KnownBits KnownB = computeKnownBits(B, DemandedElts, Q, Depth + 1);
482 if (!KnownB.isNonNegative())
483 return;
484
485 const KnownBits KnownC = computeKnownBits(C, DemandedElts, Q, Depth + 1);
486 if (!KnownC.isNonNegative())
487 return;
488
489 // If we matched subtraction as xor, we need to actually check that xor
490 // is semantically equivalent to subtraction.
491 //
492 // For that to be true, b has to be a mask for c or that b's known
493 // ones cover all known and possible ones of c.
494 if (SubBC->getOpcode() == Instruction::Xor &&
495 !KnownC.getMaxValue().isSubsetOf(KnownB.getMinValue()))
496 return;
497
498 const APInt MaxA = KnownA.getMaxValue();
499 const APInt MaxD = KnownD.getMaxValue();
500 const APInt MaxAD = APIntOps::umax(MaxA, MaxD);
501 const APInt MaxB = KnownB.getMaxValue();
502
503 // We can't infer leading zeros info if the upper-bound estimate wraps.
504 bool Overflow;
505 const APInt UpperBound = MaxAD.umul_ov(MaxB, Overflow);
506
507 if (Overflow)
508 return;
509
510 // If we know that x <= y and both are positive than x has at least the same
511 // number of leading zeros as y.
512 const unsigned MinimumNumberOfLeadingZeros = UpperBound.countl_zero();
513 KnownOut.Zero.setHighBits(MinimumNumberOfLeadingZeros);
514}
515
516/// Infer non-negativity for R + (R < 0 ? A : 0), where R = srem X, Y.
517/// A must be a non-negative Y or abs(Y).
518static void computeKnownBitsFromEuclideanRem(const Value *Op0, const Value *Op1,
519 KnownBits &KnownOut,
520 const SimplifyQuery &Q,
521 unsigned Depth) {
522 Type *Ty = Op0->getType();
523
524 // Only handle scalars for now.
525 if (Ty->isVectorTy())
526 return;
527
528 const Value *Divisor = nullptr, *Cond = nullptr, *Remainder = nullptr,
529 *AdjustmentValue = nullptr;
531 const APInt *Threshold = nullptr;
532 bool TrueIfNegative = false;
533
534 const auto Match = [&](const Value *Rem, const Value *Adjustment) {
535 Remainder = Rem;
536
537 // Match an adjustment added only when this remainder is negative.
538 return match(Rem, m_SRem(m_Value(), m_Value(Divisor))) &&
539 match(Adjustment,
540 m_Select(m_Value(Cond), m_Value(AdjustmentValue), m_Zero())) &&
541 match(Cond, m_ICmp(Pred, m_Specific(Rem), m_APInt(Threshold))) &&
542 isSignBitCheck(Pred, *Threshold, TrueIfNegative) && TrueIfNegative;
543 };
544
545 if (!Match(Op0, Op1) && !Match(Op1, Op0))
546 return;
547
548 // Accept Y itself if non-negative, or abs(Y).
549 bool IsPositiveDivisorCase =
550 Divisor == AdjustmentValue && isKnownNonNegative(Divisor, Q, Depth + 1);
551 bool IsAbsoluteValueCase =
552 match(AdjustmentValue,
554
555 if (!IsPositiveDivisorCase && !IsAbsoluteValueCase)
556 return;
557
558 // Repeated uses must agree. An undef value could break this relationship.
559 if (!isGuaranteedNotToBeUndef(Remainder, Q.AC, Q.CtxI, Q.DT, Depth + 1) ||
560 !isGuaranteedNotToBeUndef(Divisor, Q.AC, Q.CtxI, Q.DT, Depth + 1))
561 return;
562
563 // For Y other than signed minimum, adding abs(Y) to a negative remainder
564 // gives a non-negative result without overflow. For signed minimum, the
565 // wrapping addition clears the remainder's sign bit. Poison-producing cases
566 // do not invalidate this deduction.
567 KnownOut.makeNonNegative();
568}
569
570static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1,
571 bool NSW, bool NUW,
572 const APInt &DemandedElts,
573 KnownBits &KnownOut, KnownBits &Known2,
574 const SimplifyQuery &Q, unsigned Depth) {
575 computeKnownBits(Op1, DemandedElts, KnownOut, Q, Depth + 1);
576
577 if (KnownOut.isUnknown() && !NSW && !NUW) {
578 if (Add) {
579 // Independent operand bits give no result, but the Euclidean remainder
580 // pattern can establish non-negativity through the operands'
581 // relationship.
582 computeKnownBitsFromEuclideanRem(Op0, Op1, KnownOut, Q, Depth);
583 }
584 return;
585 }
586
587 computeKnownBits(Op0, DemandedElts, Known2, Q, Depth + 1);
588 KnownOut = KnownBits::computeForAddSub(Add, NSW, NUW, Known2, KnownOut);
589
590 if (!Add && NSW && !KnownOut.isNonNegative() &&
592 .value_or(false) ||
593 match(Op1, m_c_SMin(m_Specific(Op0), m_Value()))))
594 KnownOut.makeNonNegative();
595
596 if (Add) {
597 // Try to match lerp pattern and combine results
598 computeKnownBitsFromLerpPattern(Op0, Op1, DemandedElts, KnownOut, Q, Depth);
599
600 // Use the relationship between the remainder and its adjustment.
601 computeKnownBitsFromEuclideanRem(Op0, Op1, KnownOut, Q, Depth);
602 }
603}
604
605static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,
606 bool NUW, const APInt &DemandedElts,
607 KnownBits &Known, KnownBits &Known2,
608 const SimplifyQuery &Q, unsigned Depth) {
609 computeKnownBits(Op1, DemandedElts, Known, Q, Depth + 1);
610 computeKnownBits(Op0, DemandedElts, Known2, Q, Depth + 1);
611
612 bool isKnownNegative = false;
613 bool isKnownNonNegative = false;
614 // If the multiplication is known not to overflow, compute the sign bit.
615 if (NSW) {
616 if (Op0 == Op1) {
617 // The product of a number with itself is non-negative.
618 isKnownNonNegative = true;
619 } else {
620 bool isKnownNonNegativeOp1 = Known.isNonNegative();
621 bool isKnownNonNegativeOp0 = Known2.isNonNegative();
622 bool isKnownNegativeOp1 = Known.isNegative();
623 bool isKnownNegativeOp0 = Known2.isNegative();
624 // The product of two numbers with the same sign is non-negative.
625 isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) ||
626 (isKnownNonNegativeOp1 && isKnownNonNegativeOp0);
627 if (!isKnownNonNegative && NUW) {
628 // mul nuw nsw with a factor > 1 is non-negative.
629 KnownBits One = KnownBits::makeConstant(APInt(Known.getBitWidth(), 1));
630 isKnownNonNegative = KnownBits::sgt(Known, One).value_or(false) ||
631 KnownBits::sgt(Known2, One).value_or(false);
632 }
633
634 // The product of a negative number and a non-negative number is either
635 // negative or zero.
638 (isKnownNegativeOp1 && isKnownNonNegativeOp0 &&
639 Known2.isNonZero()) ||
640 (isKnownNegativeOp0 && isKnownNonNegativeOp1 && Known.isNonZero());
641 }
642 }
643
644 bool SelfMultiply = Op0 == Op1;
645 if (SelfMultiply)
646 SelfMultiply &=
647 isGuaranteedNotToBeUndef(Op0, Q.AC, Q.CtxI, Q.DT, Depth + 1);
648 Known = KnownBits::mul(Known, Known2, SelfMultiply);
649
650 if (SelfMultiply) {
651 unsigned SignBits = ComputeNumSignBits(Op0, DemandedElts, Q, Depth + 1);
652 unsigned TyBits = Op0->getType()->getScalarSizeInBits();
653 unsigned OutValidBits = 2 * (TyBits - SignBits + 1);
654
655 if (OutValidBits < TyBits) {
656 APInt KnownZeroMask =
657 APInt::getHighBitsSet(TyBits, TyBits - OutValidBits + 1);
658 Known.Zero |= KnownZeroMask;
659 }
660 }
661
662 // Only make use of no-wrap flags if we failed to compute the sign bit
663 // directly. This matters if the multiplication always overflows, in
664 // which case we prefer to follow the result of the direct computation,
665 // though as the program is invoking undefined behaviour we can choose
666 // whatever we like here.
667 if (isKnownNonNegative && !Known.isNegative())
668 Known.makeNonNegative();
669 else if (isKnownNegative && !Known.isNonNegative())
670 Known.makeNegative();
671}
672
674 KnownBits &Known) {
675 unsigned BitWidth = Known.getBitWidth();
676 unsigned NumRanges = Ranges.getNumOperands() / 2;
677 assert(NumRanges >= 1);
678
679 Known.setAllConflict();
680
681 for (unsigned i = 0; i < NumRanges; ++i) {
683 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
685 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
686 ConstantRange Range(Lower->getValue(), Upper->getValue());
687 // BitWidth must equal the Ranges BitWidth for the correct number of high
688 // bits to be set.
689 assert(BitWidth == Range.getBitWidth() &&
690 "Known bit width must match range bit width!");
691
692 // The first CommonPrefixBits of all values in Range are equal.
693 unsigned CommonPrefixBits =
694 (Range.getUnsignedMax() ^ Range.getUnsignedMin()).countl_zero();
695 APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits);
696 APInt UnsignedMax = Range.getUnsignedMax().zextOrTrunc(BitWidth);
697 Known.One &= UnsignedMax & Mask;
698 Known.Zero &= ~UnsignedMax & Mask;
699 }
700}
701
702static bool isEphemeralValueOf(const Instruction *I, const Value *E) {
703 // The instruction defining an assumption's condition itself is always
704 // considered ephemeral to that assumption (even if it has other
705 // non-ephemeral users). See r246696's test case for an example.
706 if (is_contained(I->operands(), E))
707 return true;
708
709 const auto *EI = dyn_cast<Instruction>(E);
710 if (!EI)
711 return false;
712
713 if (EI == I)
714 return true;
715
718 Visited.insert(EI);
719 WorkList.push_back(EI);
720 bool ReachesI = false;
721 while (!WorkList.empty()) {
722 const Instruction *V = WorkList.pop_back_val();
723 for (const User *U : V->users()) {
724 const auto *UI = cast<Instruction>(U);
725 if (UI == I) {
726 ReachesI = true;
727 continue;
728 }
729 if (UI->mayHaveSideEffects() || UI->isTerminator())
730 return false;
731 if (Visited.insert(UI).second)
732 WorkList.push_back(UI);
733 }
734 }
735 return ReachesI;
736}
737
738// Is this an intrinsic that cannot be speculated but also cannot trap?
740 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(I))
741 return CI->isAssumeLikeIntrinsic();
742
743 return false;
744}
745
747 const Instruction *CtxI,
748 const DominatorTree *DT,
749 bool AllowEphemerals) {
750 // There are two restrictions on the use of an assume:
751 // 1. The assume must dominate the context (or the control flow must
752 // reach the assume whenever it reaches the context).
753 // 2. The context must not be in the assume's set of ephemeral values
754 // (otherwise we will use the assume to prove that the condition
755 // feeding the assume is trivially true, thus causing the removal of
756 // the assume).
757
758 // If the checked instruction and the assume are the same, it's valid exactly
759 // if ephemeral values are allowed.
760 if (Inv == CtxI)
761 return AllowEphemerals;
762
763 if (Inv->getParent() == CtxI->getParent()) {
764 // If Inv and CtxI are in the same block, check if the assume (Inv) is first
765 // in the BB.
766 if (Inv->comesBefore(CtxI))
767 return true;
768
769 // The context comes first, but they're both in the same block.
770 // Make sure there is nothing in between that might interrupt
771 // the control flow, not even CtxI itself.
772 // We limit the scan distance between the assume and its context instruction
773 // to avoid a compile-time explosion. This limit is chosen arbitrarily, so
774 // it can be adjusted if needed (could be turned into a cl::opt).
775 auto Range = make_range(CtxI->getIterator(), Inv->getIterator());
777 return false;
778
779 return AllowEphemerals || !isEphemeralValueOf(Inv, CtxI);
780 }
781
782 // Inv and CtxI are in different blocks.
783 if (DT) {
784 if (DT->dominates(Inv, CtxI))
785 return true;
786 } else if (Inv->getParent() == CtxI->getParent()->getSinglePredecessor() ||
787 Inv->getParent()->isEntryBlock()) {
788 // We don't have a DT, but this trivially dominates.
789 return true;
790 }
791
792 return false;
793}
794
797 unsigned &NumChecked) {
798 for (const Instruction &I : make_range(Begin, End)) {
799 if (NumChecked++ > MaxInstrsToCheckForFree)
800 return false;
801 if (auto *CB = dyn_cast<CallBase>(&I)) {
802 if (!CB->hasFnAttr(Attribute::NoFree))
803 return false;
804 } else if (I.maySynchronize()) {
805 return false;
806 }
807 }
808 return true;
809}
810
812 const Instruction *CtxI,
813 const DominatorTree *DT) {
814 const BasicBlock *CtxBB = CtxI->getParent();
815 const BasicBlock *AssumeBB = Assume->getParent();
816 unsigned NumChecked = 0;
817 BasicBlock::const_iterator CtxIter = CtxI->getIterator();
818 if (CtxBB == AssumeBB) {
819 if (Assume != CtxI && !Assume->comesBefore(CtxI))
820 return false;
821 return hasNoFreeInRange(Assume->getIterator(), CtxIter, NumChecked);
822 }
823 if (DT && !DT->dominates(Assume, CtxI))
824 return false;
825 if (!hasNoFreeInRange(CtxBB->begin(), CtxIter, NumChecked))
826 return false;
827 if (pred_empty(CtxBB))
828 return false;
829
830 // Note: CtxBB is NOT pre-inserted into Visited to ensure that loop
831 // backedges returning to CtxBB are enqueued and checked correctly.
834 while (!Worklist.empty()) {
835 const BasicBlock *CurBB = Worklist.pop_back_val();
836 if (!Visited.insert(CurBB).second)
837 continue;
838
839 if (CurBB == AssumeBB) {
840 if (!hasNoFreeInRange(Assume->getIterator(), AssumeBB->end(), NumChecked))
841 return false;
842 continue;
843 }
844 assert((!DT || DT->dominates(AssumeBB, CurBB)) &&
845 "Blocks between Assume and CtxI must be dominated by AssumeBB");
846
847 if (pred_empty(CurBB)) {
848 // Reaching the entry block means there is a path to CtxI that does not
849 // pass through Assume.
850 if (CurBB->isEntryBlock())
851 return false;
852
853 // CurBB is unreachable (for example, emptied by SimplifyCFG but not yet
854 // removed), so nothing in it can execute a free.
855 continue;
856 }
857
858 // If CurBB == CtxBB (due to a loop backedge targeting CtxBB), check
859 // instructions from CtxIter to the end of CtxBB (instructions before
860 // CtxIter were checked above). Otherwise, check the entire block.
861 auto StartIt = (CurBB == CtxBB) ? CtxIter : CurBB->begin();
862 if (!hasNoFreeInRange(StartIt, CurBB->end(), NumChecked))
863 return false;
864 append_range(Worklist, predecessors(CurBB));
865 }
866 return true;
867}
868
869// TODO: cmpExcludesZero misses many cases where `RHS` is non-constant but
870// we still have enough information about `RHS` to conclude non-zero. For
871// example Pred=EQ, RHS=isKnownNonZero. cmpExcludesZero is called in loops
872// so the extra compile time may not be worth it, but possibly a second API
873// should be created for use outside of loops.
875 // v u> y implies v != 0.
877 return true;
878
879 // Special-case v != 0 to also handle v != null.
880 if (Pred == ICmpInst::ICMP_NE)
881 return match(RHS, m_Zero());
882
883 // All other predicates - rely on generic ConstantRange handling.
884 const APInt *C;
885 auto Zero = APInt::getZero(RHS->getType()->getScalarSizeInBits());
886 if (match(RHS, m_APInt(C))) {
888 return !TrueValues.contains(Zero);
889 }
890
892 if (VC == nullptr)
893 return false;
894
895 for (unsigned ElemIdx = 0, NElem = VC->getNumElements(); ElemIdx < NElem;
896 ++ElemIdx) {
898 Pred, VC->getElementAsAPInt(ElemIdx));
899 if (TrueValues.contains(Zero))
900 return false;
901 }
902 return true;
903}
904
905static void breakSelfRecursivePHI(const Use *U, const PHINode *PHI,
906 Value *&ValOut, Instruction *&CtxIOut,
907 const PHINode **PhiOut = nullptr) {
908 ValOut = U->get();
909 if (ValOut == PHI)
910 return;
911 CtxIOut = PHI->getIncomingBlock(*U)->getTerminator();
912 if (PhiOut)
913 *PhiOut = PHI;
914 Value *V;
915 // If the Use is a select of this phi, compute analysis on other arm to break
916 // recursion.
917 // TODO: Min/Max
918 if (match(ValOut, m_Select(m_Value(), m_Specific(PHI), m_Value(V))) ||
919 match(ValOut, m_Select(m_Value(), m_Value(V), m_Specific(PHI))))
920 ValOut = V;
921
922 // Same for select, if this phi is 2-operand phi, compute analysis on other
923 // incoming value to break recursion.
924 // TODO: We could handle any number of incoming edges as long as we only have
925 // two unique values.
926 if (auto *IncPhi = dyn_cast<PHINode>(ValOut);
927 IncPhi && IncPhi->getNumIncomingValues() == 2) {
928 for (int Idx = 0; Idx < 2; ++Idx) {
929 if (IncPhi->getIncomingValue(Idx) == PHI) {
930 ValOut = IncPhi->getIncomingValue(1 - Idx);
931 if (PhiOut)
932 *PhiOut = IncPhi;
933 CtxIOut = IncPhi->getIncomingBlock(1 - Idx)->getTerminator();
934 break;
935 }
936 }
937 }
938}
939
940static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q) {
941 // Use of assumptions is context-sensitive. If we don't have a context, we
942 // cannot use them!
943 if (!Q.AC || !Q.CtxI)
944 return false;
945
947 if (!Elem.Assume)
948 continue;
949
950 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
951 assert(I->getFunction() == Q.CtxI->getFunction() &&
952 "Got assumption for the wrong function!");
953
954 if (Elem.Index != AssumptionCache::ExprResultIdx) {
956 I->getOperandBundleAt(Elem.Index)) &&
958 return true;
959 continue;
960 }
961
962 // Warning: This loop can end up being somewhat performance sensitive.
963 // We're running this loop for once for each value queried resulting in a
964 // runtime of ~O(#assumes * #values).
965
966 if (match(I->getArgOperand(0), m_Trunc(m_Specific(V))) &&
968 return true;
969
970 Value *RHS;
973 if (!match(I->getArgOperand(0), m_c_ICmp(Pred, m_V, m_Value(RHS))))
974 continue;
975
977 return true;
978 }
979
980 return false;
981}
982
985 const SimplifyQuery &Q) {
986 if (RHS->getType()->isPointerTy()) {
987 // Handle comparison of pointer to null explicitly, as it will not be
988 // covered by the m_APInt() logic below.
989 if (LHS == V && match(RHS, m_Zero())) {
990 switch (Pred) {
992 Known.setAllZero();
993 break;
996 Known.makeNonNegative();
997 break;
999 Known.makeNegative();
1000 break;
1001 default:
1002 break;
1003 }
1004 }
1005 return;
1006 }
1007
1008 unsigned BitWidth = Known.getBitWidth();
1009 auto m_V =
1011
1012 Value *Y;
1013 const APInt *Mask, *C;
1014 if (!match(RHS, m_APInt(C)))
1015 return;
1016
1017 uint64_t ShAmt;
1018 switch (Pred) {
1019 case ICmpInst::ICMP_EQ:
1020 // assume(V = C)
1021 if (match(LHS, m_V)) {
1022 Known = Known.unionWith(KnownBits::makeConstant(*C));
1023 // assume(V & Mask = C)
1024 } else if (match(LHS, m_c_And(m_V, m_Value(Y)))) {
1025 // For one bits in Mask, we can propagate bits from C to V.
1026 Known.One |= *C;
1027 if (match(Y, m_APInt(Mask)))
1028 Known.Zero |= ~*C & *Mask;
1029 // assume(V | Mask = C)
1030 } else if (match(LHS, m_c_Or(m_V, m_Value(Y)))) {
1031 // For zero bits in Mask, we can propagate bits from C to V.
1032 Known.Zero |= ~*C;
1033 if (match(Y, m_APInt(Mask)))
1034 Known.One |= *C & ~*Mask;
1035 // assume(V << ShAmt = C)
1036 } else if (match(LHS, m_Shl(m_V, m_ConstantInt(ShAmt))) &&
1037 ShAmt < BitWidth) {
1038 // For those bits in C that are known, we can propagate them to known
1039 // bits in V shifted to the right by ShAmt.
1040 KnownBits RHSKnown = KnownBits::makeConstant(*C);
1041 RHSKnown >>= ShAmt;
1042 Known = Known.unionWith(RHSKnown);
1043 // assume(V >> ShAmt = C)
1044 } else if (match(LHS, m_Shr(m_V, m_ConstantInt(ShAmt))) &&
1045 ShAmt < BitWidth) {
1046 // For those bits in RHS that are known, we can propagate them to known
1047 // bits in V shifted to the right by C.
1048 KnownBits RHSKnown = KnownBits::makeConstant(*C);
1049 RHSKnown <<= ShAmt;
1050 Known = Known.unionWith(RHSKnown);
1051 }
1052 break;
1053 case ICmpInst::ICMP_NE: {
1054 // assume (V & B != 0) where B is a power of 2
1055 const APInt *BPow2;
1056 if (C->isZero() && match(LHS, m_And(m_V, m_Power2(BPow2))))
1057 Known.One |= *BPow2;
1058 break;
1059 }
1060 default: {
1061 const APInt *Offset = nullptr;
1062 if (match(LHS, m_CombineOr(m_V, m_AddLike(m_V, m_APInt(Offset))))) {
1064 if (Offset)
1065 LHSRange = LHSRange.sub(*Offset);
1066 Known = Known.unionWith(LHSRange.toKnownBits());
1067 }
1069 // X & Y u> C -> X u> C && Y u> C
1070 // X nuw- Y u> C -> X u> C
1071 if (match(LHS, m_c_And(m_V, m_Value())) ||
1072 match(LHS, m_NUWSub(m_V, m_Value())))
1073 Known.One.setHighBits(
1074 (*C + (Pred == ICmpInst::ICMP_UGT)).countLeadingOnes());
1075 }
1077 // X | Y u< C -> X u< C && Y u< C
1078 // X nuw+ Y u< C -> X u< C && Y u< C
1079 if (match(LHS, m_c_Or(m_V, m_Value())) ||
1080 match(LHS, m_c_NUWAdd(m_V, m_Value()))) {
1081 Known.Zero.setHighBits(
1082 (*C - (Pred == ICmpInst::ICMP_ULT)).countLeadingZeros());
1083 }
1084 }
1085 } break;
1086 }
1087}
1088
1091 const SimplifyQuery &SQ, bool Invert) {
1093 Invert ? Cmp->getInversePredicate() : Cmp->getPredicate();
1094 Value *LHS = Cmp->getOperand(0);
1095 Value *RHS = Cmp->getOperand(1);
1096
1097 // Handle icmp pred (trunc V), C
1098 if (match(LHS, m_Trunc(m_Specific(V)))) {
1099 KnownBits DstKnown(LHS->getType()->getScalarSizeInBits());
1100 computeKnownBitsFromCmp(LHS, Pred, LHS, RHS, DstKnown, SQ);
1102 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
1103 else
1104 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
1105 return;
1106 }
1107
1109}
1110
1112 KnownBits &Known, const SimplifyQuery &SQ,
1113 bool Invert, unsigned Depth) {
1114 Value *A, *B;
1117 KnownBits Known2(Known.getBitWidth());
1118 KnownBits Known3(Known.getBitWidth());
1119 computeKnownBitsFromCond(V, A, Known2, SQ, Invert, Depth + 1);
1120 computeKnownBitsFromCond(V, B, Known3, SQ, Invert, Depth + 1);
1121 if (Invert ? match(Cond, m_LogicalOr(m_Value(), m_Value()))
1123 Known2 = Known2.unionWith(Known3);
1124 else
1125 Known2 = Known2.intersectWith(Known3);
1126 Known = Known.unionWith(Known2);
1127 return;
1128 }
1129
1130 if (auto *Cmp = dyn_cast<ICmpInst>(Cond)) {
1131 computeKnownBitsFromICmpCond(V, Cmp, Known, SQ, Invert);
1132 return;
1133 }
1134
1135 if (match(Cond, m_Trunc(m_Specific(V)))) {
1136 KnownBits DstKnown(1);
1137 if (Invert) {
1138 DstKnown.setAllZero();
1139 } else {
1140 DstKnown.setAllOnes();
1141 }
1143 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
1144 return;
1145 }
1146 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
1147 return;
1148 }
1149
1151 computeKnownBitsFromCond(V, A, Known, SQ, !Invert, Depth + 1);
1152}
1153
1155 const SimplifyQuery &Q, unsigned Depth) {
1156 // Handle injected condition.
1157 if (Q.CC && Q.CC->AffectedValues.contains(V))
1159
1160 if (!Q.CtxI)
1161 return;
1162
1163 if (Q.DC && Q.DT) {
1164 // Handle dominating conditions.
1165 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
1166 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
1167 if (Q.DT->dominates(Edge0, Q.CtxI->getParent()))
1168 computeKnownBitsFromCond(V, BI->getCondition(), Known, Q,
1169 /*Invert*/ false, Depth);
1170
1171 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
1172 if (Q.DT->dominates(Edge1, Q.CtxI->getParent()))
1173 computeKnownBitsFromCond(V, BI->getCondition(), Known, Q,
1174 /*Invert*/ true, Depth);
1175 }
1176
1177 if (Known.hasConflict())
1178 Known.resetAll();
1179 }
1180
1181 if (!Q.AC)
1182 return;
1183
1184 unsigned BitWidth = Known.getBitWidth();
1185
1186 // Note that the patterns below need to be kept in sync with the code
1187 // in AssumptionCache::updateAffectedValues.
1188
1189 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
1190 if (!Elem.Assume)
1191 continue;
1192
1193 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
1194 assert(I->getParent()->getParent() == Q.CtxI->getParent()->getParent() &&
1195 "Got assumption for the wrong function!");
1196
1197 if (Elem.Index != AssumptionCache::ExprResultIdx) {
1198 auto OBU = I->getOperandBundleAt(Elem.Index);
1199 switch (getBundleAttrFromOBU(OBU)) {
1200 case BundleAttr::Align: {
1201 auto [Ptr, _, _2, Alignment, Offset] = getAssumeAlignInfo(OBU);
1202 if (Ptr == V && Alignment && Offset && isPowerOf2_64(*Alignment) &&
1204 Known.Zero |= (*Alignment - 1) & ~*Offset;
1205 Known.One |= (*Alignment - 1) & *Offset;
1206 }
1207 } break;
1208
1209 case BundleAttr::Range: {
1210 auto [Val, Range] = getAssumeRangeInfo(OBU);
1211 if (Val == V && isValidAssumeForContext(I, Q))
1212 Known = Known.unionWith(Range.toKnownBits());
1213 } break;
1214
1215 default:
1216 break;
1217 }
1218 continue;
1219 }
1220
1221 // Warning: This loop can end up being somewhat performance sensitive.
1222 // We're running this loop for once for each value queried resulting in a
1223 // runtime of ~O(#assumes * #values).
1224
1225 Value *Arg = I->getArgOperand(0);
1226
1227 if (Arg == V && isValidAssumeForContext(I, Q)) {
1228 assert(BitWidth == 1 && "assume operand is not i1?");
1229 (void)BitWidth;
1230 Known.setAllOnes();
1231 return;
1232 }
1233 if (match(Arg, m_Not(m_Specific(V))) &&
1235 assert(BitWidth == 1 && "assume operand is not i1?");
1236 (void)BitWidth;
1237 Known.setAllZero();
1238 return;
1239 }
1240 auto *Trunc = dyn_cast<TruncInst>(Arg);
1241 if (Trunc && Trunc->getOperand(0) == V &&
1243 if (Trunc->hasNoUnsignedWrap()) {
1245 return;
1246 }
1247 Known.One.setBit(0);
1248 return;
1249 }
1250
1251 // The remaining tests are all recursive, so bail out if we hit the limit.
1253 continue;
1254
1255 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
1256 if (!Cmp)
1257 continue;
1258
1259 if (!isValidAssumeForContext(I, Q))
1260 continue;
1261
1262 computeKnownBitsFromICmpCond(V, Cmp, Known, Q, /*Invert=*/false);
1263 }
1264
1265 // Conflicting assumption: Undefined behavior will occur on this execution
1266 // path.
1267 if (Known.hasConflict())
1268 Known.resetAll();
1269}
1270
1271/// Compute known bits from a shift operator, including those with a
1272/// non-constant shift amount. Known is the output of this function. Known2 is a
1273/// pre-allocated temporary with the same bit width as Known and on return
1274/// contains the known bit of the shift value source. KF is an
1275/// operator-specific function that, given the known-bits and a shift amount,
1276/// compute the implied known-bits of the shift operator's result respectively
1277/// for that shift amount. The results from calling KF are conservatively
1278/// combined for all permitted shift amounts.
1280 const Operator *I, const APInt &DemandedElts, KnownBits &Known,
1281 KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth,
1282 function_ref<KnownBits(const KnownBits &, const KnownBits &, bool)> KF) {
1283 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1284 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1285 // To limit compile-time impact, only query isKnownNonZero() if we know at
1286 // least something about the shift amount.
1287 bool ShAmtNonZero =
1288 Known.isNonZero() ||
1289 (Known.getMaxValue().ult(Known.getBitWidth()) &&
1290 isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth + 1));
1291 Known = KF(Known2, Known, ShAmtNonZero);
1292}
1293
1294static KnownBits
1295getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts,
1296 const KnownBits &KnownLHS, const KnownBits &KnownRHS,
1297 const SimplifyQuery &Q, unsigned Depth) {
1298 unsigned BitWidth = KnownLHS.getBitWidth();
1299 KnownBits KnownOut(BitWidth);
1300 bool IsAnd = false;
1301 bool HasKnownOne = !KnownLHS.One.isZero() || !KnownRHS.One.isZero();
1302 Value *X = nullptr, *Y = nullptr;
1303
1304 switch (I->getOpcode()) {
1305 case Instruction::And:
1306 KnownOut = KnownLHS & KnownRHS;
1307 IsAnd = true;
1308 // and(x, -x) is common idioms that will clear all but lowest set
1309 // bit. If we have a single known bit in x, we can clear all bits
1310 // above it.
1311 // TODO: instcombine often reassociates independent `and` which can hide
1312 // this pattern. Try to match and(x, and(-x, y)) / and(and(x, y), -x).
1313 if (HasKnownOne && match(I, m_c_And(m_Value(X), m_Neg(m_Deferred(X))))) {
1314 // -(-x) == x so using whichever (LHS/RHS) gets us a better result.
1315 if (KnownLHS.countMaxTrailingZeros() <= KnownRHS.countMaxTrailingZeros())
1316 KnownOut = KnownLHS.blsi();
1317 else
1318 KnownOut = KnownRHS.blsi();
1319 }
1320 break;
1321 case Instruction::Or:
1322 KnownOut = KnownLHS | KnownRHS;
1323 break;
1324 case Instruction::Xor:
1325 KnownOut = KnownLHS ^ KnownRHS;
1326 // xor(x, x-1) is common idioms that will clear all but lowest set
1327 // bit. If we have a single known bit in x, we can clear all bits
1328 // above it.
1329 // TODO: xor(x, x-1) is often rewritting as xor(x, x-C) where C !=
1330 // -1 but for the purpose of demanded bits (xor(x, x-C) &
1331 // Demanded) == (xor(x, x-1) & Demanded). Extend the xor pattern
1332 // to use arbitrary C if xor(x, x-C) as the same as xor(x, x-1).
1333 if (HasKnownOne &&
1335 const KnownBits &XBits = I->getOperand(0) == X ? KnownLHS : KnownRHS;
1336 KnownOut = XBits.blsmsk();
1337 }
1338 break;
1339 default:
1340 llvm_unreachable("Invalid Op used in 'analyzeKnownBitsFromAndXorOr'");
1341 }
1342
1343 // and(x, add (x, -1)) is a common idiom that always clears the low bit;
1344 // xor/or(x, add (x, -1)) is an idiom that will always set the low bit.
1345 // here we handle the more general case of adding any odd number by
1346 // matching the form and/xor/or(x, add(x, y)) where y is odd.
1347 // TODO: This could be generalized to clearing any bit set in y where the
1348 // following bit is known to be unset in y.
1349 if (!KnownOut.Zero[0] && !KnownOut.One[0] &&
1353 KnownBits KnownY(BitWidth);
1354 computeKnownBits(Y, DemandedElts, KnownY, Q, Depth + 1);
1355 if (KnownY.countMinTrailingOnes() > 0) {
1356 if (IsAnd)
1357 KnownOut.Zero.setBit(0);
1358 else
1359 KnownOut.One.setBit(0);
1360 }
1361 }
1362 return KnownOut;
1363}
1364
1366 const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q,
1367 unsigned Depth,
1368 const function_ref<KnownBits(const KnownBits &, const KnownBits &)>
1369 KnownBitsFunc) {
1370 APInt DemandedEltsLHS, DemandedEltsRHS;
1372 DemandedElts, DemandedEltsLHS,
1373 DemandedEltsRHS);
1374
1375 const auto ComputeForSingleOpFunc =
1376 [Depth, &Q, KnownBitsFunc](const Value *Op, APInt &DemandedEltsOp) {
1377 return KnownBitsFunc(
1378 computeKnownBits(Op, DemandedEltsOp, Q, Depth + 1),
1379 computeKnownBits(Op, DemandedEltsOp << 1, Q, Depth + 1));
1380 };
1381
1382 if (DemandedEltsRHS.isZero())
1383 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS);
1384 if (DemandedEltsLHS.isZero())
1385 return ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS);
1386
1387 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS)
1388 .intersectWith(ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS));
1389}
1390
1391// Public so this can be used in `SimplifyDemandedUseBits`.
1393 const KnownBits &KnownLHS,
1394 const KnownBits &KnownRHS,
1395 const SimplifyQuery &SQ,
1396 unsigned Depth) {
1397 auto *FVTy = dyn_cast<FixedVectorType>(I->getType());
1398 APInt DemandedElts =
1399 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
1400
1401 return getKnownBitsFromAndXorOr(I, DemandedElts, KnownLHS, KnownRHS, SQ,
1402 Depth);
1403}
1404
1406 Attribute Attr = F->getFnAttribute(Attribute::VScaleRange);
1407 // Without vscale_range, we only know that vscale is non-zero.
1408 if (!Attr.isValid())
1410
1411 unsigned AttrMin = Attr.getVScaleRangeMin();
1412 // Minimum is larger than vscale width, result is always poison.
1413 if ((unsigned)llvm::bit_width(AttrMin) > BitWidth)
1414 return ConstantRange::getEmpty(BitWidth);
1415
1416 APInt Min(BitWidth, AttrMin);
1417 std::optional<unsigned> AttrMax = Attr.getVScaleRangeMax();
1418 if (!AttrMax || (unsigned)llvm::bit_width(*AttrMax) > BitWidth)
1420
1421 return ConstantRange(Min, APInt(BitWidth, *AttrMax) + 1);
1422}
1423
1424/// Return true if \p II reads a register named "vlenb". On RISC-V this is the
1425/// VLENB CSR, which holds VLEN/8: a non-zero power of two bounded by the
1426/// target's VLEN range. Callers must ensure the target is RISC-V.
1427static bool isReadVLENB(const IntrinsicInst &II) {
1428 auto *MAV = dyn_cast<MetadataAsValue>(II.getArgOperand(0));
1429 if (!MAV)
1430 return false;
1431 auto *MD = dyn_cast<MDNode>(MAV->getMetadata());
1432 if (!MD || MD->getNumOperands() != 1)
1433 return false;
1434 auto *RegName = dyn_cast<MDString>(MD->getOperand(0));
1435 return RegName && RegName->getString() == "vlenb";
1436}
1437
1438/// Return the value range of a RISC-V vlenb CSR read. RVV requires VLEN to be a
1439/// power of two in [32, 65536] (Zvl32b is the smallest vector extension), so
1440/// VLENB = VLEN/8 is in [4, 8192]. This architectural bound is independent of
1441/// any function attribute and stays sound for Zvl32b, whose VLEN (32) is not
1442/// representable as an integer vscale (VLEN / RVVBitsPerBlock). A vscale_range
1443/// attribute, when present, pins the subtarget's VLEN in units of
1444/// RVVBitsPerBlock (64 bits) and so gives a tighter VLENB = vscale *
1445/// RVVBytesPerBlock.
1447 unsigned Width) {
1448 // Architectural bounds: VLEN in [32, 65536] => VLENB in [4, 8192].
1449 ConstantRange Range(APInt(Width, 32 / 8), APInt(Width, 65536 / 8) + 1);
1450
1451 const Function *F = II.getFunction();
1452 if (F->getFnAttribute(Attribute::VScaleRange).isValid()) {
1453 ConstantRange VScale = getVScaleRange(F, Width);
1454 Range = Range.intersectWith(
1456 }
1457 return Range;
1458}
1459
1461 Value *Arm, bool Invert,
1462 const SimplifyQuery &Q, unsigned Depth) {
1463 // If we have a constant arm, we are done.
1464 if (Known.isConstant())
1465 return;
1466
1467 // See what condition implies about the bits of the select arm.
1468 KnownBits CondRes(Known.getBitWidth());
1469 computeKnownBitsFromCond(Arm, Cond, CondRes, Q, Invert, Depth + 1);
1470 // If we don't get any information from the condition, no reason to
1471 // proceed.
1472 if (CondRes.isUnknown())
1473 return;
1474
1475 // We can have conflict if the condition is dead. I.e if we have
1476 // (x | 64) < 32 ? (x | 64) : y
1477 // we will have conflict at bit 6 from the condition/the `or`.
1478 // In that case just return. Its not particularly important
1479 // what we do, as this select is going to be simplified soon.
1480 CondRes = CondRes.unionWith(Known);
1481 if (CondRes.hasConflict())
1482 return;
1483
1484 // Finally make sure the information we found is valid. This is relatively
1485 // expensive so it's left for the very end.
1486 if (!isGuaranteedNotToBeUndef(Arm, Q.AC, Q.CtxI, Q.DT, Depth + 1))
1487 return;
1488
1489 // Finally, we know we get information from the condition and its valid,
1490 // so return it.
1491 Known = std::move(CondRes);
1492}
1493
1494// Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow).
1495// Returns the input and lower/upper bounds.
1496static bool isSignedMinMaxClamp(const Value *Select, const Value *&In,
1497 const APInt *&CLow, const APInt *&CHigh) {
1499 cast<Operator>(Select)->getOpcode() == Instruction::Select &&
1500 "Input should be a Select!");
1501
1502 const Value *LHS = nullptr, *RHS = nullptr;
1504 if (SPF != SPF_SMAX && SPF != SPF_SMIN)
1505 return false;
1506
1507 if (!match(RHS, m_APInt(CLow)))
1508 return false;
1509
1510 const Value *LHS2 = nullptr, *RHS2 = nullptr;
1512 if (getInverseMinMaxFlavor(SPF) != SPF2)
1513 return false;
1514
1515 if (!match(RHS2, m_APInt(CHigh)))
1516 return false;
1517
1518 if (SPF == SPF_SMIN)
1519 std::swap(CLow, CHigh);
1520
1521 In = LHS2;
1522 return CLow->sle(*CHigh);
1523}
1524
1526 const APInt *&CLow,
1527 const APInt *&CHigh) {
1528 assert((II->getIntrinsicID() == Intrinsic::smin ||
1529 II->getIntrinsicID() == Intrinsic::smax) &&
1530 "Must be smin/smax");
1531
1532 Intrinsic::ID InverseID = getInverseMinMaxIntrinsic(II->getIntrinsicID());
1533 auto *InnerII = dyn_cast<IntrinsicInst>(II->getArgOperand(0));
1534 if (!InnerII || InnerII->getIntrinsicID() != InverseID ||
1535 !match(II->getArgOperand(1), m_APInt(CLow)) ||
1536 !match(InnerII->getArgOperand(1), m_APInt(CHigh)))
1537 return false;
1538
1539 if (II->getIntrinsicID() == Intrinsic::smin)
1540 std::swap(CLow, CHigh);
1541 return CLow->sle(*CHigh);
1542}
1543
1545 KnownBits &Known) {
1546 const APInt *CLow, *CHigh;
1547 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
1548 Known = Known.unionWith(
1549 ConstantRange::getNonEmpty(*CLow, *CHigh + 1).toKnownBits());
1550}
1551
1553 const PHINode *P, Value *Start, Value *Step, const APInt &DemandedElts,
1554 KnownBits &KnownStart, KnownBits &KnownStep, const SimplifyQuery &Q,
1555 unsigned Depth) {
1556 // Change the context instruction to the "edge" that flows into the phi. This
1557 // is important because that is where the value is actually "evaluated" even
1558 // though it is used later somewhere else. (see also D69571).
1560 unsigned OpNum = P->getOperand(0) == Start ? 0 : 1;
1561
1562 RecQ.CtxI = P->getIncomingBlock(OpNum)->getTerminator();
1563 computeKnownBits(Start, DemandedElts, KnownStart, RecQ, Depth + 1);
1564
1565 RecQ.CtxI = P->getIncomingBlock(1 - OpNum)->getTerminator();
1566 computeKnownBits(Step, DemandedElts, KnownStep, RecQ, Depth + 1);
1567}
1568
1570 const APInt &DemandedElts,
1572 const SimplifyQuery &Q,
1573 unsigned Depth) {
1574 unsigned BitWidth = Known.getBitWidth();
1575
1576 KnownBits Known2(BitWidth);
1577 switch (I->getOpcode()) {
1578 default: break;
1579 case Instruction::Load:
1580 if (MDNode *MD =
1581 Q.IIQ.getMetadata(cast<LoadInst>(I), LLVMContext::MD_range))
1583 break;
1584 case Instruction::And:
1585 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1586 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1587
1588 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1589 break;
1590 case Instruction::Or:
1591 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1592 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1593
1594 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1595 break;
1596 case Instruction::Xor:
1597 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1598 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1599
1600 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1601 break;
1602 case Instruction::Mul: {
1605 computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, NUW,
1606 DemandedElts, Known, Known2, Q, Depth);
1607 break;
1608 }
1609 case Instruction::UDiv: {
1610 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1611 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1612 Known =
1614 break;
1615 }
1616 case Instruction::SDiv: {
1617 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1618 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1619 Known =
1621 break;
1622 }
1623 case Instruction::Select: {
1624 auto ComputeForArm = [&](Value *Arm, bool Invert) {
1625 KnownBits Res(Known.getBitWidth());
1626 computeKnownBits(Arm, DemandedElts, Res, Q, Depth + 1);
1627 adjustKnownBitsForSelectArm(Res, I->getOperand(0), Arm, Invert, Q, Depth);
1628 return Res;
1629 };
1630 // Only known if known in both the LHS and RHS.
1631 Known =
1632 ComputeForArm(I->getOperand(1), /*Invert=*/false)
1633 .intersectWith(ComputeForArm(I->getOperand(2), /*Invert=*/true));
1634 break;
1635 }
1636 case Instruction::FPToSI: {
1637 // fptosi is poison if the rounded value doesn't fit in the result type,
1638 // so we can assume the conversion is well-defined and rounds towards
1639 // zero. +-Inf can never fit in an integer type, so it is always poison,
1640 // like NaN. Negative subnormals and negative zero round to 0. That
1641 // leaves negative normals as the only class that can produce a defined
1642 // negative result.
1643 KnownFPClass SrcFPClass = computeKnownFPClass(
1644 I->getOperand(0), DemandedElts, fcNegNormal, Q, Depth + 1);
1645 if (SrcFPClass.isKnownNever(fcNegNormal))
1646 Known.makeNonNegative();
1647 break;
1648 }
1649 case Instruction::FPTrunc:
1650 case Instruction::FPExt:
1651 case Instruction::FPToUI:
1652 case Instruction::SIToFP:
1653 case Instruction::UIToFP:
1654 break; // Can't work with floating point.
1655 case Instruction::PtrToInt:
1656 case Instruction::PtrToAddr:
1657 case Instruction::IntToPtr:
1658 // Fall through and handle them the same as zext/trunc.
1659 [[fallthrough]];
1660 case Instruction::ZExt:
1661 case Instruction::Trunc: {
1662 Type *SrcTy = I->getOperand(0)->getType();
1663
1664 unsigned SrcBitWidth;
1665 // Note that we handle pointer operands here because of inttoptr/ptrtoint
1666 // which fall through here.
1667 Type *ScalarTy = SrcTy->getScalarType();
1668 SrcBitWidth = ScalarTy->isPointerTy() ?
1669 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
1670 Q.DL.getTypeSizeInBits(ScalarTy);
1671
1672 assert(SrcBitWidth && "SrcBitWidth can't be zero");
1673 Known = Known.anyextOrTrunc(SrcBitWidth);
1674 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1675 if (auto *Inst = dyn_cast<PossiblyNonNegInst>(I);
1676 Inst && Inst->hasNonNeg() && !Known.isNegative())
1677 Known.makeNonNegative();
1678 Known = Known.zextOrTrunc(BitWidth);
1679 break;
1680 }
1681 case Instruction::BitCast: {
1682 Type *SrcTy = I->getOperand(0)->getType();
1683 if (SrcTy->isIntOrPtrTy() &&
1684 // TODO: For now, not handling conversions like:
1685 // (bitcast i64 %x to <2 x i32>)
1686 !I->getType()->isVectorTy()) {
1687 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1688 break;
1689 }
1690
1691 const Value *V;
1692 // Handle bitcast from floating point to integer.
1694 V->getType()->isFPOrFPVectorTy()) {
1695 Type *FPType = V->getType()->getScalarType();
1696 KnownFPClass Result =
1697 computeKnownFPClass(V, DemandedElts, fcAllFlags, Q, Depth + 1);
1698
1699 Known = Result.toKnownBits(FPType->getFltSemantics());
1700
1701 break;
1702 }
1703
1704 // Handle cast from vector integer type to scalar or vector integer.
1705 auto *SrcVecTy = dyn_cast<FixedVectorType>(SrcTy);
1706 if (!SrcVecTy || !SrcVecTy->getElementType()->isIntegerTy() ||
1707 !I->getType()->isIntOrIntVectorTy() ||
1708 isa<ScalableVectorType>(I->getType()))
1709 break;
1710
1711 unsigned NumElts = DemandedElts.getBitWidth();
1712 bool IsLE = Q.DL.isLittleEndian();
1713 // Look through a cast from narrow vector elements to wider type.
1714 // Examples: v4i32 -> v2i64, v3i8 -> v24
1715 unsigned SubBitWidth = SrcVecTy->getScalarSizeInBits();
1716 if (BitWidth % SubBitWidth == 0) {
1717 // Known bits are automatically intersected across demanded elements of a
1718 // vector. So for example, if a bit is computed as known zero, it must be
1719 // zero across all demanded elements of the vector.
1720 //
1721 // For this bitcast, each demanded element of the output is sub-divided
1722 // across a set of smaller vector elements in the source vector. To get
1723 // the known bits for an entire element of the output, compute the known
1724 // bits for each sub-element sequentially. This is done by shifting the
1725 // one-set-bit demanded elements parameter across the sub-elements for
1726 // consecutive calls to computeKnownBits. We are using the demanded
1727 // elements parameter as a mask operator.
1728 //
1729 // The known bits of each sub-element are then inserted into place
1730 // (dependent on endian) to form the full result of known bits.
1731 unsigned SubScale = BitWidth / SubBitWidth;
1732 APInt SubDemandedElts = APInt::getZero(NumElts * SubScale);
1733 for (unsigned i = 0; i != NumElts; ++i) {
1734 if (DemandedElts[i])
1735 SubDemandedElts.setBit(i * SubScale);
1736 }
1737
1738 KnownBits KnownSrc(SubBitWidth);
1739 for (unsigned i = 0; i != SubScale; ++i) {
1740 computeKnownBits(I->getOperand(0), SubDemandedElts.shl(i), KnownSrc, Q,
1741 Depth + 1);
1742 unsigned ShiftElt = IsLE ? i : SubScale - 1 - i;
1743 Known.insertBits(KnownSrc, ShiftElt * SubBitWidth);
1744 }
1745 }
1746 // Look through a cast from wider vector elements to narrow type.
1747 // Examples: v2i64 -> v4i32
1748 if (SubBitWidth % BitWidth == 0) {
1749 unsigned SubScale = SubBitWidth / BitWidth;
1750 KnownBits KnownSrc(SubBitWidth);
1751 APInt SubDemandedElts =
1752 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
1753 computeKnownBits(I->getOperand(0), SubDemandedElts, KnownSrc, Q,
1754 Depth + 1);
1755
1756 Known.setAllConflict();
1757 for (unsigned i = 0; i != NumElts; ++i) {
1758 if (DemandedElts[i]) {
1759 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
1760 unsigned Offset = (Shifts % SubScale) * BitWidth;
1761 Known = Known.intersectWith(KnownSrc.extractBits(BitWidth, Offset));
1762 if (Known.isUnknown())
1763 break;
1764 }
1765 }
1766 }
1767 break;
1768 }
1769 case Instruction::SExt: {
1770 // Compute the bits in the result that are not present in the input.
1771 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
1772
1773 Known = Known.trunc(SrcBitWidth);
1774 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1775 // If the sign bit of the input is known set or clear, then we know the
1776 // top bits of the result.
1777 Known = Known.sext(BitWidth);
1778 break;
1779 }
1780 case Instruction::Shl: {
1783 auto KF = [NUW, NSW](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1784 bool ShAmtNonZero) {
1785 return KnownBits::shl(KnownVal, KnownAmt, NUW, NSW, ShAmtNonZero);
1786 };
1787 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1788 KF);
1789 // X << X is even: X is either zero or shifts by at least one bit. Both
1790 // uses must observe the same value, so X must not be undef.
1791 if (I->getOperand(0) == I->getOperand(1) &&
1792 isGuaranteedNotToBeUndef(I->getOperand(0), Q.AC, Q.CtxI, Q.DT,
1793 Depth + 1))
1794 Known.Zero.setBit(0);
1795
1796 // Trailing zeros of a right-shifted constant never decrease.
1797 const APInt *C;
1798 if (match(I->getOperand(0), m_APInt(C)))
1799 Known.Zero.setLowBits(C->countr_zero());
1800
1801 // shl X, sub(Y, xor(ctlz(X, true), BitWidth-1)) shifts X so that its MSB
1802 // lands at bit Y, when BitWidth is a power of 2.
1803 const APInt *YC;
1804 Value *X = I->getOperand(0);
1805 if (isPowerOf2_32(BitWidth) &&
1806 match(I->getOperand(1),
1808 m_SpecificInt(BitWidth - 1)))) &&
1809 YC->ult(BitWidth - 1)) {
1810 unsigned Y = YC->getZExtValue();
1811 Known.One.setBit(Y);
1812 Known.Zero.setBitsFrom(Y + 1);
1813 }
1814 break;
1815 }
1816 case Instruction::LShr: {
1817 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1818 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1819 bool ShAmtNonZero) {
1820 return KnownBits::lshr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1821 };
1822 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1823 KF);
1824 // Leading zeros of a left-shifted constant never decrease.
1825 const APInt *C;
1826 if (match(I->getOperand(0), m_APInt(C)))
1827 Known.Zero.setHighBits(C->countl_zero());
1828 break;
1829 }
1830 case Instruction::AShr: {
1831 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1832 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1833 bool ShAmtNonZero) {
1834 return KnownBits::ashr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1835 };
1836 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1837 KF);
1838 break;
1839 }
1840 case Instruction::Sub: {
1843 computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW, NUW,
1844 DemandedElts, Known, Known2, Q, Depth);
1845 break;
1846 }
1847 case Instruction::Add: {
1850 computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW, NUW,
1851 DemandedElts, Known, Known2, Q, Depth);
1852 break;
1853 }
1854 case Instruction::SRem:
1855 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1856 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1857 Known = KnownBits::srem(Known, Known2);
1858 break;
1859
1860 case Instruction::URem:
1861 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1862 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1863 Known = KnownBits::urem(Known, Known2);
1864 break;
1865 case Instruction::Alloca:
1866 Known.Zero.setLowBits(Log2(cast<AllocaInst>(I)->getAlign()));
1867 break;
1868 case Instruction::GetElementPtr: {
1869 // Analyze all of the subscripts of this getelementptr instruction
1870 // to determine if we can prove known low zero bits.
1871 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1872 // Accumulate the constant indices in a separate variable
1873 // to minimize the number of calls to computeForAddSub.
1874 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(I->getType());
1875 APInt AccConstIndices(IndexWidth, 0);
1876
1877 auto AddIndexToKnown = [&](KnownBits IndexBits) {
1878 if (IndexWidth == BitWidth) {
1879 // Note that inbounds does *not* guarantee nsw for the addition, as only
1880 // the offset is signed, while the base address is unsigned.
1881 Known = KnownBits::add(Known, IndexBits);
1882 } else {
1883 // If the index width is smaller than the pointer width, only add the
1884 // value to the low bits.
1885 assert(IndexWidth < BitWidth &&
1886 "Index width can't be larger than pointer width");
1887 Known.insertBits(KnownBits::add(Known.trunc(IndexWidth), IndexBits), 0);
1888 }
1889 };
1890
1892 for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
1893 // TrailZ can only become smaller, short-circuit if we hit zero.
1894 if (Known.isUnknown())
1895 break;
1896
1897 Value *Index = I->getOperand(i);
1898
1899 // Handle case when index is zero.
1900 Constant *CIndex = dyn_cast<Constant>(Index);
1901 if (CIndex && CIndex->isNullValue())
1902 continue;
1903
1904 if (StructType *STy = GTI.getStructTypeOrNull()) {
1905 // Handle struct member offset arithmetic.
1906
1907 assert(CIndex &&
1908 "Access to structure field must be known at compile time");
1909
1910 if (CIndex->getType()->isVectorTy())
1911 Index = CIndex->getSplatValue();
1912
1913 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
1914 const StructLayout *SL = Q.DL.getStructLayout(STy);
1915 uint64_t Offset = SL->getElementOffset(Idx);
1916 AccConstIndices += Offset;
1917 continue;
1918 }
1919
1920 // Handle array index arithmetic.
1921 Type *IndexedTy = GTI.getIndexedType();
1922 if (!IndexedTy->isSized()) {
1923 Known.resetAll();
1924 break;
1925 }
1926
1927 TypeSize Stride = GTI.getSequentialElementStride(Q.DL);
1928 uint64_t StrideInBytes = Stride.getKnownMinValue();
1929 if (!Stride.isScalable()) {
1930 // Fast path for constant offset.
1931 if (auto *CI = dyn_cast<ConstantInt>(Index)) {
1932 AccConstIndices +=
1933 CI->getValue().sextOrTrunc(IndexWidth) * StrideInBytes;
1934 continue;
1935 }
1936 }
1937
1938 KnownBits IndexBits =
1939 computeKnownBits(Index, Q, Depth + 1).sextOrTrunc(IndexWidth);
1940 KnownBits ScalingFactor(IndexWidth);
1941 // Multiply by current sizeof type.
1942 // &A[i] == A + i * sizeof(*A[i]).
1943 if (Stride.isScalable()) {
1944 // For scalable types the only thing we know about sizeof is
1945 // that this is a multiple of the minimum size.
1946 ScalingFactor.Zero.setLowBits(llvm::countr_zero(StrideInBytes));
1947 } else {
1948 ScalingFactor =
1949 KnownBits::makeConstant(APInt(IndexWidth, StrideInBytes));
1950 }
1951 AddIndexToKnown(KnownBits::mul(IndexBits, ScalingFactor));
1952 }
1953 if (!Known.isUnknown() && !AccConstIndices.isZero())
1954 AddIndexToKnown(KnownBits::makeConstant(AccConstIndices));
1955 break;
1956 }
1957 case Instruction::PHI: {
1958 const PHINode *P = cast<PHINode>(I);
1959 BinaryOperator *BO = nullptr;
1960 Value *Start = nullptr, *Step = nullptr;
1961 KnownBits &KnownStart = Known2;
1962 if (matchSimpleRecurrence(P, BO, Start, Step)) {
1963 // Handle the case of a simple two-predecessor recurrence PHI.
1964 // There's a lot more that could theoretically be done here, but
1965 // this is sufficient to catch some interesting cases.
1966 unsigned Opcode = BO->getOpcode();
1967
1968 switch (Opcode) {
1969 // If this is a shift recurrence, we know the bits being shifted in. We
1970 // can combine that with information about the start value of the
1971 // recurrence to conclude facts about the result. If this is a udiv
1972 // recurrence, we know that the result can never exceed either the
1973 // numerator or the start value, whichever is greater.
1974 case Instruction::LShr:
1975 case Instruction::AShr:
1976 case Instruction::Shl:
1977 case Instruction::UDiv:
1978 if (BO->getOperand(0) != I)
1979 break;
1980 [[fallthrough]];
1981
1982 // For a urem recurrence, the result can never exceed the start value. The
1983 // phi could either be the numerator or the denominator.
1984 case Instruction::URem: {
1985 // We have matched a recurrence of the form:
1986 // %iv = [R, %entry], [%iv.next, %backedge]
1987 // %iv.next = shift_op %iv, L
1988
1989 // Recurse with the phi context to avoid concern about whether facts
1990 // inferred hold at original context instruction. TODO: It may be
1991 // correct to use the original context. IF warranted, explore and
1992 // add sufficient tests to cover.
1994 RecQ.CtxI = P;
1995 computeKnownBits(Start, DemandedElts, KnownStart, RecQ, Depth + 1);
1996 switch (Opcode) {
1997 case Instruction::Shl:
1998 // A shl recurrence will only increase the tailing zeros
1999 Known.Zero.setLowBits(KnownStart.countMinTrailingZeros());
2000 break;
2001 case Instruction::LShr:
2002 case Instruction::UDiv:
2003 case Instruction::URem:
2004 // lshr, udiv, and urem recurrences will preserve the leading zeros of
2005 // the start value.
2006 Known.Zero.setHighBits(KnownStart.countMinLeadingZeros());
2007 break;
2008 case Instruction::AShr:
2009 // An ashr recurrence will extend the initial sign bit
2010 Known.Zero.setHighBits(KnownStart.countMinLeadingZeros());
2011 Known.One.setHighBits(KnownStart.countMinLeadingOnes());
2012 break;
2013 }
2014 break;
2015 }
2016
2017 case Instruction::And: {
2018 // Bits that are zero in the start value stay zero, and bits that are
2019 // one in both the start value and the step stay one.
2020 KnownBits KnownStep(BitWidth);
2021 computeKnownBitsForRecurrenceOperands(P, Start, Step, DemandedElts,
2022 KnownStart, KnownStep, Q, Depth);
2023 Known.Zero |= KnownStart.Zero;
2024 Known.One |= KnownStart.One & KnownStep.One;
2025 break;
2026 }
2027
2028 case Instruction::Or: {
2029 // Bits that are zero in both the start value and the step stay zero,
2030 // and bits that are one in the start value stay one.
2031 KnownBits KnownStep(BitWidth);
2032 computeKnownBitsForRecurrenceOperands(P, Start, Step, DemandedElts,
2033 KnownStart, KnownStep, Q, Depth);
2034 Known.Zero |= KnownStart.Zero & KnownStep.Zero;
2035 Known.One |= KnownStart.One;
2036 break;
2037 }
2038
2039 // Check for operations that have the property that if
2040 // both their operands have low zero bits, the result
2041 // will have low zero bits.
2042 case Instruction::Add:
2043 case Instruction::Sub:
2044 case Instruction::Mul: {
2045 // Ok, we have a recurrence of the form {Start,op,Step}. Check for low
2046 // zero bits.
2047 KnownBits KnownStep(BitWidth);
2048 computeKnownBitsForRecurrenceOperands(P, Start, Step, DemandedElts,
2049 KnownStart, KnownStep, Q, Depth);
2050
2051 Known.Zero.setLowBits(std::min(KnownStart.countMinTrailingZeros(),
2052 KnownStep.countMinTrailingZeros()));
2053
2054 auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(BO);
2055 if (!OverflowOp || !Q.IIQ.hasNoSignedWrap(OverflowOp))
2056 break;
2057
2058 switch (Opcode) {
2059 // If initial value of recurrence is nonnegative, and we are adding
2060 // a nonnegative number with nsw, the result can only be nonnegative
2061 // or poison value regardless of the number of times we execute the
2062 // add in phi recurrence. If initial value is negative and we are
2063 // adding a negative number with nsw, the result can only be
2064 // negative or poison value. Similar arguments apply to sub and mul.
2065 //
2066 // (add non-negative, non-negative) --> non-negative
2067 // (add negative, negative) --> negative
2068 case Instruction::Add: {
2069 if (KnownStart.isNonNegative() && KnownStep.isNonNegative())
2070 Known.makeNonNegative();
2071 else if (KnownStart.isNegative() && KnownStep.isNegative())
2072 Known.makeNegative();
2073 break;
2074 }
2075
2076 // (sub nsw non-negative, negative) --> non-negative
2077 // (sub nsw negative, non-negative) --> negative
2078 case Instruction::Sub: {
2079 if (BO->getOperand(0) != I)
2080 break;
2081 if (KnownStart.isNonNegative() && KnownStep.isNegative())
2082 Known.makeNonNegative();
2083 else if (KnownStart.isNegative() && KnownStep.isNonNegative())
2084 Known.makeNegative();
2085 break;
2086 }
2087
2088 // (mul nsw non-negative, non-negative) --> non-negative
2089 case Instruction::Mul:
2090 if (KnownStart.isNonNegative() && KnownStep.isNonNegative())
2091 Known.makeNonNegative();
2092 break;
2093
2094 default:
2095 break;
2096 }
2097 break;
2098 }
2099
2100 default:
2101 break;
2102 }
2103 } else {
2104 IntrinsicInst *II = nullptr;
2105 GEPOperator *GEP = nullptr;
2106 if (matchTwoInputRecurrence<IntrinsicInst>(P, II, Start, Step)) {
2107 // %iv = [<Start>, %entry], [%iv.next, %backedge]
2108 //
2109 // %iv.next = <II>(%iv, <Step>)
2110 // or
2111 // %iv.next = <II>(<Step>, %iv)
2112 Intrinsic::ID IntrinsicID = II->getIntrinsicID();
2113 if (IntrinsicID == Intrinsic::umin || IntrinsicID == Intrinsic::umax) {
2114 KnownBits KnownStep(BitWidth);
2116 P, Start, Step, DemandedElts, KnownStart, KnownStep, Q, Depth);
2117
2118 if (IntrinsicID == Intrinsic::umin) {
2119 Known.Zero.setHighBits(KnownStart.countMinLeadingZeros());
2120 Known.One.setHighBits(std::min(KnownStart.countMinLeadingOnes(),
2121 KnownStep.countMinLeadingOnes()));
2122 } else {
2123 // umax
2124 Known.Zero.setHighBits(std::min(KnownStart.countMinLeadingZeros(),
2125 KnownStep.countMinLeadingZeros()));
2126 Known.One.setHighBits(KnownStart.countMinLeadingOnes());
2127 }
2128 }
2129 } else if (Known.isUnknown() && I->getType()->isPointerTy() &&
2131 GEP->getPointerOperand() == P) {
2132 // Handle a pointer induction variable, whose step is a getelementptr:
2133 // %p = phi ptr [ %start, %preheader ], [ %next, %latch ]
2134 // %next = getelementptr <ty>, ptr %p, <indices>
2135 // Every value %p takes is %start plus a whole number of steps, and
2136 // tz(a + b) >= min(tz(a), tz(b)), so the trailing zeros of %p - and
2137 // hence its alignment - are at least the smaller of those of %start and
2138 // of the step.
2139
2140 // Evaluate the start value on its own incoming edge.
2141 unsigned StartIdx = P->getIncomingValue(0) == Start ? 0 : 1;
2143 RecQ.CtxI = P->getIncomingBlock(StartIdx)->getTerminator();
2144 computeKnownBits(Start, DemandedElts, KnownStart, RecQ, Depth + 1);
2145 Known.Zero.setLowBits(
2146 std::min(KnownStart.countMinTrailingZeros(),
2147 Log2(GEP->getMaxPreservedAlignment(Q.DL))));
2148 }
2149 }
2150
2151 // Unreachable blocks may have zero-operand PHI nodes.
2152 if (P->getNumIncomingValues() == 0)
2153 break;
2154
2155 // Otherwise take the unions of the known bit sets of the operands,
2156 // taking conservative care to avoid excessive recursion.
2157 if (Depth < MaxAnalysisRecursionDepth - 1 && Known.isUnknown()) {
2158 // Skip if every incoming value references to ourself.
2159 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
2160 break;
2161
2162 Known.setAllConflict();
2163 for (const Use &U : P->operands()) {
2164 Value *IncValue;
2165 const PHINode *CtxPhi;
2166 Instruction *CtxI;
2167 breakSelfRecursivePHI(&U, P, IncValue, CtxI, &CtxPhi);
2168 // Skip direct self references.
2169 if (IncValue == P)
2170 continue;
2171
2172 // Change the context instruction to the "edge" that flows into the
2173 // phi. This is important because that is where the value is actually
2174 // "evaluated" even though it is used later somewhere else. (see also
2175 // D69571).
2177
2178 Known2 = KnownBits(BitWidth);
2179
2180 // Recurse, but cap the recursion to one level, because we don't
2181 // want to waste time spinning around in loops.
2182 // TODO: See if we can base recursion limiter on number of incoming phi
2183 // edges so we don't overly clamp analysis.
2184 computeKnownBits(IncValue, DemandedElts, Known2, RecQ,
2186
2187 // See if we can further use a conditional branch into the phi
2188 // to help us determine the range of the value.
2189 if (!Known2.isConstant()) {
2191 Value *RHSValue = nullptr;
2192 BasicBlock *TrueSucc, *FalseSucc;
2193 if (match(
2194 RecQ.CtxI,
2195 m_Br(m_c_ICmp(Pred, m_Specific(IncValue), m_Value(RHSValue)),
2196 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
2197 // Check for cases of duplicate successors.
2198 if ((TrueSucc == CtxPhi->getParent()) !=
2199 (FalseSucc == CtxPhi->getParent())) {
2200 // If we're using the false successor, invert the predicate.
2201 if (FalseSucc == CtxPhi->getParent())
2203 // Get the knownbits implied by the incoming phi condition.
2204 KnownBits RHSKnown = computeKnownBits(
2205 RHSValue, DemandedElts, RecQ, MaxAnalysisRecursionDepth - 1);
2206 if (!RHSKnown.isUnknown()) {
2208 RHSKnown, /*IsSigned=*/ICmpInst::isSigned(Pred));
2209 auto CR = ConstantRange::makeAllowedICmpRegion(Pred, RHSRange);
2210 KnownBits KnownUnion = Known2.unionWith(CR.toKnownBits());
2211 // We can have conflicts here if we are analyzing deadcode (its
2212 // impossible for us reach this BB based the icmp).
2213 if (KnownUnion.hasConflict()) {
2214 // No reason to continue analyzing in a known dead region, so
2215 // just resetAll and break. This will cause us to also exit
2216 // the outer loop.
2217 Known.resetAll();
2218 break;
2219 }
2220 Known2 = KnownUnion;
2221 }
2222 }
2223 }
2224 }
2225
2226 Known = Known.intersectWith(Known2);
2227 // If all bits have been ruled out, there's no need to check
2228 // more operands.
2229 if (Known.isUnknown())
2230 break;
2231 }
2232 }
2233 break;
2234 }
2235 case Instruction::Call:
2236 case Instruction::Invoke: {
2237 // If range metadata is attached to this call, set known bits from that,
2238 // and then intersect with known bits based on other properties of the
2239 // function.
2240 if (MDNode *MD =
2241 Q.IIQ.getMetadata(cast<Instruction>(I), LLVMContext::MD_range))
2243
2244 const auto *CB = cast<CallBase>(I);
2245
2246 if (std::optional<ConstantRange> Range = CB->getRange())
2247 Known = Known.unionWith(Range->toKnownBits());
2248
2249 if (const Value *RV = CB->getReturnedArgOperand()) {
2250 if (RV->getType() == I->getType()) {
2251 computeKnownBits(RV, Known2, Q, Depth + 1);
2252 Known = Known.unionWith(Known2);
2253 // If the function doesn't return properly for all input values
2254 // (e.g. unreachable exits) then there might be conflicts between the
2255 // argument value and the range metadata. Simply discard the known bits
2256 // in case of conflicts.
2257 if (Known.hasConflict())
2258 Known.resetAll();
2259 }
2260 }
2261 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
2262 switch (II->getIntrinsicID()) {
2263 default:
2264 break;
2265 case Intrinsic::abs: {
2266 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2267 bool IntMinIsPoison = match(II->getArgOperand(1), m_One());
2268 Known = Known.unionWith(Known2.abs(IntMinIsPoison));
2269 break;
2270 }
2271 case Intrinsic::bitreverse:
2272 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2273 Known = Known.unionWith(Known2.reverseBits());
2274 break;
2275 case Intrinsic::bswap:
2276 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2277 Known = Known.unionWith(Known2.byteSwap());
2278 break;
2279 case Intrinsic::ctlz: {
2280 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2281 bool ZeroIsPoison = match(II->getArgOperand(1), m_One());
2282 Known = Known.unionWith(Known2.ctlz(ZeroIsPoison));
2283 break;
2284 }
2285 case Intrinsic::cttz: {
2286 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2287 // If we have a known 1, its position is our upper bound.
2288 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
2289 // If this call is poison for 0 input, the result will be less than 2^n.
2290 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
2291 PossibleTZ = std::min(PossibleTZ, BitWidth - 1);
2292 unsigned LowBits = llvm::bit_width(PossibleTZ);
2293 Known.Zero.setBitsFrom(LowBits);
2294 break;
2295 }
2296 case Intrinsic::ctpop: {
2297 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2298 // We can bound the space the count needs. Also, bits known to be zero
2299 // can't contribute to the population.
2300 unsigned BitsPossiblySet = Known2.countMaxPopulation();
2301 unsigned LowBits = llvm::bit_width(BitsPossiblySet);
2302 Known.Zero.setBitsFrom(LowBits);
2303 // TODO: we could bound KnownOne using the lower bound on the number
2304 // of bits which might be set provided by popcnt KnownOne2.
2305 break;
2306 }
2307 case Intrinsic::fshr:
2308 case Intrinsic::fshl: {
2309 const APInt *SA;
2310 if (!match(I->getOperand(2), m_APInt(SA)))
2311 break;
2312
2313 KnownBits Known3(BitWidth);
2314 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2315 computeKnownBits(I->getOperand(1), DemandedElts, Known3, Q, Depth + 1);
2316 Known = II->getIntrinsicID() == Intrinsic::fshl
2317 ? KnownBits::fshl(Known2, Known3, *SA)
2318 : KnownBits::fshr(Known2, Known3, *SA);
2319 break;
2320 }
2321 case Intrinsic::clmul:
2322 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2323 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2324 Known = KnownBits::clmul(Known, Known2);
2325 break;
2326 case Intrinsic::pext:
2327 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2328 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2329 Known = KnownBits::pext(Known, Known2);
2330 break;
2331 case Intrinsic::pdep:
2332 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2333 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2334 Known = KnownBits::pdep(Known, Known2);
2335 break;
2336 case Intrinsic::smulh:
2337 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2338 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2339 Known = KnownBits::mulhs(Known, Known2);
2340 break;
2341 case Intrinsic::umulh:
2342 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2343 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2344 Known = KnownBits::mulhu(Known, Known2);
2345 break;
2346 case Intrinsic::uadd_sat:
2347 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2348 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2349 Known = KnownBits::uadd_sat(Known, Known2);
2350 break;
2351 case Intrinsic::usub_sat:
2352 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2353 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2354 Known = KnownBits::usub_sat(Known, Known2);
2355 break;
2356 case Intrinsic::sadd_sat:
2357 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2358 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2359 Known = KnownBits::sadd_sat(Known, Known2);
2360 break;
2361 case Intrinsic::ssub_sat:
2362 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2363 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2364 Known = KnownBits::ssub_sat(Known, Known2);
2365 break;
2366 // Vec reverse preserves bits from input vec.
2367 case Intrinsic::vector_reverse:
2368 computeKnownBits(I->getOperand(0), DemandedElts.reverseBits(), Known, Q,
2369 Depth + 1);
2370 break;
2371 // for min/max/and/or reduce, any bit common to each element in the
2372 // input vec is set in the output.
2373 case Intrinsic::vector_reduce_and:
2374 case Intrinsic::vector_reduce_or:
2375 case Intrinsic::vector_reduce_umax:
2376 case Intrinsic::vector_reduce_umin:
2377 case Intrinsic::vector_reduce_smax:
2378 case Intrinsic::vector_reduce_smin:
2379 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2380 break;
2381 case Intrinsic::vector_reduce_xor: {
2382 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2383 // The zeros common to all vecs are zero in the output.
2384 // If the number of elements is odd, then the common ones remain. If the
2385 // number of elements is even, then the common ones becomes zeros.
2386 auto *VecTy = cast<VectorType>(I->getOperand(0)->getType());
2387 // Even, so the ones become zeros.
2388 bool EvenCnt = VecTy->getElementCount().isKnownEven();
2389 if (EvenCnt)
2390 Known.Zero |= Known.One;
2391 // Maybe even element count so need to clear ones.
2392 if (VecTy->isScalableTy() || EvenCnt)
2393 Known.One.clearAllBits();
2394 break;
2395 }
2396 case Intrinsic::vector_reduce_add: {
2397 auto *VecTy = dyn_cast<FixedVectorType>(I->getOperand(0)->getType());
2398 if (!VecTy)
2399 break;
2400 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2401 Known = Known.reduceAdd(VecTy->getNumElements());
2402 break;
2403 }
2404 case Intrinsic::umin:
2405 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2406 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2407 Known = KnownBits::umin(Known, Known2);
2408 break;
2409 case Intrinsic::umax:
2410 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2411 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2412 Known = KnownBits::umax(Known, Known2);
2413 break;
2414 case Intrinsic::smin:
2415 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2416 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2417 Known = KnownBits::smin(Known, Known2);
2419 break;
2420 case Intrinsic::smax:
2421 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2422 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2423 Known = KnownBits::smax(Known, Known2);
2425 break;
2426 case Intrinsic::ptrmask: {
2427 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2428
2429 const Value *Mask = I->getOperand(1);
2430 Known2 = KnownBits(Mask->getType()->getScalarSizeInBits());
2431 computeKnownBits(Mask, DemandedElts, Known2, Q, Depth + 1);
2432 // TODO: 1-extend would be more precise.
2433 Known &= Known2.anyextOrTrunc(BitWidth);
2434 break;
2435 }
2436 case Intrinsic::x86_sse42_crc32_64_64:
2437 Known.Zero.setBitsFrom(32);
2438 break;
2439 case Intrinsic::x86_ssse3_phadd_d_128:
2440 case Intrinsic::x86_ssse3_phadd_w_128:
2441 case Intrinsic::x86_avx2_phadd_d:
2442 case Intrinsic::x86_avx2_phadd_w: {
2444 I, DemandedElts, Q, Depth,
2445 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2446 return KnownBits::add(KnownLHS, KnownRHS);
2447 });
2448 break;
2449 }
2450 case Intrinsic::x86_ssse3_phadd_sw_128:
2451 case Intrinsic::x86_avx2_phadd_sw: {
2453 I, DemandedElts, Q, Depth, KnownBits::sadd_sat);
2454 break;
2455 }
2456 case Intrinsic::x86_ssse3_phsub_d_128:
2457 case Intrinsic::x86_ssse3_phsub_w_128:
2458 case Intrinsic::x86_avx2_phsub_d:
2459 case Intrinsic::x86_avx2_phsub_w: {
2461 I, DemandedElts, Q, Depth,
2462 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2463 return KnownBits::sub(KnownLHS, KnownRHS);
2464 });
2465 break;
2466 }
2467 case Intrinsic::x86_ssse3_phsub_sw_128:
2468 case Intrinsic::x86_avx2_phsub_sw: {
2470 I, DemandedElts, Q, Depth, KnownBits::ssub_sat);
2471 break;
2472 }
2473 case Intrinsic::riscv_vsetvli:
2474 case Intrinsic::riscv_vsetvlimax: {
2475 bool HasAVL = II->getIntrinsicID() == Intrinsic::riscv_vsetvli;
2476 const ConstantRange Range = getVScaleRange(II->getFunction(), BitWidth);
2478 cast<ConstantInt>(II->getArgOperand(HasAVL))->getZExtValue());
2479 RISCVVType::VLMUL VLMUL = static_cast<RISCVVType::VLMUL>(
2480 cast<ConstantInt>(II->getArgOperand(1 + HasAVL))->getZExtValue());
2481 uint64_t MaxVLEN =
2482 Range.getUnsignedMax().getZExtValue() * RISCV::RVVBitsPerBlock;
2483 uint64_t MaxVL = MaxVLEN / RISCVVType::getSEWLMULRatio(SEW, VLMUL);
2484
2485 // Result of vsetvli must be not larger than AVL.
2486 if (HasAVL)
2487 if (auto *CI = dyn_cast<ConstantInt>(II->getArgOperand(0)))
2488 MaxVL = std::min(MaxVL, CI->getZExtValue());
2489
2490 unsigned KnownZeroFirstBit = Log2_32(MaxVL) + 1;
2491 if (BitWidth > KnownZeroFirstBit)
2492 Known.Zero.setBitsFrom(KnownZeroFirstBit);
2493 break;
2494 }
2495 case Intrinsic::amdgcn_mbcnt_hi:
2496 case Intrinsic::amdgcn_mbcnt_lo: {
2497 // Wave64 mbcnt_lo returns at most 32 + src1. Otherwise these return at
2498 // most 31 + src1.
2499 KnownBits MbcntKnown(BitWidth);
2500 MbcntKnown.Zero.setBitsFrom(
2501 II->getIntrinsicID() == Intrinsic::amdgcn_mbcnt_lo ? 6 : 5);
2502 computeKnownBits(I->getOperand(1), Known2, Q, Depth + 1);
2503 Known = Known.unionWith(KnownBits::add(MbcntKnown, Known2));
2504 break;
2505 }
2506 case Intrinsic::vscale: {
2507 if (!II->getParent() || !II->getFunction())
2508 break;
2509
2510 Known = getVScaleRange(II->getFunction(), BitWidth).toKnownBits();
2511 break;
2512 }
2513 case Intrinsic::stepvector: {
2514 auto *VecTy = cast<VectorType>(II->getType());
2515 unsigned MinNumElts = VecTy->getElementCount().getKnownMinValue();
2516 if (!isUIntN(BitWidth, MinNumElts))
2517 break;
2518
2519 bool Overflow = false;
2520 APInt MaxNumElts(BitWidth, MinNumElts);
2521 if (VecTy->isScalableTy()) {
2522 if (!II->getParent() || !II->getFunction())
2523 break;
2524 MaxNumElts = getVScaleRange(II->getFunction(), BitWidth)
2526 .umul_ov(MaxNumElts, Overflow);
2527 }
2528
2529 // Give up if the lane count could wrap. Stepvector truncates lane
2530 // indices that do not fit in the element type.
2531 if (Overflow)
2532 break;
2533
2534 Known.Zero.setHighBits((MaxNumElts - 1).countl_zero());
2535 break;
2536 }
2537 }
2538 }
2539 break;
2540 }
2541 case Instruction::ShuffleVector: {
2542 if (auto *Splat = getSplatValue(I)) {
2544 break;
2545 }
2546
2547 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
2548 // FIXME: Do we need to handle ConstantExpr involving shufflevectors?
2549 if (!Shuf) {
2550 Known.resetAll();
2551 return;
2552 }
2553 // For undef elements, we don't know anything about the common state of
2554 // the shuffle result.
2555 APInt DemandedLHS, DemandedRHS;
2556 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) {
2557 Known.resetAll();
2558 return;
2559 }
2560 Known.setAllConflict();
2561 if (!!DemandedLHS) {
2562 const Value *LHS = Shuf->getOperand(0);
2563 computeKnownBits(LHS, DemandedLHS, Known, Q, Depth + 1);
2564 // If we don't know any bits, early out.
2565 if (Known.isUnknown())
2566 break;
2567 }
2568 if (!!DemandedRHS) {
2569 const Value *RHS = Shuf->getOperand(1);
2570 computeKnownBits(RHS, DemandedRHS, Known2, Q, Depth + 1);
2571 Known = Known.intersectWith(Known2);
2572 }
2573 break;
2574 }
2575 case Instruction::InsertElement: {
2576 if (isa<ScalableVectorType>(I->getType())) {
2577 Known.resetAll();
2578 return;
2579 }
2580 const Value *Vec = I->getOperand(0);
2581 const Value *Elt = I->getOperand(1);
2582 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
2583 unsigned NumElts = DemandedElts.getBitWidth();
2584 APInt DemandedVecElts = DemandedElts;
2585 bool NeedsElt = true;
2586 // If we know the index we are inserting too, clear it from Vec check.
2587 if (CIdx && CIdx->getValue().ult(NumElts)) {
2588 DemandedVecElts.clearBit(CIdx->getZExtValue());
2589 NeedsElt = DemandedElts[CIdx->getZExtValue()];
2590 }
2591
2592 Known.setAllConflict();
2593 if (NeedsElt) {
2594 computeKnownBits(Elt, Known, Q, Depth + 1);
2595 // If we don't know any bits, early out.
2596 if (Known.isUnknown())
2597 break;
2598 }
2599
2600 if (!DemandedVecElts.isZero()) {
2601 computeKnownBits(Vec, DemandedVecElts, Known2, Q, Depth + 1);
2602 Known = Known.intersectWith(Known2);
2603 }
2604 break;
2605 }
2606 case Instruction::ExtractElement: {
2607 // Look through extract element. If the index is non-constant or
2608 // out-of-range demand all elements, otherwise just the extracted element.
2609 const Value *Vec = I->getOperand(0);
2610 const Value *Idx = I->getOperand(1);
2611 auto *CIdx = dyn_cast<ConstantInt>(Idx);
2612 if (isa<ScalableVectorType>(Vec->getType())) {
2613 // FIXME: there's probably *something* we can do with scalable vectors
2614 Known.resetAll();
2615 break;
2616 }
2617 unsigned NumElts = cast<FixedVectorType>(Vec->getType())->getNumElements();
2618 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
2619 if (CIdx && CIdx->getValue().ult(NumElts))
2620 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
2621 computeKnownBits(Vec, DemandedVecElts, Known, Q, Depth + 1);
2622 break;
2623 }
2624 case Instruction::ExtractValue:
2625 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
2627 if (EVI->getNumIndices() != 1) break;
2628 if (EVI->getIndices()[0] == 0) {
2629 switch (II->getIntrinsicID()) {
2630 default: break;
2631 case Intrinsic::uadd_with_overflow:
2632 case Intrinsic::sadd_with_overflow:
2634 true, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2635 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2636 break;
2637 case Intrinsic::usub_with_overflow:
2638 case Intrinsic::ssub_with_overflow:
2640 false, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2641 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2642 break;
2643 case Intrinsic::umul_with_overflow:
2644 case Intrinsic::smul_with_overflow:
2645 computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false,
2646 false, DemandedElts, Known, Known2, Q, Depth);
2647 break;
2648 }
2649 }
2650 }
2651 break;
2652 case Instruction::Freeze:
2653 if (isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CtxI, Q.DT,
2654 Depth + 1))
2655 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2656 break;
2657 }
2658}
2659
2660/// Determine which bits of V are known to be either zero or one and return
2661/// them.
2662KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
2663 const SimplifyQuery &Q, unsigned Depth) {
2664 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2665 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);
2666 return Known;
2667}
2668
2669/// Determine which bits of V are known to be either zero or one and return
2670/// them.
2672 unsigned Depth) {
2673 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2675 return Known;
2676}
2677
2678/// Determine which bits of V are known to be either zero or one and return
2679/// them in the Known bit set.
2680///
2681/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that
2682/// we cannot optimize based on the assumption that it is zero without changing
2683/// it to be an explicit zero. If we don't change it to zero, other code could
2684/// optimized based on the contradictory assumption that it is non-zero.
2685/// Because instcombine aggressively folds operations with undef args anyway,
2686/// this won't lose us code quality.
2687///
2688/// This function is defined on values with integer type, values with pointer
2689/// type, and vectors of integers. In the case
2690/// where V is a vector, known zero, and known one values are the
2691/// same width as the vector element, and the bit is set only if it is true
2692/// for all of the demanded elements in the vector specified by DemandedElts.
2693void computeKnownBits(const Value *V, const APInt &DemandedElts,
2694 KnownBits &Known, const SimplifyQuery &Q,
2695 unsigned Depth) {
2696 if (!DemandedElts) {
2697 // No demanded elts, better to assume we don't know anything.
2698 Known.resetAll();
2699 return;
2700 }
2701
2702 assert(V && "No Value?");
2703 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2704
2705#ifndef NDEBUG
2706 Type *Ty = V->getType();
2707 unsigned BitWidth = Known.getBitWidth();
2708
2709 assert((Ty->isIntOrIntVectorTy(BitWidth) || Ty->isPtrOrPtrVectorTy()) &&
2710 "Not integer or pointer type!");
2711
2712 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
2713 assert(
2714 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
2715 "DemandedElt width should equal the fixed vector number of elements");
2716 } else {
2717 assert(DemandedElts == APInt(1, 1) &&
2718 "DemandedElt width should be 1 for scalars or scalable vectors");
2719 }
2720
2721 Type *ScalarTy = Ty->getScalarType();
2722 if (ScalarTy->isPointerTy()) {
2723 assert(BitWidth == Q.DL.getPointerTypeSizeInBits(ScalarTy) &&
2724 "V and Known should have same BitWidth");
2725 } else {
2726 assert(BitWidth == Q.DL.getTypeSizeInBits(ScalarTy) &&
2727 "V and Known should have same BitWidth");
2728 }
2729#endif
2730
2731 const APInt *C;
2732 if (match(V, m_APInt(C))) {
2733 // We know all of the bits for a scalar constant or a splat vector constant!
2735 return;
2736 }
2737 // Null and aggregate-zero are all-zeros.
2739 Known.setAllZero();
2740 return;
2741 }
2742 // Handle a constant vector by taking the intersection of the known bits of
2743 // each element.
2745 assert(!isa<ScalableVectorType>(V->getType()));
2746 // We know that CDV must be a vector of integers. Take the intersection of
2747 // each element.
2748 Known.setAllConflict();
2749 for (unsigned i = 0, e = CDV->getNumElements(); i != e; ++i) {
2750 if (!DemandedElts[i])
2751 continue;
2752 APInt Elt = CDV->getElementAsAPInt(i);
2753 Known.Zero &= ~Elt;
2754 Known.One &= Elt;
2755 }
2756 if (Known.hasConflict())
2757 Known.resetAll();
2758 return;
2759 }
2760
2761 if (const auto *CV = dyn_cast<ConstantVector>(V)) {
2762 assert(!isa<ScalableVectorType>(V->getType()));
2763 // We know that CV must be a vector of integers. Take the intersection of
2764 // each element.
2765 Known.setAllConflict();
2766 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
2767 if (!DemandedElts[i])
2768 continue;
2769 Constant *Element = CV->getAggregateElement(i);
2770 if (isa<PoisonValue>(Element))
2771 continue;
2772 auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
2773 if (!ElementCI) {
2774 Known.resetAll();
2775 return;
2776 }
2777 const APInt &Elt = ElementCI->getValue();
2778 Known.Zero &= ~Elt;
2779 Known.One &= Elt;
2780 }
2781 if (Known.hasConflict())
2782 Known.resetAll();
2783 return;
2784 }
2785
2786 // Start out not knowing anything.
2787 Known.resetAll();
2788
2789 // We can't imply anything about undefs.
2790 if (isa<UndefValue>(V))
2791 return;
2792
2793 // There's no point in looking through other users of ConstantData for
2794 // assumptions. Confirm that we've handled them all.
2795 assert(!isa<ConstantData>(V) && "Unhandled constant data!");
2796
2797 if (const auto *A = dyn_cast<Argument>(V))
2798 if (std::optional<ConstantRange> Range = A->getRange())
2799 Known = Range->toKnownBits();
2800
2801 // All recursive calls that increase depth must come after this.
2803 return;
2804
2805 // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
2806 // the bits of its aliasee.
2807 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
2808 if (!GA->isInterposable())
2809 computeKnownBits(GA->getAliasee(), Known, Q, Depth + 1);
2810 return;
2811 }
2812
2813 if (const Operator *I = dyn_cast<Operator>(V))
2814 computeKnownBitsFromOperator(I, DemandedElts, Known, Q, Depth);
2815 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2816 if (std::optional<ConstantRange> CR = GV->getAbsoluteSymbolRange())
2817 Known = CR->toKnownBits();
2818 }
2819
2820 // Aligned pointers have trailing zeros - refine Known.Zero set
2821 if (isa<PointerType>(V->getType())) {
2822 Align Alignment = V->getPointerAlignment(Q.DL);
2823 Known.Zero.setLowBits(Log2(Alignment));
2824 }
2825
2826 // computeKnownBitsFromContext strictly refines Known.
2827 // Therefore, we run them after computeKnownBitsFromOperator.
2828
2829 // Check whether we can determine known bits from context such as assumes.
2831}
2832
2833/// Try to detect a recurrence that the value of the induction variable is
2834/// always a power of two (or zero).
2835static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero,
2836 SimplifyQuery &Q, unsigned Depth) {
2837 BinaryOperator *BO = nullptr;
2838 Value *Start = nullptr, *Step = nullptr;
2839 if (!matchSimpleRecurrence(PN, BO, Start, Step))
2840 return false;
2841
2842 // Initial value must be a power of two.
2843 for (const Use &U : PN->operands()) {
2844 if (U.get() == Start) {
2845 // Initial value comes from a different BB, need to adjust context
2846 // instruction for analysis.
2847 Q.CtxI = PN->getIncomingBlock(U)->getTerminator();
2848 if (!isKnownToBeAPowerOfTwo(Start, OrZero, Q, Depth))
2849 return false;
2850 }
2851 }
2852
2853 // Except for Mul, the induction variable must be on the left side of the
2854 // increment expression, otherwise its value can be arbitrary.
2855 if (BO->getOpcode() != Instruction::Mul && BO->getOperand(1) != Step)
2856 return false;
2857
2858 Q.CtxI = BO->getParent()->getTerminator();
2859 switch (BO->getOpcode()) {
2860 case Instruction::Mul:
2861 // Power of two is closed under multiplication.
2862 return (OrZero || Q.IIQ.hasNoUnsignedWrap(BO) ||
2863 Q.IIQ.hasNoSignedWrap(BO)) &&
2864 isKnownToBeAPowerOfTwo(Step, OrZero, Q, Depth);
2865 case Instruction::SDiv:
2866 // Start value must not be signmask for signed division, so simply being a
2867 // power of two is not sufficient, and it has to be a constant.
2868 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2869 return false;
2870 [[fallthrough]];
2871 case Instruction::UDiv:
2872 // Divisor must be a power of two.
2873 // If OrZero is false, cannot guarantee induction variable is non-zero after
2874 // division, same for Shr, unless it is exact division.
2875 return (OrZero || Q.IIQ.isExact(BO)) &&
2876 isKnownToBeAPowerOfTwo(Step, false, Q, Depth);
2877 case Instruction::Shl:
2878 return OrZero || Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO);
2879 case Instruction::AShr:
2880 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2881 return false;
2882 [[fallthrough]];
2883 case Instruction::LShr:
2884 return OrZero || Q.IIQ.isExact(BO);
2885 default:
2886 return false;
2887 }
2888}
2889
2890/// Return true if we can infer that \p V is known to be a power of 2 from
2891/// dominating condition \p Cond (e.g., ctpop(V) == 1).
2892static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero,
2893 const Value *Cond,
2894 bool CondIsTrue) {
2896 const APInt *RHSC;
2897 if (!match(Cond, m_ICmp(Pred, m_Ctpop(m_Specific(V)), m_APInt(RHSC))))
2898 return false;
2899 if (!CondIsTrue)
2901 // ctpop(V) u< 2
2902 if (OrZero && Pred == ICmpInst::ICMP_ULT && *RHSC == 2)
2903 return true;
2904 // ctpop(V) == 1
2905 return Pred == ICmpInst::ICMP_EQ && *RHSC == 1;
2906}
2907
2908/// Return true if the given value is known to have exactly one
2909/// bit set when defined. For vectors return true if every element is known to
2910/// be a power of two when defined. Supports values with integer or pointer
2911/// types and vectors of integers.
2912bool llvm::isKnownToBeAPowerOfTwo(const Value *V, bool OrZero,
2913 const SimplifyQuery &Q, unsigned Depth) {
2914 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2915
2916 if (isa<Constant>(V))
2917 return OrZero ? match(V, m_Power2OrZero()) : match(V, m_Power2());
2918
2919 // i1 is by definition a power of 2 or zero.
2920 if (OrZero && V->getType()->getScalarSizeInBits() == 1)
2921 return true;
2922
2923 // Try to infer from assumptions.
2924 if (Q.AC && Q.CtxI) {
2925 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
2926 if (!AssumeVH)
2927 continue;
2928 CallInst *I = cast<CallInst>(AssumeVH);
2929 if (isImpliedToBeAPowerOfTwoFromCond(V, OrZero, I->getArgOperand(0),
2930 /*CondIsTrue=*/true) &&
2932 return true;
2933 }
2934 }
2935
2936 // Handle dominating conditions.
2937 if (Q.DC && Q.CtxI && Q.DT) {
2938 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
2939 Value *Cond = BI->getCondition();
2940
2941 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
2943 /*CondIsTrue=*/true) &&
2944 Q.DT->dominates(Edge0, Q.CtxI->getParent()))
2945 return true;
2946
2947 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
2949 /*CondIsTrue=*/false) &&
2950 Q.DT->dominates(Edge1, Q.CtxI->getParent()))
2951 return true;
2952 }
2953 }
2954
2955 auto *I = dyn_cast<Instruction>(V);
2956 if (!I)
2957 return false;
2958
2959 if (Q.CtxI && match(V, m_VScale())) {
2960 const Function *F = Q.CtxI->getFunction();
2961 // The vscale_range indicates vscale is a power-of-two.
2962 return F->hasFnAttribute(Attribute::VScaleRange);
2963 }
2964
2965 // 1 << X is clearly a power of two if the one is not shifted off the end. If
2966 // it is shifted off the end then the result is undefined.
2967 if (match(I, m_Shl(m_One(), m_Value())))
2968 return true;
2969
2970 // (signmask) >>l X is clearly a power of two if the one is not shifted off
2971 // the bottom. If it is shifted off the bottom then the result is undefined.
2972 if (match(I, m_LShr(m_SignMask(), m_Value())))
2973 return true;
2974
2975 // The remaining tests are all recursive, so bail out if we hit the limit.
2977 return false;
2978
2979 switch (I->getOpcode()) {
2980 case Instruction::ZExt:
2981 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2982 case Instruction::Trunc:
2983 return OrZero && isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2984 case Instruction::Shl:
2985 if (OrZero || Q.IIQ.hasNoUnsignedWrap(I) || Q.IIQ.hasNoSignedWrap(I))
2986 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2987 return false;
2988 case Instruction::LShr:
2989 if (OrZero || Q.IIQ.isExact(cast<BinaryOperator>(I)))
2990 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2991 return false;
2992 case Instruction::UDiv:
2994 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2995 return false;
2996 case Instruction::Mul:
2997 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
2998 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth) &&
2999 (OrZero || isKnownNonZero(I, Q, Depth));
3000 case Instruction::And:
3001 // A power of two and'd with anything is a power of two or zero.
3002 if (OrZero &&
3003 (isKnownToBeAPowerOfTwo(I->getOperand(1), /*OrZero*/ true, Q, Depth) ||
3004 isKnownToBeAPowerOfTwo(I->getOperand(0), /*OrZero*/ true, Q, Depth)))
3005 return true;
3006 // X & (-X) is always a power of two or zero.
3007 if (match(I->getOperand(0), m_Neg(m_Specific(I->getOperand(1)))) ||
3008 match(I->getOperand(1), m_Neg(m_Specific(I->getOperand(0)))))
3009 return OrZero || isKnownNonZero(I->getOperand(0), Q, Depth);
3010 return false;
3011 case Instruction::Add: {
3012 // Adding a power-of-two or zero to the same power-of-two or zero yields
3013 // either the original power-of-two, a larger power-of-two or zero.
3015 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) ||
3016 Q.IIQ.hasNoSignedWrap(VOBO)) {
3017 if (match(I->getOperand(0),
3018 m_c_And(m_Specific(I->getOperand(1)), m_Value())) &&
3019 isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth))
3020 return true;
3021 if (match(I->getOperand(1),
3022 m_c_And(m_Specific(I->getOperand(0)), m_Value())) &&
3023 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth))
3024 return true;
3025
3026 unsigned BitWidth = V->getType()->getScalarSizeInBits();
3027 KnownBits LHSBits(BitWidth);
3028 computeKnownBits(I->getOperand(0), LHSBits, Q, Depth);
3029
3030 KnownBits RHSBits(BitWidth);
3031 computeKnownBits(I->getOperand(1), RHSBits, Q, Depth);
3032 // If i8 V is a power of two or zero:
3033 // ZeroBits: 1 1 1 0 1 1 1 1
3034 // ~ZeroBits: 0 0 0 1 0 0 0 0
3035 if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2())
3036 // If OrZero isn't set, we cannot give back a zero result.
3037 // Make sure either the LHS or RHS has a bit set.
3038 if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue())
3039 return true;
3040 }
3041
3042 // LShr(UINT_MAX, Y) + 1 is a power of two (if add is nuw) or zero.
3043 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO))
3044 if (match(I, m_Add(m_LShr(m_AllOnes(), m_Value()), m_One())))
3045 return true;
3046 return false;
3047 }
3048 case Instruction::Select:
3049 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
3050 isKnownToBeAPowerOfTwo(I->getOperand(2), OrZero, Q, Depth);
3051 case Instruction::PHI: {
3052 // A PHI node is power of two if all incoming values are power of two, or if
3053 // it is an induction variable where in each step its value is a power of
3054 // two.
3055 auto *PN = cast<PHINode>(I);
3057
3058 // Check if it is an induction variable and always power of two.
3059 if (isPowerOfTwoRecurrence(PN, OrZero, RecQ, Depth))
3060 return true;
3061
3062 // Recursively check all incoming values. Limit recursion to 2 levels, so
3063 // that search complexity is limited to number of operands^2.
3064 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
3065 return llvm::all_of(PN->operands(), [&](const Use &U) {
3066 // Value is power of 2 if it is coming from PHI node itself by induction.
3067 if (U.get() == PN)
3068 return true;
3069
3070 // Change the context instruction to the incoming block where it is
3071 // evaluated.
3072 RecQ.CtxI = PN->getIncomingBlock(U)->getTerminator();
3073 return isKnownToBeAPowerOfTwo(U.get(), OrZero, RecQ, NewDepth);
3074 });
3075 }
3076 case Instruction::Invoke:
3077 case Instruction::Call: {
3078 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
3079 switch (II->getIntrinsicID()) {
3080 case Intrinsic::umax:
3081 case Intrinsic::smax:
3082 case Intrinsic::umin:
3083 case Intrinsic::smin:
3084 return isKnownToBeAPowerOfTwo(II->getArgOperand(1), OrZero, Q, Depth) &&
3085 isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
3086 // bswap/bitreverse just move around bits, but don't change any 1s/0s
3087 // thus dont change pow2/non-pow2 status.
3088 case Intrinsic::bitreverse:
3089 case Intrinsic::bswap:
3090 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
3091 case Intrinsic::fshr:
3092 case Intrinsic::fshl:
3093 // If Op0 == Op1, this is a rotate. is_pow2(rotate(x, y)) == is_pow2(x)
3094 if (II->getArgOperand(0) == II->getArgOperand(1))
3095 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
3096 break;
3097 case Intrinsic::riscv_vsetvlimax:
3098 // VLMAX is VLEN * LMUL / SEW, which is always a non-zero power of two
3099 // for any valid vtype, so it is a power of two regardless of OrZero.
3100 return true;
3101 case Intrinsic::read_register:
3102 case Intrinsic::read_volatile_register: {
3103 // The RISC-V vlenb CSR holds VLEN/8, which is always a non-zero power
3104 // of two, so it is a power of two regardless of OrZero.
3105 const Module *M = II->getModule();
3106 if (!M || !M->getTargetTriple().isRISCV())
3107 break;
3108 return isReadVLENB(*II);
3109 }
3110 default:
3111 break;
3112 }
3113 }
3114 return false;
3115 }
3116 default:
3117 return false;
3118 }
3119}
3120
3121/// Test whether a GEP's result is known to be non-null.
3122///
3123/// Uses properties inherent in a GEP to try to determine whether it is known
3124/// to be non-null.
3125///
3126/// Currently this routine does not support vector GEPs.
3127static bool isGEPKnownNonNull(const GEPOperator *GEP, const SimplifyQuery &Q,
3128 unsigned Depth) {
3129 const Function *F = nullptr;
3130 if (const Instruction *I = dyn_cast<Instruction>(GEP))
3131 F = I->getFunction();
3132
3133 // If the gep is nuw or inbounds with invalid null pointer, then the GEP
3134 // may be null iff the base pointer is null and the offset is zero.
3135 if (!GEP->hasNoUnsignedWrap() &&
3136 !(GEP->isInBounds() &&
3137 !NullPointerIsDefined(F, GEP->getPointerAddressSpace())))
3138 return false;
3139
3140 // FIXME: Support vector-GEPs.
3141 assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP");
3142
3143 // If the base pointer is non-null, we cannot walk to a null address with an
3144 // inbounds GEP in address space zero.
3145 if (isKnownNonZero(GEP->getPointerOperand(), Q, Depth))
3146 return true;
3147
3148 // Walk the GEP operands and see if any operand introduces a non-zero offset.
3149 // If so, then the GEP cannot produce a null pointer, as doing so would
3150 // inherently violate the inbounds contract within address space zero.
3152 GTI != GTE; ++GTI) {
3153 // Struct types are easy -- they must always be indexed by a constant.
3154 if (StructType *STy = GTI.getStructTypeOrNull()) {
3155 ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
3156 unsigned ElementIdx = OpC->getZExtValue();
3157 const StructLayout *SL = Q.DL.getStructLayout(STy);
3158 uint64_t ElementOffset = SL->getElementOffset(ElementIdx);
3159 if (ElementOffset > 0)
3160 return true;
3161 continue;
3162 }
3163
3164 // If we have a zero-sized type, the index doesn't matter. Keep looping.
3165 if (GTI.getSequentialElementStride(Q.DL).isZero())
3166 continue;
3167
3168 // Fast path the constant operand case both for efficiency and so we don't
3169 // increment Depth when just zipping down an all-constant GEP.
3170 if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
3171 if (!OpC->isZero())
3172 return true;
3173 continue;
3174 }
3175
3176 // We post-increment Depth here because while isKnownNonZero increments it
3177 // as well, when we pop back up that increment won't persist. We don't want
3178 // to recurse 10k times just because we have 10k GEP operands. We don't
3179 // bail completely out because we want to handle constant GEPs regardless
3180 // of depth.
3182 continue;
3183
3184 if (isKnownNonZero(GTI.getOperand(), Q, Depth))
3185 return true;
3186 }
3187
3188 return false;
3189}
3190
3192 const Instruction *CtxI,
3193 const DominatorTree *DT) {
3194 assert(!isa<Constant>(V) && "Called for constant?");
3195
3196 if (!CtxI || !DT)
3197 return false;
3198
3199 unsigned NumUsesExplored = 0;
3200 for (auto &U : V->uses()) {
3201 // Avoid massive lists
3202 if (NumUsesExplored >= DomConditionsMaxUses)
3203 break;
3204 NumUsesExplored++;
3205
3206 const Instruction *UI = cast<Instruction>(U.getUser());
3207 // If the value is used as an argument to a call or invoke, then argument
3208 // attributes may provide an answer about null-ness.
3209 if (V->getType()->isPointerTy()) {
3210 if (const auto *CB = dyn_cast<CallBase>(UI)) {
3211 if (CB->isArgOperand(&U) &&
3212 CB->paramHasNonNullAttr(CB->getArgOperandNo(&U),
3213 /*AllowUndefOrPoison=*/false) &&
3214 DT->dominates(CB, CtxI))
3215 return true;
3216 }
3217 }
3218
3219 // If the value is used as a load/store, then the pointer must be non null.
3220 if (V == getLoadStorePointerOperand(UI)) {
3222 V->getType()->getPointerAddressSpace()) &&
3223 DT->dominates(UI, CtxI))
3224 return true;
3225 }
3226
3227 if ((match(UI, m_IDiv(m_Value(), m_Specific(V))) ||
3228 match(UI, m_IRem(m_Value(), m_Specific(V)))) &&
3229 isValidAssumeForContext(UI, CtxI, DT))
3230 return true;
3231
3232 // Consider only compare instructions uniquely controlling a branch
3233 Value *RHS;
3235 bool NonNullIfTrue;
3236 if (match(UI, m_c_ICmp(Pred, m_Specific(V), m_Value(RHS)))) {
3237 if (cmpExcludesZero(Pred, RHS))
3238 NonNullIfTrue = true;
3240 NonNullIfTrue = false;
3241 else
3242 continue;
3243 } else if (UI->getType()->isIntegerTy(1) &&
3244 match(UI, m_Trunc(m_Specific(V)))) {
3245 NonNullIfTrue = true;
3246 } else
3247 continue;
3248
3251 for (const auto *CmpU : UI->users()) {
3252 assert(WorkList.empty() && "Should be!");
3253 if (Visited.insert(CmpU).second)
3254 WorkList.push_back(CmpU);
3255
3256 while (!WorkList.empty()) {
3257 auto *Curr = WorkList.pop_back_val();
3258
3259 // If a user is an AND, add all its users to the work list. We only
3260 // propagate "pred != null" condition through AND because it is only
3261 // correct to assume that all conditions of AND are met in true branch.
3262 // TODO: Support similar logic of OR and EQ predicate?
3263 if (NonNullIfTrue)
3264 if (match(Curr, m_LogicalAnd(m_Value(), m_Value()))) {
3265 for (const auto *CurrU : Curr->users())
3266 if (Visited.insert(CurrU).second)
3267 WorkList.push_back(CurrU);
3268 continue;
3269 }
3270
3271 if (const CondBrInst *BI = dyn_cast<CondBrInst>(Curr)) {
3272 BasicBlock *NonNullSuccessor =
3273 BI->getSuccessor(NonNullIfTrue ? 0 : 1);
3274 BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor);
3275 if (DT->dominates(Edge, CtxI->getParent()))
3276 return true;
3277 } else if (NonNullIfTrue && isGuard(Curr) &&
3278 DT->dominates(cast<Instruction>(Curr), CtxI)) {
3279 return true;
3280 }
3281 }
3282 }
3283 }
3284
3285 return false;
3286}
3287
3288/// Does the 'Range' metadata (which must be a valid MD_range operand list)
3289/// ensure that the value it's attached to is never Value? 'RangeType' is
3290/// is the type of the value described by the range.
3291static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) {
3292 const unsigned NumRanges = Ranges->getNumOperands() / 2;
3293 assert(NumRanges >= 1);
3294 for (unsigned i = 0; i < NumRanges; ++i) {
3296 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
3298 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
3299 ConstantRange Range(Lower->getValue(), Upper->getValue());
3300 if (Range.contains(Value))
3301 return false;
3302 }
3303 return true;
3304}
3305
3306/// Try to detect a recurrence that monotonically increases/decreases from a
3307/// non-zero starting value. These are common as induction variables.
3308static bool isNonZeroRecurrence(const PHINode *PN) {
3309 BinaryOperator *BO = nullptr;
3310 Value *Start = nullptr, *Step = nullptr;
3311 const APInt *StartC, *StepC;
3312 if (!matchSimpleRecurrence(PN, BO, Start, Step) ||
3313 !match(Start, m_APInt(StartC)) || StartC->isZero())
3314 return false;
3315
3316 switch (BO->getOpcode()) {
3317 case Instruction::Add:
3318 // Starting from non-zero and stepping away from zero can never wrap back
3319 // to zero.
3320 return BO->hasNoUnsignedWrap() ||
3321 (BO->hasNoSignedWrap() && match(Step, m_APInt(StepC)) &&
3322 StartC->isNegative() == StepC->isNegative());
3323 case Instruction::Mul:
3324 return (BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap()) &&
3325 match(Step, m_APInt(StepC)) && !StepC->isZero();
3326 case Instruction::Shl:
3327 return BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap();
3328 case Instruction::AShr:
3329 case Instruction::LShr:
3330 return BO->isExact();
3331 case Instruction::Or:
3332 return true;
3333 default:
3334 return false;
3335 }
3336}
3337
3338static bool matchOpWithOpEqZero(Value *Op0, Value *Op1) {
3340 m_Specific(Op1), m_Zero()))) ||
3342 m_Specific(Op0), m_Zero())));
3343}
3344
3345/// Return true if "addr(P) + Off" cannot be zero. The object P points to
3346/// cannot cross the unsigned address space boundary, including the address one
3347/// past its end, so an offset that stays within the object cannot wrap around
3348/// to zero. This holds even if the object starts at the zero address, which is
3349/// all that null_pointer_is_valid permits, so a non-zero offset is enough.
3351 Value *Y) {
3352 const APInt *Off;
3353 if (!match(Y, m_APInt(Off)) || Off->isZero())
3354 return false;
3355
3356 // ptrtoaddr always produces the address. ptrtoint only does so when its
3357 // result is at least as wide as the address: a narrower result is truncated
3358 // and may wrap to zero, while bits above the address can only make the
3359 // result more non-zero.
3360 Value *P;
3361 if (!match(X, m_PtrToAddr(m_Value(P))) &&
3362 !(match(X, m_PtrToInt(m_Value(P))) &&
3363 X->getType()->getScalarSizeInBits() >=
3364 Q.DL.getAddressSizeInBits(P->getType())))
3365 return false;
3366
3367 // A vector of pointers has no single object whose size can be taken, and
3368 // getPointerDereferenceableBytes() requires a plain pointer.
3369 if (!P->getType()->isPointerTy())
3370 return false;
3371
3372 bool CanBeNull;
3373 uint64_t DerefBytes = P->getPointerDereferenceableBytes(
3374 Q.DL, CanBeNull, /*CanBeFreed=*/nullptr);
3375 if (CanBeNull || DerefBytes == 0 || !Off->ule(DerefBytes))
3376 return false;
3377
3378 return true;
3379}
3380
3381static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q,
3382 unsigned BitWidth, Value *X, Value *Y, bool NSW,
3383 bool NUW, unsigned Depth) {
3385 return true;
3386
3387 // (X + (X != 0)) is non zero
3388 if (matchOpWithOpEqZero(X, Y))
3389 return true;
3390
3391 if (NUW)
3392 return isKnownNonZero(Y, DemandedElts, Q, Depth) ||
3393 isKnownNonZero(X, DemandedElts, Q, Depth);
3394
3395 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
3396 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
3397
3398 // If X and Y are both non-negative (as signed values) then their sum is not
3399 // zero unless both X and Y are zero.
3400 if (XKnown.isNonNegative() && YKnown.isNonNegative())
3401 if (isKnownNonZero(Y, DemandedElts, Q, Depth) ||
3402 isKnownNonZero(X, DemandedElts, Q, Depth))
3403 return true;
3404
3405 // If X and Y are both negative (as signed values) then their sum is not
3406 // zero unless both X and Y equal INT_MIN.
3407 if (XKnown.isNegative() && YKnown.isNegative()) {
3409 // The sign bit of X is set. If some other bit is set then X is not equal
3410 // to INT_MIN.
3411 if (XKnown.One.intersects(Mask))
3412 return true;
3413 // The sign bit of Y is set. If some other bit is set then Y is not equal
3414 // to INT_MIN.
3415 if (YKnown.One.intersects(Mask))
3416 return true;
3417 }
3418
3419 // The sum of a non-negative number and a power of two is not zero.
3420 if (XKnown.isNonNegative() &&
3421 isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Q, Depth))
3422 return true;
3423 if (YKnown.isNonNegative() &&
3424 isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Q, Depth))
3425 return true;
3426
3427 return KnownBits::add(XKnown, YKnown, NSW, NUW).isNonZero();
3428}
3429
3430static bool isNonZeroSub(const APInt &DemandedElts, const SimplifyQuery &Q,
3431 unsigned BitWidth, Value *X, Value *Y,
3432 unsigned Depth) {
3433 // (X - (X != 0)) is non zero
3434 // ((X != 0) - X) is non zero
3435 if (matchOpWithOpEqZero(X, Y))
3436 return true;
3437
3438 // TODO: Move this case into isKnownNonEqual().
3439 if (auto *C = dyn_cast<Constant>(X))
3440 if (C->isNullValue() && isKnownNonZero(Y, DemandedElts, Q, Depth))
3441 return true;
3442
3443 return ::isKnownNonEqual(X, Y, DemandedElts, Q, Depth);
3444}
3445
3446static bool isNonZeroMul(const APInt &DemandedElts, const SimplifyQuery &Q,
3447 unsigned BitWidth, Value *X, Value *Y, bool NSW,
3448 bool NUW, unsigned Depth) {
3449 // If X and Y are non-zero then so is X * Y as long as the multiplication
3450 // does not overflow.
3451 if (NSW || NUW)
3452 return isKnownNonZero(X, DemandedElts, Q, Depth) &&
3453 isKnownNonZero(Y, DemandedElts, Q, Depth);
3454
3455 // If either X or Y is odd, then if the other is non-zero the result can't
3456 // be zero.
3457 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
3458 if (XKnown.One[0])
3459 return isKnownNonZero(Y, DemandedElts, Q, Depth);
3460
3461 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
3462 if (YKnown.One[0])
3463 return XKnown.isNonZero() || isKnownNonZero(X, DemandedElts, Q, Depth);
3464
3465 // If there exists any subset of X (sX) and subset of Y (sY) s.t sX * sY is
3466 // non-zero, then X * Y is non-zero. We can find sX and sY by just taking
3467 // the lowest known One of X and Y. If they are non-zero, the result
3468 // must be non-zero. We can check if LSB(X) * LSB(Y) != 0 by doing
3469 // X.CountLeadingZeros + Y.CountLeadingZeros < BitWidth.
3470 return (XKnown.countMaxTrailingZeros() + YKnown.countMaxTrailingZeros()) <
3471 BitWidth;
3472}
3473
3474static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts,
3475 const SimplifyQuery &Q, const KnownBits &KnownVal,
3476 unsigned Depth) {
3477 auto ShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
3478 switch (I->getOpcode()) {
3479 case Instruction::Shl:
3480 return Lhs.shl(Rhs);
3481 case Instruction::LShr:
3482 return Lhs.lshr(Rhs);
3483 case Instruction::AShr:
3484 return Lhs.ashr(Rhs);
3485 default:
3486 llvm_unreachable("Unknown Shift Opcode");
3487 }
3488 };
3489
3490 auto InvShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
3491 switch (I->getOpcode()) {
3492 case Instruction::Shl:
3493 return Lhs.lshr(Rhs);
3494 case Instruction::LShr:
3495 case Instruction::AShr:
3496 return Lhs.shl(Rhs);
3497 default:
3498 llvm_unreachable("Unknown Shift Opcode");
3499 }
3500 };
3501
3502 if (KnownVal.isUnknown())
3503 return false;
3504
3505 KnownBits KnownCnt =
3506 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
3507 APInt MaxShift = KnownCnt.getMaxValue();
3508 unsigned NumBits = KnownVal.getBitWidth();
3509 if (MaxShift.uge(NumBits))
3510 return false;
3511
3512 if (!ShiftOp(KnownVal.One, MaxShift).isZero())
3513 return true;
3514
3515 // If all of the bits shifted out are known to be zero, and Val is known
3516 // non-zero then at least one non-zero bit must remain.
3517 if (InvShiftOp(KnownVal.Zero, NumBits - MaxShift)
3518 .eq(InvShiftOp(APInt::getAllOnes(NumBits), NumBits - MaxShift)) &&
3519 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth))
3520 return true;
3521
3522 return false;
3523}
3524
3526 const APInt &DemandedElts,
3527 const SimplifyQuery &Q, unsigned Depth) {
3528 unsigned BitWidth = getBitWidth(I->getType()->getScalarType(), Q.DL);
3529 switch (I->getOpcode()) {
3530 case Instruction::Alloca:
3531 // Alloca never returns null, malloc might.
3532 return I->getType()->getPointerAddressSpace() == 0;
3533 case Instruction::GetElementPtr:
3534 if (I->getType()->isPointerTy())
3536 break;
3537 case Instruction::BitCast: {
3538 // We need to be a bit careful here. We can only peek through the bitcast
3539 // if the scalar size of elements in the operand are smaller than and a
3540 // multiple of the size they are casting too. Take three cases:
3541 //
3542 // 1) Unsafe:
3543 // bitcast <2 x i16> %NonZero to <4 x i8>
3544 //
3545 // %NonZero can have 2 non-zero i16 elements, but isKnownNonZero on a
3546 // <4 x i8> requires that all 4 i8 elements be non-zero which isn't
3547 // guranteed (imagine just sign bit set in the 2 i16 elements).
3548 //
3549 // 2) Unsafe:
3550 // bitcast <4 x i3> %NonZero to <3 x i4>
3551 //
3552 // Even though the scalar size of the src (`i3`) is smaller than the
3553 // scalar size of the dst `i4`, because `i3` is not a multiple of `i4`
3554 // its possible for the `3 x i4` elements to be zero because there are
3555 // some elements in the destination that don't contain any full src
3556 // element.
3557 //
3558 // 3) Safe:
3559 // bitcast <4 x i8> %NonZero to <2 x i16>
3560 //
3561 // This is always safe as non-zero in the 4 i8 elements implies
3562 // non-zero in the combination of any two adjacent ones. Since i8 is a
3563 // multiple of i16, each i16 is guranteed to have 2 full i8 elements.
3564 // This all implies the 2 i16 elements are non-zero.
3565 Type *FromTy = I->getOperand(0)->getType();
3566 if ((FromTy->isIntOrIntVectorTy() || FromTy->isPtrOrPtrVectorTy()) &&
3567 (BitWidth % getBitWidth(FromTy->getScalarType(), Q.DL)) == 0)
3568 return isKnownNonZero(I->getOperand(0), Q, Depth);
3569 } break;
3570 case Instruction::IntToPtr:
3571 // Note that we have to take special care to avoid looking through
3572 // truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well
3573 // as casts that can alter the value, e.g., AddrSpaceCasts.
3574 if (!isa<ScalableVectorType>(I->getType()) &&
3575 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
3576 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
3577 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3578 break;
3579 case Instruction::PtrToAddr:
3580 // isKnownNonZero() for pointers refers to the address bits being non-zero,
3581 // so we can directly forward.
3582 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3583 case Instruction::PtrToInt:
3584 // For inttoptr, make sure the result size is >= the address size. If the
3585 // address is non-zero, any larger value is also non-zero.
3586 if (Q.DL.getAddressSizeInBits(I->getOperand(0)->getType()) <=
3587 I->getType()->getScalarSizeInBits())
3588 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3589 break;
3590 case Instruction::Trunc:
3591 // nuw/nsw trunc preserves zero/non-zero status of input.
3592 if (auto *TI = dyn_cast<TruncInst>(I))
3593 if (TI->hasNoSignedWrap() || TI->hasNoUnsignedWrap())
3594 return isKnownNonZero(TI->getOperand(0), DemandedElts, Q, Depth);
3595 break;
3596
3597 // Iff x - y != 0, then x ^ y != 0
3598 // Therefore we can do the same exact checks
3599 case Instruction::Xor:
3600 case Instruction::Sub:
3601 return isNonZeroSub(DemandedElts, Q, BitWidth, I->getOperand(0),
3602 I->getOperand(1), Depth);
3603 case Instruction::Or:
3604 // (X | (X != 0)) is non zero
3605 if (matchOpWithOpEqZero(I->getOperand(0), I->getOperand(1)))
3606 return true;
3607 // X | Y != 0 if X != Y.
3608 if (isKnownNonEqual(I->getOperand(0), I->getOperand(1), DemandedElts, Q,
3609 Depth))
3610 return true;
3611 // X | Y != 0 if X != 0 or Y != 0.
3612 return isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth) ||
3613 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3614 case Instruction::SExt:
3615 case Instruction::ZExt:
3616 // ext X != 0 if X != 0.
3617 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3618
3619 case Instruction::Shl: {
3620 // shl nsw/nuw can't remove any non-zero bits.
3622 if (Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO))
3623 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3624
3625 // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
3626 // if the lowest bit is shifted off the end.
3628 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth);
3629 if (Known.One[0])
3630 return true;
3631
3632 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3633 }
3634 case Instruction::LShr:
3635 case Instruction::AShr: {
3636 // shr exact can only shift out zero bits.
3638 if (BO->isExact())
3639 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3640
3641 // shr X, Y != 0 if X is negative. Note that the value of the shift is not
3642 // defined if the sign bit is shifted off the end.
3644 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3645 if (Known.isNegative())
3646 return true;
3647
3648 // shr (add nuw A, B), C is non-zero if A or B has a known-one bit at
3649 // position >= C, because the sum >= max(A, B).
3650 Value *A, *B;
3651 const APInt *C;
3652 if (Depth + 1 < MaxAnalysisRecursionDepth &&
3653 match(I->getOperand(0), m_NUWAdd(m_Value(A), m_Value(B))) &&
3654 match(I->getOperand(1), m_APInt(C)) && C->ult(BitWidth)) {
3655 KnownBits KnownA = computeKnownBits(A, DemandedElts, Q, Depth + 1);
3656 if (!KnownA.One.lshr(*C).isZero())
3657 return true;
3658 KnownBits KnownB = computeKnownBits(B, DemandedElts, Q, Depth + 1);
3659 if (!KnownB.One.lshr(*C).isZero())
3660 return true;
3661 }
3662
3663 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3664 }
3665 case Instruction::UDiv:
3666 case Instruction::SDiv: {
3667 // X / Y
3668 // div exact can only produce a zero if the dividend is zero.
3669 if (cast<PossiblyExactOperator>(I)->isExact())
3670 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3671
3672 KnownBits XKnown =
3673 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3674 // If X is fully unknown we won't be able to figure anything out so don't
3675 // both computing knownbits for Y.
3676 if (XKnown.isUnknown())
3677 return false;
3678
3679 KnownBits YKnown =
3680 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
3681 if (I->getOpcode() == Instruction::SDiv) {
3682 // For signed division need to compare abs value of the operands.
3683 XKnown = XKnown.abs(/*IntMinIsPoison*/ false);
3684 YKnown = YKnown.abs(/*IntMinIsPoison*/ false);
3685 }
3686 // If X u>= Y then div is non zero (0/0 is UB).
3687 std::optional<bool> XUgeY = KnownBits::uge(XKnown, YKnown);
3688 // If X is total unknown or X u< Y we won't be able to prove non-zero
3689 // with compute known bits so just return early.
3690 return XUgeY && *XUgeY;
3691 }
3692 case Instruction::Add: {
3693 // X + Y.
3694
3695 // If Add has nuw wrap flag, then if either X or Y is non-zero the result is
3696 // non-zero.
3698 return isNonZeroAdd(DemandedElts, Q, BitWidth, I->getOperand(0),
3699 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3700 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3701 }
3702 case Instruction::Mul: {
3704 return isNonZeroMul(DemandedElts, Q, BitWidth, I->getOperand(0),
3705 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3706 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3707 }
3708 case Instruction::Select: {
3709 // (C ? X : Y) != 0 if X != 0 and Y != 0.
3710
3711 // First check if the arm is non-zero using `isKnownNonZero`. If that fails,
3712 // then see if the select condition implies the arm is non-zero. For example
3713 // (X != 0 ? X : Y), we know the true arm is non-zero as the `X` "return" is
3714 // dominated by `X != 0`.
3715 auto SelectArmIsNonZero = [&](bool IsTrueArm) {
3716 Value *Op;
3717 Op = IsTrueArm ? I->getOperand(1) : I->getOperand(2);
3718 // Op is trivially non-zero.
3719 if (isKnownNonZero(Op, DemandedElts, Q, Depth))
3720 return true;
3721
3722 // The condition of the select dominates the true/false arm. Check if the
3723 // condition implies that a given arm is non-zero.
3724 Value *X;
3726 if (!match(I->getOperand(0), m_c_ICmp(Pred, m_Specific(Op), m_Value(X))))
3727 return false;
3728
3729 if (!IsTrueArm)
3731
3732 return cmpExcludesZero(Pred, X);
3733 };
3734
3735 if (SelectArmIsNonZero(/* IsTrueArm */ true) &&
3736 SelectArmIsNonZero(/* IsTrueArm */ false))
3737 return true;
3738 break;
3739 }
3740 case Instruction::PHI: {
3741 auto *PN = cast<PHINode>(I);
3743 return true;
3744
3745 // Check if all incoming values are non-zero using recursion.
3747 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
3748 return llvm::all_of(PN->operands(), [&](const Use &U) {
3749 if (U.get() == PN)
3750 return true;
3751 RecQ.CtxI = PN->getIncomingBlock(U)->getTerminator();
3752 // Check if the branch on the phi excludes zero.
3753 CmpPredicate Pred;
3754 Value *X;
3755 BasicBlock *TrueSucc, *FalseSucc;
3756 if (match(RecQ.CtxI,
3757 m_Br(m_c_ICmp(Pred, m_Specific(U.get()), m_Value(X)),
3758 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
3759 // Check for cases of duplicate successors.
3760 if ((TrueSucc == PN->getParent()) != (FalseSucc == PN->getParent())) {
3761 // If we're using the false successor, invert the predicate.
3762 if (FalseSucc == PN->getParent())
3763 Pred = CmpInst::getInversePredicate(Pred);
3764 if (cmpExcludesZero(Pred, X))
3765 return true;
3766 }
3767 }
3768 // Finally recurse on the edge and check it directly.
3769 return isKnownNonZero(U.get(), DemandedElts, RecQ, NewDepth);
3770 });
3771 }
3772 case Instruction::InsertElement: {
3773 if (isa<ScalableVectorType>(I->getType()))
3774 break;
3775
3776 const Value *Vec = I->getOperand(0);
3777 const Value *Elt = I->getOperand(1);
3778 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
3779
3780 unsigned NumElts = DemandedElts.getBitWidth();
3781 APInt DemandedVecElts = DemandedElts;
3782 bool SkipElt = false;
3783 // If we know the index we are inserting too, clear it from Vec check.
3784 if (CIdx && CIdx->getValue().ult(NumElts)) {
3785 DemandedVecElts.clearBit(CIdx->getZExtValue());
3786 SkipElt = !DemandedElts[CIdx->getZExtValue()];
3787 }
3788
3789 // Result is zero if Elt is non-zero and rest of the demanded elts in Vec
3790 // are non-zero.
3791 return (SkipElt || isKnownNonZero(Elt, Q, Depth)) &&
3792 (DemandedVecElts.isZero() ||
3793 isKnownNonZero(Vec, DemandedVecElts, Q, Depth));
3794 }
3795 case Instruction::ExtractElement:
3796 if (const auto *EEI = dyn_cast<ExtractElementInst>(I)) {
3797 const Value *Vec = EEI->getVectorOperand();
3798 const Value *Idx = EEI->getIndexOperand();
3799 auto *CIdx = dyn_cast<ConstantInt>(Idx);
3800 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
3801 unsigned NumElts = VecTy->getNumElements();
3802 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
3803 if (CIdx && CIdx->getValue().ult(NumElts))
3804 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
3805 return isKnownNonZero(Vec, DemandedVecElts, Q, Depth);
3806 }
3807 }
3808 break;
3809 case Instruction::ShuffleVector: {
3810 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
3811 if (!Shuf)
3812 break;
3813 APInt DemandedLHS, DemandedRHS;
3814 // For undef elements, we don't know anything about the common state of
3815 // the shuffle result.
3816 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
3817 break;
3818 // If demanded elements for both vecs are non-zero, the shuffle is non-zero.
3819 return (DemandedRHS.isZero() ||
3820 isKnownNonZero(Shuf->getOperand(1), DemandedRHS, Q, Depth)) &&
3821 (DemandedLHS.isZero() ||
3822 isKnownNonZero(Shuf->getOperand(0), DemandedLHS, Q, Depth));
3823 }
3824 case Instruction::Freeze:
3825 return isKnownNonZero(I->getOperand(0), Q, Depth) &&
3826 isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CtxI, Q.DT,
3827 Depth);
3828 case Instruction::Load: {
3829 auto *LI = cast<LoadInst>(I);
3830 // A Load tagged with nonnull or dereferenceable with null pointer undefined
3831 // is never null.
3832 if (auto *PtrT = dyn_cast<PointerType>(I->getType())) {
3833 if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull) ||
3834 (Q.IIQ.getMetadata(LI, LLVMContext::MD_dereferenceable) &&
3835 !NullPointerIsDefined(LI->getFunction(), PtrT->getAddressSpace())))
3836 return true;
3837 } else if (MDNode *Ranges = Q.IIQ.getMetadata(LI, LLVMContext::MD_range)) {
3839 }
3840
3841 // No need to fall through to computeKnownBits as range metadata is already
3842 // handled in isKnownNonZero.
3843 return false;
3844 }
3845 case Instruction::ExtractValue: {
3846 const WithOverflowInst *WO;
3848 switch (WO->getBinaryOp()) {
3849 default:
3850 break;
3851 case Instruction::Add:
3852 return isNonZeroAdd(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3853 WO->getArgOperand(1),
3854 /*NSW=*/false,
3855 /*NUW=*/false, Depth);
3856 case Instruction::Sub:
3857 return isNonZeroSub(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3858 WO->getArgOperand(1), Depth);
3859 case Instruction::Mul:
3860 return isNonZeroMul(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3861 WO->getArgOperand(1),
3862 /*NSW=*/false, /*NUW=*/false, Depth);
3863 break;
3864 }
3865 }
3866 break;
3867 }
3868 case Instruction::Call:
3869 case Instruction::Invoke: {
3870 const auto *Call = cast<CallBase>(I);
3871 if (I->getType()->isPointerTy()) {
3872 if (Call->isReturnNonNull())
3873 return true;
3874 if (const auto *RP = getArgumentAliasingToReturnedPointer(
3875 Call, /*MustPreserveOffset=*/true))
3876 return isKnownNonZero(RP, Q, Depth);
3877 } else {
3878 if (MDNode *Ranges = Q.IIQ.getMetadata(Call, LLVMContext::MD_range))
3880 if (std::optional<ConstantRange> Range = Call->getRange()) {
3881 const APInt ZeroValue(Range->getBitWidth(), 0);
3882 if (!Range->contains(ZeroValue))
3883 return true;
3884 }
3885 if (const Value *RV = Call->getReturnedArgOperand())
3886 if (RV->getType() == I->getType() && isKnownNonZero(RV, Q, Depth))
3887 return true;
3888 }
3889
3890 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
3891 switch (II->getIntrinsicID()) {
3892 case Intrinsic::sshl_sat:
3893 case Intrinsic::ushl_sat:
3894 case Intrinsic::abs:
3895 case Intrinsic::bitreverse:
3896 case Intrinsic::bswap:
3897 case Intrinsic::ctpop:
3898 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3899 // NB: We don't do usub_sat here as in any case we can prove its
3900 // non-zero, we will fold it to `sub nuw` in InstCombine.
3901 case Intrinsic::ssub_sat:
3902 // For most types, if x != y then ssub.sat x, y != 0. But
3903 // ssub.sat.i1 0, -1 = 0, because 1 saturates to 0. This means
3904 // isNonZeroSub will do the wrong thing for ssub.sat.i1.
3905 if (BitWidth == 1)
3906 return false;
3907 return isNonZeroSub(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3908 II->getArgOperand(1), Depth);
3909 case Intrinsic::sadd_sat:
3910 return isNonZeroAdd(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3911 II->getArgOperand(1),
3912 /*NSW=*/true, /* NUW=*/false, Depth);
3913 // Vec reverse preserves zero/non-zero status from input vec.
3914 case Intrinsic::vector_reverse:
3915 return isKnownNonZero(II->getArgOperand(0), DemandedElts.reverseBits(),
3916 Q, Depth);
3917 // umin/smin/smax/smin/or of all non-zero elements is always non-zero.
3918 case Intrinsic::vector_reduce_or:
3919 case Intrinsic::vector_reduce_umax:
3920 case Intrinsic::vector_reduce_umin:
3921 case Intrinsic::vector_reduce_smax:
3922 case Intrinsic::vector_reduce_smin:
3923 return isKnownNonZero(II->getArgOperand(0), Q, Depth);
3924 case Intrinsic::umax:
3925 case Intrinsic::uadd_sat:
3926 // umax(X, (X != 0)) is non zero
3927 // X +usat (X != 0) is non zero
3928 if (matchOpWithOpEqZero(II->getArgOperand(0), II->getArgOperand(1)))
3929 return true;
3930
3931 return isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth) ||
3932 isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3933 case Intrinsic::smax: {
3934 // If either arg is strictly positive the result is non-zero. Otherwise
3935 // the result is non-zero if both ops are non-zero.
3936 auto IsNonZero = [&](Value *Op, std::optional<bool> &OpNonZero,
3937 const KnownBits &OpKnown) {
3938 if (!OpNonZero.has_value())
3939 OpNonZero = OpKnown.isNonZero() ||
3940 isKnownNonZero(Op, DemandedElts, Q, Depth);
3941 return *OpNonZero;
3942 };
3943 // Avoid re-computing isKnownNonZero.
3944 std::optional<bool> Op0NonZero, Op1NonZero;
3945 KnownBits Op1Known =
3946 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3947 if (Op1Known.isNonNegative() &&
3948 IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known))
3949 return true;
3950 KnownBits Op0Known =
3951 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3952 if (Op0Known.isNonNegative() &&
3953 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known))
3954 return true;
3955 return IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known) &&
3956 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known);
3957 }
3958 case Intrinsic::smin: {
3959 // If either arg is negative the result is non-zero. Otherwise
3960 // the result is non-zero if both ops are non-zero.
3961 KnownBits Op1Known =
3962 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3963 if (Op1Known.isNegative())
3964 return true;
3965 KnownBits Op0Known =
3966 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3967 if (Op0Known.isNegative())
3968 return true;
3969
3970 if (Op1Known.isNonZero() && Op0Known.isNonZero())
3971 return true;
3972 }
3973 [[fallthrough]];
3974 case Intrinsic::umin:
3975 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth) &&
3976 isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth);
3977 case Intrinsic::cttz:
3978 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3979 .Zero[0];
3980 case Intrinsic::ctlz:
3981 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3982 .isNonNegative();
3983 case Intrinsic::fshr:
3984 case Intrinsic::fshl:
3985 // If Op0 == Op1, this is a rotate. rotate(x, y) != 0 iff x != 0.
3986 if (II->getArgOperand(0) == II->getArgOperand(1))
3987 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3988 break;
3989 case Intrinsic::vscale:
3990 return true;
3991 case Intrinsic::experimental_get_vector_length:
3992 return isKnownNonZero(I->getOperand(0), Q, Depth);
3993 default:
3994 break;
3995 }
3996 break;
3997 }
3998
3999 return false;
4000 }
4001 }
4002
4004 computeKnownBits(I, DemandedElts, Known, Q, Depth);
4005 return Known.One != 0;
4006}
4007
4008/// Return true if the given value is known to be non-zero when defined. For
4009/// vectors, return true if every demanded element is known to be non-zero when
4010/// defined. For pointers, if the context instruction and dominator tree are
4011/// specified, perform context-sensitive analysis and return true if the
4012/// pointer couldn't possibly be null at the specified instruction.
4013/// Supports values with integer or pointer type and vectors of integers.
4014bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
4015 const SimplifyQuery &Q, unsigned Depth) {
4016 Type *Ty = V->getType();
4017
4018#ifndef NDEBUG
4019 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4020
4021 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
4022 assert(
4023 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
4024 "DemandedElt width should equal the fixed vector number of elements");
4025 } else {
4026 assert(DemandedElts == APInt(1, 1) &&
4027 "DemandedElt width should be 1 for scalars");
4028 }
4029#endif
4030
4031 if (auto *C = dyn_cast<Constant>(V)) {
4032 if (C->isNullValue())
4033 return false;
4034 if (isa<ConstantInt>(C))
4035 // Must be non-zero due to null test above.
4036 return true;
4037
4038 // For constant vectors, check that all elements are poison or known
4039 // non-zero to determine that the whole vector is known non-zero.
4040 if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
4041 for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
4042 if (!DemandedElts[i])
4043 continue;
4044 Constant *Elt = C->getAggregateElement(i);
4045 if (!Elt || Elt->isNullValue())
4046 return false;
4047 if (!isa<PoisonValue>(Elt) && !isa<ConstantInt>(Elt))
4048 return false;
4049 }
4050 return true;
4051 }
4052
4053 // Constant ptrauth can be null, iff the base pointer can be.
4054 if (auto *CPA = dyn_cast<ConstantPtrAuth>(V))
4055 return isKnownNonZero(CPA->getPointer(), DemandedElts, Q, Depth);
4056
4057 // A global variable is non null unless extern weak or an absolute symbol
4058 // reference. An alias is also excluded, as it may resolve to a weak
4059 // undefined symbol and thus to the null address. Where null is a valid
4060 // address an allocation may start there, so nothing can be assumed; that
4061 // covers both null_pointer_is_valid and address spaces other than zero.
4062 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
4064 GV->getType()->getAddressSpace()) &&
4065 !GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&
4066 !isa<GlobalAlias>(GV))
4067 return true;
4068 }
4069
4070 // For constant expressions, fall through to the Operator code below.
4071 if (!isa<ConstantExpr>(V))
4072 return false;
4073 }
4074
4075 if (const auto *A = dyn_cast<Argument>(V))
4076 if (std::optional<ConstantRange> Range = A->getRange()) {
4077 const APInt ZeroValue(Range->getBitWidth(), 0);
4078 if (!Range->contains(ZeroValue))
4079 return true;
4080 }
4081
4083 return true;
4084
4085 // Some of the tests below are recursive, so bail out if we hit the limit.
4087 return false;
4088
4089 // Check for pointer simplifications.
4090
4091 if (PointerType *PtrTy = dyn_cast<PointerType>(Ty)) {
4092 // A byval, inalloca may not be null in a non-default addres space. A
4093 // nonnull argument is assumed never 0.
4094 if (const Argument *A = dyn_cast<Argument>(V)) {
4095 if (((A->hasPassPointeeByValueCopyAttr() &&
4096 !NullPointerIsDefined(A->getParent(), PtrTy->getAddressSpace())) ||
4097 A->hasNonNullAttr()))
4098 return true;
4099 }
4100 }
4101
4102 if (const auto *I = dyn_cast<Operator>(V))
4103 if (isKnownNonZeroFromOperator(I, DemandedElts, Q, Depth))
4104 return true;
4105
4106 if (!isa<Constant>(V) &&
4108 return true;
4109
4110 if (const Value *Stripped = stripNullTest(V))
4111 return isKnownNonZero(Stripped, DemandedElts, Q, Depth);
4112
4113 return false;
4114}
4115
4117 unsigned Depth) {
4118 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
4119 APInt DemandedElts =
4120 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
4121 return ::isKnownNonZero(V, DemandedElts, Q, Depth);
4122}
4123
4124/// If the pair of operators are the same invertible function, return the
4125/// the operands of the function corresponding to each input. Otherwise,
4126/// return std::nullopt. An invertible function is one that is 1-to-1 and maps
4127/// every input value to exactly one output value. This is equivalent to
4128/// saying that Op1 and Op2 are equal exactly when the specified pair of
4129/// operands are equal, (except that Op1 and Op2 may be poison more often.)
4130static std::optional<std::pair<Value*, Value*>>
4132 const Operator *Op2) {
4133 if (Op1->getOpcode() != Op2->getOpcode())
4134 return std::nullopt;
4135
4136 auto getOperands = [&](unsigned OpNum) -> auto {
4137 return std::make_pair(Op1->getOperand(OpNum), Op2->getOperand(OpNum));
4138 };
4139
4140 switch (Op1->getOpcode()) {
4141 default:
4142 break;
4143 case Instruction::Or:
4144 if (!cast<PossiblyDisjointInst>(Op1)->isDisjoint() ||
4145 !cast<PossiblyDisjointInst>(Op2)->isDisjoint())
4146 break;
4147 [[fallthrough]];
4148 case Instruction::Xor:
4149 case Instruction::Add: {
4150 Value *Other;
4151 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(0)), m_Value(Other))))
4152 return std::make_pair(Op1->getOperand(1), Other);
4153 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(1)), m_Value(Other))))
4154 return std::make_pair(Op1->getOperand(0), Other);
4155 break;
4156 }
4157 case Instruction::Sub:
4158 if (Op1->getOperand(0) == Op2->getOperand(0))
4159 return getOperands(1);
4160 if (Op1->getOperand(1) == Op2->getOperand(1))
4161 return getOperands(0);
4162 break;
4163 case Instruction::Mul: {
4164 // invertible if A * B == (A * B) mod 2^N where A, and B are integers
4165 // and N is the bitwdith. The nsw case is non-obvious, but proven by
4166 // alive2: https://alive2.llvm.org/ce/z/Z6D5qK
4167 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
4168 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
4169 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
4170 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
4171 break;
4172
4173 // Assume operand order has been canonicalized
4174 if (Op1->getOperand(1) == Op2->getOperand(1) &&
4175 isa<ConstantInt>(Op1->getOperand(1)) &&
4176 !cast<ConstantInt>(Op1->getOperand(1))->isZero())
4177 return getOperands(0);
4178 break;
4179 }
4180 case Instruction::Shl: {
4181 // Same as multiplies, with the difference that we don't need to check
4182 // for a non-zero multiply. Shifts always multiply by non-zero.
4183 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
4184 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
4185 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
4186 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
4187 break;
4188
4189 if (Op1->getOperand(1) == Op2->getOperand(1))
4190 return getOperands(0);
4191 break;
4192 }
4193 case Instruction::AShr:
4194 case Instruction::LShr: {
4195 auto *PEO1 = cast<PossiblyExactOperator>(Op1);
4196 auto *PEO2 = cast<PossiblyExactOperator>(Op2);
4197 if (!PEO1->isExact() || !PEO2->isExact())
4198 break;
4199
4200 if (Op1->getOperand(1) == Op2->getOperand(1))
4201 return getOperands(0);
4202 break;
4203 }
4204 case Instruction::SExt:
4205 case Instruction::ZExt:
4206 if (Op1->getOperand(0)->getType() == Op2->getOperand(0)->getType())
4207 return getOperands(0);
4208 break;
4209 case Instruction::PHI: {
4210 const PHINode *PN1 = cast<PHINode>(Op1);
4211 const PHINode *PN2 = cast<PHINode>(Op2);
4212
4213 // If PN1 and PN2 are both recurrences, can we prove the entire recurrences
4214 // are a single invertible function of the start values? Note that repeated
4215 // application of an invertible function is also invertible
4216 BinaryOperator *BO1 = nullptr;
4217 Value *Start1 = nullptr, *Step1 = nullptr;
4218 BinaryOperator *BO2 = nullptr;
4219 Value *Start2 = nullptr, *Step2 = nullptr;
4220 if (PN1->getParent() != PN2->getParent() ||
4221 !matchSimpleRecurrence(PN1, BO1, Start1, Step1) ||
4222 !matchSimpleRecurrence(PN2, BO2, Start2, Step2))
4223 break;
4224
4226 cast<Operator>(BO2));
4227 if (!Values)
4228 break;
4229
4230 // We have to be careful of mutually defined recurrences here. Ex:
4231 // * X_i = X_(i-1) OP Y_(i-1), and Y_i = X_(i-1) OP V
4232 // * X_i = Y_i = X_(i-1) OP Y_(i-1)
4233 // The invertibility of these is complicated, and not worth reasoning
4234 // about (yet?).
4235 if (Values->first != PN1 || Values->second != PN2)
4236 break;
4237
4238 return std::make_pair(Start1, Start2);
4239 }
4240 }
4241 return std::nullopt;
4242}
4243
4244/// Return true if V1 == (binop V2, X), where X is known non-zero.
4245/// Only handle a small subset of binops where (binop V2, X) with non-zero X
4246/// implies V2 != V1.
4247static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2,
4248 const APInt &DemandedElts,
4249 const SimplifyQuery &Q, unsigned Depth) {
4251 if (!BO)
4252 return false;
4253 switch (BO->getOpcode()) {
4254 default:
4255 break;
4256 case Instruction::Or:
4257 if (!cast<PossiblyDisjointInst>(V1)->isDisjoint())
4258 break;
4259 [[fallthrough]];
4260 case Instruction::Xor:
4261 case Instruction::Add:
4262 Value *Op = nullptr;
4263 if (V2 == BO->getOperand(0))
4264 Op = BO->getOperand(1);
4265 else if (V2 == BO->getOperand(1))
4266 Op = BO->getOperand(0);
4267 else
4268 return false;
4269 return isKnownNonZero(Op, DemandedElts, Q, Depth + 1);
4270 }
4271 return false;
4272}
4273
4274/// Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and
4275/// the multiplication is nuw or nsw.
4276static bool isNonEqualMul(const Value *V1, const Value *V2,
4277 const APInt &DemandedElts, const SimplifyQuery &Q,
4278 unsigned Depth) {
4279 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
4280 const APInt *C;
4281 return match(OBO, m_Mul(m_Specific(V1), m_APInt(C))) &&
4282 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
4283 !C->isZero() && !C->isOne() &&
4284 isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
4285 }
4286 return false;
4287}
4288
4289/// Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and
4290/// the shift is nuw or nsw.
4291static bool isNonEqualShl(const Value *V1, const Value *V2,
4292 const APInt &DemandedElts, const SimplifyQuery &Q,
4293 unsigned Depth) {
4294 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
4295 const APInt *C;
4296 return match(OBO, m_Shl(m_Specific(V1), m_APInt(C))) &&
4297 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
4298 !C->isZero() && isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
4299 }
4300 return false;
4301}
4302
4303static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2,
4304 const APInt &DemandedElts, const SimplifyQuery &Q,
4305 unsigned Depth) {
4306 // Check two PHIs are in same block.
4307 if (PN1->getParent() != PN2->getParent())
4308 return false;
4309
4311 bool UsedFullRecursion = false;
4312 for (const BasicBlock *IncomBB : PN1->blocks()) {
4313 if (!VisitedBBs.insert(IncomBB).second)
4314 continue; // Don't reprocess blocks that we have dealt with already.
4315 const Value *IV1 = PN1->getIncomingValueForBlock(IncomBB);
4316 const Value *IV2 = PN2->getIncomingValueForBlock(IncomBB);
4317 const APInt *C1, *C2;
4318 if (match(IV1, m_APInt(C1)) && match(IV2, m_APInt(C2)) && *C1 != *C2)
4319 continue;
4320
4321 // Only one pair of phi operands is allowed for full recursion.
4322 if (UsedFullRecursion)
4323 return false;
4324
4326 RecQ.CtxI = IncomBB->getTerminator();
4327 if (!isKnownNonEqual(IV1, IV2, DemandedElts, RecQ, Depth + 1))
4328 return false;
4329 UsedFullRecursion = true;
4330 }
4331 return true;
4332}
4333
4334static bool isNonEqualSelect(const Value *V1, const Value *V2,
4335 const APInt &DemandedElts, const SimplifyQuery &Q,
4336 unsigned Depth) {
4337 const SelectInst *SI1 = dyn_cast<SelectInst>(V1);
4338 if (!SI1)
4339 return false;
4340
4341 if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2)) {
4342 const Value *Cond1 = SI1->getCondition();
4343 const Value *Cond2 = SI2->getCondition();
4344 if (Cond1 == Cond2)
4345 return isKnownNonEqual(SI1->getTrueValue(), SI2->getTrueValue(),
4346 DemandedElts, Q, Depth + 1) &&
4347 isKnownNonEqual(SI1->getFalseValue(), SI2->getFalseValue(),
4348 DemandedElts, Q, Depth + 1);
4349 }
4350 return isKnownNonEqual(SI1->getTrueValue(), V2, DemandedElts, Q, Depth + 1) &&
4351 isKnownNonEqual(SI1->getFalseValue(), V2, DemandedElts, Q, Depth + 1);
4352}
4353
4354// Check to see if A is both a GEP and is the incoming value for a PHI in the
4355// loop, and B is either a ptr or another GEP. If the PHI has 2 incoming values,
4356// one of them being the recursive GEP A and the other a ptr at same base and at
4357// the same/higher offset than B we are only incrementing the pointer further in
4358// loop if offset of recursive GEP is greater than 0.
4360 const SimplifyQuery &Q) {
4361 if (!A->getType()->isPointerTy() || !B->getType()->isPointerTy())
4362 return false;
4363
4364 auto *GEPA = dyn_cast<GEPOperator>(A);
4365 if (!GEPA || GEPA->getNumIndices() != 1 || !isa<Constant>(GEPA->idx_begin()))
4366 return false;
4367
4368 // Handle 2 incoming PHI values with one being a recursive GEP.
4369 auto *PN = dyn_cast<PHINode>(GEPA->getPointerOperand());
4370 if (!PN || PN->getNumIncomingValues() != 2)
4371 return false;
4372
4373 // Search for the recursive GEP as an incoming operand, and record that as
4374 // Step.
4375 Value *Start = nullptr;
4376 Value *Step = const_cast<Value *>(A);
4377 if (PN->getIncomingValue(0) == Step)
4378 Start = PN->getIncomingValue(1);
4379 else if (PN->getIncomingValue(1) == Step)
4380 Start = PN->getIncomingValue(0);
4381 else
4382 return false;
4383
4384 // Other incoming node base should match the B base.
4385 // StartOffset >= OffsetB && StepOffset > 0?
4386 // StartOffset <= OffsetB && StepOffset < 0?
4387 // Is non-equal if above are true.
4388 // We use stripAndAccumulateInBoundsConstantOffsets to restrict the
4389 // optimisation to inbounds GEPs only.
4390 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(Start->getType());
4391 APInt StartOffset(IndexWidth, 0);
4392 Start = Start->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StartOffset);
4393 APInt StepOffset(IndexWidth, 0);
4394 Step = Step->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StepOffset);
4395
4396 // Check if Base Pointer of Step matches the PHI.
4397 if (Step != PN)
4398 return false;
4399 APInt OffsetB(IndexWidth, 0);
4400 B = B->stripAndAccumulateInBoundsConstantOffsets(Q.DL, OffsetB);
4401 return Start == B &&
4402 ((StartOffset.sge(OffsetB) && StepOffset.isStrictlyPositive()) ||
4403 (StartOffset.sle(OffsetB) && StepOffset.isNegative()));
4404}
4405
4406static bool isKnownNonEqualFromContext(const Value *V1, const Value *V2,
4407 const SimplifyQuery &Q, unsigned Depth) {
4408 if (!Q.CtxI)
4409 return false;
4410
4411 // Try to infer NonEqual based on information from dominating conditions.
4412 if (Q.DC && Q.DT) {
4413 auto IsKnownNonEqualFromDominatingCondition = [&](const Value *V) {
4414 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
4415 Value *Cond = BI->getCondition();
4416 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4417 if (Q.DT->dominates(Edge0, Q.CtxI->getParent()) &&
4419 /*LHSIsTrue=*/true, Depth)
4420 .value_or(false))
4421 return true;
4422
4423 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4424 if (Q.DT->dominates(Edge1, Q.CtxI->getParent()) &&
4426 /*LHSIsTrue=*/false, Depth)
4427 .value_or(false))
4428 return true;
4429 }
4430
4431 return false;
4432 };
4433
4434 if (IsKnownNonEqualFromDominatingCondition(V1) ||
4435 IsKnownNonEqualFromDominatingCondition(V2))
4436 return true;
4437 }
4438
4439 if (!Q.AC)
4440 return false;
4441
4442 // Try to infer NonEqual based on information from assumptions.
4443 for (auto &AssumeVH : Q.AC->assumptionsFor(V1)) {
4444 if (!AssumeVH)
4445 continue;
4446 CallInst *I = cast<CallInst>(AssumeVH);
4447
4448 assert(I->getFunction() == Q.CtxI->getFunction() &&
4449 "Got assumption for the wrong function!");
4450 assert(I->getIntrinsicID() == Intrinsic::assume &&
4451 "must be an assume intrinsic");
4452
4453 if (isImpliedCondition(I->getArgOperand(0), ICmpInst::ICMP_NE, V1, V2, Q.DL,
4454 /*LHSIsTrue=*/true, Depth)
4455 .value_or(false) &&
4457 return true;
4458 }
4459
4460 return false;
4461}
4462
4463static bool isNonEqualURem(const Value *X, const Value *Rem,
4464 const SimplifyQuery &Q) {
4465 const Value *Y;
4466 if (!match(Rem, m_URem(m_Specific(X), m_Value(Y))))
4467 return false;
4468
4469 // For a defined urem, X != X urem Y exactly when X u>= Y.
4470 // isTruePredicate does not handle UGE, so use the equivalent Y u<= X.
4472 return true;
4473
4474 std::optional<bool> Implied =
4476 return Implied && *Implied;
4477}
4478
4479/// Return true if it is known that V1 != V2.
4480static bool isKnownNonEqual(const Value *V1, const Value *V2,
4481 const APInt &DemandedElts, const SimplifyQuery &Q,
4482 unsigned Depth) {
4483 if (V1 == V2)
4484 return false;
4485 if (V1->getType() != V2->getType())
4486 // We can't look through casts yet.
4487 return false;
4488
4490 return false;
4491
4492 // See if we can recurse through (exactly one of) our operands. This
4493 // requires our operation be 1-to-1 and map every input value to exactly
4494 // one output value. Such an operation is invertible.
4495 auto *O1 = dyn_cast<Operator>(V1);
4496 auto *O2 = dyn_cast<Operator>(V2);
4497 if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) {
4498 if (auto Values = getInvertibleOperands(O1, O2))
4499 return isKnownNonEqual(Values->first, Values->second, DemandedElts, Q,
4500 Depth + 1);
4501
4502 if (const PHINode *PN1 = dyn_cast<PHINode>(V1)) {
4503 const PHINode *PN2 = cast<PHINode>(V2);
4504 // FIXME: This is missing a generalization to handle the case where one is
4505 // a PHI and another one isn't.
4506 if (isNonEqualPHIs(PN1, PN2, DemandedElts, Q, Depth))
4507 return true;
4508 };
4509 }
4510
4511 if (isModifyingBinopOfNonZero(V1, V2, DemandedElts, Q, Depth) ||
4512 isModifyingBinopOfNonZero(V2, V1, DemandedElts, Q, Depth))
4513 return true;
4514
4515 if (isNonEqualMul(V1, V2, DemandedElts, Q, Depth) ||
4516 isNonEqualMul(V2, V1, DemandedElts, Q, Depth))
4517 return true;
4518
4519 if (isNonEqualShl(V1, V2, DemandedElts, Q, Depth) ||
4520 isNonEqualShl(V2, V1, DemandedElts, Q, Depth))
4521 return true;
4522
4523 if (V1->getType()->isIntOrIntVectorTy()) {
4524 // Are any known bits in V1 contradictory to known bits in V2? If V1
4525 // has a known zero where V2 has a known one, they must not be equal.
4526 KnownBits Known1 = computeKnownBits(V1, DemandedElts, Q, Depth);
4527 if (!Known1.isUnknown()) {
4528 KnownBits Known2 = computeKnownBits(V2, DemandedElts, Q, Depth);
4529 if (Known1.Zero.intersects(Known2.One) ||
4530 Known2.Zero.intersects(Known1.One))
4531 return true;
4532 }
4533 }
4534
4535 if (isNonEqualSelect(V1, V2, DemandedElts, Q, Depth) ||
4536 isNonEqualSelect(V2, V1, DemandedElts, Q, Depth))
4537 return true;
4538
4541 return true;
4542
4543 Value *A, *B;
4544 // PtrToInts are NonEqual if their Ptrs are NonEqual.
4545 // Check PtrToInt type matches the pointer size.
4546 if (match(V1, m_PtrToIntSameSize(Q.DL, m_Value(A))) &&
4548 return isKnownNonEqual(A, B, DemandedElts, Q, Depth + 1);
4549
4550 if (isNonEqualURem(V1, V2, Q) || isNonEqualURem(V2, V1, Q))
4551 return true;
4552
4553 if (isKnownNonEqualFromContext(V1, V2, Q, Depth))
4554 return true;
4555
4556 return false;
4557}
4558
4559/// For vector constants, loop over the elements and find the constant with the
4560/// minimum number of sign bits. Return 0 if the value is not a vector constant
4561/// or if any element was not analyzed; otherwise, return the count for the
4562/// element with the minimum number of sign bits.
4564 const APInt &DemandedElts,
4565 unsigned TyBits) {
4566 const auto *CV = dyn_cast<Constant>(V);
4567 if (!CV || !isa<FixedVectorType>(CV->getType()))
4568 return 0;
4569
4570 unsigned MinSignBits = TyBits;
4571 unsigned NumElts = cast<FixedVectorType>(CV->getType())->getNumElements();
4572 for (unsigned i = 0; i != NumElts; ++i) {
4573 if (!DemandedElts[i])
4574 continue;
4575 // If we find a non-ConstantInt, bail out.
4576 auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));
4577 if (!Elt)
4578 return 0;
4579
4580 MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());
4581 }
4582
4583 return MinSignBits;
4584}
4585
4586static unsigned ComputeNumSignBitsImpl(const Value *V,
4587 const APInt &DemandedElts,
4588 const SimplifyQuery &Q, unsigned Depth);
4589
4590static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
4591 const SimplifyQuery &Q, unsigned Depth) {
4592 unsigned Result = ComputeNumSignBitsImpl(V, DemandedElts, Q, Depth);
4593 assert(Result > 0 && "At least one sign bit needs to be present!");
4594 return Result;
4595}
4596
4597/// Return the number of times the sign bit of the register is replicated into
4598/// the other bits. We know that at least 1 bit is always equal to the sign bit
4599/// (itself), but other cases can give us information. For example, immediately
4600/// after an "ashr X, 2", we know that the top 3 bits are all equal to each
4601/// other, so we return 3. For vectors, return the number of sign bits for the
4602/// vector element with the minimum number of known sign bits of the demanded
4603/// elements in the vector specified by DemandedElts.
4604static unsigned ComputeNumSignBitsImpl(const Value *V,
4605 const APInt &DemandedElts,
4606 const SimplifyQuery &Q, unsigned Depth) {
4607 Type *Ty = V->getType();
4608#ifndef NDEBUG
4609 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4610
4611 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
4612 assert(
4613 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
4614 "DemandedElt width should equal the fixed vector number of elements");
4615 } else {
4616 assert(DemandedElts == APInt(1, 1) &&
4617 "DemandedElt width should be 1 for scalars");
4618 }
4619#endif
4620
4621 // We return the minimum number of sign bits that are guaranteed to be present
4622 // in V, so for undef we have to conservatively return 1. We don't have the
4623 // same behavior for poison though -- that's a FIXME today.
4624
4625 Type *ScalarTy = Ty->getScalarType();
4626 unsigned TyBits = ScalarTy->isPointerTy() ?
4627 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
4628 Q.DL.getTypeSizeInBits(ScalarTy);
4629
4630 unsigned Tmp, Tmp2;
4631 unsigned FirstAnswer = 1;
4632
4633 // Note that ConstantInt is handled by the general computeKnownBits case
4634 // below.
4635
4637 return 1;
4638
4639 if (auto *U = dyn_cast<Operator>(V)) {
4640 switch (Operator::getOpcode(V)) {
4641 default: break;
4642 case Instruction::BitCast: {
4643 Value *Src = U->getOperand(0);
4644 Type *SrcTy = Src->getType();
4645
4646 // Skip if the source type is not an integer or integer vector type
4647 // This ensures we only process integer-like types
4648 if (!SrcTy->isIntOrIntVectorTy())
4649 break;
4650
4651 unsigned SrcBits = SrcTy->getScalarSizeInBits();
4652
4653 // Bitcast 'large element' scalar/vector to 'small element' vector.
4654 if ((SrcBits % TyBits) != 0)
4655 break;
4656
4657 // Only proceed if the destination type is a fixed-size vector
4658 if (isa<FixedVectorType>(Ty)) {
4659 // Fast case - sign splat can be simply split across the small elements.
4660 // This works for both vector and scalar sources
4661 unsigned Scale = SrcBits / TyBits;
4662 APInt SrcDemandedElts = APIntOps::ScaleBitMask(
4663 DemandedElts, DemandedElts.getBitWidth() / Scale);
4664 Tmp = ComputeNumSignBits(Src, SrcDemandedElts, Q, Depth + 1);
4665 if (Tmp == SrcBits)
4666 return TyBits;
4667 }
4668 break;
4669 }
4670 case Instruction::SExt:
4671 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
4672 return ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1) +
4673 Tmp;
4674
4675 case Instruction::SDiv: {
4676 const APInt *Denominator;
4677 // sdiv X, C -> adds log(C) sign bits.
4678 if (match(U->getOperand(1), m_APInt(Denominator))) {
4679
4680 // Ignore non-positive denominator.
4681 if (!Denominator->isStrictlyPositive())
4682 break;
4683
4684 // Calculate the incoming numerator bits.
4685 unsigned NumBits =
4686 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4687
4688 // Add floor(log(C)) bits to the numerator bits.
4689 return std::min(TyBits, NumBits + Denominator->logBase2());
4690 }
4691 break;
4692 }
4693
4694 case Instruction::SRem: {
4695 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4696
4697 const APInt *Denominator;
4698 // srem X, C -> we know that the result is within [-C+1,C) when C is a
4699 // positive constant. This let us put a lower bound on the number of sign
4700 // bits.
4701 if (match(U->getOperand(1), m_APInt(Denominator))) {
4702
4703 // Ignore non-positive denominator.
4704 if (Denominator->isStrictlyPositive()) {
4705 // Calculate the leading sign bit constraints by examining the
4706 // denominator. Given that the denominator is positive, there are two
4707 // cases:
4708 //
4709 // 1. The numerator is positive. The result range is [0,C) and
4710 // [0,C) u< (1 << ceilLogBase2(C)).
4711 //
4712 // 2. The numerator is negative. Then the result range is (-C,0] and
4713 // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
4714 //
4715 // Thus a lower bound on the number of sign bits is `TyBits -
4716 // ceilLogBase2(C)`.
4717
4718 unsigned ResBits = TyBits - Denominator->ceilLogBase2();
4719 Tmp = std::max(Tmp, ResBits);
4720 }
4721 }
4722 return Tmp;
4723 }
4724
4725 case Instruction::AShr: {
4726 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4727 // ashr X, C -> adds C sign bits. Vectors too.
4728 const APInt *ShAmt;
4729 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4730 if (ShAmt->uge(TyBits))
4731 break; // Bad shift.
4732 unsigned ShAmtLimited = ShAmt->getZExtValue();
4733 Tmp += ShAmtLimited;
4734 if (Tmp > TyBits) Tmp = TyBits;
4735 }
4736 return Tmp;
4737 }
4738 case Instruction::Shl: {
4739 const APInt *ShAmt;
4740 Value *X = nullptr;
4741 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4742 // shl destroys sign bits.
4743 if (ShAmt->uge(TyBits))
4744 break; // Bad shift.
4745 // We can look through a zext (more or less treating it as a sext) if
4746 // all extended bits are shifted out.
4747 if (match(U->getOperand(0), m_ZExt(m_Value(X))) &&
4748 ShAmt->uge(TyBits - X->getType()->getScalarSizeInBits())) {
4749 Tmp = ComputeNumSignBits(X, DemandedElts, Q, Depth + 1);
4750 Tmp += TyBits - X->getType()->getScalarSizeInBits();
4751 } else
4752 Tmp =
4753 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4754 if (ShAmt->uge(Tmp))
4755 break; // Shifted all sign bits out.
4756 Tmp2 = ShAmt->getZExtValue();
4757 return Tmp - Tmp2;
4758 }
4759 break;
4760 }
4761 case Instruction::And:
4762 case Instruction::Or:
4763 case Instruction::Xor: // NOT is handled here.
4764 // Logical binary ops preserve the number of sign bits at the worst.
4765 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4766 if (Tmp != 1) {
4767 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4768 FirstAnswer = std::min(Tmp, Tmp2);
4769 // We computed what we know about the sign bits as our first
4770 // answer. Now proceed to the generic code that uses
4771 // computeKnownBits, and pick whichever answer is better.
4772 }
4773 break;
4774
4775 case Instruction::Select: {
4776 // If we have a clamp pattern, we know that the number of sign bits will
4777 // be the minimum of the clamp min/max range.
4778 const Value *X;
4779 const APInt *CLow, *CHigh;
4780 if (isSignedMinMaxClamp(U, X, CLow, CHigh))
4781 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4782
4783 Tmp = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4784 if (Tmp == 1)
4785 break;
4786 Tmp2 = ComputeNumSignBits(U->getOperand(2), DemandedElts, Q, Depth + 1);
4787 return std::min(Tmp, Tmp2);
4788 }
4789
4790 case Instruction::Add:
4791 // Add can have at most one carry bit. Thus we know that the output
4792 // is, at worst, one more bit than the inputs.
4793 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4794 if (Tmp == 1) break;
4795
4796 // Special case decrementing a value (ADD X, -1):
4797 if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
4798 if (CRHS->isAllOnesValue()) {
4799 KnownBits Known(TyBits);
4800 computeKnownBits(U->getOperand(0), DemandedElts, Known, Q, Depth + 1);
4801
4802 // If the input is known to be 0 or 1, the output is 0/-1, which is
4803 // all sign bits set.
4804 if ((Known.Zero | 1).isAllOnes())
4805 return TyBits;
4806
4807 // If we are subtracting one from a positive number, there is no carry
4808 // out of the result.
4809 if (Known.isNonNegative())
4810 return Tmp;
4811 }
4812
4813 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4814 if (Tmp2 == 1)
4815 break;
4816 return std::min(Tmp, Tmp2) - 1;
4817
4818 case Instruction::Sub:
4819 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4820 if (Tmp2 == 1)
4821 break;
4822
4823 // Handle NEG.
4824 if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
4825 if (CLHS->isNullValue()) {
4826 KnownBits Known(TyBits);
4827 computeKnownBits(U->getOperand(1), DemandedElts, Known, Q, Depth + 1);
4828 // If the input is known to be 0 or 1, the output is 0/-1, which is
4829 // all sign bits set.
4830 if ((Known.Zero | 1).isAllOnes())
4831 return TyBits;
4832
4833 // If the input is known to be positive (the sign bit is known clear),
4834 // the output of the NEG has the same number of sign bits as the
4835 // input.
4836 if (Known.isNonNegative())
4837 return Tmp2;
4838
4839 // Otherwise, we treat this like a SUB.
4840 }
4841
4842 // Sub can have at most one carry bit. Thus we know that the output
4843 // is, at worst, one more bit than the inputs.
4844 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4845 if (Tmp == 1)
4846 break;
4847 return std::min(Tmp, Tmp2) - 1;
4848
4849 case Instruction::Mul: {
4850 // The output of the Mul can be at most twice the valid bits in the
4851 // inputs.
4852 unsigned SignBitsOp0 =
4853 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4854 if (SignBitsOp0 == 1)
4855 break;
4856 unsigned SignBitsOp1 =
4857 ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4858 if (SignBitsOp1 == 1)
4859 break;
4860 unsigned OutValidBits =
4861 (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);
4862 return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
4863 }
4864
4865 case Instruction::PHI: {
4866 const PHINode *PN = cast<PHINode>(U);
4867 unsigned NumIncomingValues = PN->getNumIncomingValues();
4868 // Don't analyze large in-degree PHIs.
4869 if (NumIncomingValues > 4) break;
4870 // Unreachable blocks may have zero-operand PHI nodes.
4871 if (NumIncomingValues == 0) break;
4872
4873 // Take the minimum of all incoming values. This can't infinitely loop
4874 // because of our depth threshold.
4876 Tmp = TyBits;
4877 for (unsigned i = 0, e = NumIncomingValues; i != e; ++i) {
4878 if (Tmp == 1) return Tmp;
4879 RecQ.CtxI = PN->getIncomingBlock(i)->getTerminator();
4880 Tmp = std::min(Tmp, ComputeNumSignBits(PN->getIncomingValue(i),
4881 DemandedElts, RecQ, Depth + 1));
4882 }
4883 return Tmp;
4884 }
4885
4886 case Instruction::Trunc: {
4887 // If the input contained enough sign bits that some remain after the
4888 // truncation, then we can make use of that. Otherwise we don't know
4889 // anything.
4890 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4891 unsigned OperandTyBits = U->getOperand(0)->getType()->getScalarSizeInBits();
4892 if (Tmp > (OperandTyBits - TyBits))
4893 return Tmp - (OperandTyBits - TyBits);
4894
4895 return 1;
4896 }
4897
4898 case Instruction::ExtractElement: {
4899 const Value *Vec = U->getOperand(0);
4900 auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType());
4901 if (!VecTy)
4902 return ComputeNumSignBits(Vec, Q, Depth + 1);
4903
4904 unsigned NumElts = VecTy->getNumElements();
4905 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
4906 if (auto *CIdx = dyn_cast<ConstantInt>(U->getOperand(1)))
4907 if (CIdx->getValue().ult(NumElts))
4908 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
4909 return ComputeNumSignBits(Vec, DemandedVecElts, Q, Depth + 1);
4910 }
4911
4912 case Instruction::ShuffleVector: {
4913 // Collect the minimum number of sign bits that are shared by every vector
4914 // element referenced by the shuffle.
4915 auto *Shuf = dyn_cast<ShuffleVectorInst>(U);
4916 if (!Shuf) {
4917 // FIXME: Add support for shufflevector constant expressions.
4918 return 1;
4919 }
4920 APInt DemandedLHS, DemandedRHS;
4921 // For undef elements, we don't know anything about the common state of
4922 // the shuffle result.
4923 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
4924 return 1;
4925 Tmp = std::numeric_limits<unsigned>::max();
4926 if (!!DemandedLHS) {
4927 const Value *LHS = Shuf->getOperand(0);
4928 Tmp = ComputeNumSignBits(LHS, DemandedLHS, Q, Depth + 1);
4929 }
4930 // If we don't know anything, early out and try computeKnownBits
4931 // fall-back.
4932 if (Tmp == 1)
4933 break;
4934 if (!!DemandedRHS) {
4935 const Value *RHS = Shuf->getOperand(1);
4936 Tmp2 = ComputeNumSignBits(RHS, DemandedRHS, Q, Depth + 1);
4937 Tmp = std::min(Tmp, Tmp2);
4938 }
4939 // If we don't know anything, early out and try computeKnownBits
4940 // fall-back.
4941 if (Tmp == 1)
4942 break;
4943 assert(Tmp <= TyBits && "Failed to determine minimum sign bits");
4944 return Tmp;
4945 }
4946 case Instruction::Call: {
4947 if (const auto *II = dyn_cast<IntrinsicInst>(U)) {
4948 switch (II->getIntrinsicID()) {
4949 default:
4950 break;
4951 case Intrinsic::abs:
4952 Tmp =
4953 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4954 if (Tmp == 1)
4955 break;
4956
4957 // Absolute value reduces number of sign bits by at most 1.
4958 return Tmp - 1;
4959 case Intrinsic::smin:
4960 case Intrinsic::smax: {
4961 const APInt *CLow, *CHigh;
4962 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
4963 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4964 }
4965 }
4966 }
4967 }
4968 }
4969 }
4970
4971 // Finally, if we can prove that the top bits of the result are 0's or 1's,
4972 // use this information.
4973
4974 // If we can examine all elements of a vector constant successfully, we're
4975 // done (we can't do any better than that). If not, keep trying.
4976 if (unsigned VecSignBits =
4977 computeNumSignBitsVectorConstant(V, DemandedElts, TyBits))
4978 return VecSignBits;
4979
4980 KnownBits Known(TyBits);
4981 computeKnownBits(V, DemandedElts, Known, Q, Depth);
4982
4983 // If we know that the sign bit is either zero or one, determine the number of
4984 // identical bits in the top of the input value.
4985 return std::max(FirstAnswer, Known.countMinSignBits());
4986}
4987
4989 const TargetLibraryInfo *TLI) {
4990 const Function *F = CB.getCalledFunction();
4991 if (!F)
4993
4994 if (F->isIntrinsic())
4995 return F->getIntrinsicID();
4996
4997 // We are going to infer semantics of a library function based on mapping it
4998 // to an LLVM intrinsic. Check that the library function is available from
4999 // this callbase and in this environment.
5000 if (F->hasLocalLinkage() || !TLI || !CB.onlyReadsMemory())
5002
5003 LibFunc Func = TLI->getLibFunc(CB);
5004 if (Func == NotLibFunc)
5006
5007 switch (Func) {
5008 default:
5009 break;
5010 case LibFunc_sin:
5011 case LibFunc_sinf:
5012 case LibFunc_sinl:
5013 return Intrinsic::sin;
5014 case LibFunc_cos:
5015 case LibFunc_cosf:
5016 case LibFunc_cosl:
5017 return Intrinsic::cos;
5018 case LibFunc_tan:
5019 case LibFunc_tanf:
5020 case LibFunc_tanl:
5021 return Intrinsic::tan;
5022 case LibFunc_asin:
5023 case LibFunc_asinf:
5024 case LibFunc_asinl:
5025 return Intrinsic::asin;
5026 case LibFunc_acos:
5027 case LibFunc_acosf:
5028 case LibFunc_acosl:
5029 return Intrinsic::acos;
5030 case LibFunc_atan:
5031 case LibFunc_atanf:
5032 case LibFunc_atanl:
5033 return Intrinsic::atan;
5034 case LibFunc_atan2:
5035 case LibFunc_atan2f:
5036 case LibFunc_atan2l:
5037 return Intrinsic::atan2;
5038 case LibFunc_sinh:
5039 case LibFunc_sinhf:
5040 case LibFunc_sinhl:
5041 return Intrinsic::sinh;
5042 case LibFunc_cosh:
5043 case LibFunc_coshf:
5044 case LibFunc_coshl:
5045 return Intrinsic::cosh;
5046 case LibFunc_tanh:
5047 case LibFunc_tanhf:
5048 case LibFunc_tanhl:
5049 return Intrinsic::tanh;
5050 case LibFunc_exp:
5051 case LibFunc_expf:
5052 case LibFunc_expl:
5053 return Intrinsic::exp;
5054 case LibFunc_exp2:
5055 case LibFunc_exp2f:
5056 case LibFunc_exp2l:
5057 return Intrinsic::exp2;
5058 case LibFunc_exp10:
5059 case LibFunc_exp10f:
5060 case LibFunc_exp10l:
5061 return Intrinsic::exp10;
5062 case LibFunc_log:
5063 case LibFunc_logf:
5064 case LibFunc_logl:
5065 return Intrinsic::log;
5066 case LibFunc_log10:
5067 case LibFunc_log10f:
5068 case LibFunc_log10l:
5069 return Intrinsic::log10;
5070 case LibFunc_log2:
5071 case LibFunc_log2f:
5072 case LibFunc_log2l:
5073 return Intrinsic::log2;
5074 case LibFunc_fabs:
5075 case LibFunc_fabsf:
5076 case LibFunc_fabsl:
5077 return Intrinsic::fabs;
5078 case LibFunc_fmin:
5079 case LibFunc_fminf:
5080 case LibFunc_fminl:
5081 return Intrinsic::minnum;
5082 case LibFunc_fmax:
5083 case LibFunc_fmaxf:
5084 case LibFunc_fmaxl:
5085 return Intrinsic::maxnum;
5086 case LibFunc_copysign:
5087 case LibFunc_copysignf:
5088 case LibFunc_copysignl:
5089 return Intrinsic::copysign;
5090 case LibFunc_floor:
5091 case LibFunc_floorf:
5092 case LibFunc_floorl:
5093 return Intrinsic::floor;
5094 case LibFunc_ceil:
5095 case LibFunc_ceilf:
5096 case LibFunc_ceill:
5097 return Intrinsic::ceil;
5098 case LibFunc_trunc:
5099 case LibFunc_truncf:
5100 case LibFunc_truncl:
5101 return Intrinsic::trunc;
5102 case LibFunc_rint:
5103 case LibFunc_rintf:
5104 case LibFunc_rintl:
5105 return Intrinsic::rint;
5106 case LibFunc_nearbyint:
5107 case LibFunc_nearbyintf:
5108 case LibFunc_nearbyintl:
5109 return Intrinsic::nearbyint;
5110 case LibFunc_round:
5111 case LibFunc_roundf:
5112 case LibFunc_roundl:
5113 return Intrinsic::round;
5114 case LibFunc_roundeven:
5115 case LibFunc_roundevenf:
5116 case LibFunc_roundevenl:
5117 return Intrinsic::roundeven;
5118 case LibFunc_pow:
5119 case LibFunc_powf:
5120 case LibFunc_powl:
5121 return Intrinsic::pow;
5122 case LibFunc_sqrt:
5123 case LibFunc_sqrtf:
5124 case LibFunc_sqrtl:
5125 return Intrinsic::sqrt;
5126 }
5127
5129}
5130
5131/// Given an exploded icmp instruction, return true if the comparison only
5132/// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if
5133/// the result of the comparison is true when the input value is signed.
5135 bool &TrueIfSigned) {
5136 switch (Pred) {
5137 case ICmpInst::ICMP_SLT: // True if LHS s< 0
5138 TrueIfSigned = true;
5139 return RHS.isZero();
5140 case ICmpInst::ICMP_SLE: // True if LHS s<= -1
5141 TrueIfSigned = true;
5142 return RHS.isAllOnes();
5143 case ICmpInst::ICMP_SGT: // True if LHS s> -1
5144 TrueIfSigned = false;
5145 return RHS.isAllOnes();
5146 case ICmpInst::ICMP_SGE: // True if LHS s>= 0
5147 TrueIfSigned = false;
5148 return RHS.isZero();
5149 case ICmpInst::ICMP_UGT:
5150 // True if LHS u> RHS and RHS == sign-bit-mask - 1
5151 TrueIfSigned = true;
5152 return RHS.isMaxSignedValue();
5153 case ICmpInst::ICMP_UGE:
5154 // True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
5155 TrueIfSigned = true;
5156 return RHS.isMinSignedValue();
5157 case ICmpInst::ICMP_ULT:
5158 // True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
5159 TrueIfSigned = false;
5160 return RHS.isMinSignedValue();
5161 case ICmpInst::ICMP_ULE:
5162 // True if LHS u<= RHS and RHS == sign-bit-mask - 1
5163 TrueIfSigned = false;
5164 return RHS.isMaxSignedValue();
5165 default:
5166 return false;
5167 }
5168}
5169
5171 bool CondIsTrue,
5172 const Instruction *CtxI,
5173 KnownFPClass &KnownFromContext,
5174 unsigned Depth = 0) {
5175 Value *A, *B;
5177 (CondIsTrue ? match(Cond, m_LogicalAnd(m_Value(A), m_Value(B)))
5178 : match(Cond, m_LogicalOr(m_Value(A), m_Value(B))))) {
5179 computeKnownFPClassFromCond(V, A, CondIsTrue, CtxI, KnownFromContext,
5180 Depth + 1);
5181 computeKnownFPClassFromCond(V, B, CondIsTrue, CtxI, KnownFromContext,
5182 Depth + 1);
5183 return;
5184 }
5186 computeKnownFPClassFromCond(V, A, !CondIsTrue, CtxI, KnownFromContext,
5187 Depth + 1);
5188 return;
5189 }
5191 Value *LHS;
5192 uint64_t ClassVal = 0;
5193 const APFloat *CRHS;
5194 const APInt *RHS;
5195 if (match(Cond, m_FCmp(Pred, m_Value(LHS), m_APFloat(CRHS)))) {
5196 auto [CmpVal, MaskIfTrue, MaskIfFalse] = fcmpImpliesClass(
5198 LHS != V);
5199 if (CmpVal == V)
5200 KnownFromContext.knownNot(~(CondIsTrue ? MaskIfTrue : MaskIfFalse));
5202 m_Specific(V), m_ConstantInt(ClassVal)))) {
5203 FPClassTest Mask = static_cast<FPClassTest>(ClassVal);
5204 KnownFromContext.knownNot(CondIsTrue ? ~Mask : Mask);
5206 m_APInt(RHS)))) {
5207 bool TrueIfSigned;
5208 if (!isSignBitCheck(Pred, *RHS, TrueIfSigned))
5209 return;
5210 if (TrueIfSigned == CondIsTrue)
5211 KnownFromContext.signBitMustBeOne();
5212 else
5213 KnownFromContext.signBitMustBeZero();
5214 }
5215}
5216
5217/// Compute the minimum and maximum values (inclusive) for the exponent of \p V,
5218/// assuming it is not nan. Returns {min, max, max-assuming-nonzero}. A value
5219/// frexp(0) = 0, so the tighter max-assuming-nonzero bound is only usable when
5220/// \p V is known not to be a logical zero (e.g., for fabs(x) < 0.25, the non-0
5221/// exponent range is [-149, -2], but the 0 edge case is above this range).
5222static std::tuple<int, int, int>
5224 if (!Q.CtxI || !Q.DC || !Q.DT)
5226
5227 // Intersect the bounds implied by every dominating condition, keeping the
5228 // tightest maximum. A value may participate in multiple compares
5229 // (e.g. fabs(x) < 2.0 and fabs(x) < 1.0), and the tighter one wins.
5230 int MaxExp = APFloat::IEK_Inf;
5231 int MaxExpNonZero = APFloat::IEK_Inf;
5232
5233 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
5235 const APFloat *LimitC;
5236 if (!match(BI->getCondition(),
5237 m_FCmp(Pred, m_FAbs(m_Specific(V)), m_Finite(LimitC))))
5238 continue;
5239
5242 continue;
5243
5244 // If fabs(x) <= K, implies the exponent min exp range.
5245 // if fabs(x) >= K, swap the successor
5246 bool IsLessEqual =
5250
5251 bool KnownStrictlyLess =
5254
5255 BasicBlockEdge Edge1(BI->getParent(),
5256 BI->getSuccessor(IsLessEqual ? 0 : 1));
5257 if (Q.DT->dominates(Edge1, Q.CtxI->getParent())) {
5258 // frexp returns an exponent one greater than ilogb.
5259 int Exp = ilogb(*LimitC) + 1;
5260
5261 // A strict bound fabs(V) < 2^n forces ilogb(V) <= n - 1, so the max frexp
5262 // exponent drops by one when K is exact power of two.
5263 if (KnownStrictlyLess && LimitC->getExactLog2Abs() != INT_MIN)
5264 --Exp;
5265
5266 // frexp(0) = 0, which the bound above (assuming a normal nonzero value)
5267 // may exclude.
5268
5269 // TODO: Figure out lower bound to detect no-underflow.
5270 MaxExpNonZero = std::min(MaxExpNonZero, Exp);
5271 MaxExp = std::min(MaxExp, std::max(Exp, 0));
5272 }
5273 }
5274
5275 return {APFloat::IEK_NaN, MaxExp, MaxExpNonZero};
5276}
5277
5279 const SimplifyQuery &Q) {
5280 KnownFPClass KnownFromContext;
5281
5282 if (Q.CC && Q.CC->AffectedValues.contains(V))
5284 KnownFromContext);
5285
5286 if (!Q.CtxI)
5287 return KnownFromContext;
5288
5289 if (Q.DC && Q.DT) {
5290 // Handle dominating conditions.
5291 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
5292 Value *Cond = BI->getCondition();
5293
5294 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
5295 if (Q.DT->dominates(Edge0, Q.CtxI->getParent()))
5296 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/true, Q.CtxI,
5297 KnownFromContext);
5298
5299 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
5300 if (Q.DT->dominates(Edge1, Q.CtxI->getParent()))
5301 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/false, Q.CtxI,
5302 KnownFromContext);
5303 }
5304 }
5305
5306 if (!Q.AC)
5307 return KnownFromContext;
5308
5309 // Try to restrict the floating-point classes based on information from
5310 // assumptions.
5311 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
5312 if (!AssumeVH)
5313 continue;
5314 CallInst *I = cast<CallInst>(AssumeVH);
5315
5316 assert(I->getFunction() == Q.CtxI->getParent()->getParent() &&
5317 "Got assumption for the wrong function!");
5318 assert(I->getIntrinsicID() == Intrinsic::assume &&
5319 "must be an assume intrinsic");
5320
5321 if (!isValidAssumeForContext(I, Q))
5322 continue;
5323
5324 computeKnownFPClassFromCond(V, I->getArgOperand(0),
5325 /*CondIsTrue=*/true, Q.CtxI, KnownFromContext);
5326 }
5327
5328 return KnownFromContext;
5329}
5330
5332 Value *Arm, bool Invert,
5333 const SimplifyQuery &SQ,
5334 unsigned Depth) {
5335
5336 KnownFPClass KnownSrc;
5338 /*CondIsTrue=*/!Invert, SQ.CtxI, KnownSrc,
5339 Depth + 1);
5340 KnownSrc = KnownSrc.unionWith(Known);
5341 if (KnownSrc.isUnknown())
5342 return;
5343
5344 if (isGuaranteedNotToBeUndef(Arm, SQ.AC, SQ.CtxI, SQ.DT, Depth + 1))
5345 Known = KnownSrc;
5346}
5347
5348void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
5349 FPClassTest InterestedClasses, KnownFPClass &Known,
5350 const SimplifyQuery &Q, unsigned Depth);
5351
5353 FPClassTest InterestedClasses,
5354 const SimplifyQuery &Q, unsigned Depth) {
5355 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
5356 APInt DemandedElts =
5357 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
5358 computeKnownFPClass(V, DemandedElts, InterestedClasses, Known, Q, Depth);
5359}
5360
5362 const APInt &DemandedElts,
5363 FPClassTest InterestedClasses,
5365 const SimplifyQuery &Q,
5366 unsigned Depth) {
5367 if ((InterestedClasses &
5369 return;
5370
5371 KnownFPClass KnownSrc;
5372 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5373 KnownSrc, Q, Depth + 1);
5374
5375 const fltSemantics &DstTy = Op->getType()->getScalarType()->getFltSemantics();
5376 const fltSemantics &SrcTy =
5377 Op->getOperand(0)->getType()->getScalarType()->getFltSemantics();
5378
5379 const auto *I = dyn_cast<Instruction>(Op);
5380 const Function *F = I ? I->getFunction() : nullptr;
5381 DenormalMode DstMode =
5382 F ? F->getDenormalMode(DstTy) : DenormalMode::getDynamic();
5383 DenormalMode SrcMode =
5384 F ? F->getDenormalMode(SrcTy) : DenormalMode::getDynamic();
5385
5386 Known = KnownFPClass::fptrunc(KnownSrc, DstTy, SrcTy, DstMode, SrcMode);
5387}
5388
5389static void
5391 FPClassTest InterestedClasses,
5392 KnownFPClass &Known, const SimplifyQuery &Q,
5393 unsigned Depth, bool IsAdd, bool IsStrict) {
5394 const Value *LHS = Op->getOperand(0);
5395 const Value *RHS = Op->getOperand(1);
5396
5397 const RoundingMode RM =
5398 IsStrict ? cast<ConstrainedFPIntrinsic>(Op)->getRoundingMode().value_or(
5401
5402 KnownFPClass KnownLHS, KnownRHS;
5403 bool WantNegative =
5404 IsAdd &&
5405 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;
5406 bool WantNaN = (InterestedClasses & fcNan) != fcNone;
5407 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;
5408 bool WantSubnormal = (InterestedClasses & fcSubnormal) != fcNone;
5409
5410 if (!WantNaN && !WantNegative && !WantNegZero && !WantSubnormal)
5411 return;
5412
5413 FPClassTest InterestedSrcs = InterestedClasses;
5414 if (WantNegative)
5415 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5416 if (InterestedClasses & fcNan)
5417 InterestedSrcs |= fcInf;
5418 computeKnownFPClass(RHS, DemandedElts, InterestedSrcs, KnownRHS, Q,
5419 Depth + 1);
5420
5421 // Special case fadd x, x, which is the canonical form of fmul x, 2.
5422 bool Self = LHS == RHS &&
5423 isGuaranteedNotToBeUndef(LHS, Q.AC, Q.CtxI, Q.DT, Depth + 1);
5424 if (Self)
5425 KnownLHS = KnownRHS;
5426
5427 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||
5428 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) || WantNegZero ||
5429 WantSubnormal || !IsAdd) {
5430
5431 // FIXME: Context function should always be passed in separately
5432 const Function *F = cast<Instruction>(Op)->getFunction();
5433 const fltSemantics &FltSem =
5434 Op->getType()->getScalarType()->getFltSemantics();
5436 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5437
5438 if (Self && IsAdd) {
5439 Known = KnownFPClass::fadd_self(KnownLHS, RM, Mode);
5440 } else {
5441 // RHS is canonically cheaper to compute. Skip inspecting the LHS if
5442 // there's no point.
5443
5444 if (!Self) {
5445 computeKnownFPClass(LHS, DemandedElts, InterestedSrcs, KnownLHS, Q,
5446 Depth + 1);
5447 }
5448
5449 if (const APFloat *CRHS; IsAdd && match(RHS, m_APFloat(CRHS))) {
5450 Known = KnownFPClass::fadd(KnownLHS, *CRHS, RM, Mode);
5451 return;
5452 }
5453
5454 Known = IsAdd ? KnownFPClass::fadd(KnownLHS, KnownRHS, RM, Mode)
5455 : KnownFPClass::fsub(KnownLHS, KnownRHS, RM, Mode);
5456 }
5457 }
5458}
5459
5461 const APInt &DemandedElts,
5462 FPClassTest InterestedClasses,
5464 const SimplifyQuery &Q, unsigned Depth) {
5465 const Value *LHS = Op->getOperand(0);
5466 const Value *RHS = Op->getOperand(1);
5467
5468 FPClassTest InterestedLHS = fcNone;
5469 FPClassTest InterestedRHS = fcNone;
5470
5471 // NaN is also generated for frem(Inf, x) and frem(x, 0.0).
5472 if (InterestedClasses & fcNan) {
5473 InterestedLHS |= fcNan | fcInf;
5474 InterestedRHS |= fcNan | fcZero | fcSubnormal;
5475 }
5476
5477 // The sign for frem is the same as the first operand.
5478 if (InterestedClasses & (fcPosNormal | fcPosSubnormal))
5479 InterestedLHS |= fcPosNormal | fcPosSubnormal;
5480 if (InterestedClasses & (fcNegNormal | fcNegSubnormal))
5481 InterestedLHS |= fcNegNormal | fcNegSubnormal;
5482
5483 // A negative zero result requires a negative finite first operand.
5484 if (InterestedClasses & fcNegZero)
5485 InterestedLHS |= fcNegFinite;
5486
5487 // A positive zero result can additionally come from a negative finite
5488 // result being flushed to positive zero.
5489 if (InterestedClasses & fcPosZero)
5490 InterestedLHS |= fcPosFinite | fcNegNormal | fcNegSubnormal;
5491
5492 const Function *F = cast<Instruction>(Op)->getFunction();
5494 F ? F->getDenormalMode(Op->getType()->getScalarType()->getFltSemantics())
5496
5497 if (LHS == RHS && isGuaranteedNotToBeUndef(LHS, Q.AC, Q.CtxI, Q.DT)) {
5498 // X % X is always exactly [+-]0.0 or a NaN.
5499 FPClassTest InterestedSrcs = InterestedLHS | InterestedRHS;
5500 KnownFPClass KnownSrc;
5501 if (InterestedSrcs != fcNone)
5502 computeKnownFPClass(LHS, DemandedElts, InterestedSrcs, KnownSrc, Q,
5503 Depth + 1);
5504 Known = KnownFPClass::frem_self(KnownSrc, Mode);
5505 return;
5506 }
5507
5508 KnownFPClass KnownLHS;
5509 if (InterestedLHS != fcNone)
5510 computeKnownFPClass(LHS, DemandedElts, InterestedLHS, KnownLHS, Q,
5511 Depth + 1);
5512
5513 KnownFPClass KnownRHS;
5514 // RHS is only useful for refining NaN classes.
5515 if (InterestedRHS != fcNone && KnownLHS.isKnownNever(fcSNan))
5516 computeKnownFPClass(RHS, DemandedElts, InterestedRHS, KnownRHS, Q,
5517 Depth + 1);
5518
5519 Known = KnownFPClass::frem(KnownLHS, KnownRHS, Mode);
5520}
5521
5523 switch (IID) {
5524 case Intrinsic::minimum:
5526 case Intrinsic::maximum:
5528 case Intrinsic::minimumnum:
5530 case Intrinsic::maximumnum:
5532 case Intrinsic::minnum:
5534 case Intrinsic::maxnum:
5536 default:
5537 llvm_unreachable("not a floating-point min-max intrinsic");
5538 }
5539}
5540
5541/// \return true if this is a floating point value that is known to have a
5542/// magnitude smaller than 1. i.e., fabs(X) <= 1.0 or is nan.
5543static bool isAbsoluteValueULEOne(const Value *V) {
5544 // TODO: Handle frexp
5545 // TODO: Other rounding intrinsics?
5546 // TODO: Try computeKnownExponentRangeFromContext
5547
5548 // fabs(x - floor(x)) <= 1
5549 const Value *SubFloorX;
5550 if (match(V, m_FSub(m_Value(SubFloorX),
5552 return true;
5553
5556}
5557
5558void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
5559 FPClassTest InterestedClasses, KnownFPClass &Known,
5560 const SimplifyQuery &Q, unsigned Depth) {
5561 assert(Known.isUnknown() && "should not be called with known information");
5562
5563 if (!DemandedElts) {
5564 // No demanded elts, better to assume we don't know anything.
5565 Known.resetAll();
5566 return;
5567 }
5568
5569 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
5570
5571 if (auto *CFP = dyn_cast<ConstantFP>(V)) {
5572 Known = KnownFPClass(CFP->getValueAPF());
5573 return;
5574 }
5575
5577 Known.setKnownFPClasses(fcPosZero);
5578 return;
5579 }
5580
5581 if (isa<PoisonValue>(V)) {
5583 return;
5584 }
5585
5586 // Try to handle fixed width vector constants
5587 auto *VFVTy = dyn_cast<FixedVectorType>(V->getType());
5588 const Constant *CV = dyn_cast<Constant>(V);
5589 if (VFVTy && CV) {
5591
5592 // For vectors, verify that each element is not NaN.
5593 unsigned NumElts = VFVTy->getNumElements();
5594 for (unsigned i = 0; i != NumElts; ++i) {
5595 if (!DemandedElts[i])
5596 continue;
5597
5598 Constant *Elt = CV->getAggregateElement(i);
5599 if (!Elt) {
5600 Known = KnownFPClass();
5601 return;
5602 }
5603 if (isa<PoisonValue>(Elt))
5604 continue;
5605 auto *CElt = dyn_cast<ConstantFP>(Elt);
5606 if (!CElt) {
5607 Known = KnownFPClass();
5608 return;
5609 }
5610
5611 Known |= KnownFPClass(CElt->getValueAPF());
5612 }
5613 return;
5614 }
5615
5616 if (const auto *CDS = dyn_cast<ConstantDataSequential>(V)) {
5618 for (size_t I = 0, E = CDS->getNumElements(); I != E; ++I)
5619 Known |= KnownFPClass(CDS->getElementAsAPFloat(I));
5620 return;
5621 }
5622
5623 if (const auto *CA = dyn_cast<ConstantAggregate>(V)) {
5624 // TODO: Handle complex aggregates
5626 for (const Use &Op : CA->operands()) {
5627 auto *CFP = dyn_cast<ConstantFP>(Op.get());
5628 if (!CFP) {
5629 Known = KnownFPClass();
5630 return;
5631 }
5632
5633 Known |= KnownFPClass(CFP->getValueAPF());
5634 }
5635
5636 return;
5637 }
5638
5639 FPClassTest KnownNotFromFlags = fcNone;
5640 if (const auto *CB = dyn_cast<CallBase>(V))
5641 KnownNotFromFlags |= CB->getRetNoFPClass();
5642 else if (const auto *Arg = dyn_cast<Argument>(V))
5643 KnownNotFromFlags |= Arg->getNoFPClass();
5644
5645 const Operator *Op = dyn_cast<Operator>(V);
5647 if (FPOp->hasNoNaNs())
5648 KnownNotFromFlags |= fcNan;
5649 if (FPOp->hasNoInfs())
5650 KnownNotFromFlags |= fcInf;
5651 }
5652
5653 KnownFPClass AssumedClasses = computeKnownFPClassFromContext(V, Q);
5654 KnownNotFromFlags |= ~AssumedClasses.getKnownFPClasses();
5655
5656 // We no longer need to find out about these bits from inputs if we can
5657 // assume this from flags/attributes.
5658 InterestedClasses &= ~KnownNotFromFlags;
5659
5660 llvm::scope_exit ClearClassesFromFlags([=, &Known] {
5661 Known.knownNot(KnownNotFromFlags);
5662 if (!Known.getSignBit() && AssumedClasses.getSignBit()) {
5663 if (*AssumedClasses.getSignBit())
5664 Known.signBitMustBeOne();
5665 else
5666 Known.signBitMustBeZero();
5667 }
5668 });
5669
5670 if (!Op)
5671 return;
5672
5673 // All recursive calls that increase depth must come after this.
5675 return;
5676
5677 const unsigned Opc = Op->getOpcode();
5678 switch (Opc) {
5679 case Instruction::FNeg: {
5680 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5681 Known, Q, Depth + 1);
5682 Known.fneg();
5683 break;
5684 }
5685 case Instruction::Select: {
5686 auto ComputeForArm = [&](Value *Arm, bool Invert) {
5687 KnownFPClass Res;
5688 computeKnownFPClass(Arm, DemandedElts, InterestedClasses, Res, Q,
5689 Depth + 1);
5690 adjustKnownFPClassForSelectArm(Res, Op->getOperand(0), Arm, Invert, Q,
5691 Depth);
5692 return Res;
5693 };
5694 // Only known if known in both the LHS and RHS.
5695 Known =
5696 ComputeForArm(Op->getOperand(1), /*Invert=*/false)
5697 .intersectWith(ComputeForArm(Op->getOperand(2), /*Invert=*/true));
5698 break;
5699 }
5700 case Instruction::Load: {
5701 const MDNode *NoFPClass =
5702 cast<LoadInst>(Op)->getMetadata(LLVMContext::MD_nofpclass);
5703 if (!NoFPClass)
5704 break;
5705
5706 ConstantInt *MaskVal =
5708 Known.knownNot(static_cast<FPClassTest>(MaskVal->getZExtValue()));
5709 break;
5710 }
5711 case Instruction::Call: {
5712 const CallInst *II = cast<CallInst>(Op);
5713 const Intrinsic::ID IID = II->getIntrinsicID();
5714 switch (IID) {
5715 case Intrinsic::fabs: {
5716 if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) {
5717 // If we only care about the sign bit we don't need to inspect the
5718 // operand.
5719 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5720 InterestedClasses, Known, Q, Depth + 1);
5721 }
5722
5723 Known.fabs();
5724 break;
5725 }
5726 case Intrinsic::copysign: {
5727 KnownFPClass KnownSign;
5728
5729 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5730 Known, Q, Depth + 1);
5731 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5732 KnownSign, Q, Depth + 1);
5733 Known.copysign(KnownSign);
5734 break;
5735 }
5736 case Intrinsic::fma:
5737 case Intrinsic::fmuladd: {
5738 if ((InterestedClasses & (fcNegative | fcNan)) == fcNone)
5739 break;
5740
5741 const bool IsFMA = IID == Intrinsic::fma;
5742
5743 // FIXME: This should check isGuaranteedNotToBeUndef
5744 if (II->getArgOperand(0) == II->getArgOperand(1)) {
5745 FPClassTest InterestedSquared = InterestedClasses;
5746 FPClassTest InterestedAddend = InterestedClasses;
5747 // Only fma(+inf, +inf, -inf) and fma(-inf, -inf, -inf) produce NaN when
5748 // squaring.
5749 if (InterestedClasses & fcQNan) {
5750 InterestedSquared |= fcSNan | fcInf;
5751 InterestedAddend |= fcSNan | fcNegInf;
5752 }
5753
5754 KnownFPClass KnownSrc, KnownAddend;
5755 computeKnownFPClass(II->getArgOperand(2), DemandedElts,
5756 InterestedAddend, KnownAddend, Q, Depth + 1);
5757 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5758 InterestedSquared, KnownSrc, Q, Depth + 1);
5759
5760 const Function *F = II->getFunction();
5761 const fltSemantics &FltSem =
5762 II->getType()->getScalarType()->getFltSemantics();
5764 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5765
5766 if ((KnownNotFromFlags & fcNan) == fcNan) {
5767 KnownSrc.knownNot(fcNan);
5768 KnownAddend.knownNot(fcNan);
5769 }
5770
5771 if ((KnownNotFromFlags & fcInf) == fcInf) {
5772 KnownSrc.knownNot(fcInf);
5773 KnownAddend.knownNot(fcInf);
5774 }
5775
5776 Known = IsFMA
5777 ? KnownFPClass::fma_square(KnownSrc, KnownAddend, Mode)
5778 : KnownFPClass::fmuladd_square(KnownSrc, KnownAddend, Mode);
5779 break;
5780 }
5781
5782 KnownFPClass KnownSrc[3];
5783 for (int I = 0; I != 3; ++I) {
5784 computeKnownFPClass(II->getArgOperand(I), DemandedElts,
5785 InterestedClasses, KnownSrc[I], Q, Depth + 1);
5786 if (KnownSrc[I].isUnknown())
5787 return;
5788
5789 if ((KnownNotFromFlags & fcNan) == fcNan)
5790 KnownSrc[I].knownNot(fcNan);
5791 if ((KnownNotFromFlags & fcInf) == fcInf)
5792 KnownSrc[I].knownNot(fcInf);
5793 }
5794
5795 const Function *F = II->getFunction();
5796 const fltSemantics &FltSem =
5797 II->getType()->getScalarType()->getFltSemantics();
5799 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5800 Known =
5801 IsFMA ? KnownFPClass::fma(KnownSrc[0], KnownSrc[1], KnownSrc[2], Mode)
5802 : KnownFPClass::fmuladd(KnownSrc[0], KnownSrc[1], KnownSrc[2],
5803 Mode);
5804 break;
5805 }
5806 case Intrinsic::experimental_constrained_fadd:
5807 case Intrinsic::experimental_constrained_fsub: {
5809 Op, DemandedElts, InterestedClasses, Known, Q, Depth,
5810 /*IsAdd=*/IID == Intrinsic::experimental_constrained_fadd,
5811 /*IsStrict=*/true);
5812 break;
5813 }
5814 case Intrinsic::experimental_constrained_frem: {
5815 computeKnownFPClassForFRem(Op, DemandedElts, InterestedClasses, Known, Q,
5816 Depth);
5817 break;
5818 }
5819 case Intrinsic::sqrt:
5820 case Intrinsic::experimental_constrained_sqrt: {
5821 KnownFPClass KnownSrc;
5822 FPClassTest InterestedSrcs = InterestedClasses;
5823 if (InterestedClasses & fcQNan)
5825 // Subnormals may flush to zero.
5826 if (InterestedClasses & fcZero)
5827 InterestedSrcs |= fcSubnormal;
5828
5829 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5830 KnownSrc, Q, Depth + 1);
5831
5833
5834 bool HasNSZ = Q.IIQ.hasNoSignedZeros(II);
5835 if (!HasNSZ) {
5836 const Function *F = II->getFunction();
5837 const fltSemantics &FltSem =
5838 II->getType()->getScalarType()->getFltSemantics();
5839 Mode = F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5840 }
5841
5842 Known = KnownFPClass::sqrt(KnownSrc, Mode);
5843 if (HasNSZ)
5844 Known.knownNot(fcNegZero);
5845
5846 break;
5847 }
5848 case Intrinsic::sin: {
5849 KnownFPClass KnownSrc;
5850 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5851 KnownSrc, Q, Depth + 1);
5852 Known = KnownFPClass::sin(KnownSrc);
5853 break;
5854 }
5855 case Intrinsic::cos: {
5856 KnownFPClass KnownSrc;
5857 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5858 KnownSrc, Q, Depth + 1);
5859 Known = KnownFPClass::cos(KnownSrc);
5860 break;
5861 }
5862 case Intrinsic::tan: {
5863 KnownFPClass KnownSrc;
5864 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5865 KnownSrc, Q, Depth + 1);
5866 Known = KnownFPClass::tan(KnownSrc);
5867 break;
5868 }
5869 case Intrinsic::sinh: {
5870 KnownFPClass KnownSrc;
5871 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5872 KnownSrc, Q, Depth + 1);
5873 Known = KnownFPClass::sinh(KnownSrc);
5874 break;
5875 }
5876 case Intrinsic::cosh: {
5877 KnownFPClass KnownSrc;
5878 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5879 KnownSrc, Q, Depth + 1);
5880 Known = KnownFPClass::cosh(KnownSrc);
5881 break;
5882 }
5883 case Intrinsic::tanh: {
5884 KnownFPClass KnownSrc;
5885 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5886 KnownSrc, Q, Depth + 1);
5887 Known = KnownFPClass::tanh(KnownSrc);
5888 break;
5889 }
5890 case Intrinsic::asin: {
5891 KnownFPClass KnownSrc;
5892 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5893 KnownSrc, Q, Depth + 1);
5894 Known = KnownFPClass::asin(KnownSrc);
5895 break;
5896 }
5897 case Intrinsic::acos: {
5898 KnownFPClass KnownSrc;
5899 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5900 KnownSrc, Q, Depth + 1);
5901 Known = KnownFPClass::acos(KnownSrc);
5902 break;
5903 }
5904 case Intrinsic::atan: {
5905 KnownFPClass KnownSrc;
5906 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5907 KnownSrc, Q, Depth + 1);
5908 Known = KnownFPClass::atan(KnownSrc);
5909 break;
5910 }
5911 case Intrinsic::atan2: {
5912 FPClassTest InterestedY = InterestedClasses;
5913 FPClassTest InterestedX = InterestedClasses;
5914
5915 // We can rule out negative values if y cannot have a negative value.
5916 if ((InterestedClasses & fcNegFinite) != fcNone)
5917 InterestedY |= fcNegative;
5918
5919 // We can rule out positive values if y cannot have a positive value.
5920 if ((InterestedClasses & fcPosFinite) != fcNone)
5921 InterestedY |= fcPositive | fcNegSubnormal;
5922
5923 // We can rule out zero and subnormal if x cannot have a positive value.
5924 if ((InterestedClasses & (fcZero | fcSubnormal)) != fcNone)
5925 InterestedX |= fcPositive | fcNegSubnormal;
5926
5927 KnownFPClass KnownY, KnownX;
5928 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedY,
5929 KnownY, Q, Depth + 1);
5930 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedX,
5931 KnownX, Q, Depth + 1);
5932
5933 const Function *F = II->getFunction();
5935 F ? F->getDenormalMode(
5936 II->getType()->getScalarType()->getFltSemantics())
5938 Known = KnownFPClass::atan2(KnownY, KnownX, Mode);
5939 break;
5940 }
5941 case Intrinsic::maxnum:
5942 case Intrinsic::minnum:
5943 case Intrinsic::minimum:
5944 case Intrinsic::maximum:
5945 case Intrinsic::minimumnum:
5946 case Intrinsic::maximumnum: {
5947 KnownFPClass KnownLHS, KnownRHS;
5948 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5949 KnownLHS, Q, Depth + 1);
5950 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5951 KnownRHS, Q, Depth + 1);
5952
5953 const Function *F = II->getFunction();
5954
5956 F ? F->getDenormalMode(
5957 II->getType()->getScalarType()->getFltSemantics())
5959
5960 Known = KnownFPClass::minMaxLike(KnownLHS, KnownRHS, getMinMaxKind(IID),
5961 Mode);
5962 break;
5963 }
5964 case Intrinsic::canonicalize: {
5965 KnownFPClass KnownSrc;
5966 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5967 KnownSrc, Q, Depth + 1);
5968
5969 const Function *F = II->getFunction();
5970 const fltSemantics &Flt =
5971 II->getType()->getScalarType()->getFltSemantics();
5973 F ? F->getDenormalMode(Flt) : DenormalMode::getDynamic();
5974 Known = KnownFPClass::canonicalize(KnownSrc, Flt, Mode);
5975 break;
5976 }
5977 case Intrinsic::vector_reduce_fmax:
5978 case Intrinsic::vector_reduce_fmin:
5979 case Intrinsic::vector_reduce_fmaximum:
5980 case Intrinsic::vector_reduce_fminimum:
5981 case Intrinsic::vector_reduce_fmaximumnum:
5982 case Intrinsic::vector_reduce_fminimumnum: {
5983 // reduce min/max will choose an element from one of the vector elements,
5984 // so we can infer and class information that is common to all elements.
5985 Known = computeKnownFPClass(II->getArgOperand(0), II->getFastMathFlags(),
5986 InterestedClasses, Q, Depth + 1);
5987 // Can only propagate sign if output is never NaN.
5988 if (!Known.isKnownNeverNaN())
5989 Known.setSignBit(std::nullopt);
5990 break;
5991 }
5992 // reverse preserves all characteristics of the input vec's element.
5993 case Intrinsic::vector_reverse:
5995 II->getArgOperand(0), DemandedElts.reverseBits(),
5996 II->getFastMathFlags(), InterestedClasses, Q, Depth + 1);
5997 break;
5998 case Intrinsic::trunc:
5999 case Intrinsic::floor:
6000 case Intrinsic::ceil:
6001 case Intrinsic::rint:
6002 case Intrinsic::nearbyint:
6003 case Intrinsic::round:
6004 case Intrinsic::roundeven: {
6005 KnownFPClass KnownSrc;
6006 FPClassTest InterestedSrcs = InterestedClasses;
6007
6008 // Negative round ups towards zero produce negative zero.
6009 if (InterestedSrcs & fcNegFinite)
6010 InterestedSrcs |= fcNegFinite;
6011
6012 // Negative subnormals may flush to positive zero.
6013 if (InterestedSrcs & fcPosFinite)
6014 InterestedSrcs |= fcPosFinite | fcNegSubnormal;
6015
6016 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
6017 KnownSrc, Q, Depth + 1);
6018
6019 const Function *F = II->getFunction();
6021 F ? F->getDenormalMode(
6022 II->getType()->getScalarType()->getFltSemantics())
6024 const bool IsMultiUnitFPType =
6025 V->getType()->getScalarType()->isMultiUnitFPType();
6026
6027 switch (IID) {
6028 case Intrinsic::trunc:
6029 Known = KnownFPClass::trunc(KnownSrc, Mode);
6030 break;
6031 case Intrinsic::floor:
6032 Known = KnownFPClass::floor(KnownSrc, IsMultiUnitFPType, Mode);
6033 break;
6034 case Intrinsic::ceil:
6035 Known = KnownFPClass::ceil(KnownSrc, IsMultiUnitFPType, Mode);
6036 break;
6037 default:
6038 Known =
6039 KnownFPClass::roundToIntegral(KnownSrc, IsMultiUnitFPType, Mode);
6040 break;
6041 }
6042 break;
6043 }
6044 case Intrinsic::exp:
6045 case Intrinsic::exp2:
6046 case Intrinsic::exp10:
6047 case Intrinsic::amdgcn_exp2: {
6048 KnownFPClass KnownSrc;
6049 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
6050 KnownSrc, Q, Depth + 1);
6051
6052 Known = KnownFPClass::exp(KnownSrc);
6053
6054 Type *EltTy = II->getType()->getScalarType();
6055 if (IID == Intrinsic::amdgcn_exp2 && EltTy->isFloatTy())
6056 Known.knownNot(fcSubnormal);
6057
6058 break;
6059 }
6060 case Intrinsic::fptrunc_round: {
6061 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,
6062 Q, Depth);
6063 break;
6064 }
6065 case Intrinsic::log:
6066 case Intrinsic::log10:
6067 case Intrinsic::log2:
6068 case Intrinsic::experimental_constrained_log:
6069 case Intrinsic::experimental_constrained_log10:
6070 case Intrinsic::experimental_constrained_log2:
6071 case Intrinsic::amdgcn_log: {
6072 FPClassTest InterestedSrcs = fcNone;
6073
6074 // log(negative) produces NaN.
6075 if ((InterestedClasses & fcNan) != fcNone)
6076 InterestedSrcs |= fcNan | fcNegative;
6077
6078 // log(logical-zero) produces negative infinity.
6079 if ((InterestedClasses & fcNegInf) != fcNone)
6080 InterestedSrcs |= fcZero | fcSubnormal;
6081
6082 // log(x) < -0.0 if x < +1.0
6083 if ((InterestedClasses & fcNegNormal) != fcNone)
6084 InterestedSrcs |= fcPosSubnormal | fcPosNormal;
6085
6086 // log(x) >= +0.0 if x >= +1.0
6087 if ((InterestedClasses & (fcPosZero | fcPosNormal)) != fcNone)
6088 InterestedSrcs |= fcPosNormal;
6089
6090 // log(x) is positive infinity iff x is positive infinity.
6091 if ((InterestedClasses & fcPosInf) != fcNone)
6092 InterestedSrcs |= fcPosInf;
6093
6094 KnownFPClass KnownSrc;
6095 if (InterestedSrcs != fcNone)
6096 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
6097 KnownSrc, Q, Depth + 1);
6098 const Function *F = II->getFunction();
6100 F ? F->getDenormalMode(
6101 II->getType()->getScalarType()->getFltSemantics())
6103 const bool IsMultiUnitFPType =
6104 V->getType()->getScalarType()->isMultiUnitFPType();
6105 Known = KnownFPClass::log(KnownSrc, Mode, IsMultiUnitFPType);
6106 break;
6107 }
6108 case Intrinsic::pow: {
6109 const bool WantNaN = (InterestedClasses & fcNan) != fcNone;
6110 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
6111 if (!WantNaN && !WantNegative)
6112 break;
6113
6114 FPClassTest InterestedLHS = fcNone;
6115 FPClassTest InterestedRHS = fcNone;
6116 if (WantNaN) {
6117 // pow may return NaN if one of the arguments is NaN. NaN may also be
6118 // produced from a negative, non-zero finite base and a non-integer
6119 // exponent.
6120 InterestedLHS |= fcNan | fcNegNormal | fcNegSubnormal;
6121 InterestedRHS |= fcNan;
6122 }
6123 if (WantNegative) {
6124 // A negative value is returned when a negative base is raised to an odd
6125 // integer power. Only normal values can be odd integers.
6126 InterestedLHS |= fcNegative;
6127 InterestedRHS |= fcNormal;
6128 }
6129
6130 KnownFPClass KnownLHS;
6131 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedLHS,
6132 KnownLHS, Q, Depth + 1);
6133
6134 // If the LHS is unknown, then querying the RHS is only useful for rare
6135 // edge cases.
6136 if (KnownLHS.isUnknown())
6137 break;
6138
6139 KnownFPClass KnownRHS;
6140 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedRHS,
6141 KnownRHS, Q, Depth + 1);
6142 Known = KnownFPClass::pow(KnownLHS, KnownRHS);
6143 break;
6144 }
6145 case Intrinsic::powi: {
6146 if ((InterestedClasses & (fcNan | fcInf | fcNegative)) == fcNone)
6147 break;
6148
6149 // The exponent is always a scalar, even when raising a vector to a power.
6150 const Value *Exp = II->getArgOperand(1);
6151 unsigned BitWidth = Exp->getType()->getIntegerBitWidth();
6152 KnownBits ExponentKnownBits(BitWidth);
6153 computeKnownBits(Exp, APInt(1, 1), ExponentKnownBits, Q, Depth + 1);
6154
6155 FPClassTest InterestedSrcs = fcNone;
6156 if (InterestedClasses & fcNan)
6157 InterestedSrcs |= fcNan;
6158 if (!ExponentKnownBits.isZero()) {
6159 if (InterestedClasses & fcInf)
6160 InterestedSrcs |= fcFinite | fcInf;
6161 if ((InterestedClasses & fcNegative) && !ExponentKnownBits.isEven())
6162 InterestedSrcs |= fcNegative;
6163 }
6164
6165 KnownFPClass KnownSrc;
6166 if (InterestedSrcs != fcNone)
6167 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
6168 KnownSrc, Q, Depth + 1);
6169
6170 Known = KnownFPClass::powi(KnownSrc, ExponentKnownBits);
6171 break;
6172 }
6173 case Intrinsic::ldexp: {
6174 KnownFPClass KnownSrc;
6175 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
6176 KnownSrc, Q, Depth + 1);
6177 // Can refine inf/zero handling based on the exponent operand.
6178 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;
6179
6180 const Value *ExpArg = II->getArgOperand(1);
6181 ConstantRange ExpKnownRange =
6182 ((KnownSrc.getKnownFPClasses() & ExpInfoMask) != fcNone)
6183 ? computeConstantRange(ExpArg, /*ForSigned=*/true, Q, Depth + 1)
6184 : ConstantRange::getFull(
6185 ExpArg->getType()->getScalarSizeInBits());
6186
6187 const fltSemantics &Flt =
6188 II->getType()->getScalarType()->getFltSemantics();
6189
6190 const Function *F = II->getFunction();
6192 F ? F->getDenormalMode(Flt) : DenormalMode::getDynamic();
6193
6194 Known = KnownFPClass::ldexp(KnownSrc, ExpKnownRange.getSignedMin(),
6195 ExpKnownRange.getSignedMax(), Flt, Mode);
6196 break;
6197 }
6198 case Intrinsic::arithmetic_fence: {
6199 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
6200 Known, Q, Depth + 1);
6201 break;
6202 }
6203 case Intrinsic::experimental_constrained_sitofp:
6204 case Intrinsic::experimental_constrained_uitofp:
6205 // Cannot produce nan
6206 Known.knownNot(fcNan);
6207
6208 // sitofp and uitofp turn into +0.0 for zero.
6209 Known.knownNot(fcNegZero);
6210
6211 // Integers cannot be subnormal
6212 Known.knownNot(fcSubnormal);
6213
6214 if (IID == Intrinsic::experimental_constrained_uitofp)
6215 Known.signBitMustBeZero();
6216
6217 // TODO: Copy inf handling from instructions
6218 break;
6219
6220 case Intrinsic::amdgcn_fract: {
6221 Known.knownNot(fcInf);
6222
6223 if (InterestedClasses & fcNan) {
6224 KnownFPClass KnownSrc;
6225 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
6226 InterestedClasses, KnownSrc, Q, Depth + 1);
6227
6228 if (KnownSrc.isKnownNeverInfOrNaN())
6229 Known.knownNot(fcNan);
6230 else if (KnownSrc.isKnownNever(fcSNan))
6231 Known.knownNot(fcSNan);
6232 }
6233
6234 break;
6235 }
6236 case Intrinsic::amdgcn_rcp: {
6237 KnownFPClass KnownSrc;
6238 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
6239 KnownSrc, Q, Depth + 1);
6240
6241 Known.propagateNonNaN(KnownSrc);
6242
6243 Type *EltTy = II->getType()->getScalarType();
6244
6245 // f32 denormal always flushed.
6246 if (EltTy->isFloatTy()) {
6247 Known.knownNot(fcSubnormal);
6248 KnownSrc.knownNot(fcSubnormal);
6249 }
6250
6251 if (KnownSrc.isKnownNever(fcNegative))
6252 Known.knownNot(fcNegative);
6253 if (KnownSrc.isKnownNever(fcPositive))
6254 Known.knownNot(fcPositive);
6255
6256 if (const Function *F = II->getFunction()) {
6257 DenormalMode Mode = F->getDenormalMode(EltTy->getFltSemantics());
6258 if (KnownSrc.isKnownNeverLogicalPosZero(Mode))
6259 Known.knownNot(fcPosInf);
6260 if (KnownSrc.isKnownNeverLogicalNegZero(Mode))
6261 Known.knownNot(fcNegInf);
6262 }
6263
6264 break;
6265 }
6266 case Intrinsic::amdgcn_rsq: {
6267 KnownFPClass KnownSrc;
6268 // The only negative value that can be returned is -inf for -0 inputs.
6270
6271 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
6272 KnownSrc, Q, Depth + 1);
6273
6274 // Negative -> nan
6275 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
6276 Known.knownNot(fcNan);
6277 else if (KnownSrc.isKnownNever(fcSNan))
6278 Known.knownNot(fcSNan);
6279
6280 // +inf -> +0
6281 if (KnownSrc.isKnownNeverPosInfinity())
6282 Known.knownNot(fcPosZero);
6283
6284 Type *EltTy = II->getType()->getScalarType();
6285
6286 // f32 denormal always flushed.
6287 if (EltTy->isFloatTy())
6288 Known.knownNot(fcPosSubnormal);
6289
6290 if (const Function *F = II->getFunction()) {
6291 DenormalMode Mode = F->getDenormalMode(EltTy->getFltSemantics());
6292
6293 // -0 -> -inf
6294 if (KnownSrc.isKnownNeverLogicalNegZero(Mode))
6295 Known.knownNot(fcNegInf);
6296
6297 // +0 -> +inf
6298 if (KnownSrc.isKnownNeverLogicalPosZero(Mode))
6299 Known.knownNot(fcPosInf);
6300 }
6301
6302 break;
6303 }
6304 case Intrinsic::amdgcn_trig_preop: {
6305 // Always returns a value [0, 1)
6306 Known.knownNot(fcNan | fcInf | fcNegative);
6307 break;
6308 }
6309 case Intrinsic::convert_from_arbitrary_fp: {
6310 auto *MD = cast<MetadataAsValue>(II->getArgOperand(1))->getMetadata();
6311 StringRef FormatStr = cast<MDString>(MD)->getString();
6312
6313 const fltSemantics *SrcSemantics =
6315 if (!SrcSemantics)
6316 break;
6317
6318 const fltSemantics DstSemantics =
6319 II->getType()->getScalarType()->getFltSemantics();
6320
6321 if (!APFloat::semanticsHasNaN(*SrcSemantics))
6322 Known.knownNot(fcNan);
6323
6324 // fcInf can only be cleared if the source format has no Inf encoding
6325 // and the dst max exp can accommodate src max exp.
6326 if (!APFloat::semanticsHasInf(*SrcSemantics) &&
6327 APFloat::semanticsMaxExponent(*SrcSemantics) <=
6328 APFloat::semanticsMaxExponent(DstSemantics))
6329 Known.knownNot(fcInf);
6330
6331 // Check and clear all neg flags for formats that do not have signed
6332 // representation.
6333 if (!APFloat::semanticsHasSignedRepr(*SrcSemantics))
6334 Known.knownNot(fcNegative);
6335
6336 // Check if format has no zero at all (Float8E8M0FNU), or no negative
6337 // zero.
6338 if (!APFloat::semanticsHasZero(*SrcSemantics))
6339 Known.knownNot(fcZero);
6340 else if (SrcSemantics->nanEncoding == fltNanEncoding::NegativeZero)
6341 Known.knownNot(fcNegZero);
6342
6343 // If src lands normally in dest, the result can never be subnormal.
6344 if (APFloat::isRepresentableAsNormalIn(*SrcSemantics, DstSemantics))
6345 Known.knownNot(fcSubnormal);
6346 break;
6347 }
6348 default:
6349 break;
6350 }
6351
6352 break;
6353 }
6354 case Instruction::FAdd:
6355 case Instruction::FSub: {
6356 computeKnownFPClassForFAddSub(Op, DemandedElts, InterestedClasses, Known, Q,
6357 Depth, /*IsAdd=*/Opc == Instruction::FAdd,
6358 /*IsStrict=*/false);
6359 break;
6360 }
6361 case Instruction::FMul: {
6362 const Function *F = cast<Instruction>(Op)->getFunction();
6364 F ? F->getDenormalMode(
6365 Op->getType()->getScalarType()->getFltSemantics())
6367
6368 Value *LHS = Op->getOperand(0);
6369 Value *RHS = Op->getOperand(1);
6370 // X * X is always non-negative or a NaN.
6371 // FIXME: Should check isGuaranteedNotToBeUndef
6372 if (LHS == RHS) {
6373 KnownFPClass KnownSrc;
6374 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownSrc, Q,
6375 Depth + 1);
6376 Known = KnownFPClass::square(KnownSrc, Mode);
6377 break;
6378 }
6379
6380 KnownFPClass KnownLHS, KnownRHS;
6381
6382 const APFloat *CRHS;
6383 if (match(RHS, m_APFloat(CRHS))) {
6384 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Q,
6385 Depth + 1);
6386 Known = KnownFPClass::fmul(KnownLHS, *CRHS, Mode);
6387 } else {
6388 computeKnownFPClass(RHS, DemandedElts, fcAllFlags, KnownRHS, Q,
6389 Depth + 1);
6390 // TODO: Improve accuracy in unfused FMA pattern. We can prove an
6391 // additional not-nan if the addend is known-not negative infinity if the
6392 // multiply is known-not infinity.
6393
6394 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Q,
6395 Depth + 1);
6396 Known = KnownFPClass::fmul(KnownLHS, KnownRHS, Mode);
6397 }
6398
6399 /// Propgate no-infs if the other source is known smaller than one, such
6400 /// that this cannot introduce overflow.
6401 if (KnownLHS.isKnownNever(fcInf) && isAbsoluteValueULEOne(RHS))
6402 Known.knownNot(fcInf);
6403 else if (KnownRHS.isKnownNever(fcInf) && isAbsoluteValueULEOne(LHS))
6404 Known.knownNot(fcInf);
6405
6406 break;
6407 }
6408 case Instruction::FDiv: {
6409 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
6410
6411 const Function *F = cast<Instruction>(Op)->getFunction();
6412 const fltSemantics &FltSem =
6413 Op->getType()->getScalarType()->getFltSemantics();
6415 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
6416
6417 if (Op->getOperand(0) == Op->getOperand(1) &&
6418 isGuaranteedNotToBeUndef(Op->getOperand(0), Q.AC, Q.CtxI, Q.DT)) {
6419 // X / X is always exactly 1.0 or a NaN.
6420 Known.setKnownFPClasses(fcNan | fcPosNormal);
6421
6422 if (!WantNan)
6423 break;
6424
6425 KnownFPClass KnownSrc;
6426 computeKnownFPClass(Op->getOperand(0), DemandedElts,
6427 fcNan | fcInf | fcZero | fcSubnormal, KnownSrc, Q,
6428 Depth + 1);
6429
6430 Known = KnownFPClass::fdiv_self(KnownSrc, Mode);
6431 break;
6432 }
6433
6434 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
6435 const bool WantPositive = (InterestedClasses & fcPositive) != fcNone;
6436 if (!WantNan && !WantNegative && !WantPositive)
6437 break;
6438
6439 KnownFPClass KnownLHS, KnownRHS;
6440 computeKnownFPClass(Op->getOperand(1), DemandedElts, fcAllFlags, KnownRHS,
6441 Q, Depth + 1);
6442
6443 bool KnowSomethingUseful =
6444 KnownRHS.isKnownNever(fcSNan) ||
6447
6448 if (KnowSomethingUseful)
6449 computeKnownFPClass(Op->getOperand(0), DemandedElts, fcAllFlags, KnownLHS,
6450 Q, Depth + 1);
6451
6452 Known = KnownFPClass::fdiv(KnownLHS, KnownRHS, Mode);
6453 break;
6454 }
6455 case Instruction::FRem: {
6456 computeKnownFPClassForFRem(Op, DemandedElts, InterestedClasses, Known, Q,
6457 Depth);
6458 break;
6459 }
6460 case Instruction::FPExt: {
6461 KnownFPClass KnownSrc;
6462 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
6463 KnownSrc, Q, Depth + 1);
6464
6465 const fltSemantics &DstTy =
6466 Op->getType()->getScalarType()->getFltSemantics();
6467 const fltSemantics &SrcTy =
6468 Op->getOperand(0)->getType()->getScalarType()->getFltSemantics();
6469
6470 const auto *I = dyn_cast<Instruction>(Op);
6471 const Function *F = I ? I->getFunction() : nullptr;
6472 DenormalMode DstMode =
6473 F ? F->getDenormalMode(DstTy) : DenormalMode::getDynamic();
6474 DenormalMode SrcMode =
6475 F ? F->getDenormalMode(SrcTy) : DenormalMode::getDynamic();
6476
6477 Known = KnownFPClass::fpext(KnownSrc, DstTy, SrcTy, DstMode, SrcMode);
6478 break;
6479 }
6480 case Instruction::FPTrunc: {
6481 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known, Q,
6482 Depth);
6483 break;
6484 }
6485 case Instruction::SIToFP:
6486 case Instruction::UIToFP: {
6487 // Cannot produce nan
6488 Known.knownNot(fcNan);
6489
6490 // Integers cannot be subnormal
6491 Known.knownNot(fcSubnormal);
6492
6493 // sitofp and uitofp turn into +0.0 for zero.
6494 Known.knownNot(fcNegZero);
6495
6496 // UIToFP is always non-negative regardless of known bits.
6497 if (Op->getOpcode() == Instruction::UIToFP)
6498 Known.signBitMustBeZero();
6499
6500 // Only compute known bits if we can learn something useful from them.
6501 if (!(InterestedClasses & (fcPosZero | fcNormal | fcInf)))
6502 break;
6503
6504 KnownBits IntKnown =
6505 computeKnownBits(Op->getOperand(0), DemandedElts, Q, Depth + 1);
6506
6507 // If the integer is non-zero, the result cannot be +0.0
6508 if (IntKnown.isNonZero())
6509 Known.knownNot(fcPosZero);
6510
6511 if (Op->getOpcode() == Instruction::SIToFP) {
6512 // If the signed integer is known non-negative, the result is
6513 // non-negative. If the signed integer is known negative, the result is
6514 // negative.
6515 if (IntKnown.isNonNegative()) {
6516 Known.signBitMustBeZero();
6517 } else if (IntKnown.isNegative()) {
6518 Known.signBitMustBeOne();
6519 }
6520 }
6521
6522 // Guard kept for ilogb()
6523 if (InterestedClasses & fcInf) {
6524 // Get width of largest magnitude integer known.
6525 // This still works for a signed minimum value because the largest FP
6526 // value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx).
6527 int IntSize = IntKnown.getBitWidth();
6528 if (Op->getOpcode() == Instruction::UIToFP)
6529 IntSize -= IntKnown.countMinLeadingZeros();
6530 else if (Op->getOpcode() == Instruction::SIToFP)
6531 IntSize -= IntKnown.countMinSignBits();
6532
6533 // If the exponent of the largest finite FP value can hold the largest
6534 // integer, the result of the cast must be finite.
6535 Type *FPTy = Op->getType()->getScalarType();
6536 if (ilogb(APFloat::getLargest(FPTy->getFltSemantics())) >= IntSize)
6537 Known.knownNot(fcInf);
6538 }
6539
6540 break;
6541 }
6542 case Instruction::ExtractElement: {
6543 // Look through extract element. If the index is non-constant or
6544 // out-of-range demand all elements, otherwise just the extracted element.
6545 const Value *Vec = Op->getOperand(0);
6546
6547 APInt DemandedVecElts;
6548 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
6549 unsigned NumElts = VecTy->getNumElements();
6550 DemandedVecElts = APInt::getAllOnes(NumElts);
6551 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(1));
6552 if (CIdx && CIdx->getValue().ult(NumElts))
6553 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
6554 } else {
6555 DemandedVecElts = APInt(1, 1);
6556 }
6557
6558 return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,
6559 Q, Depth + 1);
6560 }
6561 case Instruction::InsertElement: {
6562 if (isa<ScalableVectorType>(Op->getType()))
6563 return;
6564
6565 const Value *Vec = Op->getOperand(0);
6566 const Value *Elt = Op->getOperand(1);
6567 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(2));
6568 unsigned NumElts = DemandedElts.getBitWidth();
6569 APInt DemandedVecElts = DemandedElts;
6570 bool NeedsElt = true;
6571 // If we know the index we are inserting to, clear it from Vec check.
6572 if (CIdx && CIdx->getValue().ult(NumElts)) {
6573 DemandedVecElts.clearBit(CIdx->getZExtValue());
6574 NeedsElt = DemandedElts[CIdx->getZExtValue()];
6575 }
6576
6577 // Do we demand the inserted element?
6578 if (NeedsElt) {
6579 computeKnownFPClass(Elt, Known, InterestedClasses, Q, Depth + 1);
6580 // If we don't know any bits, early out.
6581 if (Known.isUnknown())
6582 break;
6583 } else {
6585 }
6586
6587 // Do we need anymore elements from Vec?
6588 if (!DemandedVecElts.isZero()) {
6589 KnownFPClass Known2;
6590 computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2, Q,
6591 Depth + 1);
6592 Known |= Known2;
6593 }
6594
6595 break;
6596 }
6597 case Instruction::ShuffleVector: {
6598 // Handle vector splat idiom
6599 if (Value *Splat = getSplatValue(V)) {
6600 computeKnownFPClass(Splat, Known, InterestedClasses, Q, Depth + 1);
6601 break;
6602 }
6603
6604 // For undef elements, we don't know anything about the common state of
6605 // the shuffle result.
6606 APInt DemandedLHS, DemandedRHS;
6607 auto *Shuf = dyn_cast<ShuffleVectorInst>(Op);
6608 if (!Shuf || !getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
6609 return;
6610
6611 if (!!DemandedLHS) {
6612 const Value *LHS = Shuf->getOperand(0);
6613 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known, Q,
6614 Depth + 1);
6615
6616 // If we don't know any bits, early out.
6617 if (Known.isUnknown())
6618 break;
6619 } else {
6621 }
6622
6623 if (!!DemandedRHS) {
6624 KnownFPClass Known2;
6625 const Value *RHS = Shuf->getOperand(1);
6626 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2, Q,
6627 Depth + 1);
6628 Known |= Known2;
6629 }
6630
6631 break;
6632 }
6633 case Instruction::ExtractValue: {
6634 const ExtractValueInst *Extract = cast<ExtractValueInst>(Op);
6635 ArrayRef<unsigned> Indices = Extract->getIndices();
6636 const Value *Src = Extract->getAggregateOperand();
6637 if (isa<StructType>(Src->getType()) && Indices.size() == 1 &&
6638 Indices[0] == 0) {
6639 if (const auto *II = dyn_cast<IntrinsicInst>(Src)) {
6640 switch (II->getIntrinsicID()) {
6641 case Intrinsic::frexp: {
6642 FPClassTest InterestedSrcs = InterestedClasses;
6643
6644 // Positive subnormals and negative subnormals could become positive
6645 // zero.
6646 if (InterestedClasses & fcPosZero)
6647 InterestedSrcs |= fcSubnormal;
6648
6649 // Negative subnormals could become negative zero.
6650 if (InterestedClasses & fcNegZero)
6651 InterestedSrcs |= fcNegSubnormal;
6652
6653 if (InterestedClasses & fcPosNormal)
6654 InterestedSrcs |= fcPosSubnormal;
6655
6656 if (InterestedClasses & fcNegNormal)
6657 InterestedSrcs |= fcNegSubnormal;
6658
6659 KnownFPClass KnownSrc;
6660 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
6661 InterestedSrcs, KnownSrc, Q, Depth + 1);
6662
6663 const Function *F = cast<Instruction>(Op)->getFunction();
6664 const fltSemantics &FltSem =
6665 Op->getType()->getScalarType()->getFltSemantics();
6666
6668 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
6669 Known = KnownFPClass::frexp_mant(KnownSrc, Mode);
6670 return;
6671 }
6672 default:
6673 break;
6674 }
6675 }
6676 }
6677
6678 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Q,
6679 Depth + 1);
6680 break;
6681 }
6682 case Instruction::PHI: {
6683 const PHINode *P = cast<PHINode>(Op);
6684 // Unreachable blocks may have zero-operand PHI nodes.
6685 if (P->getNumIncomingValues() == 0)
6686 break;
6687
6688 // Otherwise take the unions of the known bit sets of the operands,
6689 // taking conservative care to avoid excessive recursion.
6690 const unsigned PhiRecursionLimit = MaxAnalysisRecursionDepth - 2;
6691
6692 if (Depth < PhiRecursionLimit) {
6693 // Skip if every incoming value references to ourself.
6694 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
6695 break;
6696
6697 bool First = true;
6698
6699 for (const Use &U : P->operands()) {
6700 Value *IncValue;
6701 Instruction *CtxI;
6702 breakSelfRecursivePHI(&U, P, IncValue, CtxI);
6703 // Skip direct self references.
6704 if (IncValue == P)
6705 continue;
6706
6707 KnownFPClass KnownSrc;
6708 // Recurse, but cap the recursion to two levels, because we don't want
6709 // to waste time spinning around in loops. We need at least depth 2 to
6710 // detect known sign bits.
6711 computeKnownFPClass(IncValue, DemandedElts, InterestedClasses, KnownSrc,
6713 PhiRecursionLimit);
6714
6715 if (First) {
6716 Known = KnownSrc;
6717 First = false;
6718 } else {
6719 Known |= KnownSrc;
6720 }
6721
6722 if (Known.getKnownFPClasses() == fcAllFlags)
6723 break;
6724 }
6725 }
6726
6727 // Look for the case of a for loop which has a positive
6728 // initial value and is incremented by a squared value.
6729 // This will propagate sign information out of such loops.
6730 if (P->getNumIncomingValues() != 2 || Known.cannotBeOrderedLessThanZero())
6731 break;
6732 for (unsigned I = 0; I < 2; I++) {
6733 Value *RecurValue = P->getIncomingValue(1 - I);
6735 if (!II)
6736 continue;
6737 Value *R, *L, *Init;
6738 PHINode *PN;
6740 PN == P) {
6741 switch (II->getIntrinsicID()) {
6742 case Intrinsic::fma:
6743 case Intrinsic::fmuladd: {
6744 KnownFPClass KnownStart;
6745 computeKnownFPClass(Init, DemandedElts, InterestedClasses, KnownStart,
6746 Q, Depth + 1);
6747 if (KnownStart.cannotBeOrderedLessThanZero() && L == R &&
6748 isGuaranteedNotToBeUndef(L, Q.AC, Q.CtxI, Q.DT, Depth + 1))
6750 break;
6751 }
6752 }
6753 }
6754 }
6755 break;
6756 }
6757 case Instruction::BitCast: {
6758 const Value *Src;
6759 if (!match(Op, m_ElementWiseBitCast(m_Value(Src))) ||
6760 !Src->getType()->isIntOrIntVectorTy())
6761 break;
6762
6763 const Type *Ty = Op->getType();
6764
6765 Value *CastLHS, *CastRHS;
6766
6767 // Match bitcast(umax(bitcast(a), bitcast(b)))
6768 if (match(Src, m_c_MaxOrMin(m_BitCast(m_Value(CastLHS)),
6769 m_BitCast(m_Value(CastRHS)))) &&
6770 CastLHS->getType() == Ty && CastRHS->getType() == Ty) {
6771 KnownFPClass KnownLHS, KnownRHS;
6772 computeKnownFPClass(CastRHS, DemandedElts, InterestedClasses, KnownRHS, Q,
6773 Depth + 1);
6774 if (!KnownRHS.isUnknown()) {
6775 computeKnownFPClass(CastLHS, DemandedElts, InterestedClasses, KnownLHS,
6776 Q, Depth + 1);
6777 Known = KnownLHS | KnownRHS;
6778 }
6779
6780 return;
6781 }
6782
6783 const Type *EltTy = Ty->getScalarType();
6784 KnownBits Bits(EltTy->getPrimitiveSizeInBits());
6785 computeKnownBits(Src, DemandedElts, Bits, Q, Depth + 1);
6786
6788 break;
6789 }
6790 default:
6791 break;
6792 }
6793}
6794
6796 const APInt &DemandedElts,
6797 FPClassTest InterestedClasses,
6798 const SimplifyQuery &SQ,
6799 unsigned Depth) {
6800 KnownFPClass KnownClasses;
6801 ::computeKnownFPClass(V, DemandedElts, InterestedClasses, KnownClasses, SQ,
6802 Depth);
6803 return KnownClasses;
6804}
6805
6807 FPClassTest InterestedClasses,
6808 const SimplifyQuery &SQ,
6809 unsigned Depth) {
6811 ::computeKnownFPClass(V, Known, InterestedClasses, SQ, Depth);
6812 return Known;
6813}
6814
6816 const Value *V, const DataLayout &DL, FPClassTest InterestedClasses,
6817 const TargetLibraryInfo *TLI, AssumptionCache *AC, const Instruction *CtxI,
6818 const DominatorTree *DT, bool UseInstrInfo, unsigned Depth) {
6819 return computeKnownFPClass(V, InterestedClasses,
6820 SimplifyQuery(DL, TLI, DT, AC, CtxI, UseInstrInfo),
6821 Depth);
6822}
6823
6825llvm::computeKnownFPClass(const Value *V, const APInt &DemandedElts,
6826 FastMathFlags FMF, FPClassTest InterestedClasses,
6827 const SimplifyQuery &SQ, unsigned Depth) {
6828 if (FMF.noNaNs())
6829 InterestedClasses &= ~fcNan;
6830 if (FMF.noInfs())
6831 InterestedClasses &= ~fcInf;
6832
6833 KnownFPClass Result =
6834 computeKnownFPClass(V, DemandedElts, InterestedClasses, SQ, Depth);
6835
6836 if (FMF.noNaNs())
6837 Result.setKnownFPClasses(Result.getKnownFPClasses() & ~fcNan);
6838 if (FMF.noInfs())
6839 Result.setKnownFPClasses(Result.getKnownFPClasses() & ~fcInf);
6840 return Result;
6841}
6842
6844 FPClassTest InterestedClasses,
6845 const SimplifyQuery &SQ,
6846 unsigned Depth) {
6847 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
6848 APInt DemandedElts =
6849 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
6850 return computeKnownFPClass(V, DemandedElts, FMF, InterestedClasses, SQ,
6851 Depth);
6852}
6853
6855 unsigned Depth) {
6857 return Known.isKnownNever(fcNegZero);
6858}
6859
6861 unsigned Depth) {
6864 return Known.cannotBeOrderedLessThanZero();
6865}
6866
6868 unsigned Depth) {
6870 return Known.isKnownNeverInfinity();
6871}
6872
6873/// Return true if the floating-point value can never contain a NaN or infinity.
6875 unsigned Depth) {
6877 return Known.isKnownNeverNaN() && Known.isKnownNeverInfinity();
6878}
6879
6880/// Return true if the floating-point scalar value is not a NaN or if the
6881/// floating-point vector value has no NaN elements. Return false if a value
6882/// could ever be NaN.
6884 unsigned Depth) {
6886 return Known.isKnownNeverNaN();
6887}
6888
6889/// Return false if we can prove that the specified FP value's sign bit is 0.
6890/// Return true if we can prove that the specified FP value's sign bit is 1.
6891/// Otherwise return std::nullopt.
6892std::optional<bool> llvm::computeKnownFPSignBit(const Value *V,
6893 const SimplifyQuery &SQ,
6894 unsigned Depth) {
6896 return Known.getSignBit();
6897}
6898
6900 auto *User = cast<Instruction>(U.getUser());
6901 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6902 if (FPOp->hasNoSignedZeros())
6903 return true;
6904 }
6905
6906 switch (User->getOpcode()) {
6907 case Instruction::FPToSI:
6908 case Instruction::FPToUI:
6909 return true;
6910 case Instruction::FRem:
6911 // frem(x, +-0.0) == NaN, and more generally frem(x, y) == frem(x, -y).
6912 return U.getOperandNo() == 1;
6913 case Instruction::FCmp:
6914 // fcmp treats both positive and negative zero as equal.
6915 return true;
6916 case Instruction::Call:
6917 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6918 switch (II->getIntrinsicID()) {
6919 case Intrinsic::fabs:
6920 case Intrinsic::exp:
6921 case Intrinsic::exp2:
6922 case Intrinsic::exp10:
6923 case Intrinsic::log:
6924 case Intrinsic::log2:
6925 case Intrinsic::log10:
6926 case Intrinsic::cos:
6927 case Intrinsic::acos:
6928 case Intrinsic::cosh:
6929 case Intrinsic::fptosi_sat:
6930 case Intrinsic::fptoui_sat:
6931 case Intrinsic::lround:
6932 case Intrinsic::llround:
6933 case Intrinsic::lrint:
6934 case Intrinsic::llrint:
6935 return true;
6936 case Intrinsic::copysign:
6937 return U.getOperandNo() == 0;
6938 case Intrinsic::pow:
6939 // pow(x, +-0.0) == +1.0 or NaN
6940 return U.getOperandNo() == 1;
6941 case Intrinsic::is_fpclass: {
6942 auto Test =
6943 static_cast<FPClassTest>(
6944 cast<ConstantInt>(II->getArgOperand(1))->getZExtValue()) &
6947 }
6948 default:
6949 return false;
6950 }
6951 }
6952 return false;
6953 default:
6954 return false;
6955 }
6956}
6957
6959 auto *User = cast<Instruction>(U.getUser());
6960 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6961 if (FPOp->hasNoNaNs())
6962 return true;
6963 }
6964
6965 switch (User->getOpcode()) {
6966 case Instruction::FPToSI:
6967 case Instruction::FPToUI:
6968 return true;
6969 // Proper FP math operations ignore the sign bit of NaN.
6970 case Instruction::FAdd:
6971 case Instruction::FSub:
6972 case Instruction::FMul:
6973 case Instruction::FDiv:
6974 case Instruction::FRem:
6975 case Instruction::FPTrunc:
6976 case Instruction::FPExt:
6977 case Instruction::FCmp:
6978 return true;
6979 // Bitwise FP operations should preserve the sign bit of NaN.
6980 case Instruction::FNeg:
6981 case Instruction::Select:
6982 case Instruction::PHI:
6983 return false;
6984 case Instruction::Ret:
6985 return User->getFunction()->getAttributes().getRetNoFPClass() &
6987 case Instruction::Call:
6988 case Instruction::Invoke: {
6989 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6990 switch (II->getIntrinsicID()) {
6991 case Intrinsic::fabs:
6992 return true;
6993 case Intrinsic::copysign:
6994 return U.getOperandNo() == 0;
6995 // Other proper FP math intrinsics ignore the sign bit of NaN.
6996 case Intrinsic::maxnum:
6997 case Intrinsic::minnum:
6998 case Intrinsic::maximum:
6999 case Intrinsic::minimum:
7000 case Intrinsic::maximumnum:
7001 case Intrinsic::minimumnum:
7002 case Intrinsic::canonicalize:
7003 case Intrinsic::fma:
7004 case Intrinsic::fmuladd:
7005 case Intrinsic::sqrt:
7006 case Intrinsic::pow:
7007 case Intrinsic::powi:
7008 case Intrinsic::fptoui_sat:
7009 case Intrinsic::fptosi_sat:
7010 case Intrinsic::is_fpclass:
7011 return true;
7012 default:
7013 return false;
7014 }
7015 }
7016
7017 FPClassTest NoFPClass =
7018 cast<CallBase>(User)->getParamNoFPClass(U.getOperandNo());
7019 return NoFPClass & FPClassTest::fcNan;
7020 }
7021 default:
7022 return false;
7023 }
7024}
7025
7027 FastMathFlags FMF) {
7028 if (isa<PoisonValue>(V))
7029 return true;
7030 if (isa<UndefValue>(V))
7031 return false;
7032
7033 if (match(V, m_CheckedFp([](const APFloat &Val) { return Val.isInteger(); })))
7034 return true;
7035
7037 if (!I)
7038 return false;
7039
7040 switch (I->getOpcode()) {
7041 case Instruction::SIToFP:
7042 case Instruction::UIToFP:
7043 // TODO: Could check nofpclass(inf) on incoming argument
7044 if (FMF.noInfs())
7045 return true;
7046
7047 // Need to check int size cannot produce infinity, which computeKnownFPClass
7048 // knows how to do already.
7049 return isKnownNeverInfinity(I, SQ);
7050 case Instruction::Call: {
7051 const CallInst *CI = cast<CallInst>(I);
7052 switch (CI->getIntrinsicID()) {
7053 case Intrinsic::trunc:
7054 case Intrinsic::floor:
7055 case Intrinsic::ceil:
7056 case Intrinsic::rint:
7057 case Intrinsic::nearbyint:
7058 case Intrinsic::round:
7059 case Intrinsic::roundeven:
7060 return (FMF.noInfs() && FMF.noNaNs()) || isKnownNeverInfOrNaN(I, SQ);
7061 default:
7062 break;
7063 }
7064
7065 break;
7066 }
7067 default:
7068 break;
7069 }
7070
7071 return false;
7072}
7073
7075
7076 // All byte-wide stores are splatable, even of arbitrary variables.
7077 if (V->getType()->isIntegerTy(8))
7078 return V;
7079
7080 LLVMContext &Ctx = V->getContext();
7081
7082 // Undef don't care.
7083 auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
7084 if (isa<UndefValue>(V))
7085 return UndefInt8;
7086
7087 // Return poison for zero-sized type.
7088 if (DL.getTypeStoreSize(V->getType()).isZero())
7089 return PoisonValue::get(Type::getInt8Ty(Ctx));
7090
7092 if (!C) {
7093 // Conceptually, we could handle things like:
7094 // %a = zext i8 %X to i16
7095 // %b = shl i16 %a, 8
7096 // %c = or i16 %a, %b
7097 // but until there is an example that actually needs this, it doesn't seem
7098 // worth worrying about.
7099 return nullptr;
7100 }
7101
7102 // Handle 'null' ConstantArrayZero etc.
7103 if (C->isNullValue())
7105
7106 // Constant floating-point values can be handled as integer values if the
7107 // corresponding integer value is "byteable". An important case is 0.0.
7108 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
7109 Type *ScalarTy = CFP->getType()->getScalarType();
7110 if (ScalarTy->isHalfTy() || ScalarTy->isFloatTy() || ScalarTy->isDoubleTy())
7111 return isBytewiseValue(
7112 ConstantInt::get(Ctx, CFP->getValue().bitcastToAPInt()), DL);
7113
7114 // Don't handle long double formats, which have strange constraints.
7115 return nullptr;
7116 }
7117
7118 // We can handle constant integers that are multiple of 8 bits.
7119 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
7120 if (CI->getBitWidth() % 8 == 0) {
7121 if (!CI->getValue().isSplat(8))
7122 return nullptr;
7123 return ConstantInt::get(Ctx, CI->getValue().trunc(8));
7124 }
7125 }
7126
7127 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
7128 if (CE->getOpcode() == Instruction::IntToPtr) {
7129 if (auto *PtrTy = dyn_cast<PointerType>(CE->getType())) {
7130 unsigned BitWidth = DL.getPointerSizeInBits(PtrTy->getAddressSpace());
7132 CE->getOperand(0), Type::getIntNTy(Ctx, BitWidth), false, DL))
7133 return isBytewiseValue(Op, DL);
7134 }
7135 }
7136 }
7137
7138 auto Merge = [&](Value *LHS, Value *RHS) -> Value * {
7139 if (LHS == RHS)
7140 return LHS;
7141 if (!LHS || !RHS)
7142 return nullptr;
7143 if (LHS == UndefInt8)
7144 return RHS;
7145 if (RHS == UndefInt8)
7146 return LHS;
7147 return nullptr;
7148 };
7149
7151 Value *Val = UndefInt8;
7152 for (uint64_t I = 0, E = CA->getNumElements(); I != E; ++I)
7153 if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))
7154 return nullptr;
7155 return Val;
7156 }
7157
7159 Value *Val = UndefInt8;
7160 for (Value *Op : C->operands())
7161 if (!(Val = Merge(Val, isBytewiseValue(Op, DL))))
7162 return nullptr;
7163 return Val;
7164 }
7165
7166 // Don't try to handle the handful of other constants.
7167 return nullptr;
7168}
7169
7170// This is the recursive version of BuildSubAggregate. It takes a few different
7171// arguments. Idxs is the index within the nested struct From that we are
7172// looking at now (which is of type IndexedType). IdxSkip is the number of
7173// indices from Idxs that should be left out when inserting into the resulting
7174// struct. To is the result struct built so far, new insertvalue instructions
7175// build on that.
7176static Value *BuildSubAggregate(Value *From, Value *To, Type *IndexedType,
7178 unsigned IdxSkip,
7179 BasicBlock::iterator InsertBefore) {
7180 StructType *STy = dyn_cast<StructType>(IndexedType);
7181 if (STy) {
7182 // Save the original To argument so we can modify it
7183 Value *OrigTo = To;
7184 // General case, the type indexed by Idxs is a struct
7185 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
7186 // Process each struct element recursively
7187 Idxs.push_back(i);
7188 Value *PrevTo = To;
7189 To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
7190 InsertBefore);
7191 Idxs.pop_back();
7192 if (!To) {
7193 // Couldn't find any inserted value for this index? Cleanup
7194 while (PrevTo != OrigTo) {
7196 PrevTo = Del->getAggregateOperand();
7197 Del->eraseFromParent();
7198 }
7199 // Stop processing elements
7200 break;
7201 }
7202 }
7203 // If we successfully found a value for each of our subaggregates
7204 if (To)
7205 return To;
7206 }
7207 // Base case, the type indexed by SourceIdxs is not a struct, or not all of
7208 // the struct's elements had a value that was inserted directly. In the latter
7209 // case, perhaps we can't determine each of the subelements individually, but
7210 // we might be able to find the complete struct somewhere.
7211
7212 // Find the value that is at that particular spot
7213 Value *V = FindInsertedValue(From, Idxs);
7214
7215 if (!V)
7216 return nullptr;
7217
7218 // Insert the value in the new (sub) aggregate
7219 return InsertValueInst::Create(To, V, ArrayRef(Idxs).slice(IdxSkip), "tmp",
7220 InsertBefore);
7221}
7222
7223// This helper takes a nested struct and extracts a part of it (which is again a
7224// struct) into a new value. For example, given the struct:
7225// { a, { b, { c, d }, e } }
7226// and the indices "1, 1" this returns
7227// { c, d }.
7228//
7229// It does this by inserting an insertvalue for each element in the resulting
7230// struct, as opposed to just inserting a single struct. This will only work if
7231// each of the elements of the substruct are known (ie, inserted into From by an
7232// insertvalue instruction somewhere).
7233//
7234// All inserted insertvalue instructions are inserted before InsertBefore
7236 BasicBlock::iterator InsertBefore) {
7237 Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
7238 idx_range);
7239 Value *To = PoisonValue::get(IndexedType);
7240 SmallVector<unsigned, 10> Idxs(idx_range);
7241 unsigned IdxSkip = Idxs.size();
7242
7243 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
7244}
7245
7246/// Given an aggregate and a sequence of indices, see if the scalar value
7247/// indexed is already around as a register, for example if it was inserted
7248/// directly into the aggregate.
7249///
7250/// If InsertBefore is not null, this function will duplicate (modified)
7251/// insertvalues when a part of a nested struct is extracted.
7252Value *
7254 std::optional<BasicBlock::iterator> InsertBefore) {
7255 // Nothing to index? Just return V then (this is useful at the end of our
7256 // recursion).
7257 if (idx_range.empty())
7258 return V;
7259 // We have indices, so V should have an indexable type.
7260 assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
7261 "Not looking at a struct or array?");
7262 assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
7263 "Invalid indices for type?");
7264
7265 if (Constant *C = dyn_cast<Constant>(V)) {
7266 C = C->getAggregateElement(idx_range[0]);
7267 if (!C) return nullptr;
7268 return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
7269 }
7270
7272 // Loop the indices for the insertvalue instruction in parallel with the
7273 // requested indices
7274 const unsigned *req_idx = idx_range.begin();
7275 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
7276 i != e; ++i, ++req_idx) {
7277 if (req_idx == idx_range.end()) {
7278 // We can't handle this without inserting insertvalues
7279 if (!InsertBefore)
7280 return nullptr;
7281
7282 // The requested index identifies a part of a nested aggregate. Handle
7283 // this specially. For example,
7284 // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
7285 // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
7286 // %C = extractvalue {i32, { i32, i32 } } %B, 1
7287 // This can be changed into
7288 // %A = insertvalue {i32, i32 } undef, i32 10, 0
7289 // %C = insertvalue {i32, i32 } %A, i32 11, 1
7290 // which allows the unused 0,0 element from the nested struct to be
7291 // removed.
7292 return BuildSubAggregate(V, ArrayRef(idx_range.begin(), req_idx),
7293 *InsertBefore);
7294 }
7295
7296 // This insert value inserts something else than what we are looking for.
7297 // See if the (aggregate) value inserted into has the value we are
7298 // looking for, then.
7299 if (*req_idx != *i)
7300 return FindInsertedValue(I->getAggregateOperand(), idx_range,
7301 InsertBefore);
7302 }
7303 // If we end up here, the indices of the insertvalue match with those
7304 // requested (though possibly only partially). Now we recursively look at
7305 // the inserted value, passing any remaining indices.
7306 return FindInsertedValue(I->getInsertedValueOperand(),
7307 ArrayRef(req_idx, idx_range.end()), InsertBefore);
7308 }
7309
7311 // If we're extracting a value from an aggregate that was extracted from
7312 // something else, we can extract from that something else directly instead.
7313 // However, we will need to chain I's indices with the requested indices.
7314
7315 // Calculate the number of indices required
7316 unsigned size = I->getNumIndices() + idx_range.size();
7317 // Allocate some space to put the new indices in
7319 Idxs.reserve(size);
7320 // Add indices from the extract value instruction
7321 Idxs.append(I->idx_begin(), I->idx_end());
7322
7323 // Add requested indices
7324 Idxs.append(idx_range.begin(), idx_range.end());
7325
7326 assert(Idxs.size() == size
7327 && "Number of indices added not correct?");
7328
7329 return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
7330 }
7331 // Otherwise, we don't know (such as, extracting from a function return value
7332 // or load instruction)
7333 return nullptr;
7334}
7335
7336// If V refers to an initialized global constant, set Slice either to
7337// its initializer if the size of its elements equals ElementSize, or,
7338// for ElementSize == 8, to its representation as an array of unsiged
7339// char. Return true on success.
7340// Offset is in the unit "nr of ElementSize sized elements".
7343 unsigned ElementSize, uint64_t Offset) {
7344 assert(V && "V should not be null.");
7345 assert((ElementSize % 8) == 0 &&
7346 "ElementSize expected to be a multiple of the size of a byte.");
7347 unsigned ElementSizeInBytes = ElementSize / 8;
7348
7349 // Drill down into the pointer expression V, ignoring any intervening
7350 // casts, and determine the identity of the object it references along
7351 // with the cumulative byte offset into it.
7352 const GlobalVariable *GV =
7354 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
7355 // Fail if V is not based on constant global object.
7356 return false;
7357
7358 const DataLayout &DL = GV->getDataLayout();
7359 APInt Off(DL.getIndexTypeSizeInBits(V->getType()), 0);
7360
7362 /*AllowNonInbounds*/ true))
7363 // Fail if a constant offset could not be determined.
7364 return false;
7365
7366 uint64_t StartIdx = Off.getLimitedValue();
7367 if (StartIdx == UINT64_MAX)
7368 // Fail if the constant offset is excessive.
7369 return false;
7370
7371 // Off/StartIdx is in the unit of bytes. So we need to convert to number of
7372 // elements. Simply bail out if that isn't possible.
7373 if ((StartIdx % ElementSizeInBytes) != 0)
7374 return false;
7375
7376 Offset += StartIdx / ElementSizeInBytes;
7377 ConstantDataArray *Array = nullptr;
7378 ArrayType *ArrayTy = nullptr;
7379
7380 if (GV->getInitializer()->isNullValue()) {
7381 Type *GVTy = GV->getValueType();
7382 uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy).getFixedValue();
7383 uint64_t Length = SizeInBytes / ElementSizeInBytes;
7384
7385 Slice.Array = nullptr;
7386 Slice.Offset = 0;
7387 // Return an empty Slice for undersized constants to let callers
7388 // transform even undefined library calls into simpler, well-defined
7389 // expressions. This is preferable to making the calls although it
7390 // prevents sanitizers from detecting such calls.
7391 Slice.Length = Length < Offset ? 0 : Length - Offset;
7392 return true;
7393 }
7394
7395 auto *Init = const_cast<Constant *>(GV->getInitializer());
7396 if (auto *ArrayInit = dyn_cast<ConstantDataArray>(Init)) {
7397 Type *InitElTy = ArrayInit->getElementType();
7398 if (InitElTy->isIntegerTy(ElementSize)) {
7399 // If Init is an initializer for an array of the expected type
7400 // and size, use it as is.
7401 Array = ArrayInit;
7402 ArrayTy = ArrayInit->getType();
7403 }
7404 }
7405
7406 if (!Array) {
7407 if (ElementSize != 8)
7408 // TODO: Handle conversions to larger integral types.
7409 return false;
7410
7411 // Otherwise extract the portion of the initializer starting
7412 // at Offset as an array of bytes, and reset Offset.
7414 if (!Init)
7415 return false;
7416
7417 Offset = 0;
7419 ArrayTy = dyn_cast<ArrayType>(Init->getType());
7420 }
7421
7422 uint64_t NumElts = ArrayTy->getArrayNumElements();
7423 if (Offset > NumElts)
7424 return false;
7425
7426 Slice.Array = Array;
7427 Slice.Offset = Offset;
7428 Slice.Length = NumElts - Offset;
7429 return true;
7430}
7431
7432/// Extract bytes from the initializer of the constant array V, which need
7433/// not be a nul-terminated string. On success, store the bytes in Str and
7434/// return true. When TrimAtNul is set, Str will contain only the bytes up
7435/// to but not including the first nul. Return false on failure.
7437 bool TrimAtNul) {
7439 if (!getConstantDataArrayInfo(V, Slice, 8))
7440 return false;
7441
7442 if (Slice.Array == nullptr) {
7443 if (TrimAtNul) {
7444 // Return a nul-terminated string even for an empty Slice. This is
7445 // safe because all existing SimplifyLibcalls callers require string
7446 // arguments and the behavior of the functions they fold is undefined
7447 // otherwise. Folding the calls this way is preferable to making
7448 // the undefined library calls, even though it prevents sanitizers
7449 // from reporting such calls.
7450 Str = StringRef();
7451 return true;
7452 }
7453 if (Slice.Length == 1) {
7454 Str = StringRef("", 1);
7455 return true;
7456 }
7457 // We cannot instantiate a StringRef as we do not have an appropriate string
7458 // of 0s at hand.
7459 return false;
7460 }
7461
7462 // Start out with the entire array in the StringRef.
7463 Str = Slice.Array->getAsString();
7464 // Skip over 'offset' bytes.
7465 Str = Str.substr(Slice.Offset);
7466
7467 if (TrimAtNul) {
7468 // Trim off the \0 and anything after it. If the array is not nul
7469 // terminated, we just return the whole end of string. The client may know
7470 // some other way that the string is length-bound.
7471 Str = Str.substr(0, Str.find('\0'));
7472 }
7473 return true;
7474}
7475
7476// These next two are very similar to the above, but also look through PHI
7477// nodes.
7478// TODO: See if we can integrate these two together.
7479
7480/// If we can compute the length of the string pointed to by
7481/// the specified pointer, return 'len+1'. If we can't, return 0.
7484 unsigned CharSize) {
7485 // Look through noop bitcast instructions.
7486 V = V->stripPointerCasts();
7487
7488 // If this is a PHI node, there are two cases: either we have already seen it
7489 // or we haven't.
7490 if (const PHINode *PN = dyn_cast<PHINode>(V)) {
7491 if (!PHIs.insert(PN).second)
7492 return ~0ULL; // already in the set.
7493
7494 // If it was new, see if all the input strings are the same length.
7495 uint64_t LenSoFar = ~0ULL;
7496 for (Value *IncValue : PN->incoming_values()) {
7497 uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize);
7498 if (Len == 0) return 0; // Unknown length -> unknown.
7499
7500 if (Len == ~0ULL) continue;
7501
7502 if (Len != LenSoFar && LenSoFar != ~0ULL)
7503 return 0; // Disagree -> unknown.
7504 LenSoFar = Len;
7505 }
7506
7507 // Success, all agree.
7508 return LenSoFar;
7509 }
7510
7511 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
7512 if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
7513 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);
7514 if (Len1 == 0) return 0;
7515 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);
7516 if (Len2 == 0) return 0;
7517 if (Len1 == ~0ULL) return Len2;
7518 if (Len2 == ~0ULL) return Len1;
7519 if (Len1 != Len2) return 0;
7520 return Len1;
7521 }
7522
7523 // Otherwise, see if we can read the string.
7525 if (!getConstantDataArrayInfo(V, Slice, CharSize))
7526 return 0;
7527
7528 if (Slice.Array == nullptr)
7529 // Zeroinitializer (including an empty one).
7530 return 1;
7531
7532 // Search for the first nul character. Return a conservative result even
7533 // when there is no nul. This is safe since otherwise the string function
7534 // being folded such as strlen is undefined, and can be preferable to
7535 // making the undefined library call.
7536 unsigned NullIndex = 0;
7537 for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) {
7538 if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0)
7539 break;
7540 }
7541
7542 return NullIndex + 1;
7543}
7544
7545/// If we can compute the length of the string pointed to by
7546/// the specified pointer, return 'len+1'. If we can't, return 0.
7547uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) {
7548 if (!V->getType()->isPointerTy())
7549 return 0;
7550
7552 uint64_t Len = GetStringLengthH(V, PHIs, CharSize);
7553 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
7554 // an empty string as a length.
7555 return Len == ~0ULL ? 1 : Len;
7556}
7557
7558const Value *
7560 bool MustPreserveOffset,
7561 bool MustPreserveProvenance) {
7562 assert(Call &&
7563 "getArgumentAliasingToReturnedPointer only works on nonnull calls");
7564 if (const Value *RV = Call->getReturnedArgOperand())
7565 return RV;
7566 // This can be used only as a aliasing property.
7568 Call, MustPreserveOffset, MustPreserveProvenance))
7569 return Call->getArgOperand(0);
7570 return nullptr;
7571}
7572
7574 const CallBase *Call, bool MustPreserveOffset,
7575 bool MustPreserveProvenance) {
7576 switch (Call->getIntrinsicID()) {
7577 case Intrinsic::launder_invariant_group:
7578 case Intrinsic::aarch64_irg:
7579 case Intrinsic::aarch64_tagp:
7580 // The amdgcn_make_buffer_rsrc function does not alter the address of the
7581 // input pointer (and thus preserves the byte offset, which is the property
7582 // the MustPreserveOffset flag selects). However, it will not necessarily
7583 // map ptr addrspace(N) null to ptr addrspace(8) null, aka the "null
7584 // descriptor", which has "all loads return 0, all stores are dropped"
7585 // semantics. Given the context of this intrinsic list, no one should be
7586 // relying on such a strict bit-exact null mapping (and, at time of
7587 // writing, they are not), but we document this fact out of an abundance
7588 // of caution.
7589 case Intrinsic::amdgcn_make_buffer_rsrc:
7590 return !MustPreserveProvenance;
7591 case Intrinsic::ptrmask:
7592 return !MustPreserveOffset;
7593 case Intrinsic::threadlocal_address:
7594 // The underlying variable changes with thread ID. The Thread ID may change
7595 // at coroutine suspend points.
7596 return !Call->getParent()->getParent()->isPresplitCoroutine();
7597 default:
7598 return false;
7599 }
7600}
7601
7602/// \p PN defines a loop-variant pointer to an object. Check if the
7603/// previous iteration of the loop was referring to the same object as \p PN.
7605 const LoopInfo *LI) {
7606 // Find the loop-defined value.
7607 Loop *L = LI->getLoopFor(PN->getParent());
7608 if (PN->getNumIncomingValues() != 2)
7609 return true;
7610
7611 // Find the value from previous iteration.
7612 auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
7613 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
7614 PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
7615 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
7616 return true;
7617
7618 // If a new pointer is loaded in the loop, the pointer references a different
7619 // object in every iteration. E.g.:
7620 // for (i)
7621 // int *p = a[i];
7622 // ...
7623 if (auto *Load = dyn_cast<LoadInst>(PrevValue))
7624 if (!L->isLoopInvariant(Load->getPointerOperand()))
7625 return false;
7626 return true;
7627}
7628
7629const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup,
7630 bool MustPreserveProvenance) {
7631 for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
7632 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
7633 const Value *PtrOp = GEP->getPointerOperand();
7634 if (!PtrOp->getType()->isPointerTy()) // Only handle scalar pointer base.
7635 return V;
7636 V = PtrOp;
7637 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
7638 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
7639 Value *NewV = cast<Operator>(V)->getOperand(0);
7640 if (!NewV->getType()->isPointerTy())
7641 return V;
7642 V = NewV;
7643 } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
7644 if (GA->isInterposable())
7645 return V;
7646 V = GA->getAliasee();
7647 } else {
7648 if (auto *PHI = dyn_cast<PHINode>(V)) {
7649 // Look through single-arg phi nodes created by LCSSA.
7650 if (PHI->getNumIncomingValues() == 1) {
7651 V = PHI->getIncomingValue(0);
7652 continue;
7653 }
7654 } else if (auto *Call = dyn_cast<CallBase>(V)) {
7655 // CaptureTracking can know about special capturing properties of some
7656 // intrinsics like launder.invariant.group, that can't be expressed with
7657 // the attributes, but have properties like returning aliasing pointer.
7658 // Because some analysis may assume that nocaptured pointer is not
7659 // returned from some special intrinsic (because function would have to
7660 // be marked with returns attribute), it is crucial to use this function
7661 // because it should be in sync with CaptureTracking. Not using it may
7662 // cause weird miscompilations where 2 aliasing pointers are assumed to
7663 // noalias.
7665 Call, /*MustPreserveOffset=*/false, MustPreserveProvenance)) {
7666 V = RP;
7667 continue;
7668 }
7669 }
7670
7671 return V;
7672 }
7673 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
7674 }
7675 return V;
7676}
7677
7680 const LoopInfo *LI, unsigned MaxLookup) {
7683 Worklist.push_back(V);
7684 do {
7685 const Value *P = Worklist.pop_back_val();
7686 P = getUnderlyingObject(P, MaxLookup);
7687
7688 if (!Visited.insert(P).second)
7689 continue;
7690
7691 if (auto *SI = dyn_cast<SelectInst>(P)) {
7692 Worklist.push_back(SI->getTrueValue());
7693 Worklist.push_back(SI->getFalseValue());
7694 continue;
7695 }
7696
7697 if (auto *PN = dyn_cast<PHINode>(P)) {
7698 // If this PHI changes the underlying object in every iteration of the
7699 // loop, don't look through it. Consider:
7700 // int **A;
7701 // for (i) {
7702 // Prev = Curr; // Prev = PHI (Prev_0, Curr)
7703 // Curr = A[i];
7704 // *Prev, *Curr;
7705 //
7706 // Prev is tracking Curr one iteration behind so they refer to different
7707 // underlying objects.
7708 if (!LI || !LI->isLoopHeader(PN->getParent()) ||
7710 append_range(Worklist, PN->incoming_values());
7711 else
7712 Objects.push_back(P);
7713 continue;
7714 }
7715
7716 Objects.push_back(P);
7717 } while (!Worklist.empty());
7718}
7719
7721 bool MustPreserveProvenance) {
7722 const unsigned MaxVisited = 8;
7723
7726 Worklist.push_back(V);
7727 const Value *Object = nullptr;
7728 // Used as fallback if we can't find a common underlying object through
7729 // recursion.
7730 bool First = true;
7731 const Value *FirstObject =
7732 getUnderlyingObject(V, MaxLookupSearchDepth, MustPreserveProvenance);
7733 do {
7734 const Value *P = Worklist.pop_back_val();
7735 P = First ? FirstObject
7737 MustPreserveProvenance);
7738 First = false;
7739
7740 if (!Visited.insert(P).second)
7741 continue;
7742
7743 if (Visited.size() == MaxVisited)
7744 return FirstObject;
7745
7746 if (auto *SI = dyn_cast<SelectInst>(P)) {
7747 Worklist.push_back(SI->getTrueValue());
7748 Worklist.push_back(SI->getFalseValue());
7749 continue;
7750 }
7751
7752 if (auto *PN = dyn_cast<PHINode>(P)) {
7753 append_range(Worklist, PN->incoming_values());
7754 continue;
7755 }
7756
7757 if (!Object)
7758 Object = P;
7759 else if (Object != P)
7760 return FirstObject;
7761 } while (!Worklist.empty());
7762
7763 return Object ? Object : FirstObject;
7764}
7765
7766/// This is the function that does the work of looking through basic
7767/// ptrtoint+arithmetic+inttoptr sequences.
7769 do {
7770 if (const Operator *U = dyn_cast<Operator>(V)) {
7771 // If we find a ptrtoint, we can transfer control back to the
7772 // regular getUnderlyingObjectFromInt.
7773 if (U->getOpcode() == Instruction::PtrToInt)
7774 return U->getOperand(0);
7775 // If we find an add of a constant, a multiplied value, or a phi, it's
7776 // likely that the other operand will lead us to the base
7777 // object. We don't have to worry about the case where the
7778 // object address is somehow being computed by the multiply,
7779 // because our callers only care when the result is an
7780 // identifiable object.
7781 if (U->getOpcode() != Instruction::Add ||
7782 (!isa<ConstantInt>(U->getOperand(1)) &&
7783 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
7784 !isa<PHINode>(U->getOperand(1))))
7785 return V;
7786 V = U->getOperand(0);
7787 } else {
7788 return V;
7789 }
7790 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
7791 } while (true);
7792}
7793
7794/// This is a wrapper around getUnderlyingObjects and adds support for basic
7795/// ptrtoint+arithmetic+inttoptr sequences.
7796/// It returns false if unidentified object is found in getUnderlyingObjects.
7798 SmallVectorImpl<Value *> &Objects) {
7800 SmallVector<const Value *, 4> Working(1, V);
7801 bool AllObjectsIdentified = true;
7802 do {
7803 V = Working.pop_back_val();
7804
7806 getUnderlyingObjects(V, Objs);
7807
7808 for (const Value *V : Objs) {
7809 if (!Visited.insert(V).second)
7810 continue;
7811 if (Operator::getOpcode(V) == Instruction::IntToPtr) {
7812 const Value *O =
7813 getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
7814 if (O->getType()->isPointerTy()) {
7815 Working.push_back(O);
7816 continue;
7817 }
7818 }
7819 AllObjectsIdentified &= isIdentifiedObject(V);
7820 Objects.push_back(const_cast<Value *>(V));
7821 }
7822 } while (!Working.empty());
7823 return AllObjectsIdentified;
7824}
7825
7827 AllocaInst *Result = nullptr;
7829 SmallVector<Value *, 4> Worklist;
7830
7831 auto AddWork = [&](Value *V) {
7832 if (Visited.insert(V).second)
7833 Worklist.push_back(V);
7834 };
7835
7836 AddWork(V);
7837 do {
7838 V = Worklist.pop_back_val();
7839 assert(Visited.count(V));
7840
7841 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
7842 if (Result && Result != AI)
7843 return nullptr;
7844 Result = AI;
7845 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
7846 AddWork(CI->getOperand(0));
7847 } else if (PHINode *PN = dyn_cast<PHINode>(V)) {
7848 for (Value *IncValue : PN->incoming_values())
7849 AddWork(IncValue);
7850 } else if (auto *SI = dyn_cast<SelectInst>(V)) {
7851 AddWork(SI->getTrueValue());
7852 AddWork(SI->getFalseValue());
7854 if (OffsetZero && !GEP->hasAllZeroIndices())
7855 return nullptr;
7856 AddWork(GEP->getPointerOperand());
7857 } else if (CallBase *CB = dyn_cast<CallBase>(V)) {
7858 Value *Returned = CB->getReturnedArgOperand();
7859 if (Returned)
7860 AddWork(Returned);
7861 else
7862 return nullptr;
7863 } else {
7864 return nullptr;
7865 }
7866 } while (!Worklist.empty());
7867
7868 return Result;
7869}
7870
7872 if (auto *II = dyn_cast<IntrinsicInst>(I))
7873 return isTriviallyVectorizable(II->getIntrinsicID());
7874 auto *Shuffle = dyn_cast<ShuffleVectorInst>(I);
7875 return (!Shuffle || Shuffle->isSelect()) &&
7877}
7878
7880 const Instruction *Inst, const Instruction *CtxI, AssumptionCache *AC,
7881 const DominatorTree *DT, const TargetLibraryInfo *TLI, bool UseVariableInfo,
7882 bool IgnoreUBImplyingAttrs) {
7883 return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI,
7884 AC, DT, TLI, UseVariableInfo,
7885 IgnoreUBImplyingAttrs);
7886}
7887
7889 unsigned Opcode, const Instruction *Inst, const Instruction *CtxI,
7890 AssumptionCache *AC, const DominatorTree *DT, const TargetLibraryInfo *TLI,
7891 bool UseVariableInfo, bool IgnoreUBImplyingAttrs) {
7892#ifndef NDEBUG
7893 if (Inst->getOpcode() != Opcode) {
7894 // Check that the operands are actually compatible with the Opcode override.
7895 auto hasEqualReturnAndLeadingOperandTypes =
7896 [](const Instruction *Inst, unsigned NumLeadingOperands) {
7897 if (Inst->getNumOperands() < NumLeadingOperands)
7898 return false;
7899 const Type *ExpectedType = Inst->getType();
7900 for (unsigned ItOp = 0; ItOp < NumLeadingOperands; ++ItOp)
7901 if (Inst->getOperand(ItOp)->getType() != ExpectedType)
7902 return false;
7903 return true;
7904 };
7906 hasEqualReturnAndLeadingOperandTypes(Inst, 2));
7907 assert(!Instruction::isUnaryOp(Opcode) ||
7908 hasEqualReturnAndLeadingOperandTypes(Inst, 1));
7909 }
7910#endif
7911
7912 switch (Opcode) {
7913 default:
7914 return true;
7915 case Instruction::UDiv:
7916 case Instruction::URem: {
7917 // x / y is undefined if y == 0.
7918 const APInt *V;
7919 if (match(Inst->getOperand(1), m_APInt(V)))
7920 return *V != 0;
7921 return false;
7922 }
7923 case Instruction::SDiv:
7924 case Instruction::SRem: {
7925 // x / y is undefined if y == 0 or x == INT_MIN and y == -1
7926 const APInt *Numerator, *Denominator;
7927 if (!match(Inst->getOperand(1), m_APInt(Denominator)))
7928 return false;
7929 // We cannot hoist this division if the denominator is 0.
7930 if (*Denominator == 0)
7931 return false;
7932 // It's safe to hoist if the denominator is not 0 or -1.
7933 if (!Denominator->isAllOnes())
7934 return true;
7935 // At this point we know that the denominator is -1. It is safe to hoist as
7936 // long we know that the numerator is not INT_MIN.
7937 if (match(Inst->getOperand(0), m_APInt(Numerator)))
7938 return !Numerator->isMinSignedValue();
7939 // The numerator *might* be MinSignedValue.
7940 return false;
7941 }
7942 case Instruction::Load: {
7943 if (!UseVariableInfo)
7944 return false;
7945
7946 const LoadInst *LI = dyn_cast<LoadInst>(Inst);
7947 if (!LI)
7948 return false;
7949 if (mustSuppressSpeculation(*LI))
7950 return false;
7951 const DataLayout &DL = LI->getDataLayout();
7953 LI->getPointerOperand(), LI->getType(), LI->getAlign(),
7954 SimplifyQuery(DL, TLI, DT, AC, CtxI));
7955 }
7956 case Instruction::Call: {
7957 auto *CI = dyn_cast<const CallInst>(Inst);
7958 if (!CI)
7959 return false;
7960 const Function *Callee = CI->getCalledFunction();
7961
7962 // The called function could have undefined behavior or side-effects, even
7963 // if marked readnone nounwind.
7964 if (!Callee || !Callee->isSpeculatable())
7965 return false;
7966 // Since the operands may be changed after hoisting, undefined behavior may
7967 // be triggered by some UB-implying attributes.
7968 return IgnoreUBImplyingAttrs || !CI->hasUBImplyingAttrs();
7969 }
7970 case Instruction::VAArg:
7971 case Instruction::Alloca:
7972 case Instruction::Invoke:
7973 case Instruction::CallBr:
7974 case Instruction::PHI:
7975 case Instruction::Store:
7976 case Instruction::Ret:
7977 case Instruction::UncondBr:
7978 case Instruction::CondBr:
7979 case Instruction::IndirectBr:
7980 case Instruction::Switch:
7981 case Instruction::Unreachable:
7982 case Instruction::Fence:
7983 case Instruction::AtomicRMW:
7984 case Instruction::AtomicCmpXchg:
7985 case Instruction::LandingPad:
7986 case Instruction::Resume:
7987 case Instruction::CatchSwitch:
7988 case Instruction::CatchPad:
7989 case Instruction::CatchRet:
7990 case Instruction::CleanupPad:
7991 case Instruction::CleanupRet:
7992 return false; // Misc instructions which have effects
7993 }
7994}
7995
7997 if (I.mayReadOrWriteMemory())
7998 // Memory dependency possible
7999 return true;
8001 // Can't move above a maythrow call or infinite loop. Or if an
8002 // inalloca alloca, above a stacksave call.
8003 return true;
8005 // 1) Can't reorder two inf-loop calls, even if readonly
8006 // 2) Also can't reorder an inf-loop call below a instruction which isn't
8007 // safe to speculative execute. (Inverse of above)
8008 return true;
8009 return false;
8010}
8011
8012/// Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
8026
8027/// Combine constant ranges from computeConstantRange() and computeKnownBits().
8030 bool ForSigned,
8031 const SimplifyQuery &SQ) {
8032 ConstantRange CR1 =
8033 ConstantRange::fromKnownBits(V.getKnownBits(SQ), ForSigned);
8034 ConstantRange CR2 = computeConstantRange(V, ForSigned, SQ);
8037 return CR1.intersectWith(CR2, RangeType);
8038}
8039
8041 const Value *RHS,
8042 const SimplifyQuery &SQ,
8043 bool IsNSW) {
8044 ConstantRange LHSRange =
8045 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
8046 ConstantRange RHSRange =
8047 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
8048
8049 // mul nsw of two non-negative numbers is also nuw.
8050 if (IsNSW && LHSRange.isAllNonNegative() && RHSRange.isAllNonNegative())
8052
8053 return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange));
8054}
8055
8057 const Value *RHS,
8058 const SimplifyQuery &SQ) {
8059 // Multiplying n * m significant bits yields a result of n + m significant
8060 // bits. If the total number of significant bits does not exceed the
8061 // result bit width (minus 1), there is no overflow.
8062 // This means if we have enough leading sign bits in the operands
8063 // we can guarantee that the result does not overflow.
8064 // Ref: "Hacker's Delight" by Henry Warren
8065 unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
8066
8067 // Note that underestimating the number of sign bits gives a more
8068 // conservative answer.
8069 unsigned SignBits =
8070 ::ComputeNumSignBits(LHS, SQ) + ::ComputeNumSignBits(RHS, SQ);
8071
8072 // First handle the easy case: if we have enough sign bits there's
8073 // definitely no overflow.
8074 if (SignBits > BitWidth + 1)
8076
8077 // There are two ambiguous cases where there can be no overflow:
8078 // SignBits == BitWidth + 1 and
8079 // SignBits == BitWidth
8080 // The second case is difficult to check, therefore we only handle the
8081 // first case.
8082 if (SignBits == BitWidth + 1) {
8083 // It overflows only when both arguments are negative and the true
8084 // product is exactly the minimum negative number.
8085 // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
8086 // For simplicity we just check if at least one side is not negative.
8087 KnownBits LHSKnown = computeKnownBits(LHS, SQ);
8088 KnownBits RHSKnown = computeKnownBits(RHS, SQ);
8089 if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative())
8091 }
8093}
8094
8097 const WithCache<const Value *> &RHS,
8098 const SimplifyQuery &SQ) {
8099 ConstantRange LHSRange =
8100 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
8101 ConstantRange RHSRange =
8102 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
8103 return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange));
8104}
8105
8106static OverflowResult
8109 const AddOperator *Add, const SimplifyQuery &SQ) {
8110 if (Add && Add->hasNoSignedWrap()) {
8112 }
8113
8114 // If LHS and RHS each have at least two sign bits, the addition will look
8115 // like
8116 //
8117 // XX..... +
8118 // YY.....
8119 //
8120 // If the carry into the most significant position is 0, X and Y can't both
8121 // be 1 and therefore the carry out of the addition is also 0.
8122 //
8123 // If the carry into the most significant position is 1, X and Y can't both
8124 // be 0 and therefore the carry out of the addition is also 1.
8125 //
8126 // Since the carry into the most significant position is always equal to
8127 // the carry out of the addition, there is no signed overflow.
8128 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
8130
8131 ConstantRange LHSRange =
8132 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
8133 ConstantRange RHSRange =
8134 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
8135 OverflowResult OR =
8136 mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange));
8138 return OR;
8139
8140 // The remaining code needs Add to be available. Early returns if not so.
8141 if (!Add)
8143
8144 // If the sign of Add is the same as at least one of the operands, this add
8145 // CANNOT overflow. If this can be determined from the known bits of the
8146 // operands the above signedAddMayOverflow() check will have already done so.
8147 // The only other way to improve on the known bits is from an assumption, so
8148 // call computeKnownBitsFromContext() directly.
8149 bool LHSOrRHSKnownNonNegative =
8150 (LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative());
8151 bool LHSOrRHSKnownNegative =
8152 (LHSRange.isAllNegative() || RHSRange.isAllNegative());
8153 if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
8154 KnownBits AddKnown(LHSRange.getBitWidth());
8155 computeKnownBitsFromContext(Add, AddKnown, SQ);
8156 if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||
8157 (AddKnown.isNegative() && LHSOrRHSKnownNegative))
8159 }
8160
8162}
8163
8165 const Value *RHS,
8166 const SimplifyQuery &SQ) {
8167 // X - (X % ?)
8168 // The remainder of a value can't have greater magnitude than itself,
8169 // so the subtraction can't overflow.
8170
8171 // X - (X -nuw ?)
8172 // In the minimal case, this would simplify to "?", so there's no subtract
8173 // at all. But if this analysis is used to peek through casts, for example,
8174 // then determining no-overflow may allow other transforms.
8175
8176 // TODO: There are other patterns like this.
8177 // See simplifyICmpWithBinOpOnLHS() for candidates.
8178 if (match(RHS, m_URem(m_Specific(LHS), m_Value())) ||
8179 match(RHS, m_NUWSub(m_Specific(LHS), m_Value())))
8180 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CtxI, SQ.DT))
8182
8183 if (auto C = isImpliedByDomCondition(CmpInst::ICMP_UGE, LHS, RHS, SQ.CtxI,
8184 SQ.DL)) {
8185 if (*C)
8188 }
8189
8190 ConstantRange LHSRange =
8191 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
8192 ConstantRange RHSRange =
8193 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
8194 return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange));
8195}
8196
8198 const Value *RHS,
8199 const SimplifyQuery &SQ) {
8200 // X - (X % ?)
8201 // The remainder of a value can't have greater magnitude than itself,
8202 // so the subtraction can't overflow.
8203
8204 // X - (X -nsw ?)
8205 // In the minimal case, this would simplify to "?", so there's no subtract
8206 // at all. But if this analysis is used to peek through casts, for example,
8207 // then determining no-overflow may allow other transforms.
8208 if (match(RHS, m_SRem(m_Specific(LHS), m_Value())) ||
8209 match(RHS, m_NSWSub(m_Specific(LHS), m_Value())))
8210 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CtxI, SQ.DT))
8212
8213 // If LHS and RHS each have at least two sign bits, the subtraction
8214 // cannot overflow.
8215 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
8217
8218 ConstantRange LHSRange =
8219 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
8220 ConstantRange RHSRange =
8221 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
8222 return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange));
8223}
8224
8226 const DominatorTree &DT) {
8227 SmallVector<const CondBrInst *, 2> GuardingBranches;
8229
8230 for (const User *U : WO->users()) {
8231 if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) {
8232 assert(EVI->getNumIndices() == 1 && "Obvious from CI's type");
8233
8234 if (EVI->getIndices()[0] == 0)
8235 Results.push_back(EVI);
8236 else {
8237 assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type");
8238
8239 for (const auto *U : EVI->users())
8240 if (const auto *B = dyn_cast<CondBrInst>(U))
8241 GuardingBranches.push_back(B);
8242 }
8243 } else {
8244 // We are using the aggregate directly in a way we don't want to analyze
8245 // here (storing it to a global, say).
8246 return false;
8247 }
8248 }
8249
8250 auto AllUsesGuardedByBranch = [&](const CondBrInst *BI) {
8251 BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1));
8252
8253 // Check if all users of the add are provably no-wrap.
8254 for (const auto *Result : Results) {
8255 // If the extractvalue itself is not executed on overflow, the we don't
8256 // need to check each use separately, since domination is transitive.
8257 if (DT.dominates(NoWrapEdge, Result->getParent()))
8258 continue;
8259
8260 for (const auto &RU : Result->uses())
8261 if (!DT.dominates(NoWrapEdge, RU))
8262 return false;
8263 }
8264
8265 return true;
8266 };
8267
8268 return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
8269}
8270
8271/// Shifts return poison if shiftwidth is larger than the bitwidth.
8272static bool shiftAmountKnownInRange(const Value *ShiftAmount) {
8273 auto *C = dyn_cast<Constant>(ShiftAmount);
8274 if (!C)
8275 return false;
8276
8277 // Shifts return poison if shiftwidth is larger than the bitwidth.
8279 if (auto *FVTy = dyn_cast<FixedVectorType>(C->getType())) {
8280 unsigned NumElts = FVTy->getNumElements();
8281 for (unsigned i = 0; i < NumElts; ++i)
8282 ShiftAmounts.push_back(C->getAggregateElement(i));
8283 } else if (isa<ScalableVectorType>(C->getType()))
8284 return false; // Can't tell, just return false to be safe
8285 else
8286 ShiftAmounts.push_back(C);
8287
8288 bool Safe = llvm::all_of(ShiftAmounts, [](const Constant *C) {
8289 auto *CI = dyn_cast_or_null<ConstantInt>(C);
8290 return CI && CI->getValue().ult(C->getType()->getIntegerBitWidth());
8291 });
8292
8293 return Safe;
8294}
8295
8297 bool ConsiderFlagsAndMetadata) {
8298 const auto *CB = dyn_cast<CallBase>(Op);
8299 bool CBHasNoUndef = CB && CB->hasRetAttr(Attribute::NoUndef);
8300 if (ConsiderFlagsAndMetadata && includesPoison(Kind) &&
8301 Op->hasPoisonGeneratingAnnotations() && !CBHasNoUndef)
8302 return true;
8303
8304 unsigned Opcode = Op->getOpcode();
8305
8306 // Check whether opcode is a poison/undef-generating operation
8307 switch (Opcode) {
8308 case Instruction::Shl:
8309 case Instruction::AShr:
8310 case Instruction::LShr:
8311 return includesPoison(Kind) && !shiftAmountKnownInRange(Op->getOperand(1));
8312 case Instruction::FPToSI:
8313 case Instruction::FPToUI:
8314 // fptosi/ui yields poison if the resulting value does not fit in the
8315 // destination type.
8316 return true;
8317 case Instruction::Call:
8318 if (auto *II = dyn_cast<IntrinsicInst>(Op)) {
8319 switch (II->getIntrinsicID()) {
8320 // NOTE: Use IntrNoCreateUndefOrPoison when possible.
8321 case Intrinsic::ctlz:
8322 case Intrinsic::cttz:
8323 case Intrinsic::abs:
8324 // We're not considering flags so it is safe to just return false.
8325 return false;
8326 case Intrinsic::sshl_sat:
8327 case Intrinsic::ushl_sat:
8328 if (!includesPoison(Kind) ||
8329 shiftAmountKnownInRange(II->getArgOperand(1)))
8330 return false;
8331 break;
8332 }
8333 }
8334 [[fallthrough]];
8335 case Instruction::CallBr:
8336 case Instruction::Invoke: {
8337 return !CBHasNoUndef && !CB->hasFnAttr(Attribute::NoCreateUndefOrPoison);
8338 }
8339 case Instruction::InsertElement:
8340 case Instruction::ExtractElement: {
8341 // If index exceeds the length of the vector, it returns poison
8342 auto *VTy = cast<VectorType>(Op->getOperand(0)->getType());
8343 unsigned IdxOp = Op->getOpcode() == Instruction::InsertElement ? 2 : 1;
8344 auto *Idx = dyn_cast<ConstantInt>(Op->getOperand(IdxOp));
8345 if (includesPoison(Kind))
8346 return !Idx ||
8347 Idx->getValue().uge(VTy->getElementCount().getKnownMinValue());
8348 return false;
8349 }
8350 case Instruction::ShuffleVector: {
8352 ? cast<ConstantExpr>(Op)->getShuffleMask()
8353 : cast<ShuffleVectorInst>(Op)->getShuffleMask();
8354 return includesPoison(Kind) && is_contained(Mask, PoisonMaskElem);
8355 }
8356 case Instruction::FNeg:
8357 case Instruction::PHI:
8358 case Instruction::Select:
8359 case Instruction::ExtractValue:
8360 case Instruction::InsertValue:
8361 case Instruction::Freeze:
8362 case Instruction::ICmp:
8363 case Instruction::FCmp:
8364 case Instruction::GetElementPtr:
8365 return false;
8366 case Instruction::AddrSpaceCast:
8367 return true;
8368 default: {
8369 const auto *CE = dyn_cast<ConstantExpr>(Op);
8370 if (isa<CastInst>(Op) || (CE && CE->isCast()))
8371 return false;
8372 else if (Instruction::isBinaryOp(Opcode))
8373 return false;
8374 // Be conservative and return true.
8375 return true;
8376 }
8377 }
8378}
8379
8381 bool ConsiderFlagsAndMetadata) {
8382 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::UndefOrPoison,
8383 ConsiderFlagsAndMetadata);
8384}
8385
8386bool llvm::canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata) {
8387 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::PoisonOnly,
8388 ConsiderFlagsAndMetadata);
8389}
8390
8391static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V,
8392 unsigned Depth) {
8393 if (ValAssumedPoison == V)
8394 return true;
8395
8396 const unsigned MaxDepth = 2;
8397 if (Depth >= MaxDepth)
8398 return false;
8399
8400 if (const auto *I = dyn_cast<Instruction>(V)) {
8401 if (any_of(I->operands(), [=](const Use &Op) {
8402 return propagatesPoison(Op) &&
8403 directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1);
8404 }))
8405 return true;
8406
8407 // V = extractvalue V0, idx
8408 // V2 = extractvalue V0, idx2
8409 // V0's elements are all poison or not. (e.g., add_with_overflow)
8410 const WithOverflowInst *II;
8412 (match(ValAssumedPoison, m_ExtractValue(m_Specific(II))) ||
8413 llvm::is_contained(II->args(), ValAssumedPoison)))
8414 return true;
8415 }
8416 return false;
8417}
8418
8419static bool impliesPoison(const Value *ValAssumedPoison, const Value *V,
8420 unsigned Depth) {
8421 if (isGuaranteedNotToBePoison(ValAssumedPoison))
8422 return true;
8423
8424 if (directlyImpliesPoison(ValAssumedPoison, V, /* Depth */ 0))
8425 return true;
8426
8427 const unsigned MaxDepth = 2;
8428 if (Depth >= MaxDepth)
8429 return false;
8430
8431 const auto *I = dyn_cast<Instruction>(ValAssumedPoison);
8432 if (I && !canCreatePoison(cast<Operator>(I))) {
8433 return all_of(I->operands(), [=](const Value *Op) {
8434 return impliesPoison(Op, V, Depth + 1);
8435 });
8436 }
8437 return false;
8438}
8439
8440bool llvm::impliesPoison(const Value *ValAssumedPoison, const Value *V) {
8441 return ::impliesPoison(ValAssumedPoison, V, /* Depth */ 0);
8442}
8443
8444static bool programUndefinedIfUndefOrPoison(const Value *V, bool PoisonOnly);
8445
8447 const Value *V, AssumptionCache *AC, const Instruction *CtxI,
8448 const DominatorTree *DT, unsigned Depth, UndefPoisonKind Kind) {
8450 return false;
8451
8453 return false;
8454
8455 if (const auto *A = dyn_cast<Argument>(V)) {
8456 if (A->hasAttribute(Attribute::NoUndef) ||
8457 A->hasAttribute(Attribute::Dereferenceable) ||
8458 A->hasAttribute(Attribute::DereferenceableOrNull))
8459 return true;
8460 }
8461
8462 if (auto *C = dyn_cast<Constant>(V)) {
8463 if (isa<PoisonValue>(C))
8464 return !includesPoison(Kind);
8465
8466 if (isa<UndefValue>(C))
8467 return !includesUndef(Kind);
8468
8471 return true;
8472
8473 if (C->getType()->isVectorTy() || C->getType()->isAggregateType()) {
8474 if (isa<ConstantExpr>(C)) {
8475 // Scalable vectors can use a ConstantExpr to build a splat.
8476 if (Constant *SplatC = C->getSplatValue())
8477 if (isa<ConstantInt>(SplatC) || isa<ConstantFP>(SplatC))
8478 return true;
8479 } else if (auto *CA = dyn_cast<ConstantAggregate>(C)) {
8480 return all_of(CA->operands(), [&](const Use &Op) {
8481 return isGuaranteedNotToBeUndefOrPoison(Op.get(), AC, CtxI, DT,
8482 Depth + 1, Kind);
8483 });
8484 }
8485 }
8486 }
8487
8488 // Strip cast operations from a pointer value.
8489 // Note that stripPointerCastsSameRepresentation can strip off getelementptr
8490 // inbounds with zero offset. To guarantee that the result isn't poison, the
8491 // stripped pointer is checked as it has to be pointing into an allocated
8492 // object or be null `null` to ensure `inbounds` getelement pointers with a
8493 // zero offset could not produce poison.
8494 // It can strip off addrspacecast that do not change bit representation as
8495 // well. We believe that such addrspacecast is equivalent to no-op.
8496 auto *StrippedV = V->stripPointerCastsSameRepresentation();
8497 if (isa<AllocaInst>(StrippedV) || isa<GlobalVariable>(StrippedV) ||
8498 isa<Function>(StrippedV) || isa<ConstantPointerNull>(StrippedV))
8499 return true;
8500
8501 auto OpCheck = [&](const Value *V) {
8502 return isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth + 1, Kind);
8503 };
8504
8505 if (auto *Opr = dyn_cast<Operator>(V)) {
8506 // If the value is a freeze instruction, then it can never
8507 // be undef or poison.
8508 if (isa<FreezeInst>(V))
8509 return true;
8510
8511 if (const auto *CB = dyn_cast<CallBase>(V)) {
8512 if (CB->hasRetAttr(Attribute::NoUndef) ||
8513 CB->hasRetAttr(Attribute::Dereferenceable) ||
8514 CB->hasRetAttr(Attribute::DereferenceableOrNull))
8515 return true;
8516 }
8517
8518 if (!::canCreateUndefOrPoison(Opr, Kind,
8519 /*ConsiderFlagsAndMetadata=*/true)) {
8520 if (const auto *PN = dyn_cast<PHINode>(V)) {
8521 unsigned Num = PN->getNumIncomingValues();
8522 bool IsWellDefined = true;
8523 for (unsigned i = 0; i < Num; ++i) {
8524 if (PN == PN->getIncomingValue(i))
8525 continue;
8526 auto *TI = PN->getIncomingBlock(i)->getTerminator();
8527 if (!isGuaranteedNotToBeUndefOrPoison(PN->getIncomingValue(i), AC, TI,
8528 DT, Depth + 1, Kind)) {
8529 IsWellDefined = false;
8530 break;
8531 }
8532 }
8533 if (IsWellDefined)
8534 return true;
8535 } else if (auto *Splat = isa<ShuffleVectorInst>(Opr) ? getSplatValue(Opr)
8536 : nullptr) {
8537 // For splats we only need to check the value being splatted.
8538 if (OpCheck(Splat))
8539 return true;
8540 } else if (all_of(Opr->operands(), OpCheck))
8541 return true;
8542 }
8543 }
8544
8545 if (auto *I = dyn_cast<LoadInst>(V))
8546 if (I->hasMetadata(LLVMContext::MD_noundef) ||
8547 I->hasMetadata(LLVMContext::MD_dereferenceable) ||
8548 I->hasMetadata(LLVMContext::MD_dereferenceable_or_null))
8549 return true;
8550
8552 return true;
8553
8554 // CtxI may be null or a cloned instruction.
8555 if (!CtxI || !CtxI->getParent() || !DT)
8556 return false;
8557
8558 auto *DNode = DT->getNode(CtxI->getParent());
8559 if (!DNode)
8560 // Unreachable block
8561 return false;
8562
8563 // If V is used as a branch condition before reaching CtxI, V cannot be
8564 // undef or poison.
8565 // br V, BB1, BB2
8566 // BB1:
8567 // CtxI ; V cannot be undef or poison here
8568 auto *Dominator = DNode->getIDom();
8569 // This check is purely for compile time reasons: we can skip the IDom walk
8570 // if what we are checking for includes undef and the value is not an integer.
8571 if (!includesUndef(Kind) || V->getType()->isIntegerTy())
8572 while (Dominator) {
8573 auto *TI = Dominator->getBlock()->getTerminatorOrNull();
8574
8575 Value *Cond = nullptr;
8576 if (auto BI = dyn_cast_or_null<CondBrInst>(TI)) {
8577 Cond = BI->getCondition();
8578 } else if (auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
8579 Cond = SI->getCondition();
8580 }
8581
8582 if (Cond) {
8583 if (Cond == V)
8584 return true;
8585 else if (!includesUndef(Kind) && isa<Operator>(Cond)) {
8586 // For poison, we can analyze further
8587 auto *Opr = cast<Operator>(Cond);
8588 if (any_of(Opr->operands(), [V](const Use &U) {
8589 return V == U && propagatesPoison(U);
8590 }))
8591 return true;
8592 }
8593 }
8594
8595 Dominator = Dominator->getIDom();
8596 }
8597
8598 if (AC && getKnowledgeValidInContext(V, {Attribute::NoUndef}, *AC, CtxI, DT))
8599 return true;
8600
8601 return false;
8602}
8603
8605 const Instruction *CtxI,
8606 const DominatorTree *DT,
8607 unsigned Depth) {
8608 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
8610}
8611
8613 const Instruction *CtxI,
8614 const DominatorTree *DT, unsigned Depth) {
8615 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
8617}
8618
8620 const Instruction *CtxI,
8621 const DominatorTree *DT, unsigned Depth) {
8622 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
8624}
8625
8626/// Return true if undefined behavior would provably be executed on the path to
8627/// OnPathTo if Root produced a posion result. Note that this doesn't say
8628/// anything about whether OnPathTo is actually executed or whether Root is
8629/// actually poison. This can be used to assess whether a new use of Root can
8630/// be added at a location which is control equivalent with OnPathTo (such as
8631/// immediately before it) without introducing UB which didn't previously
8632/// exist. Note that a false result conveys no information.
8634 Instruction *OnPathTo,
8635 DominatorTree *DT) {
8636 // Basic approach is to assume Root is poison, propagate poison forward
8637 // through all users we can easily track, and then check whether any of those
8638 // users are provable UB and must execute before out exiting block might
8639 // exit.
8640
8641 // The set of all recursive users we've visited (which are assumed to all be
8642 // poison because of said visit)
8645 Worklist.push_back(Root);
8646 while (!Worklist.empty()) {
8647 const Instruction *I = Worklist.pop_back_val();
8648
8649 // If we know this must trigger UB on a path leading our target.
8650 if (mustTriggerUB(I, KnownPoison) && DT->dominates(I, OnPathTo))
8651 return true;
8652
8653 // If we can't analyze propagation through this instruction, just skip it
8654 // and transitive users. Safe as false is a conservative result.
8655 if (I != Root && !any_of(I->operands(), [&KnownPoison](const Use &U) {
8656 return KnownPoison.contains(U) && propagatesPoison(U);
8657 }))
8658 continue;
8659
8660 if (KnownPoison.insert(I).second)
8661 for (const User *User : I->users())
8662 Worklist.push_back(cast<Instruction>(User));
8663 }
8664
8665 // Might be non-UB, or might have a path we couldn't prove must execute on
8666 // way to exiting bb.
8667 return false;
8668}
8669
8671 const SimplifyQuery &SQ) {
8672 return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1),
8673 Add, SQ);
8674}
8675
8678 const WithCache<const Value *> &RHS,
8679 const SimplifyQuery &SQ) {
8680 return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, SQ);
8681}
8682
8684 // Note: An atomic operation isn't guaranteed to return in a reasonable amount
8685 // of time because it's possible for another thread to interfere with it for an
8686 // arbitrary length of time, but programs aren't allowed to rely on that.
8687
8688 // If there is no successor, then execution can't transfer to it.
8689 if (isa<ReturnInst>(I))
8690 return false;
8692 return false;
8693
8694 // Note: Do not add new checks here; instead, change Instruction::mayThrow or
8695 // Instruction::willReturn.
8696 //
8697 // FIXME: Move this check into Instruction::willReturn.
8698 if (isa<CatchPadInst>(I)) {
8699 switch (classifyEHPersonality(I->getFunction()->getPersonalityFn())) {
8700 default:
8701 // A catchpad may invoke exception object constructors and such, which
8702 // in some languages can be arbitrary code, so be conservative by default.
8703 return false;
8705 // For CoreCLR, it just involves a type test.
8706 return true;
8707 }
8708 }
8709
8710 // An instruction that returns without throwing must transfer control flow
8711 // to a successor.
8712 return !I->mayThrow() && I->willReturn();
8713}
8714
8716 // TODO: This is slightly conservative for invoke instruction since exiting
8717 // via an exception *is* normal control for them.
8718 for (const Instruction &I : *BB)
8720 return false;
8721 return true;
8722}
8723
8730
8733 assert(ScanLimit && "scan limit must be non-zero");
8734 for (const Instruction &I : Range) {
8735 if (--ScanLimit == 0)
8736 return false;
8738 return false;
8739 }
8740 return true;
8741}
8742
8744 const Loop *L) {
8745 // The loop header is guaranteed to be executed for every iteration.
8746 //
8747 // FIXME: Relax this constraint to cover all basic blocks that are
8748 // guaranteed to be executed at every iteration.
8749 if (I->getParent() != L->getHeader()) return false;
8750
8751 for (const Instruction &LI : *L->getHeader()) {
8752 if (&LI == I) return true;
8753 if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;
8754 }
8755 llvm_unreachable("Instruction not contained in its own parent basic block.");
8756}
8757
8759 switch (IID) {
8760 // TODO: Add more intrinsics.
8761 case Intrinsic::sadd_with_overflow:
8762 case Intrinsic::ssub_with_overflow:
8763 case Intrinsic::smul_with_overflow:
8764 case Intrinsic::uadd_with_overflow:
8765 case Intrinsic::usub_with_overflow:
8766 case Intrinsic::umul_with_overflow:
8767 // If an input is a vector containing a poison element, the
8768 // two output vectors (calculated results, overflow bits)'
8769 // corresponding lanes are poison.
8770 return true;
8771 case Intrinsic::ctpop:
8772 case Intrinsic::ctlz:
8773 case Intrinsic::cttz:
8774 case Intrinsic::abs:
8775 case Intrinsic::smax:
8776 case Intrinsic::smin:
8777 case Intrinsic::umax:
8778 case Intrinsic::umin:
8779 case Intrinsic::scmp:
8780 case Intrinsic::smulh:
8781 case Intrinsic::umulh:
8782 case Intrinsic::is_fpclass:
8783 case Intrinsic::ptrmask:
8784 case Intrinsic::ucmp:
8785 case Intrinsic::bitreverse:
8786 case Intrinsic::bswap:
8787 case Intrinsic::sadd_sat:
8788 case Intrinsic::ssub_sat:
8789 case Intrinsic::sshl_sat:
8790 case Intrinsic::uadd_sat:
8791 case Intrinsic::usub_sat:
8792 case Intrinsic::ushl_sat:
8793 case Intrinsic::smul_fix:
8794 case Intrinsic::smul_fix_sat:
8795 case Intrinsic::umul_fix:
8796 case Intrinsic::umul_fix_sat:
8797 case Intrinsic::pow:
8798 case Intrinsic::experimental_constrained_pow:
8799 case Intrinsic::powi:
8800 case Intrinsic::experimental_constrained_powi:
8801 case Intrinsic::sin:
8802 case Intrinsic::experimental_constrained_sin:
8803 case Intrinsic::sinh:
8804 case Intrinsic::experimental_constrained_sinh:
8805 case Intrinsic::cos:
8806 case Intrinsic::experimental_constrained_cos:
8807 case Intrinsic::cosh:
8808 case Intrinsic::experimental_constrained_cosh:
8809 case Intrinsic::sincos:
8810 case Intrinsic::sincospi:
8811 case Intrinsic::tan:
8812 case Intrinsic::experimental_constrained_tan:
8813 case Intrinsic::tanh:
8814 case Intrinsic::experimental_constrained_tanh:
8815 case Intrinsic::asin:
8816 case Intrinsic::experimental_constrained_asin:
8817 case Intrinsic::acos:
8818 case Intrinsic::experimental_constrained_acos:
8819 case Intrinsic::atan:
8820 case Intrinsic::experimental_constrained_atan:
8821 case Intrinsic::atan2:
8822 case Intrinsic::experimental_constrained_atan2:
8823 case Intrinsic::canonicalize:
8824 case Intrinsic::sqrt:
8825 case Intrinsic::experimental_constrained_sqrt:
8826 case Intrinsic::fma:
8827 case Intrinsic::experimental_constrained_fma:
8828 case Intrinsic::fmuladd:
8829 case Intrinsic::experimental_constrained_fmuladd:
8830 case Intrinsic::exp:
8831 case Intrinsic::experimental_constrained_exp:
8832 case Intrinsic::exp2:
8833 case Intrinsic::experimental_constrained_exp2:
8834 case Intrinsic::exp10:
8835 case Intrinsic::log:
8836 case Intrinsic::experimental_constrained_log:
8837 case Intrinsic::log2:
8838 case Intrinsic::experimental_constrained_log2:
8839 case Intrinsic::log10:
8840 case Intrinsic::experimental_constrained_log10:
8841 case Intrinsic::modf:
8842 case Intrinsic::floor:
8843 case Intrinsic::experimental_constrained_floor:
8844 case Intrinsic::ceil:
8845 case Intrinsic::experimental_constrained_ceil:
8846 case Intrinsic::trunc:
8847 case Intrinsic::experimental_constrained_trunc:
8848 case Intrinsic::rint:
8849 case Intrinsic::experimental_constrained_rint:
8850 case Intrinsic::nearbyint:
8851 case Intrinsic::experimental_constrained_nearbyint:
8852 case Intrinsic::round:
8853 case Intrinsic::experimental_constrained_round:
8854 case Intrinsic::roundeven:
8855 case Intrinsic::experimental_constrained_roundeven:
8856 case Intrinsic::lrint:
8857 case Intrinsic::experimental_constrained_lrint:
8858 case Intrinsic::llrint:
8859 case Intrinsic::experimental_constrained_llrint:
8860 case Intrinsic::lround:
8861 case Intrinsic::experimental_constrained_lround:
8862 case Intrinsic::llround:
8863 case Intrinsic::experimental_constrained_llround:
8864 case Intrinsic::fshl:
8865 case Intrinsic::fshr:
8866 case Intrinsic::frexp:
8867 case Intrinsic::ldexp:
8868 case Intrinsic::experimental_constrained_ldexp:
8869 case Intrinsic::get_active_lane_mask:
8870 case Intrinsic::mask_beforefirst:
8871 return true;
8872 default:
8873 return false;
8874 }
8875}
8876
8877bool llvm::propagatesPoison(const Use &PoisonOp) {
8878 const Operator *I = cast<Operator>(PoisonOp.getUser());
8879 switch (I->getOpcode()) {
8880 case Instruction::Freeze:
8881 case Instruction::PHI:
8882 case Instruction::Invoke:
8883 return false;
8884 case Instruction::Select:
8885 return PoisonOp.getOperandNo() == 0;
8886 case Instruction::Call:
8887 if (auto *II = dyn_cast<IntrinsicInst>(I))
8888 return intrinsicPropagatesPoison(II->getIntrinsicID());
8889 return false;
8890 case Instruction::ICmp:
8891 case Instruction::FCmp:
8892 case Instruction::GetElementPtr:
8893 return true;
8894 default:
8896 return true;
8897
8898 // Be conservative and return false.
8899 return false;
8900 }
8901}
8902
8903/// Enumerates all operands of \p I that are guaranteed to not be undef or
8904/// poison. If the callback \p Handle returns true, stop processing and return
8905/// true. Otherwise, return false.
8906template <typename CallableT>
8908 const CallableT &Handle) {
8909 switch (I->getOpcode()) {
8910 case Instruction::Store:
8911 if (Handle(cast<StoreInst>(I)->getPointerOperand()))
8912 return true;
8913 break;
8914
8915 case Instruction::Load:
8916 if (Handle(cast<LoadInst>(I)->getPointerOperand()))
8917 return true;
8918 break;
8919
8920 // Since dereferenceable attribute imply noundef, atomic operations
8921 // also implicitly have noundef pointers too
8922 case Instruction::AtomicCmpXchg:
8924 return true;
8925 break;
8926
8927 case Instruction::AtomicRMW:
8928 if (Handle(cast<AtomicRMWInst>(I)->getPointerOperand()))
8929 return true;
8930 break;
8931
8932 case Instruction::Call:
8933 case Instruction::Invoke: {
8934 const CallBase *CB = cast<CallBase>(I);
8935 if (CB->isIndirectCall() && Handle(CB->getCalledOperand()))
8936 return true;
8937 for (unsigned i = 0; i < CB->arg_size(); ++i)
8938 if ((CB->paramHasAttr(i, Attribute::NoUndef) ||
8939 CB->paramHasAttr(i, Attribute::Dereferenceable) ||
8940 CB->paramHasAttr(i, Attribute::DereferenceableOrNull)) &&
8941 Handle(CB->getArgOperand(i)))
8942 return true;
8943 break;
8944 }
8945 case Instruction::Ret:
8946 if (I->getFunction()->hasRetAttribute(Attribute::NoUndef) &&
8947 Handle(I->getOperand(0)))
8948 return true;
8949 break;
8950 case Instruction::Switch:
8951 if (Handle(cast<SwitchInst>(I)->getCondition()))
8952 return true;
8953 break;
8954 case Instruction::CondBr:
8955 if (Handle(cast<CondBrInst>(I)->getCondition()))
8956 return true;
8957 break;
8958 default:
8959 break;
8960 }
8961
8962 return false;
8963}
8964
8965/// Enumerates all operands of \p I that are guaranteed to not be poison.
8966template <typename CallableT>
8968 const CallableT &Handle) {
8969 if (handleGuaranteedWellDefinedOps(I, Handle))
8970 return true;
8971 switch (I->getOpcode()) {
8972 // Divisors of these operations are allowed to be partially undef.
8973 case Instruction::UDiv:
8974 case Instruction::SDiv:
8975 case Instruction::URem:
8976 case Instruction::SRem:
8977 return Handle(I->getOperand(1));
8978 default:
8979 return false;
8980 }
8981}
8982
8984 const SmallPtrSetImpl<const Value *> &KnownPoison) {
8986 I, [&](const Value *V) { return KnownPoison.count(V); });
8987}
8988
8990 bool PoisonOnly) {
8991 // We currently only look for uses of values within the same basic
8992 // block, as that makes it easier to guarantee that the uses will be
8993 // executed given that Inst is executed.
8994 //
8995 // FIXME: Expand this to consider uses beyond the same basic block. To do
8996 // this, look out for the distinction between post-dominance and strong
8997 // post-dominance.
8998 const BasicBlock *BB = nullptr;
9000 if (const auto *Inst = dyn_cast<Instruction>(V)) {
9001 BB = Inst->getParent();
9002 Begin = Inst->getIterator();
9003 Begin++;
9004 } else if (const auto *Arg = dyn_cast<Argument>(V)) {
9005 if (Arg->getParent()->isDeclaration())
9006 return false;
9007 BB = &Arg->getParent()->getEntryBlock();
9008 Begin = BB->begin();
9009 } else {
9010 return false;
9011 }
9012
9013 // Limit number of instructions we look at, to avoid scanning through large
9014 // blocks. The current limit is chosen arbitrarily.
9015 unsigned ScanLimit = 32;
9017
9018 if (!PoisonOnly) {
9019 // Since undef does not propagate eagerly, be conservative & just check
9020 // whether a value is directly passed to an instruction that must take
9021 // well-defined operands.
9022
9023 for (const auto &I : make_range(Begin, End)) {
9024 if (--ScanLimit == 0)
9025 break;
9026
9027 if (handleGuaranteedWellDefinedOps(&I, [V](const Value *WellDefinedOp) {
9028 return WellDefinedOp == V;
9029 }))
9030 return true;
9031
9033 break;
9034 }
9035 return false;
9036 }
9037
9038 // Set of instructions that we have proved will yield poison if Inst
9039 // does.
9040 SmallPtrSet<const Value *, 16> YieldsPoison;
9042
9043 YieldsPoison.insert(V);
9044 Visited.insert(BB);
9045
9046 while (true) {
9047 for (const auto &I : make_range(Begin, End)) {
9048 if (--ScanLimit == 0)
9049 return false;
9050 if (mustTriggerUB(&I, YieldsPoison))
9051 return true;
9053 return false;
9054
9055 // If an operand is poison and propagates it, mark I as yielding poison.
9056 for (const Use &Op : I.operands()) {
9057 if (YieldsPoison.count(Op) && propagatesPoison(Op)) {
9058 YieldsPoison.insert(&I);
9059 break;
9060 }
9061 }
9062
9063 // Special handling for select, which returns poison if its operand 0 is
9064 // poison (handled in the loop above) *or* if both its true/false operands
9065 // are poison (handled here).
9066 if (I.getOpcode() == Instruction::Select &&
9067 YieldsPoison.count(I.getOperand(1)) &&
9068 YieldsPoison.count(I.getOperand(2))) {
9069 YieldsPoison.insert(&I);
9070 }
9071 }
9072
9073 BB = BB->getSingleSuccessor();
9074 if (!BB || !Visited.insert(BB).second)
9075 break;
9076
9077 Begin = BB->getFirstNonPHIIt();
9078 End = BB->end();
9079 }
9080 return false;
9081}
9082
9084 return ::programUndefinedIfUndefOrPoison(Inst, false);
9085}
9086
9088 return ::programUndefinedIfUndefOrPoison(Inst, true);
9089}
9090
9091static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) {
9092 if (FMF.noNaNs())
9093 return true;
9094
9095 if (auto *C = dyn_cast<ConstantFP>(V))
9096 return !C->isNaN();
9097
9098 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
9099 if (!C->getElementType()->isFloatingPointTy())
9100 return false;
9101 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
9102 if (C->getElementAsAPFloat(I).isNaN())
9103 return false;
9104 }
9105 return true;
9106 }
9107
9109 return true;
9110
9111 return false;
9112}
9113
9114static bool isKnownNonZero(const Value *V) {
9115 if (auto *C = dyn_cast<ConstantFP>(V))
9116 return !C->isZero();
9117
9118 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
9119 if (!C->getElementType()->isFloatingPointTy())
9120 return false;
9121 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
9122 if (C->getElementAsAPFloat(I).isZero())
9123 return false;
9124 }
9125 return true;
9126 }
9127
9128 return false;
9129}
9130
9131/// Match clamp pattern for float types without care about NaNs or signed zeros.
9132/// Given non-min/max outer cmp/select from the clamp pattern this
9133/// function recognizes if it can be substitued by a "canonical" min/max
9134/// pattern.
9136 Value *CmpLHS, Value *CmpRHS,
9137 Value *TrueVal, Value *FalseVal,
9138 Value *&LHS, Value *&RHS) {
9139 // Try to match
9140 // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2))
9141 // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2))
9142 // and return description of the outer Max/Min.
9143
9144 // First, check if select has inverse order:
9145 if (CmpRHS == FalseVal) {
9146 std::swap(TrueVal, FalseVal);
9148 }
9149
9150 // Assume success now. If there's no match, callers should not use these anyway.
9151 LHS = TrueVal;
9152 RHS = FalseVal;
9153
9154 const APFloat *FC1;
9155 if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite())
9156 return {SPF_UNKNOWN, SPNB_NA, false};
9157
9158 const APFloat *FC2;
9159 switch (Pred) {
9160 case CmpInst::FCMP_OLT:
9161 case CmpInst::FCMP_OLE:
9162 case CmpInst::FCMP_ULT:
9163 case CmpInst::FCMP_ULE:
9164 if (match(FalseVal, m_OrdOrUnordFMin(m_Specific(CmpLHS), m_APFloat(FC2))) &&
9165 *FC1 < *FC2)
9166 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
9167 if (match(FalseVal, m_FMinNum(m_Specific(CmpLHS), m_APFloat(FC2))) &&
9168 *FC1 < *FC2)
9169 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
9170 break;
9171 case CmpInst::FCMP_OGT:
9172 case CmpInst::FCMP_OGE:
9173 case CmpInst::FCMP_UGT:
9174 case CmpInst::FCMP_UGE:
9175 if (match(FalseVal, m_OrdOrUnordFMax(m_Specific(CmpLHS), m_APFloat(FC2))) &&
9176 *FC1 > *FC2)
9177 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
9178 if (match(FalseVal, m_FMaxNum(m_Specific(CmpLHS), m_APFloat(FC2))) &&
9179 *FC1 > *FC2)
9180 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
9181 break;
9182 default:
9183 break;
9184 }
9185
9186 return {SPF_UNKNOWN, SPNB_NA, false};
9187}
9188
9189/// Recognize variations of:
9190/// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
9192 Value *CmpLHS, Value *CmpRHS,
9193 Value *TrueVal, Value *FalseVal) {
9194 // Swap the select operands and predicate to match the patterns below.
9195 if (CmpRHS != TrueVal) {
9197 std::swap(TrueVal, FalseVal);
9198 }
9199 const APInt *C1;
9200 if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) {
9201 const APInt *C2;
9202 // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
9203 if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) &&
9204 C1->slt(*C2) && Pred == CmpInst::ICMP_SLT)
9205 return {SPF_SMAX, SPNB_NA, false};
9206
9207 // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
9208 if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) &&
9209 C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT)
9210 return {SPF_SMIN, SPNB_NA, false};
9211
9212 // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
9213 if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) &&
9214 C1->ult(*C2) && Pred == CmpInst::ICMP_ULT)
9215 return {SPF_UMAX, SPNB_NA, false};
9216
9217 // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
9218 if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) &&
9219 C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT)
9220 return {SPF_UMIN, SPNB_NA, false};
9221 }
9222 return {SPF_UNKNOWN, SPNB_NA, false};
9223}
9224
9225/// If the input value is the result of a 'not' op, constant integer, or vector
9226/// splat of a constant integer, return the bitwise-not source value.
9227/// TODO: This could be extended to handle non-splat vector integer constants.
9229 Value *NotV;
9230 if (match(V, m_Not(m_Value(NotV))))
9231 return NotV;
9232
9233 const APInt *C;
9234 if (match(V, m_APInt(C)))
9235 return ConstantInt::get(V->getType(), ~(*C));
9236
9237 return nullptr;
9238}
9239
9240/// Match non-obvious integer minimum and maximum sequences.
9242 Value *CmpRHS, Value *TrueVal,
9243 Value *FalseVal, Value *&LHS,
9244 Value *&RHS) {
9245 // Assume success. If there's no match, callers should not use these anyway.
9246 LHS = TrueVal;
9247 RHS = FalseVal;
9248
9249 SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);
9251 return SPR;
9252
9253 // Look through 'not' ops to find disguised min/max.
9254 // (X > Y) ? ~X : ~Y ==> (~X < ~Y) ? ~X : ~Y ==> MIN(~X, ~Y)
9255 // (X < Y) ? ~X : ~Y ==> (~X > ~Y) ? ~X : ~Y ==> MAX(~X, ~Y)
9256 if (CmpLHS == getNotValue(TrueVal) && CmpRHS == getNotValue(FalseVal)) {
9257 switch (Pred) {
9258 case CmpInst::ICMP_SGT: return {SPF_SMIN, SPNB_NA, false};
9259 case CmpInst::ICMP_SLT: return {SPF_SMAX, SPNB_NA, false};
9260 case CmpInst::ICMP_UGT: return {SPF_UMIN, SPNB_NA, false};
9261 case CmpInst::ICMP_ULT: return {SPF_UMAX, SPNB_NA, false};
9262 default: break;
9263 }
9264 }
9265
9266 // (X > Y) ? ~Y : ~X ==> (~X < ~Y) ? ~Y : ~X ==> MAX(~Y, ~X)
9267 // (X < Y) ? ~Y : ~X ==> (~X > ~Y) ? ~Y : ~X ==> MIN(~Y, ~X)
9268 if (CmpLHS == getNotValue(FalseVal) && CmpRHS == getNotValue(TrueVal)) {
9269 switch (Pred) {
9270 case CmpInst::ICMP_SGT: return {SPF_SMAX, SPNB_NA, false};
9271 case CmpInst::ICMP_SLT: return {SPF_SMIN, SPNB_NA, false};
9272 case CmpInst::ICMP_UGT: return {SPF_UMAX, SPNB_NA, false};
9273 case CmpInst::ICMP_ULT: return {SPF_UMIN, SPNB_NA, false};
9274 default: break;
9275 }
9276 }
9277
9279 return {SPF_UNKNOWN, SPNB_NA, false};
9280
9281 const APInt *C1;
9282 if (!match(CmpRHS, m_APInt(C1)))
9283 return {SPF_UNKNOWN, SPNB_NA, false};
9284
9285 // An unsigned min/max can be written with a signed compare.
9286 const APInt *C2;
9287 if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) ||
9288 (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) {
9289 // Is the sign bit set?
9290 // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX
9291 // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN
9292 if (Pred == CmpInst::ICMP_SLT && C1->isZero() && C2->isMaxSignedValue())
9293 return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
9294
9295 // Is the sign bit clear?
9296 // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX
9297 // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN
9298 if (Pred == CmpInst::ICMP_SGT && C1->isAllOnes() && C2->isMinSignedValue())
9299 return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
9300 }
9301
9302 return {SPF_UNKNOWN, SPNB_NA, false};
9303}
9304
9305bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW,
9306 bool AllowPoison) {
9307 assert(X && Y && "Invalid operand");
9308
9309 auto IsNegationOf = [&](const Value *X, const Value *Y) {
9310 if (!match(X, m_Neg(m_Specific(Y))))
9311 return false;
9312
9313 auto *BO = cast<BinaryOperator>(X);
9314 if (NeedNSW && !BO->hasNoSignedWrap())
9315 return false;
9316
9317 auto *Zero = cast<Constant>(BO->getOperand(0));
9318 if (!AllowPoison && !Zero->isNullValue())
9319 return false;
9320
9321 return true;
9322 };
9323
9324 // X = -Y or Y = -X
9325 if (IsNegationOf(X, Y) || IsNegationOf(Y, X))
9326 return true;
9327
9328 // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A)
9329 Value *A, *B;
9330 return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) &&
9331 match(Y, m_Sub(m_Specific(B), m_Specific(A))))) ||
9332 (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) &&
9334}
9335
9336bool llvm::isKnownInversion(const Value *X, const Value *Y) {
9337 // Handle X = icmp pred A, B, Y = icmp pred A, C.
9338 Value *A, *B, *C;
9339 CmpPredicate Pred1, Pred2;
9340 if (!match(X, m_ICmp(Pred1, m_Value(A), m_Value(B))) ||
9341 !match(Y, m_c_ICmp(Pred2, m_Specific(A), m_Value(C))))
9342 return false;
9343
9344 // They must both have samesign flag or not.
9345 if (Pred1.hasSameSign() != Pred2.hasSameSign())
9346 return false;
9347
9348 if (B == C)
9349 return Pred1 == ICmpInst::getInversePredicate(Pred2);
9350
9351 // Try to infer the relationship from constant ranges.
9352 const APInt *RHSC1, *RHSC2;
9353 if (!match(B, m_APInt(RHSC1)) || !match(C, m_APInt(RHSC2)))
9354 return false;
9355
9356 // Sign bits of two RHSCs should match.
9357 if (Pred1.hasSameSign() && RHSC1->isNonNegative() != RHSC2->isNonNegative())
9358 return false;
9359
9360 const auto CR1 = ConstantRange::makeExactICmpRegion(Pred1, *RHSC1);
9361 const auto CR2 = ConstantRange::makeExactICmpRegion(Pred2, *RHSC2);
9362
9363 return CR1.inverse() == CR2;
9364}
9365
9367 SelectPatternNaNBehavior NaNBehavior,
9368 bool Ordered) {
9369 switch (Pred) {
9370 default:
9371 return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.
9372 case ICmpInst::ICMP_UGT:
9373 case ICmpInst::ICMP_UGE:
9374 return {SPF_UMAX, SPNB_NA, false};
9375 case ICmpInst::ICMP_SGT:
9376 case ICmpInst::ICMP_SGE:
9377 return {SPF_SMAX, SPNB_NA, false};
9378 case ICmpInst::ICMP_ULT:
9379 case ICmpInst::ICMP_ULE:
9380 return {SPF_UMIN, SPNB_NA, false};
9381 case ICmpInst::ICMP_SLT:
9382 case ICmpInst::ICMP_SLE:
9383 return {SPF_SMIN, SPNB_NA, false};
9384 case FCmpInst::FCMP_UGT:
9385 case FCmpInst::FCMP_UGE:
9386 case FCmpInst::FCMP_OGT:
9387 case FCmpInst::FCMP_OGE:
9388 return {SPF_FMAXNUM, NaNBehavior, Ordered};
9389 case FCmpInst::FCMP_ULT:
9390 case FCmpInst::FCMP_ULE:
9391 case FCmpInst::FCMP_OLT:
9392 case FCmpInst::FCMP_OLE:
9393 return {SPF_FMINNUM, NaNBehavior, Ordered};
9394 }
9395}
9396
9397std::optional<std::pair<CmpPredicate, Constant *>>
9400 "Only for relational integer predicates.");
9401 if (isa<UndefValue>(C))
9402 return std::nullopt;
9403
9404 Type *Type = C->getType();
9405 bool IsSigned = ICmpInst::isSigned(Pred);
9406
9408 bool WillIncrement =
9409 UnsignedPred == ICmpInst::ICMP_ULE || UnsignedPred == ICmpInst::ICMP_UGT;
9410
9411 // Check if the constant operand can be safely incremented/decremented
9412 // without overflowing/underflowing.
9413 auto ConstantIsOk = [Pred, WillIncrement, IsSigned](ConstantInt *C) {
9414 if (WillIncrement ? C->isMaxValue(IsSigned) : C->isMinValue(IsSigned))
9415 return false;
9416
9417 if (!Pred.hasSameSign())
9418 return true;
9419
9420 // Crossing the corresponding boundary in the other ordering changes the
9421 // sign bit, and therefore changes the poison domain.
9422 return WillIncrement ? !C->isMaxValue(!IsSigned)
9423 : !C->isMinValue(!IsSigned);
9424 };
9425
9426 Constant *SafeReplacementConstant = nullptr;
9427 if (auto *CI = dyn_cast<ConstantInt>(C)) {
9428 // Bail out if the constant can't be safely incremented/decremented.
9429 if (!ConstantIsOk(CI))
9430 return std::nullopt;
9431 } else if (auto *FVTy = dyn_cast<FixedVectorType>(Type)) {
9432 unsigned NumElts = FVTy->getNumElements();
9433 for (unsigned i = 0; i != NumElts; ++i) {
9434 Constant *Elt = C->getAggregateElement(i);
9435 if (!Elt)
9436 return std::nullopt;
9437
9438 if (isa<UndefValue>(Elt))
9439 continue;
9440
9441 // Bail out if we can't determine if this constant is min/max or if we
9442 // know that this constant is min/max.
9443 auto *CI = dyn_cast<ConstantInt>(Elt);
9444 if (!CI || !ConstantIsOk(CI))
9445 return std::nullopt;
9446
9447 if (!SafeReplacementConstant)
9448 SafeReplacementConstant = CI;
9449 }
9450 } else if (isa<VectorType>(C->getType())) {
9451 // Handle scalable splat
9452 Value *SplatC = C->getSplatValue();
9453 auto *CI = dyn_cast_or_null<ConstantInt>(SplatC);
9454 // Bail out if the constant can't be safely incremented/decremented.
9455 if (!CI || !ConstantIsOk(CI))
9456 return std::nullopt;
9457 } else {
9458 // ConstantExpr?
9459 return std::nullopt;
9460 }
9461
9462 // It may not be safe to change a compare predicate in the presence of
9463 // undefined elements, so replace those elements with the first safe constant
9464 // that we found.
9465 // TODO: in case of poison, it is safe; let's replace undefs only.
9466 if (C->containsUndefOrPoisonElement()) {
9467 assert(SafeReplacementConstant && "Replacement constant not set");
9468 C = Constant::replaceUndefsWith(C, SafeReplacementConstant);
9469 }
9470
9472 Pred.hasSameSign());
9473
9474 // Increment or decrement the constant.
9475 Constant *OneOrNegOne = ConstantInt::get(Type, WillIncrement ? 1 : -1, true);
9476 Constant *NewC = ConstantExpr::getAdd(C, OneOrNegOne);
9477
9478 return std::make_pair(NewPred, NewC);
9479}
9480
9482 FastMathFlags FMF, Value *CmpLHS,
9483 Value *CmpRHS, Value *TrueVal,
9484 Value *FalseVal, Value *&LHS,
9485 Value *&RHS) {
9487 // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one
9488 // 0.0 operand, set the compare's 0.0 operands to that same value for the
9489 // purpose of identifying min/max. Disregard vector constants with undefined
9490 // elements because those can not be back-propagated for analysis.
9491 Value *OutputZeroVal = nullptr;
9492 if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) &&
9493 !cast<Constant>(TrueVal)->containsUndefOrPoisonElement())
9494 OutputZeroVal = TrueVal;
9495 else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) &&
9496 !cast<Constant>(FalseVal)->containsUndefOrPoisonElement())
9497 OutputZeroVal = FalseVal;
9498
9499 if (OutputZeroVal) {
9500 if (match(CmpLHS, m_AnyZeroFP()) && CmpLHS != OutputZeroVal)
9501 CmpLHS = OutputZeroVal;
9502 if (match(CmpRHS, m_AnyZeroFP()) && CmpRHS != OutputZeroVal)
9503 CmpRHS = OutputZeroVal;
9504 }
9505 }
9506
9507 LHS = CmpLHS;
9508 RHS = CmpRHS;
9509
9510 // Signed zero may return inconsistent results between implementations.
9511 // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0
9512 // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)
9513 // Therefore, we behave conservatively and only proceed if at least one of the
9514 // operands is known to not be zero or if we don't care about signed zero.
9516 if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
9517 !isKnownNonZero(CmpRHS))
9518 return {SPF_UNKNOWN, SPNB_NA, false};
9519 }
9520
9521 SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
9522 bool Ordered = false;
9523
9524 // When given one NaN and one non-NaN input:
9525 // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
9526 // - A simple C99 (a < b ? a : b) construction will return 'b' (as the
9527 // ordered comparison fails), which could be NaN or non-NaN.
9528 // so here we discover exactly what NaN behavior is required/accepted.
9530 bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
9531 bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
9532
9533 if (LHSSafe && RHSSafe) {
9534 // Both operands are known non-NaN.
9535 NaNBehavior = SPNB_RETURNS_ANY;
9536 Ordered = CmpInst::isOrdered(Pred);
9537 } else if (CmpInst::isOrdered(Pred)) {
9538 // An ordered comparison will return false when given a NaN, so it
9539 // returns the RHS.
9540 Ordered = true;
9541 if (LHSSafe)
9542 // LHS is non-NaN, so if RHS is NaN then NaN will be returned.
9543 NaNBehavior = SPNB_RETURNS_NAN;
9544 else if (RHSSafe)
9545 NaNBehavior = SPNB_RETURNS_OTHER;
9546 else
9547 // Completely unsafe.
9548 return {SPF_UNKNOWN, SPNB_NA, false};
9549 } else {
9550 Ordered = false;
9551 // An unordered comparison will return true when given a NaN, so it
9552 // returns the LHS.
9553 if (LHSSafe)
9554 // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
9555 NaNBehavior = SPNB_RETURNS_OTHER;
9556 else if (RHSSafe)
9557 NaNBehavior = SPNB_RETURNS_NAN;
9558 else
9559 // Completely unsafe.
9560 return {SPF_UNKNOWN, SPNB_NA, false};
9561 }
9562 }
9563
9564 if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
9565 std::swap(CmpLHS, CmpRHS);
9567 if (NaNBehavior == SPNB_RETURNS_NAN)
9568 NaNBehavior = SPNB_RETURNS_OTHER;
9569 else if (NaNBehavior == SPNB_RETURNS_OTHER)
9570 NaNBehavior = SPNB_RETURNS_NAN;
9571 Ordered = !Ordered;
9572 }
9573
9574 // ([if]cmp X, Y) ? X : Y
9575 if (TrueVal == CmpLHS && FalseVal == CmpRHS)
9576 return getSelectPattern(Pred, NaNBehavior, Ordered);
9577
9578 if (isKnownNegation(TrueVal, FalseVal)) {
9579 // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can
9580 // match against either LHS or sign-preserving operations on LHS, like
9581 // sext(LHS), or binary ops that do not wrap in signed sense.
9582 auto CmpLHSOrSExt =
9583 m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS)));
9584 auto MaybeSExtOrMulCmpLHS =
9585 m_CombineOr(CmpLHSOrSExt, m_NSWMul(CmpLHSOrSExt, m_StrictlyPositive()),
9586 m_NSWShl(CmpLHSOrSExt, m_Value()));
9587 auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes());
9588 auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One());
9589 if (match(TrueVal, MaybeSExtOrMulCmpLHS)) {
9590 // Set the return values. If the compare uses the negated value (-X >s 0),
9591 // swap the return values because the negated value is always 'RHS'.
9592 LHS = TrueVal;
9593 RHS = FalseVal;
9594 if (match(CmpLHS, m_Neg(m_Specific(FalseVal))))
9595 std::swap(LHS, RHS);
9596
9597 // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X)
9598 // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X)
9599 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
9600 return {SPF_ABS, SPNB_NA, false};
9601
9602 // (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X)
9603 if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne))
9604 return {SPF_ABS, SPNB_NA, false};
9605
9606 // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X)
9607 // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X)
9608 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
9609 return {SPF_NABS, SPNB_NA, false};
9610 } else if (match(FalseVal, MaybeSExtOrMulCmpLHS)) {
9611 // Set the return values. If the compare uses the negated value (-X >s 0),
9612 // swap the return values because the negated value is always 'RHS'.
9613 LHS = FalseVal;
9614 RHS = TrueVal;
9615 if (match(CmpLHS, m_Neg(m_Specific(TrueVal))))
9616 std::swap(LHS, RHS);
9617
9618 // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X)
9619 // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X)
9620 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
9621 return {SPF_NABS, SPNB_NA, false};
9622
9623 // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X)
9624 // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X)
9625 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
9626 return {SPF_ABS, SPNB_NA, false};
9627 }
9628 }
9629
9631 return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
9632
9633 // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar
9634 // may return either -0.0 or 0.0, so fcmp/select pair has stricter
9635 // semantics than minNum. Be conservative in such case.
9636 if (NaNBehavior != SPNB_RETURNS_ANY ||
9637 (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
9638 !isKnownNonZero(CmpRHS)))
9639 return {SPF_UNKNOWN, SPNB_NA, false};
9640
9641 return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
9642}
9643
9645 Instruction::CastOps *CastOp) {
9646 const DataLayout &DL = CmpI->getDataLayout();
9647
9648 Constant *CastedTo = nullptr;
9649 switch (*CastOp) {
9650 case Instruction::ZExt:
9651 if (CmpI->isUnsigned())
9652 CastedTo = ConstantExpr::getTrunc(C, SrcTy);
9653 break;
9654 case Instruction::SExt:
9655 if (CmpI->isSigned())
9656 CastedTo = ConstantExpr::getTrunc(C, SrcTy, true);
9657 break;
9658 case Instruction::Trunc:
9659 Constant *CmpConst;
9660 if (match(CmpI->getOperand(1), m_Constant(CmpConst)) &&
9661 CmpConst->getType() == SrcTy) {
9662 // Here we have the following case:
9663 //
9664 // %cond = cmp iN %x, CmpConst
9665 // %tr = trunc iN %x to iK
9666 // %narrowsel = select i1 %cond, iK %t, iK C
9667 //
9668 // We can always move trunc after select operation:
9669 //
9670 // %cond = cmp iN %x, CmpConst
9671 // %widesel = select i1 %cond, iN %x, iN CmpConst
9672 // %tr = trunc iN %widesel to iK
9673 //
9674 // Note that C could be extended in any way because we don't care about
9675 // upper bits after truncation. It can't be abs pattern, because it would
9676 // look like:
9677 //
9678 // select i1 %cond, x, -x.
9679 //
9680 // So only min/max pattern could be matched. Such match requires widened C
9681 // == CmpConst. That is why set widened C = CmpConst, condition trunc
9682 // CmpConst == C is checked below.
9683 CastedTo = CmpConst;
9684 } else {
9685 unsigned ExtOp = CmpI->isSigned() ? Instruction::SExt : Instruction::ZExt;
9686 CastedTo = ConstantFoldCastOperand(ExtOp, C, SrcTy, DL);
9687 }
9688 break;
9689 case Instruction::FPTrunc:
9690 CastedTo = ConstantFoldCastOperand(Instruction::FPExt, C, SrcTy, DL);
9691 break;
9692 case Instruction::FPExt:
9693 CastedTo = ConstantFoldCastOperand(Instruction::FPTrunc, C, SrcTy, DL);
9694 break;
9695 case Instruction::FPToUI:
9696 CastedTo = ConstantFoldCastOperand(Instruction::UIToFP, C, SrcTy, DL);
9697 break;
9698 case Instruction::FPToSI:
9699 CastedTo = ConstantFoldCastOperand(Instruction::SIToFP, C, SrcTy, DL);
9700 break;
9701 case Instruction::UIToFP:
9702 CastedTo = ConstantFoldCastOperand(Instruction::FPToUI, C, SrcTy, DL);
9703 break;
9704 case Instruction::SIToFP:
9705 CastedTo = ConstantFoldCastOperand(Instruction::FPToSI, C, SrcTy, DL);
9706 break;
9707 default:
9708 break;
9709 }
9710
9711 if (!CastedTo)
9712 return nullptr;
9713
9714 // Make sure the cast doesn't lose any information.
9715 Constant *CastedBack =
9716 ConstantFoldCastOperand(*CastOp, CastedTo, C->getType(), DL);
9717 if (CastedBack && CastedBack != C)
9718 return nullptr;
9719
9720 return CastedTo;
9721}
9722
9723/// Helps to match a select pattern in case of a type mismatch.
9724///
9725/// The function processes the case when type of true and false values of a
9726/// select instruction differs from type of the cmp instruction operands because
9727/// of a cast instruction. The function checks if it is legal to move the cast
9728/// operation after "select". If yes, it returns the new second value of
9729/// "select" (with the assumption that cast is moved):
9730/// 1. As operand of cast instruction when both values of "select" are same cast
9731/// instructions.
9732/// 2. As restored constant (by applying reverse cast operation) when the first
9733/// value of the "select" is a cast operation and the second value is a
9734/// constant. It is implemented in lookThroughCastConst().
9735/// 3. As one operand is cast instruction and the other is not. The operands in
9736/// sel(cmp) are in different type integer.
9737/// NOTE: We return only the new second value because the first value could be
9738/// accessed as operand of cast instruction.
9740 Instruction::CastOps *CastOp) {
9741 auto *Cast1 = dyn_cast<CastInst>(V1);
9742 if (!Cast1)
9743 return nullptr;
9744
9745 *CastOp = Cast1->getOpcode();
9746 Type *SrcTy = Cast1->getSrcTy();
9747 if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
9748 // If V1 and V2 are both the same cast from the same type, look through V1.
9749 if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
9750 return Cast2->getOperand(0);
9751 return nullptr;
9752 }
9753
9754 auto *C = dyn_cast<Constant>(V2);
9755 if (C)
9756 return lookThroughCastConst(CmpI, SrcTy, C, CastOp);
9757
9758 Value *CastedTo = nullptr;
9759 if (*CastOp == Instruction::Trunc) {
9760 if (match(CmpI->getOperand(1), m_ZExtOrSExt(m_Specific(V2)))) {
9761 // Here we have the following case:
9762 // %y_ext = sext iK %y to iN
9763 // %cond = cmp iN %x, %y_ext
9764 // %tr = trunc iN %x to iK
9765 // %narrowsel = select i1 %cond, iK %tr, iK %y
9766 //
9767 // We can always move trunc after select operation:
9768 // %y_ext = sext iK %y to iN
9769 // %cond = cmp iN %x, %y_ext
9770 // %widesel = select i1 %cond, iN %x, iN %y_ext
9771 // %tr = trunc iN %widesel to iK
9772 assert(V2->getType() == Cast1->getType() &&
9773 "V2 and Cast1 should be the same type.");
9774 CastedTo = CmpI->getOperand(1);
9775 }
9776 }
9777
9778 return CastedTo;
9779}
9781 Instruction::CastOps *CastOp) {
9783 if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
9784
9785 CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
9786 if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};
9787
9788 Value *TrueVal = SI->getTrueValue();
9789 Value *FalseVal = SI->getFalseValue();
9790
9792 CmpI, TrueVal, FalseVal, LHS, RHS, SI->getFastMathFlagsOrNone(), CastOp);
9793}
9794
9796 CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS,
9797 FastMathFlags FMF, Instruction::CastOps *CastOp) {
9798 CmpInst::Predicate Pred = CmpI->getPredicate();
9799 Value *CmpLHS = CmpI->getOperand(0);
9800 Value *CmpRHS = CmpI->getOperand(1);
9801 if (isa<FPMathOperator>(CmpI) && CmpI->hasNoNaNs())
9802 FMF.setNoNaNs();
9803
9804 // Bail out early.
9805 if (CmpI->isEquality())
9806 return {SPF_UNKNOWN, SPNB_NA, false};
9807
9808 // Deal with type mismatches.
9809 if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
9810 if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) {
9811 // If this is a potential fmin/fmax with a cast to integer, then ignore
9812 // -0.0 because there is no corresponding integer value.
9813 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9814 FMF.setNoSignedZeros();
9815 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9816 cast<CastInst>(TrueVal)->getOperand(0), C,
9817 LHS, RHS);
9818 }
9819 if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) {
9820 // If this is a potential fmin/fmax with a cast to integer, then ignore
9821 // -0.0 because there is no corresponding integer value.
9822 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9823 FMF.setNoSignedZeros();
9824 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, C,
9825 cast<CastInst>(FalseVal)->getOperand(0), LHS,
9826 RHS);
9827 }
9828 }
9829 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS,
9830 RHS);
9831}
9832
9834 if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT;
9835 if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT;
9836 if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT;
9837 if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT;
9838 if (SPF == SPF_FMINNUM)
9839 return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT;
9840 if (SPF == SPF_FMAXNUM)
9841 return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT;
9842 llvm_unreachable("unhandled!");
9843}
9844
9846 switch (SPF) {
9848 return Intrinsic::umin;
9850 return Intrinsic::umax;
9852 return Intrinsic::smin;
9854 return Intrinsic::smax;
9855 default:
9856 llvm_unreachable("Unexpected SPF");
9857 }
9858}
9859
9861 if (SPF == SPF_SMIN) return SPF_SMAX;
9862 if (SPF == SPF_UMIN) return SPF_UMAX;
9863 if (SPF == SPF_SMAX) return SPF_SMIN;
9864 if (SPF == SPF_UMAX) return SPF_UMIN;
9865 llvm_unreachable("unhandled!");
9866}
9867
9869 switch (MinMaxID) {
9870 case Intrinsic::smax: return Intrinsic::smin;
9871 case Intrinsic::smin: return Intrinsic::smax;
9872 case Intrinsic::umax: return Intrinsic::umin;
9873 case Intrinsic::umin: return Intrinsic::umax;
9874 // Please note that next four intrinsics may produce the same result for
9875 // original and inverted case even if X != Y due to NaN is handled specially.
9876 case Intrinsic::maximum: return Intrinsic::minimum;
9877 case Intrinsic::minimum: return Intrinsic::maximum;
9878 case Intrinsic::maxnum: return Intrinsic::minnum;
9879 case Intrinsic::minnum: return Intrinsic::maxnum;
9880 case Intrinsic::maximumnum:
9881 return Intrinsic::minimumnum;
9882 case Intrinsic::minimumnum:
9883 return Intrinsic::maximumnum;
9884 default: llvm_unreachable("Unexpected intrinsic");
9885 }
9886}
9887
9889 switch (SPF) {
9892 case SPF_UMAX: return APInt::getMaxValue(BitWidth);
9893 case SPF_UMIN: return APInt::getMinValue(BitWidth);
9894 default: llvm_unreachable("Unexpected flavor");
9895 }
9896}
9897
9898std::pair<Intrinsic::ID, bool>
9900 // Check if VL contains select instructions that can be folded into a min/max
9901 // vector intrinsic and return the intrinsic if it is possible.
9902 // TODO: Support floating point min/max.
9903 bool AllCmpSingleUse = true;
9904 SelectPatternResult SelectPattern;
9905 SelectPattern.Flavor = SPF_UNKNOWN;
9906 if (all_of(VL, [&SelectPattern, &AllCmpSingleUse](Value *I) {
9907 Value *LHS, *RHS;
9908 auto CurrentPattern = matchSelectPattern(I, LHS, RHS);
9909 if (!SelectPatternResult::isMinOrMax(CurrentPattern.Flavor))
9910 return false;
9911 if (SelectPattern.Flavor != SPF_UNKNOWN &&
9912 SelectPattern.Flavor != CurrentPattern.Flavor)
9913 return false;
9914 SelectPattern = CurrentPattern;
9915 AllCmpSingleUse &=
9917 return true;
9918 })) {
9919 switch (SelectPattern.Flavor) {
9920 case SPF_SMIN:
9921 return {Intrinsic::smin, AllCmpSingleUse};
9922 case SPF_UMIN:
9923 return {Intrinsic::umin, AllCmpSingleUse};
9924 case SPF_SMAX:
9925 return {Intrinsic::smax, AllCmpSingleUse};
9926 case SPF_UMAX:
9927 return {Intrinsic::umax, AllCmpSingleUse};
9928 case SPF_FMAXNUM:
9929 return {Intrinsic::maxnum, AllCmpSingleUse};
9930 case SPF_FMINNUM:
9931 return {Intrinsic::minnum, AllCmpSingleUse};
9932 default:
9933 llvm_unreachable("unexpected select pattern flavor");
9934 }
9935 }
9936 return {Intrinsic::not_intrinsic, false};
9937}
9938
9939template <typename InstTy>
9940static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst,
9941 Value *&Init, Value *&OtherOp) {
9942 // Handle the case of a simple two-predecessor recurrence PHI.
9943 // There's a lot more that could theoretically be done here, but
9944 // this is sufficient to catch some interesting cases.
9945 // TODO: Expand list -- gep, uadd.sat etc.
9946 if (PN->getNumIncomingValues() != 2)
9947 return false;
9948
9949 for (unsigned I = 0; I != 2; ++I) {
9950 if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
9951 Operation && Operation->getNumOperands() >= 2) {
9952 Value *LHS = Operation->getOperand(0);
9953 Value *RHS = Operation->getOperand(1);
9954 if (LHS != PN && RHS != PN)
9955 continue;
9956
9957 Inst = Operation;
9958 Init = PN->getIncomingValue(!I);
9959 OtherOp = (LHS == PN) ? RHS : LHS;
9960 return true;
9961 }
9962 }
9963 return false;
9964}
9965
9966template <typename InstTy>
9967static bool matchThreeInputRecurrence(const PHINode *PN, InstTy *&Inst,
9968 Value *&Init, Value *&OtherOp0,
9969 Value *&OtherOp1) {
9970 if (PN->getNumIncomingValues() != 2)
9971 return false;
9972
9973 for (unsigned I = 0; I != 2; ++I) {
9974 if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
9975 Operation && Operation->getNumOperands() >= 3) {
9976 Value *Op0 = Operation->getOperand(0);
9977 Value *Op1 = Operation->getOperand(1);
9978 Value *Op2 = Operation->getOperand(2);
9979
9980 if (Op0 != PN && Op1 != PN && Op2 != PN)
9981 continue;
9982
9983 Inst = Operation;
9984 Init = PN->getIncomingValue(!I);
9985 if (Op0 == PN) {
9986 OtherOp0 = Op1;
9987 OtherOp1 = Op2;
9988 } else if (Op1 == PN) {
9989 OtherOp0 = Op0;
9990 OtherOp1 = Op2;
9991 } else {
9992 OtherOp0 = Op0;
9993 OtherOp1 = Op1;
9994 }
9995 return true;
9996 }
9997 }
9998 return false;
9999}
10001 Value *&Start, Value *&Step) {
10002 // We try to match a recurrence of the form:
10003 // %iv = [Start, %entry], [%iv.next, %backedge]
10004 // %iv.next = binop %iv, Step
10005 // Or:
10006 // %iv = [Start, %entry], [%iv.next, %backedge]
10007 // %iv.next = binop Step, %iv
10008 return matchTwoInputRecurrence(P, BO, Start, Step);
10009}
10010
10012 Value *&Start, Value *&Step) {
10013 BinaryOperator *BO = nullptr;
10014 return match(I, m_c_BinOp(m_Phi(P), m_Value())) &&
10015 matchSimpleRecurrence(P, BO, Start, Step) && BO == I;
10016}
10017
10019 PHINode *&P, Value *&Init,
10020 Value *&OtherOp) {
10021 // Binary intrinsics only supported for now.
10022 if (I->arg_size() != 2 || I->getType() != I->getArgOperand(0)->getType() ||
10023 I->getType() != I->getArgOperand(1)->getType())
10024 return false;
10025
10026 IntrinsicInst *II = nullptr;
10027 P = dyn_cast<PHINode>(I->getArgOperand(0));
10028 if (!P)
10029 P = dyn_cast<PHINode>(I->getArgOperand(1));
10030
10031 return P && matchTwoInputRecurrence(P, II, Init, OtherOp) && II == I;
10032}
10033
10035 PHINode *&P, Value *&Init,
10036 Value *&OtherOp0,
10037 Value *&OtherOp1) {
10038 if (I->arg_size() != 3 || I->getType() != I->getArgOperand(0)->getType() ||
10039 I->getType() != I->getArgOperand(1)->getType() ||
10040 I->getType() != I->getArgOperand(2)->getType())
10041 return false;
10042 IntrinsicInst *II = nullptr;
10043 P = dyn_cast<PHINode>(I->getArgOperand(0));
10044 if (!P) {
10045 P = dyn_cast<PHINode>(I->getArgOperand(1));
10046 if (!P)
10047 P = dyn_cast<PHINode>(I->getArgOperand(2));
10048 }
10049 return P && matchThreeInputRecurrence(P, II, Init, OtherOp0, OtherOp1) &&
10050 II == I;
10051}
10052
10053/// Return true if "icmp Pred LHS RHS" is always true.
10055 const Value *RHS) {
10057 return true;
10058
10059 switch (Pred) {
10060 default:
10061 return false;
10062
10063 case CmpInst::ICMP_SLE: {
10064 const APInt *C;
10065
10066 // LHS s<= LHS +_{nsw} C if C >= 0
10067 // LHS s<= LHS | C if C >= 0
10068 if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C))) ||
10070 return !C->isNegative();
10071
10072 // LHS s<= smax(LHS, V) for any V
10074 return true;
10075
10076 // smin(RHS, V) s<= RHS for any V
10078 return true;
10079
10080 // Match A to (X +_{nsw} CA) and B to (X +_{nsw} CB)
10081 const Value *X;
10082 const APInt *CLHS, *CRHS;
10083 if (match(LHS, m_NSWAddLike(m_Value(X), m_APInt(CLHS))) &&
10085 return CLHS->sle(*CRHS);
10086
10087 return false;
10088 }
10089
10090 case CmpInst::ICMP_ULE: {
10091 // LHS u<= LHS +_{nuw} V for any V
10092 if (match(RHS, m_c_Add(m_Specific(LHS), m_Value())) &&
10094 return true;
10095
10096 // LHS u<= LHS | V for any V
10097 if (match(RHS, m_c_Or(m_Specific(LHS), m_Value())))
10098 return true;
10099
10100 // LHS u<= umax(LHS, V) for any V
10102 return true;
10103
10104 // RHS >> V u<= RHS for any V
10105 if (match(LHS, m_LShr(m_Specific(RHS), m_Value())))
10106 return true;
10107
10108 // RHS u/ C_ugt_1 u<= RHS
10109 const APInt *C;
10110 if (match(LHS, m_UDiv(m_Specific(RHS), m_APInt(C))) && C->ugt(1))
10111 return true;
10112
10113 // RHS & V u<= RHS for any V
10115 return true;
10116
10117 // umin(RHS, V) u<= RHS for any V
10119 return true;
10120
10121 // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
10122 const Value *X;
10123 const APInt *CLHS, *CRHS;
10124 if (match(LHS, m_NUWAddLike(m_Value(X), m_APInt(CLHS))) &&
10126 return CLHS->ule(*CRHS);
10127
10128 return false;
10129 }
10130 }
10131}
10132
10133/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
10134/// ALHS ARHS" is true. Otherwise, return std::nullopt.
10135static std::optional<bool>
10137 const Value *ARHS, const Value *BLHS, const Value *BRHS) {
10138 switch (Pred) {
10139 default:
10140 return std::nullopt;
10141
10142 case CmpInst::ICMP_SLT:
10143 case CmpInst::ICMP_SLE:
10144 if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS) &&
10146 return true;
10147 return std::nullopt;
10148
10149 case CmpInst::ICMP_SGT:
10150 case CmpInst::ICMP_SGE:
10151 if (isTruePredicate(CmpInst::ICMP_SLE, ALHS, BLHS) &&
10153 return true;
10154 return std::nullopt;
10155
10156 case CmpInst::ICMP_ULT:
10157 case CmpInst::ICMP_ULE:
10158 if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS) &&
10160 return true;
10161 return std::nullopt;
10162
10163 case CmpInst::ICMP_UGT:
10164 case CmpInst::ICMP_UGE:
10165 if (isTruePredicate(CmpInst::ICMP_ULE, ALHS, BLHS) &&
10167 return true;
10168 return std::nullopt;
10169 }
10170}
10171
10172/// Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
10173/// Return false if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is false.
10174/// Otherwise, return std::nullopt if we can't infer anything.
10175static std::optional<bool>
10177 CmpPredicate RPred, const ConstantRange &RCR) {
10178 auto CRImpliesPred = [&](ConstantRange CR,
10179 CmpInst::Predicate Pred) -> std::optional<bool> {
10180 // If all true values for lhs and true for rhs, lhs implies rhs
10181 if (CR.icmp(Pred, RCR))
10182 return true;
10183
10184 // If there is no overlap, lhs implies not rhs
10186 return false;
10187
10188 return std::nullopt;
10189 };
10190 if (auto Res = CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
10191 RPred))
10192 return Res;
10193 if (LPred.hasSameSign() ^ RPred.hasSameSign()) {
10195 : LPred.dropSameSign();
10197 : RPred.dropSameSign();
10198 return CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
10199 RPred);
10200 }
10201 return std::nullopt;
10202}
10203
10204/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
10205/// is true. Return false if LHS implies RHS is false. Otherwise, return
10206/// std::nullopt if we can't infer anything.
10207static std::optional<bool>
10208isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1,
10209 CmpPredicate RPred, const Value *R0, const Value *R1,
10210 const DataLayout &DL, bool LHSIsTrue) {
10211 // The rest of the logic assumes the LHS condition is true. If that's not the
10212 // case, invert the predicate to make it so.
10213 if (!LHSIsTrue)
10214 LPred = ICmpInst::getInverseCmpPredicate(LPred);
10215
10216 // We can have non-canonical operands, so try to normalize any common operand
10217 // to L0/R0.
10218 if (L0 == R1) {
10219 std::swap(R0, R1);
10220 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
10221 }
10222 if (R0 == L1) {
10223 std::swap(L0, L1);
10224 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
10225 }
10226 if (L1 == R1) {
10227 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
10228 if (L0 != R0 || match(L0, m_ImmConstant())) {
10229 std::swap(L0, L1);
10230 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
10231 std::swap(R0, R1);
10232 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
10233 }
10234 }
10235
10236 // See if we can infer anything if operand-0 matches and we have at least one
10237 // constant.
10238 const APInt *Unused;
10239 if (L0 == R0 && (match(L1, m_APInt(Unused)) || match(R1, m_APInt(Unused)))) {
10240 // Potential TODO: We could also further use the constant range of L0/R0 to
10241 // further constraint the constant ranges. At the moment this leads to
10242 // several regressions related to not transforming `multi_use(A + C0) eq/ne
10243 // C1` (see discussion: D58633).
10244 SimplifyQuery SQ(DL);
10249
10250 // Even if L1/R1 are not both constant, we can still sometimes deduce
10251 // relationship from a single constant. For example X u> Y implies X != 0.
10252 if (auto R = isImpliedCondCommonOperandWithCR(LPred, LCR, RPred, RCR))
10253 return R;
10254 // If both L1/R1 were exact constant ranges and we didn't get anything
10255 // here, we won't be able to deduce this.
10256 if (match(L1, m_APInt(Unused)) && match(R1, m_APInt(Unused)))
10257 return std::nullopt;
10258 }
10259
10260 // Can we infer anything when the two compares have matching operands?
10261 if (L0 == R0 && L1 == R1)
10262 return ICmpInst::isImpliedByMatchingCmp(LPred, RPred);
10263
10264 // It only really makes sense in the context of signed comparison for "X - Y
10265 // must be positive if X >= Y and no overflow".
10266 // Take SGT as an example: L0:x > L1:y and C >= 0
10267 // ==> R0:(x -nsw y) < R1:(-C) is false
10268 CmpInst::Predicate SignedLPred = LPred.getPreferredSignedPredicate();
10269 if ((SignedLPred == ICmpInst::ICMP_SGT ||
10270 SignedLPred == ICmpInst::ICMP_SGE) &&
10271 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
10272 if (match(R1, m_NonPositive()) &&
10273 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == false)
10274 return false;
10275 }
10276
10277 // Take SLT as an example: L0:x < L1:y and C <= 0
10278 // ==> R0:(x -nsw y) < R1:(-C) is true
10279 if ((SignedLPred == ICmpInst::ICMP_SLT ||
10280 SignedLPred == ICmpInst::ICMP_SLE) &&
10281 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
10282 if (match(R1, m_NonNegative()) &&
10283 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == true)
10284 return true;
10285 }
10286
10287 // (a - b) pred C -> a != b, if 0 pred C is false
10288 // (ptrtoint(a) - ptrtoint(b)) pred C -> a != b, if 0 pred C is false
10289 Value *A, *B;
10290 if (ICmpInst::isEquality(RPred) && cmpExcludesZero(LPred, L1) &&
10291 match(L0, m_Sub(m_Value(A), m_Value(B))) &&
10292 ((A == R0 && B == R1) || (A == R1 && B == R0) ||
10297 return RPred.dropSameSign() == ICmpInst::ICMP_NE;
10298 }
10299
10300 // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1
10301 if (L0 == R0 &&
10302 (LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) &&
10303 (RPred == ICmpInst::ICMP_ULT || RPred == ICmpInst::ICMP_UGE) &&
10304 match(L0, m_c_Add(m_Specific(L1), m_Specific(R1))))
10305 return CmpPredicate::getMatching(LPred, RPred).has_value();
10306
10307 if (auto P = CmpPredicate::getMatching(LPred, RPred))
10308 return isImpliedCondOperands(*P, L0, L1, R0, R1);
10309
10310 // L0 u< C sets limits to L0's bits which may imply (L0 & Mask) pred RC
10311 // Example: L0 u< 13 => (L0 & 16) == 0
10312 const APInt *LC, *RC, *MaskC;
10313 if (match(L1, m_APInt(LC)) && match(R1, m_APInt(RC)) &&
10314 match(R0, m_And(m_Specific(L0), m_APInt(MaskC)))) {
10316 ConstantRange MaskedCRange = LCRange.binaryAnd(*MaskC);
10317 if (MaskedCRange.icmp(RPred, ConstantRange(*RC)))
10318 return true;
10319 if (MaskedCRange.icmp(ICmpInst::getInversePredicate(RPred),
10320 ConstantRange(*RC)))
10321 return false;
10322 }
10323
10324 return std::nullopt;
10325}
10326
10327/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
10328/// is true. Return false if LHS implies RHS is false. Otherwise, return
10329/// std::nullopt if we can't infer anything.
10330static std::optional<bool>
10332 FCmpInst::Predicate RPred, const Value *R0, const Value *R1,
10333 const DataLayout &DL, bool LHSIsTrue) {
10334 // The rest of the logic assumes the LHS condition is true. If that's not the
10335 // case, invert the predicate to make it so.
10336 if (!LHSIsTrue)
10337 LPred = FCmpInst::getInversePredicate(LPred);
10338
10339 // We can have non-canonical operands, so try to normalize any common operand
10340 // to L0/R0.
10341 if (L0 == R1) {
10342 std::swap(R0, R1);
10343 RPred = FCmpInst::getSwappedPredicate(RPred);
10344 }
10345 if (R0 == L1) {
10346 std::swap(L0, L1);
10347 LPred = FCmpInst::getSwappedPredicate(LPred);
10348 }
10349 if (L1 == R1) {
10350 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
10351 if (L0 != R0 || match(L0, m_ImmConstant())) {
10352 std::swap(L0, L1);
10353 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
10354 std::swap(R0, R1);
10355 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
10356 }
10357 }
10358
10359 // Can we infer anything when the two compares have matching operands?
10360 if (L0 == R0 && L1 == R1) {
10361 if ((LPred & RPred) == LPred)
10362 return true;
10363 if ((LPred & ~RPred) == LPred)
10364 return false;
10365 }
10366
10367 // See if we can infer anything if operand-0 matches and we have at least one
10368 // constant.
10369 const APFloat *L1C, *R1C;
10370 if (L0 == R0 && match(L1, m_APFloat(L1C)) && match(R1, m_APFloat(R1C))) {
10371 if (std::optional<ConstantFPRange> DomCR =
10373 if (std::optional<ConstantFPRange> ImpliedCR =
10375 if (ImpliedCR->contains(*DomCR))
10376 return true;
10377 }
10378 if (std::optional<ConstantFPRange> ImpliedCR =
10380 FCmpInst::getInversePredicate(RPred), *R1C)) {
10381 if (ImpliedCR->contains(*DomCR))
10382 return false;
10383 }
10384 }
10385 }
10386
10387 return std::nullopt;
10388}
10389
10390/// Return true if LHS implies RHS is true. Return false if LHS implies RHS is
10391/// false. Otherwise, return std::nullopt if we can't infer anything. We
10392/// expect the RHS to be an icmp and the LHS to be an 'and', 'or', or a 'select'
10393/// instruction.
10394static std::optional<bool>
10396 const Value *RHSOp0, const Value *RHSOp1,
10397 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
10398 // The LHS must be an 'or', 'and', or a 'select' instruction.
10399 assert((LHS->getOpcode() == Instruction::And ||
10400 LHS->getOpcode() == Instruction::Or ||
10401 LHS->getOpcode() == Instruction::Select) &&
10402 "Expected LHS to be 'and', 'or', or 'select'.");
10403
10404 assert(Depth <= MaxAnalysisRecursionDepth && "Hit recursion limit");
10405
10406 // If the result of an 'or' is false, then we know both legs of the 'or' are
10407 // false. Similarly, if the result of an 'and' is true, then we know both
10408 // legs of the 'and' are true.
10409 const Value *ALHS, *ARHS;
10410 if ((!LHSIsTrue && match(LHS, m_LogicalOr(m_Value(ALHS), m_Value(ARHS)))) ||
10411 (LHSIsTrue && match(LHS, m_LogicalAnd(m_Value(ALHS), m_Value(ARHS))))) {
10412 // FIXME: Make this non-recursion.
10413 if (std::optional<bool> Implication = isImpliedCondition(
10414 ALHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
10415 return Implication;
10416 if (std::optional<bool> Implication = isImpliedCondition(
10417 ARHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
10418 return Implication;
10419 return std::nullopt;
10420 }
10421 return std::nullopt;
10422}
10423
10424std::optional<bool>
10426 const Value *RHSOp0, const Value *RHSOp1,
10427 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
10428 // Bail out when we hit the limit.
10430 return std::nullopt;
10431
10432 // A mismatch occurs when we compare a scalar cmp to a vector cmp, for
10433 // example.
10434 if (RHSOp0->getType()->isVectorTy() != LHS->getType()->isVectorTy())
10435 return std::nullopt;
10436
10437 assert(LHS->getType()->isIntOrIntVectorTy(1) &&
10438 "Expected integer type only!");
10439
10440 // Match not
10441 if (match(LHS, m_Not(m_Value(LHS))))
10442 LHSIsTrue = !LHSIsTrue;
10443
10444 // Both LHS and RHS are icmps.
10445 if (RHSOp0->getType()->getScalarType()->isIntOrPtrTy()) {
10446 CmpPredicate LHSPred;
10447 Value *LHSOp0, *LHSOp1;
10448 if (match(LHS, m_ICmpLike(LHSPred, m_Value(LHSOp0), m_Value(LHSOp1))))
10449 return isImpliedCondICmps(LHSPred, LHSOp0, LHSOp1, RHSPred, RHSOp0,
10450 RHSOp1, DL, LHSIsTrue);
10451 } else {
10452 assert(RHSOp0->getType()->isFPOrFPVectorTy() &&
10453 "Expected floating point type only!");
10454 if (const auto *LHSCmp = dyn_cast<FCmpInst>(LHS))
10455 return isImpliedCondFCmps(LHSCmp->getPredicate(), LHSCmp->getOperand(0),
10456 LHSCmp->getOperand(1), RHSPred, RHSOp0, RHSOp1,
10457 DL, LHSIsTrue);
10458 }
10459
10460 /// The LHS should be an 'or', 'and', or a 'select' instruction. We expect
10461 /// the RHS to be an icmp.
10462 /// FIXME: Add support for and/or/select on the RHS.
10463 if (const Instruction *LHSI = dyn_cast<Instruction>(LHS)) {
10464 if ((LHSI->getOpcode() == Instruction::And ||
10465 LHSI->getOpcode() == Instruction::Or ||
10466 LHSI->getOpcode() == Instruction::Select))
10467 return isImpliedCondAndOr(LHSI, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue,
10468 Depth);
10469 }
10470 return std::nullopt;
10471}
10472
10473std::optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS,
10474 const DataLayout &DL,
10475 bool LHSIsTrue, unsigned Depth) {
10476 // LHS ==> RHS by definition
10477 if (LHS == RHS)
10478 return LHSIsTrue;
10479
10480 // Match not
10481 bool InvertRHS = false;
10482 if (match(RHS, m_Not(m_Value(RHS)))) {
10483 if (LHS == RHS)
10484 return !LHSIsTrue;
10485 InvertRHS = true;
10486 }
10487
10488 CmpPredicate RHSPred;
10489 Value *RHSOp0, *RHSOp1;
10490 if (match(RHS, m_ICmpLike(RHSPred, m_Value(RHSOp0), m_Value(RHSOp1)))) {
10491 if (auto Implied = isImpliedCondition(LHS, RHSPred, RHSOp0, RHSOp1, DL,
10492 LHSIsTrue, Depth))
10493 return InvertRHS ? !*Implied : *Implied;
10494 return std::nullopt;
10495 }
10496 if (const FCmpInst *RHSCmp = dyn_cast<FCmpInst>(RHS)) {
10497 if (auto Implied = isImpliedCondition(
10498 LHS, RHSCmp->getPredicate(), RHSCmp->getOperand(0),
10499 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))
10500 return InvertRHS ? !*Implied : *Implied;
10501 return std::nullopt;
10502 }
10503
10505 return std::nullopt;
10506
10507 // LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS2
10508 // LHS ==> !(RHS1 && RHS2) if LHS ==> !RHS1 or LHS ==> !RHS2
10509 const Value *RHS1, *RHS2;
10510 if (match(RHS, m_LogicalOr(m_Value(RHS1), m_Value(RHS2)))) {
10511 if (std::optional<bool> Imp =
10512 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
10513 if (*Imp == true)
10514 return !InvertRHS;
10515 if (std::optional<bool> Imp =
10516 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
10517 if (*Imp == true)
10518 return !InvertRHS;
10519 }
10520 if (match(RHS, m_LogicalAnd(m_Value(RHS1), m_Value(RHS2)))) {
10521 if (std::optional<bool> Imp =
10522 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
10523 if (*Imp == false)
10524 return InvertRHS;
10525 if (std::optional<bool> Imp =
10526 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
10527 if (*Imp == false)
10528 return InvertRHS;
10529 }
10530
10531 return std::nullopt;
10532}
10533
10534// Returns a pair (Condition, ConditionIsTrue), where Condition is a branch
10535// condition dominating ContextI or nullptr, if no condition is found.
10536static std::pair<Value *, bool>
10538 if (!ContextI || !ContextI->getParent())
10539 return {nullptr, false};
10540
10541 // TODO: This is a poor/cheap way to determine dominance. Should we use a
10542 // dominator tree (eg, from a SimplifyQuery) instead?
10543 const BasicBlock *ContextBB = ContextI->getParent();
10544 const BasicBlock *PredBB = ContextBB->getSinglePredecessor();
10545 if (!PredBB)
10546 return {nullptr, false};
10547
10548 // We need a conditional branch in the predecessor.
10549 Value *PredCond;
10550 BasicBlock *TrueBB, *FalseBB;
10551 if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))
10552 return {nullptr, false};
10553
10554 // The branch should get simplified. Don't bother simplifying this condition.
10555 if (TrueBB == FalseBB)
10556 return {nullptr, false};
10557
10558 assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
10559 "Predecessor block does not point to successor?");
10560
10561 // Is this condition implied by the predecessor condition?
10562 return {PredCond, TrueBB == ContextBB};
10563}
10564
10565std::optional<bool> llvm::isImpliedByDomCondition(const Value *Cond,
10566 const Instruction *ContextI,
10567 const DataLayout &DL) {
10568 assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool");
10569 auto PredCond = getDomPredecessorCondition(ContextI);
10570 if (PredCond.first)
10571 return isImpliedCondition(PredCond.first, Cond, DL, PredCond.second);
10572 return std::nullopt;
10573}
10574
10576 const Value *LHS,
10577 const Value *RHS,
10578 const Instruction *ContextI,
10579 const DataLayout &DL) {
10580 auto PredCond = getDomPredecessorCondition(ContextI);
10581 if (PredCond.first)
10582 return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL,
10583 PredCond.second);
10584 return std::nullopt;
10585}
10586
10588 APInt &Upper, const InstrInfoQuery &IIQ,
10589 bool PreferSignedRange) {
10590 unsigned Width = Lower.getBitWidth();
10591 const APInt *C;
10592 switch (BO.getOpcode()) {
10593 case Instruction::Sub:
10594 if (match(BO.getOperand(0), m_APInt(C))) {
10595 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
10596 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
10597
10598 // If the caller expects a signed compare, then try to use a signed range.
10599 // Otherwise if both no-wraps are set, use the unsigned range because it
10600 // is never larger than the signed range. Example:
10601 // "sub nuw nsw i8 -2, x" is unsigned [0, 254] vs. signed [-128, 126].
10602 // "sub nuw nsw i8 2, x" is unsigned [0, 2] vs. signed [-125, 127].
10603 if (PreferSignedRange && HasNSW && HasNUW)
10604 HasNUW = false;
10605
10606 if (HasNUW) {
10607 // 'sub nuw c, x' produces [0, C].
10608 Upper = *C + 1;
10609 } else if (HasNSW) {
10610 if (C->isNegative()) {
10611 // 'sub nsw -C, x' produces [SINT_MIN, -C - SINT_MIN].
10613 Upper = *C - APInt::getSignedMaxValue(Width);
10614 } else {
10615 // Note that sub 0, INT_MIN is not NSW. It techically is a signed wrap
10616 // 'sub nsw C, x' produces [C - SINT_MAX, SINT_MAX].
10617 Lower = *C - APInt::getSignedMaxValue(Width);
10619 }
10620 }
10621 }
10622 break;
10623 case Instruction::Add:
10624 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
10625 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
10626 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
10627
10628 // If the caller expects a signed compare, then try to use a signed
10629 // range. Otherwise if both no-wraps are set, use the unsigned range
10630 // because it is never larger than the signed range. Example: "add nuw
10631 // nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125].
10632 if (PreferSignedRange && HasNSW && HasNUW)
10633 HasNUW = false;
10634
10635 if (HasNUW) {
10636 // 'add nuw x, C' produces [C, UINT_MAX].
10637 Lower = *C;
10638 } else if (HasNSW) {
10639 if (C->isNegative()) {
10640 // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C].
10642 Upper = APInt::getSignedMaxValue(Width) + *C + 1;
10643 } else {
10644 // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX].
10645 Lower = APInt::getSignedMinValue(Width) + *C;
10646 Upper = APInt::getSignedMaxValue(Width) + 1;
10647 }
10648 }
10649 }
10650 break;
10651
10652 case Instruction::And:
10653 if (match(BO.getOperand(1), m_APInt(C)))
10654 // 'and x, C' produces [0, C].
10655 Upper = *C + 1;
10656 // X & -X is a power of two or zero. So we can cap the value at max power of
10657 // two.
10658 if (match(BO.getOperand(0), m_Neg(m_Specific(BO.getOperand(1)))) ||
10659 match(BO.getOperand(1), m_Neg(m_Specific(BO.getOperand(0)))))
10660 Upper = APInt::getSignedMinValue(Width) + 1;
10661 break;
10662
10663 case Instruction::Or:
10664 if (match(BO.getOperand(1), m_APInt(C)))
10665 // 'or x, C' produces [C, UINT_MAX].
10666 Lower = *C;
10667 break;
10668
10669 case Instruction::AShr:
10670 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10671 // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C].
10673 Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1;
10674 } else if (match(BO.getOperand(0), m_APInt(C))) {
10675 unsigned ShiftAmount = Width - 1;
10676 if (!C->isZero() && IIQ.isExact(&BO))
10677 ShiftAmount = C->countr_zero();
10678 if (C->isNegative()) {
10679 // 'ashr C, x' produces [C, C >> (Width-1)]
10680 Lower = *C;
10681 Upper = C->ashr(ShiftAmount) + 1;
10682 } else {
10683 // 'ashr C, x' produces [C >> (Width-1), C]
10684 Lower = C->ashr(ShiftAmount);
10685 Upper = *C + 1;
10686 }
10687 }
10688 break;
10689
10690 case Instruction::LShr:
10691 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10692 // 'lshr x, C' produces [0, UINT_MAX >> C].
10693 Upper = APInt::getAllOnes(Width).lshr(*C) + 1;
10694 } else if (match(BO.getOperand(0), m_APInt(C))) {
10695 // 'lshr C, x' produces [C >> (Width-1), C].
10696 unsigned ShiftAmount = Width - 1;
10697 if (!C->isZero() && IIQ.isExact(&BO))
10698 ShiftAmount = C->countr_zero();
10699 Lower = C->lshr(ShiftAmount);
10700 Upper = *C + 1;
10701 }
10702 break;
10703
10704 case Instruction::Shl:
10705 if (match(BO.getOperand(0), m_APInt(C))) {
10706 if (IIQ.hasNoUnsignedWrap(&BO)) {
10707 // 'shl nuw C, x' produces [C, C << CLZ(C)]
10708 Lower = *C;
10709 Upper = Lower.shl(Lower.countl_zero()) + 1;
10710 } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw?
10711 if (C->isNegative()) {
10712 // 'shl nsw C, x' produces [C << CLO(C)-1, C]
10713 unsigned ShiftAmount = C->countl_one() - 1;
10714 Lower = C->shl(ShiftAmount);
10715 Upper = *C + 1;
10716 } else {
10717 // 'shl nsw C, x' produces [C, C << CLZ(C)-1]
10718 unsigned ShiftAmount = C->countl_zero() - 1;
10719 Lower = *C;
10720 Upper = C->shl(ShiftAmount) + 1;
10721 }
10722 } else {
10723 // If lowbit is set, value can never be zero.
10724 if ((*C)[0])
10725 Lower = APInt::getOneBitSet(Width, 0);
10726 // If we are shifting a constant the largest it can be is if the longest
10727 // sequence of consecutive ones is shifted to the highbits (breaking
10728 // ties for which sequence is higher). At the moment we take a liberal
10729 // upper bound on this by just popcounting the constant.
10730 // TODO: There may be a bitwise trick for it longest/highest
10731 // consecutative sequence of ones (naive method is O(Width) loop).
10732 Upper = APInt::getHighBitsSet(Width, C->popcount()) + 1;
10733 }
10734 } else if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10735 Upper = APInt::getBitsSetFrom(Width, C->getZExtValue()) + 1;
10736 }
10737 break;
10738
10739 case Instruction::SDiv:
10740 if (match(BO.getOperand(1), m_APInt(C))) {
10741 APInt IntMin = APInt::getSignedMinValue(Width);
10742 APInt IntMax = APInt::getSignedMaxValue(Width);
10743 if (C->isAllOnes()) {
10744 // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX]
10745 // where C != -1 and C != 0 and C != 1
10746 Lower = IntMin + 1;
10747 Upper = IntMax + 1;
10748 } else if (C->countl_zero() < Width - 1) {
10749 // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C]
10750 // where C != -1 and C != 0 and C != 1
10751 Lower = IntMin.sdiv(*C);
10752 Upper = IntMax.sdiv(*C);
10753 if (Lower.sgt(Upper))
10755 Upper = Upper + 1;
10756 assert(Upper != Lower && "Upper part of range has wrapped!");
10757 }
10758 } else if (match(BO.getOperand(0), m_APInt(C))) {
10759 if (C->isMinSignedValue()) {
10760 // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2].
10761 Lower = *C;
10762 Upper = Lower.lshr(1) + 1;
10763 } else {
10764 // 'sdiv C, x' produces [-|C|, |C|].
10765 Upper = C->abs() + 1;
10766 Lower = (-Upper) + 1;
10767 }
10768 }
10769 break;
10770
10771 case Instruction::UDiv:
10772 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
10773 // 'udiv x, C' produces [0, UINT_MAX / C].
10774 Upper = APInt::getMaxValue(Width).udiv(*C) + 1;
10775 } else if (match(BO.getOperand(0), m_APInt(C))) {
10776 // 'udiv C, x' produces [0, C].
10777 Upper = *C + 1;
10778 }
10779 break;
10780
10781 case Instruction::SRem:
10782 if (match(BO.getOperand(1), m_APInt(C))) {
10783 // 'srem x, C' produces (-|C|, |C|).
10784 Upper = C->abs();
10785 Lower = (-Upper) + 1;
10786 } else if (match(BO.getOperand(0), m_APInt(C))) {
10787 if (C->isNegative()) {
10788 // 'srem -|C|, x' produces [-|C|, 0].
10789 Upper = 1;
10790 Lower = *C;
10791 } else {
10792 // 'srem |C|, x' produces [0, |C|].
10793 Upper = *C + 1;
10794 }
10795 }
10796 break;
10797
10798 case Instruction::URem:
10799 if (match(BO.getOperand(1), m_APInt(C)))
10800 // 'urem x, C' produces [0, C).
10801 Upper = *C;
10802 else if (match(BO.getOperand(0), m_APInt(C)))
10803 // 'urem C, x' produces [0, C].
10804 Upper = *C + 1;
10805 break;
10806
10807 default:
10808 break;
10809 }
10810}
10811
10813 bool UseInstrInfo) {
10814 unsigned Width = II.getType()->getScalarSizeInBits();
10815 const APInt *C;
10816 switch (II.getIntrinsicID()) {
10817 case Intrinsic::ctlz:
10818 case Intrinsic::cttz: {
10819 APInt Upper(Width, Width);
10820 if (!UseInstrInfo || !match(II.getArgOperand(1), m_One()))
10821 Upper += 1;
10822 // Maximum of set/clear bits is the bit width.
10824 }
10825 case Intrinsic::ctpop:
10826 // Maximum of set/clear bits is the bit width.
10828 APInt(Width, Width) + 1);
10829 case Intrinsic::uadd_sat:
10830 // uadd.sat(x, C) produces [C, UINT_MAX].
10831 if (match(II.getOperand(0), m_APInt(C)) ||
10832 match(II.getOperand(1), m_APInt(C)))
10834 break;
10835 case Intrinsic::sadd_sat:
10836 if (match(II.getOperand(0), m_APInt(C)) ||
10837 match(II.getOperand(1), m_APInt(C))) {
10838 if (C->isNegative())
10839 // sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)].
10841 APInt::getSignedMaxValue(Width) + *C +
10842 1);
10843
10844 // sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].
10846 APInt::getSignedMaxValue(Width) + 1);
10847 }
10848 break;
10849 case Intrinsic::usub_sat:
10850 // usub.sat(C, x) produces [0, C].
10851 if (match(II.getOperand(0), m_APInt(C)))
10852 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
10853
10854 // usub.sat(x, C) produces [0, UINT_MAX - C].
10855 if (match(II.getOperand(1), m_APInt(C)))
10857 APInt::getMaxValue(Width) - *C + 1);
10858 break;
10859 case Intrinsic::ssub_sat:
10860 if (match(II.getOperand(0), m_APInt(C))) {
10861 if (C->isNegative())
10862 // ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)].
10864 *C - APInt::getSignedMinValue(Width) +
10865 1);
10866
10867 // ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].
10869 APInt::getSignedMaxValue(Width) + 1);
10870 } else if (match(II.getOperand(1), m_APInt(C))) {
10871 if (C->isNegative())
10872 // ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]:
10874 APInt::getSignedMaxValue(Width) + 1);
10875
10876 // ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].
10878 APInt::getSignedMaxValue(Width) - *C +
10879 1);
10880 }
10881 break;
10882 case Intrinsic::ushl_sat:
10883 if (match(II.getOperand(0), m_APInt(C)))
10885 break;
10886 case Intrinsic::sshl_sat:
10887 if (match(II.getOperand(0), m_APInt(C))) {
10888 if (C->isNegative())
10890 *C + 1);
10892 APInt::getSignedMaxValue(Width) + 1);
10893 }
10894 break;
10895 case Intrinsic::umin:
10896 case Intrinsic::umax:
10897 case Intrinsic::smin:
10898 case Intrinsic::smax:
10899 if (!match(II.getOperand(0), m_APInt(C)) &&
10900 !match(II.getOperand(1), m_APInt(C)))
10901 break;
10902
10903 switch (II.getIntrinsicID()) {
10904 case Intrinsic::umin:
10905 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
10906 case Intrinsic::umax:
10908 case Intrinsic::smin:
10910 *C + 1);
10911 case Intrinsic::smax:
10913 APInt::getSignedMaxValue(Width) + 1);
10914 default:
10915 llvm_unreachable("Must be min/max intrinsic");
10916 }
10917 break;
10918 case Intrinsic::abs:
10919 // If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX],
10920 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10921 if (match(II.getOperand(1), m_One()))
10923 APInt::getSignedMaxValue(Width) + 1);
10924
10926 APInt::getSignedMinValue(Width) + 1);
10927 case Intrinsic::vscale:
10928 if (!II.getParent() || !II.getFunction())
10929 break;
10930 return getVScaleRange(II.getFunction(), Width);
10931 case Intrinsic::read_register:
10932 case Intrinsic::read_volatile_register: {
10933 const Module *M = II.getModule();
10934 if (!M || !M->getTargetTriple().isRISCV())
10935 break;
10936 if (II.getFunction() && isReadVLENB(II))
10937 return getRISCVVLENBRange(II, Width);
10938 break;
10939 }
10940 default:
10941 break;
10942 }
10943
10944 return ConstantRange::getFull(Width);
10945}
10946
10948 const InstrInfoQuery &IIQ) {
10949 unsigned BitWidth = SI.getType()->getScalarSizeInBits();
10950 const Value *LHS = nullptr, *RHS = nullptr;
10952 if (R.Flavor == SPF_UNKNOWN)
10953 return ConstantRange::getFull(BitWidth);
10954
10955 if (R.Flavor == SelectPatternFlavor::SPF_ABS) {
10956 // If the negation part of the abs (in RHS) has the NSW flag,
10957 // then the result of abs(X) is [0..SIGNED_MAX],
10958 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10959 if (match(RHS, m_Neg(m_Specific(LHS))) &&
10963
10966 }
10967
10968 if (R.Flavor == SelectPatternFlavor::SPF_NABS) {
10969 // The result of -abs(X) is <= 0.
10971 APInt(BitWidth, 1));
10972 }
10973
10974 const APInt *C;
10975 if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C)))
10976 return ConstantRange::getFull(BitWidth);
10977
10978 switch (R.Flavor) {
10979 case SPF_UMIN:
10981 case SPF_UMAX:
10983 case SPF_SMIN:
10985 *C + 1);
10986 case SPF_SMAX:
10989 default:
10990 return ConstantRange::getFull(BitWidth);
10991 }
10992}
10993
10995 // The maximum representable value of a half is 65504. For floats the maximum
10996 // value is 3.4e38 which requires roughly 129 bits.
10997 unsigned BitWidth = I->getType()->getScalarSizeInBits();
10998 if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy())
10999 return;
11000 if (isa<FPToSIInst>(I) && BitWidth >= 17) {
11001 Lower = APInt(BitWidth, -65504, true);
11002 Upper = APInt(BitWidth, 65505);
11003 }
11004
11005 if (isa<FPToUIInst>(I) && BitWidth >= 16) {
11006 // For a fptoui the lower limit is left as 0.
11007 Upper = APInt(BitWidth, 65505);
11008 }
11009}
11010
11012 const SimplifyQuery &SQ,
11013 unsigned Depth) {
11014 assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction");
11015
11017 return ConstantRange::getFull(V->getType()->getScalarSizeInBits());
11018
11019 if (auto *C = dyn_cast<Constant>(V))
11020 return C->toConstantRange();
11021
11022 unsigned BitWidth = V->getType()->getScalarSizeInBits();
11023 ConstantRange CR = ConstantRange::getFull(BitWidth);
11024 if (auto *BO = dyn_cast<BinaryOperator>(V)) {
11025 APInt Lower = APInt(BitWidth, 0);
11026 APInt Upper = APInt(BitWidth, 0);
11027 // TODO: Return ConstantRange.
11028 setLimitsForBinOp(*BO, Lower, Upper, SQ.IIQ, ForSigned);
11030 } else if (auto *II = dyn_cast<IntrinsicInst>(V))
11032 else if (auto *SI = dyn_cast<SelectInst>(V)) {
11033 ConstantRange CRTrue =
11034 computeConstantRange(SI->getTrueValue(), ForSigned, SQ, Depth + 1);
11035 ConstantRange CRFalse =
11036 computeConstantRange(SI->getFalseValue(), ForSigned, SQ, Depth + 1);
11037 CR = CRTrue.unionWith(CRFalse, ForSigned ? ConstantRange::Signed
11040 } else if (auto *TI = dyn_cast<TruncInst>(V)) {
11041 ConstantRange SrcCR =
11042 computeConstantRange(TI->getOperand(0), ForSigned, SQ, Depth + 1);
11043 CR = SrcCR.truncate(BitWidth);
11044 } else if (auto *ZExt = dyn_cast<ZExtInst>(V)) {
11045 ConstantRange SrcCR =
11046 computeConstantRange(ZExt->getOperand(0), ForSigned, SQ, Depth + 1);
11047 CR = SrcCR.zeroExtend(BitWidth);
11048 } else if (auto *SExt = dyn_cast<SExtInst>(V)) {
11049 ConstantRange SrcCR =
11050 computeConstantRange(SExt->getOperand(0), ForSigned, SQ, Depth + 1);
11051 CR = SrcCR.signExtend(BitWidth);
11052 } else if (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {
11053 APInt Lower = APInt(BitWidth, 0);
11054 APInt Upper = APInt(BitWidth, 0);
11055 // TODO: Return ConstantRange.
11058 } else if (const auto *A = dyn_cast<Argument>(V))
11059 if (std::optional<ConstantRange> Range = A->getRange())
11060 CR = *Range;
11061
11062 if (auto *I = dyn_cast<Instruction>(V)) {
11063 if (auto *Range = SQ.IIQ.getMetadata(I, LLVMContext::MD_range))
11065
11066 Value *FrexpSrc;
11067 if (const auto *CB = dyn_cast<CallBase>(V)) {
11068 if (std::optional<ConstantRange> Range = CB->getRange())
11069 CR = CR.intersectWith(*Range);
11071 m_Value(FrexpSrc))))) {
11072 const fltSemantics &FltSem =
11073 FrexpSrc->getType()->getScalarType()->getFltSemantics();
11074 // It should be possible to implement this for any type, but this logic
11075 // only computes the range assuming standard subnormal handling.
11076 if (APFloat::isIEEELikeFP(FltSem)) {
11078 FrexpSrc, fcSubnormal | fcZero | fcNan | fcInf, SQ, Depth + 1);
11079
11080 // The exponent of frexp(NaN) and frexp(Inf) is unspecified. Only
11081 // constrain its range when the source can be neither.
11082 if (KnownSrc.isKnownNeverInfOrNaN()) {
11083 int MinExp = APFloat::semanticsMinExponent(FltSem) + 1;
11084
11085 // Offset to find the true minimum exponent value for a denormal.
11086 if (!KnownSrc.isKnownNeverSubnormal())
11087 MinExp -= (APFloat::semanticsPrecision(FltSem) - 1);
11088
11089 int MaxExp = APFloat::semanticsMaxExponent(FltSem) + 1;
11090
11091 auto [AdjustedMin, AdjustedMax, AdjustedMaxNonZero] =
11093
11094 DenormalMode Mode = I->getFunction()->getDenormalMode(FltSem);
11095 bool NeverLogicalZero = KnownSrc.isKnownNeverLogicalZero(Mode);
11096
11097 MinExp = std::max(AdjustedMin, MinExp);
11098 MaxExp = std::min(NeverLogicalZero ? AdjustedMaxNonZero : AdjustedMax,
11099 MaxExp);
11100
11102 APInt(BitWidth, static_cast<int64_t>(MinExp), /*isSigned=*/true),
11103 APInt(BitWidth, static_cast<int64_t>(MaxExp) + 1,
11104 /*isSigned=*/true));
11105 }
11106 }
11107 }
11108 }
11109
11110 if (SQ.CtxI && SQ.AC) {
11111 // Try to restrict the range based on information from assumptions.
11112 for (auto &AssumeVH : SQ.AC->assumptionsFor(V)) {
11113 if (!AssumeVH)
11114 continue;
11115 CallInst *I = cast<CallInst>(AssumeVH);
11116 assert(I->getParent()->getParent() == SQ.CtxI->getParent()->getParent() &&
11117 "Got assumption for the wrong function!");
11118 assert(I->getIntrinsicID() == Intrinsic::assume &&
11119 "must be an assume intrinsic");
11120
11121 if (AssumeVH.Index != AssumptionCache::ExprResultIdx) {
11122 auto OBU = I->getOperandBundleAt(AssumeVH.Index);
11123 if (getBundleAttrFromOBU(OBU) == BundleAttr::Range) {
11124 auto [Val, Range] = getAssumeRangeInfo(OBU);
11125 if (Val == V && isValidAssumeForContext(I, SQ))
11126 CR = CR.intersectWith(Range);
11127 }
11128 continue;
11129 }
11130
11131 if (!isValidAssumeForContext(I, SQ))
11132 continue;
11133 Value *Arg = I->getArgOperand(0);
11134 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
11135 // Currently we just use information from comparisons.
11136 if (!Cmp || Cmp->getOperand(0) != V)
11137 continue;
11138 // TODO: Set "ForSigned" parameter via Cmp->isSigned()?
11139 ConstantRange RHS =
11140 computeConstantRange(Cmp->getOperand(1), /*ForSigned=*/false,
11141 SQ.getWithInstruction(I), Depth + 1);
11142 CR = CR.intersectWith(
11143 ConstantRange::makeAllowedICmpRegion(Cmp->getCmpPredicate(), RHS));
11144 }
11145 }
11146
11147 return CR;
11148}
11149
11150static void
11152 function_ref<void(Value *)> InsertAffected) {
11153 assert(V != nullptr);
11154 if (isa<Argument>(V) || isa<GlobalValue>(V)) {
11155 InsertAffected(V);
11156 } else if (auto *I = dyn_cast<Instruction>(V)) {
11157 InsertAffected(V);
11158
11159 // Peek through unary operators to find the source of the condition.
11160 Value *Op;
11162 m_Trunc(m_Value(Op))))) {
11164 InsertAffected(Op);
11165 }
11166 }
11167}
11168
11170 Value *Cond, bool IsAssume, function_ref<void(Value *)> InsertAffected) {
11171 auto AddAffected = [&InsertAffected](Value *V) {
11172 addValueAffectedByCondition(V, InsertAffected);
11173 };
11174
11175 auto AddCmpOperands = [&AddAffected, IsAssume](Value *LHS, Value *RHS) {
11176 if (IsAssume) {
11177 AddAffected(LHS);
11178 AddAffected(RHS);
11179 } else if (match(RHS, m_Constant()))
11180 AddAffected(LHS);
11181 };
11182
11183 SmallVector<Value *, 8> Worklist;
11185 Worklist.push_back(Cond);
11186 while (!Worklist.empty()) {
11187 Value *V = Worklist.pop_back_val();
11188 if (!Visited.insert(V).second)
11189 continue;
11190
11191 CmpPredicate Pred;
11192 Value *A, *B, *X;
11193
11194 if (IsAssume) {
11195 AddAffected(V);
11196 if (match(V, m_Not(m_Value(X))))
11197 AddAffected(X);
11198 }
11199
11200 if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
11201 // assume(A && B) is split to -> assume(A); assume(B);
11202 // assume(!(A || B)) is split to -> assume(!A); assume(!B);
11203 // Finally, assume(A || B) / assume(!(A && B)) generally don't provide
11204 // enough information to be worth handling (intersection of information as
11205 // opposed to union).
11206 if (!IsAssume) {
11207 Worklist.push_back(A);
11208 Worklist.push_back(B);
11209 }
11210 } else if (match(V, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
11211 bool HasRHSC = match(B, m_ConstantInt());
11212 if (ICmpInst::isEquality(Pred)) {
11213 AddAffected(A);
11214 if (IsAssume)
11215 AddAffected(B);
11216 if (HasRHSC) {
11217 Value *Y;
11218 // (X << C) or (X >>_s C) or (X >>_u C).
11219 if (match(A, m_Shift(m_Value(X), m_ConstantInt())))
11220 AddAffected(X);
11221 // (X & C) or (X | C).
11222 else if (match(A, m_And(m_Value(X), m_Value(Y))) ||
11223 match(A, m_Or(m_Value(X), m_Value(Y)))) {
11224 AddAffected(X);
11225 AddAffected(Y);
11226 }
11227 // X - Y
11228 else if (match(A, m_Sub(m_Value(X), m_Value(Y)))) {
11229 AddAffected(X);
11230 AddAffected(Y);
11231 }
11232 }
11233 } else {
11234 AddCmpOperands(A, B);
11235 if (HasRHSC) {
11236 Value *Y;
11237 // Handle (A + C1) u< C2, which is the canonical form of
11238 // A > C3 && A < C4.
11240 AddAffected(X);
11241 // X - Y pred C, which implies X != Y if 0 pred C is false.
11242 else if (match(A, m_Sub(m_Value(X), m_Value(Y)))) {
11243 AddAffected(X);
11244 AddAffected(Y);
11245 }
11246
11247 if (ICmpInst::isUnsigned(Pred)) {
11248 // X & Y u> C -> X >u C && Y >u C
11249 // X | Y u< C -> X u< C && Y u< C
11250 // X nuw+ Y u< C -> X u< C && Y u< C
11251 if (match(A, m_And(m_Value(X), m_Value(Y))) ||
11252 match(A, m_Or(m_Value(X), m_Value(Y))) ||
11253 match(A, m_NUWAdd(m_Value(X), m_Value(Y)))) {
11254 AddAffected(X);
11255 AddAffected(Y);
11256 }
11257 }
11258 }
11259
11260 // Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported
11261 // by computeKnownFPClass().
11263 if (Pred == ICmpInst::ICMP_SLT && match(B, m_Zero()))
11264 InsertAffected(X);
11265 else if (Pred == ICmpInst::ICMP_SGT && match(B, m_AllOnes()))
11266 InsertAffected(X);
11267 }
11268 }
11269
11270 auto AddNuwSquareOperand = [&AddAffected](Value *Op) {
11271 Value *SquareOp = nullptr;
11272 if (match(Op, m_NUWMul(m_Value(SquareOp), m_Deferred(SquareOp))))
11273 AddAffected(SquareOp);
11274 };
11275 AddNuwSquareOperand(A);
11276 AddNuwSquareOperand(B);
11277
11278 if (HasRHSC && match(A, m_Ctpop(m_Value(X))))
11279 AddAffected(X);
11280 } else if (match(V, m_FCmp(Pred, m_Value(A), m_Value(B)))) {
11281 AddCmpOperands(A, B);
11282
11283 // fcmp fneg(x), y
11284 // fcmp fabs(x), y
11285 // fcmp fneg(fabs(x)), y
11286 // TODO: Confirm whether m_FNegOrFSubNegZero is correct.
11288 AddAffected(A);
11289 if (match(A, m_FAbs(m_Value(A))))
11290 AddAffected(A);
11291
11293 m_Value()))) {
11294 // Handle patterns that computeKnownFPClass() support.
11295 AddAffected(A);
11296 } else if (!IsAssume && match(V, m_Trunc(m_Value(X)))) {
11297 // Assume is checked here as X is already added above for assumes in
11298 // addValueAffectedByCondition
11299 AddAffected(X);
11300 } else if (!IsAssume && match(V, m_Not(m_Value(X)))) {
11301 // Assume is checked here to avoid issues with ephemeral values
11302 Worklist.push_back(X);
11303 }
11304 }
11305}
11306
11308 // (X >> C) or/add (X & mask(C) != 0)
11309 if (const auto *BO = dyn_cast<BinaryOperator>(V)) {
11310 if (BO->getOpcode() == Instruction::Add ||
11311 BO->getOpcode() == Instruction::Or) {
11312 const Value *X;
11313 const APInt *C1, *C2;
11314 if (match(BO, m_c_BinOp(m_LShr(m_Value(X), m_APInt(C1)),
11318 m_Zero())))) &&
11319 C2->popcount() == C1->getZExtValue())
11320 return X;
11321 }
11322 }
11323
11324 // select(cond, X, -X)
11325 {
11326 const Value *X;
11327 if (match(V, m_c_Select(m_Value(X), m_Neg(m_Deferred(X)))))
11328 return X;
11329 }
11330
11331 return nullptr;
11332}
11333
11335 return const_cast<Value *>(stripNullTest(const_cast<const Value *>(V)));
11336}
11337
11340 unsigned MaxCount, bool AllowUndefOrPoison) {
11343 auto Push = [&](const Value *V) -> bool {
11344 Constant *C;
11345 if (match(const_cast<Value *>(V), m_ImmConstant(C))) {
11346 if (!AllowUndefOrPoison && !isGuaranteedNotToBeUndefOrPoison(C))
11347 return false;
11348 // Check existence first to avoid unnecessary allocations.
11349 if (Constants.contains(C))
11350 return true;
11351 if (Constants.size() == MaxCount)
11352 return false;
11353 Constants.insert(C);
11354 return true;
11355 }
11356
11357 if (auto *Inst = dyn_cast<Instruction>(V)) {
11358 if (Visited.insert(Inst).second)
11359 Worklist.push_back(Inst);
11360 return true;
11361 }
11362 return false;
11363 };
11364 if (!Push(V))
11365 return false;
11366 while (!Worklist.empty()) {
11367 const Instruction *CurInst = Worklist.pop_back_val();
11368 switch (CurInst->getOpcode()) {
11369 case Instruction::Select:
11370 if (!Push(CurInst->getOperand(1)))
11371 return false;
11372 if (!Push(CurInst->getOperand(2)))
11373 return false;
11374 break;
11375 case Instruction::PHI:
11376 for (Value *IncomingValue : cast<PHINode>(CurInst)->incoming_values()) {
11377 // Fast path for recurrence PHI.
11378 if (IncomingValue == CurInst)
11379 continue;
11380 if (!Push(IncomingValue))
11381 return false;
11382 }
11383 break;
11384 default:
11385 return false;
11386 }
11387 }
11388 return true;
11389}
const TargetLowering & TLI
SDValue Pred
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
AMDGPU Register Bank Select
Rewrite undef for PHI
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...
bpf unwind BPF Unwind Lower
static const Function * getParent(const Value *V)
#define X(NUM, ENUM, NAME)
Definition ELF.h:665
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
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 RegName(no)
#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
SmallVector< MachineOperand, 4 > Cond
int64_t C1
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 scope_exit class, which executes user-defined cleanup logic at scope exit.
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 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 const Instruction * safeCtxI(const Value *V, const Instruction *CtxI)
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR)
Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
static void addValueAffectedByCondition(Value *V, function_ref< void(Value *)> InsertAffected)
static bool isNonZeroAddrPlusOffset(const SimplifyQuery &Q, Value *X, Value *Y)
Return true if "addr(P) + Off" cannot be zero.
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 bool hasNoFreeInRange(BasicBlock::const_iterator Begin, BasicBlock::const_iterator End, unsigned &NumChecked)
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 ConstantRange getRISCVVLENBRange(const IntrinsicInst &II, unsigned Width)
Return the value range of a RISC-V vlenb CSR read.
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 SelectPatternResult matchMinMax(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS)
Match non-obvious integer minimum and maximum sequences.
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 void computeKnownFPClassFromCond(const Value *V, Value *Cond, bool CondIsTrue, const Instruction *CtxI, KnownFPClass &KnownFromContext, unsigned Depth=0)
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 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 bool isReadVLENB(const IntrinsicInst &II)
Return true if II reads a register named "vlenb".
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 void computeKnownBitsFromEuclideanRem(const Value *Op0, const Value *Op1, KnownBits &KnownOut, const SimplifyQuery &Q, unsigned Depth)
Infer non-negativity for R + (R < 0 ?
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 void computeKnownBitsForRecurrenceOperands(const PHINode *P, Value *Start, Value *Step, const APInt &DemandedElts, KnownBits &KnownStart, KnownBits &KnownStep, const SimplifyQuery &Q, unsigned Depth)
static void computeKnownFPClassForFAddSub(const Operator *Op, const APInt &DemandedElts, FPClassTest InterestedClasses, KnownFPClass &Known, const SimplifyQuery &Q, unsigned Depth, bool IsAdd, bool IsStrict)
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 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 bool isNonEqualURem(const Value *X, const Value *Rem, const SimplifyQuery &Q)
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 void computeKnownFPClassForFRem(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 bool semanticsHasInf(const fltSemantics &)
Definition APFloat.cpp:368
static LLVM_ABI ExponentType semanticsMinExponent(const fltSemantics &)
Definition APFloat.cpp:343
static LLVM_ABI bool semanticsHasSignedRepr(const fltSemantics &)
Definition APFloat.cpp:364
static LLVM_ABI ExponentType semanticsMaxExponent(const fltSemantics &)
Definition APFloat.cpp:339
static LLVM_ABI unsigned int semanticsPrecision(const fltSemantics &)
Definition APFloat.cpp:335
static LLVM_ABI bool semanticsHasNaN(const fltSemantics &)
Definition APFloat.cpp:372
static LLVM_ABI bool semanticsHasZero(const fltSemantics &)
Definition APFloat.cpp:360
static LLVM_ABI bool isRepresentableAsNormalIn(const fltSemantics &Src, const fltSemantics &Dst)
Definition APFloat.cpp:385
static LLVM_ABI bool isIEEELikeFP(const fltSemantics &)
Definition APFloat.cpp:376
static LLVM_ABI const fltSemantics * getArbitraryFPSemantics(StringRef Format)
Returns the fltSemantics for a given arbitrary FP format string, or nullptr if invalid.
Definition APFloat.cpp:6340
LLVM_READONLY int getExactLog2Abs() const
Definition APFloat.h:1654
bool isFinite() const
Definition APFloat.h:1588
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1242
bool isInteger() const
Definition APFloat.h:1600
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:2018
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1602
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:230
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition APInt.h:1426
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition APInt.h:419
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1560
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition APInt.h:1411
unsigned popcount() const
Count the number of bits set.
Definition APInt.h:1690
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1405
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition APInt.h:202
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition APInt.h:1350
unsigned ceilLogBase2() const
Definition APInt.h:1784
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition APInt.h:367
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:376
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1508
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1115
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:205
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition APInt.h:212
bool isNegative() const
Determine sign of this APInt.
Definition APInt.h:325
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:1253
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1673
LLVM_ABI APInt reverseBits() const
Definition APInt.cpp:786
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition APInt.h:1170
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1648
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition APInt.h:1618
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:215
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1086
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition APInt.h:352
unsigned logBase2() const
Definition APInt.h:1781
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:829
bool getBoolValue() const
Convert APInt to a boolean value.
Definition APInt.h:467
bool isMaxSignedValue() const
Determine if this is the largest signed value.
Definition APInt.h:401
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:330
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition APInt.h:1154
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:875
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition APInt.h:1261
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition APInt.h:292
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:196
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition APInt.h:1408
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1241
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:282
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:235
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:853
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1225
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:107
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:263
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator end()
Definition BasicBlock.h:453
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:440
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 bool isEntryBlock() const
Return true if this is the entry block of the containing function.
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
static LLVM_ABI Predicate getFlippedStrictnessPredicate(Predicate pred)
This is a static version that you can use without an instruction available.
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
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 ConstantRange zeroExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
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.
LLVM_ABI ConstantRange signExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
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 multiply(const ConstantRange &Other, unsigned NoWrapKind=0) const
Return a new range representing the possible values resulting from a multiplication of a value in thi...
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 ConstantRange binaryAnd(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a binary-and of a value in this ra...
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 APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
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:44
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:65
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:220
unsigned getAddressSizeInBits(unsigned AS) const
The size in bits of an address in for the given AS.
Definition DataLayout.h:521
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:794
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:122
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:803
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...
iterator_range< user_iterator > users()
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.
bool isKnownNeverInfOrNaN() const
Return true if it's known this can never be an infinity or nan.
static LLVM_ABI KnownFPClass atan2(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode)
Report known values for atan2.
static LLVM_ABI KnownFPClass fma(const KnownFPClass &LHS, const KnownFPClass &RHS, const KnownFPClass &Addend, DenormalMode Mode)
Report known values for fma.
static LLVM_ABI KnownFPClass sin(const KnownFPClass &Src)
Report known values for sin.
static constexpr FPClassTest OrderedLessThanZeroMask
static LLVM_ABI KnownFPClass trunc(const KnownFPClass &Src, DenormalMode Mode)
Propagate known class for trunc.
static LLVM_ABI KnownFPClass fadd(const KnownFPClass &LHS, const KnownFPClass &RHS, RoundingMode RM, DenormalMode Mode)
Report known values for fadd.
bool isKnownNeverSubnormal() const
Return true if it's known this can never be a subnormal.
static LLVM_ABI KnownFPClass fsub(const KnownFPClass &LHS, const KnownFPClass &RHS, RoundingMode RM, DenormalMode Mode)
Report known values for fsub.
static LLVM_ABI KnownFPClass fma_square(const KnownFPClass &Squared, const KnownFPClass &Addend, DenormalMode Mode)
Report known values for fma squared, squared, addend.
KnownFPClass unionWith(const KnownFPClass &RHS) const
static LLVM_ABI KnownFPClass fadd_self(const KnownFPClass &Src, RoundingMode RM, DenormalMode Mode)
Report known values for fadd x, x.
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 frem(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode)
Report known values for frem.
static LLVM_ABI KnownFPClass atan(const KnownFPClass &Src)
Report known values for atan.
static LLVM_ABI KnownFPClass ldexp(const KnownFPClass &Src, const APInt &ConstantRangeMin, const APInt &ConstantRangeMax, const fltSemantics &Flt, DenormalMode Mode)
Propagate known class for ldexp, assuming the exponent is known to be within [ConstantRangeMin,...
static LLVM_ABI KnownFPClass frexp_mant(const KnownFPClass &Src, DenormalMode Mode)
Propagate known class for mantissa component of frexp.
static LLVM_ABI KnownFPClass roundToIntegral(const KnownFPClass &Src, bool IsMultiUnitFPType, DenormalMode Mode)
Propagate known class for rounding intrinsics (rint, nearbyint, round, roundeven).
static LLVM_ABI KnownFPClass fmuladd(const KnownFPClass &LHS, const KnownFPClass &RHS, const KnownFPClass &Addend, DenormalMode Mode)
Report known values for fmuladd.
static LLVM_ABI KnownFPClass ceil(const KnownFPClass &Src, bool IsMultiUnitFPType, DenormalMode Mode)
Propagate known class for ceil.
static LLVM_ABI KnownFPClass fmuladd_square(const KnownFPClass &Squared, const KnownFPClass &Addend, DenormalMode Mode)
Report known values for fmuladd squared, squared, addend.
static LLVM_ABI KnownFPClass fpext(const KnownFPClass &KnownSrc, const fltSemantics &DstTy, const fltSemantics &SrcTy, DenormalMode DstMode, DenormalMode SrcMode)
Propagate known class for fpext.
static LLVM_ABI KnownFPClass cos(const KnownFPClass &Src)
Report known values for cos.
static LLVM_ABI KnownFPClass fptrunc(const KnownFPClass &KnownSrc, const fltSemantics &DstTy, const fltSemantics &SrcTy, DenormalMode DstMode, DenormalMode SrcMode)
Propagate known class for fptrunc and fptrunc_round.
static LLVM_ABI KnownFPClass cosh(const KnownFPClass &Src)
Report known values for cosh.
bool isUnknown() const
static LLVM_ABI KnownFPClass exp(const KnownFPClass &Src)
Report known values for exp, exp2 and exp10.
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.
static LLVM_ABI KnownFPClass minMaxLike(const KnownFPClass &LHS, const KnownFPClass &RHS, MinMaxKind Kind, DenormalMode Mode)
void knownNot(FPClassMask RuleOut)
static LLVM_ABI KnownFPClass canonicalize(const KnownFPClass &Src, const fltSemantics &Flt, DenormalMode Mode)
Apply the canonicalize intrinsic to this value.
std::optional< bool > getSignBit() const
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 floor(const KnownFPClass &Src, bool IsMultiUnitFPType, DenormalMode Mode)
Propagate known class for floor.
FPClassTest getKnownFPClasses() const
Floating-point classes the value could be one of.
static KnownFPClass getEmpty()
static LLVM_ABI KnownFPClass tan(const KnownFPClass &Src)
Report known values for tan.
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.
static LLVM_ABI KnownFPClass fdiv_self(const KnownFPClass &Src, DenormalMode Mode)
Report known values for fdiv x, x.
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 isKnownNever(FPClassMask Mask) const
Return true if it's known this can never be one of the mask entries.
bool isKnownNeverPosInfinity() const
Return true if it's known this can never be +infinity.
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 square(const KnownFPClass &Src, DenormalMode Mode)
static LLVM_ABI KnownFPClass acos(const KnownFPClass &Src)
Report known values for acos.
static LLVM_ABI KnownFPClass log(const KnownFPClass &Src, DenormalMode Mode, bool IsMultiUnitFPType)
Propagate known class for log/log2/log10.
static LLVM_ABI KnownFPClass frem_self(const KnownFPClass &Src, DenormalMode Mode)
Report known values for frem x, x. Valid for all rounding modes.
static LLVM_ABI KnownFPClass powi(const KnownFPClass &Src, const KnownBits &N)
Propagate known class for powi.
static LLVM_ABI KnownFPClass fdiv(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode)
Report known values for fdiv.
static LLVM_ABI KnownFPClass fmul(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode)
Report known values for fmul.
static LLVM_ABI KnownFPClass pow(const KnownFPClass &LHS, const KnownFPClass &RHS)
Propagate known class for pow.
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.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:76
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:1087
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1443
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
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.
unsigned getAddressSpace() const
Return the address space of the Pointer type.
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
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:589
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition DataLayout.h:746
TypeSize getElementOffset(unsigned Idx) const
Definition DataLayout.h:777
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.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:283
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition Type.h:258
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:277
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
Definition Type.h:155
LLVM_ABI uint64_t getArrayNumElements() const
bool isSized() const
Return true if it makes sense to take the size of this type.
Definition Type.h:321
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.h:354
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:292
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:370
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:187
bool isHalfTy() const
Return true if this is 'half', a 16-bit IEEE fp type.
Definition Type.h:144
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:280
bool isIntOrPtrTy() const
Return true if this is an integer type or a pointer type.
Definition Type.h:265
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:252
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition Type.cpp:298
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
Definition Type.h:222
LLVM_ABI const fltSemantics & getFltSemantics() const
Definition Type.cpp:96
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:35
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:257
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:729
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:201
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:169
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:166
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:3052
const APInt & umax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be unsigned.
Definition APInt.h:2289
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.
AllOnesConstantMatch m_AllOnes()
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_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)
CastOperator_match< OpTy, Instruction::PtrToAddr > m_PtrToAddr(const OpTy &Op)
Matches PtrToAddr.
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().
ThreeOps_match< decltype(m_Value()), LHS, RHS, Instruction::Select, true > m_c_Select(const LHS &L, const RHS &R)
Match Select(C, LHS, RHS) or Select(C, RHS, LHS)
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.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Mul, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWMul(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::UDiv > m_UDiv(const LHS &L, const RHS &R)
is_zero< true > m_Zero()
Match any null constant or a vector with all elements equal to 0.
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)
FNeg_match< OpTy, true > m_FNegOrFSubNegZero(const OpTy &X)
Match fneg X or fsub -0.0, X.
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)
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.
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
static constexpr unsigned RVVBytesPerBlock
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:680
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)
@ Offset
Definition DWP.cpp:577
@ Length
Definition DWP.cpp:577
@ 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:1755
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:1685
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 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
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
@ Load
The value being inserted comes from a load (InsertElement only).
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 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 const Value * getArgumentAliasingToReturnedPointer(const CallBase *Call, bool MustPreserveOffset, bool MustPreserveProvenance=false)
This function returns call pointer argument that is considered the same by aliasing rules.
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:2229
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:244
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CtxI=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 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:452
@ O1
Optimize quickly without destroying debuggability.
@ O2
Optimize for fast execution as much as possible without triggering significant incremental compile ti...
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition MathExtras.h:285
gep_type_iterator gep_type_end(const User *GEP)
int ilogb(const APFloat &Arg)
Returns the exponent of the internal representation of the APFloat.
Definition APFloat.h:1707
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 isKnownNegative(const WithCache< const Value * > &V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be negative (i.e.
LLVM_ABI unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CtxI=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.
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.
LLVM_ABI unsigned ComputeMaxSignificantBits(const Value *Op, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Get the upper bound on bit size for this Value Op as a signed integer.
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 isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, AssumptionCache *AC=nullptr, const Instruction *CtxI=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 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,...
LLVM_ABI bool isValidAssumeForContext(const Instruction *I, const Instruction *CtxI, const DominatorTree *DT=nullptr, bool AllowEphemerals=false)
Return true if it is valid to use the assumptions provided by an assume intrinsic,...
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:1762
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:326
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....
int countl_zero(T Val)
Count number of 0's from the most significant bit to the least stopping at the first 1.
Definition bit.h:263
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 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.
FPOp
The floating-point operations supported by the folding helpers.
Definition FPFold.h:25
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:280
LLVM_ABI bool isKnownPositive(const WithCache< const Value * > &V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be positive (i.e.
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 bool programUndefinedIfPoison(const Instruction *Inst)
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.
constexpr unsigned MaxLookupSearchDepth
The max limit of the search depth in DecomposeGEPExpression() and getUnderlyingObject().
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 const Value * getUnderlyingObjectAggressive(const Value *V, bool MustPreserveProvenance=false)
Like getUnderlyingObject(), but will try harder to find a single underlying object.
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth, bool MustPreserveProvenance=false)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
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 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 Intrinsic::ID getMinMaxIntrinsic(SelectPatternFlavor SPF)
Convert given SPF to equivalent min/max intrinsic.
bool includesUndef(UndefPoisonKind Kind)
Returns true if Kind includes the Undef bit.
Definition UndefPoison.h:33
LLVM_ABI SelectPatternResult matchDecomposedSelectPattern(CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, FastMathFlags FMF=FastMathFlags(), Instruction::CastOps *CastOp=nullptr)
Determine the pattern that a select with the given compare as its predicate and given values as its t...
LLVM_ABI bool isOnlyUsedInZeroComparison(const Instruction *CtxI)
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 bool isOnlyUsedInZeroEqualityComparison(const Instruction *CtxI)
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 isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(const CallBase *Call, bool MustPreserveOffset, bool MustPreserveProvenance=false)
launder.invariant.group and similar intrinsics return a pointer that aliases their argument,...
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:246
RoundingMode
Rounding mode.
@ NearestTiesToEven
roundTiesToEven.
@ Dynamic
Denotes mode unknown at compile time.
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 bool willNotFreeBetween(const Instruction *Assume, const Instruction *CtxI, const DominatorTree *DT=nullptr)
Returns true, if no instruction between Assume and CtxI may free (including through synchronization).
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.
LLVM_ABI AssumeRangeInfo getAssumeRangeInfo(OperandBundleUse)
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...
auto predecessors(const MachineBasicBlock *BB)
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...
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1963
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)
bool pred_empty(const BasicBlock *BB)
Definition CFG.h:107
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:202
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.
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:320
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 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 SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
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:881
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...
LLVM_ABI KnownBits ctlz(bool ZeroIsPoison=false) const
Compute known bits for the count of leading zeros.
KnownBits byteSwap() const
Definition KnownBits.h:562
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:566
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:62
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
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
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 DominatorTree * DT
SimplifyQuery getWithInstruction(const Instruction *I) const
AssumptionCache * AC
const DomConditionCache * DC
const Instruction * CtxI
const Function * getFunction() const
const InstrInfoQuery IIQ
const CondContext * CC
fltNanEncoding nanEncoding
Definition APFloat.h:1041