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 // If we have a known 1, its position is our upper bound.
2282 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
2283 // If this call is poison for 0 input, the result will be less than 2^n.
2284 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
2285 PossibleLZ = std::min(PossibleLZ, BitWidth - 1);
2286 unsigned LowBits = llvm::bit_width(PossibleLZ);
2287 Known.Zero.setBitsFrom(LowBits);
2288 break;
2289 }
2290 case Intrinsic::cttz: {
2291 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2292 // If we have a known 1, its position is our upper bound.
2293 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
2294 // If this call is poison for 0 input, the result will be less than 2^n.
2295 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
2296 PossibleTZ = std::min(PossibleTZ, BitWidth - 1);
2297 unsigned LowBits = llvm::bit_width(PossibleTZ);
2298 Known.Zero.setBitsFrom(LowBits);
2299 break;
2300 }
2301 case Intrinsic::ctpop: {
2302 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2303 // We can bound the space the count needs. Also, bits known to be zero
2304 // can't contribute to the population.
2305 unsigned BitsPossiblySet = Known2.countMaxPopulation();
2306 unsigned LowBits = llvm::bit_width(BitsPossiblySet);
2307 Known.Zero.setBitsFrom(LowBits);
2308 // TODO: we could bound KnownOne using the lower bound on the number
2309 // of bits which might be set provided by popcnt KnownOne2.
2310 break;
2311 }
2312 case Intrinsic::fshr:
2313 case Intrinsic::fshl: {
2314 const APInt *SA;
2315 if (!match(I->getOperand(2), m_APInt(SA)))
2316 break;
2317
2318 KnownBits Known3(BitWidth);
2319 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2320 computeKnownBits(I->getOperand(1), DemandedElts, Known3, Q, Depth + 1);
2321 Known = II->getIntrinsicID() == Intrinsic::fshl
2322 ? KnownBits::fshl(Known2, Known3, *SA)
2323 : KnownBits::fshr(Known2, Known3, *SA);
2324 break;
2325 }
2326 case Intrinsic::clmul:
2327 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2328 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2329 Known = KnownBits::clmul(Known, Known2);
2330 break;
2331 case Intrinsic::pext:
2332 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2333 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2334 Known = KnownBits::pext(Known, Known2);
2335 break;
2336 case Intrinsic::pdep:
2337 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2338 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2339 Known = KnownBits::pdep(Known, Known2);
2340 break;
2341 case Intrinsic::smulh:
2342 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2343 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2344 Known = KnownBits::mulhs(Known, Known2);
2345 break;
2346 case Intrinsic::umulh:
2347 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2348 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2349 Known = KnownBits::mulhu(Known, Known2);
2350 break;
2351 case Intrinsic::uadd_sat:
2352 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2353 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2354 Known = KnownBits::uadd_sat(Known, Known2);
2355 break;
2356 case Intrinsic::usub_sat:
2357 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2358 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2359 Known = KnownBits::usub_sat(Known, Known2);
2360 break;
2361 case Intrinsic::sadd_sat:
2362 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2363 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2364 Known = KnownBits::sadd_sat(Known, Known2);
2365 break;
2366 case Intrinsic::ssub_sat:
2367 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2368 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2369 Known = KnownBits::ssub_sat(Known, Known2);
2370 break;
2371 // Vec reverse preserves bits from input vec.
2372 case Intrinsic::vector_reverse:
2373 computeKnownBits(I->getOperand(0), DemandedElts.reverseBits(), Known, Q,
2374 Depth + 1);
2375 break;
2376 // for min/max/and/or reduce, any bit common to each element in the
2377 // input vec is set in the output.
2378 case Intrinsic::vector_reduce_and:
2379 case Intrinsic::vector_reduce_or:
2380 case Intrinsic::vector_reduce_umax:
2381 case Intrinsic::vector_reduce_umin:
2382 case Intrinsic::vector_reduce_smax:
2383 case Intrinsic::vector_reduce_smin:
2384 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2385 break;
2386 case Intrinsic::vector_reduce_xor: {
2387 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2388 // The zeros common to all vecs are zero in the output.
2389 // If the number of elements is odd, then the common ones remain. If the
2390 // number of elements is even, then the common ones becomes zeros.
2391 auto *VecTy = cast<VectorType>(I->getOperand(0)->getType());
2392 // Even, so the ones become zeros.
2393 bool EvenCnt = VecTy->getElementCount().isKnownEven();
2394 if (EvenCnt)
2395 Known.Zero |= Known.One;
2396 // Maybe even element count so need to clear ones.
2397 if (VecTy->isScalableTy() || EvenCnt)
2398 Known.One.clearAllBits();
2399 break;
2400 }
2401 case Intrinsic::vector_reduce_add: {
2402 auto *VecTy = dyn_cast<FixedVectorType>(I->getOperand(0)->getType());
2403 if (!VecTy)
2404 break;
2405 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2406 Known = Known.reduceAdd(VecTy->getNumElements());
2407 break;
2408 }
2409 case Intrinsic::umin:
2410 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2411 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2412 Known = KnownBits::umin(Known, Known2);
2413 break;
2414 case Intrinsic::umax:
2415 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2416 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2417 Known = KnownBits::umax(Known, Known2);
2418 break;
2419 case Intrinsic::smin:
2420 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2421 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2422 Known = KnownBits::smin(Known, Known2);
2424 break;
2425 case Intrinsic::smax:
2426 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2427 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2428 Known = KnownBits::smax(Known, Known2);
2430 break;
2431 case Intrinsic::ptrmask: {
2432 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2433
2434 const Value *Mask = I->getOperand(1);
2435 Known2 = KnownBits(Mask->getType()->getScalarSizeInBits());
2436 computeKnownBits(Mask, DemandedElts, Known2, Q, Depth + 1);
2437 // TODO: 1-extend would be more precise.
2438 Known &= Known2.anyextOrTrunc(BitWidth);
2439 break;
2440 }
2441 case Intrinsic::x86_sse42_crc32_64_64:
2442 Known.Zero.setBitsFrom(32);
2443 break;
2444 case Intrinsic::x86_ssse3_phadd_d_128:
2445 case Intrinsic::x86_ssse3_phadd_w_128:
2446 case Intrinsic::x86_avx2_phadd_d:
2447 case Intrinsic::x86_avx2_phadd_w: {
2449 I, DemandedElts, Q, Depth,
2450 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2451 return KnownBits::add(KnownLHS, KnownRHS);
2452 });
2453 break;
2454 }
2455 case Intrinsic::x86_ssse3_phadd_sw_128:
2456 case Intrinsic::x86_avx2_phadd_sw: {
2458 I, DemandedElts, Q, Depth, KnownBits::sadd_sat);
2459 break;
2460 }
2461 case Intrinsic::x86_ssse3_phsub_d_128:
2462 case Intrinsic::x86_ssse3_phsub_w_128:
2463 case Intrinsic::x86_avx2_phsub_d:
2464 case Intrinsic::x86_avx2_phsub_w: {
2466 I, DemandedElts, Q, Depth,
2467 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2468 return KnownBits::sub(KnownLHS, KnownRHS);
2469 });
2470 break;
2471 }
2472 case Intrinsic::x86_ssse3_phsub_sw_128:
2473 case Intrinsic::x86_avx2_phsub_sw: {
2475 I, DemandedElts, Q, Depth, KnownBits::ssub_sat);
2476 break;
2477 }
2478 case Intrinsic::riscv_vsetvli:
2479 case Intrinsic::riscv_vsetvlimax: {
2480 bool HasAVL = II->getIntrinsicID() == Intrinsic::riscv_vsetvli;
2481 const ConstantRange Range = getVScaleRange(II->getFunction(), BitWidth);
2483 cast<ConstantInt>(II->getArgOperand(HasAVL))->getZExtValue());
2484 RISCVVType::VLMUL VLMUL = static_cast<RISCVVType::VLMUL>(
2485 cast<ConstantInt>(II->getArgOperand(1 + HasAVL))->getZExtValue());
2486 uint64_t MaxVLEN =
2487 Range.getUnsignedMax().getZExtValue() * RISCV::RVVBitsPerBlock;
2488 uint64_t MaxVL = MaxVLEN / RISCVVType::getSEWLMULRatio(SEW, VLMUL);
2489
2490 // Result of vsetvli must be not larger than AVL.
2491 if (HasAVL)
2492 if (auto *CI = dyn_cast<ConstantInt>(II->getArgOperand(0)))
2493 MaxVL = std::min(MaxVL, CI->getZExtValue());
2494
2495 unsigned KnownZeroFirstBit = Log2_32(MaxVL) + 1;
2496 if (BitWidth > KnownZeroFirstBit)
2497 Known.Zero.setBitsFrom(KnownZeroFirstBit);
2498 break;
2499 }
2500 case Intrinsic::amdgcn_mbcnt_hi:
2501 case Intrinsic::amdgcn_mbcnt_lo: {
2502 // Wave64 mbcnt_lo returns at most 32 + src1. Otherwise these return at
2503 // most 31 + src1.
2504 KnownBits MbcntKnown(BitWidth);
2505 MbcntKnown.Zero.setBitsFrom(
2506 II->getIntrinsicID() == Intrinsic::amdgcn_mbcnt_lo ? 6 : 5);
2507 computeKnownBits(I->getOperand(1), Known2, Q, Depth + 1);
2508 Known = Known.unionWith(KnownBits::add(MbcntKnown, Known2));
2509 break;
2510 }
2511 case Intrinsic::vscale: {
2512 if (!II->getParent() || !II->getFunction())
2513 break;
2514
2515 Known = getVScaleRange(II->getFunction(), BitWidth).toKnownBits();
2516 break;
2517 }
2518 case Intrinsic::stepvector: {
2519 auto *VecTy = cast<VectorType>(II->getType());
2520 unsigned MinNumElts = VecTy->getElementCount().getKnownMinValue();
2521 if (!isUIntN(BitWidth, MinNumElts))
2522 break;
2523
2524 bool Overflow = false;
2525 APInt MaxNumElts(BitWidth, MinNumElts);
2526 if (VecTy->isScalableTy()) {
2527 if (!II->getParent() || !II->getFunction())
2528 break;
2529 MaxNumElts = getVScaleRange(II->getFunction(), BitWidth)
2531 .umul_ov(MaxNumElts, Overflow);
2532 }
2533
2534 // Give up if the lane count could wrap. Stepvector truncates lane
2535 // indices that do not fit in the element type.
2536 if (Overflow)
2537 break;
2538
2539 Known.Zero.setHighBits((MaxNumElts - 1).countl_zero());
2540 break;
2541 }
2542 }
2543 }
2544 break;
2545 }
2546 case Instruction::ShuffleVector: {
2547 if (auto *Splat = getSplatValue(I)) {
2549 break;
2550 }
2551
2552 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
2553 // FIXME: Do we need to handle ConstantExpr involving shufflevectors?
2554 if (!Shuf) {
2555 Known.resetAll();
2556 return;
2557 }
2558 // For undef elements, we don't know anything about the common state of
2559 // the shuffle result.
2560 APInt DemandedLHS, DemandedRHS;
2561 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) {
2562 Known.resetAll();
2563 return;
2564 }
2565 Known.setAllConflict();
2566 if (!!DemandedLHS) {
2567 const Value *LHS = Shuf->getOperand(0);
2568 computeKnownBits(LHS, DemandedLHS, Known, Q, Depth + 1);
2569 // If we don't know any bits, early out.
2570 if (Known.isUnknown())
2571 break;
2572 }
2573 if (!!DemandedRHS) {
2574 const Value *RHS = Shuf->getOperand(1);
2575 computeKnownBits(RHS, DemandedRHS, Known2, Q, Depth + 1);
2576 Known = Known.intersectWith(Known2);
2577 }
2578 break;
2579 }
2580 case Instruction::InsertElement: {
2581 if (isa<ScalableVectorType>(I->getType())) {
2582 Known.resetAll();
2583 return;
2584 }
2585 const Value *Vec = I->getOperand(0);
2586 const Value *Elt = I->getOperand(1);
2587 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
2588 unsigned NumElts = DemandedElts.getBitWidth();
2589 APInt DemandedVecElts = DemandedElts;
2590 bool NeedsElt = true;
2591 // If we know the index we are inserting too, clear it from Vec check.
2592 if (CIdx && CIdx->getValue().ult(NumElts)) {
2593 DemandedVecElts.clearBit(CIdx->getZExtValue());
2594 NeedsElt = DemandedElts[CIdx->getZExtValue()];
2595 }
2596
2597 Known.setAllConflict();
2598 if (NeedsElt) {
2599 computeKnownBits(Elt, Known, Q, Depth + 1);
2600 // If we don't know any bits, early out.
2601 if (Known.isUnknown())
2602 break;
2603 }
2604
2605 if (!DemandedVecElts.isZero()) {
2606 computeKnownBits(Vec, DemandedVecElts, Known2, Q, Depth + 1);
2607 Known = Known.intersectWith(Known2);
2608 }
2609 break;
2610 }
2611 case Instruction::ExtractElement: {
2612 // Look through extract element. If the index is non-constant or
2613 // out-of-range demand all elements, otherwise just the extracted element.
2614 const Value *Vec = I->getOperand(0);
2615 const Value *Idx = I->getOperand(1);
2616 auto *CIdx = dyn_cast<ConstantInt>(Idx);
2617 if (isa<ScalableVectorType>(Vec->getType())) {
2618 // FIXME: there's probably *something* we can do with scalable vectors
2619 Known.resetAll();
2620 break;
2621 }
2622 unsigned NumElts = cast<FixedVectorType>(Vec->getType())->getNumElements();
2623 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
2624 if (CIdx && CIdx->getValue().ult(NumElts))
2625 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
2626 computeKnownBits(Vec, DemandedVecElts, Known, Q, Depth + 1);
2627 break;
2628 }
2629 case Instruction::ExtractValue:
2630 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
2632 if (EVI->getNumIndices() != 1) break;
2633 if (EVI->getIndices()[0] == 0) {
2634 switch (II->getIntrinsicID()) {
2635 default: break;
2636 case Intrinsic::uadd_with_overflow:
2637 case Intrinsic::sadd_with_overflow:
2639 true, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2640 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2641 break;
2642 case Intrinsic::usub_with_overflow:
2643 case Intrinsic::ssub_with_overflow:
2645 false, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2646 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2647 break;
2648 case Intrinsic::umul_with_overflow:
2649 case Intrinsic::smul_with_overflow:
2650 computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false,
2651 false, DemandedElts, Known, Known2, Q, Depth);
2652 break;
2653 }
2654 }
2655 }
2656 break;
2657 case Instruction::Freeze:
2658 if (isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CtxI, Q.DT,
2659 Depth + 1))
2660 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2661 break;
2662 }
2663}
2664
2665/// Determine which bits of V are known to be either zero or one and return
2666/// them.
2667KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
2668 const SimplifyQuery &Q, unsigned Depth) {
2669 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2670 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);
2671 return Known;
2672}
2673
2674/// Determine which bits of V are known to be either zero or one and return
2675/// them.
2677 unsigned Depth) {
2678 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2680 return Known;
2681}
2682
2683/// Determine which bits of V are known to be either zero or one and return
2684/// them in the Known bit set.
2685///
2686/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that
2687/// we cannot optimize based on the assumption that it is zero without changing
2688/// it to be an explicit zero. If we don't change it to zero, other code could
2689/// optimized based on the contradictory assumption that it is non-zero.
2690/// Because instcombine aggressively folds operations with undef args anyway,
2691/// this won't lose us code quality.
2692///
2693/// This function is defined on values with integer type, values with pointer
2694/// type, and vectors of integers. In the case
2695/// where V is a vector, known zero, and known one values are the
2696/// same width as the vector element, and the bit is set only if it is true
2697/// for all of the demanded elements in the vector specified by DemandedElts.
2698void computeKnownBits(const Value *V, const APInt &DemandedElts,
2699 KnownBits &Known, const SimplifyQuery &Q,
2700 unsigned Depth) {
2701 if (!DemandedElts) {
2702 // No demanded elts, better to assume we don't know anything.
2703 Known.resetAll();
2704 return;
2705 }
2706
2707 assert(V && "No Value?");
2708 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2709
2710#ifndef NDEBUG
2711 Type *Ty = V->getType();
2712 unsigned BitWidth = Known.getBitWidth();
2713
2714 assert((Ty->isIntOrIntVectorTy(BitWidth) || Ty->isPtrOrPtrVectorTy()) &&
2715 "Not integer or pointer type!");
2716
2717 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
2718 assert(
2719 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
2720 "DemandedElt width should equal the fixed vector number of elements");
2721 } else {
2722 assert(DemandedElts == APInt(1, 1) &&
2723 "DemandedElt width should be 1 for scalars or scalable vectors");
2724 }
2725
2726 Type *ScalarTy = Ty->getScalarType();
2727 if (ScalarTy->isPointerTy()) {
2728 assert(BitWidth == Q.DL.getPointerTypeSizeInBits(ScalarTy) &&
2729 "V and Known should have same BitWidth");
2730 } else {
2731 assert(BitWidth == Q.DL.getTypeSizeInBits(ScalarTy) &&
2732 "V and Known should have same BitWidth");
2733 }
2734#endif
2735
2736 const APInt *C;
2737 if (match(V, m_APInt(C))) {
2738 // We know all of the bits for a scalar constant or a splat vector constant!
2740 return;
2741 }
2742 // Null and aggregate-zero are all-zeros.
2744 Known.setAllZero();
2745 return;
2746 }
2747 // Handle a constant vector by taking the intersection of the known bits of
2748 // each element.
2750 assert(!isa<ScalableVectorType>(V->getType()));
2751 // We know that CDV must be a vector of integers. Take the intersection of
2752 // each element.
2753 Known.setAllConflict();
2754 for (unsigned i = 0, e = CDV->getNumElements(); i != e; ++i) {
2755 if (!DemandedElts[i])
2756 continue;
2757 APInt Elt = CDV->getElementAsAPInt(i);
2758 Known.Zero &= ~Elt;
2759 Known.One &= Elt;
2760 }
2761 if (Known.hasConflict())
2762 Known.resetAll();
2763 return;
2764 }
2765
2766 if (const auto *CV = dyn_cast<ConstantVector>(V)) {
2767 assert(!isa<ScalableVectorType>(V->getType()));
2768 // We know that CV must be a vector of integers. Take the intersection of
2769 // each element.
2770 Known.setAllConflict();
2771 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
2772 if (!DemandedElts[i])
2773 continue;
2774 Constant *Element = CV->getAggregateElement(i);
2775 if (isa<PoisonValue>(Element))
2776 continue;
2777 auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
2778 if (!ElementCI) {
2779 Known.resetAll();
2780 return;
2781 }
2782 const APInt &Elt = ElementCI->getValue();
2783 Known.Zero &= ~Elt;
2784 Known.One &= Elt;
2785 }
2786 if (Known.hasConflict())
2787 Known.resetAll();
2788 return;
2789 }
2790
2791 // Start out not knowing anything.
2792 Known.resetAll();
2793
2794 // We can't imply anything about undefs.
2795 if (isa<UndefValue>(V))
2796 return;
2797
2798 // There's no point in looking through other users of ConstantData for
2799 // assumptions. Confirm that we've handled them all.
2800 assert(!isa<ConstantData>(V) && "Unhandled constant data!");
2801
2802 if (const auto *A = dyn_cast<Argument>(V))
2803 if (std::optional<ConstantRange> Range = A->getRange())
2804 Known = Range->toKnownBits();
2805
2806 // All recursive calls that increase depth must come after this.
2808 return;
2809
2810 // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
2811 // the bits of its aliasee.
2812 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
2813 if (!GA->isInterposable())
2814 computeKnownBits(GA->getAliasee(), Known, Q, Depth + 1);
2815 return;
2816 }
2817
2818 if (const Operator *I = dyn_cast<Operator>(V))
2819 computeKnownBitsFromOperator(I, DemandedElts, Known, Q, Depth);
2820 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2821 if (std::optional<ConstantRange> CR = GV->getAbsoluteSymbolRange())
2822 Known = CR->toKnownBits();
2823 }
2824
2825 // Aligned pointers have trailing zeros - refine Known.Zero set
2826 if (isa<PointerType>(V->getType())) {
2827 Align Alignment = V->getPointerAlignment(Q.DL);
2828 Known.Zero.setLowBits(Log2(Alignment));
2829 }
2830
2831 // computeKnownBitsFromContext strictly refines Known.
2832 // Therefore, we run them after computeKnownBitsFromOperator.
2833
2834 // Check whether we can determine known bits from context such as assumes.
2836}
2837
2838/// Try to detect a recurrence that the value of the induction variable is
2839/// always a power of two (or zero).
2840static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero,
2841 SimplifyQuery &Q, unsigned Depth) {
2842 BinaryOperator *BO = nullptr;
2843 Value *Start = nullptr, *Step = nullptr;
2844 if (!matchSimpleRecurrence(PN, BO, Start, Step))
2845 return false;
2846
2847 // Initial value must be a power of two.
2848 for (const Use &U : PN->operands()) {
2849 if (U.get() == Start) {
2850 // Initial value comes from a different BB, need to adjust context
2851 // instruction for analysis.
2852 Q.CtxI = PN->getIncomingBlock(U)->getTerminator();
2853 if (!isKnownToBeAPowerOfTwo(Start, OrZero, Q, Depth))
2854 return false;
2855 }
2856 }
2857
2858 // Except for Mul, the induction variable must be on the left side of the
2859 // increment expression, otherwise its value can be arbitrary.
2860 if (BO->getOpcode() != Instruction::Mul && BO->getOperand(1) != Step)
2861 return false;
2862
2863 Q.CtxI = BO->getParent()->getTerminator();
2864 switch (BO->getOpcode()) {
2865 case Instruction::Mul:
2866 // Power of two is closed under multiplication.
2867 return (OrZero || Q.IIQ.hasNoUnsignedWrap(BO) ||
2868 Q.IIQ.hasNoSignedWrap(BO)) &&
2869 isKnownToBeAPowerOfTwo(Step, OrZero, Q, Depth);
2870 case Instruction::SDiv:
2871 // Start value must not be signmask for signed division, so simply being a
2872 // power of two is not sufficient, and it has to be a constant.
2873 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2874 return false;
2875 [[fallthrough]];
2876 case Instruction::UDiv:
2877 // Divisor must be a power of two.
2878 // If OrZero is false, cannot guarantee induction variable is non-zero after
2879 // division, same for Shr, unless it is exact division.
2880 return (OrZero || Q.IIQ.isExact(BO)) &&
2881 isKnownToBeAPowerOfTwo(Step, false, Q, Depth);
2882 case Instruction::Shl:
2883 return OrZero || Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO);
2884 case Instruction::AShr:
2885 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2886 return false;
2887 [[fallthrough]];
2888 case Instruction::LShr:
2889 return OrZero || Q.IIQ.isExact(BO);
2890 default:
2891 return false;
2892 }
2893}
2894
2895/// Return true if we can infer that \p V is known to be a power of 2 from
2896/// dominating condition \p Cond (e.g., ctpop(V) == 1).
2897static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero,
2898 const Value *Cond,
2899 bool CondIsTrue) {
2901 const APInt *RHSC;
2902 if (!match(Cond, m_ICmp(Pred, m_Ctpop(m_Specific(V)), m_APInt(RHSC))))
2903 return false;
2904 if (!CondIsTrue)
2906 // ctpop(V) u< 2
2907 if (OrZero && Pred == ICmpInst::ICMP_ULT && *RHSC == 2)
2908 return true;
2909 // ctpop(V) == 1
2910 return Pred == ICmpInst::ICMP_EQ && *RHSC == 1;
2911}
2912
2913/// Return true if the given value is known to have exactly one
2914/// bit set when defined. For vectors return true if every element is known to
2915/// be a power of two when defined. Supports values with integer or pointer
2916/// types and vectors of integers.
2917bool llvm::isKnownToBeAPowerOfTwo(const Value *V, bool OrZero,
2918 const SimplifyQuery &Q, unsigned Depth) {
2919 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2920
2921 if (isa<Constant>(V))
2922 return OrZero ? match(V, m_Power2OrZero()) : match(V, m_Power2());
2923
2924 // i1 is by definition a power of 2 or zero.
2925 if (OrZero && V->getType()->getScalarSizeInBits() == 1)
2926 return true;
2927
2928 // Try to infer from assumptions.
2929 if (Q.AC && Q.CtxI) {
2930 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
2931 if (!AssumeVH)
2932 continue;
2933 CallInst *I = cast<CallInst>(AssumeVH);
2934 if (isImpliedToBeAPowerOfTwoFromCond(V, OrZero, I->getArgOperand(0),
2935 /*CondIsTrue=*/true) &&
2937 return true;
2938 }
2939 }
2940
2941 // Handle dominating conditions.
2942 if (Q.DC && Q.CtxI && Q.DT) {
2943 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
2944 Value *Cond = BI->getCondition();
2945
2946 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
2948 /*CondIsTrue=*/true) &&
2949 Q.DT->dominates(Edge0, Q.CtxI->getParent()))
2950 return true;
2951
2952 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
2954 /*CondIsTrue=*/false) &&
2955 Q.DT->dominates(Edge1, Q.CtxI->getParent()))
2956 return true;
2957 }
2958 }
2959
2960 auto *I = dyn_cast<Instruction>(V);
2961 if (!I)
2962 return false;
2963
2964 if (Q.CtxI && match(V, m_VScale())) {
2965 const Function *F = Q.CtxI->getFunction();
2966 // The vscale_range indicates vscale is a power-of-two.
2967 return F->hasFnAttribute(Attribute::VScaleRange);
2968 }
2969
2970 // 1 << X is clearly a power of two if the one is not shifted off the end. If
2971 // it is shifted off the end then the result is undefined.
2972 if (match(I, m_Shl(m_One(), m_Value())))
2973 return true;
2974
2975 // (signmask) >>l X is clearly a power of two if the one is not shifted off
2976 // the bottom. If it is shifted off the bottom then the result is undefined.
2977 if (match(I, m_LShr(m_SignMask(), m_Value())))
2978 return true;
2979
2980 // The remaining tests are all recursive, so bail out if we hit the limit.
2982 return false;
2983
2984 switch (I->getOpcode()) {
2985 case Instruction::ZExt:
2986 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2987 case Instruction::Trunc:
2988 return OrZero && isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2989 case Instruction::Shl:
2990 if (OrZero || Q.IIQ.hasNoUnsignedWrap(I) || Q.IIQ.hasNoSignedWrap(I))
2991 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2992 return false;
2993 case Instruction::LShr:
2994 if (OrZero || Q.IIQ.isExact(cast<BinaryOperator>(I)))
2995 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2996 return false;
2997 case Instruction::UDiv:
2999 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
3000 return false;
3001 case Instruction::Mul:
3002 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
3003 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth) &&
3004 (OrZero || isKnownNonZero(I, Q, Depth));
3005 case Instruction::And:
3006 // A power of two and'd with anything is a power of two or zero.
3007 if (OrZero &&
3008 (isKnownToBeAPowerOfTwo(I->getOperand(1), /*OrZero*/ true, Q, Depth) ||
3009 isKnownToBeAPowerOfTwo(I->getOperand(0), /*OrZero*/ true, Q, Depth)))
3010 return true;
3011 // X & (-X) is always a power of two or zero.
3012 if (match(I->getOperand(0), m_Neg(m_Specific(I->getOperand(1)))) ||
3013 match(I->getOperand(1), m_Neg(m_Specific(I->getOperand(0)))))
3014 return OrZero || isKnownNonZero(I->getOperand(0), Q, Depth);
3015 return false;
3016 case Instruction::Add: {
3017 // Adding a power-of-two or zero to the same power-of-two or zero yields
3018 // either the original power-of-two, a larger power-of-two or zero.
3020 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) ||
3021 Q.IIQ.hasNoSignedWrap(VOBO)) {
3022 if (match(I->getOperand(0),
3023 m_c_And(m_Specific(I->getOperand(1)), m_Value())) &&
3024 isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth))
3025 return true;
3026 if (match(I->getOperand(1),
3027 m_c_And(m_Specific(I->getOperand(0)), m_Value())) &&
3028 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth))
3029 return true;
3030
3031 unsigned BitWidth = V->getType()->getScalarSizeInBits();
3032 KnownBits LHSBits(BitWidth);
3033 computeKnownBits(I->getOperand(0), LHSBits, Q, Depth);
3034
3035 KnownBits RHSBits(BitWidth);
3036 computeKnownBits(I->getOperand(1), RHSBits, Q, Depth);
3037 // If i8 V is a power of two or zero:
3038 // ZeroBits: 1 1 1 0 1 1 1 1
3039 // ~ZeroBits: 0 0 0 1 0 0 0 0
3040 if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2())
3041 // If OrZero isn't set, we cannot give back a zero result.
3042 // Make sure either the LHS or RHS has a bit set.
3043 if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue())
3044 return true;
3045 }
3046
3047 // LShr(UINT_MAX, Y) + 1 is a power of two (if add is nuw) or zero.
3048 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO))
3049 if (match(I, m_Add(m_LShr(m_AllOnes(), m_Value()), m_One())))
3050 return true;
3051 return false;
3052 }
3053 case Instruction::Select:
3054 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
3055 isKnownToBeAPowerOfTwo(I->getOperand(2), OrZero, Q, Depth);
3056 case Instruction::PHI: {
3057 // A PHI node is power of two if all incoming values are power of two, or if
3058 // it is an induction variable where in each step its value is a power of
3059 // two.
3060 auto *PN = cast<PHINode>(I);
3062
3063 // Check if it is an induction variable and always power of two.
3064 if (isPowerOfTwoRecurrence(PN, OrZero, RecQ, Depth))
3065 return true;
3066
3067 // Recursively check all incoming values. Limit recursion to 2 levels, so
3068 // that search complexity is limited to number of operands^2.
3069 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
3070 return llvm::all_of(PN->operands(), [&](const Use &U) {
3071 // Value is power of 2 if it is coming from PHI node itself by induction.
3072 if (U.get() == PN)
3073 return true;
3074
3075 // Change the context instruction to the incoming block where it is
3076 // evaluated.
3077 RecQ.CtxI = PN->getIncomingBlock(U)->getTerminator();
3078 return isKnownToBeAPowerOfTwo(U.get(), OrZero, RecQ, NewDepth);
3079 });
3080 }
3081 case Instruction::Invoke:
3082 case Instruction::Call: {
3083 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
3084 switch (II->getIntrinsicID()) {
3085 case Intrinsic::umax:
3086 case Intrinsic::smax:
3087 case Intrinsic::umin:
3088 case Intrinsic::smin:
3089 return isKnownToBeAPowerOfTwo(II->getArgOperand(1), OrZero, Q, Depth) &&
3090 isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
3091 // bswap/bitreverse just move around bits, but don't change any 1s/0s
3092 // thus dont change pow2/non-pow2 status.
3093 case Intrinsic::bitreverse:
3094 case Intrinsic::bswap:
3095 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
3096 case Intrinsic::fshr:
3097 case Intrinsic::fshl:
3098 // If Op0 == Op1, this is a rotate. is_pow2(rotate(x, y)) == is_pow2(x)
3099 if (II->getArgOperand(0) == II->getArgOperand(1))
3100 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
3101 break;
3102 case Intrinsic::riscv_vsetvlimax:
3103 // VLMAX is VLEN * LMUL / SEW, which is always a non-zero power of two
3104 // for any valid vtype, so it is a power of two regardless of OrZero.
3105 return true;
3106 case Intrinsic::read_register:
3107 case Intrinsic::read_volatile_register: {
3108 // The RISC-V vlenb CSR holds VLEN/8, which is always a non-zero power
3109 // of two, so it is a power of two regardless of OrZero.
3110 const Module *M = II->getModule();
3111 if (!M || !M->getTargetTriple().isRISCV())
3112 break;
3113 return isReadVLENB(*II);
3114 }
3115 default:
3116 break;
3117 }
3118 }
3119 return false;
3120 }
3121 default:
3122 return false;
3123 }
3124}
3125
3126/// Test whether a GEP's result is known to be non-null.
3127///
3128/// Uses properties inherent in a GEP to try to determine whether it is known
3129/// to be non-null.
3130///
3131/// Currently this routine does not support vector GEPs.
3132static bool isGEPKnownNonNull(const GEPOperator *GEP, const SimplifyQuery &Q,
3133 unsigned Depth) {
3134 const Function *F = nullptr;
3135 if (const Instruction *I = dyn_cast<Instruction>(GEP))
3136 F = I->getFunction();
3137
3138 // If the gep is nuw or inbounds with invalid null pointer, then the GEP
3139 // may be null iff the base pointer is null and the offset is zero.
3140 if (!GEP->hasNoUnsignedWrap() &&
3141 !(GEP->isInBounds() &&
3142 !NullPointerIsDefined(F, GEP->getPointerAddressSpace())))
3143 return false;
3144
3145 // FIXME: Support vector-GEPs.
3146 assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP");
3147
3148 // If the base pointer is non-null, we cannot walk to a null address with an
3149 // inbounds GEP in address space zero.
3150 if (isKnownNonZero(GEP->getPointerOperand(), Q, Depth))
3151 return true;
3152
3153 // Walk the GEP operands and see if any operand introduces a non-zero offset.
3154 // If so, then the GEP cannot produce a null pointer, as doing so would
3155 // inherently violate the inbounds contract within address space zero.
3157 GTI != GTE; ++GTI) {
3158 // Struct types are easy -- they must always be indexed by a constant.
3159 if (StructType *STy = GTI.getStructTypeOrNull()) {
3160 ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
3161 unsigned ElementIdx = OpC->getZExtValue();
3162 const StructLayout *SL = Q.DL.getStructLayout(STy);
3163 uint64_t ElementOffset = SL->getElementOffset(ElementIdx);
3164 if (ElementOffset > 0)
3165 return true;
3166 continue;
3167 }
3168
3169 // If we have a zero-sized type, the index doesn't matter. Keep looping.
3170 if (GTI.getSequentialElementStride(Q.DL).isZero())
3171 continue;
3172
3173 // Fast path the constant operand case both for efficiency and so we don't
3174 // increment Depth when just zipping down an all-constant GEP.
3175 if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
3176 if (!OpC->isZero())
3177 return true;
3178 continue;
3179 }
3180
3181 // We post-increment Depth here because while isKnownNonZero increments it
3182 // as well, when we pop back up that increment won't persist. We don't want
3183 // to recurse 10k times just because we have 10k GEP operands. We don't
3184 // bail completely out because we want to handle constant GEPs regardless
3185 // of depth.
3187 continue;
3188
3189 if (isKnownNonZero(GTI.getOperand(), Q, Depth))
3190 return true;
3191 }
3192
3193 return false;
3194}
3195
3197 const Instruction *CtxI,
3198 const DominatorTree *DT) {
3199 assert(!isa<Constant>(V) && "Called for constant?");
3200
3201 if (!CtxI || !DT)
3202 return false;
3203
3204 unsigned NumUsesExplored = 0;
3205 for (auto &U : V->uses()) {
3206 // Avoid massive lists
3207 if (NumUsesExplored >= DomConditionsMaxUses)
3208 break;
3209 NumUsesExplored++;
3210
3211 const Instruction *UI = cast<Instruction>(U.getUser());
3212 // If the value is used as an argument to a call or invoke, then argument
3213 // attributes may provide an answer about null-ness.
3214 if (V->getType()->isPointerTy()) {
3215 if (const auto *CB = dyn_cast<CallBase>(UI)) {
3216 if (CB->isArgOperand(&U) &&
3217 CB->paramHasNonNullAttr(CB->getArgOperandNo(&U),
3218 /*AllowUndefOrPoison=*/false) &&
3219 DT->dominates(CB, CtxI))
3220 return true;
3221 }
3222 }
3223
3224 // If the value is used as a load/store, then the pointer must be non null.
3225 if (V == getLoadStorePointerOperand(UI)) {
3227 V->getType()->getPointerAddressSpace()) &&
3228 DT->dominates(UI, CtxI))
3229 return true;
3230 }
3231
3232 if ((match(UI, m_IDiv(m_Value(), m_Specific(V))) ||
3233 match(UI, m_IRem(m_Value(), m_Specific(V)))) &&
3234 isValidAssumeForContext(UI, CtxI, DT))
3235 return true;
3236
3237 // Consider only compare instructions uniquely controlling a branch
3238 Value *RHS;
3240 bool NonNullIfTrue;
3241 if (match(UI, m_c_ICmp(Pred, m_Specific(V), m_Value(RHS)))) {
3242 if (cmpExcludesZero(Pred, RHS))
3243 NonNullIfTrue = true;
3245 NonNullIfTrue = false;
3246 else
3247 continue;
3248 } else if (UI->getType()->isIntegerTy(1) &&
3249 match(UI, m_Trunc(m_Specific(V)))) {
3250 NonNullIfTrue = true;
3251 } else
3252 continue;
3253
3256 for (const auto *CmpU : UI->users()) {
3257 assert(WorkList.empty() && "Should be!");
3258 if (Visited.insert(CmpU).second)
3259 WorkList.push_back(CmpU);
3260
3261 while (!WorkList.empty()) {
3262 auto *Curr = WorkList.pop_back_val();
3263
3264 // If a user is an AND, add all its users to the work list. We only
3265 // propagate "pred != null" condition through AND because it is only
3266 // correct to assume that all conditions of AND are met in true branch.
3267 // TODO: Support similar logic of OR and EQ predicate?
3268 if (NonNullIfTrue)
3269 if (match(Curr, m_LogicalAnd(m_Value(), m_Value()))) {
3270 for (const auto *CurrU : Curr->users())
3271 if (Visited.insert(CurrU).second)
3272 WorkList.push_back(CurrU);
3273 continue;
3274 }
3275
3276 if (const CondBrInst *BI = dyn_cast<CondBrInst>(Curr)) {
3277 BasicBlock *NonNullSuccessor =
3278 BI->getSuccessor(NonNullIfTrue ? 0 : 1);
3279 BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor);
3280 if (DT->dominates(Edge, CtxI->getParent()))
3281 return true;
3282 } else if (NonNullIfTrue && isGuard(Curr) &&
3283 DT->dominates(cast<Instruction>(Curr), CtxI)) {
3284 return true;
3285 }
3286 }
3287 }
3288 }
3289
3290 return false;
3291}
3292
3293/// Does the 'Range' metadata (which must be a valid MD_range operand list)
3294/// ensure that the value it's attached to is never Value? 'RangeType' is
3295/// is the type of the value described by the range.
3296static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) {
3297 const unsigned NumRanges = Ranges->getNumOperands() / 2;
3298 assert(NumRanges >= 1);
3299 for (unsigned i = 0; i < NumRanges; ++i) {
3301 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
3303 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
3304 ConstantRange Range(Lower->getValue(), Upper->getValue());
3305 if (Range.contains(Value))
3306 return false;
3307 }
3308 return true;
3309}
3310
3311/// Try to detect a recurrence that monotonically increases/decreases from a
3312/// non-zero starting value. These are common as induction variables.
3313static bool isNonZeroRecurrence(const PHINode *PN) {
3314 BinaryOperator *BO = nullptr;
3315 Value *Start = nullptr, *Step = nullptr;
3316 const APInt *StartC, *StepC;
3317 if (!matchSimpleRecurrence(PN, BO, Start, Step) ||
3318 !match(Start, m_APInt(StartC)) || StartC->isZero())
3319 return false;
3320
3321 switch (BO->getOpcode()) {
3322 case Instruction::Add:
3323 // Starting from non-zero and stepping away from zero can never wrap back
3324 // to zero.
3325 return BO->hasNoUnsignedWrap() ||
3326 (BO->hasNoSignedWrap() && match(Step, m_APInt(StepC)) &&
3327 StartC->isNegative() == StepC->isNegative());
3328 case Instruction::Mul:
3329 return (BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap()) &&
3330 match(Step, m_APInt(StepC)) && !StepC->isZero();
3331 case Instruction::Shl:
3332 return BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap();
3333 case Instruction::AShr:
3334 case Instruction::LShr:
3335 return BO->isExact();
3336 case Instruction::Or:
3337 return true;
3338 default:
3339 return false;
3340 }
3341}
3342
3343static bool matchOpWithOpEqZero(Value *Op0, Value *Op1) {
3345 m_Specific(Op1), m_Zero()))) ||
3347 m_Specific(Op0), m_Zero())));
3348}
3349
3350/// Return true if "addr(P) + Off" cannot be zero. The object P points to
3351/// cannot cross the unsigned address space boundary, including the address one
3352/// past its end, so an offset that stays within the object cannot wrap around
3353/// to zero. This holds even if the object starts at the zero address, which is
3354/// all that null_pointer_is_valid permits, so a non-zero offset is enough.
3356 Value *Y) {
3357 const APInt *Off;
3358 if (!match(Y, m_APInt(Off)) || Off->isZero())
3359 return false;
3360
3361 // ptrtoaddr always produces the address. ptrtoint only does so when its
3362 // result is at least as wide as the address: a narrower result is truncated
3363 // and may wrap to zero, while bits above the address can only make the
3364 // result more non-zero.
3365 Value *P;
3366 if (!match(X, m_PtrToAddr(m_Value(P))) &&
3367 !(match(X, m_PtrToInt(m_Value(P))) &&
3368 X->getType()->getScalarSizeInBits() >=
3369 Q.DL.getAddressSizeInBits(P->getType())))
3370 return false;
3371
3372 // A vector of pointers has no single object whose size can be taken, and
3373 // getPointerDereferenceableBytes() requires a plain pointer.
3374 if (!P->getType()->isPointerTy())
3375 return false;
3376
3377 bool CanBeNull;
3378 uint64_t DerefBytes = P->getPointerDereferenceableBytes(
3379 Q.DL, CanBeNull, /*CanBeFreed=*/nullptr);
3380 if (CanBeNull || DerefBytes == 0 || !Off->ule(DerefBytes))
3381 return false;
3382
3383 return true;
3384}
3385
3386static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q,
3387 unsigned BitWidth, Value *X, Value *Y, bool NSW,
3388 bool NUW, unsigned Depth) {
3390 return true;
3391
3392 // (X + (X != 0)) is non zero
3393 if (matchOpWithOpEqZero(X, Y))
3394 return true;
3395
3396 if (NUW)
3397 return isKnownNonZero(Y, DemandedElts, Q, Depth) ||
3398 isKnownNonZero(X, DemandedElts, Q, Depth);
3399
3400 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
3401 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
3402
3403 // If X and Y are both non-negative (as signed values) then their sum is not
3404 // zero unless both X and Y are zero.
3405 if (XKnown.isNonNegative() && YKnown.isNonNegative())
3406 if (isKnownNonZero(Y, DemandedElts, Q, Depth) ||
3407 isKnownNonZero(X, DemandedElts, Q, Depth))
3408 return true;
3409
3410 // If X and Y are both negative (as signed values) then their sum is not
3411 // zero unless both X and Y equal INT_MIN.
3412 if (XKnown.isNegative() && YKnown.isNegative()) {
3414 // The sign bit of X is set. If some other bit is set then X is not equal
3415 // to INT_MIN.
3416 if (XKnown.One.intersects(Mask))
3417 return true;
3418 // The sign bit of Y is set. If some other bit is set then Y is not equal
3419 // to INT_MIN.
3420 if (YKnown.One.intersects(Mask))
3421 return true;
3422 }
3423
3424 // The sum of a non-negative number and a power of two is not zero.
3425 if (XKnown.isNonNegative() &&
3426 isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Q, Depth))
3427 return true;
3428 if (YKnown.isNonNegative() &&
3429 isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Q, Depth))
3430 return true;
3431
3432 return KnownBits::add(XKnown, YKnown, NSW, NUW).isNonZero();
3433}
3434
3435static bool isNonZeroSub(const APInt &DemandedElts, const SimplifyQuery &Q,
3436 unsigned BitWidth, Value *X, Value *Y,
3437 unsigned Depth) {
3438 // (X - (X != 0)) is non zero
3439 // ((X != 0) - X) is non zero
3440 if (matchOpWithOpEqZero(X, Y))
3441 return true;
3442
3443 // TODO: Move this case into isKnownNonEqual().
3444 if (auto *C = dyn_cast<Constant>(X))
3445 if (C->isNullValue() && isKnownNonZero(Y, DemandedElts, Q, Depth))
3446 return true;
3447
3448 return ::isKnownNonEqual(X, Y, DemandedElts, Q, Depth);
3449}
3450
3451static bool isNonZeroMul(const APInt &DemandedElts, const SimplifyQuery &Q,
3452 unsigned BitWidth, Value *X, Value *Y, bool NSW,
3453 bool NUW, unsigned Depth) {
3454 // If X and Y are non-zero then so is X * Y as long as the multiplication
3455 // does not overflow.
3456 if (NSW || NUW)
3457 return isKnownNonZero(X, DemandedElts, Q, Depth) &&
3458 isKnownNonZero(Y, DemandedElts, Q, Depth);
3459
3460 // If either X or Y is odd, then if the other is non-zero the result can't
3461 // be zero.
3462 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
3463 if (XKnown.One[0])
3464 return isKnownNonZero(Y, DemandedElts, Q, Depth);
3465
3466 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
3467 if (YKnown.One[0])
3468 return XKnown.isNonZero() || isKnownNonZero(X, DemandedElts, Q, Depth);
3469
3470 // If there exists any subset of X (sX) and subset of Y (sY) s.t sX * sY is
3471 // non-zero, then X * Y is non-zero. We can find sX and sY by just taking
3472 // the lowest known One of X and Y. If they are non-zero, the result
3473 // must be non-zero. We can check if LSB(X) * LSB(Y) != 0 by doing
3474 // X.CountLeadingZeros + Y.CountLeadingZeros < BitWidth.
3475 return (XKnown.countMaxTrailingZeros() + YKnown.countMaxTrailingZeros()) <
3476 BitWidth;
3477}
3478
3479static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts,
3480 const SimplifyQuery &Q, const KnownBits &KnownVal,
3481 unsigned Depth) {
3482 auto ShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
3483 switch (I->getOpcode()) {
3484 case Instruction::Shl:
3485 return Lhs.shl(Rhs);
3486 case Instruction::LShr:
3487 return Lhs.lshr(Rhs);
3488 case Instruction::AShr:
3489 return Lhs.ashr(Rhs);
3490 default:
3491 llvm_unreachable("Unknown Shift Opcode");
3492 }
3493 };
3494
3495 auto InvShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
3496 switch (I->getOpcode()) {
3497 case Instruction::Shl:
3498 return Lhs.lshr(Rhs);
3499 case Instruction::LShr:
3500 case Instruction::AShr:
3501 return Lhs.shl(Rhs);
3502 default:
3503 llvm_unreachable("Unknown Shift Opcode");
3504 }
3505 };
3506
3507 if (KnownVal.isUnknown())
3508 return false;
3509
3510 KnownBits KnownCnt =
3511 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
3512 APInt MaxShift = KnownCnt.getMaxValue();
3513 unsigned NumBits = KnownVal.getBitWidth();
3514 if (MaxShift.uge(NumBits))
3515 return false;
3516
3517 if (!ShiftOp(KnownVal.One, MaxShift).isZero())
3518 return true;
3519
3520 // If all of the bits shifted out are known to be zero, and Val is known
3521 // non-zero then at least one non-zero bit must remain.
3522 if (InvShiftOp(KnownVal.Zero, NumBits - MaxShift)
3523 .eq(InvShiftOp(APInt::getAllOnes(NumBits), NumBits - MaxShift)) &&
3524 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth))
3525 return true;
3526
3527 return false;
3528}
3529
3531 const APInt &DemandedElts,
3532 const SimplifyQuery &Q, unsigned Depth) {
3533 unsigned BitWidth = getBitWidth(I->getType()->getScalarType(), Q.DL);
3534 switch (I->getOpcode()) {
3535 case Instruction::Alloca:
3536 // Alloca never returns null, malloc might.
3537 return I->getType()->getPointerAddressSpace() == 0;
3538 case Instruction::GetElementPtr:
3539 if (I->getType()->isPointerTy())
3541 break;
3542 case Instruction::BitCast: {
3543 // We need to be a bit careful here. We can only peek through the bitcast
3544 // if the scalar size of elements in the operand are smaller than and a
3545 // multiple of the size they are casting too. Take three cases:
3546 //
3547 // 1) Unsafe:
3548 // bitcast <2 x i16> %NonZero to <4 x i8>
3549 //
3550 // %NonZero can have 2 non-zero i16 elements, but isKnownNonZero on a
3551 // <4 x i8> requires that all 4 i8 elements be non-zero which isn't
3552 // guranteed (imagine just sign bit set in the 2 i16 elements).
3553 //
3554 // 2) Unsafe:
3555 // bitcast <4 x i3> %NonZero to <3 x i4>
3556 //
3557 // Even though the scalar size of the src (`i3`) is smaller than the
3558 // scalar size of the dst `i4`, because `i3` is not a multiple of `i4`
3559 // its possible for the `3 x i4` elements to be zero because there are
3560 // some elements in the destination that don't contain any full src
3561 // element.
3562 //
3563 // 3) Safe:
3564 // bitcast <4 x i8> %NonZero to <2 x i16>
3565 //
3566 // This is always safe as non-zero in the 4 i8 elements implies
3567 // non-zero in the combination of any two adjacent ones. Since i8 is a
3568 // multiple of i16, each i16 is guranteed to have 2 full i8 elements.
3569 // This all implies the 2 i16 elements are non-zero.
3570 Type *FromTy = I->getOperand(0)->getType();
3571 if ((FromTy->isIntOrIntVectorTy() || FromTy->isPtrOrPtrVectorTy()) &&
3572 (BitWidth % getBitWidth(FromTy->getScalarType(), Q.DL)) == 0)
3573 return isKnownNonZero(I->getOperand(0), Q, Depth);
3574 } break;
3575 case Instruction::IntToPtr:
3576 // Note that we have to take special care to avoid looking through
3577 // truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well
3578 // as casts that can alter the value, e.g., AddrSpaceCasts.
3579 if (!isa<ScalableVectorType>(I->getType()) &&
3580 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
3581 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
3582 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3583 break;
3584 case Instruction::PtrToAddr:
3585 // isKnownNonZero() for pointers refers to the address bits being non-zero,
3586 // so we can directly forward.
3587 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3588 case Instruction::PtrToInt:
3589 // For inttoptr, make sure the result size is >= the address size. If the
3590 // address is non-zero, any larger value is also non-zero.
3591 if (Q.DL.getAddressSizeInBits(I->getOperand(0)->getType()) <=
3592 I->getType()->getScalarSizeInBits())
3593 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3594 break;
3595 case Instruction::Trunc:
3596 // nuw/nsw trunc preserves zero/non-zero status of input.
3597 if (auto *TI = dyn_cast<TruncInst>(I))
3598 if (TI->hasNoSignedWrap() || TI->hasNoUnsignedWrap())
3599 return isKnownNonZero(TI->getOperand(0), DemandedElts, Q, Depth);
3600 break;
3601
3602 // Iff x - y != 0, then x ^ y != 0
3603 // Therefore we can do the same exact checks
3604 case Instruction::Xor:
3605 case Instruction::Sub:
3606 return isNonZeroSub(DemandedElts, Q, BitWidth, I->getOperand(0),
3607 I->getOperand(1), Depth);
3608 case Instruction::Or:
3609 // (X | (X != 0)) is non zero
3610 if (matchOpWithOpEqZero(I->getOperand(0), I->getOperand(1)))
3611 return true;
3612 // X | Y != 0 if X != Y.
3613 if (isKnownNonEqual(I->getOperand(0), I->getOperand(1), DemandedElts, Q,
3614 Depth))
3615 return true;
3616 // X | Y != 0 if X != 0 or Y != 0.
3617 return isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth) ||
3618 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3619 case Instruction::SExt:
3620 case Instruction::ZExt:
3621 // ext X != 0 if X != 0.
3622 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3623
3624 case Instruction::Shl: {
3625 // shl nsw/nuw can't remove any non-zero bits.
3627 if (Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO))
3628 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3629
3630 // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
3631 // if the lowest bit is shifted off the end.
3633 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth);
3634 if (Known.One[0])
3635 return true;
3636
3637 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3638 }
3639 case Instruction::LShr:
3640 case Instruction::AShr: {
3641 // shr exact can only shift out zero bits.
3643 if (BO->isExact())
3644 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3645
3646 // shr X, Y != 0 if X is negative. Note that the value of the shift is not
3647 // defined if the sign bit is shifted off the end.
3649 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3650 if (Known.isNegative())
3651 return true;
3652
3653 // shr (add nuw A, B), C is non-zero if A or B has a known-one bit at
3654 // position >= C, because the sum >= max(A, B).
3655 Value *A, *B;
3656 const APInt *C;
3657 if (Depth + 1 < MaxAnalysisRecursionDepth &&
3658 match(I->getOperand(0), m_NUWAdd(m_Value(A), m_Value(B))) &&
3659 match(I->getOperand(1), m_APInt(C)) && C->ult(BitWidth)) {
3660 KnownBits KnownA = computeKnownBits(A, DemandedElts, Q, Depth + 1);
3661 if (!KnownA.One.lshr(*C).isZero())
3662 return true;
3663 KnownBits KnownB = computeKnownBits(B, DemandedElts, Q, Depth + 1);
3664 if (!KnownB.One.lshr(*C).isZero())
3665 return true;
3666 }
3667
3668 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3669 }
3670 case Instruction::UDiv:
3671 case Instruction::SDiv: {
3672 // X / Y
3673 // div exact can only produce a zero if the dividend is zero.
3674 if (cast<PossiblyExactOperator>(I)->isExact())
3675 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3676
3677 KnownBits XKnown =
3678 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3679 // If X is fully unknown we won't be able to figure anything out so don't
3680 // both computing knownbits for Y.
3681 if (XKnown.isUnknown())
3682 return false;
3683
3684 KnownBits YKnown =
3685 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
3686 if (I->getOpcode() == Instruction::SDiv) {
3687 // For signed division need to compare abs value of the operands.
3688 XKnown = XKnown.abs(/*IntMinIsPoison*/ false);
3689 YKnown = YKnown.abs(/*IntMinIsPoison*/ false);
3690 }
3691 // If X u>= Y then div is non zero (0/0 is UB).
3692 std::optional<bool> XUgeY = KnownBits::uge(XKnown, YKnown);
3693 // If X is total unknown or X u< Y we won't be able to prove non-zero
3694 // with compute known bits so just return early.
3695 return XUgeY && *XUgeY;
3696 }
3697 case Instruction::Add: {
3698 // X + Y.
3699
3700 // If Add has nuw wrap flag, then if either X or Y is non-zero the result is
3701 // non-zero.
3703 return isNonZeroAdd(DemandedElts, Q, BitWidth, I->getOperand(0),
3704 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3705 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3706 }
3707 case Instruction::Mul: {
3709 return isNonZeroMul(DemandedElts, Q, BitWidth, I->getOperand(0),
3710 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3711 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3712 }
3713 case Instruction::Select: {
3714 // (C ? X : Y) != 0 if X != 0 and Y != 0.
3715
3716 // First check if the arm is non-zero using `isKnownNonZero`. If that fails,
3717 // then see if the select condition implies the arm is non-zero. For example
3718 // (X != 0 ? X : Y), we know the true arm is non-zero as the `X` "return" is
3719 // dominated by `X != 0`.
3720 auto SelectArmIsNonZero = [&](bool IsTrueArm) {
3721 Value *Op;
3722 Op = IsTrueArm ? I->getOperand(1) : I->getOperand(2);
3723 // Op is trivially non-zero.
3724 if (isKnownNonZero(Op, DemandedElts, Q, Depth))
3725 return true;
3726
3727 // The condition of the select dominates the true/false arm. Check if the
3728 // condition implies that a given arm is non-zero.
3729 Value *X;
3731 if (!match(I->getOperand(0), m_c_ICmp(Pred, m_Specific(Op), m_Value(X))))
3732 return false;
3733
3734 if (!IsTrueArm)
3736
3737 return cmpExcludesZero(Pred, X);
3738 };
3739
3740 if (SelectArmIsNonZero(/* IsTrueArm */ true) &&
3741 SelectArmIsNonZero(/* IsTrueArm */ false))
3742 return true;
3743 break;
3744 }
3745 case Instruction::PHI: {
3746 auto *PN = cast<PHINode>(I);
3748 return true;
3749
3750 // Check if all incoming values are non-zero using recursion.
3752 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
3753 return llvm::all_of(PN->operands(), [&](const Use &U) {
3754 if (U.get() == PN)
3755 return true;
3756 RecQ.CtxI = PN->getIncomingBlock(U)->getTerminator();
3757 // Check if the branch on the phi excludes zero.
3758 CmpPredicate Pred;
3759 Value *X;
3760 BasicBlock *TrueSucc, *FalseSucc;
3761 if (match(RecQ.CtxI,
3762 m_Br(m_c_ICmp(Pred, m_Specific(U.get()), m_Value(X)),
3763 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
3764 // Check for cases of duplicate successors.
3765 if ((TrueSucc == PN->getParent()) != (FalseSucc == PN->getParent())) {
3766 // If we're using the false successor, invert the predicate.
3767 if (FalseSucc == PN->getParent())
3768 Pred = CmpInst::getInversePredicate(Pred);
3769 if (cmpExcludesZero(Pred, X))
3770 return true;
3771 }
3772 }
3773 // Finally recurse on the edge and check it directly.
3774 return isKnownNonZero(U.get(), DemandedElts, RecQ, NewDepth);
3775 });
3776 }
3777 case Instruction::InsertElement: {
3778 if (isa<ScalableVectorType>(I->getType()))
3779 break;
3780
3781 const Value *Vec = I->getOperand(0);
3782 const Value *Elt = I->getOperand(1);
3783 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
3784
3785 unsigned NumElts = DemandedElts.getBitWidth();
3786 APInt DemandedVecElts = DemandedElts;
3787 bool SkipElt = false;
3788 // If we know the index we are inserting too, clear it from Vec check.
3789 if (CIdx && CIdx->getValue().ult(NumElts)) {
3790 DemandedVecElts.clearBit(CIdx->getZExtValue());
3791 SkipElt = !DemandedElts[CIdx->getZExtValue()];
3792 }
3793
3794 // Result is zero if Elt is non-zero and rest of the demanded elts in Vec
3795 // are non-zero.
3796 return (SkipElt || isKnownNonZero(Elt, Q, Depth)) &&
3797 (DemandedVecElts.isZero() ||
3798 isKnownNonZero(Vec, DemandedVecElts, Q, Depth));
3799 }
3800 case Instruction::ExtractElement:
3801 if (const auto *EEI = dyn_cast<ExtractElementInst>(I)) {
3802 const Value *Vec = EEI->getVectorOperand();
3803 const Value *Idx = EEI->getIndexOperand();
3804 auto *CIdx = dyn_cast<ConstantInt>(Idx);
3805 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
3806 unsigned NumElts = VecTy->getNumElements();
3807 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
3808 if (CIdx && CIdx->getValue().ult(NumElts))
3809 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
3810 return isKnownNonZero(Vec, DemandedVecElts, Q, Depth);
3811 }
3812 }
3813 break;
3814 case Instruction::ShuffleVector: {
3815 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
3816 if (!Shuf)
3817 break;
3818 APInt DemandedLHS, DemandedRHS;
3819 // For undef elements, we don't know anything about the common state of
3820 // the shuffle result.
3821 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
3822 break;
3823 // If demanded elements for both vecs are non-zero, the shuffle is non-zero.
3824 return (DemandedRHS.isZero() ||
3825 isKnownNonZero(Shuf->getOperand(1), DemandedRHS, Q, Depth)) &&
3826 (DemandedLHS.isZero() ||
3827 isKnownNonZero(Shuf->getOperand(0), DemandedLHS, Q, Depth));
3828 }
3829 case Instruction::Freeze:
3830 return isKnownNonZero(I->getOperand(0), Q, Depth) &&
3831 isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CtxI, Q.DT,
3832 Depth);
3833 case Instruction::Load: {
3834 auto *LI = cast<LoadInst>(I);
3835 // A Load tagged with nonnull or dereferenceable with null pointer undefined
3836 // is never null.
3837 if (auto *PtrT = dyn_cast<PointerType>(I->getType())) {
3838 if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull) ||
3839 (Q.IIQ.getMetadata(LI, LLVMContext::MD_dereferenceable) &&
3840 !NullPointerIsDefined(LI->getFunction(), PtrT->getAddressSpace())))
3841 return true;
3842 } else if (MDNode *Ranges = Q.IIQ.getMetadata(LI, LLVMContext::MD_range)) {
3844 }
3845
3846 // No need to fall through to computeKnownBits as range metadata is already
3847 // handled in isKnownNonZero.
3848 return false;
3849 }
3850 case Instruction::ExtractValue: {
3851 const WithOverflowInst *WO;
3853 switch (WO->getBinaryOp()) {
3854 default:
3855 break;
3856 case Instruction::Add:
3857 return isNonZeroAdd(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3858 WO->getArgOperand(1),
3859 /*NSW=*/false,
3860 /*NUW=*/false, Depth);
3861 case Instruction::Sub:
3862 return isNonZeroSub(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3863 WO->getArgOperand(1), Depth);
3864 case Instruction::Mul:
3865 return isNonZeroMul(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3866 WO->getArgOperand(1),
3867 /*NSW=*/false, /*NUW=*/false, Depth);
3868 break;
3869 }
3870 }
3871 break;
3872 }
3873 case Instruction::Call:
3874 case Instruction::Invoke: {
3875 const auto *Call = cast<CallBase>(I);
3876 if (I->getType()->isPointerTy()) {
3877 if (Call->isReturnNonNull())
3878 return true;
3879 if (const auto *RP = getArgumentAliasingToReturnedPointer(
3880 Call, /*MustPreserveOffset=*/true))
3881 return isKnownNonZero(RP, Q, Depth);
3882 } else {
3883 if (MDNode *Ranges = Q.IIQ.getMetadata(Call, LLVMContext::MD_range))
3885 if (std::optional<ConstantRange> Range = Call->getRange()) {
3886 const APInt ZeroValue(Range->getBitWidth(), 0);
3887 if (!Range->contains(ZeroValue))
3888 return true;
3889 }
3890 if (const Value *RV = Call->getReturnedArgOperand())
3891 if (RV->getType() == I->getType() && isKnownNonZero(RV, Q, Depth))
3892 return true;
3893 }
3894
3895 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
3896 switch (II->getIntrinsicID()) {
3897 case Intrinsic::sshl_sat:
3898 case Intrinsic::ushl_sat:
3899 case Intrinsic::abs:
3900 case Intrinsic::bitreverse:
3901 case Intrinsic::bswap:
3902 case Intrinsic::ctpop:
3903 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3904 // NB: We don't do usub_sat here as in any case we can prove its
3905 // non-zero, we will fold it to `sub nuw` in InstCombine.
3906 case Intrinsic::ssub_sat:
3907 // For most types, if x != y then ssub.sat x, y != 0. But
3908 // ssub.sat.i1 0, -1 = 0, because 1 saturates to 0. This means
3909 // isNonZeroSub will do the wrong thing for ssub.sat.i1.
3910 if (BitWidth == 1)
3911 return false;
3912 return isNonZeroSub(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3913 II->getArgOperand(1), Depth);
3914 case Intrinsic::sadd_sat:
3915 return isNonZeroAdd(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3916 II->getArgOperand(1),
3917 /*NSW=*/true, /* NUW=*/false, Depth);
3918 // Vec reverse preserves zero/non-zero status from input vec.
3919 case Intrinsic::vector_reverse:
3920 return isKnownNonZero(II->getArgOperand(0), DemandedElts.reverseBits(),
3921 Q, Depth);
3922 // umin/smin/smax/smin/or of all non-zero elements is always non-zero.
3923 case Intrinsic::vector_reduce_or:
3924 case Intrinsic::vector_reduce_umax:
3925 case Intrinsic::vector_reduce_umin:
3926 case Intrinsic::vector_reduce_smax:
3927 case Intrinsic::vector_reduce_smin:
3928 return isKnownNonZero(II->getArgOperand(0), Q, Depth);
3929 case Intrinsic::umax:
3930 case Intrinsic::uadd_sat:
3931 // umax(X, (X != 0)) is non zero
3932 // X +usat (X != 0) is non zero
3933 if (matchOpWithOpEqZero(II->getArgOperand(0), II->getArgOperand(1)))
3934 return true;
3935
3936 return isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth) ||
3937 isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3938 case Intrinsic::smax: {
3939 // If either arg is strictly positive the result is non-zero. Otherwise
3940 // the result is non-zero if both ops are non-zero.
3941 auto IsNonZero = [&](Value *Op, std::optional<bool> &OpNonZero,
3942 const KnownBits &OpKnown) {
3943 if (!OpNonZero.has_value())
3944 OpNonZero = OpKnown.isNonZero() ||
3945 isKnownNonZero(Op, DemandedElts, Q, Depth);
3946 return *OpNonZero;
3947 };
3948 // Avoid re-computing isKnownNonZero.
3949 std::optional<bool> Op0NonZero, Op1NonZero;
3950 KnownBits Op1Known =
3951 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3952 if (Op1Known.isNonNegative() &&
3953 IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known))
3954 return true;
3955 KnownBits Op0Known =
3956 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3957 if (Op0Known.isNonNegative() &&
3958 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known))
3959 return true;
3960 return IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known) &&
3961 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known);
3962 }
3963 case Intrinsic::smin: {
3964 // If either arg is negative the result is non-zero. Otherwise
3965 // the result is non-zero if both ops are non-zero.
3966 KnownBits Op1Known =
3967 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3968 if (Op1Known.isNegative())
3969 return true;
3970 KnownBits Op0Known =
3971 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3972 if (Op0Known.isNegative())
3973 return true;
3974
3975 if (Op1Known.isNonZero() && Op0Known.isNonZero())
3976 return true;
3977 }
3978 [[fallthrough]];
3979 case Intrinsic::umin:
3980 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth) &&
3981 isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth);
3982 case Intrinsic::cttz:
3983 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3984 .Zero[0];
3985 case Intrinsic::ctlz:
3986 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3987 .isNonNegative();
3988 case Intrinsic::fshr:
3989 case Intrinsic::fshl:
3990 // If Op0 == Op1, this is a rotate. rotate(x, y) != 0 iff x != 0.
3991 if (II->getArgOperand(0) == II->getArgOperand(1))
3992 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3993 break;
3994 case Intrinsic::vscale:
3995 return true;
3996 case Intrinsic::experimental_get_vector_length:
3997 return isKnownNonZero(I->getOperand(0), Q, Depth);
3998 default:
3999 break;
4000 }
4001 break;
4002 }
4003
4004 return false;
4005 }
4006 }
4007
4009 computeKnownBits(I, DemandedElts, Known, Q, Depth);
4010 return Known.One != 0;
4011}
4012
4013/// Return true if the given value is known to be non-zero when defined. For
4014/// vectors, return true if every demanded element is known to be non-zero when
4015/// defined. For pointers, if the context instruction and dominator tree are
4016/// specified, perform context-sensitive analysis and return true if the
4017/// pointer couldn't possibly be null at the specified instruction.
4018/// Supports values with integer or pointer type and vectors of integers.
4019bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
4020 const SimplifyQuery &Q, unsigned Depth) {
4021 Type *Ty = V->getType();
4022
4023#ifndef NDEBUG
4024 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4025
4026 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
4027 assert(
4028 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
4029 "DemandedElt width should equal the fixed vector number of elements");
4030 } else {
4031 assert(DemandedElts == APInt(1, 1) &&
4032 "DemandedElt width should be 1 for scalars");
4033 }
4034#endif
4035
4036 if (auto *C = dyn_cast<Constant>(V)) {
4037 if (C->isNullValue())
4038 return false;
4039 if (isa<ConstantInt>(C))
4040 // Must be non-zero due to null test above.
4041 return true;
4042
4043 // For constant vectors, check that all elements are poison or known
4044 // non-zero to determine that the whole vector is known non-zero.
4045 if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
4046 for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
4047 if (!DemandedElts[i])
4048 continue;
4049 Constant *Elt = C->getAggregateElement(i);
4050 if (!Elt || Elt->isNullValue())
4051 return false;
4052 if (!isa<PoisonValue>(Elt) && !isa<ConstantInt>(Elt))
4053 return false;
4054 }
4055 return true;
4056 }
4057
4058 // Constant ptrauth can be null, iff the base pointer can be.
4059 if (auto *CPA = dyn_cast<ConstantPtrAuth>(V))
4060 return isKnownNonZero(CPA->getPointer(), DemandedElts, Q, Depth);
4061
4062 // A global variable is non null unless extern weak or an absolute symbol
4063 // reference. An alias is also excluded, as it may resolve to a weak
4064 // undefined symbol and thus to the null address. Where null is a valid
4065 // address an allocation may start there, so nothing can be assumed; that
4066 // covers both null_pointer_is_valid and address spaces other than zero.
4067 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
4069 GV->getType()->getAddressSpace()) &&
4070 !GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&
4071 !isa<GlobalAlias>(GV))
4072 return true;
4073 }
4074
4075 // For constant expressions, fall through to the Operator code below.
4076 if (!isa<ConstantExpr>(V))
4077 return false;
4078 }
4079
4080 if (const auto *A = dyn_cast<Argument>(V))
4081 if (std::optional<ConstantRange> Range = A->getRange()) {
4082 const APInt ZeroValue(Range->getBitWidth(), 0);
4083 if (!Range->contains(ZeroValue))
4084 return true;
4085 }
4086
4088 return true;
4089
4090 // Some of the tests below are recursive, so bail out if we hit the limit.
4092 return false;
4093
4094 // Check for pointer simplifications.
4095
4096 if (PointerType *PtrTy = dyn_cast<PointerType>(Ty)) {
4097 // A byval, inalloca may not be null in a non-default addres space. A
4098 // nonnull argument is assumed never 0.
4099 if (const Argument *A = dyn_cast<Argument>(V)) {
4100 if (((A->hasPassPointeeByValueCopyAttr() &&
4101 !NullPointerIsDefined(A->getParent(), PtrTy->getAddressSpace())) ||
4102 A->hasNonNullAttr()))
4103 return true;
4104 }
4105 }
4106
4107 if (const auto *I = dyn_cast<Operator>(V))
4108 if (isKnownNonZeroFromOperator(I, DemandedElts, Q, Depth))
4109 return true;
4110
4111 if (!isa<Constant>(V) &&
4113 return true;
4114
4115 if (const Value *Stripped = stripNullTest(V))
4116 return isKnownNonZero(Stripped, DemandedElts, Q, Depth);
4117
4118 return false;
4119}
4120
4122 unsigned Depth) {
4123 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
4124 APInt DemandedElts =
4125 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
4126 return ::isKnownNonZero(V, DemandedElts, Q, Depth);
4127}
4128
4129/// If the pair of operators are the same invertible function, return the
4130/// the operands of the function corresponding to each input. Otherwise,
4131/// return std::nullopt. An invertible function is one that is 1-to-1 and maps
4132/// every input value to exactly one output value. This is equivalent to
4133/// saying that Op1 and Op2 are equal exactly when the specified pair of
4134/// operands are equal, (except that Op1 and Op2 may be poison more often.)
4135static std::optional<std::pair<Value*, Value*>>
4137 const Operator *Op2) {
4138 if (Op1->getOpcode() != Op2->getOpcode())
4139 return std::nullopt;
4140
4141 auto getOperands = [&](unsigned OpNum) -> auto {
4142 return std::make_pair(Op1->getOperand(OpNum), Op2->getOperand(OpNum));
4143 };
4144
4145 switch (Op1->getOpcode()) {
4146 default:
4147 break;
4148 case Instruction::Or:
4149 if (!cast<PossiblyDisjointInst>(Op1)->isDisjoint() ||
4150 !cast<PossiblyDisjointInst>(Op2)->isDisjoint())
4151 break;
4152 [[fallthrough]];
4153 case Instruction::Xor:
4154 case Instruction::Add: {
4155 Value *Other;
4156 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(0)), m_Value(Other))))
4157 return std::make_pair(Op1->getOperand(1), Other);
4158 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(1)), m_Value(Other))))
4159 return std::make_pair(Op1->getOperand(0), Other);
4160 break;
4161 }
4162 case Instruction::Sub:
4163 if (Op1->getOperand(0) == Op2->getOperand(0))
4164 return getOperands(1);
4165 if (Op1->getOperand(1) == Op2->getOperand(1))
4166 return getOperands(0);
4167 break;
4168 case Instruction::Mul: {
4169 // invertible if A * B == (A * B) mod 2^N where A, and B are integers
4170 // and N is the bitwdith. The nsw case is non-obvious, but proven by
4171 // alive2: https://alive2.llvm.org/ce/z/Z6D5qK
4172 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
4173 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
4174 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
4175 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
4176 break;
4177
4178 // Assume operand order has been canonicalized
4179 if (Op1->getOperand(1) == Op2->getOperand(1) &&
4180 isa<ConstantInt>(Op1->getOperand(1)) &&
4181 !cast<ConstantInt>(Op1->getOperand(1))->isZero())
4182 return getOperands(0);
4183 break;
4184 }
4185 case Instruction::Shl: {
4186 // Same as multiplies, with the difference that we don't need to check
4187 // for a non-zero multiply. Shifts always multiply by non-zero.
4188 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
4189 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
4190 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
4191 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
4192 break;
4193
4194 if (Op1->getOperand(1) == Op2->getOperand(1))
4195 return getOperands(0);
4196 break;
4197 }
4198 case Instruction::AShr:
4199 case Instruction::LShr: {
4200 auto *PEO1 = cast<PossiblyExactOperator>(Op1);
4201 auto *PEO2 = cast<PossiblyExactOperator>(Op2);
4202 if (!PEO1->isExact() || !PEO2->isExact())
4203 break;
4204
4205 if (Op1->getOperand(1) == Op2->getOperand(1))
4206 return getOperands(0);
4207 break;
4208 }
4209 case Instruction::SExt:
4210 case Instruction::ZExt:
4211 if (Op1->getOperand(0)->getType() == Op2->getOperand(0)->getType())
4212 return getOperands(0);
4213 break;
4214 case Instruction::PHI: {
4215 const PHINode *PN1 = cast<PHINode>(Op1);
4216 const PHINode *PN2 = cast<PHINode>(Op2);
4217
4218 // If PN1 and PN2 are both recurrences, can we prove the entire recurrences
4219 // are a single invertible function of the start values? Note that repeated
4220 // application of an invertible function is also invertible
4221 BinaryOperator *BO1 = nullptr;
4222 Value *Start1 = nullptr, *Step1 = nullptr;
4223 BinaryOperator *BO2 = nullptr;
4224 Value *Start2 = nullptr, *Step2 = nullptr;
4225 if (PN1->getParent() != PN2->getParent() ||
4226 !matchSimpleRecurrence(PN1, BO1, Start1, Step1) ||
4227 !matchSimpleRecurrence(PN2, BO2, Start2, Step2))
4228 break;
4229
4231 cast<Operator>(BO2));
4232 if (!Values)
4233 break;
4234
4235 // We have to be careful of mutually defined recurrences here. Ex:
4236 // * X_i = X_(i-1) OP Y_(i-1), and Y_i = X_(i-1) OP V
4237 // * X_i = Y_i = X_(i-1) OP Y_(i-1)
4238 // The invertibility of these is complicated, and not worth reasoning
4239 // about (yet?).
4240 if (Values->first != PN1 || Values->second != PN2)
4241 break;
4242
4243 return std::make_pair(Start1, Start2);
4244 }
4245 }
4246 return std::nullopt;
4247}
4248
4249/// Return true if V1 == (binop V2, X), where X is known non-zero.
4250/// Only handle a small subset of binops where (binop V2, X) with non-zero X
4251/// implies V2 != V1.
4252static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2,
4253 const APInt &DemandedElts,
4254 const SimplifyQuery &Q, unsigned Depth) {
4256 if (!BO)
4257 return false;
4258 switch (BO->getOpcode()) {
4259 default:
4260 break;
4261 case Instruction::Or:
4262 if (!cast<PossiblyDisjointInst>(V1)->isDisjoint())
4263 break;
4264 [[fallthrough]];
4265 case Instruction::Xor:
4266 case Instruction::Add:
4267 Value *Op = nullptr;
4268 if (V2 == BO->getOperand(0))
4269 Op = BO->getOperand(1);
4270 else if (V2 == BO->getOperand(1))
4271 Op = BO->getOperand(0);
4272 else
4273 return false;
4274 return isKnownNonZero(Op, DemandedElts, Q, Depth + 1);
4275 }
4276 return false;
4277}
4278
4279/// Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and
4280/// the multiplication is nuw or nsw.
4281static bool isNonEqualMul(const Value *V1, const Value *V2,
4282 const APInt &DemandedElts, const SimplifyQuery &Q,
4283 unsigned Depth) {
4284 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
4285 const APInt *C;
4286 return match(OBO, m_Mul(m_Specific(V1), m_APInt(C))) &&
4287 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
4288 !C->isZero() && !C->isOne() &&
4289 isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
4290 }
4291 return false;
4292}
4293
4294/// Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and
4295/// the shift is nuw or nsw.
4296static bool isNonEqualShl(const Value *V1, const Value *V2,
4297 const APInt &DemandedElts, const SimplifyQuery &Q,
4298 unsigned Depth) {
4299 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
4300 const APInt *C;
4301 return match(OBO, m_Shl(m_Specific(V1), m_APInt(C))) &&
4302 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
4303 !C->isZero() && isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
4304 }
4305 return false;
4306}
4307
4308static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2,
4309 const APInt &DemandedElts, const SimplifyQuery &Q,
4310 unsigned Depth) {
4311 // Check two PHIs are in same block.
4312 if (PN1->getParent() != PN2->getParent())
4313 return false;
4314
4316 bool UsedFullRecursion = false;
4317 for (const BasicBlock *IncomBB : PN1->blocks()) {
4318 if (!VisitedBBs.insert(IncomBB).second)
4319 continue; // Don't reprocess blocks that we have dealt with already.
4320 const Value *IV1 = PN1->getIncomingValueForBlock(IncomBB);
4321 const Value *IV2 = PN2->getIncomingValueForBlock(IncomBB);
4322 const APInt *C1, *C2;
4323 if (match(IV1, m_APInt(C1)) && match(IV2, m_APInt(C2)) && *C1 != *C2)
4324 continue;
4325
4326 // Only one pair of phi operands is allowed for full recursion.
4327 if (UsedFullRecursion)
4328 return false;
4329
4331 RecQ.CtxI = IncomBB->getTerminator();
4332 if (!isKnownNonEqual(IV1, IV2, DemandedElts, RecQ, Depth + 1))
4333 return false;
4334 UsedFullRecursion = true;
4335 }
4336 return true;
4337}
4338
4339static bool isNonEqualSelect(const Value *V1, const Value *V2,
4340 const APInt &DemandedElts, const SimplifyQuery &Q,
4341 unsigned Depth) {
4342 const SelectInst *SI1 = dyn_cast<SelectInst>(V1);
4343 if (!SI1)
4344 return false;
4345
4346 if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2)) {
4347 const Value *Cond1 = SI1->getCondition();
4348 const Value *Cond2 = SI2->getCondition();
4349 if (Cond1 == Cond2)
4350 return isKnownNonEqual(SI1->getTrueValue(), SI2->getTrueValue(),
4351 DemandedElts, Q, Depth + 1) &&
4352 isKnownNonEqual(SI1->getFalseValue(), SI2->getFalseValue(),
4353 DemandedElts, Q, Depth + 1);
4354 }
4355 return isKnownNonEqual(SI1->getTrueValue(), V2, DemandedElts, Q, Depth + 1) &&
4356 isKnownNonEqual(SI1->getFalseValue(), V2, DemandedElts, Q, Depth + 1);
4357}
4358
4359// Check to see if A is both a GEP and is the incoming value for a PHI in the
4360// loop, and B is either a ptr or another GEP. If the PHI has 2 incoming values,
4361// one of them being the recursive GEP A and the other a ptr at same base and at
4362// the same/higher offset than B we are only incrementing the pointer further in
4363// loop if offset of recursive GEP is greater than 0.
4365 const SimplifyQuery &Q) {
4366 if (!A->getType()->isPointerTy() || !B->getType()->isPointerTy())
4367 return false;
4368
4369 auto *GEPA = dyn_cast<GEPOperator>(A);
4370 if (!GEPA || GEPA->getNumIndices() != 1 || !isa<Constant>(GEPA->idx_begin()))
4371 return false;
4372
4373 // Handle 2 incoming PHI values with one being a recursive GEP.
4374 auto *PN = dyn_cast<PHINode>(GEPA->getPointerOperand());
4375 if (!PN || PN->getNumIncomingValues() != 2)
4376 return false;
4377
4378 // Search for the recursive GEP as an incoming operand, and record that as
4379 // Step.
4380 Value *Start = nullptr;
4381 Value *Step = const_cast<Value *>(A);
4382 if (PN->getIncomingValue(0) == Step)
4383 Start = PN->getIncomingValue(1);
4384 else if (PN->getIncomingValue(1) == Step)
4385 Start = PN->getIncomingValue(0);
4386 else
4387 return false;
4388
4389 // Other incoming node base should match the B base.
4390 // StartOffset >= OffsetB && StepOffset > 0?
4391 // StartOffset <= OffsetB && StepOffset < 0?
4392 // Is non-equal if above are true.
4393 // We use stripAndAccumulateInBoundsConstantOffsets to restrict the
4394 // optimisation to inbounds GEPs only.
4395 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(Start->getType());
4396 APInt StartOffset(IndexWidth, 0);
4397 Start = Start->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StartOffset);
4398 APInt StepOffset(IndexWidth, 0);
4399 Step = Step->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StepOffset);
4400
4401 // Check if Base Pointer of Step matches the PHI.
4402 if (Step != PN)
4403 return false;
4404 APInt OffsetB(IndexWidth, 0);
4405 B = B->stripAndAccumulateInBoundsConstantOffsets(Q.DL, OffsetB);
4406 return Start == B &&
4407 ((StartOffset.sge(OffsetB) && StepOffset.isStrictlyPositive()) ||
4408 (StartOffset.sle(OffsetB) && StepOffset.isNegative()));
4409}
4410
4411static bool isKnownNonEqualFromContext(const Value *V1, const Value *V2,
4412 const SimplifyQuery &Q, unsigned Depth) {
4413 if (!Q.CtxI)
4414 return false;
4415
4416 // Try to infer NonEqual based on information from dominating conditions.
4417 if (Q.DC && Q.DT) {
4418 auto IsKnownNonEqualFromDominatingCondition = [&](const Value *V) {
4419 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
4420 Value *Cond = BI->getCondition();
4421 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4422 if (Q.DT->dominates(Edge0, Q.CtxI->getParent()) &&
4424 /*LHSIsTrue=*/true, Depth)
4425 .value_or(false))
4426 return true;
4427
4428 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4429 if (Q.DT->dominates(Edge1, Q.CtxI->getParent()) &&
4431 /*LHSIsTrue=*/false, Depth)
4432 .value_or(false))
4433 return true;
4434 }
4435
4436 return false;
4437 };
4438
4439 if (IsKnownNonEqualFromDominatingCondition(V1) ||
4440 IsKnownNonEqualFromDominatingCondition(V2))
4441 return true;
4442 }
4443
4444 if (!Q.AC)
4445 return false;
4446
4447 // Try to infer NonEqual based on information from assumptions.
4448 for (auto &AssumeVH : Q.AC->assumptionsFor(V1)) {
4449 if (!AssumeVH)
4450 continue;
4451 CallInst *I = cast<CallInst>(AssumeVH);
4452
4453 assert(I->getFunction() == Q.CtxI->getFunction() &&
4454 "Got assumption for the wrong function!");
4455 assert(I->getIntrinsicID() == Intrinsic::assume &&
4456 "must be an assume intrinsic");
4457
4458 if (isImpliedCondition(I->getArgOperand(0), ICmpInst::ICMP_NE, V1, V2, Q.DL,
4459 /*LHSIsTrue=*/true, Depth)
4460 .value_or(false) &&
4462 return true;
4463 }
4464
4465 return false;
4466}
4467
4468static bool isNonEqualURem(const Value *X, const Value *Rem,
4469 const SimplifyQuery &Q) {
4470 const Value *Y;
4471 if (!match(Rem, m_URem(m_Specific(X), m_Value(Y))))
4472 return false;
4473
4474 // For a defined urem, X != X urem Y exactly when X u>= Y.
4475 // isTruePredicate does not handle UGE, so use the equivalent Y u<= X.
4477 return true;
4478
4479 std::optional<bool> Implied =
4481 return Implied && *Implied;
4482}
4483
4484/// Return true if it is known that V1 != V2.
4485static bool isKnownNonEqual(const Value *V1, const Value *V2,
4486 const APInt &DemandedElts, const SimplifyQuery &Q,
4487 unsigned Depth) {
4488 if (V1 == V2)
4489 return false;
4490 if (V1->getType() != V2->getType())
4491 // We can't look through casts yet.
4492 return false;
4493
4495 return false;
4496
4497 // See if we can recurse through (exactly one of) our operands. This
4498 // requires our operation be 1-to-1 and map every input value to exactly
4499 // one output value. Such an operation is invertible.
4500 auto *O1 = dyn_cast<Operator>(V1);
4501 auto *O2 = dyn_cast<Operator>(V2);
4502 if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) {
4503 if (auto Values = getInvertibleOperands(O1, O2))
4504 return isKnownNonEqual(Values->first, Values->second, DemandedElts, Q,
4505 Depth + 1);
4506
4507 if (const PHINode *PN1 = dyn_cast<PHINode>(V1)) {
4508 const PHINode *PN2 = cast<PHINode>(V2);
4509 // FIXME: This is missing a generalization to handle the case where one is
4510 // a PHI and another one isn't.
4511 if (isNonEqualPHIs(PN1, PN2, DemandedElts, Q, Depth))
4512 return true;
4513 };
4514 }
4515
4516 if (isModifyingBinopOfNonZero(V1, V2, DemandedElts, Q, Depth) ||
4517 isModifyingBinopOfNonZero(V2, V1, DemandedElts, Q, Depth))
4518 return true;
4519
4520 if (isNonEqualMul(V1, V2, DemandedElts, Q, Depth) ||
4521 isNonEqualMul(V2, V1, DemandedElts, Q, Depth))
4522 return true;
4523
4524 if (isNonEqualShl(V1, V2, DemandedElts, Q, Depth) ||
4525 isNonEqualShl(V2, V1, DemandedElts, Q, Depth))
4526 return true;
4527
4528 if (V1->getType()->isIntOrIntVectorTy()) {
4529 // Are any known bits in V1 contradictory to known bits in V2? If V1
4530 // has a known zero where V2 has a known one, they must not be equal.
4531 KnownBits Known1 = computeKnownBits(V1, DemandedElts, Q, Depth);
4532 if (!Known1.isUnknown()) {
4533 KnownBits Known2 = computeKnownBits(V2, DemandedElts, Q, Depth);
4534 if (Known1.Zero.intersects(Known2.One) ||
4535 Known2.Zero.intersects(Known1.One))
4536 return true;
4537 }
4538 }
4539
4540 if (isNonEqualSelect(V1, V2, DemandedElts, Q, Depth) ||
4541 isNonEqualSelect(V2, V1, DemandedElts, Q, Depth))
4542 return true;
4543
4546 return true;
4547
4548 Value *A, *B;
4549 // PtrToInts are NonEqual if their Ptrs are NonEqual.
4550 // Check PtrToInt type matches the pointer size.
4551 if (match(V1, m_PtrToIntSameSize(Q.DL, m_Value(A))) &&
4553 return isKnownNonEqual(A, B, DemandedElts, Q, Depth + 1);
4554
4555 if (isNonEqualURem(V1, V2, Q) || isNonEqualURem(V2, V1, Q))
4556 return true;
4557
4558 if (isKnownNonEqualFromContext(V1, V2, Q, Depth))
4559 return true;
4560
4561 return false;
4562}
4563
4564/// For vector constants, loop over the elements and find the constant with the
4565/// minimum number of sign bits. Return 0 if the value is not a vector constant
4566/// or if any element was not analyzed; otherwise, return the count for the
4567/// element with the minimum number of sign bits.
4569 const APInt &DemandedElts,
4570 unsigned TyBits) {
4571 const auto *CV = dyn_cast<Constant>(V);
4572 if (!CV || !isa<FixedVectorType>(CV->getType()))
4573 return 0;
4574
4575 unsigned MinSignBits = TyBits;
4576 unsigned NumElts = cast<FixedVectorType>(CV->getType())->getNumElements();
4577 for (unsigned i = 0; i != NumElts; ++i) {
4578 if (!DemandedElts[i])
4579 continue;
4580 // If we find a non-ConstantInt, bail out.
4581 auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));
4582 if (!Elt)
4583 return 0;
4584
4585 MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());
4586 }
4587
4588 return MinSignBits;
4589}
4590
4591static unsigned ComputeNumSignBitsImpl(const Value *V,
4592 const APInt &DemandedElts,
4593 const SimplifyQuery &Q, unsigned Depth);
4594
4595static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
4596 const SimplifyQuery &Q, unsigned Depth) {
4597 unsigned Result = ComputeNumSignBitsImpl(V, DemandedElts, Q, Depth);
4598 assert(Result > 0 && "At least one sign bit needs to be present!");
4599 return Result;
4600}
4601
4602/// Return the number of times the sign bit of the register is replicated into
4603/// the other bits. We know that at least 1 bit is always equal to the sign bit
4604/// (itself), but other cases can give us information. For example, immediately
4605/// after an "ashr X, 2", we know that the top 3 bits are all equal to each
4606/// other, so we return 3. For vectors, return the number of sign bits for the
4607/// vector element with the minimum number of known sign bits of the demanded
4608/// elements in the vector specified by DemandedElts.
4609static unsigned ComputeNumSignBitsImpl(const Value *V,
4610 const APInt &DemandedElts,
4611 const SimplifyQuery &Q, unsigned Depth) {
4612 Type *Ty = V->getType();
4613#ifndef NDEBUG
4614 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4615
4616 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
4617 assert(
4618 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
4619 "DemandedElt width should equal the fixed vector number of elements");
4620 } else {
4621 assert(DemandedElts == APInt(1, 1) &&
4622 "DemandedElt width should be 1 for scalars");
4623 }
4624#endif
4625
4626 // We return the minimum number of sign bits that are guaranteed to be present
4627 // in V, so for undef we have to conservatively return 1. We don't have the
4628 // same behavior for poison though -- that's a FIXME today.
4629
4630 Type *ScalarTy = Ty->getScalarType();
4631 unsigned TyBits = ScalarTy->isPointerTy() ?
4632 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
4633 Q.DL.getTypeSizeInBits(ScalarTy);
4634
4635 unsigned Tmp, Tmp2;
4636 unsigned FirstAnswer = 1;
4637
4638 // Note that ConstantInt is handled by the general computeKnownBits case
4639 // below.
4640
4642 return 1;
4643
4644 if (auto *U = dyn_cast<Operator>(V)) {
4645 switch (Operator::getOpcode(V)) {
4646 default: break;
4647 case Instruction::BitCast: {
4648 Value *Src = U->getOperand(0);
4649 Type *SrcTy = Src->getType();
4650
4651 // Skip if the source type is not an integer or integer vector type
4652 // This ensures we only process integer-like types
4653 if (!SrcTy->isIntOrIntVectorTy())
4654 break;
4655
4656 unsigned SrcBits = SrcTy->getScalarSizeInBits();
4657
4658 // Bitcast 'large element' scalar/vector to 'small element' vector.
4659 if ((SrcBits % TyBits) != 0)
4660 break;
4661
4662 // Only proceed if the destination type is a fixed-size vector
4663 if (isa<FixedVectorType>(Ty)) {
4664 // Fast case - sign splat can be simply split across the small elements.
4665 // This works for both vector and scalar sources
4666 Tmp = ComputeNumSignBits(Src, Q, Depth + 1);
4667 if (Tmp == SrcBits)
4668 return TyBits;
4669 }
4670 break;
4671 }
4672 case Instruction::SExt:
4673 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
4674 return ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1) +
4675 Tmp;
4676
4677 case Instruction::SDiv: {
4678 const APInt *Denominator;
4679 // sdiv X, C -> adds log(C) sign bits.
4680 if (match(U->getOperand(1), m_APInt(Denominator))) {
4681
4682 // Ignore non-positive denominator.
4683 if (!Denominator->isStrictlyPositive())
4684 break;
4685
4686 // Calculate the incoming numerator bits.
4687 unsigned NumBits =
4688 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4689
4690 // Add floor(log(C)) bits to the numerator bits.
4691 return std::min(TyBits, NumBits + Denominator->logBase2());
4692 }
4693 break;
4694 }
4695
4696 case Instruction::SRem: {
4697 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4698
4699 const APInt *Denominator;
4700 // srem X, C -> we know that the result is within [-C+1,C) when C is a
4701 // positive constant. This let us put a lower bound on the number of sign
4702 // bits.
4703 if (match(U->getOperand(1), m_APInt(Denominator))) {
4704
4705 // Ignore non-positive denominator.
4706 if (Denominator->isStrictlyPositive()) {
4707 // Calculate the leading sign bit constraints by examining the
4708 // denominator. Given that the denominator is positive, there are two
4709 // cases:
4710 //
4711 // 1. The numerator is positive. The result range is [0,C) and
4712 // [0,C) u< (1 << ceilLogBase2(C)).
4713 //
4714 // 2. The numerator is negative. Then the result range is (-C,0] and
4715 // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
4716 //
4717 // Thus a lower bound on the number of sign bits is `TyBits -
4718 // ceilLogBase2(C)`.
4719
4720 unsigned ResBits = TyBits - Denominator->ceilLogBase2();
4721 Tmp = std::max(Tmp, ResBits);
4722 }
4723 }
4724 return Tmp;
4725 }
4726
4727 case Instruction::AShr: {
4728 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4729 // ashr X, C -> adds C sign bits. Vectors too.
4730 const APInt *ShAmt;
4731 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4732 if (ShAmt->uge(TyBits))
4733 break; // Bad shift.
4734 unsigned ShAmtLimited = ShAmt->getZExtValue();
4735 Tmp += ShAmtLimited;
4736 if (Tmp > TyBits) Tmp = TyBits;
4737 }
4738 return Tmp;
4739 }
4740 case Instruction::Shl: {
4741 const APInt *ShAmt;
4742 Value *X = nullptr;
4743 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4744 // shl destroys sign bits.
4745 if (ShAmt->uge(TyBits))
4746 break; // Bad shift.
4747 // We can look through a zext (more or less treating it as a sext) if
4748 // all extended bits are shifted out.
4749 if (match(U->getOperand(0), m_ZExt(m_Value(X))) &&
4750 ShAmt->uge(TyBits - X->getType()->getScalarSizeInBits())) {
4751 Tmp = ComputeNumSignBits(X, DemandedElts, Q, Depth + 1);
4752 Tmp += TyBits - X->getType()->getScalarSizeInBits();
4753 } else
4754 Tmp =
4755 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4756 if (ShAmt->uge(Tmp))
4757 break; // Shifted all sign bits out.
4758 Tmp2 = ShAmt->getZExtValue();
4759 return Tmp - Tmp2;
4760 }
4761 break;
4762 }
4763 case Instruction::And:
4764 case Instruction::Or:
4765 case Instruction::Xor: // NOT is handled here.
4766 // Logical binary ops preserve the number of sign bits at the worst.
4767 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4768 if (Tmp != 1) {
4769 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4770 FirstAnswer = std::min(Tmp, Tmp2);
4771 // We computed what we know about the sign bits as our first
4772 // answer. Now proceed to the generic code that uses
4773 // computeKnownBits, and pick whichever answer is better.
4774 }
4775 break;
4776
4777 case Instruction::Select: {
4778 // If we have a clamp pattern, we know that the number of sign bits will
4779 // be the minimum of the clamp min/max range.
4780 const Value *X;
4781 const APInt *CLow, *CHigh;
4782 if (isSignedMinMaxClamp(U, X, CLow, CHigh))
4783 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4784
4785 Tmp = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4786 if (Tmp == 1)
4787 break;
4788 Tmp2 = ComputeNumSignBits(U->getOperand(2), DemandedElts, Q, Depth + 1);
4789 return std::min(Tmp, Tmp2);
4790 }
4791
4792 case Instruction::Add:
4793 // Add can have at most one carry bit. Thus we know that the output
4794 // is, at worst, one more bit than the inputs.
4795 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4796 if (Tmp == 1) break;
4797
4798 // Special case decrementing a value (ADD X, -1):
4799 if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
4800 if (CRHS->isAllOnesValue()) {
4801 KnownBits Known(TyBits);
4802 computeKnownBits(U->getOperand(0), DemandedElts, Known, Q, Depth + 1);
4803
4804 // If the input is known to be 0 or 1, the output is 0/-1, which is
4805 // all sign bits set.
4806 if ((Known.Zero | 1).isAllOnes())
4807 return TyBits;
4808
4809 // If we are subtracting one from a positive number, there is no carry
4810 // out of the result.
4811 if (Known.isNonNegative())
4812 return Tmp;
4813 }
4814
4815 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4816 if (Tmp2 == 1)
4817 break;
4818 return std::min(Tmp, Tmp2) - 1;
4819
4820 case Instruction::Sub:
4821 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4822 if (Tmp2 == 1)
4823 break;
4824
4825 // Handle NEG.
4826 if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
4827 if (CLHS->isNullValue()) {
4828 KnownBits Known(TyBits);
4829 computeKnownBits(U->getOperand(1), DemandedElts, Known, Q, Depth + 1);
4830 // If the input is known to be 0 or 1, the output is 0/-1, which is
4831 // all sign bits set.
4832 if ((Known.Zero | 1).isAllOnes())
4833 return TyBits;
4834
4835 // If the input is known to be positive (the sign bit is known clear),
4836 // the output of the NEG has the same number of sign bits as the
4837 // input.
4838 if (Known.isNonNegative())
4839 return Tmp2;
4840
4841 // Otherwise, we treat this like a SUB.
4842 }
4843
4844 // Sub can have at most one carry bit. Thus we know that the output
4845 // is, at worst, one more bit than the inputs.
4846 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4847 if (Tmp == 1)
4848 break;
4849 return std::min(Tmp, Tmp2) - 1;
4850
4851 case Instruction::Mul: {
4852 // The output of the Mul can be at most twice the valid bits in the
4853 // inputs.
4854 unsigned SignBitsOp0 =
4855 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4856 if (SignBitsOp0 == 1)
4857 break;
4858 unsigned SignBitsOp1 =
4859 ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4860 if (SignBitsOp1 == 1)
4861 break;
4862 unsigned OutValidBits =
4863 (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);
4864 return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
4865 }
4866
4867 case Instruction::PHI: {
4868 const PHINode *PN = cast<PHINode>(U);
4869 unsigned NumIncomingValues = PN->getNumIncomingValues();
4870 // Don't analyze large in-degree PHIs.
4871 if (NumIncomingValues > 4) break;
4872 // Unreachable blocks may have zero-operand PHI nodes.
4873 if (NumIncomingValues == 0) break;
4874
4875 // Take the minimum of all incoming values. This can't infinitely loop
4876 // because of our depth threshold.
4878 Tmp = TyBits;
4879 for (unsigned i = 0, e = NumIncomingValues; i != e; ++i) {
4880 if (Tmp == 1) return Tmp;
4881 RecQ.CtxI = PN->getIncomingBlock(i)->getTerminator();
4882 Tmp = std::min(Tmp, ComputeNumSignBits(PN->getIncomingValue(i),
4883 DemandedElts, RecQ, Depth + 1));
4884 }
4885 return Tmp;
4886 }
4887
4888 case Instruction::Trunc: {
4889 // If the input contained enough sign bits that some remain after the
4890 // truncation, then we can make use of that. Otherwise we don't know
4891 // anything.
4892 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4893 unsigned OperandTyBits = U->getOperand(0)->getType()->getScalarSizeInBits();
4894 if (Tmp > (OperandTyBits - TyBits))
4895 return Tmp - (OperandTyBits - TyBits);
4896
4897 return 1;
4898 }
4899
4900 case Instruction::ExtractElement:
4901 // Look through extract element. At the moment we keep this simple and
4902 // skip tracking the specific element. But at least we might find
4903 // information valid for all elements of the vector (for example if vector
4904 // is sign extended, shifted, etc).
4905 return ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4906
4907 case Instruction::ShuffleVector: {
4908 // Collect the minimum number of sign bits that are shared by every vector
4909 // element referenced by the shuffle.
4910 auto *Shuf = dyn_cast<ShuffleVectorInst>(U);
4911 if (!Shuf) {
4912 // FIXME: Add support for shufflevector constant expressions.
4913 return 1;
4914 }
4915 APInt DemandedLHS, DemandedRHS;
4916 // For undef elements, we don't know anything about the common state of
4917 // the shuffle result.
4918 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
4919 return 1;
4920 Tmp = std::numeric_limits<unsigned>::max();
4921 if (!!DemandedLHS) {
4922 const Value *LHS = Shuf->getOperand(0);
4923 Tmp = ComputeNumSignBits(LHS, DemandedLHS, Q, Depth + 1);
4924 }
4925 // If we don't know anything, early out and try computeKnownBits
4926 // fall-back.
4927 if (Tmp == 1)
4928 break;
4929 if (!!DemandedRHS) {
4930 const Value *RHS = Shuf->getOperand(1);
4931 Tmp2 = ComputeNumSignBits(RHS, DemandedRHS, Q, Depth + 1);
4932 Tmp = std::min(Tmp, Tmp2);
4933 }
4934 // If we don't know anything, early out and try computeKnownBits
4935 // fall-back.
4936 if (Tmp == 1)
4937 break;
4938 assert(Tmp <= TyBits && "Failed to determine minimum sign bits");
4939 return Tmp;
4940 }
4941 case Instruction::Call: {
4942 if (const auto *II = dyn_cast<IntrinsicInst>(U)) {
4943 switch (II->getIntrinsicID()) {
4944 default:
4945 break;
4946 case Intrinsic::abs:
4947 Tmp =
4948 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4949 if (Tmp == 1)
4950 break;
4951
4952 // Absolute value reduces number of sign bits by at most 1.
4953 return Tmp - 1;
4954 case Intrinsic::smin:
4955 case Intrinsic::smax: {
4956 const APInt *CLow, *CHigh;
4957 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
4958 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4959 }
4960 }
4961 }
4962 }
4963 }
4964 }
4965
4966 // Finally, if we can prove that the top bits of the result are 0's or 1's,
4967 // use this information.
4968
4969 // If we can examine all elements of a vector constant successfully, we're
4970 // done (we can't do any better than that). If not, keep trying.
4971 if (unsigned VecSignBits =
4972 computeNumSignBitsVectorConstant(V, DemandedElts, TyBits))
4973 return VecSignBits;
4974
4975 KnownBits Known(TyBits);
4976 computeKnownBits(V, DemandedElts, Known, Q, Depth);
4977
4978 // If we know that the sign bit is either zero or one, determine the number of
4979 // identical bits in the top of the input value.
4980 return std::max(FirstAnswer, Known.countMinSignBits());
4981}
4982
4984 const TargetLibraryInfo *TLI) {
4985 const Function *F = CB.getCalledFunction();
4986 if (!F)
4988
4989 if (F->isIntrinsic())
4990 return F->getIntrinsicID();
4991
4992 // We are going to infer semantics of a library function based on mapping it
4993 // to an LLVM intrinsic. Check that the library function is available from
4994 // this callbase and in this environment.
4995 if (F->hasLocalLinkage() || !TLI || !CB.onlyReadsMemory())
4997
4998 LibFunc Func = TLI->getLibFunc(CB);
4999 if (Func == NotLibFunc)
5001
5002 switch (Func) {
5003 default:
5004 break;
5005 case LibFunc_sin:
5006 case LibFunc_sinf:
5007 case LibFunc_sinl:
5008 return Intrinsic::sin;
5009 case LibFunc_cos:
5010 case LibFunc_cosf:
5011 case LibFunc_cosl:
5012 return Intrinsic::cos;
5013 case LibFunc_tan:
5014 case LibFunc_tanf:
5015 case LibFunc_tanl:
5016 return Intrinsic::tan;
5017 case LibFunc_asin:
5018 case LibFunc_asinf:
5019 case LibFunc_asinl:
5020 return Intrinsic::asin;
5021 case LibFunc_acos:
5022 case LibFunc_acosf:
5023 case LibFunc_acosl:
5024 return Intrinsic::acos;
5025 case LibFunc_atan:
5026 case LibFunc_atanf:
5027 case LibFunc_atanl:
5028 return Intrinsic::atan;
5029 case LibFunc_atan2:
5030 case LibFunc_atan2f:
5031 case LibFunc_atan2l:
5032 return Intrinsic::atan2;
5033 case LibFunc_sinh:
5034 case LibFunc_sinhf:
5035 case LibFunc_sinhl:
5036 return Intrinsic::sinh;
5037 case LibFunc_cosh:
5038 case LibFunc_coshf:
5039 case LibFunc_coshl:
5040 return Intrinsic::cosh;
5041 case LibFunc_tanh:
5042 case LibFunc_tanhf:
5043 case LibFunc_tanhl:
5044 return Intrinsic::tanh;
5045 case LibFunc_exp:
5046 case LibFunc_expf:
5047 case LibFunc_expl:
5048 return Intrinsic::exp;
5049 case LibFunc_exp2:
5050 case LibFunc_exp2f:
5051 case LibFunc_exp2l:
5052 return Intrinsic::exp2;
5053 case LibFunc_exp10:
5054 case LibFunc_exp10f:
5055 case LibFunc_exp10l:
5056 return Intrinsic::exp10;
5057 case LibFunc_log:
5058 case LibFunc_logf:
5059 case LibFunc_logl:
5060 return Intrinsic::log;
5061 case LibFunc_log10:
5062 case LibFunc_log10f:
5063 case LibFunc_log10l:
5064 return Intrinsic::log10;
5065 case LibFunc_log2:
5066 case LibFunc_log2f:
5067 case LibFunc_log2l:
5068 return Intrinsic::log2;
5069 case LibFunc_fabs:
5070 case LibFunc_fabsf:
5071 case LibFunc_fabsl:
5072 return Intrinsic::fabs;
5073 case LibFunc_fmin:
5074 case LibFunc_fminf:
5075 case LibFunc_fminl:
5076 return Intrinsic::minnum;
5077 case LibFunc_fmax:
5078 case LibFunc_fmaxf:
5079 case LibFunc_fmaxl:
5080 return Intrinsic::maxnum;
5081 case LibFunc_copysign:
5082 case LibFunc_copysignf:
5083 case LibFunc_copysignl:
5084 return Intrinsic::copysign;
5085 case LibFunc_floor:
5086 case LibFunc_floorf:
5087 case LibFunc_floorl:
5088 return Intrinsic::floor;
5089 case LibFunc_ceil:
5090 case LibFunc_ceilf:
5091 case LibFunc_ceill:
5092 return Intrinsic::ceil;
5093 case LibFunc_trunc:
5094 case LibFunc_truncf:
5095 case LibFunc_truncl:
5096 return Intrinsic::trunc;
5097 case LibFunc_rint:
5098 case LibFunc_rintf:
5099 case LibFunc_rintl:
5100 return Intrinsic::rint;
5101 case LibFunc_nearbyint:
5102 case LibFunc_nearbyintf:
5103 case LibFunc_nearbyintl:
5104 return Intrinsic::nearbyint;
5105 case LibFunc_round:
5106 case LibFunc_roundf:
5107 case LibFunc_roundl:
5108 return Intrinsic::round;
5109 case LibFunc_roundeven:
5110 case LibFunc_roundevenf:
5111 case LibFunc_roundevenl:
5112 return Intrinsic::roundeven;
5113 case LibFunc_pow:
5114 case LibFunc_powf:
5115 case LibFunc_powl:
5116 return Intrinsic::pow;
5117 case LibFunc_sqrt:
5118 case LibFunc_sqrtf:
5119 case LibFunc_sqrtl:
5120 return Intrinsic::sqrt;
5121 }
5122
5124}
5125
5126/// Given an exploded icmp instruction, return true if the comparison only
5127/// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if
5128/// the result of the comparison is true when the input value is signed.
5130 bool &TrueIfSigned) {
5131 switch (Pred) {
5132 case ICmpInst::ICMP_SLT: // True if LHS s< 0
5133 TrueIfSigned = true;
5134 return RHS.isZero();
5135 case ICmpInst::ICMP_SLE: // True if LHS s<= -1
5136 TrueIfSigned = true;
5137 return RHS.isAllOnes();
5138 case ICmpInst::ICMP_SGT: // True if LHS s> -1
5139 TrueIfSigned = false;
5140 return RHS.isAllOnes();
5141 case ICmpInst::ICMP_SGE: // True if LHS s>= 0
5142 TrueIfSigned = false;
5143 return RHS.isZero();
5144 case ICmpInst::ICMP_UGT:
5145 // True if LHS u> RHS and RHS == sign-bit-mask - 1
5146 TrueIfSigned = true;
5147 return RHS.isMaxSignedValue();
5148 case ICmpInst::ICMP_UGE:
5149 // True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
5150 TrueIfSigned = true;
5151 return RHS.isMinSignedValue();
5152 case ICmpInst::ICMP_ULT:
5153 // True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
5154 TrueIfSigned = false;
5155 return RHS.isMinSignedValue();
5156 case ICmpInst::ICMP_ULE:
5157 // True if LHS u<= RHS and RHS == sign-bit-mask - 1
5158 TrueIfSigned = false;
5159 return RHS.isMaxSignedValue();
5160 default:
5161 return false;
5162 }
5163}
5164
5166 bool CondIsTrue,
5167 const Instruction *CtxI,
5168 KnownFPClass &KnownFromContext,
5169 unsigned Depth = 0) {
5170 Value *A, *B;
5172 (CondIsTrue ? match(Cond, m_LogicalAnd(m_Value(A), m_Value(B)))
5173 : match(Cond, m_LogicalOr(m_Value(A), m_Value(B))))) {
5174 computeKnownFPClassFromCond(V, A, CondIsTrue, CtxI, KnownFromContext,
5175 Depth + 1);
5176 computeKnownFPClassFromCond(V, B, CondIsTrue, CtxI, KnownFromContext,
5177 Depth + 1);
5178 return;
5179 }
5181 computeKnownFPClassFromCond(V, A, !CondIsTrue, CtxI, KnownFromContext,
5182 Depth + 1);
5183 return;
5184 }
5186 Value *LHS;
5187 uint64_t ClassVal = 0;
5188 const APFloat *CRHS;
5189 const APInt *RHS;
5190 if (match(Cond, m_FCmp(Pred, m_Value(LHS), m_APFloat(CRHS)))) {
5191 auto [CmpVal, MaskIfTrue, MaskIfFalse] = fcmpImpliesClass(
5193 LHS != V);
5194 if (CmpVal == V)
5195 KnownFromContext.knownNot(~(CondIsTrue ? MaskIfTrue : MaskIfFalse));
5197 m_Specific(V), m_ConstantInt(ClassVal)))) {
5198 FPClassTest Mask = static_cast<FPClassTest>(ClassVal);
5199 KnownFromContext.knownNot(CondIsTrue ? ~Mask : Mask);
5201 m_APInt(RHS)))) {
5202 bool TrueIfSigned;
5203 if (!isSignBitCheck(Pred, *RHS, TrueIfSigned))
5204 return;
5205 if (TrueIfSigned == CondIsTrue)
5206 KnownFromContext.signBitMustBeOne();
5207 else
5208 KnownFromContext.signBitMustBeZero();
5209 }
5210}
5211
5212/// Compute the minimum and maximum values (inclusive) for the exponent of \p V,
5213/// assuming it is not nan. Returns {min, max, max-assuming-nonzero}. A value
5214/// frexp(0) = 0, so the tighter max-assuming-nonzero bound is only usable when
5215/// \p V is known not to be a logical zero (e.g., for fabs(x) < 0.25, the non-0
5216/// exponent range is [-149, -2], but the 0 edge case is above this range).
5217static std::tuple<int, int, int>
5219 if (!Q.CtxI || !Q.DC || !Q.DT)
5221
5222 // Intersect the bounds implied by every dominating condition, keeping the
5223 // tightest maximum. A value may participate in multiple compares
5224 // (e.g. fabs(x) < 2.0 and fabs(x) < 1.0), and the tighter one wins.
5225 int MaxExp = APFloat::IEK_Inf;
5226 int MaxExpNonZero = APFloat::IEK_Inf;
5227
5228 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
5230 const APFloat *LimitC;
5231 if (!match(BI->getCondition(),
5232 m_FCmp(Pred, m_FAbs(m_Specific(V)), m_Finite(LimitC))))
5233 continue;
5234
5237 continue;
5238
5239 // If fabs(x) <= K, implies the exponent min exp range.
5240 // if fabs(x) >= K, swap the successor
5241 bool IsLessEqual =
5245
5246 bool KnownStrictlyLess =
5249
5250 BasicBlockEdge Edge1(BI->getParent(),
5251 BI->getSuccessor(IsLessEqual ? 0 : 1));
5252 if (Q.DT->dominates(Edge1, Q.CtxI->getParent())) {
5253 // frexp returns an exponent one greater than ilogb.
5254 int Exp = ilogb(*LimitC) + 1;
5255
5256 // A strict bound fabs(V) < 2^n forces ilogb(V) <= n - 1, so the max frexp
5257 // exponent drops by one when K is exact power of two.
5258 if (KnownStrictlyLess && LimitC->getExactLog2Abs() != INT_MIN)
5259 --Exp;
5260
5261 // frexp(0) = 0, which the bound above (assuming a normal nonzero value)
5262 // may exclude.
5263
5264 // TODO: Figure out lower bound to detect no-underflow.
5265 MaxExpNonZero = std::min(MaxExpNonZero, Exp);
5266 MaxExp = std::min(MaxExp, std::max(Exp, 0));
5267 }
5268 }
5269
5270 return {APFloat::IEK_NaN, MaxExp, MaxExpNonZero};
5271}
5272
5274 const SimplifyQuery &Q) {
5275 KnownFPClass KnownFromContext;
5276
5277 if (Q.CC && Q.CC->AffectedValues.contains(V))
5279 KnownFromContext);
5280
5281 if (!Q.CtxI)
5282 return KnownFromContext;
5283
5284 if (Q.DC && Q.DT) {
5285 // Handle dominating conditions.
5286 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
5287 Value *Cond = BI->getCondition();
5288
5289 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
5290 if (Q.DT->dominates(Edge0, Q.CtxI->getParent()))
5291 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/true, Q.CtxI,
5292 KnownFromContext);
5293
5294 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
5295 if (Q.DT->dominates(Edge1, Q.CtxI->getParent()))
5296 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/false, Q.CtxI,
5297 KnownFromContext);
5298 }
5299 }
5300
5301 if (!Q.AC)
5302 return KnownFromContext;
5303
5304 // Try to restrict the floating-point classes based on information from
5305 // assumptions.
5306 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
5307 if (!AssumeVH)
5308 continue;
5309 CallInst *I = cast<CallInst>(AssumeVH);
5310
5311 assert(I->getFunction() == Q.CtxI->getParent()->getParent() &&
5312 "Got assumption for the wrong function!");
5313 assert(I->getIntrinsicID() == Intrinsic::assume &&
5314 "must be an assume intrinsic");
5315
5316 if (!isValidAssumeForContext(I, Q))
5317 continue;
5318
5319 computeKnownFPClassFromCond(V, I->getArgOperand(0),
5320 /*CondIsTrue=*/true, Q.CtxI, KnownFromContext);
5321 }
5322
5323 return KnownFromContext;
5324}
5325
5327 Value *Arm, bool Invert,
5328 const SimplifyQuery &SQ,
5329 unsigned Depth) {
5330
5331 KnownFPClass KnownSrc;
5333 /*CondIsTrue=*/!Invert, SQ.CtxI, KnownSrc,
5334 Depth + 1);
5335 KnownSrc = KnownSrc.unionWith(Known);
5336 if (KnownSrc.isUnknown())
5337 return;
5338
5339 if (isGuaranteedNotToBeUndef(Arm, SQ.AC, SQ.CtxI, SQ.DT, Depth + 1))
5340 Known = KnownSrc;
5341}
5342
5343void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
5344 FPClassTest InterestedClasses, KnownFPClass &Known,
5345 const SimplifyQuery &Q, unsigned Depth);
5346
5348 FPClassTest InterestedClasses,
5349 const SimplifyQuery &Q, unsigned Depth) {
5350 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
5351 APInt DemandedElts =
5352 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
5353 computeKnownFPClass(V, DemandedElts, InterestedClasses, Known, Q, Depth);
5354}
5355
5357 const APInt &DemandedElts,
5358 FPClassTest InterestedClasses,
5360 const SimplifyQuery &Q,
5361 unsigned Depth) {
5362 if ((InterestedClasses &
5364 return;
5365
5366 KnownFPClass KnownSrc;
5367 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5368 KnownSrc, Q, Depth + 1);
5369
5370 const fltSemantics &DstTy = Op->getType()->getScalarType()->getFltSemantics();
5371 const fltSemantics &SrcTy =
5372 Op->getOperand(0)->getType()->getScalarType()->getFltSemantics();
5373
5374 const auto *I = dyn_cast<Instruction>(Op);
5375 const Function *F = I ? I->getFunction() : nullptr;
5376 DenormalMode DstMode =
5377 F ? F->getDenormalMode(DstTy) : DenormalMode::getDynamic();
5378 DenormalMode SrcMode =
5379 F ? F->getDenormalMode(SrcTy) : DenormalMode::getDynamic();
5380
5381 Known = KnownFPClass::fptrunc(KnownSrc, DstTy, SrcTy, DstMode, SrcMode);
5382}
5383
5384static void
5386 FPClassTest InterestedClasses,
5387 KnownFPClass &Known, const SimplifyQuery &Q,
5388 unsigned Depth, bool IsAdd, bool IsStrict) {
5389 const Value *LHS = Op->getOperand(0);
5390 const Value *RHS = Op->getOperand(1);
5391
5392 const RoundingMode RM =
5393 IsStrict ? cast<ConstrainedFPIntrinsic>(Op)->getRoundingMode().value_or(
5396
5397 KnownFPClass KnownLHS, KnownRHS;
5398 bool WantNegative =
5399 IsAdd &&
5400 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;
5401 bool WantNaN = (InterestedClasses & fcNan) != fcNone;
5402 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;
5403
5404 if (!WantNaN && !WantNegative && !WantNegZero)
5405 return;
5406
5407 FPClassTest InterestedSrcs = InterestedClasses;
5408 if (WantNegative)
5409 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5410 if (InterestedClasses & fcNan)
5411 InterestedSrcs |= fcInf;
5412 computeKnownFPClass(RHS, DemandedElts, InterestedSrcs, KnownRHS, Q,
5413 Depth + 1);
5414
5415 // Special case fadd x, x, which is the canonical form of fmul x, 2.
5416 bool Self = LHS == RHS &&
5417 isGuaranteedNotToBeUndef(LHS, Q.AC, Q.CtxI, Q.DT, Depth + 1);
5418 if (Self)
5419 KnownLHS = KnownRHS;
5420
5421 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||
5422 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) || WantNegZero ||
5423 !IsAdd) {
5424
5425 // FIXME: Context function should always be passed in separately
5426 const Function *F = cast<Instruction>(Op)->getFunction();
5427 const fltSemantics &FltSem =
5428 Op->getType()->getScalarType()->getFltSemantics();
5430 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5431
5432 if (Self && IsAdd) {
5433 Known = KnownFPClass::fadd_self(KnownLHS, RM, Mode);
5434 } else {
5435 // RHS is canonically cheaper to compute. Skip inspecting the LHS if
5436 // there's no point.
5437
5438 if (!Self) {
5439 computeKnownFPClass(LHS, DemandedElts, InterestedSrcs, KnownLHS, Q,
5440 Depth + 1);
5441 }
5442
5443 Known = IsAdd ? KnownFPClass::fadd(KnownLHS, KnownRHS, RM, Mode)
5444 : KnownFPClass::fsub(KnownLHS, KnownRHS, RM, Mode);
5445 }
5446 }
5447}
5448
5450 const APInt &DemandedElts,
5451 FPClassTest InterestedClasses,
5453 const SimplifyQuery &Q, unsigned Depth) {
5454 const Value *LHS = Op->getOperand(0);
5455 const Value *RHS = Op->getOperand(1);
5456
5457 FPClassTest InterestedLHS = fcNone;
5458 FPClassTest InterestedRHS = fcNone;
5459
5460 // NaN is also generated for frem(Inf, x) and frem(x, 0.0).
5461 if (InterestedClasses & fcNan) {
5462 InterestedLHS |= fcNan | fcInf;
5463 InterestedRHS |= fcNan | fcZero | fcSubnormal;
5464 }
5465
5466 // The sign for frem is the same as the first operand.
5467 if (InterestedClasses & (fcPosNormal | fcPosSubnormal))
5468 InterestedLHS |= fcPosNormal | fcPosSubnormal;
5469 if (InterestedClasses & (fcNegNormal | fcNegSubnormal))
5470 InterestedLHS |= fcNegNormal | fcNegSubnormal;
5471
5472 // A negative zero result requires a negative finite first operand.
5473 if (InterestedClasses & fcNegZero)
5474 InterestedLHS |= fcNegFinite;
5475
5476 // A positive zero result can additionally come from a negative finite
5477 // result being flushed to positive zero.
5478 if (InterestedClasses & fcPosZero)
5479 InterestedLHS |= fcPosFinite | fcNegNormal | fcNegSubnormal;
5480
5481 const Function *F = cast<Instruction>(Op)->getFunction();
5483 F ? F->getDenormalMode(Op->getType()->getScalarType()->getFltSemantics())
5485
5486 if (LHS == RHS && isGuaranteedNotToBeUndef(LHS, Q.AC, Q.CtxI, Q.DT)) {
5487 // X % X is always exactly [+-]0.0 or a NaN.
5488 FPClassTest InterestedSrcs = InterestedLHS | InterestedRHS;
5489 KnownFPClass KnownSrc;
5490 if (InterestedSrcs != fcNone)
5491 computeKnownFPClass(LHS, DemandedElts, InterestedSrcs, KnownSrc, Q,
5492 Depth + 1);
5493 Known = KnownFPClass::frem_self(KnownSrc, Mode);
5494 return;
5495 }
5496
5497 KnownFPClass KnownLHS;
5498 if (InterestedLHS != fcNone)
5499 computeKnownFPClass(LHS, DemandedElts, InterestedLHS, KnownLHS, Q,
5500 Depth + 1);
5501
5502 KnownFPClass KnownRHS;
5503 // RHS is only useful for refining NaN classes.
5504 if (InterestedRHS != fcNone && KnownLHS.isKnownNever(fcSNan))
5505 computeKnownFPClass(RHS, DemandedElts, InterestedRHS, KnownRHS, Q,
5506 Depth + 1);
5507
5508 Known = KnownFPClass::frem(KnownLHS, KnownRHS, Mode);
5509}
5510
5512 switch (IID) {
5513 case Intrinsic::minimum:
5515 case Intrinsic::maximum:
5517 case Intrinsic::minimumnum:
5519 case Intrinsic::maximumnum:
5521 case Intrinsic::minnum:
5523 case Intrinsic::maxnum:
5525 default:
5526 llvm_unreachable("not a floating-point min-max intrinsic");
5527 }
5528}
5529
5530/// \return true if this is a floating point value that is known to have a
5531/// magnitude smaller than 1. i.e., fabs(X) <= 1.0 or is nan.
5532static bool isAbsoluteValueULEOne(const Value *V) {
5533 // TODO: Handle frexp
5534 // TODO: Other rounding intrinsics?
5535 // TODO: Try computeKnownExponentRangeFromContext
5536
5537 // fabs(x - floor(x)) <= 1
5538 const Value *SubFloorX;
5539 if (match(V, m_FSub(m_Value(SubFloorX),
5541 return true;
5542
5545}
5546
5547void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
5548 FPClassTest InterestedClasses, KnownFPClass &Known,
5549 const SimplifyQuery &Q, unsigned Depth) {
5550 assert(Known.isUnknown() && "should not be called with known information");
5551
5552 if (!DemandedElts) {
5553 // No demanded elts, better to assume we don't know anything.
5554 Known.resetAll();
5555 return;
5556 }
5557
5558 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
5559
5560 if (auto *CFP = dyn_cast<ConstantFP>(V)) {
5561 Known = KnownFPClass(CFP->getValueAPF());
5562 return;
5563 }
5564
5566 Known.setKnownFPClasses(fcPosZero);
5567 return;
5568 }
5569
5570 if (isa<PoisonValue>(V)) {
5572 return;
5573 }
5574
5575 // Try to handle fixed width vector constants
5576 auto *VFVTy = dyn_cast<FixedVectorType>(V->getType());
5577 const Constant *CV = dyn_cast<Constant>(V);
5578 if (VFVTy && CV) {
5580
5581 // For vectors, verify that each element is not NaN.
5582 unsigned NumElts = VFVTy->getNumElements();
5583 for (unsigned i = 0; i != NumElts; ++i) {
5584 if (!DemandedElts[i])
5585 continue;
5586
5587 Constant *Elt = CV->getAggregateElement(i);
5588 if (!Elt) {
5589 Known = KnownFPClass();
5590 return;
5591 }
5592 if (isa<PoisonValue>(Elt))
5593 continue;
5594 auto *CElt = dyn_cast<ConstantFP>(Elt);
5595 if (!CElt) {
5596 Known = KnownFPClass();
5597 return;
5598 }
5599
5600 Known |= KnownFPClass(CElt->getValueAPF());
5601 }
5602 return;
5603 }
5604
5605 if (const auto *CDS = dyn_cast<ConstantDataSequential>(V)) {
5607 for (size_t I = 0, E = CDS->getNumElements(); I != E; ++I)
5608 Known |= KnownFPClass(CDS->getElementAsAPFloat(I));
5609 return;
5610 }
5611
5612 if (const auto *CA = dyn_cast<ConstantAggregate>(V)) {
5613 // TODO: Handle complex aggregates
5615 for (const Use &Op : CA->operands()) {
5616 auto *CFP = dyn_cast<ConstantFP>(Op.get());
5617 if (!CFP) {
5618 Known = KnownFPClass();
5619 return;
5620 }
5621
5622 Known |= KnownFPClass(CFP->getValueAPF());
5623 }
5624
5625 return;
5626 }
5627
5628 FPClassTest KnownNotFromFlags = fcNone;
5629 if (const auto *CB = dyn_cast<CallBase>(V))
5630 KnownNotFromFlags |= CB->getRetNoFPClass();
5631 else if (const auto *Arg = dyn_cast<Argument>(V))
5632 KnownNotFromFlags |= Arg->getNoFPClass();
5633
5634 const Operator *Op = dyn_cast<Operator>(V);
5636 if (FPOp->hasNoNaNs())
5637 KnownNotFromFlags |= fcNan;
5638 if (FPOp->hasNoInfs())
5639 KnownNotFromFlags |= fcInf;
5640 }
5641
5642 KnownFPClass AssumedClasses = computeKnownFPClassFromContext(V, Q);
5643 KnownNotFromFlags |= ~AssumedClasses.getKnownFPClasses();
5644
5645 // We no longer need to find out about these bits from inputs if we can
5646 // assume this from flags/attributes.
5647 InterestedClasses &= ~KnownNotFromFlags;
5648
5649 llvm::scope_exit ClearClassesFromFlags([=, &Known] {
5650 Known.knownNot(KnownNotFromFlags);
5651 if (!Known.getSignBit() && AssumedClasses.getSignBit()) {
5652 if (*AssumedClasses.getSignBit())
5653 Known.signBitMustBeOne();
5654 else
5655 Known.signBitMustBeZero();
5656 }
5657 });
5658
5659 if (!Op)
5660 return;
5661
5662 // All recursive calls that increase depth must come after this.
5664 return;
5665
5666 const unsigned Opc = Op->getOpcode();
5667 switch (Opc) {
5668 case Instruction::FNeg: {
5669 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5670 Known, Q, Depth + 1);
5671 Known.fneg();
5672 break;
5673 }
5674 case Instruction::Select: {
5675 auto ComputeForArm = [&](Value *Arm, bool Invert) {
5676 KnownFPClass Res;
5677 computeKnownFPClass(Arm, DemandedElts, InterestedClasses, Res, Q,
5678 Depth + 1);
5679 adjustKnownFPClassForSelectArm(Res, Op->getOperand(0), Arm, Invert, Q,
5680 Depth);
5681 return Res;
5682 };
5683 // Only known if known in both the LHS and RHS.
5684 Known =
5685 ComputeForArm(Op->getOperand(1), /*Invert=*/false)
5686 .intersectWith(ComputeForArm(Op->getOperand(2), /*Invert=*/true));
5687 break;
5688 }
5689 case Instruction::Load: {
5690 const MDNode *NoFPClass =
5691 cast<LoadInst>(Op)->getMetadata(LLVMContext::MD_nofpclass);
5692 if (!NoFPClass)
5693 break;
5694
5695 ConstantInt *MaskVal =
5697 Known.knownNot(static_cast<FPClassTest>(MaskVal->getZExtValue()));
5698 break;
5699 }
5700 case Instruction::Call: {
5701 const CallInst *II = cast<CallInst>(Op);
5702 const Intrinsic::ID IID = II->getIntrinsicID();
5703 switch (IID) {
5704 case Intrinsic::fabs: {
5705 if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) {
5706 // If we only care about the sign bit we don't need to inspect the
5707 // operand.
5708 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5709 InterestedClasses, Known, Q, Depth + 1);
5710 }
5711
5712 Known.fabs();
5713 break;
5714 }
5715 case Intrinsic::copysign: {
5716 KnownFPClass KnownSign;
5717
5718 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5719 Known, Q, Depth + 1);
5720 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5721 KnownSign, Q, Depth + 1);
5722 Known.copysign(KnownSign);
5723 break;
5724 }
5725 case Intrinsic::fma:
5726 case Intrinsic::fmuladd: {
5727 if ((InterestedClasses & (fcNegative | fcNan)) == fcNone)
5728 break;
5729
5730 const bool IsFMA = IID == Intrinsic::fma;
5731
5732 // FIXME: This should check isGuaranteedNotToBeUndef
5733 if (II->getArgOperand(0) == II->getArgOperand(1)) {
5734 FPClassTest InterestedSquared = InterestedClasses;
5735 FPClassTest InterestedAddend = InterestedClasses;
5736 // Only fma(+inf, +inf, -inf) and fma(-inf, -inf, -inf) produce NaN when
5737 // squaring.
5738 if (InterestedClasses & fcQNan) {
5739 InterestedSquared |= fcSNan | fcInf;
5740 InterestedAddend |= fcSNan | fcNegInf;
5741 }
5742
5743 KnownFPClass KnownSrc, KnownAddend;
5744 computeKnownFPClass(II->getArgOperand(2), DemandedElts,
5745 InterestedAddend, KnownAddend, Q, Depth + 1);
5746 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5747 InterestedSquared, KnownSrc, Q, Depth + 1);
5748
5749 const Function *F = II->getFunction();
5750 const fltSemantics &FltSem =
5751 II->getType()->getScalarType()->getFltSemantics();
5753 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5754
5755 if ((KnownNotFromFlags & fcNan) == fcNan) {
5756 KnownSrc.knownNot(fcNan);
5757 KnownAddend.knownNot(fcNan);
5758 }
5759
5760 if ((KnownNotFromFlags & fcInf) == fcInf) {
5761 KnownSrc.knownNot(fcInf);
5762 KnownAddend.knownNot(fcInf);
5763 }
5764
5765 Known = IsFMA
5766 ? KnownFPClass::fma_square(KnownSrc, KnownAddend, Mode)
5767 : KnownFPClass::fmuladd_square(KnownSrc, KnownAddend, Mode);
5768 break;
5769 }
5770
5771 KnownFPClass KnownSrc[3];
5772 for (int I = 0; I != 3; ++I) {
5773 computeKnownFPClass(II->getArgOperand(I), DemandedElts,
5774 InterestedClasses, KnownSrc[I], Q, Depth + 1);
5775 if (KnownSrc[I].isUnknown())
5776 return;
5777
5778 if ((KnownNotFromFlags & fcNan) == fcNan)
5779 KnownSrc[I].knownNot(fcNan);
5780 if ((KnownNotFromFlags & fcInf) == fcInf)
5781 KnownSrc[I].knownNot(fcInf);
5782 }
5783
5784 const Function *F = II->getFunction();
5785 const fltSemantics &FltSem =
5786 II->getType()->getScalarType()->getFltSemantics();
5788 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5789 Known =
5790 IsFMA ? KnownFPClass::fma(KnownSrc[0], KnownSrc[1], KnownSrc[2], Mode)
5791 : KnownFPClass::fmuladd(KnownSrc[0], KnownSrc[1], KnownSrc[2],
5792 Mode);
5793 break;
5794 }
5795 case Intrinsic::experimental_constrained_fadd:
5796 case Intrinsic::experimental_constrained_fsub: {
5798 Op, DemandedElts, InterestedClasses, Known, Q, Depth,
5799 /*IsAdd=*/IID == Intrinsic::experimental_constrained_fadd,
5800 /*IsStrict=*/true);
5801 break;
5802 }
5803 case Intrinsic::experimental_constrained_frem: {
5804 computeKnownFPClassForFRem(Op, DemandedElts, InterestedClasses, Known, Q,
5805 Depth);
5806 break;
5807 }
5808 case Intrinsic::sqrt:
5809 case Intrinsic::experimental_constrained_sqrt: {
5810 KnownFPClass KnownSrc;
5811 FPClassTest InterestedSrcs = InterestedClasses;
5812 if (InterestedClasses & fcQNan)
5814 // Subnormals may flush to zero.
5815 if (InterestedClasses & fcZero)
5816 InterestedSrcs |= fcSubnormal;
5817
5818 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5819 KnownSrc, Q, Depth + 1);
5820
5822
5823 bool HasNSZ = Q.IIQ.hasNoSignedZeros(II);
5824 if (!HasNSZ) {
5825 const Function *F = II->getFunction();
5826 const fltSemantics &FltSem =
5827 II->getType()->getScalarType()->getFltSemantics();
5828 Mode = F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5829 }
5830
5831 Known = KnownFPClass::sqrt(KnownSrc, Mode);
5832 if (HasNSZ)
5833 Known.knownNot(fcNegZero);
5834
5835 break;
5836 }
5837 case Intrinsic::sin: {
5838 KnownFPClass KnownSrc;
5839 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5840 KnownSrc, Q, Depth + 1);
5841 Known = KnownFPClass::sin(KnownSrc);
5842 break;
5843 }
5844 case Intrinsic::cos: {
5845 KnownFPClass KnownSrc;
5846 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5847 KnownSrc, Q, Depth + 1);
5848 Known = KnownFPClass::cos(KnownSrc);
5849 break;
5850 }
5851 case Intrinsic::tan: {
5852 KnownFPClass KnownSrc;
5853 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5854 KnownSrc, Q, Depth + 1);
5855 Known = KnownFPClass::tan(KnownSrc);
5856 break;
5857 }
5858 case Intrinsic::sinh: {
5859 KnownFPClass KnownSrc;
5860 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5861 KnownSrc, Q, Depth + 1);
5862 Known = KnownFPClass::sinh(KnownSrc);
5863 break;
5864 }
5865 case Intrinsic::cosh: {
5866 KnownFPClass KnownSrc;
5867 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5868 KnownSrc, Q, Depth + 1);
5869 Known = KnownFPClass::cosh(KnownSrc);
5870 break;
5871 }
5872 case Intrinsic::tanh: {
5873 KnownFPClass KnownSrc;
5874 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5875 KnownSrc, Q, Depth + 1);
5876 Known = KnownFPClass::tanh(KnownSrc);
5877 break;
5878 }
5879 case Intrinsic::asin: {
5880 KnownFPClass KnownSrc;
5881 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5882 KnownSrc, Q, Depth + 1);
5883 Known = KnownFPClass::asin(KnownSrc);
5884 break;
5885 }
5886 case Intrinsic::acos: {
5887 KnownFPClass KnownSrc;
5888 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5889 KnownSrc, Q, Depth + 1);
5890 Known = KnownFPClass::acos(KnownSrc);
5891 break;
5892 }
5893 case Intrinsic::atan: {
5894 KnownFPClass KnownSrc;
5895 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5896 KnownSrc, Q, Depth + 1);
5897 Known = KnownFPClass::atan(KnownSrc);
5898 break;
5899 }
5900 case Intrinsic::atan2: {
5901 FPClassTest InterestedY = InterestedClasses;
5902 FPClassTest InterestedX = InterestedClasses;
5903
5904 // We can rule out negative values if y cannot have a negative value.
5905 if ((InterestedClasses & fcNegFinite) != fcNone)
5906 InterestedY |= fcNegative;
5907
5908 // We can rule out positive values if y cannot have a positive value.
5909 if ((InterestedClasses & fcPosFinite) != fcNone)
5910 InterestedY |= fcPositive | fcNegSubnormal;
5911
5912 // We can rule out zero and subnormal if x cannot have a positive value.
5913 if ((InterestedClasses & (fcZero | fcSubnormal)) != fcNone)
5914 InterestedX |= fcPositive | fcNegSubnormal;
5915
5916 KnownFPClass KnownY, KnownX;
5917 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedY,
5918 KnownY, Q, Depth + 1);
5919 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedX,
5920 KnownX, Q, Depth + 1);
5921
5922 const Function *F = II->getFunction();
5924 F ? F->getDenormalMode(
5925 II->getType()->getScalarType()->getFltSemantics())
5927 Known = KnownFPClass::atan2(KnownY, KnownX, Mode);
5928 break;
5929 }
5930 case Intrinsic::maxnum:
5931 case Intrinsic::minnum:
5932 case Intrinsic::minimum:
5933 case Intrinsic::maximum:
5934 case Intrinsic::minimumnum:
5935 case Intrinsic::maximumnum: {
5936 KnownFPClass KnownLHS, KnownRHS;
5937 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5938 KnownLHS, Q, Depth + 1);
5939 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5940 KnownRHS, Q, Depth + 1);
5941
5942 const Function *F = II->getFunction();
5943
5945 F ? F->getDenormalMode(
5946 II->getType()->getScalarType()->getFltSemantics())
5948
5949 Known = KnownFPClass::minMaxLike(KnownLHS, KnownRHS, getMinMaxKind(IID),
5950 Mode);
5951 break;
5952 }
5953 case Intrinsic::canonicalize: {
5954 KnownFPClass KnownSrc;
5955 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5956 KnownSrc, Q, Depth + 1);
5957
5958 const Function *F = II->getFunction();
5959 DenormalMode DenormMode =
5960 F ? F->getDenormalMode(
5961 II->getType()->getScalarType()->getFltSemantics())
5963 Known = KnownFPClass::canonicalize(KnownSrc, DenormMode);
5964 break;
5965 }
5966 case Intrinsic::vector_reduce_fmax:
5967 case Intrinsic::vector_reduce_fmin:
5968 case Intrinsic::vector_reduce_fmaximum:
5969 case Intrinsic::vector_reduce_fminimum:
5970 case Intrinsic::vector_reduce_fmaximumnum:
5971 case Intrinsic::vector_reduce_fminimumnum: {
5972 // reduce min/max will choose an element from one of the vector elements,
5973 // so we can infer and class information that is common to all elements.
5974 Known = computeKnownFPClass(II->getArgOperand(0), II->getFastMathFlags(),
5975 InterestedClasses, Q, Depth + 1);
5976 // Can only propagate sign if output is never NaN.
5977 if (!Known.isKnownNeverNaN())
5978 Known.setSignBit(std::nullopt);
5979 break;
5980 }
5981 // reverse preserves all characteristics of the input vec's element.
5982 case Intrinsic::vector_reverse:
5984 II->getArgOperand(0), DemandedElts.reverseBits(),
5985 II->getFastMathFlags(), InterestedClasses, Q, Depth + 1);
5986 break;
5987 case Intrinsic::trunc:
5988 case Intrinsic::floor:
5989 case Intrinsic::ceil:
5990 case Intrinsic::rint:
5991 case Intrinsic::nearbyint:
5992 case Intrinsic::round:
5993 case Intrinsic::roundeven: {
5994 KnownFPClass KnownSrc;
5995 FPClassTest InterestedSrcs = InterestedClasses;
5996
5997 // Negative round ups towards zero produce negative zero.
5998 if (InterestedSrcs & fcNegFinite)
5999 InterestedSrcs |= fcNegFinite;
6000
6001 // Negative subnormals may flush to positive zero.
6002 if (InterestedSrcs & fcPosFinite)
6003 InterestedSrcs |= fcPosFinite | fcNegSubnormal;
6004
6005 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
6006 KnownSrc, Q, Depth + 1);
6007
6008 const Function *F = II->getFunction();
6010 F ? F->getDenormalMode(
6011 II->getType()->getScalarType()->getFltSemantics())
6013 const bool IsMultiUnitFPType =
6014 V->getType()->getScalarType()->isMultiUnitFPType();
6015
6016 switch (IID) {
6017 case Intrinsic::trunc:
6018 Known = KnownFPClass::trunc(KnownSrc, Mode);
6019 break;
6020 case Intrinsic::floor:
6021 Known = KnownFPClass::floor(KnownSrc, IsMultiUnitFPType, Mode);
6022 break;
6023 case Intrinsic::ceil:
6024 Known = KnownFPClass::ceil(KnownSrc, IsMultiUnitFPType, Mode);
6025 break;
6026 default:
6027 Known =
6028 KnownFPClass::roundToIntegral(KnownSrc, IsMultiUnitFPType, Mode);
6029 break;
6030 }
6031 break;
6032 }
6033 case Intrinsic::exp:
6034 case Intrinsic::exp2:
6035 case Intrinsic::exp10:
6036 case Intrinsic::amdgcn_exp2: {
6037 KnownFPClass KnownSrc;
6038 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
6039 KnownSrc, Q, Depth + 1);
6040
6041 Known = KnownFPClass::exp(KnownSrc);
6042
6043 Type *EltTy = II->getType()->getScalarType();
6044 if (IID == Intrinsic::amdgcn_exp2 && EltTy->isFloatTy())
6045 Known.knownNot(fcSubnormal);
6046
6047 break;
6048 }
6049 case Intrinsic::fptrunc_round: {
6050 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,
6051 Q, Depth);
6052 break;
6053 }
6054 case Intrinsic::log:
6055 case Intrinsic::log10:
6056 case Intrinsic::log2:
6057 case Intrinsic::experimental_constrained_log:
6058 case Intrinsic::experimental_constrained_log10:
6059 case Intrinsic::experimental_constrained_log2:
6060 case Intrinsic::amdgcn_log: {
6061 FPClassTest InterestedSrcs = fcNone;
6062
6063 // log(negative) produces NaN.
6064 if ((InterestedClasses & fcNan) != fcNone)
6065 InterestedSrcs |= fcNan | fcNegative;
6066
6067 // log(logical-zero) produces negative infinity.
6068 if ((InterestedClasses & fcNegInf) != fcNone)
6069 InterestedSrcs |= fcZero | fcSubnormal;
6070
6071 // log(x) < -0.0 if x < +1.0
6072 if ((InterestedClasses & fcNegNormal) != fcNone)
6073 InterestedSrcs |= fcPosSubnormal | fcPosNormal;
6074
6075 // log(x) >= +0.0 if x >= +1.0
6076 if ((InterestedClasses & (fcPosZero | fcPosNormal)) != fcNone)
6077 InterestedSrcs |= fcPosNormal;
6078
6079 // log(x) is positive infinity iff x is positive infinity.
6080 if ((InterestedClasses & fcPosInf) != fcNone)
6081 InterestedSrcs |= fcPosInf;
6082
6083 KnownFPClass KnownSrc;
6084 if (InterestedSrcs != fcNone)
6085 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
6086 KnownSrc, Q, Depth + 1);
6087 const Function *F = II->getFunction();
6089 F ? F->getDenormalMode(
6090 II->getType()->getScalarType()->getFltSemantics())
6092 const bool IsMultiUnitFPType =
6093 V->getType()->getScalarType()->isMultiUnitFPType();
6094 Known = KnownFPClass::log(KnownSrc, Mode, IsMultiUnitFPType);
6095 break;
6096 }
6097 case Intrinsic::pow: {
6098 const bool WantNaN = (InterestedClasses & fcNan) != fcNone;
6099 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
6100 if (!WantNaN && !WantNegative)
6101 break;
6102
6103 FPClassTest InterestedLHS = fcNone;
6104 FPClassTest InterestedRHS = fcNone;
6105 if (WantNaN) {
6106 // pow may return NaN if one of the arguments is NaN. NaN may also be
6107 // produced from a negative, non-zero finite base and a non-integer
6108 // exponent.
6109 InterestedLHS |= fcNan | fcNegNormal | fcNegSubnormal;
6110 InterestedRHS |= fcNan;
6111 }
6112 if (WantNegative) {
6113 // A negative value is returned when a negative base is raised to an odd
6114 // integer power. Only normal values can be odd integers.
6115 InterestedLHS |= fcNegative;
6116 InterestedRHS |= fcNormal;
6117 }
6118
6119 KnownFPClass KnownLHS;
6120 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedLHS,
6121 KnownLHS, Q, Depth + 1);
6122
6123 // If the LHS is unknown, then querying the RHS is only useful for rare
6124 // edge cases.
6125 if (KnownLHS.isUnknown())
6126 break;
6127
6128 KnownFPClass KnownRHS;
6129 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedRHS,
6130 KnownRHS, Q, Depth + 1);
6131 Known = KnownFPClass::pow(KnownLHS, KnownRHS);
6132 break;
6133 }
6134 case Intrinsic::powi: {
6135 if ((InterestedClasses & (fcNan | fcInf | fcNegative)) == fcNone)
6136 break;
6137
6138 // The exponent is always a scalar, even when raising a vector to a power.
6139 const Value *Exp = II->getArgOperand(1);
6140 unsigned BitWidth = Exp->getType()->getIntegerBitWidth();
6141 KnownBits ExponentKnownBits(BitWidth);
6142 computeKnownBits(Exp, APInt(1, 1), ExponentKnownBits, Q, Depth + 1);
6143
6144 FPClassTest InterestedSrcs = fcNone;
6145 if (InterestedClasses & fcNan)
6146 InterestedSrcs |= fcNan;
6147 if (!ExponentKnownBits.isZero()) {
6148 if (InterestedClasses & fcInf)
6149 InterestedSrcs |= fcFinite | fcInf;
6150 if ((InterestedClasses & fcNegative) && !ExponentKnownBits.isEven())
6151 InterestedSrcs |= fcNegative;
6152 }
6153
6154 KnownFPClass KnownSrc;
6155 if (InterestedSrcs != fcNone)
6156 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
6157 KnownSrc, Q, Depth + 1);
6158
6159 Known = KnownFPClass::powi(KnownSrc, ExponentKnownBits);
6160 break;
6161 }
6162 case Intrinsic::ldexp: {
6163 KnownFPClass KnownSrc;
6164 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
6165 KnownSrc, Q, Depth + 1);
6166 // Can refine inf/zero handling based on the exponent operand.
6167 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;
6168
6169 const Value *ExpArg = II->getArgOperand(1);
6170 ConstantRange ExpKnownRange =
6171 ((KnownSrc.getKnownFPClasses() & ExpInfoMask) != fcNone)
6172 ? computeConstantRange(ExpArg, /*ForSigned=*/true, Q, Depth + 1)
6173 : ConstantRange::getFull(
6174 ExpArg->getType()->getScalarSizeInBits());
6175
6176 const fltSemantics &Flt =
6177 II->getType()->getScalarType()->getFltSemantics();
6178
6179 const Function *F = II->getFunction();
6181 F ? F->getDenormalMode(Flt) : DenormalMode::getDynamic();
6182
6183 Known = KnownFPClass::ldexp(KnownSrc, ExpKnownRange.getSignedMin(),
6184 ExpKnownRange.getSignedMax(), Flt, Mode);
6185 break;
6186 }
6187 case Intrinsic::arithmetic_fence: {
6188 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
6189 Known, Q, Depth + 1);
6190 break;
6191 }
6192 case Intrinsic::experimental_constrained_sitofp:
6193 case Intrinsic::experimental_constrained_uitofp:
6194 // Cannot produce nan
6195 Known.knownNot(fcNan);
6196
6197 // sitofp and uitofp turn into +0.0 for zero.
6198 Known.knownNot(fcNegZero);
6199
6200 // Integers cannot be subnormal
6201 Known.knownNot(fcSubnormal);
6202
6203 if (IID == Intrinsic::experimental_constrained_uitofp)
6204 Known.signBitMustBeZero();
6205
6206 // TODO: Copy inf handling from instructions
6207 break;
6208
6209 case Intrinsic::amdgcn_fract: {
6210 Known.knownNot(fcInf);
6211
6212 if (InterestedClasses & fcNan) {
6213 KnownFPClass KnownSrc;
6214 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
6215 InterestedClasses, KnownSrc, Q, Depth + 1);
6216
6217 if (KnownSrc.isKnownNeverInfOrNaN())
6218 Known.knownNot(fcNan);
6219 else if (KnownSrc.isKnownNever(fcSNan))
6220 Known.knownNot(fcSNan);
6221 }
6222
6223 break;
6224 }
6225 case Intrinsic::amdgcn_rcp: {
6226 KnownFPClass KnownSrc;
6227 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
6228 KnownSrc, Q, Depth + 1);
6229
6230 Known.propagateNonNaN(KnownSrc);
6231
6232 Type *EltTy = II->getType()->getScalarType();
6233
6234 // f32 denormal always flushed.
6235 if (EltTy->isFloatTy()) {
6236 Known.knownNot(fcSubnormal);
6237 KnownSrc.knownNot(fcSubnormal);
6238 }
6239
6240 if (KnownSrc.isKnownNever(fcNegative))
6241 Known.knownNot(fcNegative);
6242 if (KnownSrc.isKnownNever(fcPositive))
6243 Known.knownNot(fcPositive);
6244
6245 if (const Function *F = II->getFunction()) {
6246 DenormalMode Mode = F->getDenormalMode(EltTy->getFltSemantics());
6247 if (KnownSrc.isKnownNeverLogicalPosZero(Mode))
6248 Known.knownNot(fcPosInf);
6249 if (KnownSrc.isKnownNeverLogicalNegZero(Mode))
6250 Known.knownNot(fcNegInf);
6251 }
6252
6253 break;
6254 }
6255 case Intrinsic::amdgcn_rsq: {
6256 KnownFPClass KnownSrc;
6257 // The only negative value that can be returned is -inf for -0 inputs.
6259
6260 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
6261 KnownSrc, Q, Depth + 1);
6262
6263 // Negative -> nan
6264 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
6265 Known.knownNot(fcNan);
6266 else if (KnownSrc.isKnownNever(fcSNan))
6267 Known.knownNot(fcSNan);
6268
6269 // +inf -> +0
6270 if (KnownSrc.isKnownNeverPosInfinity())
6271 Known.knownNot(fcPosZero);
6272
6273 Type *EltTy = II->getType()->getScalarType();
6274
6275 // f32 denormal always flushed.
6276 if (EltTy->isFloatTy())
6277 Known.knownNot(fcPosSubnormal);
6278
6279 if (const Function *F = II->getFunction()) {
6280 DenormalMode Mode = F->getDenormalMode(EltTy->getFltSemantics());
6281
6282 // -0 -> -inf
6283 if (KnownSrc.isKnownNeverLogicalNegZero(Mode))
6284 Known.knownNot(fcNegInf);
6285
6286 // +0 -> +inf
6287 if (KnownSrc.isKnownNeverLogicalPosZero(Mode))
6288 Known.knownNot(fcPosInf);
6289 }
6290
6291 break;
6292 }
6293 case Intrinsic::amdgcn_trig_preop: {
6294 // Always returns a value [0, 1)
6295 Known.knownNot(fcNan | fcInf | fcNegative);
6296 break;
6297 }
6298 case Intrinsic::convert_from_arbitrary_fp: {
6299 auto *MD = cast<MetadataAsValue>(II->getArgOperand(1))->getMetadata();
6300 StringRef FormatStr = cast<MDString>(MD)->getString();
6301
6302 const fltSemantics *SrcSemantics =
6304 if (!SrcSemantics)
6305 break;
6306
6307 const fltSemantics DstSemantics =
6308 II->getType()->getScalarType()->getFltSemantics();
6309
6310 if (!APFloat::semanticsHasNaN(*SrcSemantics))
6311 Known.knownNot(fcNan);
6312
6313 // fcInf can only be cleared if the source format has no Inf encoding
6314 // and the dst max exp can accommodate src max exp.
6315 if (!APFloat::semanticsHasInf(*SrcSemantics) &&
6316 APFloat::semanticsMaxExponent(*SrcSemantics) <=
6317 APFloat::semanticsMaxExponent(DstSemantics))
6318 Known.knownNot(fcInf);
6319
6320 // Check and clear all neg flags for formats that do not have signed
6321 // representation.
6322 if (!APFloat::semanticsHasSignedRepr(*SrcSemantics))
6323 Known.knownNot(fcNegative);
6324
6325 // Check if format has no zero at all (Float8E8M0FNU), or no negative
6326 // zero.
6327 if (!APFloat::semanticsHasZero(*SrcSemantics))
6328 Known.knownNot(fcZero);
6329 else if (SrcSemantics->nanEncoding == fltNanEncoding::NegativeZero)
6330 Known.knownNot(fcNegZero);
6331
6332 // If src lands normally in dest, the result can never be subnormal.
6333 if (APFloat::isRepresentableAsNormalIn(*SrcSemantics, DstSemantics))
6334 Known.knownNot(fcSubnormal);
6335 break;
6336 }
6337 default:
6338 break;
6339 }
6340
6341 break;
6342 }
6343 case Instruction::FAdd:
6344 case Instruction::FSub: {
6345 computeKnownFPClassForFAddSub(Op, DemandedElts, InterestedClasses, Known, Q,
6346 Depth, /*IsAdd=*/Opc == Instruction::FAdd,
6347 /*IsStrict=*/false);
6348 break;
6349 }
6350 case Instruction::FMul: {
6351 const Function *F = cast<Instruction>(Op)->getFunction();
6353 F ? F->getDenormalMode(
6354 Op->getType()->getScalarType()->getFltSemantics())
6356
6357 Value *LHS = Op->getOperand(0);
6358 Value *RHS = Op->getOperand(1);
6359 // X * X is always non-negative or a NaN.
6360 // FIXME: Should check isGuaranteedNotToBeUndef
6361 if (LHS == RHS) {
6362 KnownFPClass KnownSrc;
6363 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownSrc, Q,
6364 Depth + 1);
6365 Known = KnownFPClass::square(KnownSrc, Mode);
6366 break;
6367 }
6368
6369 KnownFPClass KnownLHS, KnownRHS;
6370
6371 const APFloat *CRHS;
6372 if (match(RHS, m_APFloat(CRHS))) {
6373 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Q,
6374 Depth + 1);
6375 Known = KnownFPClass::fmul(KnownLHS, *CRHS, Mode);
6376 } else {
6377 computeKnownFPClass(RHS, DemandedElts, fcAllFlags, KnownRHS, Q,
6378 Depth + 1);
6379 // TODO: Improve accuracy in unfused FMA pattern. We can prove an
6380 // additional not-nan if the addend is known-not negative infinity if the
6381 // multiply is known-not infinity.
6382
6383 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Q,
6384 Depth + 1);
6385 Known = KnownFPClass::fmul(KnownLHS, KnownRHS, Mode);
6386 }
6387
6388 /// Propgate no-infs if the other source is known smaller than one, such
6389 /// that this cannot introduce overflow.
6390 if (KnownLHS.isKnownNever(fcInf) && isAbsoluteValueULEOne(RHS))
6391 Known.knownNot(fcInf);
6392 else if (KnownRHS.isKnownNever(fcInf) && isAbsoluteValueULEOne(LHS))
6393 Known.knownNot(fcInf);
6394
6395 break;
6396 }
6397 case Instruction::FDiv: {
6398 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
6399
6400 const Function *F = cast<Instruction>(Op)->getFunction();
6401 const fltSemantics &FltSem =
6402 Op->getType()->getScalarType()->getFltSemantics();
6404 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
6405
6406 if (Op->getOperand(0) == Op->getOperand(1) &&
6407 isGuaranteedNotToBeUndef(Op->getOperand(0), Q.AC, Q.CtxI, Q.DT)) {
6408 // X / X is always exactly 1.0 or a NaN.
6409 Known.setKnownFPClasses(fcNan | fcPosNormal);
6410
6411 if (!WantNan)
6412 break;
6413
6414 KnownFPClass KnownSrc;
6415 computeKnownFPClass(Op->getOperand(0), DemandedElts,
6416 fcNan | fcInf | fcZero | fcSubnormal, KnownSrc, Q,
6417 Depth + 1);
6418
6419 Known = KnownFPClass::fdiv_self(KnownSrc, Mode);
6420 break;
6421 }
6422
6423 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
6424 const bool WantPositive = (InterestedClasses & fcPositive) != fcNone;
6425 if (!WantNan && !WantNegative && !WantPositive)
6426 break;
6427
6428 KnownFPClass KnownLHS, KnownRHS;
6429 computeKnownFPClass(Op->getOperand(1), DemandedElts, fcAllFlags, KnownRHS,
6430 Q, Depth + 1);
6431
6432 bool KnowSomethingUseful =
6433 KnownRHS.isKnownNever(fcSNan) ||
6436
6437 if (KnowSomethingUseful)
6438 computeKnownFPClass(Op->getOperand(0), DemandedElts, fcAllFlags, KnownLHS,
6439 Q, Depth + 1);
6440
6441 Known = KnownFPClass::fdiv(KnownLHS, KnownRHS, Mode);
6442 break;
6443 }
6444 case Instruction::FRem: {
6445 computeKnownFPClassForFRem(Op, DemandedElts, InterestedClasses, Known, Q,
6446 Depth);
6447 break;
6448 }
6449 case Instruction::FPExt: {
6450 KnownFPClass KnownSrc;
6451 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
6452 KnownSrc, Q, Depth + 1);
6453
6454 const fltSemantics &DstTy =
6455 Op->getType()->getScalarType()->getFltSemantics();
6456 const fltSemantics &SrcTy =
6457 Op->getOperand(0)->getType()->getScalarType()->getFltSemantics();
6458
6459 const auto *I = dyn_cast<Instruction>(Op);
6460 const Function *F = I ? I->getFunction() : nullptr;
6461 DenormalMode DstMode =
6462 F ? F->getDenormalMode(DstTy) : DenormalMode::getDynamic();
6463 DenormalMode SrcMode =
6464 F ? F->getDenormalMode(SrcTy) : DenormalMode::getDynamic();
6465
6466 Known = KnownFPClass::fpext(KnownSrc, DstTy, SrcTy, DstMode, SrcMode);
6467 break;
6468 }
6469 case Instruction::FPTrunc: {
6470 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known, Q,
6471 Depth);
6472 break;
6473 }
6474 case Instruction::SIToFP:
6475 case Instruction::UIToFP: {
6476 // Cannot produce nan
6477 Known.knownNot(fcNan);
6478
6479 // Integers cannot be subnormal
6480 Known.knownNot(fcSubnormal);
6481
6482 // sitofp and uitofp turn into +0.0 for zero.
6483 Known.knownNot(fcNegZero);
6484
6485 // UIToFP is always non-negative regardless of known bits.
6486 if (Op->getOpcode() == Instruction::UIToFP)
6487 Known.signBitMustBeZero();
6488
6489 // Only compute known bits if we can learn something useful from them.
6490 if (!(InterestedClasses & (fcPosZero | fcNormal | fcInf)))
6491 break;
6492
6493 KnownBits IntKnown =
6494 computeKnownBits(Op->getOperand(0), DemandedElts, Q, Depth + 1);
6495
6496 // If the integer is non-zero, the result cannot be +0.0
6497 if (IntKnown.isNonZero())
6498 Known.knownNot(fcPosZero);
6499
6500 if (Op->getOpcode() == Instruction::SIToFP) {
6501 // If the signed integer is known non-negative, the result is
6502 // non-negative. If the signed integer is known negative, the result is
6503 // negative.
6504 if (IntKnown.isNonNegative()) {
6505 Known.signBitMustBeZero();
6506 } else if (IntKnown.isNegative()) {
6507 Known.signBitMustBeOne();
6508 }
6509 }
6510
6511 // Guard kept for ilogb()
6512 if (InterestedClasses & fcInf) {
6513 // Get width of largest magnitude integer known.
6514 // This still works for a signed minimum value because the largest FP
6515 // value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx).
6516 int IntSize = IntKnown.getBitWidth();
6517 if (Op->getOpcode() == Instruction::UIToFP)
6518 IntSize -= IntKnown.countMinLeadingZeros();
6519 else if (Op->getOpcode() == Instruction::SIToFP)
6520 IntSize -= IntKnown.countMinSignBits();
6521
6522 // If the exponent of the largest finite FP value can hold the largest
6523 // integer, the result of the cast must be finite.
6524 Type *FPTy = Op->getType()->getScalarType();
6525 if (ilogb(APFloat::getLargest(FPTy->getFltSemantics())) >= IntSize)
6526 Known.knownNot(fcInf);
6527 }
6528
6529 break;
6530 }
6531 case Instruction::ExtractElement: {
6532 // Look through extract element. If the index is non-constant or
6533 // out-of-range demand all elements, otherwise just the extracted element.
6534 const Value *Vec = Op->getOperand(0);
6535
6536 APInt DemandedVecElts;
6537 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
6538 unsigned NumElts = VecTy->getNumElements();
6539 DemandedVecElts = APInt::getAllOnes(NumElts);
6540 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(1));
6541 if (CIdx && CIdx->getValue().ult(NumElts))
6542 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
6543 } else {
6544 DemandedVecElts = APInt(1, 1);
6545 }
6546
6547 return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,
6548 Q, Depth + 1);
6549 }
6550 case Instruction::InsertElement: {
6551 if (isa<ScalableVectorType>(Op->getType()))
6552 return;
6553
6554 const Value *Vec = Op->getOperand(0);
6555 const Value *Elt = Op->getOperand(1);
6556 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(2));
6557 unsigned NumElts = DemandedElts.getBitWidth();
6558 APInt DemandedVecElts = DemandedElts;
6559 bool NeedsElt = true;
6560 // If we know the index we are inserting to, clear it from Vec check.
6561 if (CIdx && CIdx->getValue().ult(NumElts)) {
6562 DemandedVecElts.clearBit(CIdx->getZExtValue());
6563 NeedsElt = DemandedElts[CIdx->getZExtValue()];
6564 }
6565
6566 // Do we demand the inserted element?
6567 if (NeedsElt) {
6568 computeKnownFPClass(Elt, Known, InterestedClasses, Q, Depth + 1);
6569 // If we don't know any bits, early out.
6570 if (Known.isUnknown())
6571 break;
6572 } else {
6574 }
6575
6576 // Do we need anymore elements from Vec?
6577 if (!DemandedVecElts.isZero()) {
6578 KnownFPClass Known2;
6579 computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2, Q,
6580 Depth + 1);
6581 Known |= Known2;
6582 }
6583
6584 break;
6585 }
6586 case Instruction::ShuffleVector: {
6587 // Handle vector splat idiom
6588 if (Value *Splat = getSplatValue(V)) {
6589 computeKnownFPClass(Splat, Known, InterestedClasses, Q, Depth + 1);
6590 break;
6591 }
6592
6593 // For undef elements, we don't know anything about the common state of
6594 // the shuffle result.
6595 APInt DemandedLHS, DemandedRHS;
6596 auto *Shuf = dyn_cast<ShuffleVectorInst>(Op);
6597 if (!Shuf || !getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
6598 return;
6599
6600 if (!!DemandedLHS) {
6601 const Value *LHS = Shuf->getOperand(0);
6602 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known, Q,
6603 Depth + 1);
6604
6605 // If we don't know any bits, early out.
6606 if (Known.isUnknown())
6607 break;
6608 } else {
6610 }
6611
6612 if (!!DemandedRHS) {
6613 KnownFPClass Known2;
6614 const Value *RHS = Shuf->getOperand(1);
6615 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2, Q,
6616 Depth + 1);
6617 Known |= Known2;
6618 }
6619
6620 break;
6621 }
6622 case Instruction::ExtractValue: {
6623 const ExtractValueInst *Extract = cast<ExtractValueInst>(Op);
6624 ArrayRef<unsigned> Indices = Extract->getIndices();
6625 const Value *Src = Extract->getAggregateOperand();
6626 if (isa<StructType>(Src->getType()) && Indices.size() == 1 &&
6627 Indices[0] == 0) {
6628 if (const auto *II = dyn_cast<IntrinsicInst>(Src)) {
6629 switch (II->getIntrinsicID()) {
6630 case Intrinsic::frexp: {
6631 FPClassTest InterestedSrcs = InterestedClasses;
6632
6633 // Positive subnormals and negative subnormals could become positive
6634 // zero.
6635 if (InterestedClasses & fcPosZero)
6636 InterestedSrcs |= fcSubnormal;
6637
6638 // Negative subnormals could become negative zero.
6639 if (InterestedClasses & fcNegZero)
6640 InterestedSrcs |= fcNegSubnormal;
6641
6642 if (InterestedClasses & fcPosNormal)
6643 InterestedSrcs |= fcPosSubnormal;
6644
6645 if (InterestedClasses & fcNegNormal)
6646 InterestedSrcs |= fcNegSubnormal;
6647
6648 KnownFPClass KnownSrc;
6649 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
6650 InterestedSrcs, KnownSrc, Q, Depth + 1);
6651
6652 const Function *F = cast<Instruction>(Op)->getFunction();
6653 const fltSemantics &FltSem =
6654 Op->getType()->getScalarType()->getFltSemantics();
6655
6657 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
6658 Known = KnownFPClass::frexp_mant(KnownSrc, Mode);
6659 return;
6660 }
6661 default:
6662 break;
6663 }
6664 }
6665 }
6666
6667 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Q,
6668 Depth + 1);
6669 break;
6670 }
6671 case Instruction::PHI: {
6672 const PHINode *P = cast<PHINode>(Op);
6673 // Unreachable blocks may have zero-operand PHI nodes.
6674 if (P->getNumIncomingValues() == 0)
6675 break;
6676
6677 // Otherwise take the unions of the known bit sets of the operands,
6678 // taking conservative care to avoid excessive recursion.
6679 const unsigned PhiRecursionLimit = MaxAnalysisRecursionDepth - 2;
6680
6681 if (Depth < PhiRecursionLimit) {
6682 // Skip if every incoming value references to ourself.
6683 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
6684 break;
6685
6686 bool First = true;
6687
6688 for (const Use &U : P->operands()) {
6689 Value *IncValue;
6690 Instruction *CtxI;
6691 breakSelfRecursivePHI(&U, P, IncValue, CtxI);
6692 // Skip direct self references.
6693 if (IncValue == P)
6694 continue;
6695
6696 KnownFPClass KnownSrc;
6697 // Recurse, but cap the recursion to two levels, because we don't want
6698 // to waste time spinning around in loops. We need at least depth 2 to
6699 // detect known sign bits.
6700 computeKnownFPClass(IncValue, DemandedElts, InterestedClasses, KnownSrc,
6702 PhiRecursionLimit);
6703
6704 if (First) {
6705 Known = KnownSrc;
6706 First = false;
6707 } else {
6708 Known |= KnownSrc;
6709 }
6710
6711 if (Known.getKnownFPClasses() == fcAllFlags)
6712 break;
6713 }
6714 }
6715
6716 // Look for the case of a for loop which has a positive
6717 // initial value and is incremented by a squared value.
6718 // This will propagate sign information out of such loops.
6719 if (P->getNumIncomingValues() != 2 || Known.cannotBeOrderedLessThanZero())
6720 break;
6721 for (unsigned I = 0; I < 2; I++) {
6722 Value *RecurValue = P->getIncomingValue(1 - I);
6724 if (!II)
6725 continue;
6726 Value *R, *L, *Init;
6727 PHINode *PN;
6729 PN == P) {
6730 switch (II->getIntrinsicID()) {
6731 case Intrinsic::fma:
6732 case Intrinsic::fmuladd: {
6733 KnownFPClass KnownStart;
6734 computeKnownFPClass(Init, DemandedElts, InterestedClasses, KnownStart,
6735 Q, Depth + 1);
6736 if (KnownStart.cannotBeOrderedLessThanZero() && L == R &&
6737 isGuaranteedNotToBeUndef(L, Q.AC, Q.CtxI, Q.DT, Depth + 1))
6739 break;
6740 }
6741 }
6742 }
6743 }
6744 break;
6745 }
6746 case Instruction::BitCast: {
6747 const Value *Src;
6748 if (!match(Op, m_ElementWiseBitCast(m_Value(Src))) ||
6749 !Src->getType()->isIntOrIntVectorTy())
6750 break;
6751
6752 const Type *Ty = Op->getType();
6753
6754 Value *CastLHS, *CastRHS;
6755
6756 // Match bitcast(umax(bitcast(a), bitcast(b)))
6757 if (match(Src, m_c_MaxOrMin(m_BitCast(m_Value(CastLHS)),
6758 m_BitCast(m_Value(CastRHS)))) &&
6759 CastLHS->getType() == Ty && CastRHS->getType() == Ty) {
6760 KnownFPClass KnownLHS, KnownRHS;
6761 computeKnownFPClass(CastRHS, DemandedElts, InterestedClasses, KnownRHS, Q,
6762 Depth + 1);
6763 if (!KnownRHS.isUnknown()) {
6764 computeKnownFPClass(CastLHS, DemandedElts, InterestedClasses, KnownLHS,
6765 Q, Depth + 1);
6766 Known = KnownLHS | KnownRHS;
6767 }
6768
6769 return;
6770 }
6771
6772 const Type *EltTy = Ty->getScalarType();
6773 KnownBits Bits(EltTy->getPrimitiveSizeInBits());
6774 computeKnownBits(Src, DemandedElts, Bits, Q, Depth + 1);
6775
6777 break;
6778 }
6779 default:
6780 break;
6781 }
6782}
6783
6785 const APInt &DemandedElts,
6786 FPClassTest InterestedClasses,
6787 const SimplifyQuery &SQ,
6788 unsigned Depth) {
6789 KnownFPClass KnownClasses;
6790 ::computeKnownFPClass(V, DemandedElts, InterestedClasses, KnownClasses, SQ,
6791 Depth);
6792 return KnownClasses;
6793}
6794
6796 FPClassTest InterestedClasses,
6797 const SimplifyQuery &SQ,
6798 unsigned Depth) {
6800 ::computeKnownFPClass(V, Known, InterestedClasses, SQ, Depth);
6801 return Known;
6802}
6803
6805 const Value *V, const DataLayout &DL, FPClassTest InterestedClasses,
6806 const TargetLibraryInfo *TLI, AssumptionCache *AC, const Instruction *CtxI,
6807 const DominatorTree *DT, bool UseInstrInfo, unsigned Depth) {
6808 return computeKnownFPClass(V, InterestedClasses,
6809 SimplifyQuery(DL, TLI, DT, AC, CtxI, UseInstrInfo),
6810 Depth);
6811}
6812
6814llvm::computeKnownFPClass(const Value *V, const APInt &DemandedElts,
6815 FastMathFlags FMF, FPClassTest InterestedClasses,
6816 const SimplifyQuery &SQ, unsigned Depth) {
6817 if (FMF.noNaNs())
6818 InterestedClasses &= ~fcNan;
6819 if (FMF.noInfs())
6820 InterestedClasses &= ~fcInf;
6821
6822 KnownFPClass Result =
6823 computeKnownFPClass(V, DemandedElts, InterestedClasses, SQ, Depth);
6824
6825 if (FMF.noNaNs())
6826 Result.setKnownFPClasses(Result.getKnownFPClasses() & ~fcNan);
6827 if (FMF.noInfs())
6828 Result.setKnownFPClasses(Result.getKnownFPClasses() & ~fcInf);
6829 return Result;
6830}
6831
6833 FPClassTest InterestedClasses,
6834 const SimplifyQuery &SQ,
6835 unsigned Depth) {
6836 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
6837 APInt DemandedElts =
6838 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
6839 return computeKnownFPClass(V, DemandedElts, FMF, InterestedClasses, SQ,
6840 Depth);
6841}
6842
6844 unsigned Depth) {
6846 return Known.isKnownNever(fcNegZero);
6847}
6848
6850 unsigned Depth) {
6853 return Known.cannotBeOrderedLessThanZero();
6854}
6855
6857 unsigned Depth) {
6859 return Known.isKnownNeverInfinity();
6860}
6861
6862/// Return true if the floating-point value can never contain a NaN or infinity.
6864 unsigned Depth) {
6866 return Known.isKnownNeverNaN() && Known.isKnownNeverInfinity();
6867}
6868
6869/// Return true if the floating-point scalar value is not a NaN or if the
6870/// floating-point vector value has no NaN elements. Return false if a value
6871/// could ever be NaN.
6873 unsigned Depth) {
6875 return Known.isKnownNeverNaN();
6876}
6877
6878/// Return false if we can prove that the specified FP value's sign bit is 0.
6879/// Return true if we can prove that the specified FP value's sign bit is 1.
6880/// Otherwise return std::nullopt.
6881std::optional<bool> llvm::computeKnownFPSignBit(const Value *V,
6882 const SimplifyQuery &SQ,
6883 unsigned Depth) {
6885 return Known.getSignBit();
6886}
6887
6889 auto *User = cast<Instruction>(U.getUser());
6890 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6891 if (FPOp->hasNoSignedZeros())
6892 return true;
6893 }
6894
6895 switch (User->getOpcode()) {
6896 case Instruction::FPToSI:
6897 case Instruction::FPToUI:
6898 return true;
6899 case Instruction::FRem:
6900 // frem(x, +-0.0) == NaN, and more generally frem(x, y) == frem(x, -y).
6901 return U.getOperandNo() == 1;
6902 case Instruction::FCmp:
6903 // fcmp treats both positive and negative zero as equal.
6904 return true;
6905 case Instruction::Call:
6906 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6907 switch (II->getIntrinsicID()) {
6908 case Intrinsic::fabs:
6909 case Intrinsic::exp:
6910 case Intrinsic::exp2:
6911 case Intrinsic::exp10:
6912 case Intrinsic::log:
6913 case Intrinsic::log2:
6914 case Intrinsic::log10:
6915 case Intrinsic::cos:
6916 case Intrinsic::acos:
6917 case Intrinsic::cosh:
6918 case Intrinsic::fptosi_sat:
6919 case Intrinsic::fptoui_sat:
6920 case Intrinsic::lround:
6921 case Intrinsic::llround:
6922 case Intrinsic::lrint:
6923 case Intrinsic::llrint:
6924 return true;
6925 case Intrinsic::copysign:
6926 return U.getOperandNo() == 0;
6927 case Intrinsic::pow:
6928 // pow(x, +-0.0) == +1.0 or NaN
6929 return U.getOperandNo() == 1;
6930 case Intrinsic::is_fpclass: {
6931 auto Test =
6932 static_cast<FPClassTest>(
6933 cast<ConstantInt>(II->getArgOperand(1))->getZExtValue()) &
6936 }
6937 default:
6938 return false;
6939 }
6940 }
6941 return false;
6942 default:
6943 return false;
6944 }
6945}
6946
6948 auto *User = cast<Instruction>(U.getUser());
6949 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6950 if (FPOp->hasNoNaNs())
6951 return true;
6952 }
6953
6954 switch (User->getOpcode()) {
6955 case Instruction::FPToSI:
6956 case Instruction::FPToUI:
6957 return true;
6958 // Proper FP math operations ignore the sign bit of NaN.
6959 case Instruction::FAdd:
6960 case Instruction::FSub:
6961 case Instruction::FMul:
6962 case Instruction::FDiv:
6963 case Instruction::FRem:
6964 case Instruction::FPTrunc:
6965 case Instruction::FPExt:
6966 case Instruction::FCmp:
6967 return true;
6968 // Bitwise FP operations should preserve the sign bit of NaN.
6969 case Instruction::FNeg:
6970 case Instruction::Select:
6971 case Instruction::PHI:
6972 return false;
6973 case Instruction::Ret:
6974 return User->getFunction()->getAttributes().getRetNoFPClass() &
6976 case Instruction::Call:
6977 case Instruction::Invoke: {
6978 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6979 switch (II->getIntrinsicID()) {
6980 case Intrinsic::fabs:
6981 return true;
6982 case Intrinsic::copysign:
6983 return U.getOperandNo() == 0;
6984 // Other proper FP math intrinsics ignore the sign bit of NaN.
6985 case Intrinsic::maxnum:
6986 case Intrinsic::minnum:
6987 case Intrinsic::maximum:
6988 case Intrinsic::minimum:
6989 case Intrinsic::maximumnum:
6990 case Intrinsic::minimumnum:
6991 case Intrinsic::canonicalize:
6992 case Intrinsic::fma:
6993 case Intrinsic::fmuladd:
6994 case Intrinsic::sqrt:
6995 case Intrinsic::pow:
6996 case Intrinsic::powi:
6997 case Intrinsic::fptoui_sat:
6998 case Intrinsic::fptosi_sat:
6999 case Intrinsic::is_fpclass:
7000 return true;
7001 default:
7002 return false;
7003 }
7004 }
7005
7006 FPClassTest NoFPClass =
7007 cast<CallBase>(User)->getParamNoFPClass(U.getOperandNo());
7008 return NoFPClass & FPClassTest::fcNan;
7009 }
7010 default:
7011 return false;
7012 }
7013}
7014
7016 FastMathFlags FMF) {
7017 if (isa<PoisonValue>(V))
7018 return true;
7019 if (isa<UndefValue>(V))
7020 return false;
7021
7022 if (match(V, m_CheckedFp([](const APFloat &Val) { return Val.isInteger(); })))
7023 return true;
7024
7026 if (!I)
7027 return false;
7028
7029 switch (I->getOpcode()) {
7030 case Instruction::SIToFP:
7031 case Instruction::UIToFP:
7032 // TODO: Could check nofpclass(inf) on incoming argument
7033 if (FMF.noInfs())
7034 return true;
7035
7036 // Need to check int size cannot produce infinity, which computeKnownFPClass
7037 // knows how to do already.
7038 return isKnownNeverInfinity(I, SQ);
7039 case Instruction::Call: {
7040 const CallInst *CI = cast<CallInst>(I);
7041 switch (CI->getIntrinsicID()) {
7042 case Intrinsic::trunc:
7043 case Intrinsic::floor:
7044 case Intrinsic::ceil:
7045 case Intrinsic::rint:
7046 case Intrinsic::nearbyint:
7047 case Intrinsic::round:
7048 case Intrinsic::roundeven:
7049 return (FMF.noInfs() && FMF.noNaNs()) || isKnownNeverInfOrNaN(I, SQ);
7050 default:
7051 break;
7052 }
7053
7054 break;
7055 }
7056 default:
7057 break;
7058 }
7059
7060 return false;
7061}
7062
7064
7065 // All byte-wide stores are splatable, even of arbitrary variables.
7066 if (V->getType()->isIntegerTy(8))
7067 return V;
7068
7069 LLVMContext &Ctx = V->getContext();
7070
7071 // Undef don't care.
7072 auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
7073 if (isa<UndefValue>(V))
7074 return UndefInt8;
7075
7076 // Return poison for zero-sized type.
7077 if (DL.getTypeStoreSize(V->getType()).isZero())
7078 return PoisonValue::get(Type::getInt8Ty(Ctx));
7079
7081 if (!C) {
7082 // Conceptually, we could handle things like:
7083 // %a = zext i8 %X to i16
7084 // %b = shl i16 %a, 8
7085 // %c = or i16 %a, %b
7086 // but until there is an example that actually needs this, it doesn't seem
7087 // worth worrying about.
7088 return nullptr;
7089 }
7090
7091 // Handle 'null' ConstantArrayZero etc.
7092 if (C->isNullValue())
7094
7095 // Constant floating-point values can be handled as integer values if the
7096 // corresponding integer value is "byteable". An important case is 0.0.
7097 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
7098 Type *ScalarTy = CFP->getType()->getScalarType();
7099 if (ScalarTy->isHalfTy() || ScalarTy->isFloatTy() || ScalarTy->isDoubleTy())
7100 return isBytewiseValue(
7101 ConstantInt::get(Ctx, CFP->getValue().bitcastToAPInt()), DL);
7102
7103 // Don't handle long double formats, which have strange constraints.
7104 return nullptr;
7105 }
7106
7107 // We can handle constant integers that are multiple of 8 bits.
7108 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
7109 if (CI->getBitWidth() % 8 == 0) {
7110 if (!CI->getValue().isSplat(8))
7111 return nullptr;
7112 return ConstantInt::get(Ctx, CI->getValue().trunc(8));
7113 }
7114 }
7115
7116 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
7117 if (CE->getOpcode() == Instruction::IntToPtr) {
7118 if (auto *PtrTy = dyn_cast<PointerType>(CE->getType())) {
7119 unsigned BitWidth = DL.getPointerSizeInBits(PtrTy->getAddressSpace());
7121 CE->getOperand(0), Type::getIntNTy(Ctx, BitWidth), false, DL))
7122 return isBytewiseValue(Op, DL);
7123 }
7124 }
7125 }
7126
7127 auto Merge = [&](Value *LHS, Value *RHS) -> Value * {
7128 if (LHS == RHS)
7129 return LHS;
7130 if (!LHS || !RHS)
7131 return nullptr;
7132 if (LHS == UndefInt8)
7133 return RHS;
7134 if (RHS == UndefInt8)
7135 return LHS;
7136 return nullptr;
7137 };
7138
7140 Value *Val = UndefInt8;
7141 for (uint64_t I = 0, E = CA->getNumElements(); I != E; ++I)
7142 if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))
7143 return nullptr;
7144 return Val;
7145 }
7146
7148 Value *Val = UndefInt8;
7149 for (Value *Op : C->operands())
7150 if (!(Val = Merge(Val, isBytewiseValue(Op, DL))))
7151 return nullptr;
7152 return Val;
7153 }
7154
7155 // Don't try to handle the handful of other constants.
7156 return nullptr;
7157}
7158
7159// This is the recursive version of BuildSubAggregate. It takes a few different
7160// arguments. Idxs is the index within the nested struct From that we are
7161// looking at now (which is of type IndexedType). IdxSkip is the number of
7162// indices from Idxs that should be left out when inserting into the resulting
7163// struct. To is the result struct built so far, new insertvalue instructions
7164// build on that.
7165static Value *BuildSubAggregate(Value *From, Value *To, Type *IndexedType,
7167 unsigned IdxSkip,
7168 BasicBlock::iterator InsertBefore) {
7169 StructType *STy = dyn_cast<StructType>(IndexedType);
7170 if (STy) {
7171 // Save the original To argument so we can modify it
7172 Value *OrigTo = To;
7173 // General case, the type indexed by Idxs is a struct
7174 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
7175 // Process each struct element recursively
7176 Idxs.push_back(i);
7177 Value *PrevTo = To;
7178 To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
7179 InsertBefore);
7180 Idxs.pop_back();
7181 if (!To) {
7182 // Couldn't find any inserted value for this index? Cleanup
7183 while (PrevTo != OrigTo) {
7185 PrevTo = Del->getAggregateOperand();
7186 Del->eraseFromParent();
7187 }
7188 // Stop processing elements
7189 break;
7190 }
7191 }
7192 // If we successfully found a value for each of our subaggregates
7193 if (To)
7194 return To;
7195 }
7196 // Base case, the type indexed by SourceIdxs is not a struct, or not all of
7197 // the struct's elements had a value that was inserted directly. In the latter
7198 // case, perhaps we can't determine each of the subelements individually, but
7199 // we might be able to find the complete struct somewhere.
7200
7201 // Find the value that is at that particular spot
7202 Value *V = FindInsertedValue(From, Idxs);
7203
7204 if (!V)
7205 return nullptr;
7206
7207 // Insert the value in the new (sub) aggregate
7208 return InsertValueInst::Create(To, V, ArrayRef(Idxs).slice(IdxSkip), "tmp",
7209 InsertBefore);
7210}
7211
7212// This helper takes a nested struct and extracts a part of it (which is again a
7213// struct) into a new value. For example, given the struct:
7214// { a, { b, { c, d }, e } }
7215// and the indices "1, 1" this returns
7216// { c, d }.
7217//
7218// It does this by inserting an insertvalue for each element in the resulting
7219// struct, as opposed to just inserting a single struct. This will only work if
7220// each of the elements of the substruct are known (ie, inserted into From by an
7221// insertvalue instruction somewhere).
7222//
7223// All inserted insertvalue instructions are inserted before InsertBefore
7225 BasicBlock::iterator InsertBefore) {
7226 Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
7227 idx_range);
7228 Value *To = PoisonValue::get(IndexedType);
7229 SmallVector<unsigned, 10> Idxs(idx_range);
7230 unsigned IdxSkip = Idxs.size();
7231
7232 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
7233}
7234
7235/// Given an aggregate and a sequence of indices, see if the scalar value
7236/// indexed is already around as a register, for example if it was inserted
7237/// directly into the aggregate.
7238///
7239/// If InsertBefore is not null, this function will duplicate (modified)
7240/// insertvalues when a part of a nested struct is extracted.
7241Value *
7243 std::optional<BasicBlock::iterator> InsertBefore) {
7244 // Nothing to index? Just return V then (this is useful at the end of our
7245 // recursion).
7246 if (idx_range.empty())
7247 return V;
7248 // We have indices, so V should have an indexable type.
7249 assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
7250 "Not looking at a struct or array?");
7251 assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
7252 "Invalid indices for type?");
7253
7254 if (Constant *C = dyn_cast<Constant>(V)) {
7255 C = C->getAggregateElement(idx_range[0]);
7256 if (!C) return nullptr;
7257 return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
7258 }
7259
7261 // Loop the indices for the insertvalue instruction in parallel with the
7262 // requested indices
7263 const unsigned *req_idx = idx_range.begin();
7264 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
7265 i != e; ++i, ++req_idx) {
7266 if (req_idx == idx_range.end()) {
7267 // We can't handle this without inserting insertvalues
7268 if (!InsertBefore)
7269 return nullptr;
7270
7271 // The requested index identifies a part of a nested aggregate. Handle
7272 // this specially. For example,
7273 // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
7274 // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
7275 // %C = extractvalue {i32, { i32, i32 } } %B, 1
7276 // This can be changed into
7277 // %A = insertvalue {i32, i32 } undef, i32 10, 0
7278 // %C = insertvalue {i32, i32 } %A, i32 11, 1
7279 // which allows the unused 0,0 element from the nested struct to be
7280 // removed.
7281 return BuildSubAggregate(V, ArrayRef(idx_range.begin(), req_idx),
7282 *InsertBefore);
7283 }
7284
7285 // This insert value inserts something else than what we are looking for.
7286 // See if the (aggregate) value inserted into has the value we are
7287 // looking for, then.
7288 if (*req_idx != *i)
7289 return FindInsertedValue(I->getAggregateOperand(), idx_range,
7290 InsertBefore);
7291 }
7292 // If we end up here, the indices of the insertvalue match with those
7293 // requested (though possibly only partially). Now we recursively look at
7294 // the inserted value, passing any remaining indices.
7295 return FindInsertedValue(I->getInsertedValueOperand(),
7296 ArrayRef(req_idx, idx_range.end()), InsertBefore);
7297 }
7298
7300 // If we're extracting a value from an aggregate that was extracted from
7301 // something else, we can extract from that something else directly instead.
7302 // However, we will need to chain I's indices with the requested indices.
7303
7304 // Calculate the number of indices required
7305 unsigned size = I->getNumIndices() + idx_range.size();
7306 // Allocate some space to put the new indices in
7308 Idxs.reserve(size);
7309 // Add indices from the extract value instruction
7310 Idxs.append(I->idx_begin(), I->idx_end());
7311
7312 // Add requested indices
7313 Idxs.append(idx_range.begin(), idx_range.end());
7314
7315 assert(Idxs.size() == size
7316 && "Number of indices added not correct?");
7317
7318 return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
7319 }
7320 // Otherwise, we don't know (such as, extracting from a function return value
7321 // or load instruction)
7322 return nullptr;
7323}
7324
7325// If V refers to an initialized global constant, set Slice either to
7326// its initializer if the size of its elements equals ElementSize, or,
7327// for ElementSize == 8, to its representation as an array of unsiged
7328// char. Return true on success.
7329// Offset is in the unit "nr of ElementSize sized elements".
7332 unsigned ElementSize, uint64_t Offset) {
7333 assert(V && "V should not be null.");
7334 assert((ElementSize % 8) == 0 &&
7335 "ElementSize expected to be a multiple of the size of a byte.");
7336 unsigned ElementSizeInBytes = ElementSize / 8;
7337
7338 // Drill down into the pointer expression V, ignoring any intervening
7339 // casts, and determine the identity of the object it references along
7340 // with the cumulative byte offset into it.
7341 const GlobalVariable *GV =
7343 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
7344 // Fail if V is not based on constant global object.
7345 return false;
7346
7347 const DataLayout &DL = GV->getDataLayout();
7348 APInt Off(DL.getIndexTypeSizeInBits(V->getType()), 0);
7349
7351 /*AllowNonInbounds*/ true))
7352 // Fail if a constant offset could not be determined.
7353 return false;
7354
7355 uint64_t StartIdx = Off.getLimitedValue();
7356 if (StartIdx == UINT64_MAX)
7357 // Fail if the constant offset is excessive.
7358 return false;
7359
7360 // Off/StartIdx is in the unit of bytes. So we need to convert to number of
7361 // elements. Simply bail out if that isn't possible.
7362 if ((StartIdx % ElementSizeInBytes) != 0)
7363 return false;
7364
7365 Offset += StartIdx / ElementSizeInBytes;
7366 ConstantDataArray *Array = nullptr;
7367 ArrayType *ArrayTy = nullptr;
7368
7369 if (GV->getInitializer()->isNullValue()) {
7370 Type *GVTy = GV->getValueType();
7371 uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy).getFixedValue();
7372 uint64_t Length = SizeInBytes / ElementSizeInBytes;
7373
7374 Slice.Array = nullptr;
7375 Slice.Offset = 0;
7376 // Return an empty Slice for undersized constants to let callers
7377 // transform even undefined library calls into simpler, well-defined
7378 // expressions. This is preferable to making the calls although it
7379 // prevents sanitizers from detecting such calls.
7380 Slice.Length = Length < Offset ? 0 : Length - Offset;
7381 return true;
7382 }
7383
7384 auto *Init = const_cast<Constant *>(GV->getInitializer());
7385 if (auto *ArrayInit = dyn_cast<ConstantDataArray>(Init)) {
7386 Type *InitElTy = ArrayInit->getElementType();
7387 if (InitElTy->isIntegerTy(ElementSize)) {
7388 // If Init is an initializer for an array of the expected type
7389 // and size, use it as is.
7390 Array = ArrayInit;
7391 ArrayTy = ArrayInit->getType();
7392 }
7393 }
7394
7395 if (!Array) {
7396 if (ElementSize != 8)
7397 // TODO: Handle conversions to larger integral types.
7398 return false;
7399
7400 // Otherwise extract the portion of the initializer starting
7401 // at Offset as an array of bytes, and reset Offset.
7403 if (!Init)
7404 return false;
7405
7406 Offset = 0;
7408 ArrayTy = dyn_cast<ArrayType>(Init->getType());
7409 }
7410
7411 uint64_t NumElts = ArrayTy->getArrayNumElements();
7412 if (Offset > NumElts)
7413 return false;
7414
7415 Slice.Array = Array;
7416 Slice.Offset = Offset;
7417 Slice.Length = NumElts - Offset;
7418 return true;
7419}
7420
7421/// Extract bytes from the initializer of the constant array V, which need
7422/// not be a nul-terminated string. On success, store the bytes in Str and
7423/// return true. When TrimAtNul is set, Str will contain only the bytes up
7424/// to but not including the first nul. Return false on failure.
7426 bool TrimAtNul) {
7428 if (!getConstantDataArrayInfo(V, Slice, 8))
7429 return false;
7430
7431 if (Slice.Array == nullptr) {
7432 if (TrimAtNul) {
7433 // Return a nul-terminated string even for an empty Slice. This is
7434 // safe because all existing SimplifyLibcalls callers require string
7435 // arguments and the behavior of the functions they fold is undefined
7436 // otherwise. Folding the calls this way is preferable to making
7437 // the undefined library calls, even though it prevents sanitizers
7438 // from reporting such calls.
7439 Str = StringRef();
7440 return true;
7441 }
7442 if (Slice.Length == 1) {
7443 Str = StringRef("", 1);
7444 return true;
7445 }
7446 // We cannot instantiate a StringRef as we do not have an appropriate string
7447 // of 0s at hand.
7448 return false;
7449 }
7450
7451 // Start out with the entire array in the StringRef.
7452 Str = Slice.Array->getAsString();
7453 // Skip over 'offset' bytes.
7454 Str = Str.substr(Slice.Offset);
7455
7456 if (TrimAtNul) {
7457 // Trim off the \0 and anything after it. If the array is not nul
7458 // terminated, we just return the whole end of string. The client may know
7459 // some other way that the string is length-bound.
7460 Str = Str.substr(0, Str.find('\0'));
7461 }
7462 return true;
7463}
7464
7465// These next two are very similar to the above, but also look through PHI
7466// nodes.
7467// TODO: See if we can integrate these two together.
7468
7469/// If we can compute the length of the string pointed to by
7470/// the specified pointer, return 'len+1'. If we can't, return 0.
7473 unsigned CharSize) {
7474 // Look through noop bitcast instructions.
7475 V = V->stripPointerCasts();
7476
7477 // If this is a PHI node, there are two cases: either we have already seen it
7478 // or we haven't.
7479 if (const PHINode *PN = dyn_cast<PHINode>(V)) {
7480 if (!PHIs.insert(PN).second)
7481 return ~0ULL; // already in the set.
7482
7483 // If it was new, see if all the input strings are the same length.
7484 uint64_t LenSoFar = ~0ULL;
7485 for (Value *IncValue : PN->incoming_values()) {
7486 uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize);
7487 if (Len == 0) return 0; // Unknown length -> unknown.
7488
7489 if (Len == ~0ULL) continue;
7490
7491 if (Len != LenSoFar && LenSoFar != ~0ULL)
7492 return 0; // Disagree -> unknown.
7493 LenSoFar = Len;
7494 }
7495
7496 // Success, all agree.
7497 return LenSoFar;
7498 }
7499
7500 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
7501 if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
7502 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);
7503 if (Len1 == 0) return 0;
7504 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);
7505 if (Len2 == 0) return 0;
7506 if (Len1 == ~0ULL) return Len2;
7507 if (Len2 == ~0ULL) return Len1;
7508 if (Len1 != Len2) return 0;
7509 return Len1;
7510 }
7511
7512 // Otherwise, see if we can read the string.
7514 if (!getConstantDataArrayInfo(V, Slice, CharSize))
7515 return 0;
7516
7517 if (Slice.Array == nullptr)
7518 // Zeroinitializer (including an empty one).
7519 return 1;
7520
7521 // Search for the first nul character. Return a conservative result even
7522 // when there is no nul. This is safe since otherwise the string function
7523 // being folded such as strlen is undefined, and can be preferable to
7524 // making the undefined library call.
7525 unsigned NullIndex = 0;
7526 for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) {
7527 if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0)
7528 break;
7529 }
7530
7531 return NullIndex + 1;
7532}
7533
7534/// If we can compute the length of the string pointed to by
7535/// the specified pointer, return 'len+1'. If we can't, return 0.
7536uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) {
7537 if (!V->getType()->isPointerTy())
7538 return 0;
7539
7541 uint64_t Len = GetStringLengthH(V, PHIs, CharSize);
7542 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
7543 // an empty string as a length.
7544 return Len == ~0ULL ? 1 : Len;
7545}
7546
7547const Value *
7549 bool MustPreserveOffset,
7550 bool MustPreserveProvenance) {
7551 assert(Call &&
7552 "getArgumentAliasingToReturnedPointer only works on nonnull calls");
7553 if (const Value *RV = Call->getReturnedArgOperand())
7554 return RV;
7555 // This can be used only as a aliasing property.
7557 Call, MustPreserveOffset, MustPreserveProvenance))
7558 return Call->getArgOperand(0);
7559 return nullptr;
7560}
7561
7563 const CallBase *Call, bool MustPreserveOffset,
7564 bool MustPreserveProvenance) {
7565 switch (Call->getIntrinsicID()) {
7566 case Intrinsic::launder_invariant_group:
7567 case Intrinsic::aarch64_irg:
7568 case Intrinsic::aarch64_tagp:
7569 // The amdgcn_make_buffer_rsrc function does not alter the address of the
7570 // input pointer (and thus preserves the byte offset, which is the property
7571 // the MustPreserveOffset flag selects). However, it will not necessarily
7572 // map ptr addrspace(N) null to ptr addrspace(8) null, aka the "null
7573 // descriptor", which has "all loads return 0, all stores are dropped"
7574 // semantics. Given the context of this intrinsic list, no one should be
7575 // relying on such a strict bit-exact null mapping (and, at time of
7576 // writing, they are not), but we document this fact out of an abundance
7577 // of caution.
7578 case Intrinsic::amdgcn_make_buffer_rsrc:
7579 return !MustPreserveProvenance;
7580 case Intrinsic::ptrmask:
7581 return !MustPreserveOffset;
7582 case Intrinsic::threadlocal_address:
7583 // The underlying variable changes with thread ID. The Thread ID may change
7584 // at coroutine suspend points.
7585 return !Call->getParent()->getParent()->isPresplitCoroutine();
7586 default:
7587 return false;
7588 }
7589}
7590
7591/// \p PN defines a loop-variant pointer to an object. Check if the
7592/// previous iteration of the loop was referring to the same object as \p PN.
7594 const LoopInfo *LI) {
7595 // Find the loop-defined value.
7596 Loop *L = LI->getLoopFor(PN->getParent());
7597 if (PN->getNumIncomingValues() != 2)
7598 return true;
7599
7600 // Find the value from previous iteration.
7601 auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
7602 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
7603 PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
7604 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
7605 return true;
7606
7607 // If a new pointer is loaded in the loop, the pointer references a different
7608 // object in every iteration. E.g.:
7609 // for (i)
7610 // int *p = a[i];
7611 // ...
7612 if (auto *Load = dyn_cast<LoadInst>(PrevValue))
7613 if (!L->isLoopInvariant(Load->getPointerOperand()))
7614 return false;
7615 return true;
7616}
7617
7618const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup,
7619 bool MustPreserveProvenance) {
7620 for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
7621 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
7622 const Value *PtrOp = GEP->getPointerOperand();
7623 if (!PtrOp->getType()->isPointerTy()) // Only handle scalar pointer base.
7624 return V;
7625 V = PtrOp;
7626 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
7627 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
7628 Value *NewV = cast<Operator>(V)->getOperand(0);
7629 if (!NewV->getType()->isPointerTy())
7630 return V;
7631 V = NewV;
7632 } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
7633 if (GA->isInterposable())
7634 return V;
7635 V = GA->getAliasee();
7636 } else {
7637 if (auto *PHI = dyn_cast<PHINode>(V)) {
7638 // Look through single-arg phi nodes created by LCSSA.
7639 if (PHI->getNumIncomingValues() == 1) {
7640 V = PHI->getIncomingValue(0);
7641 continue;
7642 }
7643 } else if (auto *Call = dyn_cast<CallBase>(V)) {
7644 // CaptureTracking can know about special capturing properties of some
7645 // intrinsics like launder.invariant.group, that can't be expressed with
7646 // the attributes, but have properties like returning aliasing pointer.
7647 // Because some analysis may assume that nocaptured pointer is not
7648 // returned from some special intrinsic (because function would have to
7649 // be marked with returns attribute), it is crucial to use this function
7650 // because it should be in sync with CaptureTracking. Not using it may
7651 // cause weird miscompilations where 2 aliasing pointers are assumed to
7652 // noalias.
7654 Call, /*MustPreserveOffset=*/false, MustPreserveProvenance)) {
7655 V = RP;
7656 continue;
7657 }
7658 }
7659
7660 return V;
7661 }
7662 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
7663 }
7664 return V;
7665}
7666
7669 const LoopInfo *LI, unsigned MaxLookup) {
7672 Worklist.push_back(V);
7673 do {
7674 const Value *P = Worklist.pop_back_val();
7675 P = getUnderlyingObject(P, MaxLookup);
7676
7677 if (!Visited.insert(P).second)
7678 continue;
7679
7680 if (auto *SI = dyn_cast<SelectInst>(P)) {
7681 Worklist.push_back(SI->getTrueValue());
7682 Worklist.push_back(SI->getFalseValue());
7683 continue;
7684 }
7685
7686 if (auto *PN = dyn_cast<PHINode>(P)) {
7687 // If this PHI changes the underlying object in every iteration of the
7688 // loop, don't look through it. Consider:
7689 // int **A;
7690 // for (i) {
7691 // Prev = Curr; // Prev = PHI (Prev_0, Curr)
7692 // Curr = A[i];
7693 // *Prev, *Curr;
7694 //
7695 // Prev is tracking Curr one iteration behind so they refer to different
7696 // underlying objects.
7697 if (!LI || !LI->isLoopHeader(PN->getParent()) ||
7699 append_range(Worklist, PN->incoming_values());
7700 else
7701 Objects.push_back(P);
7702 continue;
7703 }
7704
7705 Objects.push_back(P);
7706 } while (!Worklist.empty());
7707}
7708
7710 bool MustPreserveProvenance) {
7711 const unsigned MaxVisited = 8;
7712
7715 Worklist.push_back(V);
7716 const Value *Object = nullptr;
7717 // Used as fallback if we can't find a common underlying object through
7718 // recursion.
7719 bool First = true;
7720 const Value *FirstObject =
7721 getUnderlyingObject(V, MaxLookupSearchDepth, MustPreserveProvenance);
7722 do {
7723 const Value *P = Worklist.pop_back_val();
7724 P = First ? FirstObject
7726 MustPreserveProvenance);
7727 First = false;
7728
7729 if (!Visited.insert(P).second)
7730 continue;
7731
7732 if (Visited.size() == MaxVisited)
7733 return FirstObject;
7734
7735 if (auto *SI = dyn_cast<SelectInst>(P)) {
7736 Worklist.push_back(SI->getTrueValue());
7737 Worklist.push_back(SI->getFalseValue());
7738 continue;
7739 }
7740
7741 if (auto *PN = dyn_cast<PHINode>(P)) {
7742 append_range(Worklist, PN->incoming_values());
7743 continue;
7744 }
7745
7746 if (!Object)
7747 Object = P;
7748 else if (Object != P)
7749 return FirstObject;
7750 } while (!Worklist.empty());
7751
7752 return Object ? Object : FirstObject;
7753}
7754
7755/// This is the function that does the work of looking through basic
7756/// ptrtoint+arithmetic+inttoptr sequences.
7758 do {
7759 if (const Operator *U = dyn_cast<Operator>(V)) {
7760 // If we find a ptrtoint, we can transfer control back to the
7761 // regular getUnderlyingObjectFromInt.
7762 if (U->getOpcode() == Instruction::PtrToInt)
7763 return U->getOperand(0);
7764 // If we find an add of a constant, a multiplied value, or a phi, it's
7765 // likely that the other operand will lead us to the base
7766 // object. We don't have to worry about the case where the
7767 // object address is somehow being computed by the multiply,
7768 // because our callers only care when the result is an
7769 // identifiable object.
7770 if (U->getOpcode() != Instruction::Add ||
7771 (!isa<ConstantInt>(U->getOperand(1)) &&
7772 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
7773 !isa<PHINode>(U->getOperand(1))))
7774 return V;
7775 V = U->getOperand(0);
7776 } else {
7777 return V;
7778 }
7779 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
7780 } while (true);
7781}
7782
7783/// This is a wrapper around getUnderlyingObjects and adds support for basic
7784/// ptrtoint+arithmetic+inttoptr sequences.
7785/// It returns false if unidentified object is found in getUnderlyingObjects.
7787 SmallVectorImpl<Value *> &Objects) {
7789 SmallVector<const Value *, 4> Working(1, V);
7790 bool AllObjectsIdentified = true;
7791 do {
7792 V = Working.pop_back_val();
7793
7795 getUnderlyingObjects(V, Objs);
7796
7797 for (const Value *V : Objs) {
7798 if (!Visited.insert(V).second)
7799 continue;
7800 if (Operator::getOpcode(V) == Instruction::IntToPtr) {
7801 const Value *O =
7802 getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
7803 if (O->getType()->isPointerTy()) {
7804 Working.push_back(O);
7805 continue;
7806 }
7807 }
7808 AllObjectsIdentified &= isIdentifiedObject(V);
7809 Objects.push_back(const_cast<Value *>(V));
7810 }
7811 } while (!Working.empty());
7812 return AllObjectsIdentified;
7813}
7814
7816 AllocaInst *Result = nullptr;
7818 SmallVector<Value *, 4> Worklist;
7819
7820 auto AddWork = [&](Value *V) {
7821 if (Visited.insert(V).second)
7822 Worklist.push_back(V);
7823 };
7824
7825 AddWork(V);
7826 do {
7827 V = Worklist.pop_back_val();
7828 assert(Visited.count(V));
7829
7830 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
7831 if (Result && Result != AI)
7832 return nullptr;
7833 Result = AI;
7834 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
7835 AddWork(CI->getOperand(0));
7836 } else if (PHINode *PN = dyn_cast<PHINode>(V)) {
7837 for (Value *IncValue : PN->incoming_values())
7838 AddWork(IncValue);
7839 } else if (auto *SI = dyn_cast<SelectInst>(V)) {
7840 AddWork(SI->getTrueValue());
7841 AddWork(SI->getFalseValue());
7843 if (OffsetZero && !GEP->hasAllZeroIndices())
7844 return nullptr;
7845 AddWork(GEP->getPointerOperand());
7846 } else if (CallBase *CB = dyn_cast<CallBase>(V)) {
7847 Value *Returned = CB->getReturnedArgOperand();
7848 if (Returned)
7849 AddWork(Returned);
7850 else
7851 return nullptr;
7852 } else {
7853 return nullptr;
7854 }
7855 } while (!Worklist.empty());
7856
7857 return Result;
7858}
7859
7861 if (auto *II = dyn_cast<IntrinsicInst>(I))
7862 return isTriviallyVectorizable(II->getIntrinsicID());
7863 auto *Shuffle = dyn_cast<ShuffleVectorInst>(I);
7864 return (!Shuffle || Shuffle->isSelect()) &&
7866}
7867
7869 const Instruction *Inst, const Instruction *CtxI, AssumptionCache *AC,
7870 const DominatorTree *DT, const TargetLibraryInfo *TLI, bool UseVariableInfo,
7871 bool IgnoreUBImplyingAttrs) {
7872 return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI,
7873 AC, DT, TLI, UseVariableInfo,
7874 IgnoreUBImplyingAttrs);
7875}
7876
7878 unsigned Opcode, const Instruction *Inst, const Instruction *CtxI,
7879 AssumptionCache *AC, const DominatorTree *DT, const TargetLibraryInfo *TLI,
7880 bool UseVariableInfo, bool IgnoreUBImplyingAttrs) {
7881#ifndef NDEBUG
7882 if (Inst->getOpcode() != Opcode) {
7883 // Check that the operands are actually compatible with the Opcode override.
7884 auto hasEqualReturnAndLeadingOperandTypes =
7885 [](const Instruction *Inst, unsigned NumLeadingOperands) {
7886 if (Inst->getNumOperands() < NumLeadingOperands)
7887 return false;
7888 const Type *ExpectedType = Inst->getType();
7889 for (unsigned ItOp = 0; ItOp < NumLeadingOperands; ++ItOp)
7890 if (Inst->getOperand(ItOp)->getType() != ExpectedType)
7891 return false;
7892 return true;
7893 };
7895 hasEqualReturnAndLeadingOperandTypes(Inst, 2));
7896 assert(!Instruction::isUnaryOp(Opcode) ||
7897 hasEqualReturnAndLeadingOperandTypes(Inst, 1));
7898 }
7899#endif
7900
7901 switch (Opcode) {
7902 default:
7903 return true;
7904 case Instruction::UDiv:
7905 case Instruction::URem: {
7906 // x / y is undefined if y == 0.
7907 const APInt *V;
7908 if (match(Inst->getOperand(1), m_APInt(V)))
7909 return *V != 0;
7910 return false;
7911 }
7912 case Instruction::SDiv:
7913 case Instruction::SRem: {
7914 // x / y is undefined if y == 0 or x == INT_MIN and y == -1
7915 const APInt *Numerator, *Denominator;
7916 if (!match(Inst->getOperand(1), m_APInt(Denominator)))
7917 return false;
7918 // We cannot hoist this division if the denominator is 0.
7919 if (*Denominator == 0)
7920 return false;
7921 // It's safe to hoist if the denominator is not 0 or -1.
7922 if (!Denominator->isAllOnes())
7923 return true;
7924 // At this point we know that the denominator is -1. It is safe to hoist as
7925 // long we know that the numerator is not INT_MIN.
7926 if (match(Inst->getOperand(0), m_APInt(Numerator)))
7927 return !Numerator->isMinSignedValue();
7928 // The numerator *might* be MinSignedValue.
7929 return false;
7930 }
7931 case Instruction::Load: {
7932 if (!UseVariableInfo)
7933 return false;
7934
7935 const LoadInst *LI = dyn_cast<LoadInst>(Inst);
7936 if (!LI)
7937 return false;
7938 if (mustSuppressSpeculation(*LI))
7939 return false;
7940 const DataLayout &DL = LI->getDataLayout();
7942 LI->getPointerOperand(), LI->getType(), LI->getAlign(),
7943 SimplifyQuery(DL, TLI, DT, AC, CtxI));
7944 }
7945 case Instruction::Call: {
7946 auto *CI = dyn_cast<const CallInst>(Inst);
7947 if (!CI)
7948 return false;
7949 const Function *Callee = CI->getCalledFunction();
7950
7951 // The called function could have undefined behavior or side-effects, even
7952 // if marked readnone nounwind.
7953 if (!Callee || !Callee->isSpeculatable())
7954 return false;
7955 // Since the operands may be changed after hoisting, undefined behavior may
7956 // be triggered by some UB-implying attributes.
7957 return IgnoreUBImplyingAttrs || !CI->hasUBImplyingAttrs();
7958 }
7959 case Instruction::VAArg:
7960 case Instruction::Alloca:
7961 case Instruction::Invoke:
7962 case Instruction::CallBr:
7963 case Instruction::PHI:
7964 case Instruction::Store:
7965 case Instruction::Ret:
7966 case Instruction::UncondBr:
7967 case Instruction::CondBr:
7968 case Instruction::IndirectBr:
7969 case Instruction::Switch:
7970 case Instruction::Unreachable:
7971 case Instruction::Fence:
7972 case Instruction::AtomicRMW:
7973 case Instruction::AtomicCmpXchg:
7974 case Instruction::LandingPad:
7975 case Instruction::Resume:
7976 case Instruction::CatchSwitch:
7977 case Instruction::CatchPad:
7978 case Instruction::CatchRet:
7979 case Instruction::CleanupPad:
7980 case Instruction::CleanupRet:
7981 return false; // Misc instructions which have effects
7982 }
7983}
7984
7986 if (I.mayReadOrWriteMemory())
7987 // Memory dependency possible
7988 return true;
7990 // Can't move above a maythrow call or infinite loop. Or if an
7991 // inalloca alloca, above a stacksave call.
7992 return true;
7994 // 1) Can't reorder two inf-loop calls, even if readonly
7995 // 2) Also can't reorder an inf-loop call below a instruction which isn't
7996 // safe to speculative execute. (Inverse of above)
7997 return true;
7998 return false;
7999}
8000
8001/// Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
8015
8016/// Combine constant ranges from computeConstantRange() and computeKnownBits().
8019 bool ForSigned,
8020 const SimplifyQuery &SQ) {
8021 ConstantRange CR1 =
8022 ConstantRange::fromKnownBits(V.getKnownBits(SQ), ForSigned);
8023 ConstantRange CR2 = computeConstantRange(V, ForSigned, SQ);
8026 return CR1.intersectWith(CR2, RangeType);
8027}
8028
8030 const Value *RHS,
8031 const SimplifyQuery &SQ,
8032 bool IsNSW) {
8033 ConstantRange LHSRange =
8034 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
8035 ConstantRange RHSRange =
8036 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
8037
8038 // mul nsw of two non-negative numbers is also nuw.
8039 if (IsNSW && LHSRange.isAllNonNegative() && RHSRange.isAllNonNegative())
8041
8042 return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange));
8043}
8044
8046 const Value *RHS,
8047 const SimplifyQuery &SQ) {
8048 // Multiplying n * m significant bits yields a result of n + m significant
8049 // bits. If the total number of significant bits does not exceed the
8050 // result bit width (minus 1), there is no overflow.
8051 // This means if we have enough leading sign bits in the operands
8052 // we can guarantee that the result does not overflow.
8053 // Ref: "Hacker's Delight" by Henry Warren
8054 unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
8055
8056 // Note that underestimating the number of sign bits gives a more
8057 // conservative answer.
8058 unsigned SignBits =
8059 ::ComputeNumSignBits(LHS, SQ) + ::ComputeNumSignBits(RHS, SQ);
8060
8061 // First handle the easy case: if we have enough sign bits there's
8062 // definitely no overflow.
8063 if (SignBits > BitWidth + 1)
8065
8066 // There are two ambiguous cases where there can be no overflow:
8067 // SignBits == BitWidth + 1 and
8068 // SignBits == BitWidth
8069 // The second case is difficult to check, therefore we only handle the
8070 // first case.
8071 if (SignBits == BitWidth + 1) {
8072 // It overflows only when both arguments are negative and the true
8073 // product is exactly the minimum negative number.
8074 // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
8075 // For simplicity we just check if at least one side is not negative.
8076 KnownBits LHSKnown = computeKnownBits(LHS, SQ);
8077 KnownBits RHSKnown = computeKnownBits(RHS, SQ);
8078 if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative())
8080 }
8082}
8083
8086 const WithCache<const Value *> &RHS,
8087 const SimplifyQuery &SQ) {
8088 ConstantRange LHSRange =
8089 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
8090 ConstantRange RHSRange =
8091 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
8092 return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange));
8093}
8094
8095static OverflowResult
8098 const AddOperator *Add, const SimplifyQuery &SQ) {
8099 if (Add && Add->hasNoSignedWrap()) {
8101 }
8102
8103 // If LHS and RHS each have at least two sign bits, the addition will look
8104 // like
8105 //
8106 // XX..... +
8107 // YY.....
8108 //
8109 // If the carry into the most significant position is 0, X and Y can't both
8110 // be 1 and therefore the carry out of the addition is also 0.
8111 //
8112 // If the carry into the most significant position is 1, X and Y can't both
8113 // be 0 and therefore the carry out of the addition is also 1.
8114 //
8115 // Since the carry into the most significant position is always equal to
8116 // the carry out of the addition, there is no signed overflow.
8117 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
8119
8120 ConstantRange LHSRange =
8121 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
8122 ConstantRange RHSRange =
8123 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
8124 OverflowResult OR =
8125 mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange));
8127 return OR;
8128
8129 // The remaining code needs Add to be available. Early returns if not so.
8130 if (!Add)
8132
8133 // If the sign of Add is the same as at least one of the operands, this add
8134 // CANNOT overflow. If this can be determined from the known bits of the
8135 // operands the above signedAddMayOverflow() check will have already done so.
8136 // The only other way to improve on the known bits is from an assumption, so
8137 // call computeKnownBitsFromContext() directly.
8138 bool LHSOrRHSKnownNonNegative =
8139 (LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative());
8140 bool LHSOrRHSKnownNegative =
8141 (LHSRange.isAllNegative() || RHSRange.isAllNegative());
8142 if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
8143 KnownBits AddKnown(LHSRange.getBitWidth());
8144 computeKnownBitsFromContext(Add, AddKnown, SQ);
8145 if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||
8146 (AddKnown.isNegative() && LHSOrRHSKnownNegative))
8148 }
8149
8151}
8152
8154 const Value *RHS,
8155 const SimplifyQuery &SQ) {
8156 // X - (X % ?)
8157 // The remainder of a value can't have greater magnitude than itself,
8158 // so the subtraction can't overflow.
8159
8160 // X - (X -nuw ?)
8161 // In the minimal case, this would simplify to "?", so there's no subtract
8162 // at all. But if this analysis is used to peek through casts, for example,
8163 // then determining no-overflow may allow other transforms.
8164
8165 // TODO: There are other patterns like this.
8166 // See simplifyICmpWithBinOpOnLHS() for candidates.
8167 if (match(RHS, m_URem(m_Specific(LHS), m_Value())) ||
8168 match(RHS, m_NUWSub(m_Specific(LHS), m_Value())))
8169 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CtxI, SQ.DT))
8171
8172 if (auto C = isImpliedByDomCondition(CmpInst::ICMP_UGE, LHS, RHS, SQ.CtxI,
8173 SQ.DL)) {
8174 if (*C)
8177 }
8178
8179 ConstantRange LHSRange =
8180 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
8181 ConstantRange RHSRange =
8182 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
8183 return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange));
8184}
8185
8187 const Value *RHS,
8188 const SimplifyQuery &SQ) {
8189 // X - (X % ?)
8190 // The remainder of a value can't have greater magnitude than itself,
8191 // so the subtraction can't overflow.
8192
8193 // X - (X -nsw ?)
8194 // In the minimal case, this would simplify to "?", so there's no subtract
8195 // at all. But if this analysis is used to peek through casts, for example,
8196 // then determining no-overflow may allow other transforms.
8197 if (match(RHS, m_SRem(m_Specific(LHS), m_Value())) ||
8198 match(RHS, m_NSWSub(m_Specific(LHS), m_Value())))
8199 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CtxI, SQ.DT))
8201
8202 // If LHS and RHS each have at least two sign bits, the subtraction
8203 // cannot overflow.
8204 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
8206
8207 ConstantRange LHSRange =
8208 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
8209 ConstantRange RHSRange =
8210 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
8211 return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange));
8212}
8213
8215 const DominatorTree &DT) {
8216 SmallVector<const CondBrInst *, 2> GuardingBranches;
8218
8219 for (const User *U : WO->users()) {
8220 if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) {
8221 assert(EVI->getNumIndices() == 1 && "Obvious from CI's type");
8222
8223 if (EVI->getIndices()[0] == 0)
8224 Results.push_back(EVI);
8225 else {
8226 assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type");
8227
8228 for (const auto *U : EVI->users())
8229 if (const auto *B = dyn_cast<CondBrInst>(U))
8230 GuardingBranches.push_back(B);
8231 }
8232 } else {
8233 // We are using the aggregate directly in a way we don't want to analyze
8234 // here (storing it to a global, say).
8235 return false;
8236 }
8237 }
8238
8239 auto AllUsesGuardedByBranch = [&](const CondBrInst *BI) {
8240 BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1));
8241
8242 // Check if all users of the add are provably no-wrap.
8243 for (const auto *Result : Results) {
8244 // If the extractvalue itself is not executed on overflow, the we don't
8245 // need to check each use separately, since domination is transitive.
8246 if (DT.dominates(NoWrapEdge, Result->getParent()))
8247 continue;
8248
8249 for (const auto &RU : Result->uses())
8250 if (!DT.dominates(NoWrapEdge, RU))
8251 return false;
8252 }
8253
8254 return true;
8255 };
8256
8257 return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
8258}
8259
8260/// Shifts return poison if shiftwidth is larger than the bitwidth.
8261static bool shiftAmountKnownInRange(const Value *ShiftAmount) {
8262 auto *C = dyn_cast<Constant>(ShiftAmount);
8263 if (!C)
8264 return false;
8265
8266 // Shifts return poison if shiftwidth is larger than the bitwidth.
8268 if (auto *FVTy = dyn_cast<FixedVectorType>(C->getType())) {
8269 unsigned NumElts = FVTy->getNumElements();
8270 for (unsigned i = 0; i < NumElts; ++i)
8271 ShiftAmounts.push_back(C->getAggregateElement(i));
8272 } else if (isa<ScalableVectorType>(C->getType()))
8273 return false; // Can't tell, just return false to be safe
8274 else
8275 ShiftAmounts.push_back(C);
8276
8277 bool Safe = llvm::all_of(ShiftAmounts, [](const Constant *C) {
8278 auto *CI = dyn_cast_or_null<ConstantInt>(C);
8279 return CI && CI->getValue().ult(C->getType()->getIntegerBitWidth());
8280 });
8281
8282 return Safe;
8283}
8284
8286 bool ConsiderFlagsAndMetadata) {
8287 const auto *CB = dyn_cast<CallBase>(Op);
8288 bool CBHasNoUndef = CB && CB->hasRetAttr(Attribute::NoUndef);
8289 if (ConsiderFlagsAndMetadata && includesPoison(Kind) &&
8290 Op->hasPoisonGeneratingAnnotations() && !CBHasNoUndef)
8291 return true;
8292
8293 unsigned Opcode = Op->getOpcode();
8294
8295 // Check whether opcode is a poison/undef-generating operation
8296 switch (Opcode) {
8297 case Instruction::Shl:
8298 case Instruction::AShr:
8299 case Instruction::LShr:
8300 return includesPoison(Kind) && !shiftAmountKnownInRange(Op->getOperand(1));
8301 case Instruction::FPToSI:
8302 case Instruction::FPToUI:
8303 // fptosi/ui yields poison if the resulting value does not fit in the
8304 // destination type.
8305 return true;
8306 case Instruction::Call:
8307 if (auto *II = dyn_cast<IntrinsicInst>(Op)) {
8308 switch (II->getIntrinsicID()) {
8309 // NOTE: Use IntrNoCreateUndefOrPoison when possible.
8310 case Intrinsic::ctlz:
8311 case Intrinsic::cttz:
8312 case Intrinsic::abs:
8313 // We're not considering flags so it is safe to just return false.
8314 return false;
8315 case Intrinsic::sshl_sat:
8316 case Intrinsic::ushl_sat:
8317 if (!includesPoison(Kind) ||
8318 shiftAmountKnownInRange(II->getArgOperand(1)))
8319 return false;
8320 break;
8321 }
8322 }
8323 [[fallthrough]];
8324 case Instruction::CallBr:
8325 case Instruction::Invoke: {
8326 return !CBHasNoUndef && !CB->hasFnAttr(Attribute::NoCreateUndefOrPoison);
8327 }
8328 case Instruction::InsertElement:
8329 case Instruction::ExtractElement: {
8330 // If index exceeds the length of the vector, it returns poison
8331 auto *VTy = cast<VectorType>(Op->getOperand(0)->getType());
8332 unsigned IdxOp = Op->getOpcode() == Instruction::InsertElement ? 2 : 1;
8333 auto *Idx = dyn_cast<ConstantInt>(Op->getOperand(IdxOp));
8334 if (includesPoison(Kind))
8335 return !Idx ||
8336 Idx->getValue().uge(VTy->getElementCount().getKnownMinValue());
8337 return false;
8338 }
8339 case Instruction::ShuffleVector: {
8341 ? cast<ConstantExpr>(Op)->getShuffleMask()
8342 : cast<ShuffleVectorInst>(Op)->getShuffleMask();
8343 return includesPoison(Kind) && is_contained(Mask, PoisonMaskElem);
8344 }
8345 case Instruction::FNeg:
8346 case Instruction::PHI:
8347 case Instruction::Select:
8348 case Instruction::ExtractValue:
8349 case Instruction::InsertValue:
8350 case Instruction::Freeze:
8351 case Instruction::ICmp:
8352 case Instruction::FCmp:
8353 case Instruction::GetElementPtr:
8354 return false;
8355 case Instruction::AddrSpaceCast:
8356 return true;
8357 default: {
8358 const auto *CE = dyn_cast<ConstantExpr>(Op);
8359 if (isa<CastInst>(Op) || (CE && CE->isCast()))
8360 return false;
8361 else if (Instruction::isBinaryOp(Opcode))
8362 return false;
8363 // Be conservative and return true.
8364 return true;
8365 }
8366 }
8367}
8368
8370 bool ConsiderFlagsAndMetadata) {
8371 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::UndefOrPoison,
8372 ConsiderFlagsAndMetadata);
8373}
8374
8375bool llvm::canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata) {
8376 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::PoisonOnly,
8377 ConsiderFlagsAndMetadata);
8378}
8379
8380static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V,
8381 unsigned Depth) {
8382 if (ValAssumedPoison == V)
8383 return true;
8384
8385 const unsigned MaxDepth = 2;
8386 if (Depth >= MaxDepth)
8387 return false;
8388
8389 if (const auto *I = dyn_cast<Instruction>(V)) {
8390 if (any_of(I->operands(), [=](const Use &Op) {
8391 return propagatesPoison(Op) &&
8392 directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1);
8393 }))
8394 return true;
8395
8396 // V = extractvalue V0, idx
8397 // V2 = extractvalue V0, idx2
8398 // V0's elements are all poison or not. (e.g., add_with_overflow)
8399 const WithOverflowInst *II;
8401 (match(ValAssumedPoison, m_ExtractValue(m_Specific(II))) ||
8402 llvm::is_contained(II->args(), ValAssumedPoison)))
8403 return true;
8404 }
8405 return false;
8406}
8407
8408static bool impliesPoison(const Value *ValAssumedPoison, const Value *V,
8409 unsigned Depth) {
8410 if (isGuaranteedNotToBePoison(ValAssumedPoison))
8411 return true;
8412
8413 if (directlyImpliesPoison(ValAssumedPoison, V, /* Depth */ 0))
8414 return true;
8415
8416 const unsigned MaxDepth = 2;
8417 if (Depth >= MaxDepth)
8418 return false;
8419
8420 const auto *I = dyn_cast<Instruction>(ValAssumedPoison);
8421 if (I && !canCreatePoison(cast<Operator>(I))) {
8422 return all_of(I->operands(), [=](const Value *Op) {
8423 return impliesPoison(Op, V, Depth + 1);
8424 });
8425 }
8426 return false;
8427}
8428
8429bool llvm::impliesPoison(const Value *ValAssumedPoison, const Value *V) {
8430 return ::impliesPoison(ValAssumedPoison, V, /* Depth */ 0);
8431}
8432
8433static bool programUndefinedIfUndefOrPoison(const Value *V, bool PoisonOnly);
8434
8436 const Value *V, AssumptionCache *AC, const Instruction *CtxI,
8437 const DominatorTree *DT, unsigned Depth, UndefPoisonKind Kind) {
8439 return false;
8440
8442 return false;
8443
8444 if (const auto *A = dyn_cast<Argument>(V)) {
8445 if (A->hasAttribute(Attribute::NoUndef) ||
8446 A->hasAttribute(Attribute::Dereferenceable) ||
8447 A->hasAttribute(Attribute::DereferenceableOrNull))
8448 return true;
8449 }
8450
8451 if (auto *C = dyn_cast<Constant>(V)) {
8452 if (isa<PoisonValue>(C))
8453 return !includesPoison(Kind);
8454
8455 if (isa<UndefValue>(C))
8456 return !includesUndef(Kind);
8457
8460 return true;
8461
8462 if (C->getType()->isVectorTy() || C->getType()->isAggregateType()) {
8463 if (isa<ConstantExpr>(C)) {
8464 // Scalable vectors can use a ConstantExpr to build a splat.
8465 if (Constant *SplatC = C->getSplatValue())
8466 if (isa<ConstantInt>(SplatC) || isa<ConstantFP>(SplatC))
8467 return true;
8468 } else if (auto *CA = dyn_cast<ConstantAggregate>(C)) {
8469 return all_of(CA->operands(), [&](const Use &Op) {
8470 return isGuaranteedNotToBeUndefOrPoison(Op.get(), AC, CtxI, DT,
8471 Depth + 1, Kind);
8472 });
8473 }
8474 }
8475 }
8476
8477 // Strip cast operations from a pointer value.
8478 // Note that stripPointerCastsSameRepresentation can strip off getelementptr
8479 // inbounds with zero offset. To guarantee that the result isn't poison, the
8480 // stripped pointer is checked as it has to be pointing into an allocated
8481 // object or be null `null` to ensure `inbounds` getelement pointers with a
8482 // zero offset could not produce poison.
8483 // It can strip off addrspacecast that do not change bit representation as
8484 // well. We believe that such addrspacecast is equivalent to no-op.
8485 auto *StrippedV = V->stripPointerCastsSameRepresentation();
8486 if (isa<AllocaInst>(StrippedV) || isa<GlobalVariable>(StrippedV) ||
8487 isa<Function>(StrippedV) || isa<ConstantPointerNull>(StrippedV))
8488 return true;
8489
8490 auto OpCheck = [&](const Value *V) {
8491 return isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth + 1, Kind);
8492 };
8493
8494 if (auto *Opr = dyn_cast<Operator>(V)) {
8495 // If the value is a freeze instruction, then it can never
8496 // be undef or poison.
8497 if (isa<FreezeInst>(V))
8498 return true;
8499
8500 if (const auto *CB = dyn_cast<CallBase>(V)) {
8501 if (CB->hasRetAttr(Attribute::NoUndef) ||
8502 CB->hasRetAttr(Attribute::Dereferenceable) ||
8503 CB->hasRetAttr(Attribute::DereferenceableOrNull))
8504 return true;
8505 }
8506
8507 if (!::canCreateUndefOrPoison(Opr, Kind,
8508 /*ConsiderFlagsAndMetadata=*/true)) {
8509 if (const auto *PN = dyn_cast<PHINode>(V)) {
8510 unsigned Num = PN->getNumIncomingValues();
8511 bool IsWellDefined = true;
8512 for (unsigned i = 0; i < Num; ++i) {
8513 if (PN == PN->getIncomingValue(i))
8514 continue;
8515 auto *TI = PN->getIncomingBlock(i)->getTerminator();
8516 if (!isGuaranteedNotToBeUndefOrPoison(PN->getIncomingValue(i), AC, TI,
8517 DT, Depth + 1, Kind)) {
8518 IsWellDefined = false;
8519 break;
8520 }
8521 }
8522 if (IsWellDefined)
8523 return true;
8524 } else if (auto *Splat = isa<ShuffleVectorInst>(Opr) ? getSplatValue(Opr)
8525 : nullptr) {
8526 // For splats we only need to check the value being splatted.
8527 if (OpCheck(Splat))
8528 return true;
8529 } else if (all_of(Opr->operands(), OpCheck))
8530 return true;
8531 }
8532 }
8533
8534 if (auto *I = dyn_cast<LoadInst>(V))
8535 if (I->hasMetadata(LLVMContext::MD_noundef) ||
8536 I->hasMetadata(LLVMContext::MD_dereferenceable) ||
8537 I->hasMetadata(LLVMContext::MD_dereferenceable_or_null))
8538 return true;
8539
8541 return true;
8542
8543 // CtxI may be null or a cloned instruction.
8544 if (!CtxI || !CtxI->getParent() || !DT)
8545 return false;
8546
8547 auto *DNode = DT->getNode(CtxI->getParent());
8548 if (!DNode)
8549 // Unreachable block
8550 return false;
8551
8552 // If V is used as a branch condition before reaching CtxI, V cannot be
8553 // undef or poison.
8554 // br V, BB1, BB2
8555 // BB1:
8556 // CtxI ; V cannot be undef or poison here
8557 auto *Dominator = DNode->getIDom();
8558 // This check is purely for compile time reasons: we can skip the IDom walk
8559 // if what we are checking for includes undef and the value is not an integer.
8560 if (!includesUndef(Kind) || V->getType()->isIntegerTy())
8561 while (Dominator) {
8562 auto *TI = Dominator->getBlock()->getTerminatorOrNull();
8563
8564 Value *Cond = nullptr;
8565 if (auto BI = dyn_cast_or_null<CondBrInst>(TI)) {
8566 Cond = BI->getCondition();
8567 } else if (auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
8568 Cond = SI->getCondition();
8569 }
8570
8571 if (Cond) {
8572 if (Cond == V)
8573 return true;
8574 else if (!includesUndef(Kind) && isa<Operator>(Cond)) {
8575 // For poison, we can analyze further
8576 auto *Opr = cast<Operator>(Cond);
8577 if (any_of(Opr->operands(), [V](const Use &U) {
8578 return V == U && propagatesPoison(U);
8579 }))
8580 return true;
8581 }
8582 }
8583
8584 Dominator = Dominator->getIDom();
8585 }
8586
8587 if (AC && getKnowledgeValidInContext(V, {Attribute::NoUndef}, *AC, CtxI, DT))
8588 return true;
8589
8590 return false;
8591}
8592
8594 const Instruction *CtxI,
8595 const DominatorTree *DT,
8596 unsigned Depth) {
8597 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
8599}
8600
8602 const Instruction *CtxI,
8603 const DominatorTree *DT, unsigned Depth) {
8604 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
8606}
8607
8609 const Instruction *CtxI,
8610 const DominatorTree *DT, unsigned Depth) {
8611 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
8613}
8614
8615/// Return true if undefined behavior would provably be executed on the path to
8616/// OnPathTo if Root produced a posion result. Note that this doesn't say
8617/// anything about whether OnPathTo is actually executed or whether Root is
8618/// actually poison. This can be used to assess whether a new use of Root can
8619/// be added at a location which is control equivalent with OnPathTo (such as
8620/// immediately before it) without introducing UB which didn't previously
8621/// exist. Note that a false result conveys no information.
8623 Instruction *OnPathTo,
8624 DominatorTree *DT) {
8625 // Basic approach is to assume Root is poison, propagate poison forward
8626 // through all users we can easily track, and then check whether any of those
8627 // users are provable UB and must execute before out exiting block might
8628 // exit.
8629
8630 // The set of all recursive users we've visited (which are assumed to all be
8631 // poison because of said visit)
8634 Worklist.push_back(Root);
8635 while (!Worklist.empty()) {
8636 const Instruction *I = Worklist.pop_back_val();
8637
8638 // If we know this must trigger UB on a path leading our target.
8639 if (mustTriggerUB(I, KnownPoison) && DT->dominates(I, OnPathTo))
8640 return true;
8641
8642 // If we can't analyze propagation through this instruction, just skip it
8643 // and transitive users. Safe as false is a conservative result.
8644 if (I != Root && !any_of(I->operands(), [&KnownPoison](const Use &U) {
8645 return KnownPoison.contains(U) && propagatesPoison(U);
8646 }))
8647 continue;
8648
8649 if (KnownPoison.insert(I).second)
8650 for (const User *User : I->users())
8651 Worklist.push_back(cast<Instruction>(User));
8652 }
8653
8654 // Might be non-UB, or might have a path we couldn't prove must execute on
8655 // way to exiting bb.
8656 return false;
8657}
8658
8660 const SimplifyQuery &SQ) {
8661 return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1),
8662 Add, SQ);
8663}
8664
8667 const WithCache<const Value *> &RHS,
8668 const SimplifyQuery &SQ) {
8669 return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, SQ);
8670}
8671
8673 // Note: An atomic operation isn't guaranteed to return in a reasonable amount
8674 // of time because it's possible for another thread to interfere with it for an
8675 // arbitrary length of time, but programs aren't allowed to rely on that.
8676
8677 // If there is no successor, then execution can't transfer to it.
8678 if (isa<ReturnInst>(I))
8679 return false;
8681 return false;
8682
8683 // Note: Do not add new checks here; instead, change Instruction::mayThrow or
8684 // Instruction::willReturn.
8685 //
8686 // FIXME: Move this check into Instruction::willReturn.
8687 if (isa<CatchPadInst>(I)) {
8688 switch (classifyEHPersonality(I->getFunction()->getPersonalityFn())) {
8689 default:
8690 // A catchpad may invoke exception object constructors and such, which
8691 // in some languages can be arbitrary code, so be conservative by default.
8692 return false;
8694 // For CoreCLR, it just involves a type test.
8695 return true;
8696 }
8697 }
8698
8699 // An instruction that returns without throwing must transfer control flow
8700 // to a successor.
8701 return !I->mayThrow() && I->willReturn();
8702}
8703
8705 // TODO: This is slightly conservative for invoke instruction since exiting
8706 // via an exception *is* normal control for them.
8707 for (const Instruction &I : *BB)
8709 return false;
8710 return true;
8711}
8712
8719
8722 assert(ScanLimit && "scan limit must be non-zero");
8723 for (const Instruction &I : Range) {
8724 if (--ScanLimit == 0)
8725 return false;
8727 return false;
8728 }
8729 return true;
8730}
8731
8733 const Loop *L) {
8734 // The loop header is guaranteed to be executed for every iteration.
8735 //
8736 // FIXME: Relax this constraint to cover all basic blocks that are
8737 // guaranteed to be executed at every iteration.
8738 if (I->getParent() != L->getHeader()) return false;
8739
8740 for (const Instruction &LI : *L->getHeader()) {
8741 if (&LI == I) return true;
8742 if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;
8743 }
8744 llvm_unreachable("Instruction not contained in its own parent basic block.");
8745}
8746
8748 switch (IID) {
8749 // TODO: Add more intrinsics.
8750 case Intrinsic::sadd_with_overflow:
8751 case Intrinsic::ssub_with_overflow:
8752 case Intrinsic::smul_with_overflow:
8753 case Intrinsic::uadd_with_overflow:
8754 case Intrinsic::usub_with_overflow:
8755 case Intrinsic::umul_with_overflow:
8756 // If an input is a vector containing a poison element, the
8757 // two output vectors (calculated results, overflow bits)'
8758 // corresponding lanes are poison.
8759 return true;
8760 case Intrinsic::ctpop:
8761 case Intrinsic::ctlz:
8762 case Intrinsic::cttz:
8763 case Intrinsic::abs:
8764 case Intrinsic::smax:
8765 case Intrinsic::smin:
8766 case Intrinsic::umax:
8767 case Intrinsic::umin:
8768 case Intrinsic::scmp:
8769 case Intrinsic::smulh:
8770 case Intrinsic::umulh:
8771 case Intrinsic::is_fpclass:
8772 case Intrinsic::ptrmask:
8773 case Intrinsic::ucmp:
8774 case Intrinsic::bitreverse:
8775 case Intrinsic::bswap:
8776 case Intrinsic::sadd_sat:
8777 case Intrinsic::ssub_sat:
8778 case Intrinsic::sshl_sat:
8779 case Intrinsic::uadd_sat:
8780 case Intrinsic::usub_sat:
8781 case Intrinsic::ushl_sat:
8782 case Intrinsic::smul_fix:
8783 case Intrinsic::smul_fix_sat:
8784 case Intrinsic::umul_fix:
8785 case Intrinsic::umul_fix_sat:
8786 case Intrinsic::pow:
8787 case Intrinsic::experimental_constrained_pow:
8788 case Intrinsic::powi:
8789 case Intrinsic::experimental_constrained_powi:
8790 case Intrinsic::sin:
8791 case Intrinsic::experimental_constrained_sin:
8792 case Intrinsic::sinh:
8793 case Intrinsic::experimental_constrained_sinh:
8794 case Intrinsic::cos:
8795 case Intrinsic::experimental_constrained_cos:
8796 case Intrinsic::cosh:
8797 case Intrinsic::experimental_constrained_cosh:
8798 case Intrinsic::sincos:
8799 case Intrinsic::sincospi:
8800 case Intrinsic::tan:
8801 case Intrinsic::experimental_constrained_tan:
8802 case Intrinsic::tanh:
8803 case Intrinsic::experimental_constrained_tanh:
8804 case Intrinsic::asin:
8805 case Intrinsic::experimental_constrained_asin:
8806 case Intrinsic::acos:
8807 case Intrinsic::experimental_constrained_acos:
8808 case Intrinsic::atan:
8809 case Intrinsic::experimental_constrained_atan:
8810 case Intrinsic::atan2:
8811 case Intrinsic::experimental_constrained_atan2:
8812 case Intrinsic::canonicalize:
8813 case Intrinsic::sqrt:
8814 case Intrinsic::experimental_constrained_sqrt:
8815 case Intrinsic::fma:
8816 case Intrinsic::experimental_constrained_fma:
8817 case Intrinsic::fmuladd:
8818 case Intrinsic::experimental_constrained_fmuladd:
8819 case Intrinsic::exp:
8820 case Intrinsic::experimental_constrained_exp:
8821 case Intrinsic::exp2:
8822 case Intrinsic::experimental_constrained_exp2:
8823 case Intrinsic::exp10:
8824 case Intrinsic::log:
8825 case Intrinsic::experimental_constrained_log:
8826 case Intrinsic::log2:
8827 case Intrinsic::experimental_constrained_log2:
8828 case Intrinsic::log10:
8829 case Intrinsic::experimental_constrained_log10:
8830 case Intrinsic::modf:
8831 case Intrinsic::floor:
8832 case Intrinsic::experimental_constrained_floor:
8833 case Intrinsic::ceil:
8834 case Intrinsic::experimental_constrained_ceil:
8835 case Intrinsic::trunc:
8836 case Intrinsic::experimental_constrained_trunc:
8837 case Intrinsic::rint:
8838 case Intrinsic::experimental_constrained_rint:
8839 case Intrinsic::nearbyint:
8840 case Intrinsic::experimental_constrained_nearbyint:
8841 case Intrinsic::round:
8842 case Intrinsic::experimental_constrained_round:
8843 case Intrinsic::roundeven:
8844 case Intrinsic::experimental_constrained_roundeven:
8845 case Intrinsic::lrint:
8846 case Intrinsic::experimental_constrained_lrint:
8847 case Intrinsic::llrint:
8848 case Intrinsic::experimental_constrained_llrint:
8849 case Intrinsic::lround:
8850 case Intrinsic::experimental_constrained_lround:
8851 case Intrinsic::llround:
8852 case Intrinsic::experimental_constrained_llround:
8853 case Intrinsic::fshl:
8854 case Intrinsic::fshr:
8855 case Intrinsic::frexp:
8856 case Intrinsic::ldexp:
8857 case Intrinsic::experimental_constrained_ldexp:
8858 case Intrinsic::get_active_lane_mask:
8859 case Intrinsic::mask_beforefirst:
8860 return true;
8861 default:
8862 return false;
8863 }
8864}
8865
8866bool llvm::propagatesPoison(const Use &PoisonOp) {
8867 const Operator *I = cast<Operator>(PoisonOp.getUser());
8868 switch (I->getOpcode()) {
8869 case Instruction::Freeze:
8870 case Instruction::PHI:
8871 case Instruction::Invoke:
8872 return false;
8873 case Instruction::Select:
8874 return PoisonOp.getOperandNo() == 0;
8875 case Instruction::Call:
8876 if (auto *II = dyn_cast<IntrinsicInst>(I))
8877 return intrinsicPropagatesPoison(II->getIntrinsicID());
8878 return false;
8879 case Instruction::ICmp:
8880 case Instruction::FCmp:
8881 case Instruction::GetElementPtr:
8882 return true;
8883 default:
8885 return true;
8886
8887 // Be conservative and return false.
8888 return false;
8889 }
8890}
8891
8892/// Enumerates all operands of \p I that are guaranteed to not be undef or
8893/// poison. If the callback \p Handle returns true, stop processing and return
8894/// true. Otherwise, return false.
8895template <typename CallableT>
8897 const CallableT &Handle) {
8898 switch (I->getOpcode()) {
8899 case Instruction::Store:
8900 if (Handle(cast<StoreInst>(I)->getPointerOperand()))
8901 return true;
8902 break;
8903
8904 case Instruction::Load:
8905 if (Handle(cast<LoadInst>(I)->getPointerOperand()))
8906 return true;
8907 break;
8908
8909 // Since dereferenceable attribute imply noundef, atomic operations
8910 // also implicitly have noundef pointers too
8911 case Instruction::AtomicCmpXchg:
8913 return true;
8914 break;
8915
8916 case Instruction::AtomicRMW:
8917 if (Handle(cast<AtomicRMWInst>(I)->getPointerOperand()))
8918 return true;
8919 break;
8920
8921 case Instruction::Call:
8922 case Instruction::Invoke: {
8923 const CallBase *CB = cast<CallBase>(I);
8924 if (CB->isIndirectCall() && Handle(CB->getCalledOperand()))
8925 return true;
8926 for (unsigned i = 0; i < CB->arg_size(); ++i)
8927 if ((CB->paramHasAttr(i, Attribute::NoUndef) ||
8928 CB->paramHasAttr(i, Attribute::Dereferenceable) ||
8929 CB->paramHasAttr(i, Attribute::DereferenceableOrNull)) &&
8930 Handle(CB->getArgOperand(i)))
8931 return true;
8932 break;
8933 }
8934 case Instruction::Ret:
8935 if (I->getFunction()->hasRetAttribute(Attribute::NoUndef) &&
8936 Handle(I->getOperand(0)))
8937 return true;
8938 break;
8939 case Instruction::Switch:
8940 if (Handle(cast<SwitchInst>(I)->getCondition()))
8941 return true;
8942 break;
8943 case Instruction::CondBr:
8944 if (Handle(cast<CondBrInst>(I)->getCondition()))
8945 return true;
8946 break;
8947 default:
8948 break;
8949 }
8950
8951 return false;
8952}
8953
8954/// Enumerates all operands of \p I that are guaranteed to not be poison.
8955template <typename CallableT>
8957 const CallableT &Handle) {
8958 if (handleGuaranteedWellDefinedOps(I, Handle))
8959 return true;
8960 switch (I->getOpcode()) {
8961 // Divisors of these operations are allowed to be partially undef.
8962 case Instruction::UDiv:
8963 case Instruction::SDiv:
8964 case Instruction::URem:
8965 case Instruction::SRem:
8966 return Handle(I->getOperand(1));
8967 default:
8968 return false;
8969 }
8970}
8971
8973 const SmallPtrSetImpl<const Value *> &KnownPoison) {
8975 I, [&](const Value *V) { return KnownPoison.count(V); });
8976}
8977
8979 bool PoisonOnly) {
8980 // We currently only look for uses of values within the same basic
8981 // block, as that makes it easier to guarantee that the uses will be
8982 // executed given that Inst is executed.
8983 //
8984 // FIXME: Expand this to consider uses beyond the same basic block. To do
8985 // this, look out for the distinction between post-dominance and strong
8986 // post-dominance.
8987 const BasicBlock *BB = nullptr;
8989 if (const auto *Inst = dyn_cast<Instruction>(V)) {
8990 BB = Inst->getParent();
8991 Begin = Inst->getIterator();
8992 Begin++;
8993 } else if (const auto *Arg = dyn_cast<Argument>(V)) {
8994 if (Arg->getParent()->isDeclaration())
8995 return false;
8996 BB = &Arg->getParent()->getEntryBlock();
8997 Begin = BB->begin();
8998 } else {
8999 return false;
9000 }
9001
9002 // Limit number of instructions we look at, to avoid scanning through large
9003 // blocks. The current limit is chosen arbitrarily.
9004 unsigned ScanLimit = 32;
9006
9007 if (!PoisonOnly) {
9008 // Since undef does not propagate eagerly, be conservative & just check
9009 // whether a value is directly passed to an instruction that must take
9010 // well-defined operands.
9011
9012 for (const auto &I : make_range(Begin, End)) {
9013 if (--ScanLimit == 0)
9014 break;
9015
9016 if (handleGuaranteedWellDefinedOps(&I, [V](const Value *WellDefinedOp) {
9017 return WellDefinedOp == V;
9018 }))
9019 return true;
9020
9022 break;
9023 }
9024 return false;
9025 }
9026
9027 // Set of instructions that we have proved will yield poison if Inst
9028 // does.
9029 SmallPtrSet<const Value *, 16> YieldsPoison;
9031
9032 YieldsPoison.insert(V);
9033 Visited.insert(BB);
9034
9035 while (true) {
9036 for (const auto &I : make_range(Begin, End)) {
9037 if (--ScanLimit == 0)
9038 return false;
9039 if (mustTriggerUB(&I, YieldsPoison))
9040 return true;
9042 return false;
9043
9044 // If an operand is poison and propagates it, mark I as yielding poison.
9045 for (const Use &Op : I.operands()) {
9046 if (YieldsPoison.count(Op) && propagatesPoison(Op)) {
9047 YieldsPoison.insert(&I);
9048 break;
9049 }
9050 }
9051
9052 // Special handling for select, which returns poison if its operand 0 is
9053 // poison (handled in the loop above) *or* if both its true/false operands
9054 // are poison (handled here).
9055 if (I.getOpcode() == Instruction::Select &&
9056 YieldsPoison.count(I.getOperand(1)) &&
9057 YieldsPoison.count(I.getOperand(2))) {
9058 YieldsPoison.insert(&I);
9059 }
9060 }
9061
9062 BB = BB->getSingleSuccessor();
9063 if (!BB || !Visited.insert(BB).second)
9064 break;
9065
9066 Begin = BB->getFirstNonPHIIt();
9067 End = BB->end();
9068 }
9069 return false;
9070}
9071
9073 return ::programUndefinedIfUndefOrPoison(Inst, false);
9074}
9075
9077 return ::programUndefinedIfUndefOrPoison(Inst, true);
9078}
9079
9080static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) {
9081 if (FMF.noNaNs())
9082 return true;
9083
9084 if (auto *C = dyn_cast<ConstantFP>(V))
9085 return !C->isNaN();
9086
9087 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
9088 if (!C->getElementType()->isFloatingPointTy())
9089 return false;
9090 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
9091 if (C->getElementAsAPFloat(I).isNaN())
9092 return false;
9093 }
9094 return true;
9095 }
9096
9098 return true;
9099
9100 return false;
9101}
9102
9103static bool isKnownNonZero(const Value *V) {
9104 if (auto *C = dyn_cast<ConstantFP>(V))
9105 return !C->isZero();
9106
9107 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
9108 if (!C->getElementType()->isFloatingPointTy())
9109 return false;
9110 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
9111 if (C->getElementAsAPFloat(I).isZero())
9112 return false;
9113 }
9114 return true;
9115 }
9116
9117 return false;
9118}
9119
9120/// Match clamp pattern for float types without care about NaNs or signed zeros.
9121/// Given non-min/max outer cmp/select from the clamp pattern this
9122/// function recognizes if it can be substitued by a "canonical" min/max
9123/// pattern.
9125 Value *CmpLHS, Value *CmpRHS,
9126 Value *TrueVal, Value *FalseVal,
9127 Value *&LHS, Value *&RHS) {
9128 // Try to match
9129 // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2))
9130 // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2))
9131 // and return description of the outer Max/Min.
9132
9133 // First, check if select has inverse order:
9134 if (CmpRHS == FalseVal) {
9135 std::swap(TrueVal, FalseVal);
9137 }
9138
9139 // Assume success now. If there's no match, callers should not use these anyway.
9140 LHS = TrueVal;
9141 RHS = FalseVal;
9142
9143 const APFloat *FC1;
9144 if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite())
9145 return {SPF_UNKNOWN, SPNB_NA, false};
9146
9147 const APFloat *FC2;
9148 switch (Pred) {
9149 case CmpInst::FCMP_OLT:
9150 case CmpInst::FCMP_OLE:
9151 case CmpInst::FCMP_ULT:
9152 case CmpInst::FCMP_ULE:
9153 if (match(FalseVal, m_OrdOrUnordFMin(m_Specific(CmpLHS), m_APFloat(FC2))) &&
9154 *FC1 < *FC2)
9155 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
9156 if (match(FalseVal, m_FMinNum(m_Specific(CmpLHS), m_APFloat(FC2))) &&
9157 *FC1 < *FC2)
9158 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
9159 break;
9160 case CmpInst::FCMP_OGT:
9161 case CmpInst::FCMP_OGE:
9162 case CmpInst::FCMP_UGT:
9163 case CmpInst::FCMP_UGE:
9164 if (match(FalseVal, m_OrdOrUnordFMax(m_Specific(CmpLHS), m_APFloat(FC2))) &&
9165 *FC1 > *FC2)
9166 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
9167 if (match(FalseVal, m_FMaxNum(m_Specific(CmpLHS), m_APFloat(FC2))) &&
9168 *FC1 > *FC2)
9169 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
9170 break;
9171 default:
9172 break;
9173 }
9174
9175 return {SPF_UNKNOWN, SPNB_NA, false};
9176}
9177
9178/// Recognize variations of:
9179/// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
9181 Value *CmpLHS, Value *CmpRHS,
9182 Value *TrueVal, Value *FalseVal) {
9183 // Swap the select operands and predicate to match the patterns below.
9184 if (CmpRHS != TrueVal) {
9186 std::swap(TrueVal, FalseVal);
9187 }
9188 const APInt *C1;
9189 if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) {
9190 const APInt *C2;
9191 // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
9192 if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) &&
9193 C1->slt(*C2) && Pred == CmpInst::ICMP_SLT)
9194 return {SPF_SMAX, SPNB_NA, false};
9195
9196 // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
9197 if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) &&
9198 C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT)
9199 return {SPF_SMIN, SPNB_NA, false};
9200
9201 // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
9202 if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) &&
9203 C1->ult(*C2) && Pred == CmpInst::ICMP_ULT)
9204 return {SPF_UMAX, SPNB_NA, false};
9205
9206 // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
9207 if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) &&
9208 C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT)
9209 return {SPF_UMIN, SPNB_NA, false};
9210 }
9211 return {SPF_UNKNOWN, SPNB_NA, false};
9212}
9213
9214/// If the input value is the result of a 'not' op, constant integer, or vector
9215/// splat of a constant integer, return the bitwise-not source value.
9216/// TODO: This could be extended to handle non-splat vector integer constants.
9218 Value *NotV;
9219 if (match(V, m_Not(m_Value(NotV))))
9220 return NotV;
9221
9222 const APInt *C;
9223 if (match(V, m_APInt(C)))
9224 return ConstantInt::get(V->getType(), ~(*C));
9225
9226 return nullptr;
9227}
9228
9229/// Match non-obvious integer minimum and maximum sequences.
9231 Value *CmpRHS, Value *TrueVal,
9232 Value *FalseVal, Value *&LHS,
9233 Value *&RHS) {
9234 // Assume success. If there's no match, callers should not use these anyway.
9235 LHS = TrueVal;
9236 RHS = FalseVal;
9237
9238 SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);
9240 return SPR;
9241
9242 // Look through 'not' ops to find disguised min/max.
9243 // (X > Y) ? ~X : ~Y ==> (~X < ~Y) ? ~X : ~Y ==> MIN(~X, ~Y)
9244 // (X < Y) ? ~X : ~Y ==> (~X > ~Y) ? ~X : ~Y ==> MAX(~X, ~Y)
9245 if (CmpLHS == getNotValue(TrueVal) && CmpRHS == getNotValue(FalseVal)) {
9246 switch (Pred) {
9247 case CmpInst::ICMP_SGT: return {SPF_SMIN, SPNB_NA, false};
9248 case CmpInst::ICMP_SLT: return {SPF_SMAX, SPNB_NA, false};
9249 case CmpInst::ICMP_UGT: return {SPF_UMIN, SPNB_NA, false};
9250 case CmpInst::ICMP_ULT: return {SPF_UMAX, SPNB_NA, false};
9251 default: break;
9252 }
9253 }
9254
9255 // (X > Y) ? ~Y : ~X ==> (~X < ~Y) ? ~Y : ~X ==> MAX(~Y, ~X)
9256 // (X < Y) ? ~Y : ~X ==> (~X > ~Y) ? ~Y : ~X ==> MIN(~Y, ~X)
9257 if (CmpLHS == getNotValue(FalseVal) && CmpRHS == getNotValue(TrueVal)) {
9258 switch (Pred) {
9259 case CmpInst::ICMP_SGT: return {SPF_SMAX, SPNB_NA, false};
9260 case CmpInst::ICMP_SLT: return {SPF_SMIN, SPNB_NA, false};
9261 case CmpInst::ICMP_UGT: return {SPF_UMAX, SPNB_NA, false};
9262 case CmpInst::ICMP_ULT: return {SPF_UMIN, SPNB_NA, false};
9263 default: break;
9264 }
9265 }
9266
9268 return {SPF_UNKNOWN, SPNB_NA, false};
9269
9270 const APInt *C1;
9271 if (!match(CmpRHS, m_APInt(C1)))
9272 return {SPF_UNKNOWN, SPNB_NA, false};
9273
9274 // An unsigned min/max can be written with a signed compare.
9275 const APInt *C2;
9276 if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) ||
9277 (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) {
9278 // Is the sign bit set?
9279 // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX
9280 // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN
9281 if (Pred == CmpInst::ICMP_SLT && C1->isZero() && C2->isMaxSignedValue())
9282 return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
9283
9284 // Is the sign bit clear?
9285 // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX
9286 // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN
9287 if (Pred == CmpInst::ICMP_SGT && C1->isAllOnes() && C2->isMinSignedValue())
9288 return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
9289 }
9290
9291 return {SPF_UNKNOWN, SPNB_NA, false};
9292}
9293
9294bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW,
9295 bool AllowPoison) {
9296 assert(X && Y && "Invalid operand");
9297
9298 auto IsNegationOf = [&](const Value *X, const Value *Y) {
9299 if (!match(X, m_Neg(m_Specific(Y))))
9300 return false;
9301
9302 auto *BO = cast<BinaryOperator>(X);
9303 if (NeedNSW && !BO->hasNoSignedWrap())
9304 return false;
9305
9306 auto *Zero = cast<Constant>(BO->getOperand(0));
9307 if (!AllowPoison && !Zero->isNullValue())
9308 return false;
9309
9310 return true;
9311 };
9312
9313 // X = -Y or Y = -X
9314 if (IsNegationOf(X, Y) || IsNegationOf(Y, X))
9315 return true;
9316
9317 // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A)
9318 Value *A, *B;
9319 return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) &&
9320 match(Y, m_Sub(m_Specific(B), m_Specific(A))))) ||
9321 (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) &&
9323}
9324
9325bool llvm::isKnownInversion(const Value *X, const Value *Y) {
9326 // Handle X = icmp pred A, B, Y = icmp pred A, C.
9327 Value *A, *B, *C;
9328 CmpPredicate Pred1, Pred2;
9329 if (!match(X, m_ICmp(Pred1, m_Value(A), m_Value(B))) ||
9330 !match(Y, m_c_ICmp(Pred2, m_Specific(A), m_Value(C))))
9331 return false;
9332
9333 // They must both have samesign flag or not.
9334 if (Pred1.hasSameSign() != Pred2.hasSameSign())
9335 return false;
9336
9337 if (B == C)
9338 return Pred1 == ICmpInst::getInversePredicate(Pred2);
9339
9340 // Try to infer the relationship from constant ranges.
9341 const APInt *RHSC1, *RHSC2;
9342 if (!match(B, m_APInt(RHSC1)) || !match(C, m_APInt(RHSC2)))
9343 return false;
9344
9345 // Sign bits of two RHSCs should match.
9346 if (Pred1.hasSameSign() && RHSC1->isNonNegative() != RHSC2->isNonNegative())
9347 return false;
9348
9349 const auto CR1 = ConstantRange::makeExactICmpRegion(Pred1, *RHSC1);
9350 const auto CR2 = ConstantRange::makeExactICmpRegion(Pred2, *RHSC2);
9351
9352 return CR1.inverse() == CR2;
9353}
9354
9356 SelectPatternNaNBehavior NaNBehavior,
9357 bool Ordered) {
9358 switch (Pred) {
9359 default:
9360 return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.
9361 case ICmpInst::ICMP_UGT:
9362 case ICmpInst::ICMP_UGE:
9363 return {SPF_UMAX, SPNB_NA, false};
9364 case ICmpInst::ICMP_SGT:
9365 case ICmpInst::ICMP_SGE:
9366 return {SPF_SMAX, SPNB_NA, false};
9367 case ICmpInst::ICMP_ULT:
9368 case ICmpInst::ICMP_ULE:
9369 return {SPF_UMIN, SPNB_NA, false};
9370 case ICmpInst::ICMP_SLT:
9371 case ICmpInst::ICMP_SLE:
9372 return {SPF_SMIN, SPNB_NA, false};
9373 case FCmpInst::FCMP_UGT:
9374 case FCmpInst::FCMP_UGE:
9375 case FCmpInst::FCMP_OGT:
9376 case FCmpInst::FCMP_OGE:
9377 return {SPF_FMAXNUM, NaNBehavior, Ordered};
9378 case FCmpInst::FCMP_ULT:
9379 case FCmpInst::FCMP_ULE:
9380 case FCmpInst::FCMP_OLT:
9381 case FCmpInst::FCMP_OLE:
9382 return {SPF_FMINNUM, NaNBehavior, Ordered};
9383 }
9384}
9385
9386std::optional<std::pair<CmpPredicate, Constant *>>
9389 "Only for relational integer predicates.");
9390 if (isa<UndefValue>(C))
9391 return std::nullopt;
9392
9393 Type *Type = C->getType();
9394 bool IsSigned = ICmpInst::isSigned(Pred);
9395
9397 bool WillIncrement =
9398 UnsignedPred == ICmpInst::ICMP_ULE || UnsignedPred == ICmpInst::ICMP_UGT;
9399
9400 // Check if the constant operand can be safely incremented/decremented
9401 // without overflowing/underflowing.
9402 auto ConstantIsOk = [Pred, WillIncrement, IsSigned](ConstantInt *C) {
9403 if (WillIncrement ? C->isMaxValue(IsSigned) : C->isMinValue(IsSigned))
9404 return false;
9405
9406 if (!Pred.hasSameSign())
9407 return true;
9408
9409 // Crossing the corresponding boundary in the other ordering changes the
9410 // sign bit, and therefore changes the poison domain.
9411 return WillIncrement ? !C->isMaxValue(!IsSigned)
9412 : !C->isMinValue(!IsSigned);
9413 };
9414
9415 Constant *SafeReplacementConstant = nullptr;
9416 if (auto *CI = dyn_cast<ConstantInt>(C)) {
9417 // Bail out if the constant can't be safely incremented/decremented.
9418 if (!ConstantIsOk(CI))
9419 return std::nullopt;
9420 } else if (auto *FVTy = dyn_cast<FixedVectorType>(Type)) {
9421 unsigned NumElts = FVTy->getNumElements();
9422 for (unsigned i = 0; i != NumElts; ++i) {
9423 Constant *Elt = C->getAggregateElement(i);
9424 if (!Elt)
9425 return std::nullopt;
9426
9427 if (isa<UndefValue>(Elt))
9428 continue;
9429
9430 // Bail out if we can't determine if this constant is min/max or if we
9431 // know that this constant is min/max.
9432 auto *CI = dyn_cast<ConstantInt>(Elt);
9433 if (!CI || !ConstantIsOk(CI))
9434 return std::nullopt;
9435
9436 if (!SafeReplacementConstant)
9437 SafeReplacementConstant = CI;
9438 }
9439 } else if (isa<VectorType>(C->getType())) {
9440 // Handle scalable splat
9441 Value *SplatC = C->getSplatValue();
9442 auto *CI = dyn_cast_or_null<ConstantInt>(SplatC);
9443 // Bail out if the constant can't be safely incremented/decremented.
9444 if (!CI || !ConstantIsOk(CI))
9445 return std::nullopt;
9446 } else {
9447 // ConstantExpr?
9448 return std::nullopt;
9449 }
9450
9451 // It may not be safe to change a compare predicate in the presence of
9452 // undefined elements, so replace those elements with the first safe constant
9453 // that we found.
9454 // TODO: in case of poison, it is safe; let's replace undefs only.
9455 if (C->containsUndefOrPoisonElement()) {
9456 assert(SafeReplacementConstant && "Replacement constant not set");
9457 C = Constant::replaceUndefsWith(C, SafeReplacementConstant);
9458 }
9459
9461 Pred.hasSameSign());
9462
9463 // Increment or decrement the constant.
9464 Constant *OneOrNegOne = ConstantInt::get(Type, WillIncrement ? 1 : -1, true);
9465 Constant *NewC = ConstantExpr::getAdd(C, OneOrNegOne);
9466
9467 return std::make_pair(NewPred, NewC);
9468}
9469
9471 FastMathFlags FMF, Value *CmpLHS,
9472 Value *CmpRHS, Value *TrueVal,
9473 Value *FalseVal, Value *&LHS,
9474 Value *&RHS) {
9476 // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one
9477 // 0.0 operand, set the compare's 0.0 operands to that same value for the
9478 // purpose of identifying min/max. Disregard vector constants with undefined
9479 // elements because those can not be back-propagated for analysis.
9480 Value *OutputZeroVal = nullptr;
9481 if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) &&
9482 !cast<Constant>(TrueVal)->containsUndefOrPoisonElement())
9483 OutputZeroVal = TrueVal;
9484 else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) &&
9485 !cast<Constant>(FalseVal)->containsUndefOrPoisonElement())
9486 OutputZeroVal = FalseVal;
9487
9488 if (OutputZeroVal) {
9489 if (match(CmpLHS, m_AnyZeroFP()) && CmpLHS != OutputZeroVal)
9490 CmpLHS = OutputZeroVal;
9491 if (match(CmpRHS, m_AnyZeroFP()) && CmpRHS != OutputZeroVal)
9492 CmpRHS = OutputZeroVal;
9493 }
9494 }
9495
9496 LHS = CmpLHS;
9497 RHS = CmpRHS;
9498
9499 // Signed zero may return inconsistent results between implementations.
9500 // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0
9501 // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)
9502 // Therefore, we behave conservatively and only proceed if at least one of the
9503 // operands is known to not be zero or if we don't care about signed zero.
9505 if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
9506 !isKnownNonZero(CmpRHS))
9507 return {SPF_UNKNOWN, SPNB_NA, false};
9508 }
9509
9510 SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
9511 bool Ordered = false;
9512
9513 // When given one NaN and one non-NaN input:
9514 // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
9515 // - A simple C99 (a < b ? a : b) construction will return 'b' (as the
9516 // ordered comparison fails), which could be NaN or non-NaN.
9517 // so here we discover exactly what NaN behavior is required/accepted.
9519 bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
9520 bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
9521
9522 if (LHSSafe && RHSSafe) {
9523 // Both operands are known non-NaN.
9524 NaNBehavior = SPNB_RETURNS_ANY;
9525 Ordered = CmpInst::isOrdered(Pred);
9526 } else if (CmpInst::isOrdered(Pred)) {
9527 // An ordered comparison will return false when given a NaN, so it
9528 // returns the RHS.
9529 Ordered = true;
9530 if (LHSSafe)
9531 // LHS is non-NaN, so if RHS is NaN then NaN will be returned.
9532 NaNBehavior = SPNB_RETURNS_NAN;
9533 else if (RHSSafe)
9534 NaNBehavior = SPNB_RETURNS_OTHER;
9535 else
9536 // Completely unsafe.
9537 return {SPF_UNKNOWN, SPNB_NA, false};
9538 } else {
9539 Ordered = false;
9540 // An unordered comparison will return true when given a NaN, so it
9541 // returns the LHS.
9542 if (LHSSafe)
9543 // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
9544 NaNBehavior = SPNB_RETURNS_OTHER;
9545 else if (RHSSafe)
9546 NaNBehavior = SPNB_RETURNS_NAN;
9547 else
9548 // Completely unsafe.
9549 return {SPF_UNKNOWN, SPNB_NA, false};
9550 }
9551 }
9552
9553 if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
9554 std::swap(CmpLHS, CmpRHS);
9556 if (NaNBehavior == SPNB_RETURNS_NAN)
9557 NaNBehavior = SPNB_RETURNS_OTHER;
9558 else if (NaNBehavior == SPNB_RETURNS_OTHER)
9559 NaNBehavior = SPNB_RETURNS_NAN;
9560 Ordered = !Ordered;
9561 }
9562
9563 // ([if]cmp X, Y) ? X : Y
9564 if (TrueVal == CmpLHS && FalseVal == CmpRHS)
9565 return getSelectPattern(Pred, NaNBehavior, Ordered);
9566
9567 if (isKnownNegation(TrueVal, FalseVal)) {
9568 // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can
9569 // match against either LHS or sign-preserving operations on LHS, like
9570 // sext(LHS), or binary ops that do not wrap in signed sense.
9571 auto CmpLHSOrSExt =
9572 m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS)));
9573 auto MaybeSExtOrMulCmpLHS =
9574 m_CombineOr(CmpLHSOrSExt, m_NSWMul(CmpLHSOrSExt, m_StrictlyPositive()),
9575 m_NSWShl(CmpLHSOrSExt, m_Value()));
9576 auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes());
9577 auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One());
9578 if (match(TrueVal, MaybeSExtOrMulCmpLHS)) {
9579 // Set the return values. If the compare uses the negated value (-X >s 0),
9580 // swap the return values because the negated value is always 'RHS'.
9581 LHS = TrueVal;
9582 RHS = FalseVal;
9583 if (match(CmpLHS, m_Neg(m_Specific(FalseVal))))
9584 std::swap(LHS, RHS);
9585
9586 // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X)
9587 // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X)
9588 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
9589 return {SPF_ABS, SPNB_NA, false};
9590
9591 // (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X)
9592 if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne))
9593 return {SPF_ABS, SPNB_NA, false};
9594
9595 // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X)
9596 // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X)
9597 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
9598 return {SPF_NABS, SPNB_NA, false};
9599 } else if (match(FalseVal, MaybeSExtOrMulCmpLHS)) {
9600 // Set the return values. If the compare uses the negated value (-X >s 0),
9601 // swap the return values because the negated value is always 'RHS'.
9602 LHS = FalseVal;
9603 RHS = TrueVal;
9604 if (match(CmpLHS, m_Neg(m_Specific(TrueVal))))
9605 std::swap(LHS, RHS);
9606
9607 // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X)
9608 // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X)
9609 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
9610 return {SPF_NABS, SPNB_NA, false};
9611
9612 // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X)
9613 // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X)
9614 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
9615 return {SPF_ABS, SPNB_NA, false};
9616 }
9617 }
9618
9620 return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
9621
9622 // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar
9623 // may return either -0.0 or 0.0, so fcmp/select pair has stricter
9624 // semantics than minNum. Be conservative in such case.
9625 if (NaNBehavior != SPNB_RETURNS_ANY ||
9626 (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
9627 !isKnownNonZero(CmpRHS)))
9628 return {SPF_UNKNOWN, SPNB_NA, false};
9629
9630 return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
9631}
9632
9634 Instruction::CastOps *CastOp) {
9635 const DataLayout &DL = CmpI->getDataLayout();
9636
9637 Constant *CastedTo = nullptr;
9638 switch (*CastOp) {
9639 case Instruction::ZExt:
9640 if (CmpI->isUnsigned())
9641 CastedTo = ConstantExpr::getTrunc(C, SrcTy);
9642 break;
9643 case Instruction::SExt:
9644 if (CmpI->isSigned())
9645 CastedTo = ConstantExpr::getTrunc(C, SrcTy, true);
9646 break;
9647 case Instruction::Trunc:
9648 Constant *CmpConst;
9649 if (match(CmpI->getOperand(1), m_Constant(CmpConst)) &&
9650 CmpConst->getType() == SrcTy) {
9651 // Here we have the following case:
9652 //
9653 // %cond = cmp iN %x, CmpConst
9654 // %tr = trunc iN %x to iK
9655 // %narrowsel = select i1 %cond, iK %t, iK C
9656 //
9657 // We can always move trunc after select operation:
9658 //
9659 // %cond = cmp iN %x, CmpConst
9660 // %widesel = select i1 %cond, iN %x, iN CmpConst
9661 // %tr = trunc iN %widesel to iK
9662 //
9663 // Note that C could be extended in any way because we don't care about
9664 // upper bits after truncation. It can't be abs pattern, because it would
9665 // look like:
9666 //
9667 // select i1 %cond, x, -x.
9668 //
9669 // So only min/max pattern could be matched. Such match requires widened C
9670 // == CmpConst. That is why set widened C = CmpConst, condition trunc
9671 // CmpConst == C is checked below.
9672 CastedTo = CmpConst;
9673 } else {
9674 unsigned ExtOp = CmpI->isSigned() ? Instruction::SExt : Instruction::ZExt;
9675 CastedTo = ConstantFoldCastOperand(ExtOp, C, SrcTy, DL);
9676 }
9677 break;
9678 case Instruction::FPTrunc:
9679 CastedTo = ConstantFoldCastOperand(Instruction::FPExt, C, SrcTy, DL);
9680 break;
9681 case Instruction::FPExt:
9682 CastedTo = ConstantFoldCastOperand(Instruction::FPTrunc, C, SrcTy, DL);
9683 break;
9684 case Instruction::FPToUI:
9685 CastedTo = ConstantFoldCastOperand(Instruction::UIToFP, C, SrcTy, DL);
9686 break;
9687 case Instruction::FPToSI:
9688 CastedTo = ConstantFoldCastOperand(Instruction::SIToFP, C, SrcTy, DL);
9689 break;
9690 case Instruction::UIToFP:
9691 CastedTo = ConstantFoldCastOperand(Instruction::FPToUI, C, SrcTy, DL);
9692 break;
9693 case Instruction::SIToFP:
9694 CastedTo = ConstantFoldCastOperand(Instruction::FPToSI, C, SrcTy, DL);
9695 break;
9696 default:
9697 break;
9698 }
9699
9700 if (!CastedTo)
9701 return nullptr;
9702
9703 // Make sure the cast doesn't lose any information.
9704 Constant *CastedBack =
9705 ConstantFoldCastOperand(*CastOp, CastedTo, C->getType(), DL);
9706 if (CastedBack && CastedBack != C)
9707 return nullptr;
9708
9709 return CastedTo;
9710}
9711
9712/// Helps to match a select pattern in case of a type mismatch.
9713///
9714/// The function processes the case when type of true and false values of a
9715/// select instruction differs from type of the cmp instruction operands because
9716/// of a cast instruction. The function checks if it is legal to move the cast
9717/// operation after "select". If yes, it returns the new second value of
9718/// "select" (with the assumption that cast is moved):
9719/// 1. As operand of cast instruction when both values of "select" are same cast
9720/// instructions.
9721/// 2. As restored constant (by applying reverse cast operation) when the first
9722/// value of the "select" is a cast operation and the second value is a
9723/// constant. It is implemented in lookThroughCastConst().
9724/// 3. As one operand is cast instruction and the other is not. The operands in
9725/// sel(cmp) are in different type integer.
9726/// NOTE: We return only the new second value because the first value could be
9727/// accessed as operand of cast instruction.
9729 Instruction::CastOps *CastOp) {
9730 auto *Cast1 = dyn_cast<CastInst>(V1);
9731 if (!Cast1)
9732 return nullptr;
9733
9734 *CastOp = Cast1->getOpcode();
9735 Type *SrcTy = Cast1->getSrcTy();
9736 if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
9737 // If V1 and V2 are both the same cast from the same type, look through V1.
9738 if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
9739 return Cast2->getOperand(0);
9740 return nullptr;
9741 }
9742
9743 auto *C = dyn_cast<Constant>(V2);
9744 if (C)
9745 return lookThroughCastConst(CmpI, SrcTy, C, CastOp);
9746
9747 Value *CastedTo = nullptr;
9748 if (*CastOp == Instruction::Trunc) {
9749 if (match(CmpI->getOperand(1), m_ZExtOrSExt(m_Specific(V2)))) {
9750 // Here we have the following case:
9751 // %y_ext = sext iK %y to iN
9752 // %cond = cmp iN %x, %y_ext
9753 // %tr = trunc iN %x to iK
9754 // %narrowsel = select i1 %cond, iK %tr, iK %y
9755 //
9756 // We can always move trunc after select operation:
9757 // %y_ext = sext iK %y to iN
9758 // %cond = cmp iN %x, %y_ext
9759 // %widesel = select i1 %cond, iN %x, iN %y_ext
9760 // %tr = trunc iN %widesel to iK
9761 assert(V2->getType() == Cast1->getType() &&
9762 "V2 and Cast1 should be the same type.");
9763 CastedTo = CmpI->getOperand(1);
9764 }
9765 }
9766
9767 return CastedTo;
9768}
9770 Instruction::CastOps *CastOp) {
9772 if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
9773
9774 CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
9775 if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};
9776
9777 Value *TrueVal = SI->getTrueValue();
9778 Value *FalseVal = SI->getFalseValue();
9779
9781 CmpI, TrueVal, FalseVal, LHS, RHS, SI->getFastMathFlagsOrNone(), CastOp);
9782}
9783
9785 CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS,
9786 FastMathFlags FMF, Instruction::CastOps *CastOp) {
9787 CmpInst::Predicate Pred = CmpI->getPredicate();
9788 Value *CmpLHS = CmpI->getOperand(0);
9789 Value *CmpRHS = CmpI->getOperand(1);
9790 if (isa<FPMathOperator>(CmpI) && CmpI->hasNoNaNs())
9791 FMF.setNoNaNs();
9792
9793 // Bail out early.
9794 if (CmpI->isEquality())
9795 return {SPF_UNKNOWN, SPNB_NA, false};
9796
9797 // Deal with type mismatches.
9798 if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
9799 if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) {
9800 // If this is a potential fmin/fmax with a cast to integer, then ignore
9801 // -0.0 because there is no corresponding integer value.
9802 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9803 FMF.setNoSignedZeros();
9804 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9805 cast<CastInst>(TrueVal)->getOperand(0), C,
9806 LHS, RHS);
9807 }
9808 if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) {
9809 // If this is a potential fmin/fmax with a cast to integer, then ignore
9810 // -0.0 because there is no corresponding integer value.
9811 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9812 FMF.setNoSignedZeros();
9813 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, C,
9814 cast<CastInst>(FalseVal)->getOperand(0), LHS,
9815 RHS);
9816 }
9817 }
9818 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS,
9819 RHS);
9820}
9821
9823 if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT;
9824 if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT;
9825 if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT;
9826 if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT;
9827 if (SPF == SPF_FMINNUM)
9828 return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT;
9829 if (SPF == SPF_FMAXNUM)
9830 return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT;
9831 llvm_unreachable("unhandled!");
9832}
9833
9835 switch (SPF) {
9837 return Intrinsic::umin;
9839 return Intrinsic::umax;
9841 return Intrinsic::smin;
9843 return Intrinsic::smax;
9844 default:
9845 llvm_unreachable("Unexpected SPF");
9846 }
9847}
9848
9850 if (SPF == SPF_SMIN) return SPF_SMAX;
9851 if (SPF == SPF_UMIN) return SPF_UMAX;
9852 if (SPF == SPF_SMAX) return SPF_SMIN;
9853 if (SPF == SPF_UMAX) return SPF_UMIN;
9854 llvm_unreachable("unhandled!");
9855}
9856
9858 switch (MinMaxID) {
9859 case Intrinsic::smax: return Intrinsic::smin;
9860 case Intrinsic::smin: return Intrinsic::smax;
9861 case Intrinsic::umax: return Intrinsic::umin;
9862 case Intrinsic::umin: return Intrinsic::umax;
9863 // Please note that next four intrinsics may produce the same result for
9864 // original and inverted case even if X != Y due to NaN is handled specially.
9865 case Intrinsic::maximum: return Intrinsic::minimum;
9866 case Intrinsic::minimum: return Intrinsic::maximum;
9867 case Intrinsic::maxnum: return Intrinsic::minnum;
9868 case Intrinsic::minnum: return Intrinsic::maxnum;
9869 case Intrinsic::maximumnum:
9870 return Intrinsic::minimumnum;
9871 case Intrinsic::minimumnum:
9872 return Intrinsic::maximumnum;
9873 default: llvm_unreachable("Unexpected intrinsic");
9874 }
9875}
9876
9878 switch (SPF) {
9881 case SPF_UMAX: return APInt::getMaxValue(BitWidth);
9882 case SPF_UMIN: return APInt::getMinValue(BitWidth);
9883 default: llvm_unreachable("Unexpected flavor");
9884 }
9885}
9886
9887std::pair<Intrinsic::ID, bool>
9889 // Check if VL contains select instructions that can be folded into a min/max
9890 // vector intrinsic and return the intrinsic if it is possible.
9891 // TODO: Support floating point min/max.
9892 bool AllCmpSingleUse = true;
9893 SelectPatternResult SelectPattern;
9894 SelectPattern.Flavor = SPF_UNKNOWN;
9895 if (all_of(VL, [&SelectPattern, &AllCmpSingleUse](Value *I) {
9896 Value *LHS, *RHS;
9897 auto CurrentPattern = matchSelectPattern(I, LHS, RHS);
9898 if (!SelectPatternResult::isMinOrMax(CurrentPattern.Flavor))
9899 return false;
9900 if (SelectPattern.Flavor != SPF_UNKNOWN &&
9901 SelectPattern.Flavor != CurrentPattern.Flavor)
9902 return false;
9903 SelectPattern = CurrentPattern;
9904 AllCmpSingleUse &=
9906 return true;
9907 })) {
9908 switch (SelectPattern.Flavor) {
9909 case SPF_SMIN:
9910 return {Intrinsic::smin, AllCmpSingleUse};
9911 case SPF_UMIN:
9912 return {Intrinsic::umin, AllCmpSingleUse};
9913 case SPF_SMAX:
9914 return {Intrinsic::smax, AllCmpSingleUse};
9915 case SPF_UMAX:
9916 return {Intrinsic::umax, AllCmpSingleUse};
9917 case SPF_FMAXNUM:
9918 return {Intrinsic::maxnum, AllCmpSingleUse};
9919 case SPF_FMINNUM:
9920 return {Intrinsic::minnum, AllCmpSingleUse};
9921 default:
9922 llvm_unreachable("unexpected select pattern flavor");
9923 }
9924 }
9925 return {Intrinsic::not_intrinsic, false};
9926}
9927
9928template <typename InstTy>
9929static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst,
9930 Value *&Init, Value *&OtherOp) {
9931 // Handle the case of a simple two-predecessor recurrence PHI.
9932 // There's a lot more that could theoretically be done here, but
9933 // this is sufficient to catch some interesting cases.
9934 // TODO: Expand list -- gep, uadd.sat etc.
9935 if (PN->getNumIncomingValues() != 2)
9936 return false;
9937
9938 for (unsigned I = 0; I != 2; ++I) {
9939 if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
9940 Operation && Operation->getNumOperands() >= 2) {
9941 Value *LHS = Operation->getOperand(0);
9942 Value *RHS = Operation->getOperand(1);
9943 if (LHS != PN && RHS != PN)
9944 continue;
9945
9946 Inst = Operation;
9947 Init = PN->getIncomingValue(!I);
9948 OtherOp = (LHS == PN) ? RHS : LHS;
9949 return true;
9950 }
9951 }
9952 return false;
9953}
9954
9955template <typename InstTy>
9956static bool matchThreeInputRecurrence(const PHINode *PN, InstTy *&Inst,
9957 Value *&Init, Value *&OtherOp0,
9958 Value *&OtherOp1) {
9959 if (PN->getNumIncomingValues() != 2)
9960 return false;
9961
9962 for (unsigned I = 0; I != 2; ++I) {
9963 if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
9964 Operation && Operation->getNumOperands() >= 3) {
9965 Value *Op0 = Operation->getOperand(0);
9966 Value *Op1 = Operation->getOperand(1);
9967 Value *Op2 = Operation->getOperand(2);
9968
9969 if (Op0 != PN && Op1 != PN && Op2 != PN)
9970 continue;
9971
9972 Inst = Operation;
9973 Init = PN->getIncomingValue(!I);
9974 if (Op0 == PN) {
9975 OtherOp0 = Op1;
9976 OtherOp1 = Op2;
9977 } else if (Op1 == PN) {
9978 OtherOp0 = Op0;
9979 OtherOp1 = Op2;
9980 } else {
9981 OtherOp0 = Op0;
9982 OtherOp1 = Op1;
9983 }
9984 return true;
9985 }
9986 }
9987 return false;
9988}
9990 Value *&Start, Value *&Step) {
9991 // We try to match a recurrence of the form:
9992 // %iv = [Start, %entry], [%iv.next, %backedge]
9993 // %iv.next = binop %iv, Step
9994 // Or:
9995 // %iv = [Start, %entry], [%iv.next, %backedge]
9996 // %iv.next = binop Step, %iv
9997 return matchTwoInputRecurrence(P, BO, Start, Step);
9998}
9999
10001 Value *&Start, Value *&Step) {
10002 BinaryOperator *BO = nullptr;
10003 return match(I, m_c_BinOp(m_Phi(P), m_Value())) &&
10004 matchSimpleRecurrence(P, BO, Start, Step) && BO == I;
10005}
10006
10008 PHINode *&P, Value *&Init,
10009 Value *&OtherOp) {
10010 // Binary intrinsics only supported for now.
10011 if (I->arg_size() != 2 || I->getType() != I->getArgOperand(0)->getType() ||
10012 I->getType() != I->getArgOperand(1)->getType())
10013 return false;
10014
10015 IntrinsicInst *II = nullptr;
10016 P = dyn_cast<PHINode>(I->getArgOperand(0));
10017 if (!P)
10018 P = dyn_cast<PHINode>(I->getArgOperand(1));
10019
10020 return P && matchTwoInputRecurrence(P, II, Init, OtherOp) && II == I;
10021}
10022
10024 PHINode *&P, Value *&Init,
10025 Value *&OtherOp0,
10026 Value *&OtherOp1) {
10027 if (I->arg_size() != 3 || I->getType() != I->getArgOperand(0)->getType() ||
10028 I->getType() != I->getArgOperand(1)->getType() ||
10029 I->getType() != I->getArgOperand(2)->getType())
10030 return false;
10031 IntrinsicInst *II = nullptr;
10032 P = dyn_cast<PHINode>(I->getArgOperand(0));
10033 if (!P) {
10034 P = dyn_cast<PHINode>(I->getArgOperand(1));
10035 if (!P)
10036 P = dyn_cast<PHINode>(I->getArgOperand(2));
10037 }
10038 return P && matchThreeInputRecurrence(P, II, Init, OtherOp0, OtherOp1) &&
10039 II == I;
10040}
10041
10042/// Return true if "icmp Pred LHS RHS" is always true.
10044 const Value *RHS) {
10046 return true;
10047
10048 switch (Pred) {
10049 default:
10050 return false;
10051
10052 case CmpInst::ICMP_SLE: {
10053 const APInt *C;
10054
10055 // LHS s<= LHS +_{nsw} C if C >= 0
10056 // LHS s<= LHS | C if C >= 0
10057 if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C))) ||
10059 return !C->isNegative();
10060
10061 // LHS s<= smax(LHS, V) for any V
10063 return true;
10064
10065 // smin(RHS, V) s<= RHS for any V
10067 return true;
10068
10069 // Match A to (X +_{nsw} CA) and B to (X +_{nsw} CB)
10070 const Value *X;
10071 const APInt *CLHS, *CRHS;
10072 if (match(LHS, m_NSWAddLike(m_Value(X), m_APInt(CLHS))) &&
10074 return CLHS->sle(*CRHS);
10075
10076 return false;
10077 }
10078
10079 case CmpInst::ICMP_ULE: {
10080 // LHS u<= LHS +_{nuw} V for any V
10081 if (match(RHS, m_c_Add(m_Specific(LHS), m_Value())) &&
10083 return true;
10084
10085 // LHS u<= LHS | V for any V
10086 if (match(RHS, m_c_Or(m_Specific(LHS), m_Value())))
10087 return true;
10088
10089 // LHS u<= umax(LHS, V) for any V
10091 return true;
10092
10093 // RHS >> V u<= RHS for any V
10094 if (match(LHS, m_LShr(m_Specific(RHS), m_Value())))
10095 return true;
10096
10097 // RHS u/ C_ugt_1 u<= RHS
10098 const APInt *C;
10099 if (match(LHS, m_UDiv(m_Specific(RHS), m_APInt(C))) && C->ugt(1))
10100 return true;
10101
10102 // RHS & V u<= RHS for any V
10104 return true;
10105
10106 // umin(RHS, V) u<= RHS for any V
10108 return true;
10109
10110 // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
10111 const Value *X;
10112 const APInt *CLHS, *CRHS;
10113 if (match(LHS, m_NUWAddLike(m_Value(X), m_APInt(CLHS))) &&
10115 return CLHS->ule(*CRHS);
10116
10117 return false;
10118 }
10119 }
10120}
10121
10122/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
10123/// ALHS ARHS" is true. Otherwise, return std::nullopt.
10124static std::optional<bool>
10126 const Value *ARHS, const Value *BLHS, const Value *BRHS) {
10127 switch (Pred) {
10128 default:
10129 return std::nullopt;
10130
10131 case CmpInst::ICMP_SLT:
10132 case CmpInst::ICMP_SLE:
10133 if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS) &&
10135 return true;
10136 return std::nullopt;
10137
10138 case CmpInst::ICMP_SGT:
10139 case CmpInst::ICMP_SGE:
10140 if (isTruePredicate(CmpInst::ICMP_SLE, ALHS, BLHS) &&
10142 return true;
10143 return std::nullopt;
10144
10145 case CmpInst::ICMP_ULT:
10146 case CmpInst::ICMP_ULE:
10147 if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS) &&
10149 return true;
10150 return std::nullopt;
10151
10152 case CmpInst::ICMP_UGT:
10153 case CmpInst::ICMP_UGE:
10154 if (isTruePredicate(CmpInst::ICMP_ULE, ALHS, BLHS) &&
10156 return true;
10157 return std::nullopt;
10158 }
10159}
10160
10161/// Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
10162/// Return false if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is false.
10163/// Otherwise, return std::nullopt if we can't infer anything.
10164static std::optional<bool>
10166 CmpPredicate RPred, const ConstantRange &RCR) {
10167 auto CRImpliesPred = [&](ConstantRange CR,
10168 CmpInst::Predicate Pred) -> std::optional<bool> {
10169 // If all true values for lhs and true for rhs, lhs implies rhs
10170 if (CR.icmp(Pred, RCR))
10171 return true;
10172
10173 // If there is no overlap, lhs implies not rhs
10175 return false;
10176
10177 return std::nullopt;
10178 };
10179 if (auto Res = CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
10180 RPred))
10181 return Res;
10182 if (LPred.hasSameSign() ^ RPred.hasSameSign()) {
10184 : LPred.dropSameSign();
10186 : RPred.dropSameSign();
10187 return CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
10188 RPred);
10189 }
10190 return std::nullopt;
10191}
10192
10193/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
10194/// is true. Return false if LHS implies RHS is false. Otherwise, return
10195/// std::nullopt if we can't infer anything.
10196static std::optional<bool>
10197isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1,
10198 CmpPredicate RPred, const Value *R0, const Value *R1,
10199 const DataLayout &DL, bool LHSIsTrue) {
10200 // The rest of the logic assumes the LHS condition is true. If that's not the
10201 // case, invert the predicate to make it so.
10202 if (!LHSIsTrue)
10203 LPred = ICmpInst::getInverseCmpPredicate(LPred);
10204
10205 // We can have non-canonical operands, so try to normalize any common operand
10206 // to L0/R0.
10207 if (L0 == R1) {
10208 std::swap(R0, R1);
10209 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
10210 }
10211 if (R0 == L1) {
10212 std::swap(L0, L1);
10213 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
10214 }
10215 if (L1 == R1) {
10216 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
10217 if (L0 != R0 || match(L0, m_ImmConstant())) {
10218 std::swap(L0, L1);
10219 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
10220 std::swap(R0, R1);
10221 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
10222 }
10223 }
10224
10225 // See if we can infer anything if operand-0 matches and we have at least one
10226 // constant.
10227 const APInt *Unused;
10228 if (L0 == R0 && (match(L1, m_APInt(Unused)) || match(R1, m_APInt(Unused)))) {
10229 // Potential TODO: We could also further use the constant range of L0/R0 to
10230 // further constraint the constant ranges. At the moment this leads to
10231 // several regressions related to not transforming `multi_use(A + C0) eq/ne
10232 // C1` (see discussion: D58633).
10233 SimplifyQuery SQ(DL);
10238
10239 // Even if L1/R1 are not both constant, we can still sometimes deduce
10240 // relationship from a single constant. For example X u> Y implies X != 0.
10241 if (auto R = isImpliedCondCommonOperandWithCR(LPred, LCR, RPred, RCR))
10242 return R;
10243 // If both L1/R1 were exact constant ranges and we didn't get anything
10244 // here, we won't be able to deduce this.
10245 if (match(L1, m_APInt(Unused)) && match(R1, m_APInt(Unused)))
10246 return std::nullopt;
10247 }
10248
10249 // Can we infer anything when the two compares have matching operands?
10250 if (L0 == R0 && L1 == R1)
10251 return ICmpInst::isImpliedByMatchingCmp(LPred, RPred);
10252
10253 // It only really makes sense in the context of signed comparison for "X - Y
10254 // must be positive if X >= Y and no overflow".
10255 // Take SGT as an example: L0:x > L1:y and C >= 0
10256 // ==> R0:(x -nsw y) < R1:(-C) is false
10257 CmpInst::Predicate SignedLPred = LPred.getPreferredSignedPredicate();
10258 if ((SignedLPred == ICmpInst::ICMP_SGT ||
10259 SignedLPred == ICmpInst::ICMP_SGE) &&
10260 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
10261 if (match(R1, m_NonPositive()) &&
10262 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == false)
10263 return false;
10264 }
10265
10266 // Take SLT as an example: L0:x < L1:y and C <= 0
10267 // ==> R0:(x -nsw y) < R1:(-C) is true
10268 if ((SignedLPred == ICmpInst::ICMP_SLT ||
10269 SignedLPred == ICmpInst::ICMP_SLE) &&
10270 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
10271 if (match(R1, m_NonNegative()) &&
10272 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == true)
10273 return true;
10274 }
10275
10276 // (a - b) pred C -> a != b, if 0 pred C is false
10277 // (ptrtoint(a) - ptrtoint(b)) pred C -> a != b, if 0 pred C is false
10278 Value *A, *B;
10279 if (ICmpInst::isEquality(RPred) && cmpExcludesZero(LPred, L1) &&
10280 match(L0, m_Sub(m_Value(A), m_Value(B))) &&
10281 ((A == R0 && B == R1) || (A == R1 && B == R0) ||
10286 return RPred.dropSameSign() == ICmpInst::ICMP_NE;
10287 }
10288
10289 // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1
10290 if (L0 == R0 &&
10291 (LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) &&
10292 (RPred == ICmpInst::ICMP_ULT || RPred == ICmpInst::ICMP_UGE) &&
10293 match(L0, m_c_Add(m_Specific(L1), m_Specific(R1))))
10294 return CmpPredicate::getMatching(LPred, RPred).has_value();
10295
10296 if (auto P = CmpPredicate::getMatching(LPred, RPred))
10297 return isImpliedCondOperands(*P, L0, L1, R0, R1);
10298
10299 // L0 u< C sets limits to L0's bits which may imply (L0 & Mask) pred RC
10300 // Example: L0 u< 13 => (L0 & 16) == 0
10301 const APInt *LC, *RC, *MaskC;
10302 if (match(L1, m_APInt(LC)) && match(R1, m_APInt(RC)) &&
10303 match(R0, m_And(m_Specific(L0), m_APInt(MaskC)))) {
10305 ConstantRange MaskedCRange = LCRange.binaryAnd(*MaskC);
10306 if (MaskedCRange.icmp(RPred, ConstantRange(*RC)))
10307 return true;
10308 if (MaskedCRange.icmp(ICmpInst::getInversePredicate(RPred),
10309 ConstantRange(*RC)))
10310 return false;
10311 }
10312
10313 return std::nullopt;
10314}
10315
10316/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
10317/// is true. Return false if LHS implies RHS is false. Otherwise, return
10318/// std::nullopt if we can't infer anything.
10319static std::optional<bool>
10321 FCmpInst::Predicate RPred, const Value *R0, const Value *R1,
10322 const DataLayout &DL, bool LHSIsTrue) {
10323 // The rest of the logic assumes the LHS condition is true. If that's not the
10324 // case, invert the predicate to make it so.
10325 if (!LHSIsTrue)
10326 LPred = FCmpInst::getInversePredicate(LPred);
10327
10328 // We can have non-canonical operands, so try to normalize any common operand
10329 // to L0/R0.
10330 if (L0 == R1) {
10331 std::swap(R0, R1);
10332 RPred = FCmpInst::getSwappedPredicate(RPred);
10333 }
10334 if (R0 == L1) {
10335 std::swap(L0, L1);
10336 LPred = FCmpInst::getSwappedPredicate(LPred);
10337 }
10338 if (L1 == R1) {
10339 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
10340 if (L0 != R0 || match(L0, m_ImmConstant())) {
10341 std::swap(L0, L1);
10342 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
10343 std::swap(R0, R1);
10344 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
10345 }
10346 }
10347
10348 // Can we infer anything when the two compares have matching operands?
10349 if (L0 == R0 && L1 == R1) {
10350 if ((LPred & RPred) == LPred)
10351 return true;
10352 if ((LPred & ~RPred) == LPred)
10353 return false;
10354 }
10355
10356 // See if we can infer anything if operand-0 matches and we have at least one
10357 // constant.
10358 const APFloat *L1C, *R1C;
10359 if (L0 == R0 && match(L1, m_APFloat(L1C)) && match(R1, m_APFloat(R1C))) {
10360 if (std::optional<ConstantFPRange> DomCR =
10362 if (std::optional<ConstantFPRange> ImpliedCR =
10364 if (ImpliedCR->contains(*DomCR))
10365 return true;
10366 }
10367 if (std::optional<ConstantFPRange> ImpliedCR =
10369 FCmpInst::getInversePredicate(RPred), *R1C)) {
10370 if (ImpliedCR->contains(*DomCR))
10371 return false;
10372 }
10373 }
10374 }
10375
10376 return std::nullopt;
10377}
10378
10379/// Return true if LHS implies RHS is true. Return false if LHS implies RHS is
10380/// false. Otherwise, return std::nullopt if we can't infer anything. We
10381/// expect the RHS to be an icmp and the LHS to be an 'and', 'or', or a 'select'
10382/// instruction.
10383static std::optional<bool>
10385 const Value *RHSOp0, const Value *RHSOp1,
10386 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
10387 // The LHS must be an 'or', 'and', or a 'select' instruction.
10388 assert((LHS->getOpcode() == Instruction::And ||
10389 LHS->getOpcode() == Instruction::Or ||
10390 LHS->getOpcode() == Instruction::Select) &&
10391 "Expected LHS to be 'and', 'or', or 'select'.");
10392
10393 assert(Depth <= MaxAnalysisRecursionDepth && "Hit recursion limit");
10394
10395 // If the result of an 'or' is false, then we know both legs of the 'or' are
10396 // false. Similarly, if the result of an 'and' is true, then we know both
10397 // legs of the 'and' are true.
10398 const Value *ALHS, *ARHS;
10399 if ((!LHSIsTrue && match(LHS, m_LogicalOr(m_Value(ALHS), m_Value(ARHS)))) ||
10400 (LHSIsTrue && match(LHS, m_LogicalAnd(m_Value(ALHS), m_Value(ARHS))))) {
10401 // FIXME: Make this non-recursion.
10402 if (std::optional<bool> Implication = isImpliedCondition(
10403 ALHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
10404 return Implication;
10405 if (std::optional<bool> Implication = isImpliedCondition(
10406 ARHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
10407 return Implication;
10408 return std::nullopt;
10409 }
10410 return std::nullopt;
10411}
10412
10413std::optional<bool>
10415 const Value *RHSOp0, const Value *RHSOp1,
10416 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
10417 // Bail out when we hit the limit.
10419 return std::nullopt;
10420
10421 // A mismatch occurs when we compare a scalar cmp to a vector cmp, for
10422 // example.
10423 if (RHSOp0->getType()->isVectorTy() != LHS->getType()->isVectorTy())
10424 return std::nullopt;
10425
10426 assert(LHS->getType()->isIntOrIntVectorTy(1) &&
10427 "Expected integer type only!");
10428
10429 // Match not
10430 if (match(LHS, m_Not(m_Value(LHS))))
10431 LHSIsTrue = !LHSIsTrue;
10432
10433 // Both LHS and RHS are icmps.
10434 if (RHSOp0->getType()->getScalarType()->isIntOrPtrTy()) {
10435 CmpPredicate LHSPred;
10436 Value *LHSOp0, *LHSOp1;
10437 if (match(LHS, m_ICmpLike(LHSPred, m_Value(LHSOp0), m_Value(LHSOp1))))
10438 return isImpliedCondICmps(LHSPred, LHSOp0, LHSOp1, RHSPred, RHSOp0,
10439 RHSOp1, DL, LHSIsTrue);
10440 } else {
10441 assert(RHSOp0->getType()->isFPOrFPVectorTy() &&
10442 "Expected floating point type only!");
10443 if (const auto *LHSCmp = dyn_cast<FCmpInst>(LHS))
10444 return isImpliedCondFCmps(LHSCmp->getPredicate(), LHSCmp->getOperand(0),
10445 LHSCmp->getOperand(1), RHSPred, RHSOp0, RHSOp1,
10446 DL, LHSIsTrue);
10447 }
10448
10449 /// The LHS should be an 'or', 'and', or a 'select' instruction. We expect
10450 /// the RHS to be an icmp.
10451 /// FIXME: Add support for and/or/select on the RHS.
10452 if (const Instruction *LHSI = dyn_cast<Instruction>(LHS)) {
10453 if ((LHSI->getOpcode() == Instruction::And ||
10454 LHSI->getOpcode() == Instruction::Or ||
10455 LHSI->getOpcode() == Instruction::Select))
10456 return isImpliedCondAndOr(LHSI, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue,
10457 Depth);
10458 }
10459 return std::nullopt;
10460}
10461
10462std::optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS,
10463 const DataLayout &DL,
10464 bool LHSIsTrue, unsigned Depth) {
10465 // LHS ==> RHS by definition
10466 if (LHS == RHS)
10467 return LHSIsTrue;
10468
10469 // Match not
10470 bool InvertRHS = false;
10471 if (match(RHS, m_Not(m_Value(RHS)))) {
10472 if (LHS == RHS)
10473 return !LHSIsTrue;
10474 InvertRHS = true;
10475 }
10476
10477 CmpPredicate RHSPred;
10478 Value *RHSOp0, *RHSOp1;
10479 if (match(RHS, m_ICmpLike(RHSPred, m_Value(RHSOp0), m_Value(RHSOp1)))) {
10480 if (auto Implied = isImpliedCondition(LHS, RHSPred, RHSOp0, RHSOp1, DL,
10481 LHSIsTrue, Depth))
10482 return InvertRHS ? !*Implied : *Implied;
10483 return std::nullopt;
10484 }
10485 if (const FCmpInst *RHSCmp = dyn_cast<FCmpInst>(RHS)) {
10486 if (auto Implied = isImpliedCondition(
10487 LHS, RHSCmp->getPredicate(), RHSCmp->getOperand(0),
10488 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))
10489 return InvertRHS ? !*Implied : *Implied;
10490 return std::nullopt;
10491 }
10492
10494 return std::nullopt;
10495
10496 // LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS2
10497 // LHS ==> !(RHS1 && RHS2) if LHS ==> !RHS1 or LHS ==> !RHS2
10498 const Value *RHS1, *RHS2;
10499 if (match(RHS, m_LogicalOr(m_Value(RHS1), m_Value(RHS2)))) {
10500 if (std::optional<bool> Imp =
10501 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
10502 if (*Imp == true)
10503 return !InvertRHS;
10504 if (std::optional<bool> Imp =
10505 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
10506 if (*Imp == true)
10507 return !InvertRHS;
10508 }
10509 if (match(RHS, m_LogicalAnd(m_Value(RHS1), m_Value(RHS2)))) {
10510 if (std::optional<bool> Imp =
10511 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
10512 if (*Imp == false)
10513 return InvertRHS;
10514 if (std::optional<bool> Imp =
10515 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
10516 if (*Imp == false)
10517 return InvertRHS;
10518 }
10519
10520 return std::nullopt;
10521}
10522
10523// Returns a pair (Condition, ConditionIsTrue), where Condition is a branch
10524// condition dominating ContextI or nullptr, if no condition is found.
10525static std::pair<Value *, bool>
10527 if (!ContextI || !ContextI->getParent())
10528 return {nullptr, false};
10529
10530 // TODO: This is a poor/cheap way to determine dominance. Should we use a
10531 // dominator tree (eg, from a SimplifyQuery) instead?
10532 const BasicBlock *ContextBB = ContextI->getParent();
10533 const BasicBlock *PredBB = ContextBB->getSinglePredecessor();
10534 if (!PredBB)
10535 return {nullptr, false};
10536
10537 // We need a conditional branch in the predecessor.
10538 Value *PredCond;
10539 BasicBlock *TrueBB, *FalseBB;
10540 if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))
10541 return {nullptr, false};
10542
10543 // The branch should get simplified. Don't bother simplifying this condition.
10544 if (TrueBB == FalseBB)
10545 return {nullptr, false};
10546
10547 assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
10548 "Predecessor block does not point to successor?");
10549
10550 // Is this condition implied by the predecessor condition?
10551 return {PredCond, TrueBB == ContextBB};
10552}
10553
10554std::optional<bool> llvm::isImpliedByDomCondition(const Value *Cond,
10555 const Instruction *ContextI,
10556 const DataLayout &DL) {
10557 assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool");
10558 auto PredCond = getDomPredecessorCondition(ContextI);
10559 if (PredCond.first)
10560 return isImpliedCondition(PredCond.first, Cond, DL, PredCond.second);
10561 return std::nullopt;
10562}
10563
10565 const Value *LHS,
10566 const Value *RHS,
10567 const Instruction *ContextI,
10568 const DataLayout &DL) {
10569 auto PredCond = getDomPredecessorCondition(ContextI);
10570 if (PredCond.first)
10571 return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL,
10572 PredCond.second);
10573 return std::nullopt;
10574}
10575
10577 APInt &Upper, const InstrInfoQuery &IIQ,
10578 bool PreferSignedRange) {
10579 unsigned Width = Lower.getBitWidth();
10580 const APInt *C;
10581 switch (BO.getOpcode()) {
10582 case Instruction::Sub:
10583 if (match(BO.getOperand(0), m_APInt(C))) {
10584 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
10585 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
10586
10587 // If the caller expects a signed compare, then try to use a signed range.
10588 // Otherwise if both no-wraps are set, use the unsigned range because it
10589 // is never larger than the signed range. Example:
10590 // "sub nuw nsw i8 -2, x" is unsigned [0, 254] vs. signed [-128, 126].
10591 // "sub nuw nsw i8 2, x" is unsigned [0, 2] vs. signed [-125, 127].
10592 if (PreferSignedRange && HasNSW && HasNUW)
10593 HasNUW = false;
10594
10595 if (HasNUW) {
10596 // 'sub nuw c, x' produces [0, C].
10597 Upper = *C + 1;
10598 } else if (HasNSW) {
10599 if (C->isNegative()) {
10600 // 'sub nsw -C, x' produces [SINT_MIN, -C - SINT_MIN].
10602 Upper = *C - APInt::getSignedMaxValue(Width);
10603 } else {
10604 // Note that sub 0, INT_MIN is not NSW. It techically is a signed wrap
10605 // 'sub nsw C, x' produces [C - SINT_MAX, SINT_MAX].
10606 Lower = *C - APInt::getSignedMaxValue(Width);
10608 }
10609 }
10610 }
10611 break;
10612 case Instruction::Add:
10613 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
10614 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
10615 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
10616
10617 // If the caller expects a signed compare, then try to use a signed
10618 // range. Otherwise if both no-wraps are set, use the unsigned range
10619 // because it is never larger than the signed range. Example: "add nuw
10620 // nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125].
10621 if (PreferSignedRange && HasNSW && HasNUW)
10622 HasNUW = false;
10623
10624 if (HasNUW) {
10625 // 'add nuw x, C' produces [C, UINT_MAX].
10626 Lower = *C;
10627 } else if (HasNSW) {
10628 if (C->isNegative()) {
10629 // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C].
10631 Upper = APInt::getSignedMaxValue(Width) + *C + 1;
10632 } else {
10633 // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX].
10634 Lower = APInt::getSignedMinValue(Width) + *C;
10635 Upper = APInt::getSignedMaxValue(Width) + 1;
10636 }
10637 }
10638 }
10639 break;
10640
10641 case Instruction::And:
10642 if (match(BO.getOperand(1), m_APInt(C)))
10643 // 'and x, C' produces [0, C].
10644 Upper = *C + 1;
10645 // X & -X is a power of two or zero. So we can cap the value at max power of
10646 // two.
10647 if (match(BO.getOperand(0), m_Neg(m_Specific(BO.getOperand(1)))) ||
10648 match(BO.getOperand(1), m_Neg(m_Specific(BO.getOperand(0)))))
10649 Upper = APInt::getSignedMinValue(Width) + 1;
10650 break;
10651
10652 case Instruction::Or:
10653 if (match(BO.getOperand(1), m_APInt(C)))
10654 // 'or x, C' produces [C, UINT_MAX].
10655 Lower = *C;
10656 break;
10657
10658 case Instruction::AShr:
10659 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10660 // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C].
10662 Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1;
10663 } else if (match(BO.getOperand(0), m_APInt(C))) {
10664 unsigned ShiftAmount = Width - 1;
10665 if (!C->isZero() && IIQ.isExact(&BO))
10666 ShiftAmount = C->countr_zero();
10667 if (C->isNegative()) {
10668 // 'ashr C, x' produces [C, C >> (Width-1)]
10669 Lower = *C;
10670 Upper = C->ashr(ShiftAmount) + 1;
10671 } else {
10672 // 'ashr C, x' produces [C >> (Width-1), C]
10673 Lower = C->ashr(ShiftAmount);
10674 Upper = *C + 1;
10675 }
10676 }
10677 break;
10678
10679 case Instruction::LShr:
10680 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10681 // 'lshr x, C' produces [0, UINT_MAX >> C].
10682 Upper = APInt::getAllOnes(Width).lshr(*C) + 1;
10683 } else if (match(BO.getOperand(0), m_APInt(C))) {
10684 // 'lshr C, x' produces [C >> (Width-1), C].
10685 unsigned ShiftAmount = Width - 1;
10686 if (!C->isZero() && IIQ.isExact(&BO))
10687 ShiftAmount = C->countr_zero();
10688 Lower = C->lshr(ShiftAmount);
10689 Upper = *C + 1;
10690 }
10691 break;
10692
10693 case Instruction::Shl:
10694 if (match(BO.getOperand(0), m_APInt(C))) {
10695 if (IIQ.hasNoUnsignedWrap(&BO)) {
10696 // 'shl nuw C, x' produces [C, C << CLZ(C)]
10697 Lower = *C;
10698 Upper = Lower.shl(Lower.countl_zero()) + 1;
10699 } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw?
10700 if (C->isNegative()) {
10701 // 'shl nsw C, x' produces [C << CLO(C)-1, C]
10702 unsigned ShiftAmount = C->countl_one() - 1;
10703 Lower = C->shl(ShiftAmount);
10704 Upper = *C + 1;
10705 } else {
10706 // 'shl nsw C, x' produces [C, C << CLZ(C)-1]
10707 unsigned ShiftAmount = C->countl_zero() - 1;
10708 Lower = *C;
10709 Upper = C->shl(ShiftAmount) + 1;
10710 }
10711 } else {
10712 // If lowbit is set, value can never be zero.
10713 if ((*C)[0])
10714 Lower = APInt::getOneBitSet(Width, 0);
10715 // If we are shifting a constant the largest it can be is if the longest
10716 // sequence of consecutive ones is shifted to the highbits (breaking
10717 // ties for which sequence is higher). At the moment we take a liberal
10718 // upper bound on this by just popcounting the constant.
10719 // TODO: There may be a bitwise trick for it longest/highest
10720 // consecutative sequence of ones (naive method is O(Width) loop).
10721 Upper = APInt::getHighBitsSet(Width, C->popcount()) + 1;
10722 }
10723 } else if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10724 Upper = APInt::getBitsSetFrom(Width, C->getZExtValue()) + 1;
10725 }
10726 break;
10727
10728 case Instruction::SDiv:
10729 if (match(BO.getOperand(1), m_APInt(C))) {
10730 APInt IntMin = APInt::getSignedMinValue(Width);
10731 APInt IntMax = APInt::getSignedMaxValue(Width);
10732 if (C->isAllOnes()) {
10733 // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX]
10734 // where C != -1 and C != 0 and C != 1
10735 Lower = IntMin + 1;
10736 Upper = IntMax + 1;
10737 } else if (C->countl_zero() < Width - 1) {
10738 // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C]
10739 // where C != -1 and C != 0 and C != 1
10740 Lower = IntMin.sdiv(*C);
10741 Upper = IntMax.sdiv(*C);
10742 if (Lower.sgt(Upper))
10744 Upper = Upper + 1;
10745 assert(Upper != Lower && "Upper part of range has wrapped!");
10746 }
10747 } else if (match(BO.getOperand(0), m_APInt(C))) {
10748 if (C->isMinSignedValue()) {
10749 // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2].
10750 Lower = *C;
10751 Upper = Lower.lshr(1) + 1;
10752 } else {
10753 // 'sdiv C, x' produces [-|C|, |C|].
10754 Upper = C->abs() + 1;
10755 Lower = (-Upper) + 1;
10756 }
10757 }
10758 break;
10759
10760 case Instruction::UDiv:
10761 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
10762 // 'udiv x, C' produces [0, UINT_MAX / C].
10763 Upper = APInt::getMaxValue(Width).udiv(*C) + 1;
10764 } else if (match(BO.getOperand(0), m_APInt(C))) {
10765 // 'udiv C, x' produces [0, C].
10766 Upper = *C + 1;
10767 }
10768 break;
10769
10770 case Instruction::SRem:
10771 if (match(BO.getOperand(1), m_APInt(C))) {
10772 // 'srem x, C' produces (-|C|, |C|).
10773 Upper = C->abs();
10774 Lower = (-Upper) + 1;
10775 } else if (match(BO.getOperand(0), m_APInt(C))) {
10776 if (C->isNegative()) {
10777 // 'srem -|C|, x' produces [-|C|, 0].
10778 Upper = 1;
10779 Lower = *C;
10780 } else {
10781 // 'srem |C|, x' produces [0, |C|].
10782 Upper = *C + 1;
10783 }
10784 }
10785 break;
10786
10787 case Instruction::URem:
10788 if (match(BO.getOperand(1), m_APInt(C)))
10789 // 'urem x, C' produces [0, C).
10790 Upper = *C;
10791 else if (match(BO.getOperand(0), m_APInt(C)))
10792 // 'urem C, x' produces [0, C].
10793 Upper = *C + 1;
10794 break;
10795
10796 default:
10797 break;
10798 }
10799}
10800
10802 bool UseInstrInfo) {
10803 unsigned Width = II.getType()->getScalarSizeInBits();
10804 const APInt *C;
10805 switch (II.getIntrinsicID()) {
10806 case Intrinsic::ctlz:
10807 case Intrinsic::cttz: {
10808 APInt Upper(Width, Width);
10809 if (!UseInstrInfo || !match(II.getArgOperand(1), m_One()))
10810 Upper += 1;
10811 // Maximum of set/clear bits is the bit width.
10813 }
10814 case Intrinsic::ctpop:
10815 // Maximum of set/clear bits is the bit width.
10817 APInt(Width, Width) + 1);
10818 case Intrinsic::uadd_sat:
10819 // uadd.sat(x, C) produces [C, UINT_MAX].
10820 if (match(II.getOperand(0), m_APInt(C)) ||
10821 match(II.getOperand(1), m_APInt(C)))
10823 break;
10824 case Intrinsic::sadd_sat:
10825 if (match(II.getOperand(0), m_APInt(C)) ||
10826 match(II.getOperand(1), m_APInt(C))) {
10827 if (C->isNegative())
10828 // sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)].
10830 APInt::getSignedMaxValue(Width) + *C +
10831 1);
10832
10833 // sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].
10835 APInt::getSignedMaxValue(Width) + 1);
10836 }
10837 break;
10838 case Intrinsic::usub_sat:
10839 // usub.sat(C, x) produces [0, C].
10840 if (match(II.getOperand(0), m_APInt(C)))
10841 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
10842
10843 // usub.sat(x, C) produces [0, UINT_MAX - C].
10844 if (match(II.getOperand(1), m_APInt(C)))
10846 APInt::getMaxValue(Width) - *C + 1);
10847 break;
10848 case Intrinsic::ssub_sat:
10849 if (match(II.getOperand(0), m_APInt(C))) {
10850 if (C->isNegative())
10851 // ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)].
10853 *C - APInt::getSignedMinValue(Width) +
10854 1);
10855
10856 // ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].
10858 APInt::getSignedMaxValue(Width) + 1);
10859 } else if (match(II.getOperand(1), m_APInt(C))) {
10860 if (C->isNegative())
10861 // ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]:
10863 APInt::getSignedMaxValue(Width) + 1);
10864
10865 // ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].
10867 APInt::getSignedMaxValue(Width) - *C +
10868 1);
10869 }
10870 break;
10871 case Intrinsic::ushl_sat:
10872 if (match(II.getOperand(0), m_APInt(C)))
10874 break;
10875 case Intrinsic::sshl_sat:
10876 if (match(II.getOperand(0), m_APInt(C))) {
10877 if (C->isNegative())
10879 *C + 1);
10881 APInt::getSignedMaxValue(Width) + 1);
10882 }
10883 break;
10884 case Intrinsic::umin:
10885 case Intrinsic::umax:
10886 case Intrinsic::smin:
10887 case Intrinsic::smax:
10888 if (!match(II.getOperand(0), m_APInt(C)) &&
10889 !match(II.getOperand(1), m_APInt(C)))
10890 break;
10891
10892 switch (II.getIntrinsicID()) {
10893 case Intrinsic::umin:
10894 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
10895 case Intrinsic::umax:
10897 case Intrinsic::smin:
10899 *C + 1);
10900 case Intrinsic::smax:
10902 APInt::getSignedMaxValue(Width) + 1);
10903 default:
10904 llvm_unreachable("Must be min/max intrinsic");
10905 }
10906 break;
10907 case Intrinsic::abs:
10908 // If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX],
10909 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10910 if (match(II.getOperand(1), m_One()))
10912 APInt::getSignedMaxValue(Width) + 1);
10913
10915 APInt::getSignedMinValue(Width) + 1);
10916 case Intrinsic::vscale:
10917 if (!II.getParent() || !II.getFunction())
10918 break;
10919 return getVScaleRange(II.getFunction(), Width);
10920 case Intrinsic::read_register:
10921 case Intrinsic::read_volatile_register: {
10922 const Module *M = II.getModule();
10923 if (!M || !M->getTargetTriple().isRISCV())
10924 break;
10925 if (II.getFunction() && isReadVLENB(II))
10926 return getRISCVVLENBRange(II, Width);
10927 break;
10928 }
10929 default:
10930 break;
10931 }
10932
10933 return ConstantRange::getFull(Width);
10934}
10935
10937 const InstrInfoQuery &IIQ) {
10938 unsigned BitWidth = SI.getType()->getScalarSizeInBits();
10939 const Value *LHS = nullptr, *RHS = nullptr;
10941 if (R.Flavor == SPF_UNKNOWN)
10942 return ConstantRange::getFull(BitWidth);
10943
10944 if (R.Flavor == SelectPatternFlavor::SPF_ABS) {
10945 // If the negation part of the abs (in RHS) has the NSW flag,
10946 // then the result of abs(X) is [0..SIGNED_MAX],
10947 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10948 if (match(RHS, m_Neg(m_Specific(LHS))) &&
10952
10955 }
10956
10957 if (R.Flavor == SelectPatternFlavor::SPF_NABS) {
10958 // The result of -abs(X) is <= 0.
10960 APInt(BitWidth, 1));
10961 }
10962
10963 const APInt *C;
10964 if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C)))
10965 return ConstantRange::getFull(BitWidth);
10966
10967 switch (R.Flavor) {
10968 case SPF_UMIN:
10970 case SPF_UMAX:
10972 case SPF_SMIN:
10974 *C + 1);
10975 case SPF_SMAX:
10978 default:
10979 return ConstantRange::getFull(BitWidth);
10980 }
10981}
10982
10984 // The maximum representable value of a half is 65504. For floats the maximum
10985 // value is 3.4e38 which requires roughly 129 bits.
10986 unsigned BitWidth = I->getType()->getScalarSizeInBits();
10987 if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy())
10988 return;
10989 if (isa<FPToSIInst>(I) && BitWidth >= 17) {
10990 Lower = APInt(BitWidth, -65504, true);
10991 Upper = APInt(BitWidth, 65505);
10992 }
10993
10994 if (isa<FPToUIInst>(I) && BitWidth >= 16) {
10995 // For a fptoui the lower limit is left as 0.
10996 Upper = APInt(BitWidth, 65505);
10997 }
10998}
10999
11001 const SimplifyQuery &SQ,
11002 unsigned Depth) {
11003 assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction");
11004
11006 return ConstantRange::getFull(V->getType()->getScalarSizeInBits());
11007
11008 if (auto *C = dyn_cast<Constant>(V))
11009 return C->toConstantRange();
11010
11011 unsigned BitWidth = V->getType()->getScalarSizeInBits();
11012 ConstantRange CR = ConstantRange::getFull(BitWidth);
11013 if (auto *BO = dyn_cast<BinaryOperator>(V)) {
11014 APInt Lower = APInt(BitWidth, 0);
11015 APInt Upper = APInt(BitWidth, 0);
11016 // TODO: Return ConstantRange.
11017 setLimitsForBinOp(*BO, Lower, Upper, SQ.IIQ, ForSigned);
11019 } else if (auto *II = dyn_cast<IntrinsicInst>(V))
11021 else if (auto *SI = dyn_cast<SelectInst>(V)) {
11022 ConstantRange CRTrue =
11023 computeConstantRange(SI->getTrueValue(), ForSigned, SQ, Depth + 1);
11024 ConstantRange CRFalse =
11025 computeConstantRange(SI->getFalseValue(), ForSigned, SQ, Depth + 1);
11026 CR = CRTrue.unionWith(CRFalse);
11028 } else if (auto *TI = dyn_cast<TruncInst>(V)) {
11029 ConstantRange SrcCR =
11030 computeConstantRange(TI->getOperand(0), ForSigned, SQ, Depth + 1);
11031 CR = SrcCR.truncate(BitWidth);
11032 } else if (auto *ZExt = dyn_cast<ZExtInst>(V)) {
11033 ConstantRange SrcCR =
11034 computeConstantRange(ZExt->getOperand(0), ForSigned, SQ, Depth + 1);
11035 CR = SrcCR.zeroExtend(BitWidth);
11036 } else if (auto *SExt = dyn_cast<SExtInst>(V)) {
11037 ConstantRange SrcCR =
11038 computeConstantRange(SExt->getOperand(0), ForSigned, SQ, Depth + 1);
11039 CR = SrcCR.signExtend(BitWidth);
11040 } else if (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {
11041 APInt Lower = APInt(BitWidth, 0);
11042 APInt Upper = APInt(BitWidth, 0);
11043 // TODO: Return ConstantRange.
11046 } else if (const auto *A = dyn_cast<Argument>(V))
11047 if (std::optional<ConstantRange> Range = A->getRange())
11048 CR = *Range;
11049
11050 if (auto *I = dyn_cast<Instruction>(V)) {
11051 if (auto *Range = SQ.IIQ.getMetadata(I, LLVMContext::MD_range))
11053
11054 Value *FrexpSrc;
11055 if (const auto *CB = dyn_cast<CallBase>(V)) {
11056 if (std::optional<ConstantRange> Range = CB->getRange())
11057 CR = CR.intersectWith(*Range);
11059 m_Value(FrexpSrc))))) {
11060 const fltSemantics &FltSem =
11061 FrexpSrc->getType()->getScalarType()->getFltSemantics();
11062 // It should be possible to implement this for any type, but this logic
11063 // only computes the range assuming standard subnormal handling.
11064 if (APFloat::isIEEELikeFP(FltSem)) {
11066 FrexpSrc, fcSubnormal | fcZero | fcNan | fcInf, SQ, Depth + 1);
11067
11068 // The exponent of frexp(NaN) and frexp(Inf) is unspecified. Only
11069 // constrain its range when the source can be neither.
11070 if (KnownSrc.isKnownNeverInfOrNaN()) {
11071 int MinExp = APFloat::semanticsMinExponent(FltSem) + 1;
11072
11073 // Offset to find the true minimum exponent value for a denormal.
11074 if (!KnownSrc.isKnownNeverSubnormal())
11075 MinExp -= (APFloat::semanticsPrecision(FltSem) - 1);
11076
11077 int MaxExp = APFloat::semanticsMaxExponent(FltSem) + 1;
11078
11079 auto [AdjustedMin, AdjustedMax, AdjustedMaxNonZero] =
11081
11082 DenormalMode Mode = I->getFunction()->getDenormalMode(FltSem);
11083 bool NeverLogicalZero = KnownSrc.isKnownNeverLogicalZero(Mode);
11084
11085 MinExp = std::max(AdjustedMin, MinExp);
11086 MaxExp = std::min(NeverLogicalZero ? AdjustedMaxNonZero : AdjustedMax,
11087 MaxExp);
11088
11090 APInt(BitWidth, static_cast<int64_t>(MinExp), /*isSigned=*/true),
11091 APInt(BitWidth, static_cast<int64_t>(MaxExp) + 1,
11092 /*isSigned=*/true));
11093 }
11094 }
11095 }
11096 }
11097
11098 if (SQ.CtxI && SQ.AC) {
11099 // Try to restrict the range based on information from assumptions.
11100 for (auto &AssumeVH : SQ.AC->assumptionsFor(V)) {
11101 if (!AssumeVH)
11102 continue;
11103 CallInst *I = cast<CallInst>(AssumeVH);
11104 assert(I->getParent()->getParent() == SQ.CtxI->getParent()->getParent() &&
11105 "Got assumption for the wrong function!");
11106 assert(I->getIntrinsicID() == Intrinsic::assume &&
11107 "must be an assume intrinsic");
11108
11109 if (AssumeVH.Index != AssumptionCache::ExprResultIdx) {
11110 auto OBU = I->getOperandBundleAt(AssumeVH.Index);
11111 if (getBundleAttrFromOBU(OBU) == BundleAttr::Range) {
11112 auto [Val, Range] = getAssumeRangeInfo(OBU);
11113 if (Val == V && isValidAssumeForContext(I, SQ))
11114 CR = CR.intersectWith(Range);
11115 }
11116 continue;
11117 }
11118
11119 if (!isValidAssumeForContext(I, SQ))
11120 continue;
11121 Value *Arg = I->getArgOperand(0);
11122 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
11123 // Currently we just use information from comparisons.
11124 if (!Cmp || Cmp->getOperand(0) != V)
11125 continue;
11126 // TODO: Set "ForSigned" parameter via Cmp->isSigned()?
11127 ConstantRange RHS =
11128 computeConstantRange(Cmp->getOperand(1), /*ForSigned=*/false,
11129 SQ.getWithInstruction(I), Depth + 1);
11130 CR = CR.intersectWith(
11131 ConstantRange::makeAllowedICmpRegion(Cmp->getCmpPredicate(), RHS));
11132 }
11133 }
11134
11135 return CR;
11136}
11137
11138static void
11140 function_ref<void(Value *)> InsertAffected) {
11141 assert(V != nullptr);
11142 if (isa<Argument>(V) || isa<GlobalValue>(V)) {
11143 InsertAffected(V);
11144 } else if (auto *I = dyn_cast<Instruction>(V)) {
11145 InsertAffected(V);
11146
11147 // Peek through unary operators to find the source of the condition.
11148 Value *Op;
11150 m_Trunc(m_Value(Op))))) {
11152 InsertAffected(Op);
11153 }
11154 }
11155}
11156
11158 Value *Cond, bool IsAssume, function_ref<void(Value *)> InsertAffected) {
11159 auto AddAffected = [&InsertAffected](Value *V) {
11160 addValueAffectedByCondition(V, InsertAffected);
11161 };
11162
11163 auto AddCmpOperands = [&AddAffected, IsAssume](Value *LHS, Value *RHS) {
11164 if (IsAssume) {
11165 AddAffected(LHS);
11166 AddAffected(RHS);
11167 } else if (match(RHS, m_Constant()))
11168 AddAffected(LHS);
11169 };
11170
11171 SmallVector<Value *, 8> Worklist;
11173 Worklist.push_back(Cond);
11174 while (!Worklist.empty()) {
11175 Value *V = Worklist.pop_back_val();
11176 if (!Visited.insert(V).second)
11177 continue;
11178
11179 CmpPredicate Pred;
11180 Value *A, *B, *X;
11181
11182 if (IsAssume) {
11183 AddAffected(V);
11184 if (match(V, m_Not(m_Value(X))))
11185 AddAffected(X);
11186 }
11187
11188 if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
11189 // assume(A && B) is split to -> assume(A); assume(B);
11190 // assume(!(A || B)) is split to -> assume(!A); assume(!B);
11191 // Finally, assume(A || B) / assume(!(A && B)) generally don't provide
11192 // enough information to be worth handling (intersection of information as
11193 // opposed to union).
11194 if (!IsAssume) {
11195 Worklist.push_back(A);
11196 Worklist.push_back(B);
11197 }
11198 } else if (match(V, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
11199 bool HasRHSC = match(B, m_ConstantInt());
11200 if (ICmpInst::isEquality(Pred)) {
11201 AddAffected(A);
11202 if (IsAssume)
11203 AddAffected(B);
11204 if (HasRHSC) {
11205 Value *Y;
11206 // (X << C) or (X >>_s C) or (X >>_u C).
11207 if (match(A, m_Shift(m_Value(X), m_ConstantInt())))
11208 AddAffected(X);
11209 // (X & C) or (X | C).
11210 else if (match(A, m_And(m_Value(X), m_Value(Y))) ||
11211 match(A, m_Or(m_Value(X), m_Value(Y)))) {
11212 AddAffected(X);
11213 AddAffected(Y);
11214 }
11215 // X - Y
11216 else if (match(A, m_Sub(m_Value(X), m_Value(Y)))) {
11217 AddAffected(X);
11218 AddAffected(Y);
11219 }
11220 }
11221 } else {
11222 AddCmpOperands(A, B);
11223 if (HasRHSC) {
11224 Value *Y;
11225 // Handle (A + C1) u< C2, which is the canonical form of
11226 // A > C3 && A < C4.
11228 AddAffected(X);
11229 // X - Y pred C, which implies X != Y if 0 pred C is false.
11230 else if (match(A, m_Sub(m_Value(X), m_Value(Y)))) {
11231 AddAffected(X);
11232 AddAffected(Y);
11233 }
11234
11235 if (ICmpInst::isUnsigned(Pred)) {
11236 // X & Y u> C -> X >u C && Y >u C
11237 // X | Y u< C -> X u< C && Y u< C
11238 // X nuw+ Y u< C -> X u< C && Y u< C
11239 if (match(A, m_And(m_Value(X), m_Value(Y))) ||
11240 match(A, m_Or(m_Value(X), m_Value(Y))) ||
11241 match(A, m_NUWAdd(m_Value(X), m_Value(Y)))) {
11242 AddAffected(X);
11243 AddAffected(Y);
11244 }
11245 }
11246 }
11247
11248 // Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported
11249 // by computeKnownFPClass().
11251 if (Pred == ICmpInst::ICMP_SLT && match(B, m_Zero()))
11252 InsertAffected(X);
11253 else if (Pred == ICmpInst::ICMP_SGT && match(B, m_AllOnes()))
11254 InsertAffected(X);
11255 }
11256 }
11257
11258 auto AddNuwSquareOperand = [&AddAffected](Value *Op) {
11259 Value *SquareOp = nullptr;
11260 if (match(Op, m_NUWMul(m_Value(SquareOp), m_Deferred(SquareOp))))
11261 AddAffected(SquareOp);
11262 };
11263 AddNuwSquareOperand(A);
11264 AddNuwSquareOperand(B);
11265
11266 if (HasRHSC && match(A, m_Ctpop(m_Value(X))))
11267 AddAffected(X);
11268 } else if (match(V, m_FCmp(Pred, m_Value(A), m_Value(B)))) {
11269 AddCmpOperands(A, B);
11270
11271 // fcmp fneg(x), y
11272 // fcmp fabs(x), y
11273 // fcmp fneg(fabs(x)), y
11274 // TODO: Confirm whether m_FNegOrFSubNegZero is correct.
11276 AddAffected(A);
11277 if (match(A, m_FAbs(m_Value(A))))
11278 AddAffected(A);
11279
11281 m_Value()))) {
11282 // Handle patterns that computeKnownFPClass() support.
11283 AddAffected(A);
11284 } else if (!IsAssume && match(V, m_Trunc(m_Value(X)))) {
11285 // Assume is checked here as X is already added above for assumes in
11286 // addValueAffectedByCondition
11287 AddAffected(X);
11288 } else if (!IsAssume && match(V, m_Not(m_Value(X)))) {
11289 // Assume is checked here to avoid issues with ephemeral values
11290 Worklist.push_back(X);
11291 }
11292 }
11293}
11294
11296 // (X >> C) or/add (X & mask(C) != 0)
11297 if (const auto *BO = dyn_cast<BinaryOperator>(V)) {
11298 if (BO->getOpcode() == Instruction::Add ||
11299 BO->getOpcode() == Instruction::Or) {
11300 const Value *X;
11301 const APInt *C1, *C2;
11302 if (match(BO, m_c_BinOp(m_LShr(m_Value(X), m_APInt(C1)),
11306 m_Zero())))) &&
11307 C2->popcount() == C1->getZExtValue())
11308 return X;
11309 }
11310 }
11311
11312 // select(cond, X, -X)
11313 {
11314 const Value *X;
11315 if (match(V, m_c_Select(m_Value(X), m_Neg(m_Deferred(X)))))
11316 return X;
11317 }
11318
11319 return nullptr;
11320}
11321
11323 return const_cast<Value *>(stripNullTest(const_cast<const Value *>(V)));
11324}
11325
11328 unsigned MaxCount, bool AllowUndefOrPoison) {
11331 auto Push = [&](const Value *V) -> bool {
11332 Constant *C;
11333 if (match(const_cast<Value *>(V), m_ImmConstant(C))) {
11334 if (!AllowUndefOrPoison && !isGuaranteedNotToBeUndefOrPoison(C))
11335 return false;
11336 // Check existence first to avoid unnecessary allocations.
11337 if (Constants.contains(C))
11338 return true;
11339 if (Constants.size() == MaxCount)
11340 return false;
11341 Constants.insert(C);
11342 return true;
11343 }
11344
11345 if (auto *Inst = dyn_cast<Instruction>(V)) {
11346 if (Visited.insert(Inst).second)
11347 Worklist.push_back(Inst);
11348 return true;
11349 }
11350 return false;
11351 };
11352 if (!Push(V))
11353 return false;
11354 while (!Worklist.empty()) {
11355 const Instruction *CurInst = Worklist.pop_back_val();
11356 switch (CurInst->getOpcode()) {
11357 case Instruction::Select:
11358 if (!Push(CurInst->getOperand(1)))
11359 return false;
11360 if (!Push(CurInst->getOperand(2)))
11361 return false;
11362 break;
11363 case Instruction::PHI:
11364 for (Value *IncomingValue : cast<PHINode>(CurInst)->incoming_values()) {
11365 // Fast path for recurrence PHI.
11366 if (IncomingValue == CurInst)
11367 continue;
11368 if (!Push(IncomingValue))
11369 return false;
11370 }
11371 break;
11372 default:
11373 return false;
11374 }
11375 }
11376 return true;
11377}
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)
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 canonicalize(const KnownFPClass &Src, DenormalMode Mode)
Apply the canonicalize intrinsic to this value.
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:1081
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1437
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:597
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:679
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:2224
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.
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...
KnownBits byteSwap() const
Definition KnownBits.h:559
bool hasConflict() const
Returns true if there is conflicting information.
Definition KnownBits.h:51
static LLVM_ABI KnownBits fshl(const KnownBits &LHS, const KnownBits &RHS, const APInt &Amt)
Compute known bits for fshl(LHS, RHS, Amt).
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition KnownBits.h:303
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition KnownBits.h:84
KnownBits reverseBits() const
Definition KnownBits.h:563
unsigned getBitWidth() const
Get the bit width of this value.
Definition KnownBits.h:44
static LLVM_ABI KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition KnownBits.h:176
bool isConstant() const
Returns true if we know the value of all bits.
Definition KnownBits.h:54
static KnownBits add(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false, bool SelfAdd=false)
Compute knownbits resulting from addition of LHS and RHS.
Definition KnownBits.h:361
KnownBits unionWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for either this or RHS or both.
Definition KnownBits.h:335
static LLVM_ABI KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition KnownBits.h:109
bool isEven() const
Return if the value is known even (the low bit is 0).
Definition KnownBits.h:162
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition KnownBits.h:239
static LLVM_ABI KnownBits pdep(const KnownBits &Val, const KnownBits &Mask)
Compute known bits for pdep(Val, Mask).
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition KnownBits.h:325
unsigned countMinTrailingOnes() const
Returns the minimum number of trailing one bits.
Definition KnownBits.h:259
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition KnownBits.h:262
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:146
static LLVM_ABI KnownBits fshr(const KnownBits &LHS, const KnownBits &RHS, const APInt &Amt)
Compute known bits for fshr(LHS, RHS, Amt).
static LLVM_ABI KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
static LLVM_ABI KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
static LLVM_ABI KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
static LLVM_ABI KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
APInt getMinValue() const
Return the minimal unsigned value possible given these KnownBits.
Definition KnownBits.h:130
static LLVM_ABI KnownBits computeForAddSub(bool Add, bool NSW, bool NUW, const KnownBits &LHS, const KnownBits &RHS)
Compute known bits resulting from adding LHS and RHS.
Definition KnownBits.cpp:61
static LLVM_ABI KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition KnownBits.h:340
bool isNegative() const
Returns true if this value is known to be negative.
Definition KnownBits.h:103
static KnownBits sub(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from subtraction of LHS and RHS.
Definition KnownBits.h:376
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition KnownBits.h:294
void setAllOnes()
Make all bits known to be one and discard any previous information.
Definition KnownBits.h:90
static LLVM_ABI KnownBits uadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.uadd.sat(LHS, RHS)
static LLVM_ABI KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
KnownBits anyext(unsigned BitWidth) const
Return known bits for an "any" extension of the value we're tracking, where we don't know anything ab...
Definition KnownBits.h:171
static LLVM_ABI KnownBits clmul(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for clmul(LHS, RHS).
LLVM_ABI KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
static LLVM_ABI std::optional< bool > sgt(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SGT result.
static LLVM_ABI std::optional< bool > uge(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_UGE result.
static LLVM_ABI KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
static LLVM_ABI KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
static LLVM_ABI KnownBits pext(const KnownBits &Val, const KnownBits &Mask)
Compute known bits for pext(Val, Mask).
KnownBits sextOrTrunc(unsigned BitWidth) const
Return known bits for a sign extension or truncation of the value we're tracking.
Definition KnownBits.h:210
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