LLVM 24.0.0git
ValueTracking.cpp
Go to the documentation of this file.
1//===- ValueTracking.cpp - Walk computations to compute properties --------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains routines that help analyze properties that chains of
10// computations have.
11//
12//===----------------------------------------------------------------------===//
13
15#include "llvm/ADT/APInt.h"
16#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/ScopeExit.h"
22#include "llvm/ADT/StringRef.h"
32#include "llvm/Analysis/Loads.h"
37#include "llvm/IR/Argument.h"
38#include "llvm/IR/Attributes.h"
39#include "llvm/IR/BasicBlock.h"
41#include "llvm/IR/Constant.h"
44#include "llvm/IR/Constants.h"
47#include "llvm/IR/Dominators.h"
49#include "llvm/IR/Function.h"
51#include "llvm/IR/GlobalAlias.h"
52#include "llvm/IR/GlobalValue.h"
54#include "llvm/IR/InstrTypes.h"
55#include "llvm/IR/Instruction.h"
58#include "llvm/IR/Intrinsics.h"
59#include "llvm/IR/IntrinsicsAArch64.h"
60#include "llvm/IR/IntrinsicsAMDGPU.h"
61#include "llvm/IR/IntrinsicsRISCV.h"
62#include "llvm/IR/IntrinsicsX86.h"
63#include "llvm/IR/LLVMContext.h"
64#include "llvm/IR/Metadata.h"
65#include "llvm/IR/Module.h"
66#include "llvm/IR/Operator.h"
68#include "llvm/IR/Type.h"
69#include "llvm/IR/User.h"
70#include "llvm/IR/Value.h"
80#include <algorithm>
81#include <cassert>
82#include <cstdint>
83#include <optional>
84#include <utility>
85
86using namespace llvm;
87using namespace llvm::PatternMatch;
88
89// Controls the number of uses of the value searched for possible
90// dominating comparisons.
91static cl::opt<unsigned> DomConditionsMaxUses("dom-conditions-max-uses",
92 cl::Hidden, cl::init(20));
93
94/// Maximum number of instructions to check between assume and context
95/// instruction.
96static constexpr unsigned MaxInstrsToCheckForFree = 32;
97
98template <typename InstTy>
99static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst,
100 Value *&Init, Value *&OtherOp);
101
102/// Returns the bitwidth of the given scalar or pointer type. For vector types,
103/// returns the element type's bitwidth.
104static unsigned getBitWidth(Type *Ty, const DataLayout &DL) {
105 if (unsigned BitWidth = Ty->getScalarSizeInBits())
106 return BitWidth;
107
108 return DL.getPointerTypeSizeInBits(Ty);
109}
110
111// Given the provided Value and, potentially, a context instruction, return
112// the preferred context instruction (if any).
113static const Instruction *safeCtxI(const Value *V, const Instruction *CtxI) {
114 // If we've been provided with a context instruction, then use that (provided
115 // it has been inserted).
116 if (CtxI && CtxI->getParent())
117 return CtxI;
118
119 // If the value is really an already-inserted instruction, then use that.
120 CtxI = dyn_cast<Instruction>(V);
121 if (CtxI && CtxI->getParent())
122 return CtxI;
123
124 return nullptr;
125}
126
128 const APInt &DemandedElts,
129 APInt &DemandedLHS, APInt &DemandedRHS) {
130 if (isa<ScalableVectorType>(Shuf->getType())) {
131 assert(DemandedElts == APInt(1,1));
132 DemandedLHS = DemandedRHS = DemandedElts;
133 return true;
134 }
135
136 int NumElts =
137 cast<FixedVectorType>(Shuf->getOperand(0)->getType())->getNumElements();
138 return llvm::getShuffleDemandedElts(NumElts, Shuf->getShuffleMask(),
139 DemandedElts, DemandedLHS, DemandedRHS);
140}
141
142static void computeKnownBits(const Value *V, const APInt &DemandedElts,
143 KnownBits &Known, const SimplifyQuery &Q,
144 unsigned Depth);
145
147 const SimplifyQuery &Q, unsigned Depth) {
148 // Since the number of lanes in a scalable vector is unknown at compile time,
149 // we track one bit which is implicitly broadcast to all lanes. This means
150 // that all lanes in a scalable vector are considered demanded.
151 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
152 APInt DemandedElts =
153 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
154 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);
155}
156
158 const DataLayout &DL, AssumptionCache *AC,
159 const Instruction *CtxI, const DominatorTree *DT,
160 bool UseInstrInfo, unsigned Depth) {
162 SimplifyQuery(DL, DT, AC, safeCtxI(V, CtxI), UseInstrInfo),
163 Depth);
164}
165
167 AssumptionCache *AC, const Instruction *CtxI,
168 const DominatorTree *DT, bool UseInstrInfo,
169 unsigned Depth) {
170 return computeKnownBits(
171 V, SimplifyQuery(DL, DT, AC, safeCtxI(V, CtxI), UseInstrInfo), Depth);
172}
173
176 const SimplifyQuery &SQ) {
177 // Look for an inverted mask: (X & ~M) op (Y & M).
178 {
179 Value *M;
180 if (match(LHS, m_c_And(m_Not(m_Value(M)), m_Value())) &&
182 return isGuaranteedNotToBeUndef(M, SQ.AC, SQ.CtxI, SQ.DT)
185 }
186
187 // X op (Y & ~X)
189 return isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CtxI, SQ.DT)
192
193 // X op ((X & Y) ^ Y) -- this is the canonical form of the previous pattern
194 // for constant Y.
195 Value *Y;
196 if (match(RHS,
198 bool IsNoUndef = isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CtxI, SQ.DT) &&
199 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CtxI, SQ.DT);
200 return IsNoUndef ? NoCommonBitsSetResult::Known
202 }
203
204 // Peek through extends to find a 'not' of the other side:
205 // (ext Y) op ext(~Y)
206 if (match(LHS, m_ZExtOrSExt(m_Value(Y))) &&
208 return isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CtxI, SQ.DT)
211
212 // Look for: (A & B) op ~(A | B)
213 {
214 Value *A, *B;
215 if (match(LHS, m_And(m_Value(A), m_Value(B))) &&
217 bool IsNoUndef = isGuaranteedNotToBeUndef(A, SQ.AC, SQ.CtxI, SQ.DT) &&
218 isGuaranteedNotToBeUndef(B, SQ.AC, SQ.CtxI, SQ.DT);
219 return IsNoUndef ? NoCommonBitsSetResult::Known
221 }
222 }
223
224 // Look for: (X << V) op (Y >> (BitWidth - V))
225 // or (X >> V) op (Y << (BitWidth - V))
226 {
227 const Value *V;
228 const APInt *R;
229 if (((match(RHS, m_Shl(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&
231 (match(RHS, m_LShr(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&
232 match(LHS, m_Shl(m_Value(), m_Specific(V))))) &&
233 R->uge(LHS->getType()->getScalarSizeInBits()))
235 }
236
238}
239
242 const WithCache<const Value *> &RHSCache,
243 const SimplifyQuery &SQ) {
244 const Value *LHS = LHSCache.getValue();
245 const Value *RHS = RHSCache.getValue();
246
247 assert(LHS->getType() == RHS->getType() &&
248 "LHS and RHS should have the same type");
249 assert(LHS->getType()->isIntOrIntVectorTy() &&
250 "LHS and RHS should be integers");
251
253 if (Result == NoCommonBitsSetResult::Known)
255
256 NoCommonBitsSetResult CommuteResult =
258 if (CommuteResult == NoCommonBitsSetResult::Known)
260
262 RHSCache.getKnownBits(SQ)))
264
268
270}
271
273 const WithCache<const Value *> &RHSCache,
274 const SimplifyQuery &SQ) {
275 NoCommonBitsSetResult Result =
276 getNoCommonBitsSetResult(LHSCache, RHSCache, SQ);
277 return Result == NoCommonBitsSetResult::Known;
278}
279
281 return !I->user_empty() &&
282 all_of(I->users(), match_fn(m_ICmp(m_Value(), m_Zero())));
283}
284
286 return !I->user_empty() && all_of(I->users(), [](const User *U) {
287 CmpPredicate P;
288 return match(U, m_ICmp(P, m_Value(), m_Zero())) && ICmpInst::isEquality(P);
289 });
290}
291
293 bool OrZero, AssumptionCache *AC,
294 const Instruction *CtxI,
295 const DominatorTree *DT, bool UseInstrInfo,
296 unsigned Depth) {
297 return ::isKnownToBeAPowerOfTwo(
298 V, OrZero, SimplifyQuery(DL, DT, AC, safeCtxI(V, CtxI), UseInstrInfo),
299 Depth);
300}
301
302static bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
303 const SimplifyQuery &Q, unsigned Depth);
304
306 unsigned Depth) {
307 return computeKnownBits(V, SQ, Depth).isNonNegative();
308}
309
311 const SimplifyQuery &SQ, unsigned Depth) {
312 if (auto *CI = dyn_cast<ConstantInt>(V.getValue()))
313 return CI->getValue().isStrictlyPositive();
314
315 // If `isKnownNonNegative` ever becomes more sophisticated, make sure to keep
316 // this updated.
317 KnownBits Known = Depth == 0 ? V.getKnownBits(SQ)
318 : computeKnownBits(V.getValue(), SQ, Depth);
319 return Known.isNonNegative() &&
320 (Known.isNonZero() || isKnownNonZero(V.getValue(), SQ, Depth));
321}
322
324 const SimplifyQuery &SQ, unsigned Depth) {
325 if (Depth == 0)
326 return V.getKnownBits(SQ).isNegative();
327 return computeKnownBits(V.getValue(), SQ, Depth).isNegative();
328}
329
330static bool isKnownNonEqual(const Value *V1, const Value *V2,
331 const APInt &DemandedElts, const SimplifyQuery &Q,
332 unsigned Depth);
333
335 const Value *RHS);
336
337bool llvm::isKnownNonEqual(const Value *V1, const Value *V2,
338 const SimplifyQuery &Q, unsigned Depth) {
339 // We don't support looking through casts.
340 if (V1 == V2 || V1->getType() != V2->getType())
341 return false;
342 auto *FVTy = dyn_cast<FixedVectorType>(V1->getType());
343 APInt DemandedElts =
344 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
345 return ::isKnownNonEqual(V1, V2, DemandedElts, Q, Depth);
346}
347
348bool llvm::MaskedValueIsZero(const Value *V, const APInt &Mask,
349 const SimplifyQuery &SQ, unsigned Depth) {
350 KnownBits Known(Mask.getBitWidth());
352 return Mask.isSubsetOf(Known.Zero);
353}
354
355static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
356 const SimplifyQuery &Q, unsigned Depth);
357
358static unsigned ComputeNumSignBits(const Value *V, const SimplifyQuery &Q,
359 unsigned Depth = 0) {
360 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
361 APInt DemandedElts =
362 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
363 return ComputeNumSignBits(V, DemandedElts, Q, Depth);
364}
365
366unsigned llvm::ComputeNumSignBits(const Value *V, const DataLayout &DL,
367 AssumptionCache *AC, const Instruction *CtxI,
368 const DominatorTree *DT, bool UseInstrInfo,
369 unsigned Depth) {
370 return ::ComputeNumSignBits(
371 V, SimplifyQuery(DL, DT, AC, safeCtxI(V, CtxI), UseInstrInfo), Depth);
372}
373
375 AssumptionCache *AC,
376 const Instruction *CtxI,
377 const DominatorTree *DT,
378 unsigned Depth) {
379 unsigned SignBits = ComputeNumSignBits(V, DL, AC, CtxI, DT, Depth);
380 return V->getType()->getScalarSizeInBits() - SignBits + 1;
381}
382
383/// Try to detect the lerp pattern: a * (b - c) + c * d
384/// where a >= 0, b >= 0, c >= 0, d >= 0, and b >= c.
385///
386/// In that particular case, we can use the following chain of reasoning:
387///
388/// a * (b - c) + c * d <= a' * (b - c) + a' * c = a' * b where a' = max(a, d)
389///
390/// Since that is true for arbitrary a, b, c and d within our constraints, we
391/// can conclude that:
392///
393/// max(a * (b - c) + c * d) <= max(max(a), max(d)) * max(b) = U
394///
395/// Considering that any result of the lerp would be less or equal to U, it
396/// would have at least the number of leading 0s as in U.
397///
398/// While being quite a specific situation, it is fairly common in computer
399/// graphics in the shape of alpha blending.
400///
401/// Modifies given KnownOut in-place with the inferred information.
402static void computeKnownBitsFromLerpPattern(const Value *Op0, const Value *Op1,
403 const APInt &DemandedElts,
404 KnownBits &KnownOut,
405 const SimplifyQuery &Q,
406 unsigned Depth) {
407
408 Type *Ty = Op0->getType();
409 const unsigned BitWidth = Ty->getScalarSizeInBits();
410
411 // Only handle scalar types for now
412 if (Ty->isVectorTy())
413 return;
414
415 // Try to match: a * (b - c) + c * d.
416 // When a == 1 => A == nullptr, the same applies to d/D as well.
417 const Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
418 const Instruction *SubBC = nullptr;
419
420 const auto MatchSubBC = [&]() {
421 // (b - c) can have two forms that interest us:
422 //
423 // 1. sub nuw %b, %c
424 // 2. xor %c, %b
425 //
426 // For the first case, nuw flag guarantees our requirement b >= c.
427 //
428 // The second case might happen when the analysis can infer that b is a mask
429 // for c and we can transform sub operation into xor (that is usually true
430 // for constant b's). Even though xor is symmetrical, canonicalization
431 // ensures that the constant will be the RHS. We have additional checks
432 // later on to ensure that this xor operation is equivalent to subtraction.
434 m_Xor(m_Value(C), m_Value(B))));
435 };
436
437 const auto MatchASubBC = [&]() {
438 // Cases:
439 // - a * (b - c)
440 // - (b - c) * a
441 // - (b - c) <- a implicitly equals 1
442 return m_CombineOr(m_c_Mul(m_Value(A), MatchSubBC()), MatchSubBC());
443 };
444
445 const auto MatchCD = [&]() {
446 // Cases:
447 // - d * c
448 // - c * d
449 // - c <- d implicitly equals 1
451 };
452
453 const auto Match = [&](const Value *LHS, const Value *RHS) {
454 // We do use m_Specific(C) in MatchCD, so we have to make sure that
455 // it's bound to anything and match(LHS, MatchASubBC()) absolutely
456 // has to evaluate first and return true.
457 //
458 // If Match returns true, it is guaranteed that B != nullptr, C != nullptr.
459 return match(LHS, MatchASubBC()) && match(RHS, MatchCD());
460 };
461
462 if (!Match(Op0, Op1) && !Match(Op1, Op0))
463 return;
464
465 const auto ComputeKnownBitsOrOne = [&](const Value *V) {
466 // For some of the values we use the convention of leaving
467 // it nullptr to signify an implicit constant 1.
468 return V ? computeKnownBits(V, DemandedElts, Q, Depth + 1)
470 };
471
472 // Check that all operands are non-negative
473 const KnownBits KnownA = ComputeKnownBitsOrOne(A);
474 if (!KnownA.isNonNegative())
475 return;
476
477 const KnownBits KnownD = ComputeKnownBitsOrOne(D);
478 if (!KnownD.isNonNegative())
479 return;
480
481 const KnownBits KnownB = computeKnownBits(B, DemandedElts, Q, Depth + 1);
482 if (!KnownB.isNonNegative())
483 return;
484
485 const KnownBits KnownC = computeKnownBits(C, DemandedElts, Q, Depth + 1);
486 if (!KnownC.isNonNegative())
487 return;
488
489 // If we matched subtraction as xor, we need to actually check that xor
490 // is semantically equivalent to subtraction.
491 //
492 // For that to be true, b has to be a mask for c or that b's known
493 // ones cover all known and possible ones of c.
494 if (SubBC->getOpcode() == Instruction::Xor &&
495 !KnownC.getMaxValue().isSubsetOf(KnownB.getMinValue()))
496 return;
497
498 const APInt MaxA = KnownA.getMaxValue();
499 const APInt MaxD = KnownD.getMaxValue();
500 const APInt MaxAD = APIntOps::umax(MaxA, MaxD);
501 const APInt MaxB = KnownB.getMaxValue();
502
503 // We can't infer leading zeros info if the upper-bound estimate wraps.
504 bool Overflow;
505 const APInt UpperBound = MaxAD.umul_ov(MaxB, Overflow);
506
507 if (Overflow)
508 return;
509
510 // If we know that x <= y and both are positive than x has at least the same
511 // number of leading zeros as y.
512 const unsigned MinimumNumberOfLeadingZeros = UpperBound.countl_zero();
513 KnownOut.Zero.setHighBits(MinimumNumberOfLeadingZeros);
514}
515
516/// Infer non-negativity for R + (R < 0 ? A : 0), where R = srem X, Y.
517/// A must be a non-negative Y or abs(Y).
518static void computeKnownBitsFromEuclideanRem(const Value *Op0, const Value *Op1,
519 KnownBits &KnownOut,
520 const SimplifyQuery &Q,
521 unsigned Depth) {
522 Type *Ty = Op0->getType();
523
524 // Only handle scalars for now.
525 if (Ty->isVectorTy())
526 return;
527
528 const Value *Divisor = nullptr, *Cond = nullptr, *Remainder = nullptr,
529 *AdjustmentValue = nullptr;
531 const APInt *Threshold = nullptr;
532 bool TrueIfNegative = false;
533
534 const auto Match = [&](const Value *Rem, const Value *Adjustment) {
535 Remainder = Rem;
536
537 // Match an adjustment added only when this remainder is negative.
538 return match(Rem, m_SRem(m_Value(), m_Value(Divisor))) &&
539 match(Adjustment,
540 m_Select(m_Value(Cond), m_Value(AdjustmentValue), m_Zero())) &&
541 match(Cond, m_ICmp(Pred, m_Specific(Rem), m_APInt(Threshold))) &&
542 isSignBitCheck(Pred, *Threshold, TrueIfNegative) && TrueIfNegative;
543 };
544
545 if (!Match(Op0, Op1) && !Match(Op1, Op0))
546 return;
547
548 // Accept Y itself if non-negative, or abs(Y).
549 bool IsPositiveDivisorCase =
550 Divisor == AdjustmentValue && isKnownNonNegative(Divisor, Q, Depth + 1);
551 bool IsAbsoluteValueCase =
552 match(AdjustmentValue,
554
555 if (!IsPositiveDivisorCase && !IsAbsoluteValueCase)
556 return;
557
558 // Repeated uses must agree. An undef value could break this relationship.
559 if (!isGuaranteedNotToBeUndef(Remainder, Q.AC, Q.CtxI, Q.DT, Depth + 1) ||
560 !isGuaranteedNotToBeUndef(Divisor, Q.AC, Q.CtxI, Q.DT, Depth + 1))
561 return;
562
563 // For Y other than signed minimum, adding abs(Y) to a negative remainder
564 // gives a non-negative result without overflow. For signed minimum, the
565 // wrapping addition clears the remainder's sign bit. Poison-producing cases
566 // do not invalidate this deduction.
567 KnownOut.makeNonNegative();
568}
569
570static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1,
571 bool NSW, bool NUW,
572 const APInt &DemandedElts,
573 KnownBits &KnownOut, KnownBits &Known2,
574 const SimplifyQuery &Q, unsigned Depth) {
575 computeKnownBits(Op1, DemandedElts, KnownOut, Q, Depth + 1);
576
577 if (KnownOut.isUnknown() && !NSW && !NUW) {
578 if (Add) {
579 // Independent operand bits give no result, but the Euclidean remainder
580 // pattern can establish non-negativity through the operands'
581 // relationship.
582 computeKnownBitsFromEuclideanRem(Op0, Op1, KnownOut, Q, Depth);
583 }
584 return;
585 }
586
587 computeKnownBits(Op0, DemandedElts, Known2, Q, Depth + 1);
588 KnownOut = KnownBits::computeForAddSub(Add, NSW, NUW, Known2, KnownOut);
589
590 if (!Add && NSW && !KnownOut.isNonNegative() &&
592 .value_or(false) ||
593 match(Op1, m_c_SMin(m_Specific(Op0), m_Value()))))
594 KnownOut.makeNonNegative();
595
596 if (Add) {
597 // Try to match lerp pattern and combine results
598 computeKnownBitsFromLerpPattern(Op0, Op1, DemandedElts, KnownOut, Q, Depth);
599
600 // Use the relationship between the remainder and its adjustment.
601 computeKnownBitsFromEuclideanRem(Op0, Op1, KnownOut, Q, Depth);
602 }
603}
604
605static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,
606 bool NUW, const APInt &DemandedElts,
607 KnownBits &Known, KnownBits &Known2,
608 const SimplifyQuery &Q, unsigned Depth) {
609 computeKnownBits(Op1, DemandedElts, Known, Q, Depth + 1);
610 computeKnownBits(Op0, DemandedElts, Known2, Q, Depth + 1);
611
612 bool isKnownNegative = false;
613 bool isKnownNonNegative = false;
614 // If the multiplication is known not to overflow, compute the sign bit.
615 if (NSW) {
616 if (Op0 == Op1) {
617 // The product of a number with itself is non-negative.
618 isKnownNonNegative = true;
619 } else {
620 bool isKnownNonNegativeOp1 = Known.isNonNegative();
621 bool isKnownNonNegativeOp0 = Known2.isNonNegative();
622 bool isKnownNegativeOp1 = Known.isNegative();
623 bool isKnownNegativeOp0 = Known2.isNegative();
624 // The product of two numbers with the same sign is non-negative.
625 isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) ||
626 (isKnownNonNegativeOp1 && isKnownNonNegativeOp0);
627 if (!isKnownNonNegative && NUW) {
628 // mul nuw nsw with a factor > 1 is non-negative.
629 KnownBits One = KnownBits::makeConstant(APInt(Known.getBitWidth(), 1));
630 isKnownNonNegative = KnownBits::sgt(Known, One).value_or(false) ||
631 KnownBits::sgt(Known2, One).value_or(false);
632 }
633
634 // The product of a negative number and a non-negative number is either
635 // negative or zero.
638 (isKnownNegativeOp1 && isKnownNonNegativeOp0 &&
639 Known2.isNonZero()) ||
640 (isKnownNegativeOp0 && isKnownNonNegativeOp1 && Known.isNonZero());
641 }
642 }
643
644 bool SelfMultiply = Op0 == Op1;
645 if (SelfMultiply)
646 SelfMultiply &=
647 isGuaranteedNotToBeUndef(Op0, Q.AC, Q.CtxI, Q.DT, Depth + 1);
648 Known = KnownBits::mul(Known, Known2, SelfMultiply);
649
650 if (SelfMultiply) {
651 unsigned SignBits = ComputeNumSignBits(Op0, DemandedElts, Q, Depth + 1);
652 unsigned TyBits = Op0->getType()->getScalarSizeInBits();
653 unsigned OutValidBits = 2 * (TyBits - SignBits + 1);
654
655 if (OutValidBits < TyBits) {
656 APInt KnownZeroMask =
657 APInt::getHighBitsSet(TyBits, TyBits - OutValidBits + 1);
658 Known.Zero |= KnownZeroMask;
659 }
660 }
661
662 // Only make use of no-wrap flags if we failed to compute the sign bit
663 // directly. This matters if the multiplication always overflows, in
664 // which case we prefer to follow the result of the direct computation,
665 // though as the program is invoking undefined behaviour we can choose
666 // whatever we like here.
667 if (isKnownNonNegative && !Known.isNegative())
668 Known.makeNonNegative();
669 else if (isKnownNegative && !Known.isNonNegative())
670 Known.makeNegative();
671}
672
674 KnownBits &Known) {
675 unsigned BitWidth = Known.getBitWidth();
676 unsigned NumRanges = Ranges.getNumOperands() / 2;
677 assert(NumRanges >= 1);
678
679 Known.setAllConflict();
680
681 for (unsigned i = 0; i < NumRanges; ++i) {
683 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
685 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
686 ConstantRange Range(Lower->getValue(), Upper->getValue());
687 // BitWidth must equal the Ranges BitWidth for the correct number of high
688 // bits to be set.
689 assert(BitWidth == Range.getBitWidth() &&
690 "Known bit width must match range bit width!");
691
692 // The first CommonPrefixBits of all values in Range are equal.
693 unsigned CommonPrefixBits =
694 (Range.getUnsignedMax() ^ Range.getUnsignedMin()).countl_zero();
695 APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits);
696 APInt UnsignedMax = Range.getUnsignedMax().zextOrTrunc(BitWidth);
697 Known.One &= UnsignedMax & Mask;
698 Known.Zero &= ~UnsignedMax & Mask;
699 }
700}
701
702static bool isEphemeralValueOf(const Instruction *I, const Value *E) {
703 // The instruction defining an assumption's condition itself is always
704 // considered ephemeral to that assumption (even if it has other
705 // non-ephemeral users). See r246696's test case for an example.
706 if (is_contained(I->operands(), E))
707 return true;
708
709 const auto *EI = dyn_cast<Instruction>(E);
710 if (!EI)
711 return false;
712
713 if (EI == I)
714 return true;
715
718 Visited.insert(EI);
719 WorkList.push_back(EI);
720 bool ReachesI = false;
721 while (!WorkList.empty()) {
722 const Instruction *V = WorkList.pop_back_val();
723 for (const User *U : V->users()) {
724 const auto *UI = cast<Instruction>(U);
725 if (UI == I) {
726 ReachesI = true;
727 continue;
728 }
729 if (UI->mayHaveSideEffects() || UI->isTerminator())
730 return false;
731 if (Visited.insert(UI).second)
732 WorkList.push_back(UI);
733 }
734 }
735 return ReachesI;
736}
737
738// Is this an intrinsic that cannot be speculated but also cannot trap?
740 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(I))
741 return CI->isAssumeLikeIntrinsic();
742
743 return false;
744}
745
747 const Instruction *CtxI,
748 const DominatorTree *DT,
749 bool AllowEphemerals) {
750 // There are two restrictions on the use of an assume:
751 // 1. The assume must dominate the context (or the control flow must
752 // reach the assume whenever it reaches the context).
753 // 2. The context must not be in the assume's set of ephemeral values
754 // (otherwise we will use the assume to prove that the condition
755 // feeding the assume is trivially true, thus causing the removal of
756 // the assume).
757
758 // If the checked instruction and the assume are the same, it's valid exactly
759 // if ephemeral values are allowed.
760 if (Inv == CtxI)
761 return AllowEphemerals;
762
763 if (Inv->getParent() == CtxI->getParent()) {
764 // If Inv and CtxI are in the same block, check if the assume (Inv) is first
765 // in the BB.
766 if (Inv->comesBefore(CtxI))
767 return true;
768
769 // The context comes first, but they're both in the same block.
770 // Make sure there is nothing in between that might interrupt
771 // the control flow, not even CtxI itself.
772 // We limit the scan distance between the assume and its context instruction
773 // to avoid a compile-time explosion. This limit is chosen arbitrarily, so
774 // it can be adjusted if needed (could be turned into a cl::opt).
775 auto Range = make_range(CtxI->getIterator(), Inv->getIterator());
777 return false;
778
779 return AllowEphemerals || !isEphemeralValueOf(Inv, CtxI);
780 }
781
782 // Inv and CtxI are in different blocks.
783 if (DT) {
784 if (DT->dominates(Inv, CtxI))
785 return true;
786 } else if (Inv->getParent() == CtxI->getParent()->getSinglePredecessor() ||
787 Inv->getParent()->isEntryBlock()) {
788 // We don't have a DT, but this trivially dominates.
789 return true;
790 }
791
792 return false;
793}
794
797 unsigned &NumChecked) {
798 for (const Instruction &I : make_range(Begin, End)) {
799 if (NumChecked++ > MaxInstrsToCheckForFree)
800 return false;
801 if (auto *CB = dyn_cast<CallBase>(&I)) {
802 if (!CB->hasFnAttr(Attribute::NoFree))
803 return false;
804 } else if (I.maySynchronize()) {
805 return false;
806 }
807 }
808 return true;
809}
810
812 const Instruction *CtxI,
813 const DominatorTree *DT) {
814 const BasicBlock *CtxBB = CtxI->getParent();
815 const BasicBlock *AssumeBB = Assume->getParent();
816 unsigned NumChecked = 0;
817 BasicBlock::const_iterator CtxIter = CtxI->getIterator();
818 if (CtxBB == AssumeBB) {
819 if (Assume != CtxI && !Assume->comesBefore(CtxI))
820 return false;
821 return hasNoFreeInRange(Assume->getIterator(), CtxIter, NumChecked);
822 }
823 if (DT && !DT->dominates(Assume, CtxI))
824 return false;
825 if (!hasNoFreeInRange(CtxBB->begin(), CtxIter, NumChecked))
826 return false;
827 if (pred_empty(CtxBB))
828 return false;
829
830 // Note: CtxBB is NOT pre-inserted into Visited to ensure that loop
831 // backedges returning to CtxBB are enqueued and checked correctly.
834 while (!Worklist.empty()) {
835 const BasicBlock *CurBB = Worklist.pop_back_val();
836 if (!Visited.insert(CurBB).second)
837 continue;
838
839 if (CurBB == AssumeBB) {
840 if (!hasNoFreeInRange(Assume->getIterator(), AssumeBB->end(), NumChecked))
841 return false;
842 continue;
843 }
844 assert((!DT || DT->dominates(AssumeBB, CurBB)) &&
845 "Blocks between Assume and CtxI must be dominated by AssumeBB");
846
847 if (pred_empty(CurBB)) {
848 // Reaching the entry block means there is a path to CtxI that does not
849 // pass through Assume.
850 if (CurBB->isEntryBlock())
851 return false;
852
853 // CurBB is unreachable (for example, emptied by SimplifyCFG but not yet
854 // removed), so nothing in it can execute a free.
855 continue;
856 }
857
858 // If CurBB == CtxBB (due to a loop backedge targeting CtxBB), check
859 // instructions from CtxIter to the end of CtxBB (instructions before
860 // CtxIter were checked above). Otherwise, check the entire block.
861 auto StartIt = (CurBB == CtxBB) ? CtxIter : CurBB->begin();
862 if (!hasNoFreeInRange(StartIt, CurBB->end(), NumChecked))
863 return false;
864 append_range(Worklist, predecessors(CurBB));
865 }
866 return true;
867}
868
869// TODO: cmpExcludesZero misses many cases where `RHS` is non-constant but
870// we still have enough information about `RHS` to conclude non-zero. For
871// example Pred=EQ, RHS=isKnownNonZero. cmpExcludesZero is called in loops
872// so the extra compile time may not be worth it, but possibly a second API
873// should be created for use outside of loops.
875 // v u> y implies v != 0.
877 return true;
878
879 // Special-case v != 0 to also handle v != null.
880 if (Pred == ICmpInst::ICMP_NE)
881 return match(RHS, m_Zero());
882
883 // All other predicates - rely on generic ConstantRange handling.
884 const APInt *C;
885 auto Zero = APInt::getZero(RHS->getType()->getScalarSizeInBits());
886 if (match(RHS, m_APInt(C))) {
888 return !TrueValues.contains(Zero);
889 }
890
892 if (VC == nullptr)
893 return false;
894
895 for (unsigned ElemIdx = 0, NElem = VC->getNumElements(); ElemIdx < NElem;
896 ++ElemIdx) {
898 Pred, VC->getElementAsAPInt(ElemIdx));
899 if (TrueValues.contains(Zero))
900 return false;
901 }
902 return true;
903}
904
905static void breakSelfRecursivePHI(const Use *U, const PHINode *PHI,
906 Value *&ValOut, Instruction *&CtxIOut,
907 const PHINode **PhiOut = nullptr) {
908 ValOut = U->get();
909 if (ValOut == PHI)
910 return;
911 CtxIOut = PHI->getIncomingBlock(*U)->getTerminator();
912 if (PhiOut)
913 *PhiOut = PHI;
914 Value *V;
915 // If the Use is a select of this phi, compute analysis on other arm to break
916 // recursion.
917 // TODO: Min/Max
918 if (match(ValOut, m_Select(m_Value(), m_Specific(PHI), m_Value(V))) ||
919 match(ValOut, m_Select(m_Value(), m_Value(V), m_Specific(PHI))))
920 ValOut = V;
921
922 // Same for select, if this phi is 2-operand phi, compute analysis on other
923 // incoming value to break recursion.
924 // TODO: We could handle any number of incoming edges as long as we only have
925 // two unique values.
926 if (auto *IncPhi = dyn_cast<PHINode>(ValOut);
927 IncPhi && IncPhi->getNumIncomingValues() == 2) {
928 for (int Idx = 0; Idx < 2; ++Idx) {
929 if (IncPhi->getIncomingValue(Idx) == PHI) {
930 ValOut = IncPhi->getIncomingValue(1 - Idx);
931 if (PhiOut)
932 *PhiOut = IncPhi;
933 CtxIOut = IncPhi->getIncomingBlock(1 - Idx)->getTerminator();
934 break;
935 }
936 }
937 }
938}
939
940static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q) {
941 // Use of assumptions is context-sensitive. If we don't have a context, we
942 // cannot use them!
943 if (!Q.AC || !Q.CtxI)
944 return false;
945
947 if (!Elem.Assume)
948 continue;
949
950 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
951 assert(I->getFunction() == Q.CtxI->getFunction() &&
952 "Got assumption for the wrong function!");
953
954 if (Elem.Index != AssumptionCache::ExprResultIdx) {
956 I->getOperandBundleAt(Elem.Index)) &&
958 return true;
959 continue;
960 }
961
962 // Warning: This loop can end up being somewhat performance sensitive.
963 // We're running this loop for once for each value queried resulting in a
964 // runtime of ~O(#assumes * #values).
965
966 if (match(I->getArgOperand(0), m_Trunc(m_Specific(V))) &&
968 return true;
969
970 Value *RHS;
973 if (!match(I->getArgOperand(0), m_c_ICmp(Pred, m_V, m_Value(RHS))))
974 continue;
975
977 return true;
978 }
979
980 return false;
981}
982
985 const SimplifyQuery &Q) {
986 if (RHS->getType()->isPointerTy()) {
987 // Handle comparison of pointer to null explicitly, as it will not be
988 // covered by the m_APInt() logic below.
989 if (LHS == V && match(RHS, m_Zero())) {
990 switch (Pred) {
992 Known.setAllZero();
993 break;
996 Known.makeNonNegative();
997 break;
999 Known.makeNegative();
1000 break;
1001 default:
1002 break;
1003 }
1004 }
1005 return;
1006 }
1007
1008 unsigned BitWidth = Known.getBitWidth();
1009 auto m_V =
1011
1012 Value *Y;
1013 const APInt *Mask, *C;
1014 if (!match(RHS, m_APInt(C)))
1015 return;
1016
1017 uint64_t ShAmt;
1018 switch (Pred) {
1019 case ICmpInst::ICMP_EQ:
1020 // assume(V = C)
1021 if (match(LHS, m_V)) {
1022 Known = Known.unionWith(KnownBits::makeConstant(*C));
1023 // assume(V & Mask = C)
1024 } else if (match(LHS, m_c_And(m_V, m_Value(Y)))) {
1025 // For one bits in Mask, we can propagate bits from C to V.
1026 Known.One |= *C;
1027 if (match(Y, m_APInt(Mask)))
1028 Known.Zero |= ~*C & *Mask;
1029 // assume(V | Mask = C)
1030 } else if (match(LHS, m_c_Or(m_V, m_Value(Y)))) {
1031 // For zero bits in Mask, we can propagate bits from C to V.
1032 Known.Zero |= ~*C;
1033 if (match(Y, m_APInt(Mask)))
1034 Known.One |= *C & ~*Mask;
1035 // assume(V << ShAmt = C)
1036 } else if (match(LHS, m_Shl(m_V, m_ConstantInt(ShAmt))) &&
1037 ShAmt < BitWidth) {
1038 // For those bits in C that are known, we can propagate them to known
1039 // bits in V shifted to the right by ShAmt.
1040 KnownBits RHSKnown = KnownBits::makeConstant(*C);
1041 RHSKnown >>= ShAmt;
1042 Known = Known.unionWith(RHSKnown);
1043 // assume(V >> ShAmt = C)
1044 } else if (match(LHS, m_Shr(m_V, m_ConstantInt(ShAmt))) &&
1045 ShAmt < BitWidth) {
1046 // For those bits in RHS that are known, we can propagate them to known
1047 // bits in V shifted to the right by C.
1048 KnownBits RHSKnown = KnownBits::makeConstant(*C);
1049 RHSKnown <<= ShAmt;
1050 Known = Known.unionWith(RHSKnown);
1051 }
1052 break;
1053 case ICmpInst::ICMP_NE: {
1054 // assume (V & B != 0) where B is a power of 2
1055 const APInt *BPow2;
1056 if (C->isZero() && match(LHS, m_And(m_V, m_Power2(BPow2))))
1057 Known.One |= *BPow2;
1058 break;
1059 }
1060 default: {
1061 const APInt *Offset = nullptr;
1062 if (match(LHS, m_CombineOr(m_V, m_AddLike(m_V, m_APInt(Offset))))) {
1064 if (Offset)
1065 LHSRange = LHSRange.sub(*Offset);
1066 Known = Known.unionWith(LHSRange.toKnownBits());
1067 }
1069 // X & Y u> C -> X u> C && Y u> C
1070 // X nuw- Y u> C -> X u> C
1071 if (match(LHS, m_c_And(m_V, m_Value())) ||
1072 match(LHS, m_NUWSub(m_V, m_Value())))
1073 Known.One.setHighBits(
1074 (*C + (Pred == ICmpInst::ICMP_UGT)).countLeadingOnes());
1075 }
1077 // X | Y u< C -> X u< C && Y u< C
1078 // X nuw+ Y u< C -> X u< C && Y u< C
1079 if (match(LHS, m_c_Or(m_V, m_Value())) ||
1080 match(LHS, m_c_NUWAdd(m_V, m_Value()))) {
1081 Known.Zero.setHighBits(
1082 (*C - (Pred == ICmpInst::ICMP_ULT)).countLeadingZeros());
1083 }
1084 }
1085 } break;
1086 }
1087}
1088
1091 const SimplifyQuery &SQ, bool Invert) {
1093 Invert ? Cmp->getInversePredicate() : Cmp->getPredicate();
1094 Value *LHS = Cmp->getOperand(0);
1095 Value *RHS = Cmp->getOperand(1);
1096
1097 // Handle icmp pred (trunc V), C
1098 if (match(LHS, m_Trunc(m_Specific(V)))) {
1099 KnownBits DstKnown(LHS->getType()->getScalarSizeInBits());
1100 computeKnownBitsFromCmp(LHS, Pred, LHS, RHS, DstKnown, SQ);
1102 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
1103 else
1104 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
1105 return;
1106 }
1107
1109}
1110
1112 KnownBits &Known, const SimplifyQuery &SQ,
1113 bool Invert, unsigned Depth) {
1114 Value *A, *B;
1117 KnownBits Known2(Known.getBitWidth());
1118 KnownBits Known3(Known.getBitWidth());
1119 computeKnownBitsFromCond(V, A, Known2, SQ, Invert, Depth + 1);
1120 computeKnownBitsFromCond(V, B, Known3, SQ, Invert, Depth + 1);
1121 if (Invert ? match(Cond, m_LogicalOr(m_Value(), m_Value()))
1123 Known2 = Known2.unionWith(Known3);
1124 else
1125 Known2 = Known2.intersectWith(Known3);
1126 Known = Known.unionWith(Known2);
1127 return;
1128 }
1129
1130 if (auto *Cmp = dyn_cast<ICmpInst>(Cond)) {
1131 computeKnownBitsFromICmpCond(V, Cmp, Known, SQ, Invert);
1132 return;
1133 }
1134
1135 if (match(Cond, m_Trunc(m_Specific(V)))) {
1136 KnownBits DstKnown(1);
1137 if (Invert) {
1138 DstKnown.setAllZero();
1139 } else {
1140 DstKnown.setAllOnes();
1141 }
1143 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
1144 return;
1145 }
1146 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
1147 return;
1148 }
1149
1151 computeKnownBitsFromCond(V, A, Known, SQ, !Invert, Depth + 1);
1152}
1153
1155 const SimplifyQuery &Q, unsigned Depth) {
1156 // Handle injected condition.
1157 if (Q.CC && Q.CC->AffectedValues.contains(V))
1159
1160 if (!Q.CtxI)
1161 return;
1162
1163 if (Q.DC && Q.DT) {
1164 // Handle dominating conditions.
1165 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
1166 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
1167 if (Q.DT->dominates(Edge0, Q.CtxI->getParent()))
1168 computeKnownBitsFromCond(V, BI->getCondition(), Known, Q,
1169 /*Invert*/ false, Depth);
1170
1171 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
1172 if (Q.DT->dominates(Edge1, Q.CtxI->getParent()))
1173 computeKnownBitsFromCond(V, BI->getCondition(), Known, Q,
1174 /*Invert*/ true, Depth);
1175 }
1176
1177 if (Known.hasConflict())
1178 Known.resetAll();
1179 }
1180
1181 if (!Q.AC)
1182 return;
1183
1184 unsigned BitWidth = Known.getBitWidth();
1185
1186 // Note that the patterns below need to be kept in sync with the code
1187 // in AssumptionCache::updateAffectedValues.
1188
1189 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
1190 if (!Elem.Assume)
1191 continue;
1192
1193 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
1194 assert(I->getParent()->getParent() == Q.CtxI->getParent()->getParent() &&
1195 "Got assumption for the wrong function!");
1196
1197 if (Elem.Index != AssumptionCache::ExprResultIdx) {
1198 auto OBU = I->getOperandBundleAt(Elem.Index);
1199 switch (getBundleAttrFromOBU(OBU)) {
1200 case BundleAttr::Align: {
1201 auto [Ptr, _, _2, Alignment, Offset] = getAssumeAlignInfo(OBU);
1202 if (Ptr == V && Alignment && Offset && isPowerOf2_64(*Alignment) &&
1204 Known.Zero |= (*Alignment - 1) & ~*Offset;
1205 Known.One |= (*Alignment - 1) & *Offset;
1206 }
1207 } break;
1208
1209 case BundleAttr::Range: {
1210 auto [Val, Range] = getAssumeRangeInfo(OBU);
1211 if (Val == V && isValidAssumeForContext(I, Q))
1212 Known = Known.unionWith(Range.toKnownBits());
1213 } break;
1214
1215 default:
1216 break;
1217 }
1218 continue;
1219 }
1220
1221 // Warning: This loop can end up being somewhat performance sensitive.
1222 // We're running this loop for once for each value queried resulting in a
1223 // runtime of ~O(#assumes * #values).
1224
1225 Value *Arg = I->getArgOperand(0);
1226
1227 if (Arg == V && isValidAssumeForContext(I, Q)) {
1228 assert(BitWidth == 1 && "assume operand is not i1?");
1229 (void)BitWidth;
1230 Known.setAllOnes();
1231 return;
1232 }
1233 if (match(Arg, m_Not(m_Specific(V))) &&
1235 assert(BitWidth == 1 && "assume operand is not i1?");
1236 (void)BitWidth;
1237 Known.setAllZero();
1238 return;
1239 }
1240 auto *Trunc = dyn_cast<TruncInst>(Arg);
1241 if (Trunc && Trunc->getOperand(0) == V &&
1243 if (Trunc->hasNoUnsignedWrap()) {
1245 return;
1246 }
1247 Known.One.setBit(0);
1248 return;
1249 }
1250
1251 // The remaining tests are all recursive, so bail out if we hit the limit.
1253 continue;
1254
1255 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
1256 if (!Cmp)
1257 continue;
1258
1259 if (!isValidAssumeForContext(I, Q))
1260 continue;
1261
1262 computeKnownBitsFromICmpCond(V, Cmp, Known, Q, /*Invert=*/false);
1263 }
1264
1265 // Conflicting assumption: Undefined behavior will occur on this execution
1266 // path.
1267 if (Known.hasConflict())
1268 Known.resetAll();
1269}
1270
1271/// Compute known bits from a shift operator, including those with a
1272/// non-constant shift amount. Known is the output of this function. Known2 is a
1273/// pre-allocated temporary with the same bit width as Known and on return
1274/// contains the known bit of the shift value source. KF is an
1275/// operator-specific function that, given the known-bits and a shift amount,
1276/// compute the implied known-bits of the shift operator's result respectively
1277/// for that shift amount. The results from calling KF are conservatively
1278/// combined for all permitted shift amounts.
1280 const Operator *I, const APInt &DemandedElts, KnownBits &Known,
1281 KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth,
1282 function_ref<KnownBits(const KnownBits &, const KnownBits &, bool)> KF) {
1283 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1284 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1285 // To limit compile-time impact, only query isKnownNonZero() if we know at
1286 // least something about the shift amount.
1287 bool ShAmtNonZero =
1288 Known.isNonZero() ||
1289 (Known.getMaxValue().ult(Known.getBitWidth()) &&
1290 isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth + 1));
1291 Known = KF(Known2, Known, ShAmtNonZero);
1292}
1293
1294static KnownBits
1295getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts,
1296 const KnownBits &KnownLHS, const KnownBits &KnownRHS,
1297 const SimplifyQuery &Q, unsigned Depth) {
1298 unsigned BitWidth = KnownLHS.getBitWidth();
1299 KnownBits KnownOut(BitWidth);
1300 bool IsAnd = false;
1301 bool HasKnownOne = !KnownLHS.One.isZero() || !KnownRHS.One.isZero();
1302 Value *X = nullptr, *Y = nullptr;
1303
1304 switch (I->getOpcode()) {
1305 case Instruction::And:
1306 KnownOut = KnownLHS & KnownRHS;
1307 IsAnd = true;
1308 // and(x, -x) is common idioms that will clear all but lowest set
1309 // bit. If we have a single known bit in x, we can clear all bits
1310 // above it.
1311 // TODO: instcombine often reassociates independent `and` which can hide
1312 // this pattern. Try to match and(x, and(-x, y)) / and(and(x, y), -x).
1313 if (HasKnownOne && match(I, m_c_And(m_Value(X), m_Neg(m_Deferred(X))))) {
1314 // -(-x) == x so using whichever (LHS/RHS) gets us a better result.
1315 if (KnownLHS.countMaxTrailingZeros() <= KnownRHS.countMaxTrailingZeros())
1316 KnownOut = KnownLHS.blsi();
1317 else
1318 KnownOut = KnownRHS.blsi();
1319 }
1320 break;
1321 case Instruction::Or:
1322 KnownOut = KnownLHS | KnownRHS;
1323 break;
1324 case Instruction::Xor:
1325 KnownOut = KnownLHS ^ KnownRHS;
1326 // xor(x, x-1) is common idioms that will clear all but lowest set
1327 // bit. If we have a single known bit in x, we can clear all bits
1328 // above it.
1329 // TODO: xor(x, x-1) is often rewritting as xor(x, x-C) where C !=
1330 // -1 but for the purpose of demanded bits (xor(x, x-C) &
1331 // Demanded) == (xor(x, x-1) & Demanded). Extend the xor pattern
1332 // to use arbitrary C if xor(x, x-C) as the same as xor(x, x-1).
1333 if (HasKnownOne &&
1335 const KnownBits &XBits = I->getOperand(0) == X ? KnownLHS : KnownRHS;
1336 KnownOut = XBits.blsmsk();
1337 }
1338 break;
1339 default:
1340 llvm_unreachable("Invalid Op used in 'analyzeKnownBitsFromAndXorOr'");
1341 }
1342
1343 // and(x, add (x, -1)) is a common idiom that always clears the low bit;
1344 // xor/or(x, add (x, -1)) is an idiom that will always set the low bit.
1345 // here we handle the more general case of adding any odd number by
1346 // matching the form and/xor/or(x, add(x, y)) where y is odd.
1347 // TODO: This could be generalized to clearing any bit set in y where the
1348 // following bit is known to be unset in y.
1349 if (!KnownOut.Zero[0] && !KnownOut.One[0] &&
1353 KnownBits KnownY(BitWidth);
1354 computeKnownBits(Y, DemandedElts, KnownY, Q, Depth + 1);
1355 if (KnownY.countMinTrailingOnes() > 0) {
1356 if (IsAnd)
1357 KnownOut.Zero.setBit(0);
1358 else
1359 KnownOut.One.setBit(0);
1360 }
1361 }
1362 return KnownOut;
1363}
1364
1366 const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q,
1367 unsigned Depth,
1368 const function_ref<KnownBits(const KnownBits &, const KnownBits &)>
1369 KnownBitsFunc) {
1370 APInt DemandedEltsLHS, DemandedEltsRHS;
1372 DemandedElts, DemandedEltsLHS,
1373 DemandedEltsRHS);
1374
1375 const auto ComputeForSingleOpFunc =
1376 [Depth, &Q, KnownBitsFunc](const Value *Op, APInt &DemandedEltsOp) {
1377 return KnownBitsFunc(
1378 computeKnownBits(Op, DemandedEltsOp, Q, Depth + 1),
1379 computeKnownBits(Op, DemandedEltsOp << 1, Q, Depth + 1));
1380 };
1381
1382 if (DemandedEltsRHS.isZero())
1383 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS);
1384 if (DemandedEltsLHS.isZero())
1385 return ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS);
1386
1387 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS)
1388 .intersectWith(ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS));
1389}
1390
1391// Public so this can be used in `SimplifyDemandedUseBits`.
1393 const KnownBits &KnownLHS,
1394 const KnownBits &KnownRHS,
1395 const SimplifyQuery &SQ,
1396 unsigned Depth) {
1397 auto *FVTy = dyn_cast<FixedVectorType>(I->getType());
1398 APInt DemandedElts =
1399 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
1400
1401 return getKnownBitsFromAndXorOr(I, DemandedElts, KnownLHS, KnownRHS, SQ,
1402 Depth);
1403}
1404
1406 Attribute Attr = F->getFnAttribute(Attribute::VScaleRange);
1407 // Without vscale_range, we only know that vscale is non-zero.
1408 if (!Attr.isValid())
1410
1411 unsigned AttrMin = Attr.getVScaleRangeMin();
1412 // Minimum is larger than vscale width, result is always poison.
1413 if ((unsigned)llvm::bit_width(AttrMin) > BitWidth)
1414 return ConstantRange::getEmpty(BitWidth);
1415
1416 APInt Min(BitWidth, AttrMin);
1417 std::optional<unsigned> AttrMax = Attr.getVScaleRangeMax();
1418 if (!AttrMax || (unsigned)llvm::bit_width(*AttrMax) > BitWidth)
1420
1421 return ConstantRange(Min, APInt(BitWidth, *AttrMax) + 1);
1422}
1423
1424/// Return true if \p II reads a register named "vlenb". On RISC-V this is the
1425/// VLENB CSR, which holds VLEN/8: a non-zero power of two bounded by the
1426/// target's VLEN range. Callers must ensure the target is RISC-V.
1427static bool isReadVLENB(const IntrinsicInst &II) {
1428 auto *MAV = dyn_cast<MetadataAsValue>(II.getArgOperand(0));
1429 if (!MAV)
1430 return false;
1431 auto *MD = dyn_cast<MDNode>(MAV->getMetadata());
1432 if (!MD || MD->getNumOperands() != 1)
1433 return false;
1434 auto *RegName = dyn_cast<MDString>(MD->getOperand(0));
1435 return RegName && RegName->getString() == "vlenb";
1436}
1437
1438/// Return the value range of a RISC-V vlenb CSR read. RVV requires VLEN to be a
1439/// power of two in [32, 65536] (Zvl32b is the smallest vector extension), so
1440/// VLENB = VLEN/8 is in [4, 8192]. This architectural bound is independent of
1441/// any function attribute and stays sound for Zvl32b, whose VLEN (32) is not
1442/// representable as an integer vscale (VLEN / RVVBitsPerBlock). A vscale_range
1443/// attribute, when present, pins the subtarget's VLEN in units of
1444/// RVVBitsPerBlock (64 bits) and so gives a tighter VLENB = vscale *
1445/// RVVBytesPerBlock.
1447 unsigned Width) {
1448 // Architectural bounds: VLEN in [32, 65536] => VLENB in [4, 8192].
1449 ConstantRange Range(APInt(Width, 32 / 8), APInt(Width, 65536 / 8) + 1);
1450
1451 const Function *F = II.getFunction();
1452 if (F->getFnAttribute(Attribute::VScaleRange).isValid()) {
1453 ConstantRange VScale = getVScaleRange(F, Width);
1454 Range = Range.intersectWith(
1456 }
1457 return Range;
1458}
1459
1461 Value *Arm, bool Invert,
1462 const SimplifyQuery &Q, unsigned Depth) {
1463 // If we have a constant arm, we are done.
1464 if (Known.isConstant())
1465 return;
1466
1467 // See what condition implies about the bits of the select arm.
1468 KnownBits CondRes(Known.getBitWidth());
1469 computeKnownBitsFromCond(Arm, Cond, CondRes, Q, Invert, Depth + 1);
1470 // If we don't get any information from the condition, no reason to
1471 // proceed.
1472 if (CondRes.isUnknown())
1473 return;
1474
1475 // We can have conflict if the condition is dead. I.e if we have
1476 // (x | 64) < 32 ? (x | 64) : y
1477 // we will have conflict at bit 6 from the condition/the `or`.
1478 // In that case just return. Its not particularly important
1479 // what we do, as this select is going to be simplified soon.
1480 CondRes = CondRes.unionWith(Known);
1481 if (CondRes.hasConflict())
1482 return;
1483
1484 // Finally make sure the information we found is valid. This is relatively
1485 // expensive so it's left for the very end.
1486 if (!isGuaranteedNotToBeUndef(Arm, Q.AC, Q.CtxI, Q.DT, Depth + 1))
1487 return;
1488
1489 // Finally, we know we get information from the condition and its valid,
1490 // so return it.
1491 Known = std::move(CondRes);
1492}
1493
1494// Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow).
1495// Returns the input and lower/upper bounds.
1496static bool isSignedMinMaxClamp(const Value *Select, const Value *&In,
1497 const APInt *&CLow, const APInt *&CHigh) {
1499 cast<Operator>(Select)->getOpcode() == Instruction::Select &&
1500 "Input should be a Select!");
1501
1502 const Value *LHS = nullptr, *RHS = nullptr;
1504 if (SPF != SPF_SMAX && SPF != SPF_SMIN)
1505 return false;
1506
1507 if (!match(RHS, m_APInt(CLow)))
1508 return false;
1509
1510 const Value *LHS2 = nullptr, *RHS2 = nullptr;
1512 if (getInverseMinMaxFlavor(SPF) != SPF2)
1513 return false;
1514
1515 if (!match(RHS2, m_APInt(CHigh)))
1516 return false;
1517
1518 if (SPF == SPF_SMIN)
1519 std::swap(CLow, CHigh);
1520
1521 In = LHS2;
1522 return CLow->sle(*CHigh);
1523}
1524
1526 const APInt *&CLow,
1527 const APInt *&CHigh) {
1528 assert((II->getIntrinsicID() == Intrinsic::smin ||
1529 II->getIntrinsicID() == Intrinsic::smax) &&
1530 "Must be smin/smax");
1531
1532 Intrinsic::ID InverseID = getInverseMinMaxIntrinsic(II->getIntrinsicID());
1533 auto *InnerII = dyn_cast<IntrinsicInst>(II->getArgOperand(0));
1534 if (!InnerII || InnerII->getIntrinsicID() != InverseID ||
1535 !match(II->getArgOperand(1), m_APInt(CLow)) ||
1536 !match(InnerII->getArgOperand(1), m_APInt(CHigh)))
1537 return false;
1538
1539 if (II->getIntrinsicID() == Intrinsic::smin)
1540 std::swap(CLow, CHigh);
1541 return CLow->sle(*CHigh);
1542}
1543
1545 KnownBits &Known) {
1546 const APInt *CLow, *CHigh;
1547 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
1548 Known = Known.unionWith(
1549 ConstantRange::getNonEmpty(*CLow, *CHigh + 1).toKnownBits());
1550}
1551
1553 const PHINode *P, Value *Start, Value *Step, const APInt &DemandedElts,
1554 KnownBits &KnownStart, KnownBits &KnownStep, const SimplifyQuery &Q,
1555 unsigned Depth) {
1556 // Change the context instruction to the "edge" that flows into the phi. This
1557 // is important because that is where the value is actually "evaluated" even
1558 // though it is used later somewhere else. (see also D69571).
1560 unsigned OpNum = P->getOperand(0) == Start ? 0 : 1;
1561
1562 RecQ.CtxI = P->getIncomingBlock(OpNum)->getTerminator();
1563 computeKnownBits(Start, DemandedElts, KnownStart, RecQ, Depth + 1);
1564
1565 RecQ.CtxI = P->getIncomingBlock(1 - OpNum)->getTerminator();
1566 computeKnownBits(Step, DemandedElts, KnownStep, RecQ, Depth + 1);
1567}
1568
1570 const APInt &DemandedElts,
1572 const SimplifyQuery &Q,
1573 unsigned Depth) {
1574 unsigned BitWidth = Known.getBitWidth();
1575
1576 KnownBits Known2(BitWidth);
1577 switch (I->getOpcode()) {
1578 default: break;
1579 case Instruction::Load:
1580 if (MDNode *MD =
1581 Q.IIQ.getMetadata(cast<LoadInst>(I), LLVMContext::MD_range))
1583 break;
1584 case Instruction::And:
1585 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1586 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1587
1588 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1589 break;
1590 case Instruction::Or:
1591 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1592 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1593
1594 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1595 break;
1596 case Instruction::Xor:
1597 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1598 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1599
1600 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1601 break;
1602 case Instruction::Mul: {
1605 computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, NUW,
1606 DemandedElts, Known, Known2, Q, Depth);
1607 break;
1608 }
1609 case Instruction::UDiv: {
1610 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1611 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1612 Known =
1614 break;
1615 }
1616 case Instruction::SDiv: {
1617 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1618 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1619 Known =
1621 break;
1622 }
1623 case Instruction::Select: {
1624 auto ComputeForArm = [&](Value *Arm, bool Invert) {
1625 KnownBits Res(Known.getBitWidth());
1626 computeKnownBits(Arm, DemandedElts, Res, Q, Depth + 1);
1627 adjustKnownBitsForSelectArm(Res, I->getOperand(0), Arm, Invert, Q, Depth);
1628 return Res;
1629 };
1630 // Only known if known in both the LHS and RHS.
1631 Known =
1632 ComputeForArm(I->getOperand(1), /*Invert=*/false)
1633 .intersectWith(ComputeForArm(I->getOperand(2), /*Invert=*/true));
1634 break;
1635 }
1636 case Instruction::FPToSI: {
1637 // fptosi is poison if the rounded value doesn't fit in the result type,
1638 // so we can assume the conversion is well-defined and rounds towards
1639 // zero. +-Inf can never fit in an integer type, so it is always poison,
1640 // like NaN. Negative subnormals and negative zero round to 0. That
1641 // leaves negative normals as the only class that can produce a defined
1642 // negative result.
1643 KnownFPClass SrcFPClass = computeKnownFPClass(
1644 I->getOperand(0), DemandedElts, fcNegNormal, Q, Depth + 1);
1645 if (SrcFPClass.isKnownNever(fcNegNormal))
1646 Known.makeNonNegative();
1647 break;
1648 }
1649 case Instruction::FPTrunc:
1650 case Instruction::FPExt:
1651 case Instruction::FPToUI:
1652 case Instruction::SIToFP:
1653 case Instruction::UIToFP:
1654 break; // Can't work with floating point.
1655 case Instruction::PtrToInt:
1656 case Instruction::PtrToAddr:
1657 case Instruction::IntToPtr:
1658 // Fall through and handle them the same as zext/trunc.
1659 [[fallthrough]];
1660 case Instruction::ZExt:
1661 case Instruction::Trunc: {
1662 Type *SrcTy = I->getOperand(0)->getType();
1663
1664 unsigned SrcBitWidth;
1665 // Note that we handle pointer operands here because of inttoptr/ptrtoint
1666 // which fall through here.
1667 Type *ScalarTy = SrcTy->getScalarType();
1668 SrcBitWidth = ScalarTy->isPointerTy() ?
1669 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
1670 Q.DL.getTypeSizeInBits(ScalarTy);
1671
1672 assert(SrcBitWidth && "SrcBitWidth can't be zero");
1673 Known = Known.anyextOrTrunc(SrcBitWidth);
1674 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1675 if (auto *Inst = dyn_cast<PossiblyNonNegInst>(I);
1676 Inst && Inst->hasNonNeg() && !Known.isNegative())
1677 Known.makeNonNegative();
1678 Known = Known.zextOrTrunc(BitWidth);
1679 break;
1680 }
1681 case Instruction::BitCast: {
1682 Type *SrcTy = I->getOperand(0)->getType();
1683 if (SrcTy->isIntOrPtrTy() &&
1684 // TODO: For now, not handling conversions like:
1685 // (bitcast i64 %x to <2 x i32>)
1686 !I->getType()->isVectorTy()) {
1687 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1688 break;
1689 }
1690
1691 const Value *V;
1692 // Handle bitcast from floating point to integer.
1694 V->getType()->isFPOrFPVectorTy()) {
1695 Type *FPType = V->getType()->getScalarType();
1696 KnownFPClass Result =
1697 computeKnownFPClass(V, DemandedElts, fcAllFlags, Q, Depth + 1);
1698
1699 Known = Result.toKnownBits(FPType->getFltSemantics());
1700
1701 break;
1702 }
1703
1704 // Handle cast from vector integer type to scalar or vector integer.
1705 auto *SrcVecTy = dyn_cast<FixedVectorType>(SrcTy);
1706 if (!SrcVecTy || !SrcVecTy->getElementType()->isIntegerTy() ||
1707 !I->getType()->isIntOrIntVectorTy() ||
1708 isa<ScalableVectorType>(I->getType()))
1709 break;
1710
1711 unsigned NumElts = DemandedElts.getBitWidth();
1712 bool IsLE = Q.DL.isLittleEndian();
1713 // Look through a cast from narrow vector elements to wider type.
1714 // Examples: v4i32 -> v2i64, v3i8 -> v24
1715 unsigned SubBitWidth = SrcVecTy->getScalarSizeInBits();
1716 if (BitWidth % SubBitWidth == 0) {
1717 // Known bits are automatically intersected across demanded elements of a
1718 // vector. So for example, if a bit is computed as known zero, it must be
1719 // zero across all demanded elements of the vector.
1720 //
1721 // For this bitcast, each demanded element of the output is sub-divided
1722 // across a set of smaller vector elements in the source vector. To get
1723 // the known bits for an entire element of the output, compute the known
1724 // bits for each sub-element sequentially. This is done by shifting the
1725 // one-set-bit demanded elements parameter across the sub-elements for
1726 // consecutive calls to computeKnownBits. We are using the demanded
1727 // elements parameter as a mask operator.
1728 //
1729 // The known bits of each sub-element are then inserted into place
1730 // (dependent on endian) to form the full result of known bits.
1731 unsigned SubScale = BitWidth / SubBitWidth;
1732 APInt SubDemandedElts = APInt::getZero(NumElts * SubScale);
1733 for (unsigned i = 0; i != NumElts; ++i) {
1734 if (DemandedElts[i])
1735 SubDemandedElts.setBit(i * SubScale);
1736 }
1737
1738 KnownBits KnownSrc(SubBitWidth);
1739 for (unsigned i = 0; i != SubScale; ++i) {
1740 computeKnownBits(I->getOperand(0), SubDemandedElts.shl(i), KnownSrc, Q,
1741 Depth + 1);
1742 unsigned ShiftElt = IsLE ? i : SubScale - 1 - i;
1743 Known.insertBits(KnownSrc, ShiftElt * SubBitWidth);
1744 }
1745 }
1746 // Look through a cast from wider vector elements to narrow type.
1747 // Examples: v2i64 -> v4i32
1748 if (SubBitWidth % BitWidth == 0) {
1749 unsigned SubScale = SubBitWidth / BitWidth;
1750 KnownBits KnownSrc(SubBitWidth);
1751 APInt SubDemandedElts =
1752 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
1753 computeKnownBits(I->getOperand(0), SubDemandedElts, KnownSrc, Q,
1754 Depth + 1);
1755
1756 Known.setAllConflict();
1757 for (unsigned i = 0; i != NumElts; ++i) {
1758 if (DemandedElts[i]) {
1759 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
1760 unsigned Offset = (Shifts % SubScale) * BitWidth;
1761 Known = Known.intersectWith(KnownSrc.extractBits(BitWidth, Offset));
1762 if (Known.isUnknown())
1763 break;
1764 }
1765 }
1766 }
1767 break;
1768 }
1769 case Instruction::SExt: {
1770 // Compute the bits in the result that are not present in the input.
1771 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
1772
1773 Known = Known.trunc(SrcBitWidth);
1774 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1775 // If the sign bit of the input is known set or clear, then we know the
1776 // top bits of the result.
1777 Known = Known.sext(BitWidth);
1778 break;
1779 }
1780 case Instruction::Shl: {
1783 auto KF = [NUW, NSW](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1784 bool ShAmtNonZero) {
1785 return KnownBits::shl(KnownVal, KnownAmt, NUW, NSW, ShAmtNonZero);
1786 };
1787 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1788 KF);
1789 // X << X is even: X is either zero or shifts by at least one bit. Both
1790 // uses must observe the same value, so X must not be undef.
1791 if (I->getOperand(0) == I->getOperand(1) &&
1792 isGuaranteedNotToBeUndef(I->getOperand(0), Q.AC, Q.CtxI, Q.DT,
1793 Depth + 1))
1794 Known.Zero.setBit(0);
1795
1796 // Trailing zeros of a right-shifted constant never decrease.
1797 const APInt *C;
1798 if (match(I->getOperand(0), m_APInt(C)))
1799 Known.Zero.setLowBits(C->countr_zero());
1800
1801 // shl X, sub(Y, xor(ctlz(X, true), BitWidth-1)) shifts X so that its MSB
1802 // lands at bit Y, when BitWidth is a power of 2.
1803 const APInt *YC;
1804 Value *X = I->getOperand(0);
1805 if (isPowerOf2_32(BitWidth) &&
1806 match(I->getOperand(1),
1808 m_SpecificInt(BitWidth - 1)))) &&
1809 YC->ult(BitWidth - 1)) {
1810 unsigned Y = YC->getZExtValue();
1811 Known.One.setBit(Y);
1812 Known.Zero.setBitsFrom(Y + 1);
1813 }
1814 break;
1815 }
1816 case Instruction::LShr: {
1817 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1818 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1819 bool ShAmtNonZero) {
1820 return KnownBits::lshr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1821 };
1822 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1823 KF);
1824 // Leading zeros of a left-shifted constant never decrease.
1825 const APInt *C;
1826 if (match(I->getOperand(0), m_APInt(C)))
1827 Known.Zero.setHighBits(C->countl_zero());
1828 break;
1829 }
1830 case Instruction::AShr: {
1831 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1832 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1833 bool ShAmtNonZero) {
1834 return KnownBits::ashr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1835 };
1836 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1837 KF);
1838 break;
1839 }
1840 case Instruction::Sub: {
1843 computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW, NUW,
1844 DemandedElts, Known, Known2, Q, Depth);
1845 break;
1846 }
1847 case Instruction::Add: {
1850 computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW, NUW,
1851 DemandedElts, Known, Known2, Q, Depth);
1852 break;
1853 }
1854 case Instruction::SRem:
1855 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1856 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1857 Known = KnownBits::srem(Known, Known2);
1858 break;
1859
1860 case Instruction::URem:
1861 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1862 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1863 Known = KnownBits::urem(Known, Known2);
1864 break;
1865 case Instruction::Alloca:
1866 Known.Zero.setLowBits(Log2(cast<AllocaInst>(I)->getAlign()));
1867 break;
1868 case Instruction::GetElementPtr: {
1869 // Analyze all of the subscripts of this getelementptr instruction
1870 // to determine if we can prove known low zero bits.
1871 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1872 // Accumulate the constant indices in a separate variable
1873 // to minimize the number of calls to computeForAddSub.
1874 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(I->getType());
1875 APInt AccConstIndices(IndexWidth, 0);
1876
1877 auto AddIndexToKnown = [&](KnownBits IndexBits) {
1878 if (IndexWidth == BitWidth) {
1879 // Note that inbounds does *not* guarantee nsw for the addition, as only
1880 // the offset is signed, while the base address is unsigned.
1881 Known = KnownBits::add(Known, IndexBits);
1882 } else {
1883 // If the index width is smaller than the pointer width, only add the
1884 // value to the low bits.
1885 assert(IndexWidth < BitWidth &&
1886 "Index width can't be larger than pointer width");
1887 Known.insertBits(KnownBits::add(Known.trunc(IndexWidth), IndexBits), 0);
1888 }
1889 };
1890
1892 for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
1893 // TrailZ can only become smaller, short-circuit if we hit zero.
1894 if (Known.isUnknown())
1895 break;
1896
1897 Value *Index = I->getOperand(i);
1898
1899 // Handle case when index is zero.
1900 Constant *CIndex = dyn_cast<Constant>(Index);
1901 if (CIndex && CIndex->isNullValue())
1902 continue;
1903
1904 if (StructType *STy = GTI.getStructTypeOrNull()) {
1905 // Handle struct member offset arithmetic.
1906
1907 assert(CIndex &&
1908 "Access to structure field must be known at compile time");
1909
1910 if (CIndex->getType()->isVectorTy())
1911 Index = CIndex->getSplatValue();
1912
1913 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
1914 const StructLayout *SL = Q.DL.getStructLayout(STy);
1915 uint64_t Offset = SL->getElementOffset(Idx);
1916 AccConstIndices += Offset;
1917 continue;
1918 }
1919
1920 // Handle array index arithmetic.
1921 Type *IndexedTy = GTI.getIndexedType();
1922 if (!IndexedTy->isSized()) {
1923 Known.resetAll();
1924 break;
1925 }
1926
1927 TypeSize Stride = GTI.getSequentialElementStride(Q.DL);
1928 uint64_t StrideInBytes = Stride.getKnownMinValue();
1929 if (!Stride.isScalable()) {
1930 // Fast path for constant offset.
1931 if (auto *CI = dyn_cast<ConstantInt>(Index)) {
1932 AccConstIndices +=
1933 CI->getValue().sextOrTrunc(IndexWidth) * StrideInBytes;
1934 continue;
1935 }
1936 }
1937
1938 KnownBits IndexBits =
1939 computeKnownBits(Index, Q, Depth + 1).sextOrTrunc(IndexWidth);
1940 KnownBits ScalingFactor(IndexWidth);
1941 // Multiply by current sizeof type.
1942 // &A[i] == A + i * sizeof(*A[i]).
1943 if (Stride.isScalable()) {
1944 // For scalable types the only thing we know about sizeof is
1945 // that this is a multiple of the minimum size.
1946 ScalingFactor.Zero.setLowBits(llvm::countr_zero(StrideInBytes));
1947 } else {
1948 ScalingFactor =
1949 KnownBits::makeConstant(APInt(IndexWidth, StrideInBytes));
1950 }
1951 AddIndexToKnown(KnownBits::mul(IndexBits, ScalingFactor));
1952 }
1953 if (!Known.isUnknown() && !AccConstIndices.isZero())
1954 AddIndexToKnown(KnownBits::makeConstant(AccConstIndices));
1955 break;
1956 }
1957 case Instruction::PHI: {
1958 const PHINode *P = cast<PHINode>(I);
1959 BinaryOperator *BO = nullptr;
1960 Value *Start = nullptr, *Step = nullptr;
1961 KnownBits &KnownStart = Known2;
1962 if (matchSimpleRecurrence(P, BO, Start, Step)) {
1963 // Handle the case of a simple two-predecessor recurrence PHI.
1964 // There's a lot more that could theoretically be done here, but
1965 // this is sufficient to catch some interesting cases.
1966 unsigned Opcode = BO->getOpcode();
1967
1968 switch (Opcode) {
1969 // If this is a shift recurrence, we know the bits being shifted in. We
1970 // can combine that with information about the start value of the
1971 // recurrence to conclude facts about the result. If this is a udiv
1972 // recurrence, we know that the result can never exceed either the
1973 // numerator or the start value, whichever is greater.
1974 case Instruction::LShr:
1975 case Instruction::AShr:
1976 case Instruction::Shl:
1977 case Instruction::UDiv:
1978 if (BO->getOperand(0) != I)
1979 break;
1980 [[fallthrough]];
1981
1982 // For a urem recurrence, the result can never exceed the start value. The
1983 // phi could either be the numerator or the denominator.
1984 case Instruction::URem: {
1985 // We have matched a recurrence of the form:
1986 // %iv = [R, %entry], [%iv.next, %backedge]
1987 // %iv.next = shift_op %iv, L
1988
1989 // Recurse with the phi context to avoid concern about whether facts
1990 // inferred hold at original context instruction. TODO: It may be
1991 // correct to use the original context. IF warranted, explore and
1992 // add sufficient tests to cover.
1994 RecQ.CtxI = P;
1995 computeKnownBits(Start, DemandedElts, KnownStart, RecQ, Depth + 1);
1996 switch (Opcode) {
1997 case Instruction::Shl:
1998 // A shl recurrence will only increase the tailing zeros
1999 Known.Zero.setLowBits(KnownStart.countMinTrailingZeros());
2000 break;
2001 case Instruction::LShr:
2002 case Instruction::UDiv:
2003 case Instruction::URem:
2004 // lshr, udiv, and urem recurrences will preserve the leading zeros of
2005 // the start value.
2006 Known.Zero.setHighBits(KnownStart.countMinLeadingZeros());
2007 break;
2008 case Instruction::AShr:
2009 // An ashr recurrence will extend the initial sign bit
2010 Known.Zero.setHighBits(KnownStart.countMinLeadingZeros());
2011 Known.One.setHighBits(KnownStart.countMinLeadingOnes());
2012 break;
2013 }
2014 break;
2015 }
2016
2017 case Instruction::And: {
2018 // Bits that are zero in the start value stay zero, and bits that are
2019 // one in both the start value and the step stay one.
2020 KnownBits KnownStep(BitWidth);
2021 computeKnownBitsForRecurrenceOperands(P, Start, Step, DemandedElts,
2022 KnownStart, KnownStep, Q, Depth);
2023 Known.Zero |= KnownStart.Zero;
2024 Known.One |= KnownStart.One & KnownStep.One;
2025 break;
2026 }
2027
2028 case Instruction::Or: {
2029 // Bits that are zero in both the start value and the step stay zero,
2030 // and bits that are one in the start value stay one.
2031 KnownBits KnownStep(BitWidth);
2032 computeKnownBitsForRecurrenceOperands(P, Start, Step, DemandedElts,
2033 KnownStart, KnownStep, Q, Depth);
2034 Known.Zero |= KnownStart.Zero & KnownStep.Zero;
2035 Known.One |= KnownStart.One;
2036 break;
2037 }
2038
2039 // Check for operations that have the property that if
2040 // both their operands have low zero bits, the result
2041 // will have low zero bits.
2042 case Instruction::Add:
2043 case Instruction::Sub:
2044 case Instruction::Mul: {
2045 // Ok, we have a recurrence of the form {Start,op,Step}. Check for low
2046 // zero bits.
2047 KnownBits KnownStep(BitWidth);
2048 computeKnownBitsForRecurrenceOperands(P, Start, Step, DemandedElts,
2049 KnownStart, KnownStep, Q, Depth);
2050
2051 Known.Zero.setLowBits(std::min(KnownStart.countMinTrailingZeros(),
2052 KnownStep.countMinTrailingZeros()));
2053
2054 auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(BO);
2055 if (!OverflowOp || !Q.IIQ.hasNoSignedWrap(OverflowOp))
2056 break;
2057
2058 switch (Opcode) {
2059 // If initial value of recurrence is nonnegative, and we are adding
2060 // a nonnegative number with nsw, the result can only be nonnegative
2061 // or poison value regardless of the number of times we execute the
2062 // add in phi recurrence. If initial value is negative and we are
2063 // adding a negative number with nsw, the result can only be
2064 // negative or poison value. Similar arguments apply to sub and mul.
2065 //
2066 // (add non-negative, non-negative) --> non-negative
2067 // (add negative, negative) --> negative
2068 case Instruction::Add: {
2069 if (KnownStart.isNonNegative() && KnownStep.isNonNegative())
2070 Known.makeNonNegative();
2071 else if (KnownStart.isNegative() && KnownStep.isNegative())
2072 Known.makeNegative();
2073 break;
2074 }
2075
2076 // (sub nsw non-negative, negative) --> non-negative
2077 // (sub nsw negative, non-negative) --> negative
2078 case Instruction::Sub: {
2079 if (BO->getOperand(0) != I)
2080 break;
2081 if (KnownStart.isNonNegative() && KnownStep.isNegative())
2082 Known.makeNonNegative();
2083 else if (KnownStart.isNegative() && KnownStep.isNonNegative())
2084 Known.makeNegative();
2085 break;
2086 }
2087
2088 // (mul nsw non-negative, non-negative) --> non-negative
2089 case Instruction::Mul:
2090 if (KnownStart.isNonNegative() && KnownStep.isNonNegative())
2091 Known.makeNonNegative();
2092 break;
2093
2094 default:
2095 break;
2096 }
2097 break;
2098 }
2099
2100 default:
2101 break;
2102 }
2103 } else {
2104 IntrinsicInst *II = nullptr;
2105 GEPOperator *GEP = nullptr;
2106 if (matchTwoInputRecurrence<IntrinsicInst>(P, II, Start, Step)) {
2107 // %iv = [<Start>, %entry], [%iv.next, %backedge]
2108 //
2109 // %iv.next = <II>(%iv, <Step>)
2110 // or
2111 // %iv.next = <II>(<Step>, %iv)
2112 Intrinsic::ID IntrinsicID = II->getIntrinsicID();
2113 if (IntrinsicID == Intrinsic::umin || IntrinsicID == Intrinsic::umax) {
2114 KnownBits KnownStep(BitWidth);
2116 P, Start, Step, DemandedElts, KnownStart, KnownStep, Q, Depth);
2117
2118 if (IntrinsicID == Intrinsic::umin) {
2119 Known.Zero.setHighBits(KnownStart.countMinLeadingZeros());
2120 Known.One.setHighBits(std::min(KnownStart.countMinLeadingOnes(),
2121 KnownStep.countMinLeadingOnes()));
2122 } else {
2123 // umax
2124 Known.Zero.setHighBits(std::min(KnownStart.countMinLeadingZeros(),
2125 KnownStep.countMinLeadingZeros()));
2126 Known.One.setHighBits(KnownStart.countMinLeadingOnes());
2127 }
2128 }
2129 } else if (Known.isUnknown() && I->getType()->isPointerTy() &&
2131 GEP->getPointerOperand() == P) {
2132 // Handle a pointer induction variable, whose step is a getelementptr:
2133 // %p = phi ptr [ %start, %preheader ], [ %next, %latch ]
2134 // %next = getelementptr <ty>, ptr %p, <indices>
2135 // Every value %p takes is %start plus a whole number of steps, and
2136 // tz(a + b) >= min(tz(a), tz(b)), so the trailing zeros of %p - and
2137 // hence its alignment - are at least the smaller of those of %start and
2138 // of the step.
2139
2140 // Evaluate the start value on its own incoming edge.
2141 unsigned StartIdx = P->getIncomingValue(0) == Start ? 0 : 1;
2143 RecQ.CtxI = P->getIncomingBlock(StartIdx)->getTerminator();
2144 computeKnownBits(Start, DemandedElts, KnownStart, RecQ, Depth + 1);
2145 Known.Zero.setLowBits(
2146 std::min(KnownStart.countMinTrailingZeros(),
2147 Log2(GEP->getMaxPreservedAlignment(Q.DL))));
2148 }
2149 }
2150
2151 // Unreachable blocks may have zero-operand PHI nodes.
2152 if (P->getNumIncomingValues() == 0)
2153 break;
2154
2155 // Otherwise take the unions of the known bit sets of the operands,
2156 // taking conservative care to avoid excessive recursion.
2157 if (Depth < MaxAnalysisRecursionDepth - 1 && Known.isUnknown()) {
2158 // Skip if every incoming value references to ourself.
2159 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
2160 break;
2161
2162 Known.setAllConflict();
2163 for (const Use &U : P->operands()) {
2164 Value *IncValue;
2165 const PHINode *CtxPhi;
2166 Instruction *CtxI;
2167 breakSelfRecursivePHI(&U, P, IncValue, CtxI, &CtxPhi);
2168 // Skip direct self references.
2169 if (IncValue == P)
2170 continue;
2171
2172 // Change the context instruction to the "edge" that flows into the
2173 // phi. This is important because that is where the value is actually
2174 // "evaluated" even though it is used later somewhere else. (see also
2175 // D69571).
2177
2178 Known2 = KnownBits(BitWidth);
2179
2180 // Recurse, but cap the recursion to one level, because we don't
2181 // want to waste time spinning around in loops.
2182 // TODO: See if we can base recursion limiter on number of incoming phi
2183 // edges so we don't overly clamp analysis.
2184 computeKnownBits(IncValue, DemandedElts, Known2, RecQ,
2186
2187 // See if we can further use a conditional branch into the phi
2188 // to help us determine the range of the value.
2189 if (!Known2.isConstant()) {
2191 Value *RHSValue = nullptr;
2192 BasicBlock *TrueSucc, *FalseSucc;
2193 if (match(
2194 RecQ.CtxI,
2195 m_Br(m_c_ICmp(Pred, m_Specific(IncValue), m_Value(RHSValue)),
2196 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
2197 // Check for cases of duplicate successors.
2198 if ((TrueSucc == CtxPhi->getParent()) !=
2199 (FalseSucc == CtxPhi->getParent())) {
2200 // If we're using the false successor, invert the predicate.
2201 if (FalseSucc == CtxPhi->getParent())
2203 // Get the knownbits implied by the incoming phi condition.
2204 KnownBits RHSKnown = computeKnownBits(
2205 RHSValue, DemandedElts, RecQ, MaxAnalysisRecursionDepth - 1);
2206 if (!RHSKnown.isUnknown()) {
2208 RHSKnown, /*IsSigned=*/ICmpInst::isSigned(Pred));
2209 auto CR = ConstantRange::makeAllowedICmpRegion(Pred, RHSRange);
2210 KnownBits KnownUnion = Known2.unionWith(CR.toKnownBits());
2211 // We can have conflicts here if we are analyzing deadcode (its
2212 // impossible for us reach this BB based the icmp).
2213 if (KnownUnion.hasConflict()) {
2214 // No reason to continue analyzing in a known dead region, so
2215 // just resetAll and break. This will cause us to also exit
2216 // the outer loop.
2217 Known.resetAll();
2218 break;
2219 }
2220 Known2 = KnownUnion;
2221 }
2222 }
2223 }
2224 }
2225
2226 Known = Known.intersectWith(Known2);
2227 // If all bits have been ruled out, there's no need to check
2228 // more operands.
2229 if (Known.isUnknown())
2230 break;
2231 }
2232 }
2233 break;
2234 }
2235 case Instruction::Call:
2236 case Instruction::Invoke: {
2237 // If range metadata is attached to this call, set known bits from that,
2238 // and then intersect with known bits based on other properties of the
2239 // function.
2240 if (MDNode *MD =
2241 Q.IIQ.getMetadata(cast<Instruction>(I), LLVMContext::MD_range))
2243
2244 const auto *CB = cast<CallBase>(I);
2245
2246 if (std::optional<ConstantRange> Range = CB->getRange())
2247 Known = Known.unionWith(Range->toKnownBits());
2248
2249 if (const Value *RV = CB->getReturnedArgOperand()) {
2250 if (RV->getType() == I->getType()) {
2251 computeKnownBits(RV, Known2, Q, Depth + 1);
2252 Known = Known.unionWith(Known2);
2253 // If the function doesn't return properly for all input values
2254 // (e.g. unreachable exits) then there might be conflicts between the
2255 // argument value and the range metadata. Simply discard the known bits
2256 // in case of conflicts.
2257 if (Known.hasConflict())
2258 Known.resetAll();
2259 }
2260 }
2261 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
2262 switch (II->getIntrinsicID()) {
2263 default:
2264 break;
2265 case Intrinsic::abs: {
2266 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2267 bool IntMinIsPoison = match(II->getArgOperand(1), m_One());
2268 Known = Known.unionWith(Known2.abs(IntMinIsPoison));
2269 break;
2270 }
2271 case Intrinsic::bitreverse:
2272 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2273 Known = Known.unionWith(Known2.reverseBits());
2274 break;
2275 case Intrinsic::bswap:
2276 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2277 Known = Known.unionWith(Known2.byteSwap());
2278 break;
2279 case Intrinsic::ctlz: {
2280 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2281 bool ZeroIsPoison = match(II->getArgOperand(1), m_One());
2282 Known = Known.unionWith(Known2.ctlz(ZeroIsPoison));
2283 break;
2284 }
2285 case Intrinsic::cttz: {
2286 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2287 // If we have a known 1, its position is our upper bound.
2288 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
2289 // If this call is poison for 0 input, the result will be less than 2^n.
2290 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
2291 PossibleTZ = std::min(PossibleTZ, BitWidth - 1);
2292 unsigned LowBits = llvm::bit_width(PossibleTZ);
2293 Known.Zero.setBitsFrom(LowBits);
2294 break;
2295 }
2296 case Intrinsic::ctpop: {
2297 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2298 // We can bound the space the count needs. Also, bits known to be zero
2299 // can't contribute to the population.
2300 unsigned BitsPossiblySet = Known2.countMaxPopulation();
2301 unsigned LowBits = llvm::bit_width(BitsPossiblySet);
2302 Known.Zero.setBitsFrom(LowBits);
2303 // TODO: we could bound KnownOne using the lower bound on the number
2304 // of bits which might be set provided by popcnt KnownOne2.
2305 break;
2306 }
2307 case Intrinsic::fshr:
2308 case Intrinsic::fshl: {
2309 const APInt *SA;
2310 if (!match(I->getOperand(2), m_APInt(SA)))
2311 break;
2312
2313 KnownBits Known3(BitWidth);
2314 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2315 computeKnownBits(I->getOperand(1), DemandedElts, Known3, Q, Depth + 1);
2316 Known = II->getIntrinsicID() == Intrinsic::fshl
2317 ? KnownBits::fshl(Known2, Known3, *SA)
2318 : KnownBits::fshr(Known2, Known3, *SA);
2319 break;
2320 }
2321 case Intrinsic::clmul:
2322 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2323 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2324 Known = KnownBits::clmul(Known, Known2);
2325 break;
2326 case Intrinsic::pext:
2327 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2328 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2329 Known = KnownBits::pext(Known, Known2);
2330 break;
2331 case Intrinsic::pdep:
2332 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2333 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2334 Known = KnownBits::pdep(Known, Known2);
2335 break;
2336 case Intrinsic::smulh:
2337 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2338 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2339 Known = KnownBits::mulhs(Known, Known2);
2340 break;
2341 case Intrinsic::umulh:
2342 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2343 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2344 Known = KnownBits::mulhu(Known, Known2);
2345 break;
2346 case Intrinsic::uadd_sat:
2347 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2348 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2349 Known = KnownBits::uadd_sat(Known, Known2);
2350 break;
2351 case Intrinsic::usub_sat:
2352 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2353 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2354 Known = KnownBits::usub_sat(Known, Known2);
2355 break;
2356 case Intrinsic::sadd_sat:
2357 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2358 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2359 Known = KnownBits::sadd_sat(Known, Known2);
2360 break;
2361 case Intrinsic::ssub_sat:
2362 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2363 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2364 Known = KnownBits::ssub_sat(Known, Known2);
2365 break;
2366 // Vec reverse preserves bits from input vec.
2367 case Intrinsic::vector_reverse:
2368 computeKnownBits(I->getOperand(0), DemandedElts.reverseBits(), Known, Q,
2369 Depth + 1);
2370 break;
2371 // for min/max/and/or reduce, any bit common to each element in the
2372 // input vec is set in the output.
2373 case Intrinsic::vector_reduce_and:
2374 case Intrinsic::vector_reduce_or:
2375 case Intrinsic::vector_reduce_umax:
2376 case Intrinsic::vector_reduce_umin:
2377 case Intrinsic::vector_reduce_smax:
2378 case Intrinsic::vector_reduce_smin:
2379 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2380 break;
2381 case Intrinsic::vector_reduce_xor: {
2382 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2383 // The zeros common to all vecs are zero in the output.
2384 // If the number of elements is odd, then the common ones remain. If the
2385 // number of elements is even, then the common ones becomes zeros.
2386 auto *VecTy = cast<VectorType>(I->getOperand(0)->getType());
2387 // Even, so the ones become zeros.
2388 bool EvenCnt = VecTy->getElementCount().isKnownEven();
2389 if (EvenCnt)
2390 Known.Zero |= Known.One;
2391 // Maybe even element count so need to clear ones.
2392 if (VecTy->isScalableTy() || EvenCnt)
2393 Known.One.clearAllBits();
2394 break;
2395 }
2396 case Intrinsic::vector_reduce_add: {
2397 auto *VecTy = dyn_cast<FixedVectorType>(I->getOperand(0)->getType());
2398 if (!VecTy)
2399 break;
2400 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2401 Known = Known.reduceAdd(VecTy->getNumElements());
2402 break;
2403 }
2404 case Intrinsic::umin:
2405 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2406 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2407 Known = KnownBits::umin(Known, Known2);
2408 break;
2409 case Intrinsic::umax:
2410 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2411 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2412 Known = KnownBits::umax(Known, Known2);
2413 break;
2414 case Intrinsic::smin:
2415 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2416 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2417 Known = KnownBits::smin(Known, Known2);
2419 break;
2420 case Intrinsic::smax:
2421 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2422 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2423 Known = KnownBits::smax(Known, Known2);
2425 break;
2426 case Intrinsic::ptrmask: {
2427 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2428
2429 const Value *Mask = I->getOperand(1);
2430 Known2 = KnownBits(Mask->getType()->getScalarSizeInBits());
2431 computeKnownBits(Mask, DemandedElts, Known2, Q, Depth + 1);
2432 // TODO: 1-extend would be more precise.
2433 Known &= Known2.anyextOrTrunc(BitWidth);
2434 break;
2435 }
2436 case Intrinsic::x86_sse42_crc32_64_64:
2437 Known.Zero.setBitsFrom(32);
2438 break;
2439 case Intrinsic::x86_ssse3_phadd_d_128:
2440 case Intrinsic::x86_ssse3_phadd_w_128:
2441 case Intrinsic::x86_avx2_phadd_d:
2442 case Intrinsic::x86_avx2_phadd_w: {
2444 I, DemandedElts, Q, Depth,
2445 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2446 return KnownBits::add(KnownLHS, KnownRHS);
2447 });
2448 break;
2449 }
2450 case Intrinsic::x86_ssse3_phadd_sw_128:
2451 case Intrinsic::x86_avx2_phadd_sw: {
2453 I, DemandedElts, Q, Depth, KnownBits::sadd_sat);
2454 break;
2455 }
2456 case Intrinsic::x86_ssse3_phsub_d_128:
2457 case Intrinsic::x86_ssse3_phsub_w_128:
2458 case Intrinsic::x86_avx2_phsub_d:
2459 case Intrinsic::x86_avx2_phsub_w: {
2461 I, DemandedElts, Q, Depth,
2462 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2463 return KnownBits::sub(KnownLHS, KnownRHS);
2464 });
2465 break;
2466 }
2467 case Intrinsic::x86_ssse3_phsub_sw_128:
2468 case Intrinsic::x86_avx2_phsub_sw: {
2470 I, DemandedElts, Q, Depth, KnownBits::ssub_sat);
2471 break;
2472 }
2473 case Intrinsic::riscv_vsetvli:
2474 case Intrinsic::riscv_vsetvlimax: {
2475 bool HasAVL = II->getIntrinsicID() == Intrinsic::riscv_vsetvli;
2476 const ConstantRange Range = getVScaleRange(II->getFunction(), BitWidth);
2478 cast<ConstantInt>(II->getArgOperand(HasAVL))->getZExtValue());
2479 RISCVVType::VLMUL VLMUL = static_cast<RISCVVType::VLMUL>(
2480 cast<ConstantInt>(II->getArgOperand(1 + HasAVL))->getZExtValue());
2481 uint64_t MaxVLEN =
2482 Range.getUnsignedMax().getZExtValue() * RISCV::RVVBitsPerBlock;
2483 uint64_t MaxVL = MaxVLEN / RISCVVType::getSEWLMULRatio(SEW, VLMUL);
2484
2485 // Result of vsetvli must be not larger than AVL.
2486 if (HasAVL)
2487 if (auto *CI = dyn_cast<ConstantInt>(II->getArgOperand(0)))
2488 MaxVL = std::min(MaxVL, CI->getZExtValue());
2489
2490 unsigned KnownZeroFirstBit = Log2_32(MaxVL) + 1;
2491 if (BitWidth > KnownZeroFirstBit)
2492 Known.Zero.setBitsFrom(KnownZeroFirstBit);
2493 break;
2494 }
2495 case Intrinsic::amdgcn_mbcnt_hi:
2496 case Intrinsic::amdgcn_mbcnt_lo: {
2497 // Wave64 mbcnt_lo returns at most 32 + src1. Otherwise these return at
2498 // most 31 + src1.
2499 KnownBits MbcntKnown(BitWidth);
2500 MbcntKnown.Zero.setBitsFrom(
2501 II->getIntrinsicID() == Intrinsic::amdgcn_mbcnt_lo ? 6 : 5);
2502 computeKnownBits(I->getOperand(1), Known2, Q, Depth + 1);
2503 Known = Known.unionWith(KnownBits::add(MbcntKnown, Known2));
2504 break;
2505 }
2506 case Intrinsic::vscale: {
2507 if (!II->getParent() || !II->getFunction())
2508 break;
2509
2510 Known = getVScaleRange(II->getFunction(), BitWidth).toKnownBits();
2511 break;
2512 }
2513 case Intrinsic::stepvector: {
2514 auto *VecTy = cast<VectorType>(II->getType());
2515 unsigned MinNumElts = VecTy->getElementCount().getKnownMinValue();
2516 if (!isUIntN(BitWidth, MinNumElts))
2517 break;
2518
2519 bool Overflow = false;
2520 APInt MaxNumElts(BitWidth, MinNumElts);
2521 if (VecTy->isScalableTy()) {
2522 if (!II->getParent() || !II->getFunction())
2523 break;
2524 MaxNumElts = getVScaleRange(II->getFunction(), BitWidth)
2526 .umul_ov(MaxNumElts, Overflow);
2527 }
2528
2529 // Give up if the lane count could wrap. Stepvector truncates lane
2530 // indices that do not fit in the element type.
2531 if (Overflow)
2532 break;
2533
2534 Known.Zero.setHighBits((MaxNumElts - 1).countl_zero());
2535 break;
2536 }
2537 }
2538 }
2539 break;
2540 }
2541 case Instruction::ShuffleVector: {
2542 if (auto *Splat = getSplatValue(I)) {
2544 break;
2545 }
2546
2547 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
2548 // FIXME: Do we need to handle ConstantExpr involving shufflevectors?
2549 if (!Shuf) {
2550 Known.resetAll();
2551 return;
2552 }
2553 // For undef elements, we don't know anything about the common state of
2554 // the shuffle result.
2555 APInt DemandedLHS, DemandedRHS;
2556 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) {
2557 Known.resetAll();
2558 return;
2559 }
2560 Known.setAllConflict();
2561 if (!!DemandedLHS) {
2562 const Value *LHS = Shuf->getOperand(0);
2563 computeKnownBits(LHS, DemandedLHS, Known, Q, Depth + 1);
2564 // If we don't know any bits, early out.
2565 if (Known.isUnknown())
2566 break;
2567 }
2568 if (!!DemandedRHS) {
2569 const Value *RHS = Shuf->getOperand(1);
2570 computeKnownBits(RHS, DemandedRHS, Known2, Q, Depth + 1);
2571 Known = Known.intersectWith(Known2);
2572 }
2573 break;
2574 }
2575 case Instruction::InsertElement: {
2576 if (isa<ScalableVectorType>(I->getType())) {
2577 Known.resetAll();
2578 return;
2579 }
2580 const Value *Vec = I->getOperand(0);
2581 const Value *Elt = I->getOperand(1);
2582 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
2583 unsigned NumElts = DemandedElts.getBitWidth();
2584 APInt DemandedVecElts = DemandedElts;
2585 bool NeedsElt = true;
2586 // If we know the index we are inserting too, clear it from Vec check.
2587 if (CIdx && CIdx->getValue().ult(NumElts)) {
2588 DemandedVecElts.clearBit(CIdx->getZExtValue());
2589 NeedsElt = DemandedElts[CIdx->getZExtValue()];
2590 }
2591
2592 Known.setAllConflict();
2593 if (NeedsElt) {
2594 computeKnownBits(Elt, Known, Q, Depth + 1);
2595 // If we don't know any bits, early out.
2596 if (Known.isUnknown())
2597 break;
2598 }
2599
2600 if (!DemandedVecElts.isZero()) {
2601 computeKnownBits(Vec, DemandedVecElts, Known2, Q, Depth + 1);
2602 Known = Known.intersectWith(Known2);
2603 }
2604 break;
2605 }
2606 case Instruction::ExtractElement: {
2607 // Look through extract element. If the index is non-constant or
2608 // out-of-range demand all elements, otherwise just the extracted element.
2609 const Value *Vec = I->getOperand(0);
2610 const Value *Idx = I->getOperand(1);
2611 auto *CIdx = dyn_cast<ConstantInt>(Idx);
2612 if (isa<ScalableVectorType>(Vec->getType())) {
2613 // FIXME: there's probably *something* we can do with scalable vectors
2614 Known.resetAll();
2615 break;
2616 }
2617 unsigned NumElts = cast<FixedVectorType>(Vec->getType())->getNumElements();
2618 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
2619 if (CIdx && CIdx->getValue().ult(NumElts))
2620 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
2621 computeKnownBits(Vec, DemandedVecElts, Known, Q, Depth + 1);
2622 break;
2623 }
2624 case Instruction::ExtractValue:
2625 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
2627 if (EVI->getNumIndices() != 1) break;
2628 if (EVI->getIndices()[0] == 0) {
2629 switch (II->getIntrinsicID()) {
2630 default: break;
2631 case Intrinsic::uadd_with_overflow:
2632 case Intrinsic::sadd_with_overflow:
2634 true, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2635 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2636 break;
2637 case Intrinsic::usub_with_overflow:
2638 case Intrinsic::ssub_with_overflow:
2640 false, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2641 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2642 break;
2643 case Intrinsic::umul_with_overflow:
2644 case Intrinsic::smul_with_overflow:
2645 computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false,
2646 false, DemandedElts, Known, Known2, Q, Depth);
2647 break;
2648 }
2649 }
2650 }
2651 break;
2652 case Instruction::Freeze:
2653 if (isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CtxI, Q.DT,
2654 Depth + 1))
2655 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2656 break;
2657 }
2658}
2659
2660/// Determine which bits of V are known to be either zero or one and return
2661/// them.
2662KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
2663 const SimplifyQuery &Q, unsigned Depth) {
2664 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2665 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);
2666 return Known;
2667}
2668
2669/// Determine which bits of V are known to be either zero or one and return
2670/// them.
2672 unsigned Depth) {
2673 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2675 return Known;
2676}
2677
2678/// Determine which bits of V are known to be either zero or one and return
2679/// them in the Known bit set.
2680///
2681/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that
2682/// we cannot optimize based on the assumption that it is zero without changing
2683/// it to be an explicit zero. If we don't change it to zero, other code could
2684/// optimized based on the contradictory assumption that it is non-zero.
2685/// Because instcombine aggressively folds operations with undef args anyway,
2686/// this won't lose us code quality.
2687///
2688/// This function is defined on values with integer type, values with pointer
2689/// type, and vectors of integers. In the case
2690/// where V is a vector, known zero, and known one values are the
2691/// same width as the vector element, and the bit is set only if it is true
2692/// for all of the demanded elements in the vector specified by DemandedElts.
2693void computeKnownBits(const Value *V, const APInt &DemandedElts,
2694 KnownBits &Known, const SimplifyQuery &Q,
2695 unsigned Depth) {
2696 if (!DemandedElts) {
2697 // No demanded elts, better to assume we don't know anything.
2698 Known.resetAll();
2699 return;
2700 }
2701
2702 assert(V && "No Value?");
2703 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2704
2705#ifndef NDEBUG
2706 Type *Ty = V->getType();
2707 unsigned BitWidth = Known.getBitWidth();
2708
2709 assert((Ty->isIntOrIntVectorTy(BitWidth) || Ty->isPtrOrPtrVectorTy()) &&
2710 "Not integer or pointer type!");
2711
2712 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
2713 assert(
2714 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
2715 "DemandedElt width should equal the fixed vector number of elements");
2716 } else {
2717 assert(DemandedElts == APInt(1, 1) &&
2718 "DemandedElt width should be 1 for scalars or scalable vectors");
2719 }
2720
2721 Type *ScalarTy = Ty->getScalarType();
2722 if (ScalarTy->isPointerTy()) {
2723 assert(BitWidth == Q.DL.getPointerTypeSizeInBits(ScalarTy) &&
2724 "V and Known should have same BitWidth");
2725 } else {
2726 assert(BitWidth == Q.DL.getTypeSizeInBits(ScalarTy) &&
2727 "V and Known should have same BitWidth");
2728 }
2729#endif
2730
2731 const APInt *C;
2732 if (match(V, m_APInt(C))) {
2733 // We know all of the bits for a scalar constant or a splat vector constant!
2735 return;
2736 }
2737 // Null and aggregate-zero are all-zeros.
2739 Known.setAllZero();
2740 return;
2741 }
2742 // Handle a constant vector by taking the intersection of the known bits of
2743 // each element.
2745 assert(!isa<ScalableVectorType>(V->getType()));
2746 // We know that CDV must be a vector of integers. Take the intersection of
2747 // each element.
2748 Known.setAllConflict();
2749 for (unsigned i = 0, e = CDV->getNumElements(); i != e; ++i) {
2750 if (!DemandedElts[i])
2751 continue;
2752 APInt Elt = CDV->getElementAsAPInt(i);
2753 Known.Zero &= ~Elt;
2754 Known.One &= Elt;
2755 }
2756 if (Known.hasConflict())
2757 Known.resetAll();
2758 return;
2759 }
2760
2761 if (const auto *CV = dyn_cast<ConstantVector>(V)) {
2762 assert(!isa<ScalableVectorType>(V->getType()));
2763 // We know that CV must be a vector of integers. Take the intersection of
2764 // each element.
2765 Known.setAllConflict();
2766 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
2767 if (!DemandedElts[i])
2768 continue;
2769 Constant *Element = CV->getAggregateElement(i);
2770 if (isa<PoisonValue>(Element))
2771 continue;
2772 auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
2773 if (!ElementCI) {
2774 Known.resetAll();
2775 return;
2776 }
2777 const APInt &Elt = ElementCI->getValue();
2778 Known.Zero &= ~Elt;
2779 Known.One &= Elt;
2780 }
2781 if (Known.hasConflict())
2782 Known.resetAll();
2783 return;
2784 }
2785
2786 // Start out not knowing anything.
2787 Known.resetAll();
2788
2789 // We can't imply anything about undefs.
2790 if (isa<UndefValue>(V))
2791 return;
2792
2793 // There's no point in looking through other users of ConstantData for
2794 // assumptions. Confirm that we've handled them all.
2795 assert(!isa<ConstantData>(V) && "Unhandled constant data!");
2796
2797 if (const auto *A = dyn_cast<Argument>(V))
2798 if (std::optional<ConstantRange> Range = A->getRange())
2799 Known = Range->toKnownBits();
2800
2801 // All recursive calls that increase depth must come after this.
2803 return;
2804
2805 // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
2806 // the bits of its aliasee.
2807 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
2808 if (!GA->isInterposable())
2809 computeKnownBits(GA->getAliasee(), Known, Q, Depth + 1);
2810 return;
2811 }
2812
2813 if (const Operator *I = dyn_cast<Operator>(V))
2814 computeKnownBitsFromOperator(I, DemandedElts, Known, Q, Depth);
2815 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2816 if (std::optional<ConstantRange> CR = GV->getAbsoluteSymbolRange())
2817 Known = CR->toKnownBits();
2818 }
2819
2820 // Aligned pointers have trailing zeros - refine Known.Zero set
2821 if (isa<PointerType>(V->getType())) {
2822 Align Alignment = V->getPointerAlignment(Q.DL);
2823 Known.Zero.setLowBits(Log2(Alignment));
2824 }
2825
2826 // computeKnownBitsFromContext strictly refines Known.
2827 // Therefore, we run them after computeKnownBitsFromOperator.
2828
2829 // Check whether we can determine known bits from context such as assumes.
2831}
2832
2833/// Try to detect a recurrence that the value of the induction variable is
2834/// always a power of two (or zero).
2835static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero,
2836 SimplifyQuery &Q, unsigned Depth) {
2837 BinaryOperator *BO = nullptr;
2838 Value *Start = nullptr, *Step = nullptr;
2839 if (!matchSimpleRecurrence(PN, BO, Start, Step))
2840 return false;
2841
2842 // Initial value must be a power of two.
2843 for (const Use &U : PN->operands()) {
2844 if (U.get() == Start) {
2845 // Initial value comes from a different BB, need to adjust context
2846 // instruction for analysis.
2847 Q.CtxI = PN->getIncomingBlock(U)->getTerminator();
2848 if (!isKnownToBeAPowerOfTwo(Start, OrZero, Q, Depth))
2849 return false;
2850 }
2851 }
2852
2853 // Except for Mul, the induction variable must be on the left side of the
2854 // increment expression, otherwise its value can be arbitrary.
2855 if (BO->getOpcode() != Instruction::Mul && BO->getOperand(1) != Step)
2856 return false;
2857
2858 Q.CtxI = BO->getParent()->getTerminator();
2859 switch (BO->getOpcode()) {
2860 case Instruction::Mul:
2861 // Power of two is closed under multiplication.
2862 return (OrZero || Q.IIQ.hasNoUnsignedWrap(BO) ||
2863 Q.IIQ.hasNoSignedWrap(BO)) &&
2864 isKnownToBeAPowerOfTwo(Step, OrZero, Q, Depth);
2865 case Instruction::SDiv:
2866 // Start value must not be signmask for signed division, so simply being a
2867 // power of two is not sufficient, and it has to be a constant.
2868 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2869 return false;
2870 [[fallthrough]];
2871 case Instruction::UDiv:
2872 // Divisor must be a power of two.
2873 // If OrZero is false, cannot guarantee induction variable is non-zero after
2874 // division, same for Shr, unless it is exact division.
2875 return (OrZero || Q.IIQ.isExact(BO)) &&
2876 isKnownToBeAPowerOfTwo(Step, false, Q, Depth);
2877 case Instruction::Shl:
2878 return OrZero || Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO);
2879 case Instruction::AShr:
2880 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2881 return false;
2882 [[fallthrough]];
2883 case Instruction::LShr:
2884 return OrZero || Q.IIQ.isExact(BO);
2885 default:
2886 return false;
2887 }
2888}
2889
2890/// Return true if we can infer that \p V is known to be a power of 2 from
2891/// dominating condition \p Cond (e.g., ctpop(V) == 1).
2892static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero,
2893 const Value *Cond,
2894 bool CondIsTrue) {
2896 const APInt *RHSC;
2897 if (!match(Cond, m_ICmp(Pred, m_Ctpop(m_Specific(V)), m_APInt(RHSC))))
2898 return false;
2899 if (!CondIsTrue)
2901 // ctpop(V) u< 2
2902 if (OrZero && Pred == ICmpInst::ICMP_ULT && *RHSC == 2)
2903 return true;
2904 // ctpop(V) == 1
2905 return Pred == ICmpInst::ICMP_EQ && *RHSC == 1;
2906}
2907
2908/// Return true if the given value is known to have exactly one
2909/// bit set when defined. For vectors return true if every element is known to
2910/// be a power of two when defined. Supports values with integer or pointer
2911/// types and vectors of integers.
2912bool llvm::isKnownToBeAPowerOfTwo(const Value *V, bool OrZero,
2913 const SimplifyQuery &Q, unsigned Depth) {
2914 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2915
2916 if (isa<Constant>(V))
2917 return OrZero ? match(V, m_Power2OrZero()) : match(V, m_Power2());
2918
2919 // i1 is by definition a power of 2 or zero.
2920 if (OrZero && V->getType()->getScalarSizeInBits() == 1)
2921 return true;
2922
2923 // Try to infer from assumptions.
2924 if (Q.AC && Q.CtxI) {
2925 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
2926 if (!AssumeVH)
2927 continue;
2928 CallInst *I = cast<CallInst>(AssumeVH);
2929 if (isImpliedToBeAPowerOfTwoFromCond(V, OrZero, I->getArgOperand(0),
2930 /*CondIsTrue=*/true) &&
2932 return true;
2933 }
2934 }
2935
2936 // Handle dominating conditions.
2937 if (Q.DC && Q.CtxI && Q.DT) {
2938 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
2939 Value *Cond = BI->getCondition();
2940
2941 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
2943 /*CondIsTrue=*/true) &&
2944 Q.DT->dominates(Edge0, Q.CtxI->getParent()))
2945 return true;
2946
2947 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
2949 /*CondIsTrue=*/false) &&
2950 Q.DT->dominates(Edge1, Q.CtxI->getParent()))
2951 return true;
2952 }
2953 }
2954
2955 auto *I = dyn_cast<Instruction>(V);
2956 if (!I)
2957 return false;
2958
2959 if (Q.CtxI && match(V, m_VScale())) {
2960 const Function *F = Q.CtxI->getFunction();
2961 // The vscale_range indicates vscale is a power-of-two.
2962 return F->hasFnAttribute(Attribute::VScaleRange);
2963 }
2964
2965 // 1 << X is clearly a power of two if the one is not shifted off the end. If
2966 // it is shifted off the end then the result is undefined.
2967 if (match(I, m_Shl(m_One(), m_Value())))
2968 return true;
2969
2970 // (signmask) >>l X is clearly a power of two if the one is not shifted off
2971 // the bottom. If it is shifted off the bottom then the result is undefined.
2972 if (match(I, m_LShr(m_SignMask(), m_Value())))
2973 return true;
2974
2975 // The remaining tests are all recursive, so bail out if we hit the limit.
2977 return false;
2978
2979 switch (I->getOpcode()) {
2980 case Instruction::ZExt:
2981 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2982 case Instruction::Trunc:
2983 return OrZero && isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2984 case Instruction::Shl:
2985 if (OrZero || Q.IIQ.hasNoUnsignedWrap(I) || Q.IIQ.hasNoSignedWrap(I))
2986 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2987 return false;
2988 case Instruction::LShr:
2989 if (OrZero || Q.IIQ.isExact(cast<BinaryOperator>(I)))
2990 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2991 return false;
2992 case Instruction::UDiv:
2994 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2995 return false;
2996 case Instruction::Mul:
2997 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
2998 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth) &&
2999 (OrZero || isKnownNonZero(I, Q, Depth));
3000 case Instruction::And:
3001 // A power of two and'd with anything is a power of two or zero.
3002 if (OrZero &&
3003 (isKnownToBeAPowerOfTwo(I->getOperand(1), /*OrZero*/ true, Q, Depth) ||
3004 isKnownToBeAPowerOfTwo(I->getOperand(0), /*OrZero*/ true, Q, Depth)))
3005 return true;
3006 // X & (-X) is always a power of two or zero.
3007 if (match(I->getOperand(0), m_Neg(m_Specific(I->getOperand(1)))) ||
3008 match(I->getOperand(1), m_Neg(m_Specific(I->getOperand(0)))))
3009 return OrZero || isKnownNonZero(I->getOperand(0), Q, Depth);
3010 return false;
3011 case Instruction::Add: {
3012 // Adding a power-of-two or zero to the same power-of-two or zero yields
3013 // either the original power-of-two, a larger power-of-two or zero.
3015 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) ||
3016 Q.IIQ.hasNoSignedWrap(VOBO)) {
3017 if (match(I->getOperand(0),
3018 m_c_And(m_Specific(I->getOperand(1)), m_Value())) &&
3019 isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth))
3020 return true;
3021 if (match(I->getOperand(1),
3022 m_c_And(m_Specific(I->getOperand(0)), m_Value())) &&
3023 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth))
3024 return true;
3025
3026 unsigned BitWidth = V->getType()->getScalarSizeInBits();
3027 KnownBits LHSBits(BitWidth);
3028 computeKnownBits(I->getOperand(0), LHSBits, Q, Depth);
3029
3030 KnownBits RHSBits(BitWidth);
3031 computeKnownBits(I->getOperand(1), RHSBits, Q, Depth);
3032 // If i8 V is a power of two or zero:
3033 // ZeroBits: 1 1 1 0 1 1 1 1
3034 // ~ZeroBits: 0 0 0 1 0 0 0 0
3035 if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2())
3036 // If OrZero isn't set, we cannot give back a zero result.
3037 // Make sure either the LHS or RHS has a bit set.
3038 if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue())
3039 return true;
3040 }
3041
3042 // LShr(UINT_MAX, Y) + 1 is a power of two (if add is nuw) or zero.
3043 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO))
3044 if (match(I, m_Add(m_LShr(m_AllOnes(), m_Value()), m_One())))
3045 return true;
3046 return false;
3047 }
3048 case Instruction::Select:
3049 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
3050 isKnownToBeAPowerOfTwo(I->getOperand(2), OrZero, Q, Depth);
3051 case Instruction::PHI: {
3052 // A PHI node is power of two if all incoming values are power of two, or if
3053 // it is an induction variable where in each step its value is a power of
3054 // two.
3055 auto *PN = cast<PHINode>(I);
3057
3058 // Check if it is an induction variable and always power of two.
3059 if (isPowerOfTwoRecurrence(PN, OrZero, RecQ, Depth))
3060 return true;
3061
3062 // Recursively check all incoming values. Limit recursion to 2 levels, so
3063 // that search complexity is limited to number of operands^2.
3064 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
3065 return llvm::all_of(PN->operands(), [&](const Use &U) {
3066 // Value is power of 2 if it is coming from PHI node itself by induction.
3067 if (U.get() == PN)
3068 return true;
3069
3070 // Change the context instruction to the incoming block where it is
3071 // evaluated.
3072 RecQ.CtxI = PN->getIncomingBlock(U)->getTerminator();
3073 return isKnownToBeAPowerOfTwo(U.get(), OrZero, RecQ, NewDepth);
3074 });
3075 }
3076 case Instruction::Invoke:
3077 case Instruction::Call: {
3078 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
3079 switch (II->getIntrinsicID()) {
3080 case Intrinsic::umax:
3081 case Intrinsic::smax:
3082 case Intrinsic::umin:
3083 case Intrinsic::smin:
3084 return isKnownToBeAPowerOfTwo(II->getArgOperand(1), OrZero, Q, Depth) &&
3085 isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
3086 // bswap/bitreverse just move around bits, but don't change any 1s/0s
3087 // thus dont change pow2/non-pow2 status.
3088 case Intrinsic::bitreverse:
3089 case Intrinsic::bswap:
3090 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
3091 case Intrinsic::fshr:
3092 case Intrinsic::fshl:
3093 // If Op0 == Op1, this is a rotate. is_pow2(rotate(x, y)) == is_pow2(x)
3094 if (II->getArgOperand(0) == II->getArgOperand(1))
3095 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
3096 break;
3097 case Intrinsic::riscv_vsetvlimax:
3098 // VLMAX is VLEN * LMUL / SEW, which is always a non-zero power of two
3099 // for any valid vtype, so it is a power of two regardless of OrZero.
3100 return true;
3101 case Intrinsic::read_register:
3102 case Intrinsic::read_volatile_register: {
3103 // The RISC-V vlenb CSR holds VLEN/8, which is always a non-zero power
3104 // of two, so it is a power of two regardless of OrZero.
3105 const Module *M = II->getModule();
3106 if (!M || !M->getTargetTriple().isRISCV())
3107 break;
3108 return isReadVLENB(*II);
3109 }
3110 default:
3111 break;
3112 }
3113 }
3114 return false;
3115 }
3116 default:
3117 return false;
3118 }
3119}
3120
3121/// Test whether a GEP's result is known to be non-null.
3122///
3123/// Uses properties inherent in a GEP to try to determine whether it is known
3124/// to be non-null.
3125///
3126/// Currently this routine does not support vector GEPs.
3127static bool isGEPKnownNonNull(const GEPOperator *GEP, const SimplifyQuery &Q,
3128 unsigned Depth) {
3129 const Function *F = nullptr;
3130 if (const Instruction *I = dyn_cast<Instruction>(GEP))
3131 F = I->getFunction();
3132
3133 // If the gep is nuw or inbounds with invalid null pointer, then the GEP
3134 // may be null iff the base pointer is null and the offset is zero.
3135 if (!GEP->hasNoUnsignedWrap() &&
3136 !(GEP->isInBounds() &&
3137 !NullPointerIsDefined(F, GEP->getPointerAddressSpace())))
3138 return false;
3139
3140 // FIXME: Support vector-GEPs.
3141 assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP");
3142
3143 // If the base pointer is non-null, we cannot walk to a null address with an
3144 // inbounds GEP in address space zero.
3145 if (isKnownNonZero(GEP->getPointerOperand(), Q, Depth))
3146 return true;
3147
3148 // Walk the GEP operands and see if any operand introduces a non-zero offset.
3149 // If so, then the GEP cannot produce a null pointer, as doing so would
3150 // inherently violate the inbounds contract within address space zero.
3152 GTI != GTE; ++GTI) {
3153 // Struct types are easy -- they must always be indexed by a constant.
3154 if (StructType *STy = GTI.getStructTypeOrNull()) {
3155 ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
3156 unsigned ElementIdx = OpC->getZExtValue();
3157 const StructLayout *SL = Q.DL.getStructLayout(STy);
3158 uint64_t ElementOffset = SL->getElementOffset(ElementIdx);
3159 if (ElementOffset > 0)
3160 return true;
3161 continue;
3162 }
3163
3164 // If we have a zero-sized type, the index doesn't matter. Keep looping.
3165 if (GTI.getSequentialElementStride(Q.DL).isZero())
3166 continue;
3167
3168 // Fast path the constant operand case both for efficiency and so we don't
3169 // increment Depth when just zipping down an all-constant GEP.
3170 if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
3171 if (!OpC->isZero())
3172 return true;
3173 continue;
3174 }
3175
3176 // We post-increment Depth here because while isKnownNonZero increments it
3177 // as well, when we pop back up that increment won't persist. We don't want
3178 // to recurse 10k times just because we have 10k GEP operands. We don't
3179 // bail completely out because we want to handle constant GEPs regardless
3180 // of depth.
3182 continue;
3183
3184 if (isKnownNonZero(GTI.getOperand(), Q, Depth))
3185 return true;
3186 }
3187
3188 return false;
3189}
3190
3192 const Instruction *CtxI,
3193 const DominatorTree *DT) {
3194 assert(!isa<Constant>(V) && "Called for constant?");
3195
3196 if (!CtxI || !DT)
3197 return false;
3198
3199 unsigned NumUsesExplored = 0;
3200 for (auto &U : V->uses()) {
3201 // Avoid massive lists
3202 if (NumUsesExplored >= DomConditionsMaxUses)
3203 break;
3204 NumUsesExplored++;
3205
3206 const Instruction *UI = cast<Instruction>(U.getUser());
3207 // If the value is used as an argument to a call or invoke, then argument
3208 // attributes may provide an answer about null-ness.
3209 if (V->getType()->isPointerTy()) {
3210 if (const auto *CB = dyn_cast<CallBase>(UI)) {
3211 if (CB->isArgOperand(&U) &&
3212 CB->paramHasNonNullAttr(CB->getArgOperandNo(&U),
3213 /*AllowUndefOrPoison=*/false) &&
3214 DT->dominates(CB, CtxI))
3215 return true;
3216 }
3217 }
3218
3219 // If the value is used as a load/store, then the pointer must be non null.
3220 if (V == getLoadStorePointerOperand(UI)) {
3222 V->getType()->getPointerAddressSpace()) &&
3223 DT->dominates(UI, CtxI))
3224 return true;
3225 }
3226
3227 if ((match(UI, m_IDiv(m_Value(), m_Specific(V))) ||
3228 match(UI, m_IRem(m_Value(), m_Specific(V)))) &&
3229 isValidAssumeForContext(UI, CtxI, DT))
3230 return true;
3231
3232 // Consider only compare instructions uniquely controlling a branch
3233 Value *RHS;
3235 bool NonNullIfTrue;
3236 if (match(UI, m_c_ICmp(Pred, m_Specific(V), m_Value(RHS)))) {
3237 if (cmpExcludesZero(Pred, RHS))
3238 NonNullIfTrue = true;
3240 NonNullIfTrue = false;
3241 else
3242 continue;
3243 } else if (UI->getType()->isIntegerTy(1) &&
3244 match(UI, m_Trunc(m_Specific(V)))) {
3245 NonNullIfTrue = true;
3246 } else
3247 continue;
3248
3251 for (const auto *CmpU : UI->users()) {
3252 assert(WorkList.empty() && "Should be!");
3253 if (Visited.insert(CmpU).second)
3254 WorkList.push_back(CmpU);
3255
3256 while (!WorkList.empty()) {
3257 auto *Curr = WorkList.pop_back_val();
3258
3259 // If a user is an AND, add all its users to the work list. We only
3260 // propagate "pred != null" condition through AND because it is only
3261 // correct to assume that all conditions of AND are met in true branch.
3262 // TODO: Support similar logic of OR and EQ predicate?
3263 if (NonNullIfTrue)
3264 if (match(Curr, m_LogicalAnd(m_Value(), m_Value()))) {
3265 for (const auto *CurrU : Curr->users())
3266 if (Visited.insert(CurrU).second)
3267 WorkList.push_back(CurrU);
3268 continue;
3269 }
3270
3271 if (const CondBrInst *BI = dyn_cast<CondBrInst>(Curr)) {
3272 BasicBlock *NonNullSuccessor =
3273 BI->getSuccessor(NonNullIfTrue ? 0 : 1);
3274 BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor);
3275 if (DT->dominates(Edge, CtxI->getParent()))
3276 return true;
3277 } else if (NonNullIfTrue && isGuard(Curr) &&
3278 DT->dominates(cast<Instruction>(Curr), CtxI)) {
3279 return true;
3280 }
3281 }
3282 }
3283 }
3284
3285 return false;
3286}
3287
3288/// Does the 'Range' metadata (which must be a valid MD_range operand list)
3289/// ensure that the value it's attached to is never Value? 'RangeType' is
3290/// is the type of the value described by the range.
3291static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) {
3292 const unsigned NumRanges = Ranges->getNumOperands() / 2;
3293 assert(NumRanges >= 1);
3294 for (unsigned i = 0; i < NumRanges; ++i) {
3296 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
3298 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
3299 ConstantRange Range(Lower->getValue(), Upper->getValue());
3300 if (Range.contains(Value))
3301 return false;
3302 }
3303 return true;
3304}
3305
3306/// Try to detect a recurrence that monotonically increases/decreases from a
3307/// non-zero starting value. These are common as induction variables.
3308static bool isNonZeroRecurrence(const PHINode *PN) {
3309 BinaryOperator *BO = nullptr;
3310 Value *Start = nullptr, *Step = nullptr;
3311 const APInt *StartC, *StepC;
3312 if (!matchSimpleRecurrence(PN, BO, Start, Step) ||
3313 !match(Start, m_APInt(StartC)) || StartC->isZero())
3314 return false;
3315
3316 switch (BO->getOpcode()) {
3317 case Instruction::Add:
3318 // Starting from non-zero and stepping away from zero can never wrap back
3319 // to zero.
3320 return BO->hasNoUnsignedWrap() ||
3321 (BO->hasNoSignedWrap() && match(Step, m_APInt(StepC)) &&
3322 StartC->isNegative() == StepC->isNegative());
3323 case Instruction::Mul:
3324 return (BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap()) &&
3325 match(Step, m_APInt(StepC)) && !StepC->isZero();
3326 case Instruction::Shl:
3327 return BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap();
3328 case Instruction::AShr:
3329 case Instruction::LShr:
3330 return BO->isExact();
3331 case Instruction::Or:
3332 return true;
3333 default:
3334 return false;
3335 }
3336}
3337
3338static bool matchOpWithOpEqZero(Value *Op0, Value *Op1) {
3340 m_Specific(Op1), m_Zero()))) ||
3342 m_Specific(Op0), m_Zero())));
3343}
3344
3345/// Return true if "addr(P) + Off" cannot be zero. The object P points to
3346/// cannot cross the unsigned address space boundary, including the address one
3347/// past its end, so an offset that stays within the object cannot wrap around
3348/// to zero. This holds even if the object starts at the zero address, which is
3349/// all that null_pointer_is_valid permits, so a non-zero offset is enough.
3351 Value *Y) {
3352 const APInt *Off;
3353 if (!match(Y, m_APInt(Off)) || Off->isZero())
3354 return false;
3355
3356 // ptrtoaddr always produces the address. ptrtoint only does so when its
3357 // result is at least as wide as the address: a narrower result is truncated
3358 // and may wrap to zero, while bits above the address can only make the
3359 // result more non-zero.
3360 Value *P;
3361 if (!match(X, m_PtrToAddr(m_Value(P))) &&
3362 !(match(X, m_PtrToInt(m_Value(P))) &&
3363 X->getType()->getScalarSizeInBits() >=
3364 Q.DL.getAddressSizeInBits(P->getType())))
3365 return false;
3366
3367 // A vector of pointers has no single object whose size can be taken, and
3368 // getPointerDereferenceableBytes() requires a plain pointer.
3369 if (!P->getType()->isPointerTy())
3370 return false;
3371
3372 bool CanBeNull;
3373 uint64_t DerefBytes = P->getPointerDereferenceableBytes(
3374 Q.DL, CanBeNull, /*CanBeFreed=*/nullptr);
3375 if (CanBeNull || DerefBytes == 0 || !Off->ule(DerefBytes))
3376 return false;
3377
3378 return true;
3379}
3380
3381static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q,
3382 unsigned BitWidth, Value *X, Value *Y, bool NSW,
3383 bool NUW, unsigned Depth) {
3385 return true;
3386
3387 // (X + (X != 0)) is non zero
3388 if (matchOpWithOpEqZero(X, Y))
3389 return true;
3390
3391 if (NUW)
3392 return isKnownNonZero(Y, DemandedElts, Q, Depth) ||
3393 isKnownNonZero(X, DemandedElts, Q, Depth);
3394
3395 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
3396 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
3397
3398 // If X and Y are both non-negative (as signed values) then their sum is not
3399 // zero unless both X and Y are zero.
3400 if (XKnown.isNonNegative() && YKnown.isNonNegative())
3401 if (isKnownNonZero(Y, DemandedElts, Q, Depth) ||
3402 isKnownNonZero(X, DemandedElts, Q, Depth))
3403 return true;
3404
3405 // If X and Y are both negative (as signed values) then their sum is not
3406 // zero unless both X and Y equal INT_MIN.
3407 if (XKnown.isNegative() && YKnown.isNegative()) {
3409 // The sign bit of X is set. If some other bit is set then X is not equal
3410 // to INT_MIN.
3411 if (XKnown.One.intersects(Mask))
3412 return true;
3413 // The sign bit of Y is set. If some other bit is set then Y is not equal
3414 // to INT_MIN.
3415 if (YKnown.One.intersects(Mask))
3416 return true;
3417 }
3418
3419 // The sum of a non-negative number and a power of two is not zero.
3420 if (XKnown.isNonNegative() &&
3421 isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Q, Depth))
3422 return true;
3423 if (YKnown.isNonNegative() &&
3424 isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Q, Depth))
3425 return true;
3426
3427 return KnownBits::add(XKnown, YKnown, NSW, NUW).isNonZero();
3428}
3429
3430static bool isNonZeroSub(const APInt &DemandedElts, const SimplifyQuery &Q,
3431 unsigned BitWidth, Value *X, Value *Y,
3432 unsigned Depth) {
3433 // (X - (X != 0)) is non zero
3434 // ((X != 0) - X) is non zero
3435 if (matchOpWithOpEqZero(X, Y))
3436 return true;
3437
3438 // TODO: Move this case into isKnownNonEqual().
3439 if (auto *C = dyn_cast<Constant>(X))
3440 if (C->isNullValue() && isKnownNonZero(Y, DemandedElts, Q, Depth))
3441 return true;
3442
3443 return ::isKnownNonEqual(X, Y, DemandedElts, Q, Depth);
3444}
3445
3446static bool isNonZeroMul(const APInt &DemandedElts, const SimplifyQuery &Q,
3447 unsigned BitWidth, Value *X, Value *Y, bool NSW,
3448 bool NUW, unsigned Depth) {
3449 // If X and Y are non-zero then so is X * Y as long as the multiplication
3450 // does not overflow.
3451 if (NSW || NUW)
3452 return isKnownNonZero(X, DemandedElts, Q, Depth) &&
3453 isKnownNonZero(Y, DemandedElts, Q, Depth);
3454
3455 // If either X or Y is odd, then if the other is non-zero the result can't
3456 // be zero.
3457 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
3458 if (XKnown.One[0])
3459 return isKnownNonZero(Y, DemandedElts, Q, Depth);
3460
3461 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
3462 if (YKnown.One[0])
3463 return XKnown.isNonZero() || isKnownNonZero(X, DemandedElts, Q, Depth);
3464
3465 // If there exists any subset of X (sX) and subset of Y (sY) s.t sX * sY is
3466 // non-zero, then X * Y is non-zero. We can find sX and sY by just taking
3467 // the lowest known One of X and Y. If they are non-zero, the result
3468 // must be non-zero. We can check if LSB(X) * LSB(Y) != 0 by doing
3469 // X.CountLeadingZeros + Y.CountLeadingZeros < BitWidth.
3470 return (XKnown.countMaxTrailingZeros() + YKnown.countMaxTrailingZeros()) <
3471 BitWidth;
3472}
3473
3474static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts,
3475 const SimplifyQuery &Q, const KnownBits &KnownVal,
3476 unsigned Depth) {
3477 auto ShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
3478 switch (I->getOpcode()) {
3479 case Instruction::Shl:
3480 return Lhs.shl(Rhs);
3481 case Instruction::LShr:
3482 return Lhs.lshr(Rhs);
3483 case Instruction::AShr:
3484 return Lhs.ashr(Rhs);
3485 default:
3486 llvm_unreachable("Unknown Shift Opcode");
3487 }
3488 };
3489
3490 auto InvShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
3491 switch (I->getOpcode()) {
3492 case Instruction::Shl:
3493 return Lhs.lshr(Rhs);
3494 case Instruction::LShr:
3495 case Instruction::AShr:
3496 return Lhs.shl(Rhs);
3497 default:
3498 llvm_unreachable("Unknown Shift Opcode");
3499 }
3500 };
3501
3502 if (KnownVal.isUnknown())
3503 return false;
3504
3505 KnownBits KnownCnt =
3506 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
3507 APInt MaxShift = KnownCnt.getMaxValue();
3508 unsigned NumBits = KnownVal.getBitWidth();
3509 if (MaxShift.uge(NumBits))
3510 return false;
3511
3512 if (!ShiftOp(KnownVal.One, MaxShift).isZero())
3513 return true;
3514
3515 // If all of the bits shifted out are known to be zero, and Val is known
3516 // non-zero then at least one non-zero bit must remain.
3517 if (InvShiftOp(KnownVal.Zero, NumBits - MaxShift)
3518 .eq(InvShiftOp(APInt::getAllOnes(NumBits), NumBits - MaxShift)) &&
3519 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth))
3520 return true;
3521
3522 return false;
3523}
3524
3526 const APInt &DemandedElts,
3527 const SimplifyQuery &Q, unsigned Depth) {
3528 unsigned BitWidth = getBitWidth(I->getType()->getScalarType(), Q.DL);
3529 switch (I->getOpcode()) {
3530 case Instruction::Alloca:
3531 // Alloca never returns null, malloc might.
3532 return I->getType()->getPointerAddressSpace() == 0;
3533 case Instruction::GetElementPtr:
3534 if (I->getType()->isPointerTy())
3536 break;
3537 case Instruction::BitCast: {
3538 // We need to be a bit careful here. We can only peek through the bitcast
3539 // if the scalar size of elements in the operand are smaller than and a
3540 // multiple of the size they are casting too. Take three cases:
3541 //
3542 // 1) Unsafe:
3543 // bitcast <2 x i16> %NonZero to <4 x i8>
3544 //
3545 // %NonZero can have 2 non-zero i16 elements, but isKnownNonZero on a
3546 // <4 x i8> requires that all 4 i8 elements be non-zero which isn't
3547 // guranteed (imagine just sign bit set in the 2 i16 elements).
3548 //
3549 // 2) Unsafe:
3550 // bitcast <4 x i3> %NonZero to <3 x i4>
3551 //
3552 // Even though the scalar size of the src (`i3`) is smaller than the
3553 // scalar size of the dst `i4`, because `i3` is not a multiple of `i4`
3554 // its possible for the `3 x i4` elements to be zero because there are
3555 // some elements in the destination that don't contain any full src
3556 // element.
3557 //
3558 // 3) Safe:
3559 // bitcast <4 x i8> %NonZero to <2 x i16>
3560 //
3561 // This is always safe as non-zero in the 4 i8 elements implies
3562 // non-zero in the combination of any two adjacent ones. Since i8 is a
3563 // multiple of i16, each i16 is guranteed to have 2 full i8 elements.
3564 // This all implies the 2 i16 elements are non-zero.
3565 Type *FromTy = I->getOperand(0)->getType();
3566 if ((FromTy->isIntOrIntVectorTy() || FromTy->isPtrOrPtrVectorTy()) &&
3567 (BitWidth % getBitWidth(FromTy->getScalarType(), Q.DL)) == 0)
3568 return isKnownNonZero(I->getOperand(0), Q, Depth);
3569 } break;
3570 case Instruction::IntToPtr:
3571 // Note that we have to take special care to avoid looking through
3572 // truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well
3573 // as casts that can alter the value, e.g., AddrSpaceCasts.
3574 if (!isa<ScalableVectorType>(I->getType()) &&
3575 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
3576 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
3577 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3578 break;
3579 case Instruction::PtrToAddr:
3580 // isKnownNonZero() for pointers refers to the address bits being non-zero,
3581 // so we can directly forward.
3582 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3583 case Instruction::PtrToInt:
3584 // For inttoptr, make sure the result size is >= the address size. If the
3585 // address is non-zero, any larger value is also non-zero.
3586 if (Q.DL.getAddressSizeInBits(I->getOperand(0)->getType()) <=
3587 I->getType()->getScalarSizeInBits())
3588 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3589 break;
3590 case Instruction::Trunc:
3591 // nuw/nsw trunc preserves zero/non-zero status of input.
3592 if (auto *TI = dyn_cast<TruncInst>(I))
3593 if (TI->hasNoSignedWrap() || TI->hasNoUnsignedWrap())
3594 return isKnownNonZero(TI->getOperand(0), DemandedElts, Q, Depth);
3595 break;
3596
3597 // Iff x - y != 0, then x ^ y != 0
3598 // Therefore we can do the same exact checks
3599 case Instruction::Xor:
3600 case Instruction::Sub:
3601 return isNonZeroSub(DemandedElts, Q, BitWidth, I->getOperand(0),
3602 I->getOperand(1), Depth);
3603 case Instruction::Or:
3604 // (X | (X != 0)) is non zero
3605 if (matchOpWithOpEqZero(I->getOperand(0), I->getOperand(1)))
3606 return true;
3607 // X | Y != 0 if X != Y.
3608 if (isKnownNonEqual(I->getOperand(0), I->getOperand(1), DemandedElts, Q,
3609 Depth))
3610 return true;
3611 // X | Y != 0 if X != 0 or Y != 0.
3612 return isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth) ||
3613 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3614 case Instruction::SExt:
3615 case Instruction::ZExt:
3616 // ext X != 0 if X != 0.
3617 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3618
3619 case Instruction::Shl: {
3620 // shl nsw/nuw can't remove any non-zero bits.
3622 if (Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO))
3623 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3624
3625 // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
3626 // if the lowest bit is shifted off the end.
3628 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth);
3629 if (Known.One[0])
3630 return true;
3631
3632 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3633 }
3634 case Instruction::LShr:
3635 case Instruction::AShr: {
3636 // shr exact can only shift out zero bits.
3638 if (BO->isExact())
3639 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3640
3641 // shr X, Y != 0 if X is negative. Note that the value of the shift is not
3642 // defined if the sign bit is shifted off the end.
3644 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3645 if (Known.isNegative())
3646 return true;
3647
3648 // shr (add nuw A, B), C is non-zero if A or B has a known-one bit at
3649 // position >= C, because the sum >= max(A, B).
3650 Value *A, *B;
3651 const APInt *C;
3652 if (Depth + 1 < MaxAnalysisRecursionDepth &&
3653 match(I->getOperand(0), m_NUWAdd(m_Value(A), m_Value(B))) &&
3654 match(I->getOperand(1), m_APInt(C)) && C->ult(BitWidth)) {
3655 KnownBits KnownA = computeKnownBits(A, DemandedElts, Q, Depth + 1);
3656 if (!KnownA.One.lshr(*C).isZero())
3657 return true;
3658 KnownBits KnownB = computeKnownBits(B, DemandedElts, Q, Depth + 1);
3659 if (!KnownB.One.lshr(*C).isZero())
3660 return true;
3661 }
3662
3663 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3664 }
3665 case Instruction::UDiv:
3666 case Instruction::SDiv: {
3667 // X / Y
3668 // div exact can only produce a zero if the dividend is zero.
3669 if (cast<PossiblyExactOperator>(I)->isExact())
3670 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3671
3672 KnownBits XKnown =
3673 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3674 // If X is fully unknown we won't be able to figure anything out so don't
3675 // both computing knownbits for Y.
3676 if (XKnown.isUnknown())
3677 return false;
3678
3679 KnownBits YKnown =
3680 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
3681 if (I->getOpcode() == Instruction::SDiv) {
3682 // For signed division need to compare abs value of the operands.
3683 XKnown = XKnown.abs(/*IntMinIsPoison*/ false);
3684 YKnown = YKnown.abs(/*IntMinIsPoison*/ false);
3685 }
3686 // If X u>= Y then div is non zero (0/0 is UB).
3687 std::optional<bool> XUgeY = KnownBits::uge(XKnown, YKnown);
3688 // If X is total unknown or X u< Y we won't be able to prove non-zero
3689 // with compute known bits so just return early.
3690 return XUgeY && *XUgeY;
3691 }
3692 case Instruction::Add: {
3693 // X + Y.
3694
3695 // If Add has nuw wrap flag, then if either X or Y is non-zero the result is
3696 // non-zero.
3698 return isNonZeroAdd(DemandedElts, Q, BitWidth, I->getOperand(0),
3699 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3700 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3701 }
3702 case Instruction::Mul: {
3704 return isNonZeroMul(DemandedElts, Q, BitWidth, I->getOperand(0),
3705 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3706 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3707 }
3708 case Instruction::Select: {
3709 // (C ? X : Y) != 0 if X != 0 and Y != 0.
3710
3711 // First check if the arm is non-zero using `isKnownNonZero`. If that fails,
3712 // then see if the select condition implies the arm is non-zero. For example
3713 // (X != 0 ? X : Y), we know the true arm is non-zero as the `X` "return" is
3714 // dominated by `X != 0`.
3715 auto SelectArmIsNonZero = [&](bool IsTrueArm) {
3716 Value *Op;
3717 Op = IsTrueArm ? I->getOperand(1) : I->getOperand(2);
3718 // Op is trivially non-zero.
3719 if (isKnownNonZero(Op, DemandedElts, Q, Depth))
3720 return true;
3721
3722 // The condition of the select dominates the true/false arm. Check if the
3723 // condition implies that a given arm is non-zero.
3724 Value *X;
3726 if (!match(I->getOperand(0), m_c_ICmp(Pred, m_Specific(Op), m_Value(X))))
3727 return false;
3728
3729 if (!IsTrueArm)
3731
3732 return cmpExcludesZero(Pred, X);
3733 };
3734
3735 if (SelectArmIsNonZero(/* IsTrueArm */ true) &&
3736 SelectArmIsNonZero(/* IsTrueArm */ false))
3737 return true;
3738 break;
3739 }
3740 case Instruction::PHI: {
3741 auto *PN = cast<PHINode>(I);
3743 return true;
3744
3745 // Check if all incoming values are non-zero using recursion.
3747 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
3748 return llvm::all_of(PN->operands(), [&](const Use &U) {
3749 if (U.get() == PN)
3750 return true;
3751 RecQ.CtxI = PN->getIncomingBlock(U)->getTerminator();
3752 // Check if the branch on the phi excludes zero.
3753 CmpPredicate Pred;
3754 Value *X;
3755 BasicBlock *TrueSucc, *FalseSucc;
3756 if (match(RecQ.CtxI,
3757 m_Br(m_c_ICmp(Pred, m_Specific(U.get()), m_Value(X)),
3758 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
3759 // Check for cases of duplicate successors.
3760 if ((TrueSucc == PN->getParent()) != (FalseSucc == PN->getParent())) {
3761 // If we're using the false successor, invert the predicate.
3762 if (FalseSucc == PN->getParent())
3763 Pred = CmpInst::getInversePredicate(Pred);
3764 if (cmpExcludesZero(Pred, X))
3765 return true;
3766 }
3767 }
3768 // Finally recurse on the edge and check it directly.
3769 return isKnownNonZero(U.get(), DemandedElts, RecQ, NewDepth);
3770 });
3771 }
3772 case Instruction::InsertElement: {
3773 if (isa<ScalableVectorType>(I->getType()))
3774 break;
3775
3776 const Value *Vec = I->getOperand(0);
3777 const Value *Elt = I->getOperand(1);
3778 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
3779
3780 unsigned NumElts = DemandedElts.getBitWidth();
3781 APInt DemandedVecElts = DemandedElts;
3782 bool SkipElt = false;
3783 // If we know the index we are inserting too, clear it from Vec check.
3784 if (CIdx && CIdx->getValue().ult(NumElts)) {
3785 DemandedVecElts.clearBit(CIdx->getZExtValue());
3786 SkipElt = !DemandedElts[CIdx->getZExtValue()];
3787 }
3788
3789 // Result is zero if Elt is non-zero and rest of the demanded elts in Vec
3790 // are non-zero.
3791 return (SkipElt || isKnownNonZero(Elt, Q, Depth)) &&
3792 (DemandedVecElts.isZero() ||
3793 isKnownNonZero(Vec, DemandedVecElts, Q, Depth));
3794 }
3795 case Instruction::ExtractElement:
3796 if (const auto *EEI = dyn_cast<ExtractElementInst>(I)) {
3797 const Value *Vec = EEI->getVectorOperand();
3798 const Value *Idx = EEI->getIndexOperand();
3799 auto *CIdx = dyn_cast<ConstantInt>(Idx);
3800 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
3801 unsigned NumElts = VecTy->getNumElements();
3802 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
3803 if (CIdx && CIdx->getValue().ult(NumElts))
3804 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
3805 return isKnownNonZero(Vec, DemandedVecElts, Q, Depth);
3806 }
3807 }
3808 break;
3809 case Instruction::ShuffleVector: {
3810 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
3811 if (!Shuf)
3812 break;
3813 APInt DemandedLHS, DemandedRHS;
3814 // For undef elements, we don't know anything about the common state of
3815 // the shuffle result.
3816 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
3817 break;
3818 // If demanded elements for both vecs are non-zero, the shuffle is non-zero.
3819 return (DemandedRHS.isZero() ||
3820 isKnownNonZero(Shuf->getOperand(1), DemandedRHS, Q, Depth)) &&
3821 (DemandedLHS.isZero() ||
3822 isKnownNonZero(Shuf->getOperand(0), DemandedLHS, Q, Depth));
3823 }
3824 case Instruction::Freeze:
3825 return isKnownNonZero(I->getOperand(0), Q, Depth) &&
3826 isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CtxI, Q.DT,
3827 Depth);
3828 case Instruction::Load: {
3829 auto *LI = cast<LoadInst>(I);
3830 // A Load tagged with nonnull or dereferenceable with null pointer undefined
3831 // is never null.
3832 if (auto *PtrT = dyn_cast<PointerType>(I->getType())) {
3833 if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull) ||
3834 (Q.IIQ.getMetadata(LI, LLVMContext::MD_dereferenceable) &&
3835 !NullPointerIsDefined(LI->getFunction(), PtrT->getAddressSpace())))
3836 return true;
3837 } else if (MDNode *Ranges = Q.IIQ.getMetadata(LI, LLVMContext::MD_range)) {
3839 }
3840
3841 // No need to fall through to computeKnownBits as range metadata is already
3842 // handled in isKnownNonZero.
3843 return false;
3844 }
3845 case Instruction::ExtractValue: {
3846 const WithOverflowInst *WO;
3848 switch (WO->getBinaryOp()) {
3849 default:
3850 break;
3851 case Instruction::Add:
3852 return isNonZeroAdd(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3853 WO->getArgOperand(1),
3854 /*NSW=*/false,
3855 /*NUW=*/false, Depth);
3856 case Instruction::Sub:
3857 return isNonZeroSub(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3858 WO->getArgOperand(1), Depth);
3859 case Instruction::Mul:
3860 return isNonZeroMul(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3861 WO->getArgOperand(1),
3862 /*NSW=*/false, /*NUW=*/false, Depth);
3863 break;
3864 }
3865 }
3866 break;
3867 }
3868 case Instruction::Call:
3869 case Instruction::Invoke: {
3870 const auto *Call = cast<CallBase>(I);
3871 if (I->getType()->isPointerTy()) {
3872 if (Call->isReturnNonNull())
3873 return true;
3874 if (const auto *RP = getArgumentAliasingToReturnedPointer(
3875 Call, /*MustPreserveOffset=*/true))
3876 return isKnownNonZero(RP, Q, Depth);
3877 } else {
3878 if (MDNode *Ranges = Q.IIQ.getMetadata(Call, LLVMContext::MD_range))
3880 if (std::optional<ConstantRange> Range = Call->getRange()) {
3881 const APInt ZeroValue(Range->getBitWidth(), 0);
3882 if (!Range->contains(ZeroValue))
3883 return true;
3884 }
3885 if (const Value *RV = Call->getReturnedArgOperand())
3886 if (RV->getType() == I->getType() && isKnownNonZero(RV, Q, Depth))
3887 return true;
3888 }
3889
3890 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
3891 switch (II->getIntrinsicID()) {
3892 case Intrinsic::sshl_sat:
3893 case Intrinsic::ushl_sat:
3894 case Intrinsic::abs:
3895 case Intrinsic::bitreverse:
3896 case Intrinsic::bswap:
3897 case Intrinsic::ctpop:
3898 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3899 // NB: We don't do usub_sat here as in any case we can prove its
3900 // non-zero, we will fold it to `sub nuw` in InstCombine.
3901 case Intrinsic::ssub_sat:
3902 // For most types, if x != y then ssub.sat x, y != 0. But
3903 // ssub.sat.i1 0, -1 = 0, because 1 saturates to 0. This means
3904 // isNonZeroSub will do the wrong thing for ssub.sat.i1.
3905 if (BitWidth == 1)
3906 return false;
3907 return isNonZeroSub(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3908 II->getArgOperand(1), Depth);
3909 case Intrinsic::sadd_sat:
3910 return isNonZeroAdd(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3911 II->getArgOperand(1),
3912 /*NSW=*/true, /* NUW=*/false, Depth);
3913 // Vec reverse preserves zero/non-zero status from input vec.
3914 case Intrinsic::vector_reverse:
3915 return isKnownNonZero(II->getArgOperand(0), DemandedElts.reverseBits(),
3916 Q, Depth);
3917 // umin/smin/smax/smin/or of all non-zero elements is always non-zero.
3918 case Intrinsic::vector_reduce_or:
3919 case Intrinsic::vector_reduce_umax:
3920 case Intrinsic::vector_reduce_umin:
3921 case Intrinsic::vector_reduce_smax:
3922 case Intrinsic::vector_reduce_smin:
3923 return isKnownNonZero(II->getArgOperand(0), Q, Depth);
3924 case Intrinsic::umax:
3925 case Intrinsic::uadd_sat:
3926 // umax(X, (X != 0)) is non zero
3927 // X +usat (X != 0) is non zero
3928 if (matchOpWithOpEqZero(II->getArgOperand(0), II->getArgOperand(1)))
3929 return true;
3930
3931 return isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth) ||
3932 isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3933 case Intrinsic::smax: {
3934 // If either arg is strictly positive the result is non-zero. Otherwise
3935 // the result is non-zero if both ops are non-zero.
3936 auto IsNonZero = [&](Value *Op, std::optional<bool> &OpNonZero,
3937 const KnownBits &OpKnown) {
3938 if (!OpNonZero.has_value())
3939 OpNonZero = OpKnown.isNonZero() ||
3940 isKnownNonZero(Op, DemandedElts, Q, Depth);
3941 return *OpNonZero;
3942 };
3943 // Avoid re-computing isKnownNonZero.
3944 std::optional<bool> Op0NonZero, Op1NonZero;
3945 KnownBits Op1Known =
3946 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3947 if (Op1Known.isNonNegative() &&
3948 IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known))
3949 return true;
3950 KnownBits Op0Known =
3951 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3952 if (Op0Known.isNonNegative() &&
3953 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known))
3954 return true;
3955 return IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known) &&
3956 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known);
3957 }
3958 case Intrinsic::smin: {
3959 // If either arg is negative the result is non-zero. Otherwise
3960 // the result is non-zero if both ops are non-zero.
3961 KnownBits Op1Known =
3962 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3963 if (Op1Known.isNegative())
3964 return true;
3965 KnownBits Op0Known =
3966 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3967 if (Op0Known.isNegative())
3968 return true;
3969
3970 if (Op1Known.isNonZero() && Op0Known.isNonZero())
3971 return true;
3972 }
3973 [[fallthrough]];
3974 case Intrinsic::umin:
3975 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth) &&
3976 isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth);
3977 case Intrinsic::cttz:
3978 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3979 .Zero[0];
3980 case Intrinsic::ctlz:
3981 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3982 .isNonNegative();
3983 case Intrinsic::fshr:
3984 case Intrinsic::fshl:
3985 // If Op0 == Op1, this is a rotate. rotate(x, y) != 0 iff x != 0.
3986 if (II->getArgOperand(0) == II->getArgOperand(1))
3987 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3988 break;
3989 case Intrinsic::vscale:
3990 return true;
3991 case Intrinsic::experimental_get_vector_length:
3992 return isKnownNonZero(I->getOperand(0), Q, Depth);
3993 default:
3994 break;
3995 }
3996 break;
3997 }
3998
3999 return false;
4000 }
4001 }
4002
4004 computeKnownBits(I, DemandedElts, Known, Q, Depth);
4005 return Known.One != 0;
4006}
4007
4008/// Return true if the given value is known to be non-zero when defined. For
4009/// vectors, return true if every demanded element is known to be non-zero when
4010/// defined. For pointers, if the context instruction and dominator tree are
4011/// specified, perform context-sensitive analysis and return true if the
4012/// pointer couldn't possibly be null at the specified instruction.
4013/// Supports values with integer or pointer type and vectors of integers.
4014bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
4015 const SimplifyQuery &Q, unsigned Depth) {
4016 Type *Ty = V->getType();
4017
4018#ifndef NDEBUG
4019 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4020
4021 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
4022 assert(
4023 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
4024 "DemandedElt width should equal the fixed vector number of elements");
4025 } else {
4026 assert(DemandedElts == APInt(1, 1) &&
4027 "DemandedElt width should be 1 for scalars");
4028 }
4029#endif
4030
4031 if (auto *C = dyn_cast<Constant>(V)) {
4032 if (C->isNullValue())
4033 return false;
4034 if (isa<ConstantInt>(C))
4035 // Must be non-zero due to null test above.
4036 return true;
4037
4038 // For constant vectors, check that all elements are poison or known
4039 // non-zero to determine that the whole vector is known non-zero.
4040 if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
4041 for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
4042 if (!DemandedElts[i])
4043 continue;
4044 Constant *Elt = C->getAggregateElement(i);
4045 if (!Elt || Elt->isNullValue())
4046 return false;
4047 if (!isa<PoisonValue>(Elt) && !isa<ConstantInt>(Elt))
4048 return false;
4049 }
4050 return true;
4051 }
4052
4053 // Constant ptrauth can be null, iff the base pointer can be.
4054 if (auto *CPA = dyn_cast<ConstantPtrAuth>(V))
4055 return isKnownNonZero(CPA->getPointer(), DemandedElts, Q, Depth);
4056
4057 // A global variable is non null unless extern weak or an absolute symbol
4058 // reference. An alias is also excluded, as it may resolve to a weak
4059 // undefined symbol and thus to the null address. Where null is a valid
4060 // address an allocation may start there, so nothing can be assumed; that
4061 // covers both null_pointer_is_valid and address spaces other than zero.
4062 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
4064 GV->getType()->getAddressSpace()) &&
4065 !GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&
4066 !isa<GlobalAlias>(GV))
4067 return true;
4068 }
4069
4070 // For constant expressions, fall through to the Operator code below.
4071 if (!isa<ConstantExpr>(V))
4072 return false;
4073 }
4074
4075 if (const auto *A = dyn_cast<Argument>(V))
4076 if (std::optional<ConstantRange> Range = A->getRange()) {
4077 const APInt ZeroValue(Range->getBitWidth(), 0);
4078 if (!Range->contains(ZeroValue))
4079 return true;
4080 }
4081
4083 return true;
4084
4085 // Some of the tests below are recursive, so bail out if we hit the limit.
4087 return false;
4088
4089 // Check for pointer simplifications.
4090
4091 if (PointerType *PtrTy = dyn_cast<PointerType>(Ty)) {
4092 // A byval, inalloca may not be null in a non-default addres space. A
4093 // nonnull argument is assumed never 0.
4094 if (const Argument *A = dyn_cast<Argument>(V)) {
4095 if (((A->hasPassPointeeByValueCopyAttr() &&
4096 !NullPointerIsDefined(A->getParent(), PtrTy->getAddressSpace())) ||
4097 A->hasNonNullAttr()))
4098 return true;
4099 }
4100 }
4101
4102 if (const auto *I = dyn_cast<Operator>(V))
4103 if (isKnownNonZeroFromOperator(I, DemandedElts, Q, Depth))
4104 return true;
4105
4106 if (!isa<Constant>(V) &&
4108 return true;
4109
4110 if (const Value *Stripped = stripNullTest(V))
4111 return isKnownNonZero(Stripped, DemandedElts, Q, Depth);
4112
4113 return false;
4114}
4115
4117 unsigned Depth) {
4118 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
4119 APInt DemandedElts =
4120 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
4121 return ::isKnownNonZero(V, DemandedElts, Q, Depth);
4122}
4123
4124/// If the pair of operators are the same invertible function, return the
4125/// the operands of the function corresponding to each input. Otherwise,
4126/// return std::nullopt. An invertible function is one that is 1-to-1 and maps
4127/// every input value to exactly one output value. This is equivalent to
4128/// saying that Op1 and Op2 are equal exactly when the specified pair of
4129/// operands are equal, (except that Op1 and Op2 may be poison more often.)
4130static std::optional<std::pair<Value*, Value*>>
4132 const Operator *Op2) {
4133 if (Op1->getOpcode() != Op2->getOpcode())
4134 return std::nullopt;
4135
4136 auto getOperands = [&](unsigned OpNum) -> auto {
4137 return std::make_pair(Op1->getOperand(OpNum), Op2->getOperand(OpNum));
4138 };
4139
4140 switch (Op1->getOpcode()) {
4141 default:
4142 break;
4143 case Instruction::Or:
4144 if (!cast<PossiblyDisjointInst>(Op1)->isDisjoint() ||
4145 !cast<PossiblyDisjointInst>(Op2)->isDisjoint())
4146 break;
4147 [[fallthrough]];
4148 case Instruction::Xor:
4149 case Instruction::Add: {
4150 Value *Other;
4151 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(0)), m_Value(Other))))
4152 return std::make_pair(Op1->getOperand(1), Other);
4153 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(1)), m_Value(Other))))
4154 return std::make_pair(Op1->getOperand(0), Other);
4155 break;
4156 }
4157 case Instruction::Sub:
4158 if (Op1->getOperand(0) == Op2->getOperand(0))
4159 return getOperands(1);
4160 if (Op1->getOperand(1) == Op2->getOperand(1))
4161 return getOperands(0);
4162 break;
4163 case Instruction::Mul: {
4164 // invertible if A * B == (A * B) mod 2^N where A, and B are integers
4165 // and N is the bitwdith. The nsw case is non-obvious, but proven by
4166 // alive2: https://alive2.llvm.org/ce/z/Z6D5qK
4167 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
4168 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
4169 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
4170 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
4171 break;
4172
4173 // Assume operand order has been canonicalized
4174 if (Op1->getOperand(1) == Op2->getOperand(1) &&
4175 isa<ConstantInt>(Op1->getOperand(1)) &&
4176 !cast<ConstantInt>(Op1->getOperand(1))->isZero())
4177 return getOperands(0);
4178 break;
4179 }
4180 case Instruction::Shl: {
4181 // Same as multiplies, with the difference that we don't need to check
4182 // for a non-zero multiply. Shifts always multiply by non-zero.
4183 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
4184 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
4185 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
4186 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
4187 break;
4188
4189 if (Op1->getOperand(1) == Op2->getOperand(1))
4190 return getOperands(0);
4191 break;
4192 }
4193 case Instruction::AShr:
4194 case Instruction::LShr: {
4195 auto *PEO1 = cast<PossiblyExactOperator>(Op1);
4196 auto *PEO2 = cast<PossiblyExactOperator>(Op2);
4197 if (!PEO1->isExact() || !PEO2->isExact())
4198 break;
4199
4200 if (Op1->getOperand(1) == Op2->getOperand(1))
4201 return getOperands(0);
4202 break;
4203 }
4204 case Instruction::SExt:
4205 case Instruction::ZExt:
4206 if (Op1->getOperand(0)->getType() == Op2->getOperand(0)->getType())
4207 return getOperands(0);
4208 break;
4209 case Instruction::PHI: {
4210 const PHINode *PN1 = cast<PHINode>(Op1);
4211 const PHINode *PN2 = cast<PHINode>(Op2);
4212
4213 // If PN1 and PN2 are both recurrences, can we prove the entire recurrences
4214 // are a single invertible function of the start values? Note that repeated
4215 // application of an invertible function is also invertible
4216 BinaryOperator *BO1 = nullptr;
4217 Value *Start1 = nullptr, *Step1 = nullptr;
4218 BinaryOperator *BO2 = nullptr;
4219 Value *Start2 = nullptr, *Step2 = nullptr;
4220 if (PN1->getParent() != PN2->getParent() ||
4221 !matchSimpleRecurrence(PN1, BO1, Start1, Step1) ||
4222 !matchSimpleRecurrence(PN2, BO2, Start2, Step2))
4223 break;
4224
4226 cast<Operator>(BO2));
4227 if (!Values)
4228 break;
4229
4230 // We have to be careful of mutually defined recurrences here. Ex:
4231 // * X_i = X_(i-1) OP Y_(i-1), and Y_i = X_(i-1) OP V
4232 // * X_i = Y_i = X_(i-1) OP Y_(i-1)
4233 // The invertibility of these is complicated, and not worth reasoning
4234 // about (yet?).
4235 if (Values->first != PN1 || Values->second != PN2)
4236 break;
4237
4238 return std::make_pair(Start1, Start2);
4239 }
4240 }
4241 return std::nullopt;
4242}
4243
4244/// Return true if V1 == (binop V2, X), where X is known non-zero.
4245/// Only handle a small subset of binops where (binop V2, X) with non-zero X
4246/// implies V2 != V1.
4247static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2,
4248 const APInt &DemandedElts,
4249 const SimplifyQuery &Q, unsigned Depth) {
4251 if (!BO)
4252 return false;
4253 switch (BO->getOpcode()) {
4254 default:
4255 break;
4256 case Instruction::Or:
4257 if (!cast<PossiblyDisjointInst>(V1)->isDisjoint())
4258 break;
4259 [[fallthrough]];
4260 case Instruction::Xor:
4261 case Instruction::Add:
4262 Value *Op = nullptr;
4263 if (V2 == BO->getOperand(0))
4264 Op = BO->getOperand(1);
4265 else if (V2 == BO->getOperand(1))
4266 Op = BO->getOperand(0);
4267 else
4268 return false;
4269 return isKnownNonZero(Op, DemandedElts, Q, Depth + 1);
4270 }
4271 return false;
4272}
4273
4274/// Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and
4275/// the multiplication is nuw or nsw.
4276static bool isNonEqualMul(const Value *V1, const Value *V2,
4277 const APInt &DemandedElts, const SimplifyQuery &Q,
4278 unsigned Depth) {
4279 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
4280 const APInt *C;
4281 return match(OBO, m_Mul(m_Specific(V1), m_APInt(C))) &&
4282 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
4283 !C->isZero() && !C->isOne() &&
4284 isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
4285 }
4286 return false;
4287}
4288
4289/// Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and
4290/// the shift is nuw or nsw.
4291static bool isNonEqualShl(const Value *V1, const Value *V2,
4292 const APInt &DemandedElts, const SimplifyQuery &Q,
4293 unsigned Depth) {
4294 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
4295 const APInt *C;
4296 return match(OBO, m_Shl(m_Specific(V1), m_APInt(C))) &&
4297 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
4298 !C->isZero() && isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
4299 }
4300 return false;
4301}
4302
4303static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2,
4304 const APInt &DemandedElts, const SimplifyQuery &Q,
4305 unsigned Depth) {
4306 // Check two PHIs are in same block.
4307 if (PN1->getParent() != PN2->getParent())
4308 return false;
4309
4311 bool UsedFullRecursion = false;
4312 for (const BasicBlock *IncomBB : PN1->blocks()) {
4313 if (!VisitedBBs.insert(IncomBB).second)
4314 continue; // Don't reprocess blocks that we have dealt with already.
4315 const Value *IV1 = PN1->getIncomingValueForBlock(IncomBB);
4316 const Value *IV2 = PN2->getIncomingValueForBlock(IncomBB);
4317 const APInt *C1, *C2;
4318 if (match(IV1, m_APInt(C1)) && match(IV2, m_APInt(C2)) && *C1 != *C2)
4319 continue;
4320
4321 // Only one pair of phi operands is allowed for full recursion.
4322 if (UsedFullRecursion)
4323 return false;
4324
4326 RecQ.CtxI = IncomBB->getTerminator();
4327 if (!isKnownNonEqual(IV1, IV2, DemandedElts, RecQ, Depth + 1))
4328 return false;
4329 UsedFullRecursion = true;
4330 }
4331 return true;
4332}
4333
4334static bool isNonEqualSelect(const Value *V1, const Value *V2,
4335 const APInt &DemandedElts, const SimplifyQuery &Q,
4336 unsigned Depth) {
4337 const SelectInst *SI1 = dyn_cast<SelectInst>(V1);
4338 if (!SI1)
4339 return false;
4340
4341 if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2)) {
4342 const Value *Cond1 = SI1->getCondition();
4343 const Value *Cond2 = SI2->getCondition();
4344 if (Cond1 == Cond2)
4345 return isKnownNonEqual(SI1->getTrueValue(), SI2->getTrueValue(),
4346 DemandedElts, Q, Depth + 1) &&
4347 isKnownNonEqual(SI1->getFalseValue(), SI2->getFalseValue(),
4348 DemandedElts, Q, Depth + 1);
4349 }
4350 return isKnownNonEqual(SI1->getTrueValue(), V2, DemandedElts, Q, Depth + 1) &&
4351 isKnownNonEqual(SI1->getFalseValue(), V2, DemandedElts, Q, Depth + 1);
4352}
4353
4354// Check to see if A is both a GEP and is the incoming value for a PHI in the
4355// loop, and B is either a ptr or another GEP. If the PHI has 2 incoming values,
4356// one of them being the recursive GEP A and the other a ptr at same base and at
4357// the same/higher offset than B we are only incrementing the pointer further in
4358// loop if offset of recursive GEP is greater than 0.
4360 const SimplifyQuery &Q) {
4361 if (!A->getType()->isPointerTy() || !B->getType()->isPointerTy())
4362 return false;
4363
4364 auto *GEPA = dyn_cast<GEPOperator>(A);
4365 if (!GEPA || GEPA->getNumIndices() != 1 || !isa<Constant>(GEPA->idx_begin()))
4366 return false;
4367
4368 // Handle 2 incoming PHI values with one being a recursive GEP.
4369 auto *PN = dyn_cast<PHINode>(GEPA->getPointerOperand());
4370 if (!PN || PN->getNumIncomingValues() != 2)
4371 return false;
4372
4373 // Search for the recursive GEP as an incoming operand, and record that as
4374 // Step.
4375 Value *Start = nullptr;
4376 Value *Step = const_cast<Value *>(A);
4377 if (PN->getIncomingValue(0) == Step)
4378 Start = PN->getIncomingValue(1);
4379 else if (PN->getIncomingValue(1) == Step)
4380 Start = PN->getIncomingValue(0);
4381 else
4382 return false;
4383
4384 // Other incoming node base should match the B base.
4385 // StartOffset >= OffsetB && StepOffset > 0?
4386 // StartOffset <= OffsetB && StepOffset < 0?
4387 // Is non-equal if above are true.
4388 // We use stripAndAccumulateInBoundsConstantOffsets to restrict the
4389 // optimisation to inbounds GEPs only.
4390 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(Start->getType());
4391 APInt StartOffset(IndexWidth, 0);
4392 Start = Start->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StartOffset);
4393 APInt StepOffset(IndexWidth, 0);
4394 Step = Step->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StepOffset);
4395
4396 // Check if Base Pointer of Step matches the PHI.
4397 if (Step != PN)
4398 return false;
4399 APInt OffsetB(IndexWidth, 0);
4400 B = B->stripAndAccumulateInBoundsConstantOffsets(Q.DL, OffsetB);
4401 return Start == B &&
4402 ((StartOffset.sge(OffsetB) && StepOffset.isStrictlyPositive()) ||
4403 (StartOffset.sle(OffsetB) && StepOffset.isNegative()));
4404}
4405
4406static bool isKnownNonEqualFromContext(const Value *V1, const Value *V2,
4407 const SimplifyQuery &Q, unsigned Depth) {
4408 if (!Q.CtxI)
4409 return false;
4410
4411 // Try to infer NonEqual based on information from dominating conditions.
4412 if (Q.DC && Q.DT) {
4413 auto IsKnownNonEqualFromDominatingCondition = [&](const Value *V) {
4414 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
4415 Value *Cond = BI->getCondition();
4416 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4417 if (Q.DT->dominates(Edge0, Q.CtxI->getParent()) &&
4419 /*LHSIsTrue=*/true, Depth)
4420 .value_or(false))
4421 return true;
4422
4423 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4424 if (Q.DT->dominates(Edge1, Q.CtxI->getParent()) &&
4426 /*LHSIsTrue=*/false, Depth)
4427 .value_or(false))
4428 return true;
4429 }
4430
4431 return false;
4432 };
4433
4434 if (IsKnownNonEqualFromDominatingCondition(V1) ||
4435 IsKnownNonEqualFromDominatingCondition(V2))
4436 return true;
4437 }
4438
4439 if (!Q.AC)
4440 return false;
4441
4442 // Try to infer NonEqual based on information from assumptions.
4443 for (auto &AssumeVH : Q.AC->assumptionsFor(V1)) {
4444 if (!AssumeVH)
4445 continue;
4446 CallInst *I = cast<CallInst>(AssumeVH);
4447
4448 assert(I->getFunction() == Q.CtxI->getFunction() &&
4449 "Got assumption for the wrong function!");
4450 assert(I->getIntrinsicID() == Intrinsic::assume &&
4451 "must be an assume intrinsic");
4452
4453 if (isImpliedCondition(I->getArgOperand(0), ICmpInst::ICMP_NE, V1, V2, Q.DL,
4454 /*LHSIsTrue=*/true, Depth)
4455 .value_or(false) &&
4457 return true;
4458 }
4459
4460 return false;
4461}
4462
4463static bool isNonEqualURem(const Value *X, const Value *Rem,
4464 const SimplifyQuery &Q) {
4465 const Value *Y;
4466 if (!match(Rem, m_URem(m_Specific(X), m_Value(Y))))
4467 return false;
4468
4469 // For a defined urem, X != X urem Y exactly when X u>= Y.
4470 // isTruePredicate does not handle UGE, so use the equivalent Y u<= X.
4472 return true;
4473
4474 std::optional<bool> Implied =
4476 return Implied && *Implied;
4477}
4478
4479/// Return true if it is known that V1 != V2.
4480static bool isKnownNonEqual(const Value *V1, const Value *V2,
4481 const APInt &DemandedElts, const SimplifyQuery &Q,
4482 unsigned Depth) {
4483 if (V1 == V2)
4484 return false;
4485 if (V1->getType() != V2->getType())
4486 // We can't look through casts yet.
4487 return false;
4488
4490 return false;
4491
4492 // See if we can recurse through (exactly one of) our operands. This
4493 // requires our operation be 1-to-1 and map every input value to exactly
4494 // one output value. Such an operation is invertible.
4495 auto *O1 = dyn_cast<Operator>(V1);
4496 auto *O2 = dyn_cast<Operator>(V2);
4497 if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) {
4498 if (auto Values = getInvertibleOperands(O1, O2))
4499 return isKnownNonEqual(Values->first, Values->second, DemandedElts, Q,
4500 Depth + 1);
4501
4502 if (const PHINode *PN1 = dyn_cast<PHINode>(V1)) {
4503 const PHINode *PN2 = cast<PHINode>(V2);
4504 // FIXME: This is missing a generalization to handle the case where one is
4505 // a PHI and another one isn't.
4506 if (isNonEqualPHIs(PN1, PN2, DemandedElts, Q, Depth))
4507 return true;
4508 };
4509 }
4510
4511 if (isModifyingBinopOfNonZero(V1, V2, DemandedElts, Q, Depth) ||
4512 isModifyingBinopOfNonZero(V2, V1, DemandedElts, Q, Depth))
4513 return true;
4514
4515 if (isNonEqualMul(V1, V2, DemandedElts, Q, Depth) ||
4516 isNonEqualMul(V2, V1, DemandedElts, Q, Depth))
4517 return true;
4518
4519 if (isNonEqualShl(V1, V2, DemandedElts, Q, Depth) ||
4520 isNonEqualShl(V2, V1, DemandedElts, Q, Depth))
4521 return true;
4522
4523 if (V1->getType()->isIntOrIntVectorTy()) {
4524 // Are any known bits in V1 contradictory to known bits in V2? If V1
4525 // has a known zero where V2 has a known one, they must not be equal.
4526 KnownBits Known1 = computeKnownBits(V1, DemandedElts, Q, Depth);
4527 if (!Known1.isUnknown()) {
4528 KnownBits Known2 = computeKnownBits(V2, DemandedElts, Q, Depth);
4529 if (Known1.Zero.intersects(Known2.One) ||
4530 Known2.Zero.intersects(Known1.One))
4531 return true;
4532 }
4533 }
4534
4535 if (isNonEqualSelect(V1, V2, DemandedElts, Q, Depth) ||
4536 isNonEqualSelect(V2, V1, DemandedElts, Q, Depth))
4537 return true;
4538
4541 return true;
4542
4543 Value *A, *B;
4544 // PtrToInts are NonEqual if their Ptrs are NonEqual.
4545 // Check PtrToInt type matches the pointer size.
4546 if (match(V1, m_PtrToIntSameSize(Q.DL, m_Value(A))) &&
4548 return isKnownNonEqual(A, B, DemandedElts, Q, Depth + 1);
4549
4550 if (isNonEqualURem(V1, V2, Q) || isNonEqualURem(V2, V1, Q))
4551 return true;
4552
4553 if (isKnownNonEqualFromContext(V1, V2, Q, Depth))
4554 return true;
4555
4556 return false;
4557}
4558
4559/// For vector constants, loop over the elements and find the constant with the
4560/// minimum number of sign bits. Return 0 if the value is not a vector constant
4561/// or if any element was not analyzed; otherwise, return the count for the
4562/// element with the minimum number of sign bits.
4564 const APInt &DemandedElts,
4565 unsigned TyBits) {
4566 const auto *CV = dyn_cast<Constant>(V);
4567 if (!CV || !isa<FixedVectorType>(CV->getType()))
4568 return 0;
4569
4570 unsigned MinSignBits = TyBits;
4571 unsigned NumElts = cast<FixedVectorType>(CV->getType())->getNumElements();
4572 for (unsigned i = 0; i != NumElts; ++i) {
4573 if (!DemandedElts[i])
4574 continue;
4575 // If we find a non-ConstantInt, bail out.
4576 auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));
4577 if (!Elt)
4578 return 0;
4579
4580 MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());
4581 }
4582
4583 return MinSignBits;
4584}
4585
4586static unsigned ComputeNumSignBitsImpl(const Value *V,
4587 const APInt &DemandedElts,
4588 const SimplifyQuery &Q, unsigned Depth);
4589
4590static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
4591 const SimplifyQuery &Q, unsigned Depth) {
4592 unsigned Result = ComputeNumSignBitsImpl(V, DemandedElts, Q, Depth);
4593 assert(Result > 0 && "At least one sign bit needs to be present!");
4594 return Result;
4595}
4596
4597/// Return the number of times the sign bit of the register is replicated into
4598/// the other bits. We know that at least 1 bit is always equal to the sign bit
4599/// (itself), but other cases can give us information. For example, immediately
4600/// after an "ashr X, 2", we know that the top 3 bits are all equal to each
4601/// other, so we return 3. For vectors, return the number of sign bits for the
4602/// vector element with the minimum number of known sign bits of the demanded
4603/// elements in the vector specified by DemandedElts.
4604static unsigned ComputeNumSignBitsImpl(const Value *V,
4605 const APInt &DemandedElts,
4606 const SimplifyQuery &Q, unsigned Depth) {
4607 Type *Ty = V->getType();
4608#ifndef NDEBUG
4609 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4610
4611 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
4612 assert(
4613 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
4614 "DemandedElt width should equal the fixed vector number of elements");
4615 } else {
4616 assert(DemandedElts == APInt(1, 1) &&
4617 "DemandedElt width should be 1 for scalars");
4618 }
4619#endif
4620
4621 // We return the minimum number of sign bits that are guaranteed to be present
4622 // in V, so for undef we have to conservatively return 1. We don't have the
4623 // same behavior for poison though -- that's a FIXME today.
4624
4625 Type *ScalarTy = Ty->getScalarType();
4626 unsigned TyBits = ScalarTy->isPointerTy() ?
4627 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
4628 Q.DL.getTypeSizeInBits(ScalarTy);
4629
4630 unsigned Tmp, Tmp2;
4631 unsigned FirstAnswer = 1;
4632
4633 // Note that ConstantInt is handled by the general computeKnownBits case
4634 // below.
4635
4637 return 1;
4638
4639 if (auto *U = dyn_cast<Operator>(V)) {
4640 switch (Operator::getOpcode(V)) {
4641 default: break;
4642 case Instruction::BitCast: {
4643 Value *Src = U->getOperand(0);
4644 Type *SrcTy = Src->getType();
4645
4646 // Skip if the source type is not an integer or integer vector type
4647 // This ensures we only process integer-like types
4648 if (!SrcTy->isIntOrIntVectorTy())
4649 break;
4650
4651 unsigned SrcBits = SrcTy->getScalarSizeInBits();
4652
4653 // Bitcast 'large element' scalar/vector to 'small element' vector.
4654 if ((SrcBits % TyBits) != 0)
4655 break;
4656
4657 // Only proceed if the destination type is a fixed-size vector
4658 if (isa<FixedVectorType>(Ty)) {
4659 // Fast case - sign splat can be simply split across the small elements.
4660 // This works for both vector and scalar sources
4661 Tmp = ComputeNumSignBits(Src, Q, Depth + 1);
4662 if (Tmp == SrcBits)
4663 return TyBits;
4664 }
4665 break;
4666 }
4667 case Instruction::SExt:
4668 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
4669 return ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1) +
4670 Tmp;
4671
4672 case Instruction::SDiv: {
4673 const APInt *Denominator;
4674 // sdiv X, C -> adds log(C) sign bits.
4675 if (match(U->getOperand(1), m_APInt(Denominator))) {
4676
4677 // Ignore non-positive denominator.
4678 if (!Denominator->isStrictlyPositive())
4679 break;
4680
4681 // Calculate the incoming numerator bits.
4682 unsigned NumBits =
4683 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4684
4685 // Add floor(log(C)) bits to the numerator bits.
4686 return std::min(TyBits, NumBits + Denominator->logBase2());
4687 }
4688 break;
4689 }
4690
4691 case Instruction::SRem: {
4692 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4693
4694 const APInt *Denominator;
4695 // srem X, C -> we know that the result is within [-C+1,C) when C is a
4696 // positive constant. This let us put a lower bound on the number of sign
4697 // bits.
4698 if (match(U->getOperand(1), m_APInt(Denominator))) {
4699
4700 // Ignore non-positive denominator.
4701 if (Denominator->isStrictlyPositive()) {
4702 // Calculate the leading sign bit constraints by examining the
4703 // denominator. Given that the denominator is positive, there are two
4704 // cases:
4705 //
4706 // 1. The numerator is positive. The result range is [0,C) and
4707 // [0,C) u< (1 << ceilLogBase2(C)).
4708 //
4709 // 2. The numerator is negative. Then the result range is (-C,0] and
4710 // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
4711 //
4712 // Thus a lower bound on the number of sign bits is `TyBits -
4713 // ceilLogBase2(C)`.
4714
4715 unsigned ResBits = TyBits - Denominator->ceilLogBase2();
4716 Tmp = std::max(Tmp, ResBits);
4717 }
4718 }
4719 return Tmp;
4720 }
4721
4722 case Instruction::AShr: {
4723 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4724 // ashr X, C -> adds C sign bits. Vectors too.
4725 const APInt *ShAmt;
4726 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4727 if (ShAmt->uge(TyBits))
4728 break; // Bad shift.
4729 unsigned ShAmtLimited = ShAmt->getZExtValue();
4730 Tmp += ShAmtLimited;
4731 if (Tmp > TyBits) Tmp = TyBits;
4732 }
4733 return Tmp;
4734 }
4735 case Instruction::Shl: {
4736 const APInt *ShAmt;
4737 Value *X = nullptr;
4738 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4739 // shl destroys sign bits.
4740 if (ShAmt->uge(TyBits))
4741 break; // Bad shift.
4742 // We can look through a zext (more or less treating it as a sext) if
4743 // all extended bits are shifted out.
4744 if (match(U->getOperand(0), m_ZExt(m_Value(X))) &&
4745 ShAmt->uge(TyBits - X->getType()->getScalarSizeInBits())) {
4746 Tmp = ComputeNumSignBits(X, DemandedElts, Q, Depth + 1);
4747 Tmp += TyBits - X->getType()->getScalarSizeInBits();
4748 } else
4749 Tmp =
4750 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4751 if (ShAmt->uge(Tmp))
4752 break; // Shifted all sign bits out.
4753 Tmp2 = ShAmt->getZExtValue();
4754 return Tmp - Tmp2;
4755 }
4756 break;
4757 }
4758 case Instruction::And:
4759 case Instruction::Or:
4760 case Instruction::Xor: // NOT is handled here.
4761 // Logical binary ops preserve the number of sign bits at the worst.
4762 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4763 if (Tmp != 1) {
4764 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4765 FirstAnswer = std::min(Tmp, Tmp2);
4766 // We computed what we know about the sign bits as our first
4767 // answer. Now proceed to the generic code that uses
4768 // computeKnownBits, and pick whichever answer is better.
4769 }
4770 break;
4771
4772 case Instruction::Select: {
4773 // If we have a clamp pattern, we know that the number of sign bits will
4774 // be the minimum of the clamp min/max range.
4775 const Value *X;
4776 const APInt *CLow, *CHigh;
4777 if (isSignedMinMaxClamp(U, X, CLow, CHigh))
4778 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4779
4780 Tmp = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4781 if (Tmp == 1)
4782 break;
4783 Tmp2 = ComputeNumSignBits(U->getOperand(2), DemandedElts, Q, Depth + 1);
4784 return std::min(Tmp, Tmp2);
4785 }
4786
4787 case Instruction::Add:
4788 // Add can have at most one carry bit. Thus we know that the output
4789 // is, at worst, one more bit than the inputs.
4790 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4791 if (Tmp == 1) break;
4792
4793 // Special case decrementing a value (ADD X, -1):
4794 if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
4795 if (CRHS->isAllOnesValue()) {
4796 KnownBits Known(TyBits);
4797 computeKnownBits(U->getOperand(0), DemandedElts, Known, Q, Depth + 1);
4798
4799 // If the input is known to be 0 or 1, the output is 0/-1, which is
4800 // all sign bits set.
4801 if ((Known.Zero | 1).isAllOnes())
4802 return TyBits;
4803
4804 // If we are subtracting one from a positive number, there is no carry
4805 // out of the result.
4806 if (Known.isNonNegative())
4807 return Tmp;
4808 }
4809
4810 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4811 if (Tmp2 == 1)
4812 break;
4813 return std::min(Tmp, Tmp2) - 1;
4814
4815 case Instruction::Sub:
4816 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4817 if (Tmp2 == 1)
4818 break;
4819
4820 // Handle NEG.
4821 if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
4822 if (CLHS->isNullValue()) {
4823 KnownBits Known(TyBits);
4824 computeKnownBits(U->getOperand(1), DemandedElts, Known, Q, Depth + 1);
4825 // If the input is known to be 0 or 1, the output is 0/-1, which is
4826 // all sign bits set.
4827 if ((Known.Zero | 1).isAllOnes())
4828 return TyBits;
4829
4830 // If the input is known to be positive (the sign bit is known clear),
4831 // the output of the NEG has the same number of sign bits as the
4832 // input.
4833 if (Known.isNonNegative())
4834 return Tmp2;
4835
4836 // Otherwise, we treat this like a SUB.
4837 }
4838
4839 // Sub can have at most one carry bit. Thus we know that the output
4840 // is, at worst, one more bit than the inputs.
4841 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4842 if (Tmp == 1)
4843 break;
4844 return std::min(Tmp, Tmp2) - 1;
4845
4846 case Instruction::Mul: {
4847 // The output of the Mul can be at most twice the valid bits in the
4848 // inputs.
4849 unsigned SignBitsOp0 =
4850 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4851 if (SignBitsOp0 == 1)
4852 break;
4853 unsigned SignBitsOp1 =
4854 ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4855 if (SignBitsOp1 == 1)
4856 break;
4857 unsigned OutValidBits =
4858 (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);
4859 return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
4860 }
4861
4862 case Instruction::PHI: {
4863 const PHINode *PN = cast<PHINode>(U);
4864 unsigned NumIncomingValues = PN->getNumIncomingValues();
4865 // Don't analyze large in-degree PHIs.
4866 if (NumIncomingValues > 4) break;
4867 // Unreachable blocks may have zero-operand PHI nodes.
4868 if (NumIncomingValues == 0) break;
4869
4870 // Take the minimum of all incoming values. This can't infinitely loop
4871 // because of our depth threshold.
4873 Tmp = TyBits;
4874 for (unsigned i = 0, e = NumIncomingValues; i != e; ++i) {
4875 if (Tmp == 1) return Tmp;
4876 RecQ.CtxI = PN->getIncomingBlock(i)->getTerminator();
4877 Tmp = std::min(Tmp, ComputeNumSignBits(PN->getIncomingValue(i),
4878 DemandedElts, RecQ, Depth + 1));
4879 }
4880 return Tmp;
4881 }
4882
4883 case Instruction::Trunc: {
4884 // If the input contained enough sign bits that some remain after the
4885 // truncation, then we can make use of that. Otherwise we don't know
4886 // anything.
4887 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4888 unsigned OperandTyBits = U->getOperand(0)->getType()->getScalarSizeInBits();
4889 if (Tmp > (OperandTyBits - TyBits))
4890 return Tmp - (OperandTyBits - TyBits);
4891
4892 return 1;
4893 }
4894
4895 case Instruction::ExtractElement:
4896 // Look through extract element. At the moment we keep this simple and
4897 // skip tracking the specific element. But at least we might find
4898 // information valid for all elements of the vector (for example if vector
4899 // is sign extended, shifted, etc).
4900 return ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4901
4902 case Instruction::ShuffleVector: {
4903 // Collect the minimum number of sign bits that are shared by every vector
4904 // element referenced by the shuffle.
4905 auto *Shuf = dyn_cast<ShuffleVectorInst>(U);
4906 if (!Shuf) {
4907 // FIXME: Add support for shufflevector constant expressions.
4908 return 1;
4909 }
4910 APInt DemandedLHS, DemandedRHS;
4911 // For undef elements, we don't know anything about the common state of
4912 // the shuffle result.
4913 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
4914 return 1;
4915 Tmp = std::numeric_limits<unsigned>::max();
4916 if (!!DemandedLHS) {
4917 const Value *LHS = Shuf->getOperand(0);
4918 Tmp = ComputeNumSignBits(LHS, DemandedLHS, Q, Depth + 1);
4919 }
4920 // If we don't know anything, early out and try computeKnownBits
4921 // fall-back.
4922 if (Tmp == 1)
4923 break;
4924 if (!!DemandedRHS) {
4925 const Value *RHS = Shuf->getOperand(1);
4926 Tmp2 = ComputeNumSignBits(RHS, DemandedRHS, Q, Depth + 1);
4927 Tmp = std::min(Tmp, Tmp2);
4928 }
4929 // If we don't know anything, early out and try computeKnownBits
4930 // fall-back.
4931 if (Tmp == 1)
4932 break;
4933 assert(Tmp <= TyBits && "Failed to determine minimum sign bits");
4934 return Tmp;
4935 }
4936 case Instruction::Call: {
4937 if (const auto *II = dyn_cast<IntrinsicInst>(U)) {
4938 switch (II->getIntrinsicID()) {
4939 default:
4940 break;
4941 case Intrinsic::abs:
4942 Tmp =
4943 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4944 if (Tmp == 1)
4945 break;
4946
4947 // Absolute value reduces number of sign bits by at most 1.
4948 return Tmp - 1;
4949 case Intrinsic::smin:
4950 case Intrinsic::smax: {
4951 const APInt *CLow, *CHigh;
4952 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
4953 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4954 }
4955 }
4956 }
4957 }
4958 }
4959 }
4960
4961 // Finally, if we can prove that the top bits of the result are 0's or 1's,
4962 // use this information.
4963
4964 // If we can examine all elements of a vector constant successfully, we're
4965 // done (we can't do any better than that). If not, keep trying.
4966 if (unsigned VecSignBits =
4967 computeNumSignBitsVectorConstant(V, DemandedElts, TyBits))
4968 return VecSignBits;
4969
4970 KnownBits Known(TyBits);
4971 computeKnownBits(V, DemandedElts, Known, Q, Depth);
4972
4973 // If we know that the sign bit is either zero or one, determine the number of
4974 // identical bits in the top of the input value.
4975 return std::max(FirstAnswer, Known.countMinSignBits());
4976}
4977
4979 const TargetLibraryInfo *TLI) {
4980 const Function *F = CB.getCalledFunction();
4981 if (!F)
4983
4984 if (F->isIntrinsic())
4985 return F->getIntrinsicID();
4986
4987 // We are going to infer semantics of a library function based on mapping it
4988 // to an LLVM intrinsic. Check that the library function is available from
4989 // this callbase and in this environment.
4990 if (F->hasLocalLinkage() || !TLI || !CB.onlyReadsMemory())
4992
4993 LibFunc Func = TLI->getLibFunc(CB);
4994 if (Func == NotLibFunc)
4996
4997 switch (Func) {
4998 default:
4999 break;
5000 case LibFunc_sin:
5001 case LibFunc_sinf:
5002 case LibFunc_sinl:
5003 return Intrinsic::sin;
5004 case LibFunc_cos:
5005 case LibFunc_cosf:
5006 case LibFunc_cosl:
5007 return Intrinsic::cos;
5008 case LibFunc_tan:
5009 case LibFunc_tanf:
5010 case LibFunc_tanl:
5011 return Intrinsic::tan;
5012 case LibFunc_asin:
5013 case LibFunc_asinf:
5014 case LibFunc_asinl:
5015 return Intrinsic::asin;
5016 case LibFunc_acos:
5017 case LibFunc_acosf:
5018 case LibFunc_acosl:
5019 return Intrinsic::acos;
5020 case LibFunc_atan:
5021 case LibFunc_atanf:
5022 case LibFunc_atanl:
5023 return Intrinsic::atan;
5024 case LibFunc_atan2:
5025 case LibFunc_atan2f:
5026 case LibFunc_atan2l:
5027 return Intrinsic::atan2;
5028 case LibFunc_sinh:
5029 case LibFunc_sinhf:
5030 case LibFunc_sinhl:
5031 return Intrinsic::sinh;
5032 case LibFunc_cosh:
5033 case LibFunc_coshf:
5034 case LibFunc_coshl:
5035 return Intrinsic::cosh;
5036 case LibFunc_tanh:
5037 case LibFunc_tanhf:
5038 case LibFunc_tanhl:
5039 return Intrinsic::tanh;
5040 case LibFunc_exp:
5041 case LibFunc_expf:
5042 case LibFunc_expl:
5043 return Intrinsic::exp;
5044 case LibFunc_exp2:
5045 case LibFunc_exp2f:
5046 case LibFunc_exp2l:
5047 return Intrinsic::exp2;
5048 case LibFunc_exp10:
5049 case LibFunc_exp10f:
5050 case LibFunc_exp10l:
5051 return Intrinsic::exp10;
5052 case LibFunc_log:
5053 case LibFunc_logf:
5054 case LibFunc_logl:
5055 return Intrinsic::log;
5056 case LibFunc_log10:
5057 case LibFunc_log10f:
5058 case LibFunc_log10l:
5059 return Intrinsic::log10;
5060 case LibFunc_log2:
5061 case LibFunc_log2f:
5062 case LibFunc_log2l:
5063 return Intrinsic::log2;
5064 case LibFunc_fabs:
5065 case LibFunc_fabsf:
5066 case LibFunc_fabsl:
5067 return Intrinsic::fabs;
5068 case LibFunc_fmin:
5069 case LibFunc_fminf:
5070 case LibFunc_fminl:
5071 return Intrinsic::minnum;
5072 case LibFunc_fmax:
5073 case LibFunc_fmaxf:
5074 case LibFunc_fmaxl:
5075 return Intrinsic::maxnum;
5076 case LibFunc_copysign:
5077 case LibFunc_copysignf:
5078 case LibFunc_copysignl:
5079 return Intrinsic::copysign;
5080 case LibFunc_floor:
5081 case LibFunc_floorf:
5082 case LibFunc_floorl:
5083 return Intrinsic::floor;
5084 case LibFunc_ceil:
5085 case LibFunc_ceilf:
5086 case LibFunc_ceill:
5087 return Intrinsic::ceil;
5088 case LibFunc_trunc:
5089 case LibFunc_truncf:
5090 case LibFunc_truncl:
5091 return Intrinsic::trunc;
5092 case LibFunc_rint:
5093 case LibFunc_rintf:
5094 case LibFunc_rintl:
5095 return Intrinsic::rint;
5096 case LibFunc_nearbyint:
5097 case LibFunc_nearbyintf:
5098 case LibFunc_nearbyintl:
5099 return Intrinsic::nearbyint;
5100 case LibFunc_round:
5101 case LibFunc_roundf:
5102 case LibFunc_roundl:
5103 return Intrinsic::round;
5104 case LibFunc_roundeven:
5105 case LibFunc_roundevenf:
5106 case LibFunc_roundevenl:
5107 return Intrinsic::roundeven;
5108 case LibFunc_pow:
5109 case LibFunc_powf:
5110 case LibFunc_powl:
5111 return Intrinsic::pow;
5112 case LibFunc_sqrt:
5113 case LibFunc_sqrtf:
5114 case LibFunc_sqrtl:
5115 return Intrinsic::sqrt;
5116 }
5117
5119}
5120
5121/// Given an exploded icmp instruction, return true if the comparison only
5122/// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if
5123/// the result of the comparison is true when the input value is signed.
5125 bool &TrueIfSigned) {
5126 switch (Pred) {
5127 case ICmpInst::ICMP_SLT: // True if LHS s< 0
5128 TrueIfSigned = true;
5129 return RHS.isZero();
5130 case ICmpInst::ICMP_SLE: // True if LHS s<= -1
5131 TrueIfSigned = true;
5132 return RHS.isAllOnes();
5133 case ICmpInst::ICMP_SGT: // True if LHS s> -1
5134 TrueIfSigned = false;
5135 return RHS.isAllOnes();
5136 case ICmpInst::ICMP_SGE: // True if LHS s>= 0
5137 TrueIfSigned = false;
5138 return RHS.isZero();
5139 case ICmpInst::ICMP_UGT:
5140 // True if LHS u> RHS and RHS == sign-bit-mask - 1
5141 TrueIfSigned = true;
5142 return RHS.isMaxSignedValue();
5143 case ICmpInst::ICMP_UGE:
5144 // True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
5145 TrueIfSigned = true;
5146 return RHS.isMinSignedValue();
5147 case ICmpInst::ICMP_ULT:
5148 // True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
5149 TrueIfSigned = false;
5150 return RHS.isMinSignedValue();
5151 case ICmpInst::ICMP_ULE:
5152 // True if LHS u<= RHS and RHS == sign-bit-mask - 1
5153 TrueIfSigned = false;
5154 return RHS.isMaxSignedValue();
5155 default:
5156 return false;
5157 }
5158}
5159
5161 bool CondIsTrue,
5162 const Instruction *CtxI,
5163 KnownFPClass &KnownFromContext,
5164 unsigned Depth = 0) {
5165 Value *A, *B;
5167 (CondIsTrue ? match(Cond, m_LogicalAnd(m_Value(A), m_Value(B)))
5168 : match(Cond, m_LogicalOr(m_Value(A), m_Value(B))))) {
5169 computeKnownFPClassFromCond(V, A, CondIsTrue, CtxI, KnownFromContext,
5170 Depth + 1);
5171 computeKnownFPClassFromCond(V, B, CondIsTrue, CtxI, KnownFromContext,
5172 Depth + 1);
5173 return;
5174 }
5176 computeKnownFPClassFromCond(V, A, !CondIsTrue, CtxI, KnownFromContext,
5177 Depth + 1);
5178 return;
5179 }
5181 Value *LHS;
5182 uint64_t ClassVal = 0;
5183 const APFloat *CRHS;
5184 const APInt *RHS;
5185 if (match(Cond, m_FCmp(Pred, m_Value(LHS), m_APFloat(CRHS)))) {
5186 auto [CmpVal, MaskIfTrue, MaskIfFalse] = fcmpImpliesClass(
5188 LHS != V);
5189 if (CmpVal == V)
5190 KnownFromContext.knownNot(~(CondIsTrue ? MaskIfTrue : MaskIfFalse));
5192 m_Specific(V), m_ConstantInt(ClassVal)))) {
5193 FPClassTest Mask = static_cast<FPClassTest>(ClassVal);
5194 KnownFromContext.knownNot(CondIsTrue ? ~Mask : Mask);
5196 m_APInt(RHS)))) {
5197 bool TrueIfSigned;
5198 if (!isSignBitCheck(Pred, *RHS, TrueIfSigned))
5199 return;
5200 if (TrueIfSigned == CondIsTrue)
5201 KnownFromContext.signBitMustBeOne();
5202 else
5203 KnownFromContext.signBitMustBeZero();
5204 }
5205}
5206
5207/// Compute the minimum and maximum values (inclusive) for the exponent of \p V,
5208/// assuming it is not nan. Returns {min, max, max-assuming-nonzero}. A value
5209/// frexp(0) = 0, so the tighter max-assuming-nonzero bound is only usable when
5210/// \p V is known not to be a logical zero (e.g., for fabs(x) < 0.25, the non-0
5211/// exponent range is [-149, -2], but the 0 edge case is above this range).
5212static std::tuple<int, int, int>
5214 if (!Q.CtxI || !Q.DC || !Q.DT)
5216
5217 // Intersect the bounds implied by every dominating condition, keeping the
5218 // tightest maximum. A value may participate in multiple compares
5219 // (e.g. fabs(x) < 2.0 and fabs(x) < 1.0), and the tighter one wins.
5220 int MaxExp = APFloat::IEK_Inf;
5221 int MaxExpNonZero = APFloat::IEK_Inf;
5222
5223 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
5225 const APFloat *LimitC;
5226 if (!match(BI->getCondition(),
5227 m_FCmp(Pred, m_FAbs(m_Specific(V)), m_Finite(LimitC))))
5228 continue;
5229
5232 continue;
5233
5234 // If fabs(x) <= K, implies the exponent min exp range.
5235 // if fabs(x) >= K, swap the successor
5236 bool IsLessEqual =
5240
5241 bool KnownStrictlyLess =
5244
5245 BasicBlockEdge Edge1(BI->getParent(),
5246 BI->getSuccessor(IsLessEqual ? 0 : 1));
5247 if (Q.DT->dominates(Edge1, Q.CtxI->getParent())) {
5248 // frexp returns an exponent one greater than ilogb.
5249 int Exp = ilogb(*LimitC) + 1;
5250
5251 // A strict bound fabs(V) < 2^n forces ilogb(V) <= n - 1, so the max frexp
5252 // exponent drops by one when K is exact power of two.
5253 if (KnownStrictlyLess && LimitC->getExactLog2Abs() != INT_MIN)
5254 --Exp;
5255
5256 // frexp(0) = 0, which the bound above (assuming a normal nonzero value)
5257 // may exclude.
5258
5259 // TODO: Figure out lower bound to detect no-underflow.
5260 MaxExpNonZero = std::min(MaxExpNonZero, Exp);
5261 MaxExp = std::min(MaxExp, std::max(Exp, 0));
5262 }
5263 }
5264
5265 return {APFloat::IEK_NaN, MaxExp, MaxExpNonZero};
5266}
5267
5269 const SimplifyQuery &Q) {
5270 KnownFPClass KnownFromContext;
5271
5272 if (Q.CC && Q.CC->AffectedValues.contains(V))
5274 KnownFromContext);
5275
5276 if (!Q.CtxI)
5277 return KnownFromContext;
5278
5279 if (Q.DC && Q.DT) {
5280 // Handle dominating conditions.
5281 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
5282 Value *Cond = BI->getCondition();
5283
5284 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
5285 if (Q.DT->dominates(Edge0, Q.CtxI->getParent()))
5286 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/true, Q.CtxI,
5287 KnownFromContext);
5288
5289 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
5290 if (Q.DT->dominates(Edge1, Q.CtxI->getParent()))
5291 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/false, Q.CtxI,
5292 KnownFromContext);
5293 }
5294 }
5295
5296 if (!Q.AC)
5297 return KnownFromContext;
5298
5299 // Try to restrict the floating-point classes based on information from
5300 // assumptions.
5301 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
5302 if (!AssumeVH)
5303 continue;
5304 CallInst *I = cast<CallInst>(AssumeVH);
5305
5306 assert(I->getFunction() == Q.CtxI->getParent()->getParent() &&
5307 "Got assumption for the wrong function!");
5308 assert(I->getIntrinsicID() == Intrinsic::assume &&
5309 "must be an assume intrinsic");
5310
5311 if (!isValidAssumeForContext(I, Q))
5312 continue;
5313
5314 computeKnownFPClassFromCond(V, I->getArgOperand(0),
5315 /*CondIsTrue=*/true, Q.CtxI, KnownFromContext);
5316 }
5317
5318 return KnownFromContext;
5319}
5320
5322 Value *Arm, bool Invert,
5323 const SimplifyQuery &SQ,
5324 unsigned Depth) {
5325
5326 KnownFPClass KnownSrc;
5328 /*CondIsTrue=*/!Invert, SQ.CtxI, KnownSrc,
5329 Depth + 1);
5330 KnownSrc = KnownSrc.unionWith(Known);
5331 if (KnownSrc.isUnknown())
5332 return;
5333
5334 if (isGuaranteedNotToBeUndef(Arm, SQ.AC, SQ.CtxI, SQ.DT, Depth + 1))
5335 Known = KnownSrc;
5336}
5337
5338void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
5339 FPClassTest InterestedClasses, KnownFPClass &Known,
5340 const SimplifyQuery &Q, unsigned Depth);
5341
5343 FPClassTest InterestedClasses,
5344 const SimplifyQuery &Q, unsigned Depth) {
5345 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
5346 APInt DemandedElts =
5347 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
5348 computeKnownFPClass(V, DemandedElts, InterestedClasses, Known, Q, Depth);
5349}
5350
5352 const APInt &DemandedElts,
5353 FPClassTest InterestedClasses,
5355 const SimplifyQuery &Q,
5356 unsigned Depth) {
5357 if ((InterestedClasses &
5359 return;
5360
5361 KnownFPClass KnownSrc;
5362 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5363 KnownSrc, Q, Depth + 1);
5364
5365 const fltSemantics &DstTy = Op->getType()->getScalarType()->getFltSemantics();
5366 const fltSemantics &SrcTy =
5367 Op->getOperand(0)->getType()->getScalarType()->getFltSemantics();
5368
5369 const auto *I = dyn_cast<Instruction>(Op);
5370 const Function *F = I ? I->getFunction() : nullptr;
5371 DenormalMode DstMode =
5372 F ? F->getDenormalMode(DstTy) : DenormalMode::getDynamic();
5373 DenormalMode SrcMode =
5374 F ? F->getDenormalMode(SrcTy) : DenormalMode::getDynamic();
5375
5376 Known = KnownFPClass::fptrunc(KnownSrc, DstTy, SrcTy, DstMode, SrcMode);
5377}
5378
5379static void
5381 FPClassTest InterestedClasses,
5382 KnownFPClass &Known, const SimplifyQuery &Q,
5383 unsigned Depth, bool IsAdd, bool IsStrict) {
5384 const Value *LHS = Op->getOperand(0);
5385 const Value *RHS = Op->getOperand(1);
5386
5387 const RoundingMode RM =
5388 IsStrict ? cast<ConstrainedFPIntrinsic>(Op)->getRoundingMode().value_or(
5391
5392 KnownFPClass KnownLHS, KnownRHS;
5393 bool WantNegative =
5394 IsAdd &&
5395 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;
5396 bool WantNaN = (InterestedClasses & fcNan) != fcNone;
5397 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;
5398 bool WantSubnormal = (InterestedClasses & fcSubnormal) != fcNone;
5399
5400 if (!WantNaN && !WantNegative && !WantNegZero && !WantSubnormal)
5401 return;
5402
5403 FPClassTest InterestedSrcs = InterestedClasses;
5404 if (WantNegative)
5405 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5406 if (InterestedClasses & fcNan)
5407 InterestedSrcs |= fcInf;
5408 computeKnownFPClass(RHS, DemandedElts, InterestedSrcs, KnownRHS, Q,
5409 Depth + 1);
5410
5411 // Special case fadd x, x, which is the canonical form of fmul x, 2.
5412 bool Self = LHS == RHS &&
5413 isGuaranteedNotToBeUndef(LHS, Q.AC, Q.CtxI, Q.DT, Depth + 1);
5414 if (Self)
5415 KnownLHS = KnownRHS;
5416
5417 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||
5418 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) || WantNegZero ||
5419 WantSubnormal || !IsAdd) {
5420
5421 // FIXME: Context function should always be passed in separately
5422 const Function *F = cast<Instruction>(Op)->getFunction();
5423 const fltSemantics &FltSem =
5424 Op->getType()->getScalarType()->getFltSemantics();
5426 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5427
5428 if (Self && IsAdd) {
5429 Known = KnownFPClass::fadd_self(KnownLHS, RM, Mode);
5430 } else {
5431 // RHS is canonically cheaper to compute. Skip inspecting the LHS if
5432 // there's no point.
5433
5434 if (!Self) {
5435 computeKnownFPClass(LHS, DemandedElts, InterestedSrcs, KnownLHS, Q,
5436 Depth + 1);
5437 }
5438
5439 if (const APFloat *CRHS; IsAdd && match(RHS, m_APFloat(CRHS))) {
5440 Known = KnownFPClass::fadd(KnownLHS, *CRHS, RM, Mode);
5441 return;
5442 }
5443
5444 Known = IsAdd ? KnownFPClass::fadd(KnownLHS, KnownRHS, RM, Mode)
5445 : KnownFPClass::fsub(KnownLHS, KnownRHS, RM, Mode);
5446 }
5447 }
5448}
5449
5451 const APInt &DemandedElts,
5452 FPClassTest InterestedClasses,
5454 const SimplifyQuery &Q, unsigned Depth) {
5455 const Value *LHS = Op->getOperand(0);
5456 const Value *RHS = Op->getOperand(1);
5457
5458 FPClassTest InterestedLHS = fcNone;
5459 FPClassTest InterestedRHS = fcNone;
5460
5461 // NaN is also generated for frem(Inf, x) and frem(x, 0.0).
5462 if (InterestedClasses & fcNan) {
5463 InterestedLHS |= fcNan | fcInf;
5464 InterestedRHS |= fcNan | fcZero | fcSubnormal;
5465 }
5466
5467 // The sign for frem is the same as the first operand.
5468 if (InterestedClasses & (fcPosNormal | fcPosSubnormal))
5469 InterestedLHS |= fcPosNormal | fcPosSubnormal;
5470 if (InterestedClasses & (fcNegNormal | fcNegSubnormal))
5471 InterestedLHS |= fcNegNormal | fcNegSubnormal;
5472
5473 // A negative zero result requires a negative finite first operand.
5474 if (InterestedClasses & fcNegZero)
5475 InterestedLHS |= fcNegFinite;
5476
5477 // A positive zero result can additionally come from a negative finite
5478 // result being flushed to positive zero.
5479 if (InterestedClasses & fcPosZero)
5480 InterestedLHS |= fcPosFinite | fcNegNormal | fcNegSubnormal;
5481
5482 const Function *F = cast<Instruction>(Op)->getFunction();
5484 F ? F->getDenormalMode(Op->getType()->getScalarType()->getFltSemantics())
5486
5487 if (LHS == RHS && isGuaranteedNotToBeUndef(LHS, Q.AC, Q.CtxI, Q.DT)) {
5488 // X % X is always exactly [+-]0.0 or a NaN.
5489 FPClassTest InterestedSrcs = InterestedLHS | InterestedRHS;
5490 KnownFPClass KnownSrc;
5491 if (InterestedSrcs != fcNone)
5492 computeKnownFPClass(LHS, DemandedElts, InterestedSrcs, KnownSrc, Q,
5493 Depth + 1);
5494 Known = KnownFPClass::frem_self(KnownSrc, Mode);
5495 return;
5496 }
5497
5498 KnownFPClass KnownLHS;
5499 if (InterestedLHS != fcNone)
5500 computeKnownFPClass(LHS, DemandedElts, InterestedLHS, KnownLHS, Q,
5501 Depth + 1);
5502
5503 KnownFPClass KnownRHS;
5504 // RHS is only useful for refining NaN classes.
5505 if (InterestedRHS != fcNone && KnownLHS.isKnownNever(fcSNan))
5506 computeKnownFPClass(RHS, DemandedElts, InterestedRHS, KnownRHS, Q,
5507 Depth + 1);
5508
5509 Known = KnownFPClass::frem(KnownLHS, KnownRHS, Mode);
5510}
5511
5513 switch (IID) {
5514 case Intrinsic::minimum:
5516 case Intrinsic::maximum:
5518 case Intrinsic::minimumnum:
5520 case Intrinsic::maximumnum:
5522 case Intrinsic::minnum:
5524 case Intrinsic::maxnum:
5526 default:
5527 llvm_unreachable("not a floating-point min-max intrinsic");
5528 }
5529}
5530
5531/// \return true if this is a floating point value that is known to have a
5532/// magnitude smaller than 1. i.e., fabs(X) <= 1.0 or is nan.
5533static bool isAbsoluteValueULEOne(const Value *V) {
5534 // TODO: Handle frexp
5535 // TODO: Other rounding intrinsics?
5536 // TODO: Try computeKnownExponentRangeFromContext
5537
5538 // fabs(x - floor(x)) <= 1
5539 const Value *SubFloorX;
5540 if (match(V, m_FSub(m_Value(SubFloorX),
5542 return true;
5543
5546}
5547
5548void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
5549 FPClassTest InterestedClasses, KnownFPClass &Known,
5550 const SimplifyQuery &Q, unsigned Depth) {
5551 assert(Known.isUnknown() && "should not be called with known information");
5552
5553 if (!DemandedElts) {
5554 // No demanded elts, better to assume we don't know anything.
5555 Known.resetAll();
5556 return;
5557 }
5558
5559 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
5560
5561 if (auto *CFP = dyn_cast<ConstantFP>(V)) {
5562 Known = KnownFPClass(CFP->getValueAPF());
5563 return;
5564 }
5565
5567 Known.setKnownFPClasses(fcPosZero);
5568 return;
5569 }
5570
5571 if (isa<PoisonValue>(V)) {
5573 return;
5574 }
5575
5576 // Try to handle fixed width vector constants
5577 auto *VFVTy = dyn_cast<FixedVectorType>(V->getType());
5578 const Constant *CV = dyn_cast<Constant>(V);
5579 if (VFVTy && CV) {
5581
5582 // For vectors, verify that each element is not NaN.
5583 unsigned NumElts = VFVTy->getNumElements();
5584 for (unsigned i = 0; i != NumElts; ++i) {
5585 if (!DemandedElts[i])
5586 continue;
5587
5588 Constant *Elt = CV->getAggregateElement(i);
5589 if (!Elt) {
5590 Known = KnownFPClass();
5591 return;
5592 }
5593 if (isa<PoisonValue>(Elt))
5594 continue;
5595 auto *CElt = dyn_cast<ConstantFP>(Elt);
5596 if (!CElt) {
5597 Known = KnownFPClass();
5598 return;
5599 }
5600
5601 Known |= KnownFPClass(CElt->getValueAPF());
5602 }
5603 return;
5604 }
5605
5606 if (const auto *CDS = dyn_cast<ConstantDataSequential>(V)) {
5608 for (size_t I = 0, E = CDS->getNumElements(); I != E; ++I)
5609 Known |= KnownFPClass(CDS->getElementAsAPFloat(I));
5610 return;
5611 }
5612
5613 if (const auto *CA = dyn_cast<ConstantAggregate>(V)) {
5614 // TODO: Handle complex aggregates
5616 for (const Use &Op : CA->operands()) {
5617 auto *CFP = dyn_cast<ConstantFP>(Op.get());
5618 if (!CFP) {
5619 Known = KnownFPClass();
5620 return;
5621 }
5622
5623 Known |= KnownFPClass(CFP->getValueAPF());
5624 }
5625
5626 return;
5627 }
5628
5629 FPClassTest KnownNotFromFlags = fcNone;
5630 if (const auto *CB = dyn_cast<CallBase>(V))
5631 KnownNotFromFlags |= CB->getRetNoFPClass();
5632 else if (const auto *Arg = dyn_cast<Argument>(V))
5633 KnownNotFromFlags |= Arg->getNoFPClass();
5634
5635 const Operator *Op = dyn_cast<Operator>(V);
5637 if (FPOp->hasNoNaNs())
5638 KnownNotFromFlags |= fcNan;
5639 if (FPOp->hasNoInfs())
5640 KnownNotFromFlags |= fcInf;
5641 }
5642
5643 KnownFPClass AssumedClasses = computeKnownFPClassFromContext(V, Q);
5644 KnownNotFromFlags |= ~AssumedClasses.getKnownFPClasses();
5645
5646 // We no longer need to find out about these bits from inputs if we can
5647 // assume this from flags/attributes.
5648 InterestedClasses &= ~KnownNotFromFlags;
5649
5650 llvm::scope_exit ClearClassesFromFlags([=, &Known] {
5651 Known.knownNot(KnownNotFromFlags);
5652 if (!Known.getSignBit() && AssumedClasses.getSignBit()) {
5653 if (*AssumedClasses.getSignBit())
5654 Known.signBitMustBeOne();
5655 else
5656 Known.signBitMustBeZero();
5657 }
5658 });
5659
5660 if (!Op)
5661 return;
5662
5663 // All recursive calls that increase depth must come after this.
5665 return;
5666
5667 const unsigned Opc = Op->getOpcode();
5668 switch (Opc) {
5669 case Instruction::FNeg: {
5670 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5671 Known, Q, Depth + 1);
5672 Known.fneg();
5673 break;
5674 }
5675 case Instruction::Select: {
5676 auto ComputeForArm = [&](Value *Arm, bool Invert) {
5677 KnownFPClass Res;
5678 computeKnownFPClass(Arm, DemandedElts, InterestedClasses, Res, Q,
5679 Depth + 1);
5680 adjustKnownFPClassForSelectArm(Res, Op->getOperand(0), Arm, Invert, Q,
5681 Depth);
5682 return Res;
5683 };
5684 // Only known if known in both the LHS and RHS.
5685 Known =
5686 ComputeForArm(Op->getOperand(1), /*Invert=*/false)
5687 .intersectWith(ComputeForArm(Op->getOperand(2), /*Invert=*/true));
5688 break;
5689 }
5690 case Instruction::Load: {
5691 const MDNode *NoFPClass =
5692 cast<LoadInst>(Op)->getMetadata(LLVMContext::MD_nofpclass);
5693 if (!NoFPClass)
5694 break;
5695
5696 ConstantInt *MaskVal =
5698 Known.knownNot(static_cast<FPClassTest>(MaskVal->getZExtValue()));
5699 break;
5700 }
5701 case Instruction::Call: {
5702 const CallInst *II = cast<CallInst>(Op);
5703 const Intrinsic::ID IID = II->getIntrinsicID();
5704 switch (IID) {
5705 case Intrinsic::fabs: {
5706 if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) {
5707 // If we only care about the sign bit we don't need to inspect the
5708 // operand.
5709 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5710 InterestedClasses, Known, Q, Depth + 1);
5711 }
5712
5713 Known.fabs();
5714 break;
5715 }
5716 case Intrinsic::copysign: {
5717 KnownFPClass KnownSign;
5718
5719 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5720 Known, Q, Depth + 1);
5721 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5722 KnownSign, Q, Depth + 1);
5723 Known.copysign(KnownSign);
5724 break;
5725 }
5726 case Intrinsic::fma:
5727 case Intrinsic::fmuladd: {
5728 if ((InterestedClasses & (fcNegative | fcNan)) == fcNone)
5729 break;
5730
5731 const bool IsFMA = IID == Intrinsic::fma;
5732
5733 // FIXME: This should check isGuaranteedNotToBeUndef
5734 if (II->getArgOperand(0) == II->getArgOperand(1)) {
5735 FPClassTest InterestedSquared = InterestedClasses;
5736 FPClassTest InterestedAddend = InterestedClasses;
5737 // Only fma(+inf, +inf, -inf) and fma(-inf, -inf, -inf) produce NaN when
5738 // squaring.
5739 if (InterestedClasses & fcQNan) {
5740 InterestedSquared |= fcSNan | fcInf;
5741 InterestedAddend |= fcSNan | fcNegInf;
5742 }
5743
5744 KnownFPClass KnownSrc, KnownAddend;
5745 computeKnownFPClass(II->getArgOperand(2), DemandedElts,
5746 InterestedAddend, KnownAddend, Q, Depth + 1);
5747 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5748 InterestedSquared, KnownSrc, Q, Depth + 1);
5749
5750 const Function *F = II->getFunction();
5751 const fltSemantics &FltSem =
5752 II->getType()->getScalarType()->getFltSemantics();
5754 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5755
5756 if ((KnownNotFromFlags & fcNan) == fcNan) {
5757 KnownSrc.knownNot(fcNan);
5758 KnownAddend.knownNot(fcNan);
5759 }
5760
5761 if ((KnownNotFromFlags & fcInf) == fcInf) {
5762 KnownSrc.knownNot(fcInf);
5763 KnownAddend.knownNot(fcInf);
5764 }
5765
5766 Known = IsFMA
5767 ? KnownFPClass::fma_square(KnownSrc, KnownAddend, Mode)
5768 : KnownFPClass::fmuladd_square(KnownSrc, KnownAddend, Mode);
5769 break;
5770 }
5771
5772 KnownFPClass KnownSrc[3];
5773 for (int I = 0; I != 3; ++I) {
5774 computeKnownFPClass(II->getArgOperand(I), DemandedElts,
5775 InterestedClasses, KnownSrc[I], Q, Depth + 1);
5776 if (KnownSrc[I].isUnknown())
5777 return;
5778
5779 if ((KnownNotFromFlags & fcNan) == fcNan)
5780 KnownSrc[I].knownNot(fcNan);
5781 if ((KnownNotFromFlags & fcInf) == fcInf)
5782 KnownSrc[I].knownNot(fcInf);
5783 }
5784
5785 const Function *F = II->getFunction();
5786 const fltSemantics &FltSem =
5787 II->getType()->getScalarType()->getFltSemantics();
5789 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5790 Known =
5791 IsFMA ? KnownFPClass::fma(KnownSrc[0], KnownSrc[1], KnownSrc[2], Mode)
5792 : KnownFPClass::fmuladd(KnownSrc[0], KnownSrc[1], KnownSrc[2],
5793 Mode);
5794 break;
5795 }
5796 case Intrinsic::experimental_constrained_fadd:
5797 case Intrinsic::experimental_constrained_fsub: {
5799 Op, DemandedElts, InterestedClasses, Known, Q, Depth,
5800 /*IsAdd=*/IID == Intrinsic::experimental_constrained_fadd,
5801 /*IsStrict=*/true);
5802 break;
5803 }
5804 case Intrinsic::experimental_constrained_frem: {
5805 computeKnownFPClassForFRem(Op, DemandedElts, InterestedClasses, Known, Q,
5806 Depth);
5807 break;
5808 }
5809 case Intrinsic::sqrt:
5810 case Intrinsic::experimental_constrained_sqrt: {
5811 KnownFPClass KnownSrc;
5812 FPClassTest InterestedSrcs = InterestedClasses;
5813 if (InterestedClasses & fcQNan)
5815 // Subnormals may flush to zero.
5816 if (InterestedClasses & fcZero)
5817 InterestedSrcs |= fcSubnormal;
5818
5819 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5820 KnownSrc, Q, Depth + 1);
5821
5823
5824 bool HasNSZ = Q.IIQ.hasNoSignedZeros(II);
5825 if (!HasNSZ) {
5826 const Function *F = II->getFunction();
5827 const fltSemantics &FltSem =
5828 II->getType()->getScalarType()->getFltSemantics();
5829 Mode = F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5830 }
5831
5832 Known = KnownFPClass::sqrt(KnownSrc, Mode);
5833 if (HasNSZ)
5834 Known.knownNot(fcNegZero);
5835
5836 break;
5837 }
5838 case Intrinsic::sin: {
5839 KnownFPClass KnownSrc;
5840 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5841 KnownSrc, Q, Depth + 1);
5842 Known = KnownFPClass::sin(KnownSrc);
5843 break;
5844 }
5845 case Intrinsic::cos: {
5846 KnownFPClass KnownSrc;
5847 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5848 KnownSrc, Q, Depth + 1);
5849 Known = KnownFPClass::cos(KnownSrc);
5850 break;
5851 }
5852 case Intrinsic::tan: {
5853 KnownFPClass KnownSrc;
5854 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5855 KnownSrc, Q, Depth + 1);
5856 Known = KnownFPClass::tan(KnownSrc);
5857 break;
5858 }
5859 case Intrinsic::sinh: {
5860 KnownFPClass KnownSrc;
5861 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5862 KnownSrc, Q, Depth + 1);
5863 Known = KnownFPClass::sinh(KnownSrc);
5864 break;
5865 }
5866 case Intrinsic::cosh: {
5867 KnownFPClass KnownSrc;
5868 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5869 KnownSrc, Q, Depth + 1);
5870 Known = KnownFPClass::cosh(KnownSrc);
5871 break;
5872 }
5873 case Intrinsic::tanh: {
5874 KnownFPClass KnownSrc;
5875 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5876 KnownSrc, Q, Depth + 1);
5877 Known = KnownFPClass::tanh(KnownSrc);
5878 break;
5879 }
5880 case Intrinsic::asin: {
5881 KnownFPClass KnownSrc;
5882 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5883 KnownSrc, Q, Depth + 1);
5884 Known = KnownFPClass::asin(KnownSrc);
5885 break;
5886 }
5887 case Intrinsic::acos: {
5888 KnownFPClass KnownSrc;
5889 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5890 KnownSrc, Q, Depth + 1);
5891 Known = KnownFPClass::acos(KnownSrc);
5892 break;
5893 }
5894 case Intrinsic::atan: {
5895 KnownFPClass KnownSrc;
5896 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5897 KnownSrc, Q, Depth + 1);
5898 Known = KnownFPClass::atan(KnownSrc);
5899 break;
5900 }
5901 case Intrinsic::atan2: {
5902 FPClassTest InterestedY = InterestedClasses;
5903 FPClassTest InterestedX = InterestedClasses;
5904
5905 // We can rule out negative values if y cannot have a negative value.
5906 if ((InterestedClasses & fcNegFinite) != fcNone)
5907 InterestedY |= fcNegative;
5908
5909 // We can rule out positive values if y cannot have a positive value.
5910 if ((InterestedClasses & fcPosFinite) != fcNone)
5911 InterestedY |= fcPositive | fcNegSubnormal;
5912
5913 // We can rule out zero and subnormal if x cannot have a positive value.
5914 if ((InterestedClasses & (fcZero | fcSubnormal)) != fcNone)
5915 InterestedX |= fcPositive | fcNegSubnormal;
5916
5917 KnownFPClass KnownY, KnownX;
5918 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedY,
5919 KnownY, Q, Depth + 1);
5920 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedX,
5921 KnownX, Q, Depth + 1);
5922
5923 const Function *F = II->getFunction();
5925 F ? F->getDenormalMode(
5926 II->getType()->getScalarType()->getFltSemantics())
5928 Known = KnownFPClass::atan2(KnownY, KnownX, Mode);
5929 break;
5930 }
5931 case Intrinsic::maxnum:
5932 case Intrinsic::minnum:
5933 case Intrinsic::minimum:
5934 case Intrinsic::maximum:
5935 case Intrinsic::minimumnum:
5936 case Intrinsic::maximumnum: {
5937 KnownFPClass KnownLHS, KnownRHS;
5938 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5939 KnownLHS, Q, Depth + 1);
5940 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5941 KnownRHS, Q, Depth + 1);
5942
5943 const Function *F = II->getFunction();
5944
5946 F ? F->getDenormalMode(
5947 II->getType()->getScalarType()->getFltSemantics())
5949
5950 Known = KnownFPClass::minMaxLike(KnownLHS, KnownRHS, getMinMaxKind(IID),
5951 Mode);
5952 break;
5953 }
5954 case Intrinsic::canonicalize: {
5955 KnownFPClass KnownSrc;
5956 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5957 KnownSrc, Q, Depth + 1);
5958
5959 const Function *F = II->getFunction();
5960 const fltSemantics &Flt =
5961 II->getType()->getScalarType()->getFltSemantics();
5963 F ? F->getDenormalMode(Flt) : DenormalMode::getDynamic();
5964 Known = KnownFPClass::canonicalize(KnownSrc, Flt, Mode);
5965 break;
5966 }
5967 case Intrinsic::vector_reduce_fmax:
5968 case Intrinsic::vector_reduce_fmin:
5969 case Intrinsic::vector_reduce_fmaximum:
5970 case Intrinsic::vector_reduce_fminimum:
5971 case Intrinsic::vector_reduce_fmaximumnum:
5972 case Intrinsic::vector_reduce_fminimumnum: {
5973 // reduce min/max will choose an element from one of the vector elements,
5974 // so we can infer and class information that is common to all elements.
5975 Known = computeKnownFPClass(II->getArgOperand(0), II->getFastMathFlags(),
5976 InterestedClasses, Q, Depth + 1);
5977 // Can only propagate sign if output is never NaN.
5978 if (!Known.isKnownNeverNaN())
5979 Known.setSignBit(std::nullopt);
5980 break;
5981 }
5982 // reverse preserves all characteristics of the input vec's element.
5983 case Intrinsic::vector_reverse:
5985 II->getArgOperand(0), DemandedElts.reverseBits(),
5986 II->getFastMathFlags(), InterestedClasses, Q, Depth + 1);
5987 break;
5988 case Intrinsic::trunc:
5989 case Intrinsic::floor:
5990 case Intrinsic::ceil:
5991 case Intrinsic::rint:
5992 case Intrinsic::nearbyint:
5993 case Intrinsic::round:
5994 case Intrinsic::roundeven: {
5995 KnownFPClass KnownSrc;
5996 FPClassTest InterestedSrcs = InterestedClasses;
5997
5998 // Negative round ups towards zero produce negative zero.
5999 if (InterestedSrcs & fcNegFinite)
6000 InterestedSrcs |= fcNegFinite;
6001
6002 // Negative subnormals may flush to positive zero.
6003 if (InterestedSrcs & fcPosFinite)
6004 InterestedSrcs |= fcPosFinite | fcNegSubnormal;
6005
6006 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
6007 KnownSrc, Q, Depth + 1);
6008
6009 const Function *F = II->getFunction();
6011 F ? F->getDenormalMode(
6012 II->getType()->getScalarType()->getFltSemantics())
6014 const bool IsMultiUnitFPType =
6015 V->getType()->getScalarType()->isMultiUnitFPType();
6016
6017 switch (IID) {
6018 case Intrinsic::trunc:
6019 Known = KnownFPClass::trunc(KnownSrc, Mode);
6020 break;
6021 case Intrinsic::floor:
6022 Known = KnownFPClass::floor(KnownSrc, IsMultiUnitFPType, Mode);
6023 break;
6024 case Intrinsic::ceil:
6025 Known = KnownFPClass::ceil(KnownSrc, IsMultiUnitFPType, Mode);
6026 break;
6027 default:
6028 Known =
6029 KnownFPClass::roundToIntegral(KnownSrc, IsMultiUnitFPType, Mode);
6030 break;
6031 }
6032 break;
6033 }
6034 case Intrinsic::exp:
6035 case Intrinsic::exp2:
6036 case Intrinsic::exp10:
6037 case Intrinsic::amdgcn_exp2: {
6038 KnownFPClass KnownSrc;
6039 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
6040 KnownSrc, Q, Depth + 1);
6041
6042 Known = KnownFPClass::exp(KnownSrc);
6043
6044 Type *EltTy = II->getType()->getScalarType();
6045 if (IID == Intrinsic::amdgcn_exp2 && EltTy->isFloatTy())
6046 Known.knownNot(fcSubnormal);
6047
6048 break;
6049 }
6050 case Intrinsic::fptrunc_round: {
6051 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,
6052 Q, Depth);
6053 break;
6054 }
6055 case Intrinsic::log:
6056 case Intrinsic::log10:
6057 case Intrinsic::log2:
6058 case Intrinsic::experimental_constrained_log:
6059 case Intrinsic::experimental_constrained_log10:
6060 case Intrinsic::experimental_constrained_log2:
6061 case Intrinsic::amdgcn_log: {
6062 FPClassTest InterestedSrcs = fcNone;
6063
6064 // log(negative) produces NaN.
6065 if ((InterestedClasses & fcNan) != fcNone)
6066 InterestedSrcs |= fcNan | fcNegative;
6067
6068 // log(logical-zero) produces negative infinity.
6069 if ((InterestedClasses & fcNegInf) != fcNone)
6070 InterestedSrcs |= fcZero | fcSubnormal;
6071
6072 // log(x) < -0.0 if x < +1.0
6073 if ((InterestedClasses & fcNegNormal) != fcNone)
6074 InterestedSrcs |= fcPosSubnormal | fcPosNormal;
6075
6076 // log(x) >= +0.0 if x >= +1.0
6077 if ((InterestedClasses & (fcPosZero | fcPosNormal)) != fcNone)
6078 InterestedSrcs |= fcPosNormal;
6079
6080 // log(x) is positive infinity iff x is positive infinity.
6081 if ((InterestedClasses & fcPosInf) != fcNone)
6082 InterestedSrcs |= fcPosInf;
6083
6084 KnownFPClass KnownSrc;
6085 if (InterestedSrcs != fcNone)
6086 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
6087 KnownSrc, Q, Depth + 1);
6088 const Function *F = II->getFunction();
6090 F ? F->getDenormalMode(
6091 II->getType()->getScalarType()->getFltSemantics())
6093 const bool IsMultiUnitFPType =
6094 V->getType()->getScalarType()->isMultiUnitFPType();
6095 Known = KnownFPClass::log(KnownSrc, Mode, IsMultiUnitFPType);
6096 break;
6097 }
6098 case Intrinsic::pow: {
6099 const bool WantNaN = (InterestedClasses & fcNan) != fcNone;
6100 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
6101 if (!WantNaN && !WantNegative)
6102 break;
6103
6104 FPClassTest InterestedLHS = fcNone;
6105 FPClassTest InterestedRHS = fcNone;
6106 if (WantNaN) {
6107 // pow may return NaN if one of the arguments is NaN. NaN may also be
6108 // produced from a negative, non-zero finite base and a non-integer
6109 // exponent.
6110 InterestedLHS |= fcNan | fcNegNormal | fcNegSubnormal;
6111 InterestedRHS |= fcNan;
6112 }
6113 if (WantNegative) {
6114 // A negative value is returned when a negative base is raised to an odd
6115 // integer power. Only normal values can be odd integers.
6116 InterestedLHS |= fcNegative;
6117 InterestedRHS |= fcNormal;
6118 }
6119
6120 KnownFPClass KnownLHS;
6121 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedLHS,
6122 KnownLHS, Q, Depth + 1);
6123
6124 // If the LHS is unknown, then querying the RHS is only useful for rare
6125 // edge cases.
6126 if (KnownLHS.isUnknown())
6127 break;
6128
6129 KnownFPClass KnownRHS;
6130 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedRHS,
6131 KnownRHS, Q, Depth + 1);
6132 Known = KnownFPClass::pow(KnownLHS, KnownRHS);
6133 break;
6134 }
6135 case Intrinsic::powi: {
6136 if ((InterestedClasses & (fcNan | fcInf | fcNegative)) == fcNone)
6137 break;
6138
6139 // The exponent is always a scalar, even when raising a vector to a power.
6140 const Value *Exp = II->getArgOperand(1);
6141 unsigned BitWidth = Exp->getType()->getIntegerBitWidth();
6142 KnownBits ExponentKnownBits(BitWidth);
6143 computeKnownBits(Exp, APInt(1, 1), ExponentKnownBits, Q, Depth + 1);
6144
6145 FPClassTest InterestedSrcs = fcNone;
6146 if (InterestedClasses & fcNan)
6147 InterestedSrcs |= fcNan;
6148 if (!ExponentKnownBits.isZero()) {
6149 if (InterestedClasses & fcInf)
6150 InterestedSrcs |= fcFinite | fcInf;
6151 if ((InterestedClasses & fcNegative) && !ExponentKnownBits.isEven())
6152 InterestedSrcs |= fcNegative;
6153 }
6154
6155 KnownFPClass KnownSrc;
6156 if (InterestedSrcs != fcNone)
6157 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
6158 KnownSrc, Q, Depth + 1);
6159
6160 Known = KnownFPClass::powi(KnownSrc, ExponentKnownBits);
6161 break;
6162 }
6163 case Intrinsic::ldexp: {
6164 KnownFPClass KnownSrc;
6165 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
6166 KnownSrc, Q, Depth + 1);
6167 // Can refine inf/zero handling based on the exponent operand.
6168 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;
6169
6170 const Value *ExpArg = II->getArgOperand(1);
6171 ConstantRange ExpKnownRange =
6172 ((KnownSrc.getKnownFPClasses() & ExpInfoMask) != fcNone)
6173 ? computeConstantRange(ExpArg, /*ForSigned=*/true, Q, Depth + 1)
6174 : ConstantRange::getFull(
6175 ExpArg->getType()->getScalarSizeInBits());
6176
6177 const fltSemantics &Flt =
6178 II->getType()->getScalarType()->getFltSemantics();
6179
6180 const Function *F = II->getFunction();
6182 F ? F->getDenormalMode(Flt) : DenormalMode::getDynamic();
6183
6184 Known = KnownFPClass::ldexp(KnownSrc, ExpKnownRange.getSignedMin(),
6185 ExpKnownRange.getSignedMax(), Flt, Mode);
6186 break;
6187 }
6188 case Intrinsic::arithmetic_fence: {
6189 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
6190 Known, Q, Depth + 1);
6191 break;
6192 }
6193 case Intrinsic::experimental_constrained_sitofp:
6194 case Intrinsic::experimental_constrained_uitofp:
6195 // Cannot produce nan
6196 Known.knownNot(fcNan);
6197
6198 // sitofp and uitofp turn into +0.0 for zero.
6199 Known.knownNot(fcNegZero);
6200
6201 // Integers cannot be subnormal
6202 Known.knownNot(fcSubnormal);
6203
6204 if (IID == Intrinsic::experimental_constrained_uitofp)
6205 Known.signBitMustBeZero();
6206
6207 // TODO: Copy inf handling from instructions
6208 break;
6209
6210 case Intrinsic::amdgcn_fract: {
6211 Known.knownNot(fcInf);
6212
6213 if (InterestedClasses & fcNan) {
6214 KnownFPClass KnownSrc;
6215 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
6216 InterestedClasses, KnownSrc, Q, Depth + 1);
6217
6218 if (KnownSrc.isKnownNeverInfOrNaN())
6219 Known.knownNot(fcNan);
6220 else if (KnownSrc.isKnownNever(fcSNan))
6221 Known.knownNot(fcSNan);
6222 }
6223
6224 break;
6225 }
6226 case Intrinsic::amdgcn_rcp: {
6227 KnownFPClass KnownSrc;
6228 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
6229 KnownSrc, Q, Depth + 1);
6230
6231 Known.propagateNonNaN(KnownSrc);
6232
6233 Type *EltTy = II->getType()->getScalarType();
6234
6235 // f32 denormal always flushed.
6236 if (EltTy->isFloatTy()) {
6237 Known.knownNot(fcSubnormal);
6238 KnownSrc.knownNot(fcSubnormal);
6239 }
6240
6241 if (KnownSrc.isKnownNever(fcNegative))
6242 Known.knownNot(fcNegative);
6243 if (KnownSrc.isKnownNever(fcPositive))
6244 Known.knownNot(fcPositive);
6245
6246 if (const Function *F = II->getFunction()) {
6247 DenormalMode Mode = F->getDenormalMode(EltTy->getFltSemantics());
6248 if (KnownSrc.isKnownNeverLogicalPosZero(Mode))
6249 Known.knownNot(fcPosInf);
6250 if (KnownSrc.isKnownNeverLogicalNegZero(Mode))
6251 Known.knownNot(fcNegInf);
6252 }
6253
6254 break;
6255 }
6256 case Intrinsic::amdgcn_rsq: {
6257 KnownFPClass KnownSrc;
6258 // The only negative value that can be returned is -inf for -0 inputs.
6260
6261 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
6262 KnownSrc, Q, Depth + 1);
6263
6264 // Negative -> nan
6265 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
6266 Known.knownNot(fcNan);
6267 else if (KnownSrc.isKnownNever(fcSNan))
6268 Known.knownNot(fcSNan);
6269
6270 // +inf -> +0
6271 if (KnownSrc.isKnownNeverPosInfinity())
6272 Known.knownNot(fcPosZero);
6273
6274 Type *EltTy = II->getType()->getScalarType();
6275
6276 // f32 denormal always flushed.
6277 if (EltTy->isFloatTy())
6278 Known.knownNot(fcPosSubnormal);
6279
6280 if (const Function *F = II->getFunction()) {
6281 DenormalMode Mode = F->getDenormalMode(EltTy->getFltSemantics());
6282
6283 // -0 -> -inf
6284 if (KnownSrc.isKnownNeverLogicalNegZero(Mode))
6285 Known.knownNot(fcNegInf);
6286
6287 // +0 -> +inf
6288 if (KnownSrc.isKnownNeverLogicalPosZero(Mode))
6289 Known.knownNot(fcPosInf);
6290 }
6291
6292 break;
6293 }
6294 case Intrinsic::amdgcn_trig_preop: {
6295 // Always returns a value [0, 1)
6296 Known.knownNot(fcNan | fcInf | fcNegative);
6297 break;
6298 }
6299 case Intrinsic::convert_from_arbitrary_fp: {
6300 auto *MD = cast<MetadataAsValue>(II->getArgOperand(1))->getMetadata();
6301 StringRef FormatStr = cast<MDString>(MD)->getString();
6302
6303 const fltSemantics *SrcSemantics =
6305 if (!SrcSemantics)
6306 break;
6307
6308 const fltSemantics DstSemantics =
6309 II->getType()->getScalarType()->getFltSemantics();
6310
6311 if (!APFloat::semanticsHasNaN(*SrcSemantics))
6312 Known.knownNot(fcNan);
6313
6314 // fcInf can only be cleared if the source format has no Inf encoding
6315 // and the dst max exp can accommodate src max exp.
6316 if (!APFloat::semanticsHasInf(*SrcSemantics) &&
6317 APFloat::semanticsMaxExponent(*SrcSemantics) <=
6318 APFloat::semanticsMaxExponent(DstSemantics))
6319 Known.knownNot(fcInf);
6320
6321 // Check and clear all neg flags for formats that do not have signed
6322 // representation.
6323 if (!APFloat::semanticsHasSignedRepr(*SrcSemantics))
6324 Known.knownNot(fcNegative);
6325
6326 // Check if format has no zero at all (Float8E8M0FNU), or no negative
6327 // zero.
6328 if (!APFloat::semanticsHasZero(*SrcSemantics))
6329 Known.knownNot(fcZero);
6330 else if (SrcSemantics->nanEncoding == fltNanEncoding::NegativeZero)
6331 Known.knownNot(fcNegZero);
6332
6333 // If src lands normally in dest, the result can never be subnormal.
6334 if (APFloat::isRepresentableAsNormalIn(*SrcSemantics, DstSemantics))
6335 Known.knownNot(fcSubnormal);
6336 break;
6337 }
6338 default:
6339 break;
6340 }
6341
6342 break;
6343 }
6344 case Instruction::FAdd:
6345 case Instruction::FSub: {
6346 computeKnownFPClassForFAddSub(Op, DemandedElts, InterestedClasses, Known, Q,
6347 Depth, /*IsAdd=*/Opc == Instruction::FAdd,
6348 /*IsStrict=*/false);
6349 break;
6350 }
6351 case Instruction::FMul: {
6352 const Function *F = cast<Instruction>(Op)->getFunction();
6354 F ? F->getDenormalMode(
6355 Op->getType()->getScalarType()->getFltSemantics())
6357
6358 Value *LHS = Op->getOperand(0);
6359 Value *RHS = Op->getOperand(1);
6360 // X * X is always non-negative or a NaN.
6361 // FIXME: Should check isGuaranteedNotToBeUndef
6362 if (LHS == RHS) {
6363 KnownFPClass KnownSrc;
6364 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownSrc, Q,
6365 Depth + 1);
6366 Known = KnownFPClass::square(KnownSrc, Mode);
6367 break;
6368 }
6369
6370 KnownFPClass KnownLHS, KnownRHS;
6371
6372 const APFloat *CRHS;
6373 if (match(RHS, m_APFloat(CRHS))) {
6374 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Q,
6375 Depth + 1);
6376 Known = KnownFPClass::fmul(KnownLHS, *CRHS, Mode);
6377 } else {
6378 computeKnownFPClass(RHS, DemandedElts, fcAllFlags, KnownRHS, Q,
6379 Depth + 1);
6380 // TODO: Improve accuracy in unfused FMA pattern. We can prove an
6381 // additional not-nan if the addend is known-not negative infinity if the
6382 // multiply is known-not infinity.
6383
6384 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Q,
6385 Depth + 1);
6386 Known = KnownFPClass::fmul(KnownLHS, KnownRHS, Mode);
6387 }
6388
6389 /// Propgate no-infs if the other source is known smaller than one, such
6390 /// that this cannot introduce overflow.
6391 if (KnownLHS.isKnownNever(fcInf) && isAbsoluteValueULEOne(RHS))
6392 Known.knownNot(fcInf);
6393 else if (KnownRHS.isKnownNever(fcInf) && isAbsoluteValueULEOne(LHS))
6394 Known.knownNot(fcInf);
6395
6396 break;
6397 }
6398 case Instruction::FDiv: {
6399 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
6400
6401 const Function *F = cast<Instruction>(Op)->getFunction();
6402 const fltSemantics &FltSem =
6403 Op->getType()->getScalarType()->getFltSemantics();
6405 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
6406
6407 if (Op->getOperand(0) == Op->getOperand(1) &&
6408 isGuaranteedNotToBeUndef(Op->getOperand(0), Q.AC, Q.CtxI, Q.DT)) {
6409 // X / X is always exactly 1.0 or a NaN.
6410 Known.setKnownFPClasses(fcNan | fcPosNormal);
6411
6412 if (!WantNan)
6413 break;
6414
6415 KnownFPClass KnownSrc;
6416 computeKnownFPClass(Op->getOperand(0), DemandedElts,
6417 fcNan | fcInf | fcZero | fcSubnormal, KnownSrc, Q,
6418 Depth + 1);
6419
6420 Known = KnownFPClass::fdiv_self(KnownSrc, Mode);
6421 break;
6422 }
6423
6424 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
6425 const bool WantPositive = (InterestedClasses & fcPositive) != fcNone;
6426 if (!WantNan && !WantNegative && !WantPositive)
6427 break;
6428
6429 KnownFPClass KnownLHS, KnownRHS;
6430 computeKnownFPClass(Op->getOperand(1), DemandedElts, fcAllFlags, KnownRHS,
6431 Q, Depth + 1);
6432
6433 bool KnowSomethingUseful =
6434 KnownRHS.isKnownNever(fcSNan) ||
6437
6438 if (KnowSomethingUseful)
6439 computeKnownFPClass(Op->getOperand(0), DemandedElts, fcAllFlags, KnownLHS,
6440 Q, Depth + 1);
6441
6442 Known = KnownFPClass::fdiv(KnownLHS, KnownRHS, Mode);
6443 break;
6444 }
6445 case Instruction::FRem: {
6446 computeKnownFPClassForFRem(Op, DemandedElts, InterestedClasses, Known, Q,
6447 Depth);
6448 break;
6449 }
6450 case Instruction::FPExt: {
6451 KnownFPClass KnownSrc;
6452 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
6453 KnownSrc, Q, Depth + 1);
6454
6455 const fltSemantics &DstTy =
6456 Op->getType()->getScalarType()->getFltSemantics();
6457 const fltSemantics &SrcTy =
6458 Op->getOperand(0)->getType()->getScalarType()->getFltSemantics();
6459
6460 const auto *I = dyn_cast<Instruction>(Op);
6461 const Function *F = I ? I->getFunction() : nullptr;
6462 DenormalMode DstMode =
6463 F ? F->getDenormalMode(DstTy) : DenormalMode::getDynamic();
6464 DenormalMode SrcMode =
6465 F ? F->getDenormalMode(SrcTy) : DenormalMode::getDynamic();
6466
6467 Known = KnownFPClass::fpext(KnownSrc, DstTy, SrcTy, DstMode, SrcMode);
6468 break;
6469 }
6470 case Instruction::FPTrunc: {
6471 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known, Q,
6472 Depth);
6473 break;
6474 }
6475 case Instruction::SIToFP:
6476 case Instruction::UIToFP: {
6477 // Cannot produce nan
6478 Known.knownNot(fcNan);
6479
6480 // Integers cannot be subnormal
6481 Known.knownNot(fcSubnormal);
6482
6483 // sitofp and uitofp turn into +0.0 for zero.
6484 Known.knownNot(fcNegZero);
6485
6486 // UIToFP is always non-negative regardless of known bits.
6487 if (Op->getOpcode() == Instruction::UIToFP)
6488 Known.signBitMustBeZero();
6489
6490 // Only compute known bits if we can learn something useful from them.
6491 if (!(InterestedClasses & (fcPosZero | fcNormal | fcInf)))
6492 break;
6493
6494 KnownBits IntKnown =
6495 computeKnownBits(Op->getOperand(0), DemandedElts, Q, Depth + 1);
6496
6497 // If the integer is non-zero, the result cannot be +0.0
6498 if (IntKnown.isNonZero())
6499 Known.knownNot(fcPosZero);
6500
6501 if (Op->getOpcode() == Instruction::SIToFP) {
6502 // If the signed integer is known non-negative, the result is
6503 // non-negative. If the signed integer is known negative, the result is
6504 // negative.
6505 if (IntKnown.isNonNegative()) {
6506 Known.signBitMustBeZero();
6507 } else if (IntKnown.isNegative()) {
6508 Known.signBitMustBeOne();
6509 }
6510 }
6511
6512 // Guard kept for ilogb()
6513 if (InterestedClasses & fcInf) {
6514 // Get width of largest magnitude integer known.
6515 // This still works for a signed minimum value because the largest FP
6516 // value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx).
6517 int IntSize = IntKnown.getBitWidth();
6518 if (Op->getOpcode() == Instruction::UIToFP)
6519 IntSize -= IntKnown.countMinLeadingZeros();
6520 else if (Op->getOpcode() == Instruction::SIToFP)
6521 IntSize -= IntKnown.countMinSignBits();
6522
6523 // If the exponent of the largest finite FP value can hold the largest
6524 // integer, the result of the cast must be finite.
6525 Type *FPTy = Op->getType()->getScalarType();
6526 if (ilogb(APFloat::getLargest(FPTy->getFltSemantics())) >= IntSize)
6527 Known.knownNot(fcInf);
6528 }
6529
6530 break;
6531 }
6532 case Instruction::ExtractElement: {
6533 // Look through extract element. If the index is non-constant or
6534 // out-of-range demand all elements, otherwise just the extracted element.
6535 const Value *Vec = Op->getOperand(0);
6536
6537 APInt DemandedVecElts;
6538 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
6539 unsigned NumElts = VecTy->getNumElements();
6540 DemandedVecElts = APInt::getAllOnes(NumElts);
6541 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(1));
6542 if (CIdx && CIdx->getValue().ult(NumElts))
6543 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
6544 } else {
6545 DemandedVecElts = APInt(1, 1);
6546 }
6547
6548 return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,
6549 Q, Depth + 1);
6550 }
6551 case Instruction::InsertElement: {
6552 if (isa<ScalableVectorType>(Op->getType()))
6553 return;
6554
6555 const Value *Vec = Op->getOperand(0);
6556 const Value *Elt = Op->getOperand(1);
6557 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(2));
6558 unsigned NumElts = DemandedElts.getBitWidth();
6559 APInt DemandedVecElts = DemandedElts;
6560 bool NeedsElt = true;
6561 // If we know the index we are inserting to, clear it from Vec check.
6562 if (CIdx && CIdx->getValue().ult(NumElts)) {
6563 DemandedVecElts.clearBit(CIdx->getZExtValue());
6564 NeedsElt = DemandedElts[CIdx->getZExtValue()];
6565 }
6566
6567 // Do we demand the inserted element?
6568 if (NeedsElt) {
6569 computeKnownFPClass(Elt, Known, InterestedClasses, Q, Depth + 1);
6570 // If we don't know any bits, early out.
6571 if (Known.isUnknown())
6572 break;
6573 } else {
6575 }
6576
6577 // Do we need anymore elements from Vec?
6578 if (!DemandedVecElts.isZero()) {
6579 KnownFPClass Known2;
6580 computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2, Q,
6581 Depth + 1);
6582 Known |= Known2;
6583 }
6584
6585 break;
6586 }
6587 case Instruction::ShuffleVector: {
6588 // Handle vector splat idiom
6589 if (Value *Splat = getSplatValue(V)) {
6590 computeKnownFPClass(Splat, Known, InterestedClasses, Q, Depth + 1);
6591 break;
6592 }
6593
6594 // For undef elements, we don't know anything about the common state of
6595 // the shuffle result.
6596 APInt DemandedLHS, DemandedRHS;
6597 auto *Shuf = dyn_cast<ShuffleVectorInst>(Op);
6598 if (!Shuf || !getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
6599 return;
6600
6601 if (!!DemandedLHS) {
6602 const Value *LHS = Shuf->getOperand(0);
6603 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known, Q,
6604 Depth + 1);
6605
6606 // If we don't know any bits, early out.
6607 if (Known.isUnknown())
6608 break;
6609 } else {
6611 }
6612
6613 if (!!DemandedRHS) {
6614 KnownFPClass Known2;
6615 const Value *RHS = Shuf->getOperand(1);
6616 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2, Q,
6617 Depth + 1);
6618 Known |= Known2;
6619 }
6620
6621 break;
6622 }
6623 case Instruction::ExtractValue: {
6624 const ExtractValueInst *Extract = cast<ExtractValueInst>(Op);
6625 ArrayRef<unsigned> Indices = Extract->getIndices();
6626 const Value *Src = Extract->getAggregateOperand();
6627 if (isa<StructType>(Src->getType()) && Indices.size() == 1 &&
6628 Indices[0] == 0) {
6629 if (const auto *II = dyn_cast<IntrinsicInst>(Src)) {
6630 switch (II->getIntrinsicID()) {
6631 case Intrinsic::frexp: {
6632 FPClassTest InterestedSrcs = InterestedClasses;
6633
6634 // Positive subnormals and negative subnormals could become positive
6635 // zero.
6636 if (InterestedClasses & fcPosZero)
6637 InterestedSrcs |= fcSubnormal;
6638
6639 // Negative subnormals could become negative zero.
6640 if (InterestedClasses & fcNegZero)
6641 InterestedSrcs |= fcNegSubnormal;
6642
6643 if (InterestedClasses & fcPosNormal)
6644 InterestedSrcs |= fcPosSubnormal;
6645
6646 if (InterestedClasses & fcNegNormal)
6647 InterestedSrcs |= fcNegSubnormal;
6648
6649 KnownFPClass KnownSrc;
6650 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
6651 InterestedSrcs, KnownSrc, Q, Depth + 1);
6652
6653 const Function *F = cast<Instruction>(Op)->getFunction();
6654 const fltSemantics &FltSem =
6655 Op->getType()->getScalarType()->getFltSemantics();
6656
6658 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
6659 Known = KnownFPClass::frexp_mant(KnownSrc, Mode);
6660 return;
6661 }
6662 default:
6663 break;
6664 }
6665 }
6666 }
6667
6668 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Q,
6669 Depth + 1);
6670 break;
6671 }
6672 case Instruction::PHI: {
6673 const PHINode *P = cast<PHINode>(Op);
6674 // Unreachable blocks may have zero-operand PHI nodes.
6675 if (P->getNumIncomingValues() == 0)
6676 break;
6677
6678 // Otherwise take the unions of the known bit sets of the operands,
6679 // taking conservative care to avoid excessive recursion.
6680 const unsigned PhiRecursionLimit = MaxAnalysisRecursionDepth - 2;
6681
6682 if (Depth < PhiRecursionLimit) {
6683 // Skip if every incoming value references to ourself.
6684 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
6685 break;
6686
6687 bool First = true;
6688
6689 for (const Use &U : P->operands()) {
6690 Value *IncValue;
6691 Instruction *CtxI;
6692 breakSelfRecursivePHI(&U, P, IncValue, CtxI);
6693 // Skip direct self references.
6694 if (IncValue == P)
6695 continue;
6696
6697 KnownFPClass KnownSrc;
6698 // Recurse, but cap the recursion to two levels, because we don't want
6699 // to waste time spinning around in loops. We need at least depth 2 to
6700 // detect known sign bits.
6701 computeKnownFPClass(IncValue, DemandedElts, InterestedClasses, KnownSrc,
6703 PhiRecursionLimit);
6704
6705 if (First) {
6706 Known = KnownSrc;
6707 First = false;
6708 } else {
6709 Known |= KnownSrc;
6710 }
6711
6712 if (Known.getKnownFPClasses() == fcAllFlags)
6713 break;
6714 }
6715 }
6716
6717 // Look for the case of a for loop which has a positive
6718 // initial value and is incremented by a squared value.
6719 // This will propagate sign information out of such loops.
6720 if (P->getNumIncomingValues() != 2 || Known.cannotBeOrderedLessThanZero())
6721 break;
6722 for (unsigned I = 0; I < 2; I++) {
6723 Value *RecurValue = P->getIncomingValue(1 - I);
6725 if (!II)
6726 continue;
6727 Value *R, *L, *Init;
6728 PHINode *PN;
6730 PN == P) {
6731 switch (II->getIntrinsicID()) {
6732 case Intrinsic::fma:
6733 case Intrinsic::fmuladd: {
6734 KnownFPClass KnownStart;
6735 computeKnownFPClass(Init, DemandedElts, InterestedClasses, KnownStart,
6736 Q, Depth + 1);
6737 if (KnownStart.cannotBeOrderedLessThanZero() && L == R &&
6738 isGuaranteedNotToBeUndef(L, Q.AC, Q.CtxI, Q.DT, Depth + 1))
6740 break;
6741 }
6742 }
6743 }
6744 }
6745 break;
6746 }
6747 case Instruction::BitCast: {
6748 const Value *Src;
6749 if (!match(Op, m_ElementWiseBitCast(m_Value(Src))) ||
6750 !Src->getType()->isIntOrIntVectorTy())
6751 break;
6752
6753 const Type *Ty = Op->getType();
6754
6755 Value *CastLHS, *CastRHS;
6756
6757 // Match bitcast(umax(bitcast(a), bitcast(b)))
6758 if (match(Src, m_c_MaxOrMin(m_BitCast(m_Value(CastLHS)),
6759 m_BitCast(m_Value(CastRHS)))) &&
6760 CastLHS->getType() == Ty && CastRHS->getType() == Ty) {
6761 KnownFPClass KnownLHS, KnownRHS;
6762 computeKnownFPClass(CastRHS, DemandedElts, InterestedClasses, KnownRHS, Q,
6763 Depth + 1);
6764 if (!KnownRHS.isUnknown()) {
6765 computeKnownFPClass(CastLHS, DemandedElts, InterestedClasses, KnownLHS,
6766 Q, Depth + 1);
6767 Known = KnownLHS | KnownRHS;
6768 }
6769
6770 return;
6771 }
6772
6773 const Type *EltTy = Ty->getScalarType();
6774 KnownBits Bits(EltTy->getPrimitiveSizeInBits());
6775 computeKnownBits(Src, DemandedElts, Bits, Q, Depth + 1);
6776
6778 break;
6779 }
6780 default:
6781 break;
6782 }
6783}
6784
6786 const APInt &DemandedElts,
6787 FPClassTest InterestedClasses,
6788 const SimplifyQuery &SQ,
6789 unsigned Depth) {
6790 KnownFPClass KnownClasses;
6791 ::computeKnownFPClass(V, DemandedElts, InterestedClasses, KnownClasses, SQ,
6792 Depth);
6793 return KnownClasses;
6794}
6795
6797 FPClassTest InterestedClasses,
6798 const SimplifyQuery &SQ,
6799 unsigned Depth) {
6801 ::computeKnownFPClass(V, Known, InterestedClasses, SQ, Depth);
6802 return Known;
6803}
6804
6806 const Value *V, const DataLayout &DL, FPClassTest InterestedClasses,
6807 const TargetLibraryInfo *TLI, AssumptionCache *AC, const Instruction *CtxI,
6808 const DominatorTree *DT, bool UseInstrInfo, unsigned Depth) {
6809 return computeKnownFPClass(V, InterestedClasses,
6810 SimplifyQuery(DL, TLI, DT, AC, CtxI, UseInstrInfo),
6811 Depth);
6812}
6813
6815llvm::computeKnownFPClass(const Value *V, const APInt &DemandedElts,
6816 FastMathFlags FMF, FPClassTest InterestedClasses,
6817 const SimplifyQuery &SQ, unsigned Depth) {
6818 if (FMF.noNaNs())
6819 InterestedClasses &= ~fcNan;
6820 if (FMF.noInfs())
6821 InterestedClasses &= ~fcInf;
6822
6823 KnownFPClass Result =
6824 computeKnownFPClass(V, DemandedElts, InterestedClasses, SQ, Depth);
6825
6826 if (FMF.noNaNs())
6827 Result.setKnownFPClasses(Result.getKnownFPClasses() & ~fcNan);
6828 if (FMF.noInfs())
6829 Result.setKnownFPClasses(Result.getKnownFPClasses() & ~fcInf);
6830 return Result;
6831}
6832
6834 FPClassTest InterestedClasses,
6835 const SimplifyQuery &SQ,
6836 unsigned Depth) {
6837 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
6838 APInt DemandedElts =
6839 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
6840 return computeKnownFPClass(V, DemandedElts, FMF, InterestedClasses, SQ,
6841 Depth);
6842}
6843
6845 unsigned Depth) {
6847 return Known.isKnownNever(fcNegZero);
6848}
6849
6851 unsigned Depth) {
6854 return Known.cannotBeOrderedLessThanZero();
6855}
6856
6858 unsigned Depth) {
6860 return Known.isKnownNeverInfinity();
6861}
6862
6863/// Return true if the floating-point value can never contain a NaN or infinity.
6865 unsigned Depth) {
6867 return Known.isKnownNeverNaN() && Known.isKnownNeverInfinity();
6868}
6869
6870/// Return true if the floating-point scalar value is not a NaN or if the
6871/// floating-point vector value has no NaN elements. Return false if a value
6872/// could ever be NaN.
6874 unsigned Depth) {
6876 return Known.isKnownNeverNaN();
6877}
6878
6879/// Return false if we can prove that the specified FP value's sign bit is 0.
6880/// Return true if we can prove that the specified FP value's sign bit is 1.
6881/// Otherwise return std::nullopt.
6882std::optional<bool> llvm::computeKnownFPSignBit(const Value *V,
6883 const SimplifyQuery &SQ,
6884 unsigned Depth) {
6886 return Known.getSignBit();
6887}
6888
6890 auto *User = cast<Instruction>(U.getUser());
6891 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6892 if (FPOp->hasNoSignedZeros())
6893 return true;
6894 }
6895
6896 switch (User->getOpcode()) {
6897 case Instruction::FPToSI:
6898 case Instruction::FPToUI:
6899 return true;
6900 case Instruction::FRem:
6901 // frem(x, +-0.0) == NaN, and more generally frem(x, y) == frem(x, -y).
6902 return U.getOperandNo() == 1;
6903 case Instruction::FCmp:
6904 // fcmp treats both positive and negative zero as equal.
6905 return true;
6906 case Instruction::Call:
6907 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6908 switch (II->getIntrinsicID()) {
6909 case Intrinsic::fabs:
6910 case Intrinsic::exp:
6911 case Intrinsic::exp2:
6912 case Intrinsic::exp10:
6913 case Intrinsic::log:
6914 case Intrinsic::log2:
6915 case Intrinsic::log10:
6916 case Intrinsic::cos:
6917 case Intrinsic::acos:
6918 case Intrinsic::cosh:
6919 case Intrinsic::fptosi_sat:
6920 case Intrinsic::fptoui_sat:
6921 case Intrinsic::lround:
6922 case Intrinsic::llround:
6923 case Intrinsic::lrint:
6924 case Intrinsic::llrint:
6925 return true;
6926 case Intrinsic::copysign:
6927 return U.getOperandNo() == 0;
6928 case Intrinsic::pow:
6929 // pow(x, +-0.0) == +1.0 or NaN
6930 return U.getOperandNo() == 1;
6931 case Intrinsic::is_fpclass: {
6932 auto Test =
6933 static_cast<FPClassTest>(
6934 cast<ConstantInt>(II->getArgOperand(1))->getZExtValue()) &
6937 }
6938 default:
6939 return false;
6940 }
6941 }
6942 return false;
6943 default:
6944 return false;
6945 }
6946}
6947
6949 auto *User = cast<Instruction>(U.getUser());
6950 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6951 if (FPOp->hasNoNaNs())
6952 return true;
6953 }
6954
6955 switch (User->getOpcode()) {
6956 case Instruction::FPToSI:
6957 case Instruction::FPToUI:
6958 return true;
6959 // Proper FP math operations ignore the sign bit of NaN.
6960 case Instruction::FAdd:
6961 case Instruction::FSub:
6962 case Instruction::FMul:
6963 case Instruction::FDiv:
6964 case Instruction::FRem:
6965 case Instruction::FPTrunc:
6966 case Instruction::FPExt:
6967 case Instruction::FCmp:
6968 return true;
6969 // Bitwise FP operations should preserve the sign bit of NaN.
6970 case Instruction::FNeg:
6971 case Instruction::Select:
6972 case Instruction::PHI:
6973 return false;
6974 case Instruction::Ret:
6975 return User->getFunction()->getAttributes().getRetNoFPClass() &
6977 case Instruction::Call:
6978 case Instruction::Invoke: {
6979 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6980 switch (II->getIntrinsicID()) {
6981 case Intrinsic::fabs:
6982 return true;
6983 case Intrinsic::copysign:
6984 return U.getOperandNo() == 0;
6985 // Other proper FP math intrinsics ignore the sign bit of NaN.
6986 case Intrinsic::maxnum:
6987 case Intrinsic::minnum:
6988 case Intrinsic::maximum:
6989 case Intrinsic::minimum:
6990 case Intrinsic::maximumnum:
6991 case Intrinsic::minimumnum:
6992 case Intrinsic::canonicalize:
6993 case Intrinsic::fma:
6994 case Intrinsic::fmuladd:
6995 case Intrinsic::sqrt:
6996 case Intrinsic::pow:
6997 case Intrinsic::powi:
6998 case Intrinsic::fptoui_sat:
6999 case Intrinsic::fptosi_sat:
7000 case Intrinsic::is_fpclass:
7001 return true;
7002 default:
7003 return false;
7004 }
7005 }
7006
7007 FPClassTest NoFPClass =
7008 cast<CallBase>(User)->getParamNoFPClass(U.getOperandNo());
7009 return NoFPClass & FPClassTest::fcNan;
7010 }
7011 default:
7012 return false;
7013 }
7014}
7015
7017 FastMathFlags FMF) {
7018 if (isa<PoisonValue>(V))
7019 return true;
7020 if (isa<UndefValue>(V))
7021 return false;
7022
7023 if (match(V, m_CheckedFp([](const APFloat &Val) { return Val.isInteger(); })))
7024 return true;
7025
7027 if (!I)
7028 return false;
7029
7030 switch (I->getOpcode()) {
7031 case Instruction::SIToFP:
7032 case Instruction::UIToFP:
7033 // TODO: Could check nofpclass(inf) on incoming argument
7034 if (FMF.noInfs())
7035 return true;
7036
7037 // Need to check int size cannot produce infinity, which computeKnownFPClass
7038 // knows how to do already.
7039 return isKnownNeverInfinity(I, SQ);
7040 case Instruction::Call: {
7041 const CallInst *CI = cast<CallInst>(I);
7042 switch (CI->getIntrinsicID()) {
7043 case Intrinsic::trunc:
7044 case Intrinsic::floor:
7045 case Intrinsic::ceil:
7046 case Intrinsic::rint:
7047 case Intrinsic::nearbyint:
7048 case Intrinsic::round:
7049 case Intrinsic::roundeven:
7050 return (FMF.noInfs() && FMF.noNaNs()) || isKnownNeverInfOrNaN(I, SQ);
7051 default:
7052 break;
7053 }
7054
7055 break;
7056 }
7057 default:
7058 break;
7059 }
7060
7061 return false;
7062}
7063
7065
7066 // All byte-wide stores are splatable, even of arbitrary variables.
7067 if (V->getType()->isIntegerTy(8))
7068 return V;
7069
7070 LLVMContext &Ctx = V->getContext();
7071
7072 // Undef don't care.
7073 auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
7074 if (isa<UndefValue>(V))
7075 return UndefInt8;
7076
7077 // Return poison for zero-sized type.
7078 if (DL.getTypeStoreSize(V->getType()).isZero())
7079 return PoisonValue::get(Type::getInt8Ty(Ctx));
7080
7082 if (!C) {
7083 // Conceptually, we could handle things like:
7084 // %a = zext i8 %X to i16
7085 // %b = shl i16 %a, 8
7086 // %c = or i16 %a, %b
7087 // but until there is an example that actually needs this, it doesn't seem
7088 // worth worrying about.
7089 return nullptr;
7090 }
7091
7092 // Handle 'null' ConstantArrayZero etc.
7093 if (C->isNullValue())
7095
7096 // Constant floating-point values can be handled as integer values if the
7097 // corresponding integer value is "byteable". An important case is 0.0.
7098 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
7099 Type *ScalarTy = CFP->getType()->getScalarType();
7100 if (ScalarTy->isHalfTy() || ScalarTy->isFloatTy() || ScalarTy->isDoubleTy())
7101 return isBytewiseValue(
7102 ConstantInt::get(Ctx, CFP->getValue().bitcastToAPInt()), DL);
7103
7104 // Don't handle long double formats, which have strange constraints.
7105 return nullptr;
7106 }
7107
7108 // We can handle constant integers that are multiple of 8 bits.
7109 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
7110 if (CI->getBitWidth() % 8 == 0) {
7111 if (!CI->getValue().isSplat(8))
7112 return nullptr;
7113 return ConstantInt::get(Ctx, CI->getValue().trunc(8));
7114 }
7115 }
7116
7117 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
7118 if (CE->getOpcode() == Instruction::IntToPtr) {
7119 if (auto *PtrTy = dyn_cast<PointerType>(CE->getType())) {
7120 unsigned BitWidth = DL.getPointerSizeInBits(PtrTy->getAddressSpace());
7122 CE->getOperand(0), Type::getIntNTy(Ctx, BitWidth), false, DL))
7123 return isBytewiseValue(Op, DL);
7124 }
7125 }
7126 }
7127
7128 auto Merge = [&](Value *LHS, Value *RHS) -> Value * {
7129 if (LHS == RHS)
7130 return LHS;
7131 if (!LHS || !RHS)
7132 return nullptr;
7133 if (LHS == UndefInt8)
7134 return RHS;
7135 if (RHS == UndefInt8)
7136 return LHS;
7137 return nullptr;
7138 };
7139
7141 Value *Val = UndefInt8;
7142 for (uint64_t I = 0, E = CA->getNumElements(); I != E; ++I)
7143 if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))
7144 return nullptr;
7145 return Val;
7146 }
7147
7149 Value *Val = UndefInt8;
7150 for (Value *Op : C->operands())
7151 if (!(Val = Merge(Val, isBytewiseValue(Op, DL))))
7152 return nullptr;
7153 return Val;
7154 }
7155
7156 // Don't try to handle the handful of other constants.
7157 return nullptr;
7158}
7159
7160// This is the recursive version of BuildSubAggregate. It takes a few different
7161// arguments. Idxs is the index within the nested struct From that we are
7162// looking at now (which is of type IndexedType). IdxSkip is the number of
7163// indices from Idxs that should be left out when inserting into the resulting
7164// struct. To is the result struct built so far, new insertvalue instructions
7165// build on that.
7166static Value *BuildSubAggregate(Value *From, Value *To, Type *IndexedType,
7168 unsigned IdxSkip,
7169 BasicBlock::iterator InsertBefore) {
7170 StructType *STy = dyn_cast<StructType>(IndexedType);
7171 if (STy) {
7172 // Save the original To argument so we can modify it
7173 Value *OrigTo = To;
7174 // General case, the type indexed by Idxs is a struct
7175 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
7176 // Process each struct element recursively
7177 Idxs.push_back(i);
7178 Value *PrevTo = To;
7179 To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
7180 InsertBefore);
7181 Idxs.pop_back();
7182 if (!To) {
7183 // Couldn't find any inserted value for this index? Cleanup
7184 while (PrevTo != OrigTo) {
7186 PrevTo = Del->getAggregateOperand();
7187 Del->eraseFromParent();
7188 }
7189 // Stop processing elements
7190 break;
7191 }
7192 }
7193 // If we successfully found a value for each of our subaggregates
7194 if (To)
7195 return To;
7196 }
7197 // Base case, the type indexed by SourceIdxs is not a struct, or not all of
7198 // the struct's elements had a value that was inserted directly. In the latter
7199 // case, perhaps we can't determine each of the subelements individually, but
7200 // we might be able to find the complete struct somewhere.
7201
7202 // Find the value that is at that particular spot
7203 Value *V = FindInsertedValue(From, Idxs);
7204
7205 if (!V)
7206 return nullptr;
7207
7208 // Insert the value in the new (sub) aggregate
7209 return InsertValueInst::Create(To, V, ArrayRef(Idxs).slice(IdxSkip), "tmp",
7210 InsertBefore);
7211}
7212
7213// This helper takes a nested struct and extracts a part of it (which is again a
7214// struct) into a new value. For example, given the struct:
7215// { a, { b, { c, d }, e } }
7216// and the indices "1, 1" this returns
7217// { c, d }.
7218//
7219// It does this by inserting an insertvalue for each element in the resulting
7220// struct, as opposed to just inserting a single struct. This will only work if
7221// each of the elements of the substruct are known (ie, inserted into From by an
7222// insertvalue instruction somewhere).
7223//
7224// All inserted insertvalue instructions are inserted before InsertBefore
7226 BasicBlock::iterator InsertBefore) {
7227 Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
7228 idx_range);
7229 Value *To = PoisonValue::get(IndexedType);
7230 SmallVector<unsigned, 10> Idxs(idx_range);
7231 unsigned IdxSkip = Idxs.size();
7232
7233 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
7234}
7235
7236/// Given an aggregate and a sequence of indices, see if the scalar value
7237/// indexed is already around as a register, for example if it was inserted
7238/// directly into the aggregate.
7239///
7240/// If InsertBefore is not null, this function will duplicate (modified)
7241/// insertvalues when a part of a nested struct is extracted.
7242Value *
7244 std::optional<BasicBlock::iterator> InsertBefore) {
7245 // Nothing to index? Just return V then (this is useful at the end of our
7246 // recursion).
7247 if (idx_range.empty())
7248 return V;
7249 // We have indices, so V should have an indexable type.
7250 assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
7251 "Not looking at a struct or array?");
7252 assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
7253 "Invalid indices for type?");
7254
7255 if (Constant *C = dyn_cast<Constant>(V)) {
7256 C = C->getAggregateElement(idx_range[0]);
7257 if (!C) return nullptr;
7258 return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
7259 }
7260
7262 // Loop the indices for the insertvalue instruction in parallel with the
7263 // requested indices
7264 const unsigned *req_idx = idx_range.begin();
7265 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
7266 i != e; ++i, ++req_idx) {
7267 if (req_idx == idx_range.end()) {
7268 // We can't handle this without inserting insertvalues
7269 if (!InsertBefore)
7270 return nullptr;
7271
7272 // The requested index identifies a part of a nested aggregate. Handle
7273 // this specially. For example,
7274 // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
7275 // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
7276 // %C = extractvalue {i32, { i32, i32 } } %B, 1
7277 // This can be changed into
7278 // %A = insertvalue {i32, i32 } undef, i32 10, 0
7279 // %C = insertvalue {i32, i32 } %A, i32 11, 1
7280 // which allows the unused 0,0 element from the nested struct to be
7281 // removed.
7282 return BuildSubAggregate(V, ArrayRef(idx_range.begin(), req_idx),
7283 *InsertBefore);
7284 }
7285
7286 // This insert value inserts something else than what we are looking for.
7287 // See if the (aggregate) value inserted into has the value we are
7288 // looking for, then.
7289 if (*req_idx != *i)
7290 return FindInsertedValue(I->getAggregateOperand(), idx_range,
7291 InsertBefore);
7292 }
7293 // If we end up here, the indices of the insertvalue match with those
7294 // requested (though possibly only partially). Now we recursively look at
7295 // the inserted value, passing any remaining indices.
7296 return FindInsertedValue(I->getInsertedValueOperand(),
7297 ArrayRef(req_idx, idx_range.end()), InsertBefore);
7298 }
7299
7301 // If we're extracting a value from an aggregate that was extracted from
7302 // something else, we can extract from that something else directly instead.
7303 // However, we will need to chain I's indices with the requested indices.
7304
7305 // Calculate the number of indices required
7306 unsigned size = I->getNumIndices() + idx_range.size();
7307 // Allocate some space to put the new indices in
7309 Idxs.reserve(size);
7310 // Add indices from the extract value instruction
7311 Idxs.append(I->idx_begin(), I->idx_end());
7312
7313 // Add requested indices
7314 Idxs.append(idx_range.begin(), idx_range.end());
7315
7316 assert(Idxs.size() == size
7317 && "Number of indices added not correct?");
7318
7319 return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
7320 }
7321 // Otherwise, we don't know (such as, extracting from a function return value
7322 // or load instruction)
7323 return nullptr;
7324}
7325
7326// If V refers to an initialized global constant, set Slice either to
7327// its initializer if the size of its elements equals ElementSize, or,
7328// for ElementSize == 8, to its representation as an array of unsiged
7329// char. Return true on success.
7330// Offset is in the unit "nr of ElementSize sized elements".
7333 unsigned ElementSize, uint64_t Offset) {
7334 assert(V && "V should not be null.");
7335 assert((ElementSize % 8) == 0 &&
7336 "ElementSize expected to be a multiple of the size of a byte.");
7337 unsigned ElementSizeInBytes = ElementSize / 8;
7338
7339 // Drill down into the pointer expression V, ignoring any intervening
7340 // casts, and determine the identity of the object it references along
7341 // with the cumulative byte offset into it.
7342 const GlobalVariable *GV =
7344 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
7345 // Fail if V is not based on constant global object.
7346 return false;
7347
7348 const DataLayout &DL = GV->getDataLayout();
7349 APInt Off(DL.getIndexTypeSizeInBits(V->getType()), 0);
7350
7352 /*AllowNonInbounds*/ true))
7353 // Fail if a constant offset could not be determined.
7354 return false;
7355
7356 uint64_t StartIdx = Off.getLimitedValue();
7357 if (StartIdx == UINT64_MAX)
7358 // Fail if the constant offset is excessive.
7359 return false;
7360
7361 // Off/StartIdx is in the unit of bytes. So we need to convert to number of
7362 // elements. Simply bail out if that isn't possible.
7363 if ((StartIdx % ElementSizeInBytes) != 0)
7364 return false;
7365
7366 Offset += StartIdx / ElementSizeInBytes;
7367 ConstantDataArray *Array = nullptr;
7368 ArrayType *ArrayTy = nullptr;
7369
7370 if (GV->getInitializer()->isNullValue()) {
7371 Type *GVTy = GV->getValueType();
7372 uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy).getFixedValue();
7373 uint64_t Length = SizeInBytes / ElementSizeInBytes;
7374
7375 Slice.Array = nullptr;
7376 Slice.Offset = 0;
7377 // Return an empty Slice for undersized constants to let callers
7378 // transform even undefined library calls into simpler, well-defined
7379 // expressions. This is preferable to making the calls although it
7380 // prevents sanitizers from detecting such calls.
7381 Slice.Length = Length < Offset ? 0 : Length - Offset;
7382 return true;
7383 }
7384
7385 auto *Init = const_cast<Constant *>(GV->getInitializer());
7386 if (auto *ArrayInit = dyn_cast<ConstantDataArray>(Init)) {
7387 Type *InitElTy = ArrayInit->getElementType();
7388 if (InitElTy->isIntegerTy(ElementSize)) {
7389 // If Init is an initializer for an array of the expected type
7390 // and size, use it as is.
7391 Array = ArrayInit;
7392 ArrayTy = ArrayInit->getType();
7393 }
7394 }
7395
7396 if (!Array) {
7397 if (ElementSize != 8)
7398 // TODO: Handle conversions to larger integral types.
7399 return false;
7400
7401 // Otherwise extract the portion of the initializer starting
7402 // at Offset as an array of bytes, and reset Offset.
7404 if (!Init)
7405 return false;
7406
7407 Offset = 0;
7409 ArrayTy = dyn_cast<ArrayType>(Init->getType());
7410 }
7411
7412 uint64_t NumElts = ArrayTy->getArrayNumElements();
7413 if (Offset > NumElts)
7414 return false;
7415
7416 Slice.Array = Array;
7417 Slice.Offset = Offset;
7418 Slice.Length = NumElts - Offset;
7419 return true;
7420}
7421
7422/// Extract bytes from the initializer of the constant array V, which need
7423/// not be a nul-terminated string. On success, store the bytes in Str and
7424/// return true. When TrimAtNul is set, Str will contain only the bytes up
7425/// to but not including the first nul. Return false on failure.
7427 bool TrimAtNul) {
7429 if (!getConstantDataArrayInfo(V, Slice, 8))
7430 return false;
7431
7432 if (Slice.Array == nullptr) {
7433 if (TrimAtNul) {
7434 // Return a nul-terminated string even for an empty Slice. This is
7435 // safe because all existing SimplifyLibcalls callers require string
7436 // arguments and the behavior of the functions they fold is undefined
7437 // otherwise. Folding the calls this way is preferable to making
7438 // the undefined library calls, even though it prevents sanitizers
7439 // from reporting such calls.
7440 Str = StringRef();
7441 return true;
7442 }
7443 if (Slice.Length == 1) {
7444 Str = StringRef("", 1);
7445 return true;
7446 }
7447 // We cannot instantiate a StringRef as we do not have an appropriate string
7448 // of 0s at hand.
7449 return false;
7450 }
7451
7452 // Start out with the entire array in the StringRef.
7453 Str = Slice.Array->getAsString();
7454 // Skip over 'offset' bytes.
7455 Str = Str.substr(Slice.Offset);
7456
7457 if (TrimAtNul) {
7458 // Trim off the \0 and anything after it. If the array is not nul
7459 // terminated, we just return the whole end of string. The client may know
7460 // some other way that the string is length-bound.
7461 Str = Str.substr(0, Str.find('\0'));
7462 }
7463 return true;
7464}
7465
7466// These next two are very similar to the above, but also look through PHI
7467// nodes.
7468// TODO: See if we can integrate these two together.
7469
7470/// If we can compute the length of the string pointed to by
7471/// the specified pointer, return 'len+1'. If we can't, return 0.
7474 unsigned CharSize) {
7475 // Look through noop bitcast instructions.
7476 V = V->stripPointerCasts();
7477
7478 // If this is a PHI node, there are two cases: either we have already seen it
7479 // or we haven't.
7480 if (const PHINode *PN = dyn_cast<PHINode>(V)) {
7481 if (!PHIs.insert(PN).second)
7482 return ~0ULL; // already in the set.
7483
7484 // If it was new, see if all the input strings are the same length.
7485 uint64_t LenSoFar = ~0ULL;
7486 for (Value *IncValue : PN->incoming_values()) {
7487 uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize);
7488 if (Len == 0) return 0; // Unknown length -> unknown.
7489
7490 if (Len == ~0ULL) continue;
7491
7492 if (Len != LenSoFar && LenSoFar != ~0ULL)
7493 return 0; // Disagree -> unknown.
7494 LenSoFar = Len;
7495 }
7496
7497 // Success, all agree.
7498 return LenSoFar;
7499 }
7500
7501 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
7502 if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
7503 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);
7504 if (Len1 == 0) return 0;
7505 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);
7506 if (Len2 == 0) return 0;
7507 if (Len1 == ~0ULL) return Len2;
7508 if (Len2 == ~0ULL) return Len1;
7509 if (Len1 != Len2) return 0;
7510 return Len1;
7511 }
7512
7513 // Otherwise, see if we can read the string.
7515 if (!getConstantDataArrayInfo(V, Slice, CharSize))
7516 return 0;
7517
7518 if (Slice.Array == nullptr)
7519 // Zeroinitializer (including an empty one).
7520 return 1;
7521
7522 // Search for the first nul character. Return a conservative result even
7523 // when there is no nul. This is safe since otherwise the string function
7524 // being folded such as strlen is undefined, and can be preferable to
7525 // making the undefined library call.
7526 unsigned NullIndex = 0;
7527 for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) {
7528 if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0)
7529 break;
7530 }
7531
7532 return NullIndex + 1;
7533}
7534
7535/// If we can compute the length of the string pointed to by
7536/// the specified pointer, return 'len+1'. If we can't, return 0.
7537uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) {
7538 if (!V->getType()->isPointerTy())
7539 return 0;
7540
7542 uint64_t Len = GetStringLengthH(V, PHIs, CharSize);
7543 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
7544 // an empty string as a length.
7545 return Len == ~0ULL ? 1 : Len;
7546}
7547
7548const Value *
7550 bool MustPreserveOffset,
7551 bool MustPreserveProvenance) {
7552 assert(Call &&
7553 "getArgumentAliasingToReturnedPointer only works on nonnull calls");
7554 if (const Value *RV = Call->getReturnedArgOperand())
7555 return RV;
7556 // This can be used only as a aliasing property.
7558 Call, MustPreserveOffset, MustPreserveProvenance))
7559 return Call->getArgOperand(0);
7560 return nullptr;
7561}
7562
7564 const CallBase *Call, bool MustPreserveOffset,
7565 bool MustPreserveProvenance) {
7566 switch (Call->getIntrinsicID()) {
7567 case Intrinsic::launder_invariant_group:
7568 case Intrinsic::aarch64_irg:
7569 case Intrinsic::aarch64_tagp:
7570 // The amdgcn_make_buffer_rsrc function does not alter the address of the
7571 // input pointer (and thus preserves the byte offset, which is the property
7572 // the MustPreserveOffset flag selects). However, it will not necessarily
7573 // map ptr addrspace(N) null to ptr addrspace(8) null, aka the "null
7574 // descriptor", which has "all loads return 0, all stores are dropped"
7575 // semantics. Given the context of this intrinsic list, no one should be
7576 // relying on such a strict bit-exact null mapping (and, at time of
7577 // writing, they are not), but we document this fact out of an abundance
7578 // of caution.
7579 case Intrinsic::amdgcn_make_buffer_rsrc:
7580 return !MustPreserveProvenance;
7581 case Intrinsic::ptrmask:
7582 return !MustPreserveOffset;
7583 case Intrinsic::threadlocal_address:
7584 // The underlying variable changes with thread ID. The Thread ID may change
7585 // at coroutine suspend points.
7586 return !Call->getParent()->getParent()->isPresplitCoroutine();
7587 default:
7588 return false;
7589 }
7590}
7591
7592/// \p PN defines a loop-variant pointer to an object. Check if the
7593/// previous iteration of the loop was referring to the same object as \p PN.
7595 const LoopInfo *LI) {
7596 // Find the loop-defined value.
7597 Loop *L = LI->getLoopFor(PN->getParent());
7598 if (PN->getNumIncomingValues() != 2)
7599 return true;
7600
7601 // Find the value from previous iteration.
7602 auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
7603 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
7604 PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
7605 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
7606 return true;
7607
7608 // If a new pointer is loaded in the loop, the pointer references a different
7609 // object in every iteration. E.g.:
7610 // for (i)
7611 // int *p = a[i];
7612 // ...
7613 if (auto *Load = dyn_cast<LoadInst>(PrevValue))
7614 if (!L->isLoopInvariant(Load->getPointerOperand()))
7615 return false;
7616 return true;
7617}
7618
7619const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup,
7620 bool MustPreserveProvenance) {
7621 for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
7622 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
7623 const Value *PtrOp = GEP->getPointerOperand();
7624 if (!PtrOp->getType()->isPointerTy()) // Only handle scalar pointer base.
7625 return V;
7626 V = PtrOp;
7627 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
7628 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
7629 Value *NewV = cast<Operator>(V)->getOperand(0);
7630 if (!NewV->getType()->isPointerTy())
7631 return V;
7632 V = NewV;
7633 } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
7634 if (GA->isInterposable())
7635 return V;
7636 V = GA->getAliasee();
7637 } else {
7638 if (auto *PHI = dyn_cast<PHINode>(V)) {
7639 // Look through single-arg phi nodes created by LCSSA.
7640 if (PHI->getNumIncomingValues() == 1) {
7641 V = PHI->getIncomingValue(0);
7642 continue;
7643 }
7644 } else if (auto *Call = dyn_cast<CallBase>(V)) {
7645 // CaptureTracking can know about special capturing properties of some
7646 // intrinsics like launder.invariant.group, that can't be expressed with
7647 // the attributes, but have properties like returning aliasing pointer.
7648 // Because some analysis may assume that nocaptured pointer is not
7649 // returned from some special intrinsic (because function would have to
7650 // be marked with returns attribute), it is crucial to use this function
7651 // because it should be in sync with CaptureTracking. Not using it may
7652 // cause weird miscompilations where 2 aliasing pointers are assumed to
7653 // noalias.
7655 Call, /*MustPreserveOffset=*/false, MustPreserveProvenance)) {
7656 V = RP;
7657 continue;
7658 }
7659 }
7660
7661 return V;
7662 }
7663 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
7664 }
7665 return V;
7666}
7667
7670 const LoopInfo *LI, unsigned MaxLookup) {
7673 Worklist.push_back(V);
7674 do {
7675 const Value *P = Worklist.pop_back_val();
7676 P = getUnderlyingObject(P, MaxLookup);
7677
7678 if (!Visited.insert(P).second)
7679 continue;
7680
7681 if (auto *SI = dyn_cast<SelectInst>(P)) {
7682 Worklist.push_back(SI->getTrueValue());
7683 Worklist.push_back(SI->getFalseValue());
7684 continue;
7685 }
7686
7687 if (auto *PN = dyn_cast<PHINode>(P)) {
7688 // If this PHI changes the underlying object in every iteration of the
7689 // loop, don't look through it. Consider:
7690 // int **A;
7691 // for (i) {
7692 // Prev = Curr; // Prev = PHI (Prev_0, Curr)
7693 // Curr = A[i];
7694 // *Prev, *Curr;
7695 //
7696 // Prev is tracking Curr one iteration behind so they refer to different
7697 // underlying objects.
7698 if (!LI || !LI->isLoopHeader(PN->getParent()) ||
7700 append_range(Worklist, PN->incoming_values());
7701 else
7702 Objects.push_back(P);
7703 continue;
7704 }
7705
7706 Objects.push_back(P);
7707 } while (!Worklist.empty());
7708}
7709
7711 bool MustPreserveProvenance) {
7712 const unsigned MaxVisited = 8;
7713
7716 Worklist.push_back(V);
7717 const Value *Object = nullptr;
7718 // Used as fallback if we can't find a common underlying object through
7719 // recursion.
7720 bool First = true;
7721 const Value *FirstObject =
7722 getUnderlyingObject(V, MaxLookupSearchDepth, MustPreserveProvenance);
7723 do {
7724 const Value *P = Worklist.pop_back_val();
7725 P = First ? FirstObject
7727 MustPreserveProvenance);
7728 First = false;
7729
7730 if (!Visited.insert(P).second)
7731 continue;
7732
7733 if (Visited.size() == MaxVisited)
7734 return FirstObject;
7735
7736 if (auto *SI = dyn_cast<SelectInst>(P)) {
7737 Worklist.push_back(SI->getTrueValue());
7738 Worklist.push_back(SI->getFalseValue());
7739 continue;
7740 }
7741
7742 if (auto *PN = dyn_cast<PHINode>(P)) {
7743 append_range(Worklist, PN->incoming_values());
7744 continue;
7745 }
7746
7747 if (!Object)
7748 Object = P;
7749 else if (Object != P)
7750 return FirstObject;
7751 } while (!Worklist.empty());
7752
7753 return Object ? Object : FirstObject;
7754}
7755
7756/// This is the function that does the work of looking through basic
7757/// ptrtoint+arithmetic+inttoptr sequences.
7759 do {
7760 if (const Operator *U = dyn_cast<Operator>(V)) {
7761 // If we find a ptrtoint, we can transfer control back to the
7762 // regular getUnderlyingObjectFromInt.
7763 if (U->getOpcode() == Instruction::PtrToInt)
7764 return U->getOperand(0);
7765 // If we find an add of a constant, a multiplied value, or a phi, it's
7766 // likely that the other operand will lead us to the base
7767 // object. We don't have to worry about the case where the
7768 // object address is somehow being computed by the multiply,
7769 // because our callers only care when the result is an
7770 // identifiable object.
7771 if (U->getOpcode() != Instruction::Add ||
7772 (!isa<ConstantInt>(U->getOperand(1)) &&
7773 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
7774 !isa<PHINode>(U->getOperand(1))))
7775 return V;
7776 V = U->getOperand(0);
7777 } else {
7778 return V;
7779 }
7780 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
7781 } while (true);
7782}
7783
7784/// This is a wrapper around getUnderlyingObjects and adds support for basic
7785/// ptrtoint+arithmetic+inttoptr sequences.
7786/// It returns false if unidentified object is found in getUnderlyingObjects.
7788 SmallVectorImpl<Value *> &Objects) {
7790 SmallVector<const Value *, 4> Working(1, V);
7791 bool AllObjectsIdentified = true;
7792 do {
7793 V = Working.pop_back_val();
7794
7796 getUnderlyingObjects(V, Objs);
7797
7798 for (const Value *V : Objs) {
7799 if (!Visited.insert(V).second)
7800 continue;
7801 if (Operator::getOpcode(V) == Instruction::IntToPtr) {
7802 const Value *O =
7803 getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
7804 if (O->getType()->isPointerTy()) {
7805 Working.push_back(O);
7806 continue;
7807 }
7808 }
7809 AllObjectsIdentified &= isIdentifiedObject(V);
7810 Objects.push_back(const_cast<Value *>(V));
7811 }
7812 } while (!Working.empty());
7813 return AllObjectsIdentified;
7814}
7815
7817 AllocaInst *Result = nullptr;
7819 SmallVector<Value *, 4> Worklist;
7820
7821 auto AddWork = [&](Value *V) {
7822 if (Visited.insert(V).second)
7823 Worklist.push_back(V);
7824 };
7825
7826 AddWork(V);
7827 do {
7828 V = Worklist.pop_back_val();
7829 assert(Visited.count(V));
7830
7831 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
7832 if (Result && Result != AI)
7833 return nullptr;
7834 Result = AI;
7835 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
7836 AddWork(CI->getOperand(0));
7837 } else if (PHINode *PN = dyn_cast<PHINode>(V)) {
7838 for (Value *IncValue : PN->incoming_values())
7839 AddWork(IncValue);
7840 } else if (auto *SI = dyn_cast<SelectInst>(V)) {
7841 AddWork(SI->getTrueValue());
7842 AddWork(SI->getFalseValue());
7844 if (OffsetZero && !GEP->hasAllZeroIndices())
7845 return nullptr;
7846 AddWork(GEP->getPointerOperand());
7847 } else if (CallBase *CB = dyn_cast<CallBase>(V)) {
7848 Value *Returned = CB->getReturnedArgOperand();
7849 if (Returned)
7850 AddWork(Returned);
7851 else
7852 return nullptr;
7853 } else {
7854 return nullptr;
7855 }
7856 } while (!Worklist.empty());
7857
7858 return Result;
7859}
7860
7862 if (auto *II = dyn_cast<IntrinsicInst>(I))
7863 return isTriviallyVectorizable(II->getIntrinsicID());
7864 auto *Shuffle = dyn_cast<ShuffleVectorInst>(I);
7865 return (!Shuffle || Shuffle->isSelect()) &&
7867}
7868
7870 const Instruction *Inst, const Instruction *CtxI, AssumptionCache *AC,
7871 const DominatorTree *DT, const TargetLibraryInfo *TLI, bool UseVariableInfo,
7872 bool IgnoreUBImplyingAttrs) {
7873 return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI,
7874 AC, DT, TLI, UseVariableInfo,
7875 IgnoreUBImplyingAttrs);
7876}
7877
7879 unsigned Opcode, const Instruction *Inst, const Instruction *CtxI,
7880 AssumptionCache *AC, const DominatorTree *DT, const TargetLibraryInfo *TLI,
7881 bool UseVariableInfo, bool IgnoreUBImplyingAttrs) {
7882#ifndef NDEBUG
7883 if (Inst->getOpcode() != Opcode) {
7884 // Check that the operands are actually compatible with the Opcode override.
7885 auto hasEqualReturnAndLeadingOperandTypes =
7886 [](const Instruction *Inst, unsigned NumLeadingOperands) {
7887 if (Inst->getNumOperands() < NumLeadingOperands)
7888 return false;
7889 const Type *ExpectedType = Inst->getType();
7890 for (unsigned ItOp = 0; ItOp < NumLeadingOperands; ++ItOp)
7891 if (Inst->getOperand(ItOp)->getType() != ExpectedType)
7892 return false;
7893 return true;
7894 };
7896 hasEqualReturnAndLeadingOperandTypes(Inst, 2));
7897 assert(!Instruction::isUnaryOp(Opcode) ||
7898 hasEqualReturnAndLeadingOperandTypes(Inst, 1));
7899 }
7900#endif
7901
7902 switch (Opcode) {
7903 default:
7904 return true;
7905 case Instruction::UDiv:
7906 case Instruction::URem: {
7907 // x / y is undefined if y == 0.
7908 const APInt *V;
7909 if (match(Inst->getOperand(1), m_APInt(V)))
7910 return *V != 0;
7911 return false;
7912 }
7913 case Instruction::SDiv:
7914 case Instruction::SRem: {
7915 // x / y is undefined if y == 0 or x == INT_MIN and y == -1
7916 const APInt *Numerator, *Denominator;
7917 if (!match(Inst->getOperand(1), m_APInt(Denominator)))
7918 return false;
7919 // We cannot hoist this division if the denominator is 0.
7920 if (*Denominator == 0)
7921 return false;
7922 // It's safe to hoist if the denominator is not 0 or -1.
7923 if (!Denominator->isAllOnes())
7924 return true;
7925 // At this point we know that the denominator is -1. It is safe to hoist as
7926 // long we know that the numerator is not INT_MIN.
7927 if (match(Inst->getOperand(0), m_APInt(Numerator)))
7928 return !Numerator->isMinSignedValue();
7929 // The numerator *might* be MinSignedValue.
7930 return false;
7931 }
7932 case Instruction::Load: {
7933 if (!UseVariableInfo)
7934 return false;
7935
7936 const LoadInst *LI = dyn_cast<LoadInst>(Inst);
7937 if (!LI)
7938 return false;
7939 if (mustSuppressSpeculation(*LI))
7940 return false;
7941 const DataLayout &DL = LI->getDataLayout();
7943 LI->getPointerOperand(), LI->getType(), LI->getAlign(),
7944 SimplifyQuery(DL, TLI, DT, AC, CtxI));
7945 }
7946 case Instruction::Call: {
7947 auto *CI = dyn_cast<const CallInst>(Inst);
7948 if (!CI)
7949 return false;
7950 const Function *Callee = CI->getCalledFunction();
7951
7952 // The called function could have undefined behavior or side-effects, even
7953 // if marked readnone nounwind.
7954 if (!Callee || !Callee->isSpeculatable())
7955 return false;
7956 // Since the operands may be changed after hoisting, undefined behavior may
7957 // be triggered by some UB-implying attributes.
7958 return IgnoreUBImplyingAttrs || !CI->hasUBImplyingAttrs();
7959 }
7960 case Instruction::VAArg:
7961 case Instruction::Alloca:
7962 case Instruction::Invoke:
7963 case Instruction::CallBr:
7964 case Instruction::PHI:
7965 case Instruction::Store:
7966 case Instruction::Ret:
7967 case Instruction::UncondBr:
7968 case Instruction::CondBr:
7969 case Instruction::IndirectBr:
7970 case Instruction::Switch:
7971 case Instruction::Unreachable:
7972 case Instruction::Fence:
7973 case Instruction::AtomicRMW:
7974 case Instruction::AtomicCmpXchg:
7975 case Instruction::LandingPad:
7976 case Instruction::Resume:
7977 case Instruction::CatchSwitch:
7978 case Instruction::CatchPad:
7979 case Instruction::CatchRet:
7980 case Instruction::CleanupPad:
7981 case Instruction::CleanupRet:
7982 return false; // Misc instructions which have effects
7983 }
7984}
7985
7987 if (I.mayReadOrWriteMemory())
7988 // Memory dependency possible
7989 return true;
7991 // Can't move above a maythrow call or infinite loop. Or if an
7992 // inalloca alloca, above a stacksave call.
7993 return true;
7995 // 1) Can't reorder two inf-loop calls, even if readonly
7996 // 2) Also can't reorder an inf-loop call below a instruction which isn't
7997 // safe to speculative execute. (Inverse of above)
7998 return true;
7999 return false;
8000}
8001
8002/// Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
8016
8017/// Combine constant ranges from computeConstantRange() and computeKnownBits().
8020 bool ForSigned,
8021 const SimplifyQuery &SQ) {
8022 ConstantRange CR1 =
8023 ConstantRange::fromKnownBits(V.getKnownBits(SQ), ForSigned);
8024 ConstantRange CR2 = computeConstantRange(V, ForSigned, SQ);
8027 return CR1.intersectWith(CR2, RangeType);
8028}
8029
8031 const Value *RHS,
8032 const SimplifyQuery &SQ,
8033 bool IsNSW) {
8034 ConstantRange LHSRange =
8035 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
8036 ConstantRange RHSRange =
8037 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
8038
8039 // mul nsw of two non-negative numbers is also nuw.
8040 if (IsNSW && LHSRange.isAllNonNegative() && RHSRange.isAllNonNegative())
8042
8043 return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange));
8044}
8045
8047 const Value *RHS,
8048 const SimplifyQuery &SQ) {
8049 // Multiplying n * m significant bits yields a result of n + m significant
8050 // bits. If the total number of significant bits does not exceed the
8051 // result bit width (minus 1), there is no overflow.
8052 // This means if we have enough leading sign bits in the operands
8053 // we can guarantee that the result does not overflow.
8054 // Ref: "Hacker's Delight" by Henry Warren
8055 unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
8056
8057 // Note that underestimating the number of sign bits gives a more
8058 // conservative answer.
8059 unsigned SignBits =
8060 ::ComputeNumSignBits(LHS, SQ) + ::ComputeNumSignBits(RHS, SQ);
8061
8062 // First handle the easy case: if we have enough sign bits there's
8063 // definitely no overflow.
8064 if (SignBits > BitWidth + 1)
8066
8067 // There are two ambiguous cases where there can be no overflow:
8068 // SignBits == BitWidth + 1 and
8069 // SignBits == BitWidth
8070 // The second case is difficult to check, therefore we only handle the
8071 // first case.
8072 if (SignBits == BitWidth + 1) {
8073 // It overflows only when both arguments are negative and the true
8074 // product is exactly the minimum negative number.
8075 // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
8076 // For simplicity we just check if at least one side is not negative.
8077 KnownBits LHSKnown = computeKnownBits(LHS, SQ);
8078 KnownBits RHSKnown = computeKnownBits(RHS, SQ);
8079 if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative())
8081 }
8083}
8084
8087 const WithCache<const Value *> &RHS,
8088 const SimplifyQuery &SQ) {
8089 ConstantRange LHSRange =
8090 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
8091 ConstantRange RHSRange =
8092 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
8093 return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange));
8094}
8095
8096static OverflowResult
8099 const AddOperator *Add, const SimplifyQuery &SQ) {
8100 if (Add && Add->hasNoSignedWrap()) {
8102 }
8103
8104 // If LHS and RHS each have at least two sign bits, the addition will look
8105 // like
8106 //
8107 // XX..... +
8108 // YY.....
8109 //
8110 // If the carry into the most significant position is 0, X and Y can't both
8111 // be 1 and therefore the carry out of the addition is also 0.
8112 //
8113 // If the carry into the most significant position is 1, X and Y can't both
8114 // be 0 and therefore the carry out of the addition is also 1.
8115 //
8116 // Since the carry into the most significant position is always equal to
8117 // the carry out of the addition, there is no signed overflow.
8118 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
8120
8121 ConstantRange LHSRange =
8122 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
8123 ConstantRange RHSRange =
8124 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
8125 OverflowResult OR =
8126 mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange));
8128 return OR;
8129
8130 // The remaining code needs Add to be available. Early returns if not so.
8131 if (!Add)
8133
8134 // If the sign of Add is the same as at least one of the operands, this add
8135 // CANNOT overflow. If this can be determined from the known bits of the
8136 // operands the above signedAddMayOverflow() check will have already done so.
8137 // The only other way to improve on the known bits is from an assumption, so
8138 // call computeKnownBitsFromContext() directly.
8139 bool LHSOrRHSKnownNonNegative =
8140 (LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative());
8141 bool LHSOrRHSKnownNegative =
8142 (LHSRange.isAllNegative() || RHSRange.isAllNegative());
8143 if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
8144 KnownBits AddKnown(LHSRange.getBitWidth());
8145 computeKnownBitsFromContext(Add, AddKnown, SQ);
8146 if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||
8147 (AddKnown.isNegative() && LHSOrRHSKnownNegative))
8149 }
8150
8152}
8153
8155 const Value *RHS,
8156 const SimplifyQuery &SQ) {
8157 // X - (X % ?)
8158 // The remainder of a value can't have greater magnitude than itself,
8159 // so the subtraction can't overflow.
8160
8161 // X - (X -nuw ?)
8162 // In the minimal case, this would simplify to "?", so there's no subtract
8163 // at all. But if this analysis is used to peek through casts, for example,
8164 // then determining no-overflow may allow other transforms.
8165
8166 // TODO: There are other patterns like this.
8167 // See simplifyICmpWithBinOpOnLHS() for candidates.
8168 if (match(RHS, m_URem(m_Specific(LHS), m_Value())) ||
8169 match(RHS, m_NUWSub(m_Specific(LHS), m_Value())))
8170 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CtxI, SQ.DT))
8172
8173 if (auto C = isImpliedByDomCondition(CmpInst::ICMP_UGE, LHS, RHS, SQ.CtxI,
8174 SQ.DL)) {
8175 if (*C)
8178 }
8179
8180 ConstantRange LHSRange =
8181 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
8182 ConstantRange RHSRange =
8183 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
8184 return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange));
8185}
8186
8188 const Value *RHS,
8189 const SimplifyQuery &SQ) {
8190 // X - (X % ?)
8191 // The remainder of a value can't have greater magnitude than itself,
8192 // so the subtraction can't overflow.
8193
8194 // X - (X -nsw ?)
8195 // In the minimal case, this would simplify to "?", so there's no subtract
8196 // at all. But if this analysis is used to peek through casts, for example,
8197 // then determining no-overflow may allow other transforms.
8198 if (match(RHS, m_SRem(m_Specific(LHS), m_Value())) ||
8199 match(RHS, m_NSWSub(m_Specific(LHS), m_Value())))
8200 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CtxI, SQ.DT))
8202
8203 // If LHS and RHS each have at least two sign bits, the subtraction
8204 // cannot overflow.
8205 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
8207
8208 ConstantRange LHSRange =
8209 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
8210 ConstantRange RHSRange =
8211 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
8212 return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange));
8213}
8214
8216 const DominatorTree &DT) {
8217 SmallVector<const CondBrInst *, 2> GuardingBranches;
8219
8220 for (const User *U : WO->users()) {
8221 if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) {
8222 assert(EVI->getNumIndices() == 1 && "Obvious from CI's type");
8223
8224 if (EVI->getIndices()[0] == 0)
8225 Results.push_back(EVI);
8226 else {
8227 assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type");
8228
8229 for (const auto *U : EVI->users())
8230 if (const auto *B = dyn_cast<CondBrInst>(U))
8231 GuardingBranches.push_back(B);
8232 }
8233 } else {
8234 // We are using the aggregate directly in a way we don't want to analyze
8235 // here (storing it to a global, say).
8236 return false;
8237 }
8238 }
8239
8240 auto AllUsesGuardedByBranch = [&](const CondBrInst *BI) {
8241 BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1));
8242
8243 // Check if all users of the add are provably no-wrap.
8244 for (const auto *Result : Results) {
8245 // If the extractvalue itself is not executed on overflow, the we don't
8246 // need to check each use separately, since domination is transitive.
8247 if (DT.dominates(NoWrapEdge, Result->getParent()))
8248 continue;
8249
8250 for (const auto &RU : Result->uses())
8251 if (!DT.dominates(NoWrapEdge, RU))
8252 return false;
8253 }
8254
8255 return true;
8256 };
8257
8258 return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
8259}
8260
8261/// Shifts return poison if shiftwidth is larger than the bitwidth.
8262static bool shiftAmountKnownInRange(const Value *ShiftAmount) {
8263 auto *C = dyn_cast<Constant>(ShiftAmount);
8264 if (!C)
8265 return false;
8266
8267 // Shifts return poison if shiftwidth is larger than the bitwidth.
8269 if (auto *FVTy = dyn_cast<FixedVectorType>(C->getType())) {
8270 unsigned NumElts = FVTy->getNumElements();
8271 for (unsigned i = 0; i < NumElts; ++i)
8272 ShiftAmounts.push_back(C->getAggregateElement(i));
8273 } else if (isa<ScalableVectorType>(C->getType()))
8274 return false; // Can't tell, just return false to be safe
8275 else
8276 ShiftAmounts.push_back(C);
8277
8278 bool Safe = llvm::all_of(ShiftAmounts, [](const Constant *C) {
8279 auto *CI = dyn_cast_or_null<ConstantInt>(C);
8280 return CI && CI->getValue().ult(C->getType()->getIntegerBitWidth());
8281 });
8282
8283 return Safe;
8284}
8285
8287 bool ConsiderFlagsAndMetadata) {
8288 const auto *CB = dyn_cast<CallBase>(Op);
8289 bool CBHasNoUndef = CB && CB->hasRetAttr(Attribute::NoUndef);
8290 if (ConsiderFlagsAndMetadata && includesPoison(Kind) &&
8291 Op->hasPoisonGeneratingAnnotations() && !CBHasNoUndef)
8292 return true;
8293
8294 unsigned Opcode = Op->getOpcode();
8295
8296 // Check whether opcode is a poison/undef-generating operation
8297 switch (Opcode) {
8298 case Instruction::Shl:
8299 case Instruction::AShr:
8300 case Instruction::LShr:
8301 return includesPoison(Kind) && !shiftAmountKnownInRange(Op->getOperand(1));
8302 case Instruction::FPToSI:
8303 case Instruction::FPToUI:
8304 // fptosi/ui yields poison if the resulting value does not fit in the
8305 // destination type.
8306 return true;
8307 case Instruction::Call:
8308 if (auto *II = dyn_cast<IntrinsicInst>(Op)) {
8309 switch (II->getIntrinsicID()) {
8310 // NOTE: Use IntrNoCreateUndefOrPoison when possible.
8311 case Intrinsic::ctlz:
8312 case Intrinsic::cttz:
8313 case Intrinsic::abs:
8314 // We're not considering flags so it is safe to just return false.
8315 return false;
8316 case Intrinsic::sshl_sat:
8317 case Intrinsic::ushl_sat:
8318 if (!includesPoison(Kind) ||
8319 shiftAmountKnownInRange(II->getArgOperand(1)))
8320 return false;
8321 break;
8322 }
8323 }
8324 [[fallthrough]];
8325 case Instruction::CallBr:
8326 case Instruction::Invoke: {
8327 return !CBHasNoUndef && !CB->hasFnAttr(Attribute::NoCreateUndefOrPoison);
8328 }
8329 case Instruction::InsertElement:
8330 case Instruction::ExtractElement: {
8331 // If index exceeds the length of the vector, it returns poison
8332 auto *VTy = cast<VectorType>(Op->getOperand(0)->getType());
8333 unsigned IdxOp = Op->getOpcode() == Instruction::InsertElement ? 2 : 1;
8334 auto *Idx = dyn_cast<ConstantInt>(Op->getOperand(IdxOp));
8335 if (includesPoison(Kind))
8336 return !Idx ||
8337 Idx->getValue().uge(VTy->getElementCount().getKnownMinValue());
8338 return false;
8339 }
8340 case Instruction::ShuffleVector: {
8342 ? cast<ConstantExpr>(Op)->getShuffleMask()
8343 : cast<ShuffleVectorInst>(Op)->getShuffleMask();
8344 return includesPoison(Kind) && is_contained(Mask, PoisonMaskElem);
8345 }
8346 case Instruction::FNeg:
8347 case Instruction::PHI:
8348 case Instruction::Select:
8349 case Instruction::ExtractValue:
8350 case Instruction::InsertValue:
8351 case Instruction::Freeze:
8352 case Instruction::ICmp:
8353 case Instruction::FCmp:
8354 case Instruction::GetElementPtr:
8355 return false;
8356 case Instruction::AddrSpaceCast:
8357 return true;
8358 default: {
8359 const auto *CE = dyn_cast<ConstantExpr>(Op);
8360 if (isa<CastInst>(Op) || (CE && CE->isCast()))
8361 return false;
8362 else if (Instruction::isBinaryOp(Opcode))
8363 return false;
8364 // Be conservative and return true.
8365 return true;
8366 }
8367 }
8368}
8369
8371 bool ConsiderFlagsAndMetadata) {
8372 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::UndefOrPoison,
8373 ConsiderFlagsAndMetadata);
8374}
8375
8376bool llvm::canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata) {
8377 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::PoisonOnly,
8378 ConsiderFlagsAndMetadata);
8379}
8380
8381static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V,
8382 unsigned Depth) {
8383 if (ValAssumedPoison == V)
8384 return true;
8385
8386 const unsigned MaxDepth = 2;
8387 if (Depth >= MaxDepth)
8388 return false;
8389
8390 if (const auto *I = dyn_cast<Instruction>(V)) {
8391 if (any_of(I->operands(), [=](const Use &Op) {
8392 return propagatesPoison(Op) &&
8393 directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1);
8394 }))
8395 return true;
8396
8397 // V = extractvalue V0, idx
8398 // V2 = extractvalue V0, idx2
8399 // V0's elements are all poison or not. (e.g., add_with_overflow)
8400 const WithOverflowInst *II;
8402 (match(ValAssumedPoison, m_ExtractValue(m_Specific(II))) ||
8403 llvm::is_contained(II->args(), ValAssumedPoison)))
8404 return true;
8405 }
8406 return false;
8407}
8408
8409static bool impliesPoison(const Value *ValAssumedPoison, const Value *V,
8410 unsigned Depth) {
8411 if (isGuaranteedNotToBePoison(ValAssumedPoison))
8412 return true;
8413
8414 if (directlyImpliesPoison(ValAssumedPoison, V, /* Depth */ 0))
8415 return true;
8416
8417 const unsigned MaxDepth = 2;
8418 if (Depth >= MaxDepth)
8419 return false;
8420
8421 const auto *I = dyn_cast<Instruction>(ValAssumedPoison);
8422 if (I && !canCreatePoison(cast<Operator>(I))) {
8423 return all_of(I->operands(), [=](const Value *Op) {
8424 return impliesPoison(Op, V, Depth + 1);
8425 });
8426 }
8427 return false;
8428}
8429
8430bool llvm::impliesPoison(const Value *ValAssumedPoison, const Value *V) {
8431 return ::impliesPoison(ValAssumedPoison, V, /* Depth */ 0);
8432}
8433
8434static bool programUndefinedIfUndefOrPoison(const Value *V, bool PoisonOnly);
8435
8437 const Value *V, AssumptionCache *AC, const Instruction *CtxI,
8438 const DominatorTree *DT, unsigned Depth, UndefPoisonKind Kind) {
8440 return false;
8441
8443 return false;
8444
8445 if (const auto *A = dyn_cast<Argument>(V)) {
8446 if (A->hasAttribute(Attribute::NoUndef) ||
8447 A->hasAttribute(Attribute::Dereferenceable) ||
8448 A->hasAttribute(Attribute::DereferenceableOrNull))
8449 return true;
8450 }
8451
8452 if (auto *C = dyn_cast<Constant>(V)) {
8453 if (isa<PoisonValue>(C))
8454 return !includesPoison(Kind);
8455
8456 if (isa<UndefValue>(C))
8457 return !includesUndef(Kind);
8458
8461 return true;
8462
8463 if (C->getType()->isVectorTy() || C->getType()->isAggregateType()) {
8464 if (isa<ConstantExpr>(C)) {
8465 // Scalable vectors can use a ConstantExpr to build a splat.
8466 if (Constant *SplatC = C->getSplatValue())
8467 if (isa<ConstantInt>(SplatC) || isa<ConstantFP>(SplatC))
8468 return true;
8469 } else if (auto *CA = dyn_cast<ConstantAggregate>(C)) {
8470 return all_of(CA->operands(), [&](const Use &Op) {
8471 return isGuaranteedNotToBeUndefOrPoison(Op.get(), AC, CtxI, DT,
8472 Depth + 1, Kind);
8473 });
8474 }
8475 }
8476 }
8477
8478 // Strip cast operations from a pointer value.
8479 // Note that stripPointerCastsSameRepresentation can strip off getelementptr
8480 // inbounds with zero offset. To guarantee that the result isn't poison, the
8481 // stripped pointer is checked as it has to be pointing into an allocated
8482 // object or be null `null` to ensure `inbounds` getelement pointers with a
8483 // zero offset could not produce poison.
8484 // It can strip off addrspacecast that do not change bit representation as
8485 // well. We believe that such addrspacecast is equivalent to no-op.
8486 auto *StrippedV = V->stripPointerCastsSameRepresentation();
8487 if (isa<AllocaInst>(StrippedV) || isa<GlobalVariable>(StrippedV) ||
8488 isa<Function>(StrippedV) || isa<ConstantPointerNull>(StrippedV))
8489 return true;
8490
8491 auto OpCheck = [&](const Value *V) {
8492 return isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth + 1, Kind);
8493 };
8494
8495 if (auto *Opr = dyn_cast<Operator>(V)) {
8496 // If the value is a freeze instruction, then it can never
8497 // be undef or poison.
8498 if (isa<FreezeInst>(V))
8499 return true;
8500
8501 if (const auto *CB = dyn_cast<CallBase>(V)) {
8502 if (CB->hasRetAttr(Attribute::NoUndef) ||
8503 CB->hasRetAttr(Attribute::Dereferenceable) ||
8504 CB->hasRetAttr(Attribute::DereferenceableOrNull))
8505 return true;
8506 }
8507
8508 if (!::canCreateUndefOrPoison(Opr, Kind,
8509 /*ConsiderFlagsAndMetadata=*/true)) {
8510 if (const auto *PN = dyn_cast<PHINode>(V)) {
8511 unsigned Num = PN->getNumIncomingValues();
8512 bool IsWellDefined = true;
8513 for (unsigned i = 0; i < Num; ++i) {
8514 if (PN == PN->getIncomingValue(i))
8515 continue;
8516 auto *TI = PN->getIncomingBlock(i)->getTerminator();
8517 if (!isGuaranteedNotToBeUndefOrPoison(PN->getIncomingValue(i), AC, TI,
8518 DT, Depth + 1, Kind)) {
8519 IsWellDefined = false;
8520 break;
8521 }
8522 }
8523 if (IsWellDefined)
8524 return true;
8525 } else if (auto *Splat = isa<ShuffleVectorInst>(Opr) ? getSplatValue(Opr)
8526 : nullptr) {
8527 // For splats we only need to check the value being splatted.
8528 if (OpCheck(Splat))
8529 return true;
8530 } else if (all_of(Opr->operands(), OpCheck))
8531 return true;
8532 }
8533 }
8534
8535 if (auto *I = dyn_cast<LoadInst>(V))
8536 if (I->hasMetadata(LLVMContext::MD_noundef) ||
8537 I->hasMetadata(LLVMContext::MD_dereferenceable) ||
8538 I->hasMetadata(LLVMContext::MD_dereferenceable_or_null))
8539 return true;
8540
8542 return true;
8543
8544 // CtxI may be null or a cloned instruction.
8545 if (!CtxI || !CtxI->getParent() || !DT)
8546 return false;
8547
8548 auto *DNode = DT->getNode(CtxI->getParent());
8549 if (!DNode)
8550 // Unreachable block
8551 return false;
8552
8553 // If V is used as a branch condition before reaching CtxI, V cannot be
8554 // undef or poison.
8555 // br V, BB1, BB2
8556 // BB1:
8557 // CtxI ; V cannot be undef or poison here
8558 auto *Dominator = DNode->getIDom();
8559 // This check is purely for compile time reasons: we can skip the IDom walk
8560 // if what we are checking for includes undef and the value is not an integer.
8561 if (!includesUndef(Kind) || V->getType()->isIntegerTy())
8562 while (Dominator) {
8563 auto *TI = Dominator->getBlock()->getTerminatorOrNull();
8564
8565 Value *Cond = nullptr;
8566 if (auto BI = dyn_cast_or_null<CondBrInst>(TI)) {
8567 Cond = BI->getCondition();
8568 } else if (auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
8569 Cond = SI->getCondition();
8570 }
8571
8572 if (Cond) {
8573 if (Cond == V)
8574 return true;
8575 else if (!includesUndef(Kind) && isa<Operator>(Cond)) {
8576 // For poison, we can analyze further
8577 auto *Opr = cast<Operator>(Cond);
8578 if (any_of(Opr->operands(), [V](const Use &U) {
8579 return V == U && propagatesPoison(U);
8580 }))
8581 return true;
8582 }
8583 }
8584
8585 Dominator = Dominator->getIDom();
8586 }
8587
8588 if (AC && getKnowledgeValidInContext(V, {Attribute::NoUndef}, *AC, CtxI, DT))
8589 return true;
8590
8591 return false;
8592}
8593
8595 const Instruction *CtxI,
8596 const DominatorTree *DT,
8597 unsigned Depth) {
8598 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
8600}
8601
8603 const Instruction *CtxI,
8604 const DominatorTree *DT, unsigned Depth) {
8605 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
8607}
8608
8610 const Instruction *CtxI,
8611 const DominatorTree *DT, unsigned Depth) {
8612 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
8614}
8615
8616/// Return true if undefined behavior would provably be executed on the path to
8617/// OnPathTo if Root produced a posion result. Note that this doesn't say
8618/// anything about whether OnPathTo is actually executed or whether Root is
8619/// actually poison. This can be used to assess whether a new use of Root can
8620/// be added at a location which is control equivalent with OnPathTo (such as
8621/// immediately before it) without introducing UB which didn't previously
8622/// exist. Note that a false result conveys no information.
8624 Instruction *OnPathTo,
8625 DominatorTree *DT) {
8626 // Basic approach is to assume Root is poison, propagate poison forward
8627 // through all users we can easily track, and then check whether any of those
8628 // users are provable UB and must execute before out exiting block might
8629 // exit.
8630
8631 // The set of all recursive users we've visited (which are assumed to all be
8632 // poison because of said visit)
8635 Worklist.push_back(Root);
8636 while (!Worklist.empty()) {
8637 const Instruction *I = Worklist.pop_back_val();
8638
8639 // If we know this must trigger UB on a path leading our target.
8640 if (mustTriggerUB(I, KnownPoison) && DT->dominates(I, OnPathTo))
8641 return true;
8642
8643 // If we can't analyze propagation through this instruction, just skip it
8644 // and transitive users. Safe as false is a conservative result.
8645 if (I != Root && !any_of(I->operands(), [&KnownPoison](const Use &U) {
8646 return KnownPoison.contains(U) && propagatesPoison(U);
8647 }))
8648 continue;
8649
8650 if (KnownPoison.insert(I).second)
8651 for (const User *User : I->users())
8652 Worklist.push_back(cast<Instruction>(User));
8653 }
8654
8655 // Might be non-UB, or might have a path we couldn't prove must execute on
8656 // way to exiting bb.
8657 return false;
8658}
8659
8661 const SimplifyQuery &SQ) {
8662 return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1),
8663 Add, SQ);
8664}
8665
8668 const WithCache<const Value *> &RHS,
8669 const SimplifyQuery &SQ) {
8670 return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, SQ);
8671}
8672
8674 // Note: An atomic operation isn't guaranteed to return in a reasonable amount
8675 // of time because it's possible for another thread to interfere with it for an
8676 // arbitrary length of time, but programs aren't allowed to rely on that.
8677
8678 // If there is no successor, then execution can't transfer to it.
8679 if (isa<ReturnInst>(I))
8680 return false;
8682 return false;
8683
8684 // Note: Do not add new checks here; instead, change Instruction::mayThrow or
8685 // Instruction::willReturn.
8686 //
8687 // FIXME: Move this check into Instruction::willReturn.
8688 if (isa<CatchPadInst>(I)) {
8689 switch (classifyEHPersonality(I->getFunction()->getPersonalityFn())) {
8690 default:
8691 // A catchpad may invoke exception object constructors and such, which
8692 // in some languages can be arbitrary code, so be conservative by default.
8693 return false;
8695 // For CoreCLR, it just involves a type test.
8696 return true;
8697 }
8698 }
8699
8700 // An instruction that returns without throwing must transfer control flow
8701 // to a successor.
8702 return !I->mayThrow() && I->willReturn();
8703}
8704
8706 // TODO: This is slightly conservative for invoke instruction since exiting
8707 // via an exception *is* normal control for them.
8708 for (const Instruction &I : *BB)
8710 return false;
8711 return true;
8712}
8713
8720
8723 assert(ScanLimit && "scan limit must be non-zero");
8724 for (const Instruction &I : Range) {
8725 if (--ScanLimit == 0)
8726 return false;
8728 return false;
8729 }
8730 return true;
8731}
8732
8734 const Loop *L) {
8735 // The loop header is guaranteed to be executed for every iteration.
8736 //
8737 // FIXME: Relax this constraint to cover all basic blocks that are
8738 // guaranteed to be executed at every iteration.
8739 if (I->getParent() != L->getHeader()) return false;
8740
8741 for (const Instruction &LI : *L->getHeader()) {
8742 if (&LI == I) return true;
8743 if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;
8744 }
8745 llvm_unreachable("Instruction not contained in its own parent basic block.");
8746}
8747
8749 switch (IID) {
8750 // TODO: Add more intrinsics.
8751 case Intrinsic::sadd_with_overflow:
8752 case Intrinsic::ssub_with_overflow:
8753 case Intrinsic::smul_with_overflow:
8754 case Intrinsic::uadd_with_overflow:
8755 case Intrinsic::usub_with_overflow:
8756 case Intrinsic::umul_with_overflow:
8757 // If an input is a vector containing a poison element, the
8758 // two output vectors (calculated results, overflow bits)'
8759 // corresponding lanes are poison.
8760 return true;
8761 case Intrinsic::ctpop:
8762 case Intrinsic::ctlz:
8763 case Intrinsic::cttz:
8764 case Intrinsic::abs:
8765 case Intrinsic::smax:
8766 case Intrinsic::smin:
8767 case Intrinsic::umax:
8768 case Intrinsic::umin:
8769 case Intrinsic::scmp:
8770 case Intrinsic::smulh:
8771 case Intrinsic::umulh:
8772 case Intrinsic::is_fpclass:
8773 case Intrinsic::ptrmask:
8774 case Intrinsic::ucmp:
8775 case Intrinsic::bitreverse:
8776 case Intrinsic::bswap:
8777 case Intrinsic::sadd_sat:
8778 case Intrinsic::ssub_sat:
8779 case Intrinsic::sshl_sat:
8780 case Intrinsic::uadd_sat:
8781 case Intrinsic::usub_sat:
8782 case Intrinsic::ushl_sat:
8783 case Intrinsic::smul_fix:
8784 case Intrinsic::smul_fix_sat:
8785 case Intrinsic::umul_fix:
8786 case Intrinsic::umul_fix_sat:
8787 case Intrinsic::pow:
8788 case Intrinsic::experimental_constrained_pow:
8789 case Intrinsic::powi:
8790 case Intrinsic::experimental_constrained_powi:
8791 case Intrinsic::sin:
8792 case Intrinsic::experimental_constrained_sin:
8793 case Intrinsic::sinh:
8794 case Intrinsic::experimental_constrained_sinh:
8795 case Intrinsic::cos:
8796 case Intrinsic::experimental_constrained_cos:
8797 case Intrinsic::cosh:
8798 case Intrinsic::experimental_constrained_cosh:
8799 case Intrinsic::sincos:
8800 case Intrinsic::sincospi:
8801 case Intrinsic::tan:
8802 case Intrinsic::experimental_constrained_tan:
8803 case Intrinsic::tanh:
8804 case Intrinsic::experimental_constrained_tanh:
8805 case Intrinsic::asin:
8806 case Intrinsic::experimental_constrained_asin:
8807 case Intrinsic::acos:
8808 case Intrinsic::experimental_constrained_acos:
8809 case Intrinsic::atan:
8810 case Intrinsic::experimental_constrained_atan:
8811 case Intrinsic::atan2:
8812 case Intrinsic::experimental_constrained_atan2:
8813 case Intrinsic::canonicalize:
8814 case Intrinsic::sqrt:
8815 case Intrinsic::experimental_constrained_sqrt:
8816 case Intrinsic::fma:
8817 case Intrinsic::experimental_constrained_fma:
8818 case Intrinsic::fmuladd:
8819 case Intrinsic::experimental_constrained_fmuladd:
8820 case Intrinsic::exp:
8821 case Intrinsic::experimental_constrained_exp:
8822 case Intrinsic::exp2:
8823 case Intrinsic::experimental_constrained_exp2:
8824 case Intrinsic::exp10:
8825 case Intrinsic::log:
8826 case Intrinsic::experimental_constrained_log:
8827 case Intrinsic::log2:
8828 case Intrinsic::experimental_constrained_log2:
8829 case Intrinsic::log10:
8830 case Intrinsic::experimental_constrained_log10:
8831 case Intrinsic::modf:
8832 case Intrinsic::floor:
8833 case Intrinsic::experimental_constrained_floor:
8834 case Intrinsic::ceil:
8835 case Intrinsic::experimental_constrained_ceil:
8836 case Intrinsic::trunc:
8837 case Intrinsic::experimental_constrained_trunc:
8838 case Intrinsic::rint:
8839 case Intrinsic::experimental_constrained_rint:
8840 case Intrinsic::nearbyint:
8841 case Intrinsic::experimental_constrained_nearbyint:
8842 case Intrinsic::round:
8843 case Intrinsic::experimental_constrained_round:
8844 case Intrinsic::roundeven:
8845 case Intrinsic::experimental_constrained_roundeven:
8846 case Intrinsic::lrint:
8847 case Intrinsic::experimental_constrained_lrint:
8848 case Intrinsic::llrint:
8849 case Intrinsic::experimental_constrained_llrint:
8850 case Intrinsic::lround:
8851 case Intrinsic::experimental_constrained_lround:
8852 case Intrinsic::llround:
8853 case Intrinsic::experimental_constrained_llround:
8854 case Intrinsic::fshl:
8855 case Intrinsic::fshr:
8856 case Intrinsic::frexp:
8857 case Intrinsic::ldexp:
8858 case Intrinsic::experimental_constrained_ldexp:
8859 case Intrinsic::get_active_lane_mask:
8860 case Intrinsic::mask_beforefirst:
8861 return true;
8862 default:
8863 return false;
8864 }
8865}
8866
8867bool llvm::propagatesPoison(const Use &PoisonOp) {
8868 const Operator *I = cast<Operator>(PoisonOp.getUser());
8869 switch (I->getOpcode()) {
8870 case Instruction::Freeze:
8871 case Instruction::PHI:
8872 case Instruction::Invoke:
8873 return false;
8874 case Instruction::Select:
8875 return PoisonOp.getOperandNo() == 0;
8876 case Instruction::Call:
8877 if (auto *II = dyn_cast<IntrinsicInst>(I))
8878 return intrinsicPropagatesPoison(II->getIntrinsicID());
8879 return false;
8880 case Instruction::ICmp:
8881 case Instruction::FCmp:
8882 case Instruction::GetElementPtr:
8883 return true;
8884 default:
8886 return true;
8887
8888 // Be conservative and return false.
8889 return false;
8890 }
8891}
8892
8893/// Enumerates all operands of \p I that are guaranteed to not be undef or
8894/// poison. If the callback \p Handle returns true, stop processing and return
8895/// true. Otherwise, return false.
8896template <typename CallableT>
8898 const CallableT &Handle) {
8899 switch (I->getOpcode()) {
8900 case Instruction::Store:
8901 if (Handle(cast<StoreInst>(I)->getPointerOperand()))
8902 return true;
8903 break;
8904
8905 case Instruction::Load:
8906 if (Handle(cast<LoadInst>(I)->getPointerOperand()))
8907 return true;
8908 break;
8909
8910 // Since dereferenceable attribute imply noundef, atomic operations
8911 // also implicitly have noundef pointers too
8912 case Instruction::AtomicCmpXchg:
8914 return true;
8915 break;
8916
8917 case Instruction::AtomicRMW:
8918 if (Handle(cast<AtomicRMWInst>(I)->getPointerOperand()))
8919 return true;
8920 break;
8921
8922 case Instruction::Call:
8923 case Instruction::Invoke: {
8924 const CallBase *CB = cast<CallBase>(I);
8925 if (CB->isIndirectCall() && Handle(CB->getCalledOperand()))
8926 return true;
8927 for (unsigned i = 0; i < CB->arg_size(); ++i)
8928 if ((CB->paramHasAttr(i, Attribute::NoUndef) ||
8929 CB->paramHasAttr(i, Attribute::Dereferenceable) ||
8930 CB->paramHasAttr(i, Attribute::DereferenceableOrNull)) &&
8931 Handle(CB->getArgOperand(i)))
8932 return true;
8933 break;
8934 }
8935 case Instruction::Ret:
8936 if (I->getFunction()->hasRetAttribute(Attribute::NoUndef) &&
8937 Handle(I->getOperand(0)))
8938 return true;
8939 break;
8940 case Instruction::Switch:
8941 if (Handle(cast<SwitchInst>(I)->getCondition()))
8942 return true;
8943 break;
8944 case Instruction::CondBr:
8945 if (Handle(cast<CondBrInst>(I)->getCondition()))
8946 return true;
8947 break;
8948 default:
8949 break;
8950 }
8951
8952 return false;
8953}
8954
8955/// Enumerates all operands of \p I that are guaranteed to not be poison.
8956template <typename CallableT>
8958 const CallableT &Handle) {
8959 if (handleGuaranteedWellDefinedOps(I, Handle))
8960 return true;
8961 switch (I->getOpcode()) {
8962 // Divisors of these operations are allowed to be partially undef.
8963 case Instruction::UDiv:
8964 case Instruction::SDiv:
8965 case Instruction::URem:
8966 case Instruction::SRem:
8967 return Handle(I->getOperand(1));
8968 default:
8969 return false;
8970 }
8971}
8972
8974 const SmallPtrSetImpl<const Value *> &KnownPoison) {
8976 I, [&](const Value *V) { return KnownPoison.count(V); });
8977}
8978
8980 bool PoisonOnly) {
8981 // We currently only look for uses of values within the same basic
8982 // block, as that makes it easier to guarantee that the uses will be
8983 // executed given that Inst is executed.
8984 //
8985 // FIXME: Expand this to consider uses beyond the same basic block. To do
8986 // this, look out for the distinction between post-dominance and strong
8987 // post-dominance.
8988 const BasicBlock *BB = nullptr;
8990 if (const auto *Inst = dyn_cast<Instruction>(V)) {
8991 BB = Inst->getParent();
8992 Begin = Inst->getIterator();
8993 Begin++;
8994 } else if (const auto *Arg = dyn_cast<Argument>(V)) {
8995 if (Arg->getParent()->isDeclaration())
8996 return false;
8997 BB = &Arg->getParent()->getEntryBlock();
8998 Begin = BB->begin();
8999 } else {
9000 return false;
9001 }
9002
9003 // Limit number of instructions we look at, to avoid scanning through large
9004 // blocks. The current limit is chosen arbitrarily.
9005 unsigned ScanLimit = 32;
9007
9008 if (!PoisonOnly) {
9009 // Since undef does not propagate eagerly, be conservative & just check
9010 // whether a value is directly passed to an instruction that must take
9011 // well-defined operands.
9012
9013 for (const auto &I : make_range(Begin, End)) {
9014 if (--ScanLimit == 0)
9015 break;
9016
9017 if (handleGuaranteedWellDefinedOps(&I, [V](const Value *WellDefinedOp) {
9018 return WellDefinedOp == V;
9019 }))
9020 return true;
9021
9023 break;
9024 }
9025 return false;
9026 }
9027
9028 // Set of instructions that we have proved will yield poison if Inst
9029 // does.
9030 SmallPtrSet<const Value *, 16> YieldsPoison;
9032
9033 YieldsPoison.insert(V);
9034 Visited.insert(BB);
9035
9036 while (true) {
9037 for (const auto &I : make_range(Begin, End)) {
9038 if (--ScanLimit == 0)
9039 return false;
9040 if (mustTriggerUB(&I, YieldsPoison))
9041 return true;
9043 return false;
9044
9045 // If an operand is poison and propagates it, mark I as yielding poison.
9046 for (const Use &Op : I.operands()) {
9047 if (YieldsPoison.count(Op) && propagatesPoison(Op)) {
9048 YieldsPoison.insert(&I);
9049 break;
9050 }
9051 }
9052
9053 // Special handling for select, which returns poison if its operand 0 is
9054 // poison (handled in the loop above) *or* if both its true/false operands
9055 // are poison (handled here).
9056 if (I.getOpcode() == Instruction::Select &&
9057 YieldsPoison.count(I.getOperand(1)) &&
9058 YieldsPoison.count(I.getOperand(2))) {
9059 YieldsPoison.insert(&I);
9060 }
9061 }
9062
9063 BB = BB->getSingleSuccessor();
9064 if (!BB || !Visited.insert(BB).second)
9065 break;
9066
9067 Begin = BB->getFirstNonPHIIt();
9068 End = BB->end();
9069 }
9070 return false;
9071}
9072
9074 return ::programUndefinedIfUndefOrPoison(Inst, false);
9075}
9076
9078 return ::programUndefinedIfUndefOrPoison(Inst, true);
9079}
9080
9081static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) {
9082 if (FMF.noNaNs())
9083 return true;
9084
9085 if (auto *C = dyn_cast<ConstantFP>(V))
9086 return !C->isNaN();
9087
9088 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
9089 if (!C->getElementType()->isFloatingPointTy())
9090 return false;
9091 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
9092 if (C->getElementAsAPFloat(I).isNaN())
9093 return false;
9094 }
9095 return true;
9096 }
9097
9099 return true;
9100
9101 return false;
9102}
9103
9104static bool isKnownNonZero(const Value *V) {
9105 if (auto *C = dyn_cast<ConstantFP>(V))
9106 return !C->isZero();
9107
9108 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
9109 if (!C->getElementType()->isFloatingPointTy())
9110 return false;
9111 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
9112 if (C->getElementAsAPFloat(I).isZero())
9113 return false;
9114 }
9115 return true;
9116 }
9117
9118 return false;
9119}
9120
9121/// Match clamp pattern for float types without care about NaNs or signed zeros.
9122/// Given non-min/max outer cmp/select from the clamp pattern this
9123/// function recognizes if it can be substitued by a "canonical" min/max
9124/// pattern.
9126 Value *CmpLHS, Value *CmpRHS,
9127 Value *TrueVal, Value *FalseVal,
9128 Value *&LHS, Value *&RHS) {
9129 // Try to match
9130 // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2))
9131 // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2))
9132 // and return description of the outer Max/Min.
9133
9134 // First, check if select has inverse order:
9135 if (CmpRHS == FalseVal) {
9136 std::swap(TrueVal, FalseVal);
9138 }
9139
9140 // Assume success now. If there's no match, callers should not use these anyway.
9141 LHS = TrueVal;
9142 RHS = FalseVal;
9143
9144 const APFloat *FC1;
9145 if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite())
9146 return {SPF_UNKNOWN, SPNB_NA, false};
9147
9148 const APFloat *FC2;
9149 switch (Pred) {
9150 case CmpInst::FCMP_OLT:
9151 case CmpInst::FCMP_OLE:
9152 case CmpInst::FCMP_ULT:
9153 case CmpInst::FCMP_ULE:
9154 if (match(FalseVal, m_OrdOrUnordFMin(m_Specific(CmpLHS), m_APFloat(FC2))) &&
9155 *FC1 < *FC2)
9156 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
9157 if (match(FalseVal, m_FMinNum(m_Specific(CmpLHS), m_APFloat(FC2))) &&
9158 *FC1 < *FC2)
9159 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
9160 break;
9161 case CmpInst::FCMP_OGT:
9162 case CmpInst::FCMP_OGE:
9163 case CmpInst::FCMP_UGT:
9164 case CmpInst::FCMP_UGE:
9165 if (match(FalseVal, m_OrdOrUnordFMax(m_Specific(CmpLHS), m_APFloat(FC2))) &&
9166 *FC1 > *FC2)
9167 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
9168 if (match(FalseVal, m_FMaxNum(m_Specific(CmpLHS), m_APFloat(FC2))) &&
9169 *FC1 > *FC2)
9170 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
9171 break;
9172 default:
9173 break;
9174 }
9175
9176 return {SPF_UNKNOWN, SPNB_NA, false};
9177}
9178
9179/// Recognize variations of:
9180/// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
9182 Value *CmpLHS, Value *CmpRHS,
9183 Value *TrueVal, Value *FalseVal) {
9184 // Swap the select operands and predicate to match the patterns below.
9185 if (CmpRHS != TrueVal) {
9187 std::swap(TrueVal, FalseVal);
9188 }
9189 const APInt *C1;
9190 if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) {
9191 const APInt *C2;
9192 // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
9193 if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) &&
9194 C1->slt(*C2) && Pred == CmpInst::ICMP_SLT)
9195 return {SPF_SMAX, SPNB_NA, false};
9196
9197 // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
9198 if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) &&
9199 C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT)
9200 return {SPF_SMIN, SPNB_NA, false};
9201
9202 // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
9203 if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) &&
9204 C1->ult(*C2) && Pred == CmpInst::ICMP_ULT)
9205 return {SPF_UMAX, SPNB_NA, false};
9206
9207 // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
9208 if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) &&
9209 C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT)
9210 return {SPF_UMIN, SPNB_NA, false};
9211 }
9212 return {SPF_UNKNOWN, SPNB_NA, false};
9213}
9214
9215/// If the input value is the result of a 'not' op, constant integer, or vector
9216/// splat of a constant integer, return the bitwise-not source value.
9217/// TODO: This could be extended to handle non-splat vector integer constants.
9219 Value *NotV;
9220 if (match(V, m_Not(m_Value(NotV))))
9221 return NotV;
9222
9223 const APInt *C;
9224 if (match(V, m_APInt(C)))
9225 return ConstantInt::get(V->getType(), ~(*C));
9226
9227 return nullptr;
9228}
9229
9230/// Match non-obvious integer minimum and maximum sequences.
9232 Value *CmpRHS, Value *TrueVal,
9233 Value *FalseVal, Value *&LHS,
9234 Value *&RHS) {
9235 // Assume success. If there's no match, callers should not use these anyway.
9236 LHS = TrueVal;
9237 RHS = FalseVal;
9238
9239 SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);
9241 return SPR;
9242
9243 // Look through 'not' ops to find disguised min/max.
9244 // (X > Y) ? ~X : ~Y ==> (~X < ~Y) ? ~X : ~Y ==> MIN(~X, ~Y)
9245 // (X < Y) ? ~X : ~Y ==> (~X > ~Y) ? ~X : ~Y ==> MAX(~X, ~Y)
9246 if (CmpLHS == getNotValue(TrueVal) && CmpRHS == getNotValue(FalseVal)) {
9247 switch (Pred) {
9248 case CmpInst::ICMP_SGT: return {SPF_SMIN, SPNB_NA, false};
9249 case CmpInst::ICMP_SLT: return {SPF_SMAX, SPNB_NA, false};
9250 case CmpInst::ICMP_UGT: return {SPF_UMIN, SPNB_NA, false};
9251 case CmpInst::ICMP_ULT: return {SPF_UMAX, SPNB_NA, false};
9252 default: break;
9253 }
9254 }
9255
9256 // (X > Y) ? ~Y : ~X ==> (~X < ~Y) ? ~Y : ~X ==> MAX(~Y, ~X)
9257 // (X < Y) ? ~Y : ~X ==> (~X > ~Y) ? ~Y : ~X ==> MIN(~Y, ~X)
9258 if (CmpLHS == getNotValue(FalseVal) && CmpRHS == getNotValue(TrueVal)) {
9259 switch (Pred) {
9260 case CmpInst::ICMP_SGT: return {SPF_SMAX, SPNB_NA, false};
9261 case CmpInst::ICMP_SLT: return {SPF_SMIN, SPNB_NA, false};
9262 case CmpInst::ICMP_UGT: return {SPF_UMAX, SPNB_NA, false};
9263 case CmpInst::ICMP_ULT: return {SPF_UMIN, SPNB_NA, false};
9264 default: break;
9265 }
9266 }
9267
9269 return {SPF_UNKNOWN, SPNB_NA, false};
9270
9271 const APInt *C1;
9272 if (!match(CmpRHS, m_APInt(C1)))
9273 return {SPF_UNKNOWN, SPNB_NA, false};
9274
9275 // An unsigned min/max can be written with a signed compare.
9276 const APInt *C2;
9277 if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) ||
9278 (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) {
9279 // Is the sign bit set?
9280 // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX
9281 // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN
9282 if (Pred == CmpInst::ICMP_SLT && C1->isZero() && C2->isMaxSignedValue())
9283 return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
9284
9285 // Is the sign bit clear?
9286 // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX
9287 // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN
9288 if (Pred == CmpInst::ICMP_SGT && C1->isAllOnes() && C2->isMinSignedValue())
9289 return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
9290 }
9291
9292 return {SPF_UNKNOWN, SPNB_NA, false};
9293}
9294
9295bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW,
9296 bool AllowPoison) {
9297 assert(X && Y && "Invalid operand");
9298
9299 auto IsNegationOf = [&](const Value *X, const Value *Y) {
9300 if (!match(X, m_Neg(m_Specific(Y))))
9301 return false;
9302
9303 auto *BO = cast<BinaryOperator>(X);
9304 if (NeedNSW && !BO->hasNoSignedWrap())
9305 return false;
9306
9307 auto *Zero = cast<Constant>(BO->getOperand(0));
9308 if (!AllowPoison && !Zero->isNullValue())
9309 return false;
9310
9311 return true;
9312 };
9313
9314 // X = -Y or Y = -X
9315 if (IsNegationOf(X, Y) || IsNegationOf(Y, X))
9316 return true;
9317
9318 // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A)
9319 Value *A, *B;
9320 return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) &&
9321 match(Y, m_Sub(m_Specific(B), m_Specific(A))))) ||
9322 (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) &&
9324}
9325
9326bool llvm::isKnownInversion(const Value *X, const Value *Y) {
9327 // Handle X = icmp pred A, B, Y = icmp pred A, C.
9328 Value *A, *B, *C;
9329 CmpPredicate Pred1, Pred2;
9330 if (!match(X, m_ICmp(Pred1, m_Value(A), m_Value(B))) ||
9331 !match(Y, m_c_ICmp(Pred2, m_Specific(A), m_Value(C))))
9332 return false;
9333
9334 // They must both have samesign flag or not.
9335 if (Pred1.hasSameSign() != Pred2.hasSameSign())
9336 return false;
9337
9338 if (B == C)
9339 return Pred1 == ICmpInst::getInversePredicate(Pred2);
9340
9341 // Try to infer the relationship from constant ranges.
9342 const APInt *RHSC1, *RHSC2;
9343 if (!match(B, m_APInt(RHSC1)) || !match(C, m_APInt(RHSC2)))
9344 return false;
9345
9346 // Sign bits of two RHSCs should match.
9347 if (Pred1.hasSameSign() && RHSC1->isNonNegative() != RHSC2->isNonNegative())
9348 return false;
9349
9350 const auto CR1 = ConstantRange::makeExactICmpRegion(Pred1, *RHSC1);
9351 const auto CR2 = ConstantRange::makeExactICmpRegion(Pred2, *RHSC2);
9352
9353 return CR1.inverse() == CR2;
9354}
9355
9357 SelectPatternNaNBehavior NaNBehavior,
9358 bool Ordered) {
9359 switch (Pred) {
9360 default:
9361 return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.
9362 case ICmpInst::ICMP_UGT:
9363 case ICmpInst::ICMP_UGE:
9364 return {SPF_UMAX, SPNB_NA, false};
9365 case ICmpInst::ICMP_SGT:
9366 case ICmpInst::ICMP_SGE:
9367 return {SPF_SMAX, SPNB_NA, false};
9368 case ICmpInst::ICMP_ULT:
9369 case ICmpInst::ICMP_ULE:
9370 return {SPF_UMIN, SPNB_NA, false};
9371 case ICmpInst::ICMP_SLT:
9372 case ICmpInst::ICMP_SLE:
9373 return {SPF_SMIN, SPNB_NA, false};
9374 case FCmpInst::FCMP_UGT:
9375 case FCmpInst::FCMP_UGE:
9376 case FCmpInst::FCMP_OGT:
9377 case FCmpInst::FCMP_OGE:
9378 return {SPF_FMAXNUM, NaNBehavior, Ordered};
9379 case FCmpInst::FCMP_ULT:
9380 case FCmpInst::FCMP_ULE:
9381 case FCmpInst::FCMP_OLT:
9382 case FCmpInst::FCMP_OLE:
9383 return {SPF_FMINNUM, NaNBehavior, Ordered};
9384 }
9385}
9386
9387std::optional<std::pair<CmpPredicate, Constant *>>
9390 "Only for relational integer predicates.");
9391 if (isa<UndefValue>(C))
9392 return std::nullopt;
9393
9394 Type *Type = C->getType();
9395 bool IsSigned = ICmpInst::isSigned(Pred);
9396
9398 bool WillIncrement =
9399 UnsignedPred == ICmpInst::ICMP_ULE || UnsignedPred == ICmpInst::ICMP_UGT;
9400
9401 // Check if the constant operand can be safely incremented/decremented
9402 // without overflowing/underflowing.
9403 auto ConstantIsOk = [Pred, WillIncrement, IsSigned](ConstantInt *C) {
9404 if (WillIncrement ? C->isMaxValue(IsSigned) : C->isMinValue(IsSigned))
9405 return false;
9406
9407 if (!Pred.hasSameSign())
9408 return true;
9409
9410 // Crossing the corresponding boundary in the other ordering changes the
9411 // sign bit, and therefore changes the poison domain.
9412 return WillIncrement ? !C->isMaxValue(!IsSigned)
9413 : !C->isMinValue(!IsSigned);
9414 };
9415
9416 Constant *SafeReplacementConstant = nullptr;
9417 if (auto *CI = dyn_cast<ConstantInt>(C)) {
9418 // Bail out if the constant can't be safely incremented/decremented.
9419 if (!ConstantIsOk(CI))
9420 return std::nullopt;
9421 } else if (auto *FVTy = dyn_cast<FixedVectorType>(Type)) {
9422 unsigned NumElts = FVTy->getNumElements();
9423 for (unsigned i = 0; i != NumElts; ++i) {
9424 Constant *Elt = C->getAggregateElement(i);
9425 if (!Elt)
9426 return std::nullopt;
9427
9428 if (isa<UndefValue>(Elt))
9429 continue;
9430
9431 // Bail out if we can't determine if this constant is min/max or if we
9432 // know that this constant is min/max.
9433 auto *CI = dyn_cast<ConstantInt>(Elt);
9434 if (!CI || !ConstantIsOk(CI))
9435 return std::nullopt;
9436
9437 if (!SafeReplacementConstant)
9438 SafeReplacementConstant = CI;
9439 }
9440 } else if (isa<VectorType>(C->getType())) {
9441 // Handle scalable splat
9442 Value *SplatC = C->getSplatValue();
9443 auto *CI = dyn_cast_or_null<ConstantInt>(SplatC);
9444 // Bail out if the constant can't be safely incremented/decremented.
9445 if (!CI || !ConstantIsOk(CI))
9446 return std::nullopt;
9447 } else {
9448 // ConstantExpr?
9449 return std::nullopt;
9450 }
9451
9452 // It may not be safe to change a compare predicate in the presence of
9453 // undefined elements, so replace those elements with the first safe constant
9454 // that we found.
9455 // TODO: in case of poison, it is safe; let's replace undefs only.
9456 if (C->containsUndefOrPoisonElement()) {
9457 assert(SafeReplacementConstant && "Replacement constant not set");
9458 C = Constant::replaceUndefsWith(C, SafeReplacementConstant);
9459 }
9460
9462 Pred.hasSameSign());
9463
9464 // Increment or decrement the constant.
9465 Constant *OneOrNegOne = ConstantInt::get(Type, WillIncrement ? 1 : -1, true);
9466 Constant *NewC = ConstantExpr::getAdd(C, OneOrNegOne);
9467
9468 return std::make_pair(NewPred, NewC);
9469}
9470
9472 FastMathFlags FMF, Value *CmpLHS,
9473 Value *CmpRHS, Value *TrueVal,
9474 Value *FalseVal, Value *&LHS,
9475 Value *&RHS) {
9477 // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one
9478 // 0.0 operand, set the compare's 0.0 operands to that same value for the
9479 // purpose of identifying min/max. Disregard vector constants with undefined
9480 // elements because those can not be back-propagated for analysis.
9481 Value *OutputZeroVal = nullptr;
9482 if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) &&
9483 !cast<Constant>(TrueVal)->containsUndefOrPoisonElement())
9484 OutputZeroVal = TrueVal;
9485 else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) &&
9486 !cast<Constant>(FalseVal)->containsUndefOrPoisonElement())
9487 OutputZeroVal = FalseVal;
9488
9489 if (OutputZeroVal) {
9490 if (match(CmpLHS, m_AnyZeroFP()) && CmpLHS != OutputZeroVal)
9491 CmpLHS = OutputZeroVal;
9492 if (match(CmpRHS, m_AnyZeroFP()) && CmpRHS != OutputZeroVal)
9493 CmpRHS = OutputZeroVal;
9494 }
9495 }
9496
9497 LHS = CmpLHS;
9498 RHS = CmpRHS;
9499
9500 // Signed zero may return inconsistent results between implementations.
9501 // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0
9502 // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)
9503 // Therefore, we behave conservatively and only proceed if at least one of the
9504 // operands is known to not be zero or if we don't care about signed zero.
9506 if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
9507 !isKnownNonZero(CmpRHS))
9508 return {SPF_UNKNOWN, SPNB_NA, false};
9509 }
9510
9511 SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
9512 bool Ordered = false;
9513
9514 // When given one NaN and one non-NaN input:
9515 // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
9516 // - A simple C99 (a < b ? a : b) construction will return 'b' (as the
9517 // ordered comparison fails), which could be NaN or non-NaN.
9518 // so here we discover exactly what NaN behavior is required/accepted.
9520 bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
9521 bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
9522
9523 if (LHSSafe && RHSSafe) {
9524 // Both operands are known non-NaN.
9525 NaNBehavior = SPNB_RETURNS_ANY;
9526 Ordered = CmpInst::isOrdered(Pred);
9527 } else if (CmpInst::isOrdered(Pred)) {
9528 // An ordered comparison will return false when given a NaN, so it
9529 // returns the RHS.
9530 Ordered = true;
9531 if (LHSSafe)
9532 // LHS is non-NaN, so if RHS is NaN then NaN will be returned.
9533 NaNBehavior = SPNB_RETURNS_NAN;
9534 else if (RHSSafe)
9535 NaNBehavior = SPNB_RETURNS_OTHER;
9536 else
9537 // Completely unsafe.
9538 return {SPF_UNKNOWN, SPNB_NA, false};
9539 } else {
9540 Ordered = false;
9541 // An unordered comparison will return true when given a NaN, so it
9542 // returns the LHS.
9543 if (LHSSafe)
9544 // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
9545 NaNBehavior = SPNB_RETURNS_OTHER;
9546 else if (RHSSafe)
9547 NaNBehavior = SPNB_RETURNS_NAN;
9548 else
9549 // Completely unsafe.
9550 return {SPF_UNKNOWN, SPNB_NA, false};
9551 }
9552 }
9553
9554 if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
9555 std::swap(CmpLHS, CmpRHS);
9557 if (NaNBehavior == SPNB_RETURNS_NAN)
9558 NaNBehavior = SPNB_RETURNS_OTHER;
9559 else if (NaNBehavior == SPNB_RETURNS_OTHER)
9560 NaNBehavior = SPNB_RETURNS_NAN;
9561 Ordered = !Ordered;
9562 }
9563
9564 // ([if]cmp X, Y) ? X : Y
9565 if (TrueVal == CmpLHS && FalseVal == CmpRHS)
9566 return getSelectPattern(Pred, NaNBehavior, Ordered);
9567
9568 if (isKnownNegation(TrueVal, FalseVal)) {
9569 // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can
9570 // match against either LHS or sign-preserving operations on LHS, like
9571 // sext(LHS), or binary ops that do not wrap in signed sense.
9572 auto CmpLHSOrSExt =
9573 m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS)));
9574 auto MaybeSExtOrMulCmpLHS =
9575 m_CombineOr(CmpLHSOrSExt, m_NSWMul(CmpLHSOrSExt, m_StrictlyPositive()),
9576 m_NSWShl(CmpLHSOrSExt, m_Value()));
9577 auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes());
9578 auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One());
9579 if (match(TrueVal, MaybeSExtOrMulCmpLHS)) {
9580 // Set the return values. If the compare uses the negated value (-X >s 0),
9581 // swap the return values because the negated value is always 'RHS'.
9582 LHS = TrueVal;
9583 RHS = FalseVal;
9584 if (match(CmpLHS, m_Neg(m_Specific(FalseVal))))
9585 std::swap(LHS, RHS);
9586
9587 // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X)
9588 // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X)
9589 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
9590 return {SPF_ABS, SPNB_NA, false};
9591
9592 // (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X)
9593 if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne))
9594 return {SPF_ABS, SPNB_NA, false};
9595
9596 // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X)
9597 // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X)
9598 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
9599 return {SPF_NABS, SPNB_NA, false};
9600 } else if (match(FalseVal, MaybeSExtOrMulCmpLHS)) {
9601 // Set the return values. If the compare uses the negated value (-X >s 0),
9602 // swap the return values because the negated value is always 'RHS'.
9603 LHS = FalseVal;
9604 RHS = TrueVal;
9605 if (match(CmpLHS, m_Neg(m_Specific(TrueVal))))
9606 std::swap(LHS, RHS);
9607
9608 // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X)
9609 // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X)
9610 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
9611 return {SPF_NABS, SPNB_NA, false};
9612
9613 // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X)
9614 // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X)
9615 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
9616 return {SPF_ABS, SPNB_NA, false};
9617 }
9618 }
9619
9621 return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
9622
9623 // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar
9624 // may return either -0.0 or 0.0, so fcmp/select pair has stricter
9625 // semantics than minNum. Be conservative in such case.
9626 if (NaNBehavior != SPNB_RETURNS_ANY ||
9627 (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
9628 !isKnownNonZero(CmpRHS)))
9629 return {SPF_UNKNOWN, SPNB_NA, false};
9630
9631 return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
9632}
9633
9635 Instruction::CastOps *CastOp) {
9636 const DataLayout &DL = CmpI->getDataLayout();
9637
9638 Constant *CastedTo = nullptr;
9639 switch (*CastOp) {
9640 case Instruction::ZExt:
9641 if (CmpI->isUnsigned())
9642 CastedTo = ConstantExpr::getTrunc(C, SrcTy);
9643 break;
9644 case Instruction::SExt:
9645 if (CmpI->isSigned())
9646 CastedTo = ConstantExpr::getTrunc(C, SrcTy, true);
9647 break;
9648 case Instruction::Trunc:
9649 Constant *CmpConst;
9650 if (match(CmpI->getOperand(1), m_Constant(CmpConst)) &&
9651 CmpConst->getType() == SrcTy) {
9652 // Here we have the following case:
9653 //
9654 // %cond = cmp iN %x, CmpConst
9655 // %tr = trunc iN %x to iK
9656 // %narrowsel = select i1 %cond, iK %t, iK C
9657 //
9658 // We can always move trunc after select operation:
9659 //
9660 // %cond = cmp iN %x, CmpConst
9661 // %widesel = select i1 %cond, iN %x, iN CmpConst
9662 // %tr = trunc iN %widesel to iK
9663 //
9664 // Note that C could be extended in any way because we don't care about
9665 // upper bits after truncation. It can't be abs pattern, because it would
9666 // look like:
9667 //
9668 // select i1 %cond, x, -x.
9669 //
9670 // So only min/max pattern could be matched. Such match requires widened C
9671 // == CmpConst. That is why set widened C = CmpConst, condition trunc
9672 // CmpConst == C is checked below.
9673 CastedTo = CmpConst;
9674 } else {
9675 unsigned ExtOp = CmpI->isSigned() ? Instruction::SExt : Instruction::ZExt;
9676 CastedTo = ConstantFoldCastOperand(ExtOp, C, SrcTy, DL);
9677 }
9678 break;
9679 case Instruction::FPTrunc:
9680 CastedTo = ConstantFoldCastOperand(Instruction::FPExt, C, SrcTy, DL);
9681 break;
9682 case Instruction::FPExt:
9683 CastedTo = ConstantFoldCastOperand(Instruction::FPTrunc, C, SrcTy, DL);
9684 break;
9685 case Instruction::FPToUI:
9686 CastedTo = ConstantFoldCastOperand(Instruction::UIToFP, C, SrcTy, DL);
9687 break;
9688 case Instruction::FPToSI:
9689 CastedTo = ConstantFoldCastOperand(Instruction::SIToFP, C, SrcTy, DL);
9690 break;
9691 case Instruction::UIToFP:
9692 CastedTo = ConstantFoldCastOperand(Instruction::FPToUI, C, SrcTy, DL);
9693 break;
9694 case Instruction::SIToFP:
9695 CastedTo = ConstantFoldCastOperand(Instruction::FPToSI, C, SrcTy, DL);
9696 break;
9697 default:
9698 break;
9699 }
9700
9701 if (!CastedTo)
9702 return nullptr;
9703
9704 // Make sure the cast doesn't lose any information.
9705 Constant *CastedBack =
9706 ConstantFoldCastOperand(*CastOp, CastedTo, C->getType(), DL);
9707 if (CastedBack && CastedBack != C)
9708 return nullptr;
9709
9710 return CastedTo;
9711}
9712
9713/// Helps to match a select pattern in case of a type mismatch.
9714///
9715/// The function processes the case when type of true and false values of a
9716/// select instruction differs from type of the cmp instruction operands because
9717/// of a cast instruction. The function checks if it is legal to move the cast
9718/// operation after "select". If yes, it returns the new second value of
9719/// "select" (with the assumption that cast is moved):
9720/// 1. As operand of cast instruction when both values of "select" are same cast
9721/// instructions.
9722/// 2. As restored constant (by applying reverse cast operation) when the first
9723/// value of the "select" is a cast operation and the second value is a
9724/// constant. It is implemented in lookThroughCastConst().
9725/// 3. As one operand is cast instruction and the other is not. The operands in
9726/// sel(cmp) are in different type integer.
9727/// NOTE: We return only the new second value because the first value could be
9728/// accessed as operand of cast instruction.
9730 Instruction::CastOps *CastOp) {
9731 auto *Cast1 = dyn_cast<CastInst>(V1);
9732 if (!Cast1)
9733 return nullptr;
9734
9735 *CastOp = Cast1->getOpcode();
9736 Type *SrcTy = Cast1->getSrcTy();
9737 if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
9738 // If V1 and V2 are both the same cast from the same type, look through V1.
9739 if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
9740 return Cast2->getOperand(0);
9741 return nullptr;
9742 }
9743
9744 auto *C = dyn_cast<Constant>(V2);
9745 if (C)
9746 return lookThroughCastConst(CmpI, SrcTy, C, CastOp);
9747
9748 Value *CastedTo = nullptr;
9749 if (*CastOp == Instruction::Trunc) {
9750 if (match(CmpI->getOperand(1), m_ZExtOrSExt(m_Specific(V2)))) {
9751 // Here we have the following case:
9752 // %y_ext = sext iK %y to iN
9753 // %cond = cmp iN %x, %y_ext
9754 // %tr = trunc iN %x to iK
9755 // %narrowsel = select i1 %cond, iK %tr, iK %y
9756 //
9757 // We can always move trunc after select operation:
9758 // %y_ext = sext iK %y to iN
9759 // %cond = cmp iN %x, %y_ext
9760 // %widesel = select i1 %cond, iN %x, iN %y_ext
9761 // %tr = trunc iN %widesel to iK
9762 assert(V2->getType() == Cast1->getType() &&
9763 "V2 and Cast1 should be the same type.");
9764 CastedTo = CmpI->getOperand(1);
9765 }
9766 }
9767
9768 return CastedTo;
9769}
9771 Instruction::CastOps *CastOp) {
9773 if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
9774
9775 CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
9776 if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};
9777
9778 Value *TrueVal = SI->getTrueValue();
9779 Value *FalseVal = SI->getFalseValue();
9780
9782 CmpI, TrueVal, FalseVal, LHS, RHS, SI->getFastMathFlagsOrNone(), CastOp);
9783}
9784
9786 CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS,
9787 FastMathFlags FMF, Instruction::CastOps *CastOp) {
9788 CmpInst::Predicate Pred = CmpI->getPredicate();
9789 Value *CmpLHS = CmpI->getOperand(0);
9790 Value *CmpRHS = CmpI->getOperand(1);
9791 if (isa<FPMathOperator>(CmpI) && CmpI->hasNoNaNs())
9792 FMF.setNoNaNs();
9793
9794 // Bail out early.
9795 if (CmpI->isEquality())
9796 return {SPF_UNKNOWN, SPNB_NA, false};
9797
9798 // Deal with type mismatches.
9799 if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
9800 if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) {
9801 // If this is a potential fmin/fmax with a cast to integer, then ignore
9802 // -0.0 because there is no corresponding integer value.
9803 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9804 FMF.setNoSignedZeros();
9805 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9806 cast<CastInst>(TrueVal)->getOperand(0), C,
9807 LHS, RHS);
9808 }
9809 if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) {
9810 // If this is a potential fmin/fmax with a cast to integer, then ignore
9811 // -0.0 because there is no corresponding integer value.
9812 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9813 FMF.setNoSignedZeros();
9814 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, C,
9815 cast<CastInst>(FalseVal)->getOperand(0), LHS,
9816 RHS);
9817 }
9818 }
9819 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS,
9820 RHS);
9821}
9822
9824 if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT;
9825 if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT;
9826 if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT;
9827 if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT;
9828 if (SPF == SPF_FMINNUM)
9829 return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT;
9830 if (SPF == SPF_FMAXNUM)
9831 return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT;
9832 llvm_unreachable("unhandled!");
9833}
9834
9836 switch (SPF) {
9838 return Intrinsic::umin;
9840 return Intrinsic::umax;
9842 return Intrinsic::smin;
9844 return Intrinsic::smax;
9845 default:
9846 llvm_unreachable("Unexpected SPF");
9847 }
9848}
9849
9851 if (SPF == SPF_SMIN) return SPF_SMAX;
9852 if (SPF == SPF_UMIN) return SPF_UMAX;
9853 if (SPF == SPF_SMAX) return SPF_SMIN;
9854 if (SPF == SPF_UMAX) return SPF_UMIN;
9855 llvm_unreachable("unhandled!");
9856}
9857
9859 switch (MinMaxID) {
9860 case Intrinsic::smax: return Intrinsic::smin;
9861 case Intrinsic::smin: return Intrinsic::smax;
9862 case Intrinsic::umax: return Intrinsic::umin;
9863 case Intrinsic::umin: return Intrinsic::umax;
9864 // Please note that next four intrinsics may produce the same result for
9865 // original and inverted case even if X != Y due to NaN is handled specially.
9866 case Intrinsic::maximum: return Intrinsic::minimum;
9867 case Intrinsic::minimum: return Intrinsic::maximum;
9868 case Intrinsic::maxnum: return Intrinsic::minnum;
9869 case Intrinsic::minnum: return Intrinsic::maxnum;
9870 case Intrinsic::maximumnum:
9871 return Intrinsic::minimumnum;
9872 case Intrinsic::minimumnum:
9873 return Intrinsic::maximumnum;
9874 default: llvm_unreachable("Unexpected intrinsic");
9875 }
9876}
9877
9879 switch (SPF) {
9882 case SPF_UMAX: return APInt::getMaxValue(BitWidth);
9883 case SPF_UMIN: return APInt::getMinValue(BitWidth);
9884 default: llvm_unreachable("Unexpected flavor");
9885 }
9886}
9887
9888std::pair<Intrinsic::ID, bool>
9890 // Check if VL contains select instructions that can be folded into a min/max
9891 // vector intrinsic and return the intrinsic if it is possible.
9892 // TODO: Support floating point min/max.
9893 bool AllCmpSingleUse = true;
9894 SelectPatternResult SelectPattern;
9895 SelectPattern.Flavor = SPF_UNKNOWN;
9896 if (all_of(VL, [&SelectPattern, &AllCmpSingleUse](Value *I) {
9897 Value *LHS, *RHS;
9898 auto CurrentPattern = matchSelectPattern(I, LHS, RHS);
9899 if (!SelectPatternResult::isMinOrMax(CurrentPattern.Flavor))
9900 return false;
9901 if (SelectPattern.Flavor != SPF_UNKNOWN &&
9902 SelectPattern.Flavor != CurrentPattern.Flavor)
9903 return false;
9904 SelectPattern = CurrentPattern;
9905 AllCmpSingleUse &=
9907 return true;
9908 })) {
9909 switch (SelectPattern.Flavor) {
9910 case SPF_SMIN:
9911 return {Intrinsic::smin, AllCmpSingleUse};
9912 case SPF_UMIN:
9913 return {Intrinsic::umin, AllCmpSingleUse};
9914 case SPF_SMAX:
9915 return {Intrinsic::smax, AllCmpSingleUse};
9916 case SPF_UMAX:
9917 return {Intrinsic::umax, AllCmpSingleUse};
9918 case SPF_FMAXNUM:
9919 return {Intrinsic::maxnum, AllCmpSingleUse};
9920 case SPF_FMINNUM:
9921 return {Intrinsic::minnum, AllCmpSingleUse};
9922 default:
9923 llvm_unreachable("unexpected select pattern flavor");
9924 }
9925 }
9926 return {Intrinsic::not_intrinsic, false};
9927}
9928
9929template <typename InstTy>
9930static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst,
9931 Value *&Init, Value *&OtherOp) {
9932 // Handle the case of a simple two-predecessor recurrence PHI.
9933 // There's a lot more that could theoretically be done here, but
9934 // this is sufficient to catch some interesting cases.
9935 // TODO: Expand list -- gep, uadd.sat etc.
9936 if (PN->getNumIncomingValues() != 2)
9937 return false;
9938
9939 for (unsigned I = 0; I != 2; ++I) {
9940 if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
9941 Operation && Operation->getNumOperands() >= 2) {
9942 Value *LHS = Operation->getOperand(0);
9943 Value *RHS = Operation->getOperand(1);
9944 if (LHS != PN && RHS != PN)
9945 continue;
9946
9947 Inst = Operation;
9948 Init = PN->getIncomingValue(!I);
9949 OtherOp = (LHS == PN) ? RHS : LHS;
9950 return true;
9951 }
9952 }
9953 return false;
9954}
9955
9956template <typename InstTy>
9957static bool matchThreeInputRecurrence(const PHINode *PN, InstTy *&Inst,
9958 Value *&Init, Value *&OtherOp0,
9959 Value *&OtherOp1) {
9960 if (PN->getNumIncomingValues() != 2)
9961 return false;
9962
9963 for (unsigned I = 0; I != 2; ++I) {
9964 if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
9965 Operation && Operation->getNumOperands() >= 3) {
9966 Value *Op0 = Operation->getOperand(0);
9967 Value *Op1 = Operation->getOperand(1);
9968 Value *Op2 = Operation->getOperand(2);
9969
9970 if (Op0 != PN && Op1 != PN && Op2 != PN)
9971 continue;
9972
9973 Inst = Operation;
9974 Init = PN->getIncomingValue(!I);
9975 if (Op0 == PN) {
9976 OtherOp0 = Op1;
9977 OtherOp1 = Op2;
9978 } else if (Op1 == PN) {
9979 OtherOp0 = Op0;
9980 OtherOp1 = Op2;
9981 } else {
9982 OtherOp0 = Op0;
9983 OtherOp1 = Op1;
9984 }
9985 return true;
9986 }
9987 }
9988 return false;
9989}
9991 Value *&Start, Value *&Step) {
9992 // We try to match a recurrence of the form:
9993 // %iv = [Start, %entry], [%iv.next, %backedge]
9994 // %iv.next = binop %iv, Step
9995 // Or:
9996 // %iv = [Start, %entry], [%iv.next, %backedge]
9997 // %iv.next = binop Step, %iv
9998 return matchTwoInputRecurrence(P, BO, Start, Step);
9999}
10000
10002 Value *&Start, Value *&Step) {
10003 BinaryOperator *BO = nullptr;
10004 return match(I, m_c_BinOp(m_Phi(P), m_Value())) &&
10005 matchSimpleRecurrence(P, BO, Start, Step) && BO == I;
10006}
10007
10009 PHINode *&P, Value *&Init,
10010 Value *&OtherOp) {
10011 // Binary intrinsics only supported for now.
10012 if (I->arg_size() != 2 || I->getType() != I->getArgOperand(0)->getType() ||
10013 I->getType() != I->getArgOperand(1)->getType())
10014 return false;
10015
10016 IntrinsicInst *II = nullptr;
10017 P = dyn_cast<PHINode>(I->getArgOperand(0));
10018 if (!P)
10019 P = dyn_cast<PHINode>(I->getArgOperand(1));
10020
10021 return P && matchTwoInputRecurrence(P, II, Init, OtherOp) && II == I;
10022}
10023
10025 PHINode *&P, Value *&Init,
10026 Value *&OtherOp0,
10027 Value *&OtherOp1) {
10028 if (I->arg_size() != 3 || I->getType() != I->getArgOperand(0)->getType() ||
10029 I->getType() != I->getArgOperand(1)->getType() ||
10030 I->getType() != I->getArgOperand(2)->getType())
10031 return false;
10032 IntrinsicInst *II = nullptr;
10033 P = dyn_cast<PHINode>(I->getArgOperand(0));
10034 if (!P) {
10035 P = dyn_cast<PHINode>(I->getArgOperand(1));
10036 if (!P)
10037 P = dyn_cast<PHINode>(I->getArgOperand(2));
10038 }
10039 return P && matchThreeInputRecurrence(P, II, Init, OtherOp0, OtherOp1) &&
10040 II == I;
10041}
10042
10043/// Return true if "icmp Pred LHS RHS" is always true.
10045 const Value *RHS) {
10047 return true;
10048
10049 switch (Pred) {
10050 default:
10051 return false;
10052
10053 case CmpInst::ICMP_SLE: {
10054 const APInt *C;
10055
10056 // LHS s<= LHS +_{nsw} C if C >= 0
10057 // LHS s<= LHS | C if C >= 0
10058 if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C))) ||
10060 return !C->isNegative();
10061
10062 // LHS s<= smax(LHS, V) for any V
10064 return true;
10065
10066 // smin(RHS, V) s<= RHS for any V
10068 return true;
10069
10070 // Match A to (X +_{nsw} CA) and B to (X +_{nsw} CB)
10071 const Value *X;
10072 const APInt *CLHS, *CRHS;
10073 if (match(LHS, m_NSWAddLike(m_Value(X), m_APInt(CLHS))) &&
10075 return CLHS->sle(*CRHS);
10076
10077 return false;
10078 }
10079
10080 case CmpInst::ICMP_ULE: {
10081 // LHS u<= LHS +_{nuw} V for any V
10082 if (match(RHS, m_c_Add(m_Specific(LHS), m_Value())) &&
10084 return true;
10085
10086 // LHS u<= LHS | V for any V
10087 if (match(RHS, m_c_Or(m_Specific(LHS), m_Value())))
10088 return true;
10089
10090 // LHS u<= umax(LHS, V) for any V
10092 return true;
10093
10094 // RHS >> V u<= RHS for any V
10095 if (match(LHS, m_LShr(m_Specific(RHS), m_Value())))
10096 return true;
10097
10098 // RHS u/ C_ugt_1 u<= RHS
10099 const APInt *C;
10100 if (match(LHS, m_UDiv(m_Specific(RHS), m_APInt(C))) && C->ugt(1))
10101 return true;
10102
10103 // RHS & V u<= RHS for any V
10105 return true;
10106
10107 // umin(RHS, V) u<= RHS for any V
10109 return true;
10110
10111 // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
10112 const Value *X;
10113 const APInt *CLHS, *CRHS;
10114 if (match(LHS, m_NUWAddLike(m_Value(X), m_APInt(CLHS))) &&
10116 return CLHS->ule(*CRHS);
10117
10118 return false;
10119 }
10120 }
10121}
10122
10123/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
10124/// ALHS ARHS" is true. Otherwise, return std::nullopt.
10125static std::optional<bool>
10127 const Value *ARHS, const Value *BLHS, const Value *BRHS) {
10128 switch (Pred) {
10129 default:
10130 return std::nullopt;
10131
10132 case CmpInst::ICMP_SLT:
10133 case CmpInst::ICMP_SLE:
10134 if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS) &&
10136 return true;
10137 return std::nullopt;
10138
10139 case CmpInst::ICMP_SGT:
10140 case CmpInst::ICMP_SGE:
10141 if (isTruePredicate(CmpInst::ICMP_SLE, ALHS, BLHS) &&
10143 return true;
10144 return std::nullopt;
10145
10146 case CmpInst::ICMP_ULT:
10147 case CmpInst::ICMP_ULE:
10148 if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS) &&
10150 return true;
10151 return std::nullopt;
10152
10153 case CmpInst::ICMP_UGT:
10154 case CmpInst::ICMP_UGE:
10155 if (isTruePredicate(CmpInst::ICMP_ULE, ALHS, BLHS) &&
10157 return true;
10158 return std::nullopt;
10159 }
10160}
10161
10162/// Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
10163/// Return false if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is false.
10164/// Otherwise, return std::nullopt if we can't infer anything.
10165static std::optional<bool>
10167 CmpPredicate RPred, const ConstantRange &RCR) {
10168 auto CRImpliesPred = [&](ConstantRange CR,
10169 CmpInst::Predicate Pred) -> std::optional<bool> {
10170 // If all true values for lhs and true for rhs, lhs implies rhs
10171 if (CR.icmp(Pred, RCR))
10172 return true;
10173
10174 // If there is no overlap, lhs implies not rhs
10176 return false;
10177
10178 return std::nullopt;
10179 };
10180 if (auto Res = CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
10181 RPred))
10182 return Res;
10183 if (LPred.hasSameSign() ^ RPred.hasSameSign()) {
10185 : LPred.dropSameSign();
10187 : RPred.dropSameSign();
10188 return CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
10189 RPred);
10190 }
10191 return std::nullopt;
10192}
10193
10194/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
10195/// is true. Return false if LHS implies RHS is false. Otherwise, return
10196/// std::nullopt if we can't infer anything.
10197static std::optional<bool>
10198isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1,
10199 CmpPredicate RPred, const Value *R0, const Value *R1,
10200 const DataLayout &DL, bool LHSIsTrue) {
10201 // The rest of the logic assumes the LHS condition is true. If that's not the
10202 // case, invert the predicate to make it so.
10203 if (!LHSIsTrue)
10204 LPred = ICmpInst::getInverseCmpPredicate(LPred);
10205
10206 // We can have non-canonical operands, so try to normalize any common operand
10207 // to L0/R0.
10208 if (L0 == R1) {
10209 std::swap(R0, R1);
10210 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
10211 }
10212 if (R0 == L1) {
10213 std::swap(L0, L1);
10214 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
10215 }
10216 if (L1 == R1) {
10217 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
10218 if (L0 != R0 || match(L0, m_ImmConstant())) {
10219 std::swap(L0, L1);
10220 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
10221 std::swap(R0, R1);
10222 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
10223 }
10224 }
10225
10226 // See if we can infer anything if operand-0 matches and we have at least one
10227 // constant.
10228 const APInt *Unused;
10229 if (L0 == R0 && (match(L1, m_APInt(Unused)) || match(R1, m_APInt(Unused)))) {
10230 // Potential TODO: We could also further use the constant range of L0/R0 to
10231 // further constraint the constant ranges. At the moment this leads to
10232 // several regressions related to not transforming `multi_use(A + C0) eq/ne
10233 // C1` (see discussion: D58633).
10234 SimplifyQuery SQ(DL);
10239
10240 // Even if L1/R1 are not both constant, we can still sometimes deduce
10241 // relationship from a single constant. For example X u> Y implies X != 0.
10242 if (auto R = isImpliedCondCommonOperandWithCR(LPred, LCR, RPred, RCR))
10243 return R;
10244 // If both L1/R1 were exact constant ranges and we didn't get anything
10245 // here, we won't be able to deduce this.
10246 if (match(L1, m_APInt(Unused)) && match(R1, m_APInt(Unused)))
10247 return std::nullopt;
10248 }
10249
10250 // Can we infer anything when the two compares have matching operands?
10251 if (L0 == R0 && L1 == R1)
10252 return ICmpInst::isImpliedByMatchingCmp(LPred, RPred);
10253
10254 // It only really makes sense in the context of signed comparison for "X - Y
10255 // must be positive if X >= Y and no overflow".
10256 // Take SGT as an example: L0:x > L1:y and C >= 0
10257 // ==> R0:(x -nsw y) < R1:(-C) is false
10258 CmpInst::Predicate SignedLPred = LPred.getPreferredSignedPredicate();
10259 if ((SignedLPred == ICmpInst::ICMP_SGT ||
10260 SignedLPred == ICmpInst::ICMP_SGE) &&
10261 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
10262 if (match(R1, m_NonPositive()) &&
10263 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == false)
10264 return false;
10265 }
10266
10267 // Take SLT as an example: L0:x < L1:y and C <= 0
10268 // ==> R0:(x -nsw y) < R1:(-C) is true
10269 if ((SignedLPred == ICmpInst::ICMP_SLT ||
10270 SignedLPred == ICmpInst::ICMP_SLE) &&
10271 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
10272 if (match(R1, m_NonNegative()) &&
10273 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == true)
10274 return true;
10275 }
10276
10277 // (a - b) pred C -> a != b, if 0 pred C is false
10278 // (ptrtoint(a) - ptrtoint(b)) pred C -> a != b, if 0 pred C is false
10279 Value *A, *B;
10280 if (ICmpInst::isEquality(RPred) && cmpExcludesZero(LPred, L1) &&
10281 match(L0, m_Sub(m_Value(A), m_Value(B))) &&
10282 ((A == R0 && B == R1) || (A == R1 && B == R0) ||
10287 return RPred.dropSameSign() == ICmpInst::ICMP_NE;
10288 }
10289
10290 // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1
10291 if (L0 == R0 &&
10292 (LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) &&
10293 (RPred == ICmpInst::ICMP_ULT || RPred == ICmpInst::ICMP_UGE) &&
10294 match(L0, m_c_Add(m_Specific(L1), m_Specific(R1))))
10295 return CmpPredicate::getMatching(LPred, RPred).has_value();
10296
10297 if (auto P = CmpPredicate::getMatching(LPred, RPred))
10298 return isImpliedCondOperands(*P, L0, L1, R0, R1);
10299
10300 // L0 u< C sets limits to L0's bits which may imply (L0 & Mask) pred RC
10301 // Example: L0 u< 13 => (L0 & 16) == 0
10302 const APInt *LC, *RC, *MaskC;
10303 if (match(L1, m_APInt(LC)) && match(R1, m_APInt(RC)) &&
10304 match(R0, m_And(m_Specific(L0), m_APInt(MaskC)))) {
10306 ConstantRange MaskedCRange = LCRange.binaryAnd(*MaskC);
10307 if (MaskedCRange.icmp(RPred, ConstantRange(*RC)))
10308 return true;
10309 if (MaskedCRange.icmp(ICmpInst::getInversePredicate(RPred),
10310 ConstantRange(*RC)))
10311 return false;
10312 }
10313
10314 return std::nullopt;
10315}
10316
10317/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
10318/// is true. Return false if LHS implies RHS is false. Otherwise, return
10319/// std::nullopt if we can't infer anything.
10320static std::optional<bool>
10322 FCmpInst::Predicate RPred, const Value *R0, const Value *R1,
10323 const DataLayout &DL, bool LHSIsTrue) {
10324 // The rest of the logic assumes the LHS condition is true. If that's not the
10325 // case, invert the predicate to make it so.
10326 if (!LHSIsTrue)
10327 LPred = FCmpInst::getInversePredicate(LPred);
10328
10329 // We can have non-canonical operands, so try to normalize any common operand
10330 // to L0/R0.
10331 if (L0 == R1) {
10332 std::swap(R0, R1);
10333 RPred = FCmpInst::getSwappedPredicate(RPred);
10334 }
10335 if (R0 == L1) {
10336 std::swap(L0, L1);
10337 LPred = FCmpInst::getSwappedPredicate(LPred);
10338 }
10339 if (L1 == R1) {
10340 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
10341 if (L0 != R0 || match(L0, m_ImmConstant())) {
10342 std::swap(L0, L1);
10343 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
10344 std::swap(R0, R1);
10345 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
10346 }
10347 }
10348
10349 // Can we infer anything when the two compares have matching operands?
10350 if (L0 == R0 && L1 == R1) {
10351 if ((LPred & RPred) == LPred)
10352 return true;
10353 if ((LPred & ~RPred) == LPred)
10354 return false;
10355 }
10356
10357 // See if we can infer anything if operand-0 matches and we have at least one
10358 // constant.
10359 const APFloat *L1C, *R1C;
10360 if (L0 == R0 && match(L1, m_APFloat(L1C)) && match(R1, m_APFloat(R1C))) {
10361 if (std::optional<ConstantFPRange> DomCR =
10363 if (std::optional<ConstantFPRange> ImpliedCR =
10365 if (ImpliedCR->contains(*DomCR))
10366 return true;
10367 }
10368 if (std::optional<ConstantFPRange> ImpliedCR =
10370 FCmpInst::getInversePredicate(RPred), *R1C)) {
10371 if (ImpliedCR->contains(*DomCR))
10372 return false;
10373 }
10374 }
10375 }
10376
10377 return std::nullopt;
10378}
10379
10380/// Return true if LHS implies RHS is true. Return false if LHS implies RHS is
10381/// false. Otherwise, return std::nullopt if we can't infer anything. We
10382/// expect the RHS to be an icmp and the LHS to be an 'and', 'or', or a 'select'
10383/// instruction.
10384static std::optional<bool>
10386 const Value *RHSOp0, const Value *RHSOp1,
10387 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
10388 // The LHS must be an 'or', 'and', or a 'select' instruction.
10389 assert((LHS->getOpcode() == Instruction::And ||
10390 LHS->getOpcode() == Instruction::Or ||
10391 LHS->getOpcode() == Instruction::Select) &&
10392 "Expected LHS to be 'and', 'or', or 'select'.");
10393
10394 assert(Depth <= MaxAnalysisRecursionDepth && "Hit recursion limit");
10395
10396 // If the result of an 'or' is false, then we know both legs of the 'or' are
10397 // false. Similarly, if the result of an 'and' is true, then we know both
10398 // legs of the 'and' are true.
10399 const Value *ALHS, *ARHS;
10400 if ((!LHSIsTrue && match(LHS, m_LogicalOr(m_Value(ALHS), m_Value(ARHS)))) ||
10401 (LHSIsTrue && match(LHS, m_LogicalAnd(m_Value(ALHS), m_Value(ARHS))))) {
10402 // FIXME: Make this non-recursion.
10403 if (std::optional<bool> Implication = isImpliedCondition(
10404 ALHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
10405 return Implication;
10406 if (std::optional<bool> Implication = isImpliedCondition(
10407 ARHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
10408 return Implication;
10409 return std::nullopt;
10410 }
10411 return std::nullopt;
10412}
10413
10414std::optional<bool>
10416 const Value *RHSOp0, const Value *RHSOp1,
10417 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
10418 // Bail out when we hit the limit.
10420 return std::nullopt;
10421
10422 // A mismatch occurs when we compare a scalar cmp to a vector cmp, for
10423 // example.
10424 if (RHSOp0->getType()->isVectorTy() != LHS->getType()->isVectorTy())
10425 return std::nullopt;
10426
10427 assert(LHS->getType()->isIntOrIntVectorTy(1) &&
10428 "Expected integer type only!");
10429
10430 // Match not
10431 if (match(LHS, m_Not(m_Value(LHS))))
10432 LHSIsTrue = !LHSIsTrue;
10433
10434 // Both LHS and RHS are icmps.
10435 if (RHSOp0->getType()->getScalarType()->isIntOrPtrTy()) {
10436 CmpPredicate LHSPred;
10437 Value *LHSOp0, *LHSOp1;
10438 if (match(LHS, m_ICmpLike(LHSPred, m_Value(LHSOp0), m_Value(LHSOp1))))
10439 return isImpliedCondICmps(LHSPred, LHSOp0, LHSOp1, RHSPred, RHSOp0,
10440 RHSOp1, DL, LHSIsTrue);
10441 } else {
10442 assert(RHSOp0->getType()->isFPOrFPVectorTy() &&
10443 "Expected floating point type only!");
10444 if (const auto *LHSCmp = dyn_cast<FCmpInst>(LHS))
10445 return isImpliedCondFCmps(LHSCmp->getPredicate(), LHSCmp->getOperand(0),
10446 LHSCmp->getOperand(1), RHSPred, RHSOp0, RHSOp1,
10447 DL, LHSIsTrue);
10448 }
10449
10450 /// The LHS should be an 'or', 'and', or a 'select' instruction. We expect
10451 /// the RHS to be an icmp.
10452 /// FIXME: Add support for and/or/select on the RHS.
10453 if (const Instruction *LHSI = dyn_cast<Instruction>(LHS)) {
10454 if ((LHSI->getOpcode() == Instruction::And ||
10455 LHSI->getOpcode() == Instruction::Or ||
10456 LHSI->getOpcode() == Instruction::Select))
10457 return isImpliedCondAndOr(LHSI, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue,
10458 Depth);
10459 }
10460 return std::nullopt;
10461}
10462
10463std::optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS,
10464 const DataLayout &DL,
10465 bool LHSIsTrue, unsigned Depth) {
10466 // LHS ==> RHS by definition
10467 if (LHS == RHS)
10468 return LHSIsTrue;
10469
10470 // Match not
10471 bool InvertRHS = false;
10472 if (match(RHS, m_Not(m_Value(RHS)))) {
10473 if (LHS == RHS)
10474 return !LHSIsTrue;
10475 InvertRHS = true;
10476 }
10477
10478 CmpPredicate RHSPred;
10479 Value *RHSOp0, *RHSOp1;
10480 if (match(RHS, m_ICmpLike(RHSPred, m_Value(RHSOp0), m_Value(RHSOp1)))) {
10481 if (auto Implied = isImpliedCondition(LHS, RHSPred, RHSOp0, RHSOp1, DL,
10482 LHSIsTrue, Depth))
10483 return InvertRHS ? !*Implied : *Implied;
10484 return std::nullopt;
10485 }
10486 if (const FCmpInst *RHSCmp = dyn_cast<FCmpInst>(RHS)) {
10487 if (auto Implied = isImpliedCondition(
10488 LHS, RHSCmp->getPredicate(), RHSCmp->getOperand(0),
10489 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))
10490 return InvertRHS ? !*Implied : *Implied;
10491 return std::nullopt;
10492 }
10493
10495 return std::nullopt;
10496
10497 // LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS2
10498 // LHS ==> !(RHS1 && RHS2) if LHS ==> !RHS1 or LHS ==> !RHS2
10499 const Value *RHS1, *RHS2;
10500 if (match(RHS, m_LogicalOr(m_Value(RHS1), m_Value(RHS2)))) {
10501 if (std::optional<bool> Imp =
10502 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
10503 if (*Imp == true)
10504 return !InvertRHS;
10505 if (std::optional<bool> Imp =
10506 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
10507 if (*Imp == true)
10508 return !InvertRHS;
10509 }
10510 if (match(RHS, m_LogicalAnd(m_Value(RHS1), m_Value(RHS2)))) {
10511 if (std::optional<bool> Imp =
10512 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
10513 if (*Imp == false)
10514 return InvertRHS;
10515 if (std::optional<bool> Imp =
10516 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
10517 if (*Imp == false)
10518 return InvertRHS;
10519 }
10520
10521 return std::nullopt;
10522}
10523
10524// Returns a pair (Condition, ConditionIsTrue), where Condition is a branch
10525// condition dominating ContextI or nullptr, if no condition is found.
10526static std::pair<Value *, bool>
10528 if (!ContextI || !ContextI->getParent())
10529 return {nullptr, false};
10530
10531 // TODO: This is a poor/cheap way to determine dominance. Should we use a
10532 // dominator tree (eg, from a SimplifyQuery) instead?
10533 const BasicBlock *ContextBB = ContextI->getParent();
10534 const BasicBlock *PredBB = ContextBB->getSinglePredecessor();
10535 if (!PredBB)
10536 return {nullptr, false};
10537
10538 // We need a conditional branch in the predecessor.
10539 Value *PredCond;
10540 BasicBlock *TrueBB, *FalseBB;
10541 if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))
10542 return {nullptr, false};
10543
10544 // The branch should get simplified. Don't bother simplifying this condition.
10545 if (TrueBB == FalseBB)
10546 return {nullptr, false};
10547
10548 assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
10549 "Predecessor block does not point to successor?");
10550
10551 // Is this condition implied by the predecessor condition?
10552 return {PredCond, TrueBB == ContextBB};
10553}
10554
10555std::optional<bool> llvm::isImpliedByDomCondition(const Value *Cond,
10556 const Instruction *ContextI,
10557 const DataLayout &DL) {
10558 assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool");
10559 auto PredCond = getDomPredecessorCondition(ContextI);
10560 if (PredCond.first)
10561 return isImpliedCondition(PredCond.first, Cond, DL, PredCond.second);
10562 return std::nullopt;
10563}
10564
10566 const Value *LHS,
10567 const Value *RHS,
10568 const Instruction *ContextI,
10569 const DataLayout &DL) {
10570 auto PredCond = getDomPredecessorCondition(ContextI);
10571 if (PredCond.first)
10572 return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL,
10573 PredCond.second);
10574 return std::nullopt;
10575}
10576
10578 APInt &Upper, const InstrInfoQuery &IIQ,
10579 bool PreferSignedRange) {
10580 unsigned Width = Lower.getBitWidth();
10581 const APInt *C;
10582 switch (BO.getOpcode()) {
10583 case Instruction::Sub:
10584 if (match(BO.getOperand(0), m_APInt(C))) {
10585 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
10586 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
10587
10588 // If the caller expects a signed compare, then try to use a signed range.
10589 // Otherwise if both no-wraps are set, use the unsigned range because it
10590 // is never larger than the signed range. Example:
10591 // "sub nuw nsw i8 -2, x" is unsigned [0, 254] vs. signed [-128, 126].
10592 // "sub nuw nsw i8 2, x" is unsigned [0, 2] vs. signed [-125, 127].
10593 if (PreferSignedRange && HasNSW && HasNUW)
10594 HasNUW = false;
10595
10596 if (HasNUW) {
10597 // 'sub nuw c, x' produces [0, C].
10598 Upper = *C + 1;
10599 } else if (HasNSW) {
10600 if (C->isNegative()) {
10601 // 'sub nsw -C, x' produces [SINT_MIN, -C - SINT_MIN].
10603 Upper = *C - APInt::getSignedMaxValue(Width);
10604 } else {
10605 // Note that sub 0, INT_MIN is not NSW. It techically is a signed wrap
10606 // 'sub nsw C, x' produces [C - SINT_MAX, SINT_MAX].
10607 Lower = *C - APInt::getSignedMaxValue(Width);
10609 }
10610 }
10611 }
10612 break;
10613 case Instruction::Add:
10614 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
10615 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
10616 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
10617
10618 // If the caller expects a signed compare, then try to use a signed
10619 // range. Otherwise if both no-wraps are set, use the unsigned range
10620 // because it is never larger than the signed range. Example: "add nuw
10621 // nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125].
10622 if (PreferSignedRange && HasNSW && HasNUW)
10623 HasNUW = false;
10624
10625 if (HasNUW) {
10626 // 'add nuw x, C' produces [C, UINT_MAX].
10627 Lower = *C;
10628 } else if (HasNSW) {
10629 if (C->isNegative()) {
10630 // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C].
10632 Upper = APInt::getSignedMaxValue(Width) + *C + 1;
10633 } else {
10634 // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX].
10635 Lower = APInt::getSignedMinValue(Width) + *C;
10636 Upper = APInt::getSignedMaxValue(Width) + 1;
10637 }
10638 }
10639 }
10640 break;
10641
10642 case Instruction::And:
10643 if (match(BO.getOperand(1), m_APInt(C)))
10644 // 'and x, C' produces [0, C].
10645 Upper = *C + 1;
10646 // X & -X is a power of two or zero. So we can cap the value at max power of
10647 // two.
10648 if (match(BO.getOperand(0), m_Neg(m_Specific(BO.getOperand(1)))) ||
10649 match(BO.getOperand(1), m_Neg(m_Specific(BO.getOperand(0)))))
10650 Upper = APInt::getSignedMinValue(Width) + 1;
10651 break;
10652
10653 case Instruction::Or:
10654 if (match(BO.getOperand(1), m_APInt(C)))
10655 // 'or x, C' produces [C, UINT_MAX].
10656 Lower = *C;
10657 break;
10658
10659 case Instruction::AShr:
10660 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10661 // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C].
10663 Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1;
10664 } else if (match(BO.getOperand(0), m_APInt(C))) {
10665 unsigned ShiftAmount = Width - 1;
10666 if (!C->isZero() && IIQ.isExact(&BO))
10667 ShiftAmount = C->countr_zero();
10668 if (C->isNegative()) {
10669 // 'ashr C, x' produces [C, C >> (Width-1)]
10670 Lower = *C;
10671 Upper = C->ashr(ShiftAmount) + 1;
10672 } else {
10673 // 'ashr C, x' produces [C >> (Width-1), C]
10674 Lower = C->ashr(ShiftAmount);
10675 Upper = *C + 1;
10676 }
10677 }
10678 break;
10679
10680 case Instruction::LShr:
10681 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10682 // 'lshr x, C' produces [0, UINT_MAX >> C].
10683 Upper = APInt::getAllOnes(Width).lshr(*C) + 1;
10684 } else if (match(BO.getOperand(0), m_APInt(C))) {
10685 // 'lshr C, x' produces [C >> (Width-1), C].
10686 unsigned ShiftAmount = Width - 1;
10687 if (!C->isZero() && IIQ.isExact(&BO))
10688 ShiftAmount = C->countr_zero();
10689 Lower = C->lshr(ShiftAmount);
10690 Upper = *C + 1;
10691 }
10692 break;
10693
10694 case Instruction::Shl:
10695 if (match(BO.getOperand(0), m_APInt(C))) {
10696 if (IIQ.hasNoUnsignedWrap(&BO)) {
10697 // 'shl nuw C, x' produces [C, C << CLZ(C)]
10698 Lower = *C;
10699 Upper = Lower.shl(Lower.countl_zero()) + 1;
10700 } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw?
10701 if (C->isNegative()) {
10702 // 'shl nsw C, x' produces [C << CLO(C)-1, C]
10703 unsigned ShiftAmount = C->countl_one() - 1;
10704 Lower = C->shl(ShiftAmount);
10705 Upper = *C + 1;
10706 } else {
10707 // 'shl nsw C, x' produces [C, C << CLZ(C)-1]
10708 unsigned ShiftAmount = C->countl_zero() - 1;
10709 Lower = *C;
10710 Upper = C->shl(ShiftAmount) + 1;
10711 }
10712 } else {
10713 // If lowbit is set, value can never be zero.
10714 if ((*C)[0])
10715 Lower = APInt::getOneBitSet(Width, 0);
10716 // If we are shifting a constant the largest it can be is if the longest
10717 // sequence of consecutive ones is shifted to the highbits (breaking
10718 // ties for which sequence is higher). At the moment we take a liberal
10719 // upper bound on this by just popcounting the constant.
10720 // TODO: There may be a bitwise trick for it longest/highest
10721 // consecutative sequence of ones (naive method is O(Width) loop).
10722 Upper = APInt::getHighBitsSet(Width, C->popcount()) + 1;
10723 }
10724 } else if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10725 Upper = APInt::getBitsSetFrom(Width, C->getZExtValue()) + 1;
10726 }
10727 break;
10728
10729 case Instruction::SDiv:
10730 if (match(BO.getOperand(1), m_APInt(C))) {
10731 APInt IntMin = APInt::getSignedMinValue(Width);
10732 APInt IntMax = APInt::getSignedMaxValue(Width);
10733 if (C->isAllOnes()) {
10734 // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX]
10735 // where C != -1 and C != 0 and C != 1
10736 Lower = IntMin + 1;
10737 Upper = IntMax + 1;
10738 } else if (C->countl_zero() < Width - 1) {
10739 // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C]
10740 // where C != -1 and C != 0 and C != 1
10741 Lower = IntMin.sdiv(*C);
10742 Upper = IntMax.sdiv(*C);
10743 if (Lower.sgt(Upper))
10745 Upper = Upper + 1;
10746 assert(Upper != Lower && "Upper part of range has wrapped!");
10747 }
10748 } else if (match(BO.getOperand(0), m_APInt(C))) {
10749 if (C->isMinSignedValue()) {
10750 // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2].
10751 Lower = *C;
10752 Upper = Lower.lshr(1) + 1;
10753 } else {
10754 // 'sdiv C, x' produces [-|C|, |C|].
10755 Upper = C->abs() + 1;
10756 Lower = (-Upper) + 1;
10757 }
10758 }
10759 break;
10760
10761 case Instruction::UDiv:
10762 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
10763 // 'udiv x, C' produces [0, UINT_MAX / C].
10764 Upper = APInt::getMaxValue(Width).udiv(*C) + 1;
10765 } else if (match(BO.getOperand(0), m_APInt(C))) {
10766 // 'udiv C, x' produces [0, C].
10767 Upper = *C + 1;
10768 }
10769 break;
10770
10771 case Instruction::SRem:
10772 if (match(BO.getOperand(1), m_APInt(C))) {
10773 // 'srem x, C' produces (-|C|, |C|).
10774 Upper = C->abs();
10775 Lower = (-Upper) + 1;
10776 } else if (match(BO.getOperand(0), m_APInt(C))) {
10777 if (C->isNegative()) {
10778 // 'srem -|C|, x' produces [-|C|, 0].
10779 Upper = 1;
10780 Lower = *C;
10781 } else {
10782 // 'srem |C|, x' produces [0, |C|].
10783 Upper = *C + 1;
10784 }
10785 }
10786 break;
10787
10788 case Instruction::URem:
10789 if (match(BO.getOperand(1), m_APInt(C)))
10790 // 'urem x, C' produces [0, C).
10791 Upper = *C;
10792 else if (match(BO.getOperand(0), m_APInt(C)))
10793 // 'urem C, x' produces [0, C].
10794 Upper = *C + 1;
10795 break;
10796
10797 default:
10798 break;
10799 }
10800}
10801
10803 bool UseInstrInfo) {
10804 unsigned Width = II.getType()->getScalarSizeInBits();
10805 const APInt *C;
10806 switch (II.getIntrinsicID()) {
10807 case Intrinsic::ctlz:
10808 case Intrinsic::cttz: {
10809 APInt Upper(Width, Width);
10810 if (!UseInstrInfo || !match(II.getArgOperand(1), m_One()))
10811 Upper += 1;
10812 // Maximum of set/clear bits is the bit width.
10814 }
10815 case Intrinsic::ctpop:
10816 // Maximum of set/clear bits is the bit width.
10818 APInt(Width, Width) + 1);
10819 case Intrinsic::uadd_sat:
10820 // uadd.sat(x, C) produces [C, UINT_MAX].
10821 if (match(II.getOperand(0), m_APInt(C)) ||
10822 match(II.getOperand(1), m_APInt(C)))
10824 break;
10825 case Intrinsic::sadd_sat:
10826 if (match(II.getOperand(0), m_APInt(C)) ||
10827 match(II.getOperand(1), m_APInt(C))) {
10828 if (C->isNegative())
10829 // sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)].
10831 APInt::getSignedMaxValue(Width) + *C +
10832 1);
10833
10834 // sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].
10836 APInt::getSignedMaxValue(Width) + 1);
10837 }
10838 break;
10839 case Intrinsic::usub_sat:
10840 // usub.sat(C, x) produces [0, C].
10841 if (match(II.getOperand(0), m_APInt(C)))
10842 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
10843
10844 // usub.sat(x, C) produces [0, UINT_MAX - C].
10845 if (match(II.getOperand(1), m_APInt(C)))
10847 APInt::getMaxValue(Width) - *C + 1);
10848 break;
10849 case Intrinsic::ssub_sat:
10850 if (match(II.getOperand(0), m_APInt(C))) {
10851 if (C->isNegative())
10852 // ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)].
10854 *C - APInt::getSignedMinValue(Width) +
10855 1);
10856
10857 // ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].
10859 APInt::getSignedMaxValue(Width) + 1);
10860 } else if (match(II.getOperand(1), m_APInt(C))) {
10861 if (C->isNegative())
10862 // ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]:
10864 APInt::getSignedMaxValue(Width) + 1);
10865
10866 // ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].
10868 APInt::getSignedMaxValue(Width) - *C +
10869 1);
10870 }
10871 break;
10872 case Intrinsic::ushl_sat:
10873 if (match(II.getOperand(0), m_APInt(C)))
10875 break;
10876 case Intrinsic::sshl_sat:
10877 if (match(II.getOperand(0), m_APInt(C))) {
10878 if (C->isNegative())
10880 *C + 1);
10882 APInt::getSignedMaxValue(Width) + 1);
10883 }
10884 break;
10885 case Intrinsic::umin:
10886 case Intrinsic::umax:
10887 case Intrinsic::smin:
10888 case Intrinsic::smax:
10889 if (!match(II.getOperand(0), m_APInt(C)) &&
10890 !match(II.getOperand(1), m_APInt(C)))
10891 break;
10892
10893 switch (II.getIntrinsicID()) {
10894 case Intrinsic::umin:
10895 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
10896 case Intrinsic::umax:
10898 case Intrinsic::smin:
10900 *C + 1);
10901 case Intrinsic::smax:
10903 APInt::getSignedMaxValue(Width) + 1);
10904 default:
10905 llvm_unreachable("Must be min/max intrinsic");
10906 }
10907 break;
10908 case Intrinsic::abs:
10909 // If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX],
10910 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10911 if (match(II.getOperand(1), m_One()))
10913 APInt::getSignedMaxValue(Width) + 1);
10914
10916 APInt::getSignedMinValue(Width) + 1);
10917 case Intrinsic::vscale:
10918 if (!II.getParent() || !II.getFunction())
10919 break;
10920 return getVScaleRange(II.getFunction(), Width);
10921 case Intrinsic::read_register:
10922 case Intrinsic::read_volatile_register: {
10923 const Module *M = II.getModule();
10924 if (!M || !M->getTargetTriple().isRISCV())
10925 break;
10926 if (II.getFunction() && isReadVLENB(II))
10927 return getRISCVVLENBRange(II, Width);
10928 break;
10929 }
10930 default:
10931 break;
10932 }
10933
10934 return ConstantRange::getFull(Width);
10935}
10936
10938 const InstrInfoQuery &IIQ) {
10939 unsigned BitWidth = SI.getType()->getScalarSizeInBits();
10940 const Value *LHS = nullptr, *RHS = nullptr;
10942 if (R.Flavor == SPF_UNKNOWN)
10943 return ConstantRange::getFull(BitWidth);
10944
10945 if (R.Flavor == SelectPatternFlavor::SPF_ABS) {
10946 // If the negation part of the abs (in RHS) has the NSW flag,
10947 // then the result of abs(X) is [0..SIGNED_MAX],
10948 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10949 if (match(RHS, m_Neg(m_Specific(LHS))) &&
10953
10956 }
10957
10958 if (R.Flavor == SelectPatternFlavor::SPF_NABS) {
10959 // The result of -abs(X) is <= 0.
10961 APInt(BitWidth, 1));
10962 }
10963
10964 const APInt *C;
10965 if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C)))
10966 return ConstantRange::getFull(BitWidth);
10967
10968 switch (R.Flavor) {
10969 case SPF_UMIN:
10971 case SPF_UMAX:
10973 case SPF_SMIN:
10975 *C + 1);
10976 case SPF_SMAX:
10979 default:
10980 return ConstantRange::getFull(BitWidth);
10981 }
10982}
10983
10985 // The maximum representable value of a half is 65504. For floats the maximum
10986 // value is 3.4e38 which requires roughly 129 bits.
10987 unsigned BitWidth = I->getType()->getScalarSizeInBits();
10988 if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy())
10989 return;
10990 if (isa<FPToSIInst>(I) && BitWidth >= 17) {
10991 Lower = APInt(BitWidth, -65504, true);
10992 Upper = APInt(BitWidth, 65505);
10993 }
10994
10995 if (isa<FPToUIInst>(I) && BitWidth >= 16) {
10996 // For a fptoui the lower limit is left as 0.
10997 Upper = APInt(BitWidth, 65505);
10998 }
10999}
11000
11002 const SimplifyQuery &SQ,
11003 unsigned Depth) {
11004 assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction");
11005
11007 return ConstantRange::getFull(V->getType()->getScalarSizeInBits());
11008
11009 if (auto *C = dyn_cast<Constant>(V))
11010 return C->toConstantRange();
11011
11012 unsigned BitWidth = V->getType()->getScalarSizeInBits();
11013 ConstantRange CR = ConstantRange::getFull(BitWidth);
11014 if (auto *BO = dyn_cast<BinaryOperator>(V)) {
11015 APInt Lower = APInt(BitWidth, 0);
11016 APInt Upper = APInt(BitWidth, 0);
11017 // TODO: Return ConstantRange.
11018 setLimitsForBinOp(*BO, Lower, Upper, SQ.IIQ, ForSigned);
11020 } else if (auto *II = dyn_cast<IntrinsicInst>(V))
11022 else if (auto *SI = dyn_cast<SelectInst>(V)) {
11023 ConstantRange CRTrue =
11024 computeConstantRange(SI->getTrueValue(), ForSigned, SQ, Depth + 1);
11025 ConstantRange CRFalse =
11026 computeConstantRange(SI->getFalseValue(), ForSigned, SQ, Depth + 1);
11027 CR = CRTrue.unionWith(CRFalse, ForSigned ? ConstantRange::Signed
11030 } else if (auto *TI = dyn_cast<TruncInst>(V)) {
11031 ConstantRange SrcCR =
11032 computeConstantRange(TI->getOperand(0), ForSigned, SQ, Depth + 1);
11033 CR = SrcCR.truncate(BitWidth);
11034 } else if (auto *ZExt = dyn_cast<ZExtInst>(V)) {
11035 ConstantRange SrcCR =
11036 computeConstantRange(ZExt->getOperand(0), ForSigned, SQ, Depth + 1);
11037 CR = SrcCR.zeroExtend(BitWidth);
11038 } else if (auto *SExt = dyn_cast<SExtInst>(V)) {
11039 ConstantRange SrcCR =
11040 computeConstantRange(SExt->getOperand(0), ForSigned, SQ, Depth + 1);
11041 CR = SrcCR.signExtend(BitWidth);
11042 } else if (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {
11043 APInt Lower = APInt(BitWidth, 0);
11044 APInt Upper = APInt(BitWidth, 0);
11045 // TODO: Return ConstantRange.
11048 } else if (const auto *A = dyn_cast<Argument>(V))
11049 if (std::optional<ConstantRange> Range = A->getRange())
11050 CR = *Range;
11051
11052 if (auto *I = dyn_cast<Instruction>(V)) {
11053 if (auto *Range = SQ.IIQ.getMetadata(I, LLVMContext::MD_range))
11055
11056 Value *FrexpSrc;
11057 if (const auto *CB = dyn_cast<CallBase>(V)) {
11058 if (std::optional<ConstantRange> Range = CB->getRange())
11059 CR = CR.intersectWith(*Range);
11061 m_Value(FrexpSrc))))) {
11062 const fltSemantics &FltSem =
11063 FrexpSrc->getType()->getScalarType()->getFltSemantics();
11064 // It should be possible to implement this for any type, but this logic
11065 // only computes the range assuming standard subnormal handling.
11066 if (APFloat::isIEEELikeFP(FltSem)) {
11068 FrexpSrc, fcSubnormal | fcZero | fcNan | fcInf, SQ, Depth + 1);
11069
11070 // The exponent of frexp(NaN) and frexp(Inf) is unspecified. Only
11071 // constrain its range when the source can be neither.
11072 if (KnownSrc.isKnownNeverInfOrNaN()) {
11073 int MinExp = APFloat::semanticsMinExponent(FltSem) + 1;
11074
11075 // Offset to find the true minimum exponent value for a denormal.
11076 if (!KnownSrc.isKnownNeverSubnormal())
11077 MinExp -= (APFloat::semanticsPrecision(FltSem) - 1);
11078
11079 int MaxExp = APFloat::semanticsMaxExponent(FltSem) + 1;
11080
11081 auto [AdjustedMin, AdjustedMax, AdjustedMaxNonZero] =
11083
11084 DenormalMode Mode = I->getFunction()->getDenormalMode(FltSem);
11085 bool NeverLogicalZero = KnownSrc.isKnownNeverLogicalZero(Mode);
11086
11087 MinExp = std::max(AdjustedMin, MinExp);
11088 MaxExp = std::min(NeverLogicalZero ? AdjustedMaxNonZero : AdjustedMax,
11089 MaxExp);
11090
11092 APInt(BitWidth, static_cast<int64_t>(MinExp), /*isSigned=*/true),
11093 APInt(BitWidth, static_cast<int64_t>(MaxExp) + 1,
11094 /*isSigned=*/true));
11095 }
11096 }
11097 }
11098 }
11099
11100 if (SQ.CtxI && SQ.AC) {
11101 // Try to restrict the range based on information from assumptions.
11102 for (auto &AssumeVH : SQ.AC->assumptionsFor(V)) {
11103 if (!AssumeVH)
11104 continue;
11105 CallInst *I = cast<CallInst>(AssumeVH);
11106 assert(I->getParent()->getParent() == SQ.CtxI->getParent()->getParent() &&
11107 "Got assumption for the wrong function!");
11108 assert(I->getIntrinsicID() == Intrinsic::assume &&
11109 "must be an assume intrinsic");
11110
11111 if (AssumeVH.Index != AssumptionCache::ExprResultIdx) {
11112 auto OBU = I->getOperandBundleAt(AssumeVH.Index);
11113 if (getBundleAttrFromOBU(OBU) == BundleAttr::Range) {
11114 auto [Val, Range] = getAssumeRangeInfo(OBU);
11115 if (Val == V && isValidAssumeForContext(I, SQ))
11116 CR = CR.intersectWith(Range);
11117 }
11118 continue;
11119 }
11120
11121 if (!isValidAssumeForContext(I, SQ))
11122 continue;
11123 Value *Arg = I->getArgOperand(0);
11124 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
11125 // Currently we just use information from comparisons.
11126 if (!Cmp || Cmp->getOperand(0) != V)
11127 continue;
11128 // TODO: Set "ForSigned" parameter via Cmp->isSigned()?
11129 ConstantRange RHS =
11130 computeConstantRange(Cmp->getOperand(1), /*ForSigned=*/false,
11131 SQ.getWithInstruction(I), Depth + 1);
11132 CR = CR.intersectWith(
11133 ConstantRange::makeAllowedICmpRegion(Cmp->getCmpPredicate(), RHS));
11134 }
11135 }
11136
11137 return CR;
11138}
11139
11140static void
11142 function_ref<void(Value *)> InsertAffected) {
11143 assert(V != nullptr);
11144 if (isa<Argument>(V) || isa<GlobalValue>(V)) {
11145 InsertAffected(V);
11146 } else if (auto *I = dyn_cast<Instruction>(V)) {
11147 InsertAffected(V);
11148
11149 // Peek through unary operators to find the source of the condition.
11150 Value *Op;
11152 m_Trunc(m_Value(Op))))) {
11154 InsertAffected(Op);
11155 }
11156 }
11157}
11158
11160 Value *Cond, bool IsAssume, function_ref<void(Value *)> InsertAffected) {
11161 auto AddAffected = [&InsertAffected](Value *V) {
11162 addValueAffectedByCondition(V, InsertAffected);
11163 };
11164
11165 auto AddCmpOperands = [&AddAffected, IsAssume](Value *LHS, Value *RHS) {
11166 if (IsAssume) {
11167 AddAffected(LHS);
11168 AddAffected(RHS);
11169 } else if (match(RHS, m_Constant()))
11170 AddAffected(LHS);
11171 };
11172
11173 SmallVector<Value *, 8> Worklist;
11175 Worklist.push_back(Cond);
11176 while (!Worklist.empty()) {
11177 Value *V = Worklist.pop_back_val();
11178 if (!Visited.insert(V).second)
11179 continue;
11180
11181 CmpPredicate Pred;
11182 Value *A, *B, *X;
11183
11184 if (IsAssume) {
11185 AddAffected(V);
11186 if (match(V, m_Not(m_Value(X))))
11187 AddAffected(X);
11188 }
11189
11190 if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
11191 // assume(A && B) is split to -> assume(A); assume(B);
11192 // assume(!(A || B)) is split to -> assume(!A); assume(!B);
11193 // Finally, assume(A || B) / assume(!(A && B)) generally don't provide
11194 // enough information to be worth handling (intersection of information as
11195 // opposed to union).
11196 if (!IsAssume) {
11197 Worklist.push_back(A);
11198 Worklist.push_back(B);
11199 }
11200 } else if (match(V, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
11201 bool HasRHSC = match(B, m_ConstantInt());
11202 if (ICmpInst::isEquality(Pred)) {
11203 AddAffected(A);
11204 if (IsAssume)
11205 AddAffected(B);
11206 if (HasRHSC) {
11207 Value *Y;
11208 // (X << C) or (X >>_s C) or (X >>_u C).
11209 if (match(A, m_Shift(m_Value(X), m_ConstantInt())))
11210 AddAffected(X);
11211 // (X & C) or (X | C).
11212 else if (match(A, m_And(m_Value(X), m_Value(Y))) ||
11213 match(A, m_Or(m_Value(X), m_Value(Y)))) {
11214 AddAffected(X);
11215 AddAffected(Y);
11216 }
11217 // X - Y
11218 else if (match(A, m_Sub(m_Value(X), m_Value(Y)))) {
11219 AddAffected(X);
11220 AddAffected(Y);
11221 }
11222 }
11223 } else {
11224 AddCmpOperands(A, B);
11225 if (HasRHSC) {
11226 Value *Y;
11227 // Handle (A + C1) u< C2, which is the canonical form of
11228 // A > C3 && A < C4.
11230 AddAffected(X);
11231 // X - Y pred C, which implies X != Y if 0 pred C is false.
11232 else if (match(A, m_Sub(m_Value(X), m_Value(Y)))) {
11233 AddAffected(X);
11234 AddAffected(Y);
11235 }
11236
11237 if (ICmpInst::isUnsigned(Pred)) {
11238 // X & Y u> C -> X >u C && Y >u C
11239 // X | Y u< C -> X u< C && Y u< C
11240 // X nuw+ Y u< C -> X u< C && Y u< C
11241 if (match(A, m_And(m_Value(X), m_Value(Y))) ||
11242 match(A, m_Or(m_Value(X), m_Value(Y))) ||
11243 match(A, m_NUWAdd(m_Value(X), m_Value(Y)))) {
11244 AddAffected(X);
11245 AddAffected(Y);
11246 }
11247 }
11248 }
11249
11250 // Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported
11251 // by computeKnownFPClass().
11253 if (Pred == ICmpInst::ICMP_SLT && match(B, m_Zero()))
11254 InsertAffected(X);
11255 else if (Pred == ICmpInst::ICMP_SGT && match(B, m_AllOnes()))
11256 InsertAffected(X);
11257 }
11258 }
11259
11260 auto AddNuwSquareOperand = [&AddAffected](Value *Op) {
11261 Value *SquareOp = nullptr;
11262 if (match(Op, m_NUWMul(m_Value(SquareOp), m_Deferred(SquareOp))))
11263 AddAffected(SquareOp);
11264 };
11265 AddNuwSquareOperand(A);
11266 AddNuwSquareOperand(B);
11267
11268 if (HasRHSC && match(A, m_Ctpop(m_Value(X))))
11269 AddAffected(X);
11270 } else if (match(V, m_FCmp(Pred, m_Value(A), m_Value(B)))) {
11271 AddCmpOperands(A, B);
11272
11273 // fcmp fneg(x), y
11274 // fcmp fabs(x), y
11275 // fcmp fneg(fabs(x)), y
11276 // TODO: Confirm whether m_FNegOrFSubNegZero is correct.
11278 AddAffected(A);
11279 if (match(A, m_FAbs(m_Value(A))))
11280 AddAffected(A);
11281
11283 m_Value()))) {
11284 // Handle patterns that computeKnownFPClass() support.
11285 AddAffected(A);
11286 } else if (!IsAssume && match(V, m_Trunc(m_Value(X)))) {
11287 // Assume is checked here as X is already added above for assumes in
11288 // addValueAffectedByCondition
11289 AddAffected(X);
11290 } else if (!IsAssume && match(V, m_Not(m_Value(X)))) {
11291 // Assume is checked here to avoid issues with ephemeral values
11292 Worklist.push_back(X);
11293 }
11294 }
11295}
11296
11298 // (X >> C) or/add (X & mask(C) != 0)
11299 if (const auto *BO = dyn_cast<BinaryOperator>(V)) {
11300 if (BO->getOpcode() == Instruction::Add ||
11301 BO->getOpcode() == Instruction::Or) {
11302 const Value *X;
11303 const APInt *C1, *C2;
11304 if (match(BO, m_c_BinOp(m_LShr(m_Value(X), m_APInt(C1)),
11308 m_Zero())))) &&
11309 C2->popcount() == C1->getZExtValue())
11310 return X;
11311 }
11312 }
11313
11314 // select(cond, X, -X)
11315 {
11316 const Value *X;
11317 if (match(V, m_c_Select(m_Value(X), m_Neg(m_Deferred(X)))))
11318 return X;
11319 }
11320
11321 return nullptr;
11322}
11323
11325 return const_cast<Value *>(stripNullTest(const_cast<const Value *>(V)));
11326}
11327
11330 unsigned MaxCount, bool AllowUndefOrPoison) {
11333 auto Push = [&](const Value *V) -> bool {
11334 Constant *C;
11335 if (match(const_cast<Value *>(V), m_ImmConstant(C))) {
11336 if (!AllowUndefOrPoison && !isGuaranteedNotToBeUndefOrPoison(C))
11337 return false;
11338 // Check existence first to avoid unnecessary allocations.
11339 if (Constants.contains(C))
11340 return true;
11341 if (Constants.size() == MaxCount)
11342 return false;
11343 Constants.insert(C);
11344 return true;
11345 }
11346
11347 if (auto *Inst = dyn_cast<Instruction>(V)) {
11348 if (Visited.insert(Inst).second)
11349 Worklist.push_back(Inst);
11350 return true;
11351 }
11352 return false;
11353 };
11354 if (!Push(V))
11355 return false;
11356 while (!Worklist.empty()) {
11357 const Instruction *CurInst = Worklist.pop_back_val();
11358 switch (CurInst->getOpcode()) {
11359 case Instruction::Select:
11360 if (!Push(CurInst->getOperand(1)))
11361 return false;
11362 if (!Push(CurInst->getOperand(2)))
11363 return false;
11364 break;
11365 case Instruction::PHI:
11366 for (Value *IncomingValue : cast<PHINode>(CurInst)->incoming_values()) {
11367 // Fast path for recurrence PHI.
11368 if (IncomingValue == CurInst)
11369 continue;
11370 if (!Push(IncomingValue))
11371 return false;
11372 }
11373 break;
11374 default:
11375 return false;
11376 }
11377 }
11378 return true;
11379}
const TargetLowering & TLI
SDValue Pred
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
AMDGPU Register Bank Select
Rewrite undef for PHI
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis Results
This file contains the simple types necessary to represent the attributes associated with functions a...
bpf unwind BPF Unwind Lower
static const Function * getParent(const Value *V)
#define X(NUM, ENUM, NAME)
Definition ELF.h:665
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Utilities for dealing with flags related to floating point properties and mode controls.
static Value * getCondition(Instruction *I)
Hexagon Common GEP
#define _
static MaybeAlign getAlign(Value *Ptr)
Module.h This file contains the declarations for the Module class.
static bool hasNoUnsignedWrap(BinaryOperator &I)
#define RegName(no)
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
This file contains the declarations for metadata subclasses.
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
#define P(N)
PowerPC Reduce CR logical Operation
R600 Clause Merge
SmallVector< MachineOperand, 4 > Cond
int64_t C1
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
std::pair< BasicBlock *, BasicBlock * > Edge
This file contains some templates that are useful if you are working with the STL at all.
This file defines the scope_exit class, which executes user-defined cleanup logic at scope exit.
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
This file contains the UndefPoisonKind enum and helper functions.
static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero, SimplifyQuery &Q, unsigned Depth)
Try to detect a recurrence that the value of the induction variable is always a power of two (or zero...
static cl::opt< unsigned > DomConditionsMaxUses("dom-conditions-max-uses", cl::Hidden, cl::init(20))
static unsigned computeNumSignBitsVectorConstant(const Value *V, const APInt &DemandedElts, unsigned TyBits)
For vector constants, loop over the elements and find the constant with the minimum number of sign bi...
static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS, const Value *RHS)
Return true if "icmp Pred LHS RHS" is always true.
static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if V1 == (binop V2, X), where X is known non-zero.
static bool isGEPKnownNonNull(const GEPOperator *GEP, const SimplifyQuery &Q, unsigned Depth)
Test whether a GEP's result is known to be non-null.
static bool isNonEqualShl(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and the shift is nuw or nsw.
static bool isKnownNonNullFromDominatingCondition(const Value *V, const Instruction *CtxI, const DominatorTree *DT)
static const Value * getUnderlyingObjectFromInt(const Value *V)
This is the function that does the work of looking through basic ptrtoint+arithmetic+inttoptr sequenc...
static bool isNonZeroMul(const APInt &DemandedElts, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, bool NSW, bool NUW, unsigned Depth)
static bool rangeMetadataExcludesValue(const MDNode *Ranges, const APInt &Value)
Does the 'Range' metadata (which must be a valid MD_range operand list) ensure that the value it's at...
static KnownBits getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts, const KnownBits &KnownLHS, const KnownBits &KnownRHS, const SimplifyQuery &Q, unsigned Depth)
static void breakSelfRecursivePHI(const Use *U, const PHINode *PHI, Value *&ValOut, Instruction *&CtxIOut, const PHINode **PhiOut=nullptr)
static bool isNonZeroSub(const APInt &DemandedElts, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, unsigned Depth)
static const Instruction * safeCtxI(const Value *V, const Instruction *CtxI)
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR)
Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
static void addValueAffectedByCondition(Value *V, function_ref< void(Value *)> InsertAffected)
static bool isNonZeroAddrPlusOffset(const SimplifyQuery &Q, Value *X, Value *Y)
Return true if "addr(P) + Off" cannot be zero.
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
static void setLimitsForBinOp(const BinaryOperator &BO, APInt &Lower, APInt &Upper, const InstrInfoQuery &IIQ, bool PreferSignedRange)
static Value * lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2, Instruction::CastOps *CastOp)
Helps to match a select pattern in case of a type mismatch.
static std::pair< Value *, bool > getDomPredecessorCondition(const Instruction *ContextI)
static constexpr unsigned MaxInstrsToCheckForFree
Maximum number of instructions to check between assume and context instruction.
static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, const KnownBits &KnownVal, unsigned Depth)
static bool hasNoFreeInRange(BasicBlock::const_iterator Begin, BasicBlock::const_iterator End, unsigned &NumChecked)
static std::optional< bool > isImpliedCondFCmps(FCmpInst::Predicate LPred, const Value *L0, const Value *L1, FCmpInst::Predicate RPred, const Value *R0, const Value *R1, const DataLayout &DL, bool LHSIsTrue)
Return true if LHS implies RHS (expanded to its components as "R0 RPred R1") is true.
static ConstantRange getRISCVVLENBRange(const IntrinsicInst &II, unsigned Width)
Return the value range of a RISC-V vlenb CSR read.
static bool isKnownNonEqualFromContext(const Value *V1, const Value *V2, const SimplifyQuery &Q, unsigned Depth)
static SelectPatternResult matchFastFloatClamp(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS)
Match clamp pattern for float types without care about NaNs or signed zeros.
static std::optional< bool > isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1, CmpPredicate RPred, const Value *R0, const Value *R1, const DataLayout &DL, bool LHSIsTrue)
Return true if LHS implies RHS (expanded to its components as "R0 RPred R1") is true.
static std::optional< bool > isImpliedCondCommonOperandWithCR(CmpPredicate LPred, const ConstantRange &LCR, CmpPredicate RPred, const ConstantRange &RCR)
Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
static SelectPatternResult matchMinMax(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS)
Match non-obvious integer minimum and maximum sequences.
static ConstantRange getRangeForSelectPattern(const SelectInst &SI, const InstrInfoQuery &IIQ)
static void computeKnownBitsFromOperator(const Operator *I, const APInt &DemandedElts, KnownBits &Known, const SimplifyQuery &Q, unsigned Depth)
static uint64_t GetStringLengthH(const Value *V, SmallPtrSetImpl< const PHINode * > &PHIs, unsigned CharSize)
If we can compute the length of the string pointed to by the specified pointer, return 'len+1'.
static void computeKnownBitsFromShiftOperator(const Operator *I, const APInt &DemandedElts, KnownBits &Known, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth, function_ref< KnownBits(const KnownBits &, const KnownBits &, bool)> KF)
Compute known bits from a shift operator, including those with a non-constant shift amount.
static void computeKnownFPClassFromCond(const Value *V, Value *Cond, bool CondIsTrue, const Instruction *CtxI, KnownFPClass &KnownFromContext, unsigned Depth=0)
static std::optional< bool > isImpliedCondAndOr(const Instruction *LHS, CmpPredicate RHSPred, const Value *RHSOp0, const Value *RHSOp1, const DataLayout &DL, bool LHSIsTrue, unsigned Depth)
Return true if LHS implies RHS is true.
static std::tuple< int, int, int > computeKnownExponentRangeFromContext(const Value *V, const SimplifyQuery &Q)
Compute the minimum and maximum values (inclusive) for the exponent of V, assuming it is not nan.
static bool isSignedMinMaxClamp(const Value *Select, const Value *&In, const APInt *&CLow, const APInt *&CHigh)
static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, bool NSW, bool NUW, unsigned Depth)
static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V, unsigned Depth)
static bool isNonEqualSelect(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst, Value *&Init, Value *&OtherOp)
static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static void computeKnownBitsFromCmp(const Value *V, CmpInst::Predicate Pred, Value *LHS, Value *RHS, KnownBits &Known, const SimplifyQuery &Q)
static void unionWithMinMaxIntrinsicClamp(const IntrinsicInst *II, KnownBits &Known)
static void setLimitForFPToI(const Instruction *I, APInt &Lower, APInt &Upper)
static bool isSameUnderlyingObjectInLoop(const PHINode *PN, const LoopInfo *LI)
PN defines a loop-variant pointer to an object.
static bool isNonEqualPointersWithRecursiveGEP(const Value *A, const Value *B, const SimplifyQuery &Q)
static bool isSignedMinMaxIntrinsicClamp(const IntrinsicInst *II, const APInt *&CLow, const APInt *&CHigh)
static Value * lookThroughCastConst(CmpInst *CmpI, Type *SrcTy, Constant *C, Instruction::CastOps *CastOp)
static bool handleGuaranteedWellDefinedOps(const Instruction *I, const CallableT &Handle)
Enumerates all operands of I that are guaranteed to not be undef or poison.
static bool isAbsoluteValueULEOne(const Value *V)
static void computeKnownBitsFromLerpPattern(const Value *Op0, const Value *Op1, const APInt &DemandedElts, KnownBits &KnownOut, const SimplifyQuery &Q, unsigned Depth)
Try to detect the lerp pattern: a * (b - c) + c * d where a >= 0, b >= 0, c >= 0, d >= 0,...
static KnownFPClass computeKnownFPClassFromContext(const Value *V, const SimplifyQuery &Q)
static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1, bool NSW, bool NUW, const APInt &DemandedElts, KnownBits &KnownOut, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth)
static Value * getNotValue(Value *V)
If the input value is the result of a 'not' op, constant integer, or vector splat of a constant integ...
static constexpr KnownFPClass::MinMaxKind getMinMaxKind(Intrinsic::ID IID)
static bool isReadVLENB(const IntrinsicInst &II)
Return true if II reads a register named "vlenb".
static unsigned ComputeNumSignBitsImpl(const Value *V, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return the number of times the sign bit of the register is replicated into the other bits.
static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp, KnownBits &Known, const SimplifyQuery &SQ, bool Invert)
static void computeKnownBitsFromEuclideanRem(const Value *Op0, const Value *Op1, KnownBits &KnownOut, const SimplifyQuery &Q, unsigned Depth)
Infer non-negativity for R + (R < 0 ?
static bool isKnownNonZeroFromOperator(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static bool matchOpWithOpEqZero(Value *Op0, Value *Op1)
static bool isNonZeroRecurrence(const PHINode *PN)
Try to detect a recurrence that monotonically increases/decreases from a non-zero starting value.
static SelectPatternResult matchClamp(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal)
Recognize variations of: CLAMP(v,l,h) ==> ((v) < (l) ?
static bool shiftAmountKnownInRange(const Value *ShiftAmount)
Shifts return poison if shiftwidth is larger than the bitwidth.
static bool isEphemeralValueOf(const Instruction *I, const Value *E)
static void computeKnownBitsForRecurrenceOperands(const PHINode *P, Value *Start, Value *Step, const APInt &DemandedElts, KnownBits &KnownStart, KnownBits &KnownStep, const SimplifyQuery &Q, unsigned Depth)
static void computeKnownFPClassForFAddSub(const Operator *Op, const APInt &DemandedElts, FPClassTest InterestedClasses, KnownFPClass &Known, const SimplifyQuery &Q, unsigned Depth, bool IsAdd, bool IsStrict)
static KnownBits computeKnownBitsForHorizontalOperation(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth, const function_ref< KnownBits(const KnownBits &, const KnownBits &)> KnownBitsFunc)
static bool handleGuaranteedNonPoisonOps(const Instruction *I, const CallableT &Handle)
Enumerates all operands of I that are guaranteed to not be poison.
static std::optional< std::pair< Value *, Value * > > getInvertibleOperands(const Operator *Op1, const Operator *Op2)
If the pair of operators are the same invertible function, return the the operands of the function co...
static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS)
static void computeKnownBitsFromCond(const Value *V, Value *Cond, KnownBits &Known, const SimplifyQuery &SQ, bool Invert, unsigned Depth)
static NoCommonBitsSetResult haveNoCommonBitsSetSpecialCases(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q)
static std::optional< bool > isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS, const Value *ARHS, const Value *BLHS, const Value *BRHS)
Return true if "icmp Pred BLHS BRHS" is true whenever "icmp PredALHS ARHS" is true.
static bool isNonEqualMul(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and the multiplication is nuw o...
static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero, const Value *Cond, bool CondIsTrue)
Return true if we can infer that V is known to be a power of 2 from dominating condition Cond (e....
static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW, bool NUW, const APInt &DemandedElts, KnownBits &Known, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth)
static bool matchThreeInputRecurrence(const PHINode *PN, InstTy *&Inst, Value *&Init, Value *&OtherOp0, Value *&OtherOp1)
static bool isKnownNonNaN(const Value *V, FastMathFlags FMF)
static bool isNonEqualURem(const Value *X, const Value *Rem, const SimplifyQuery &Q)
static ConstantRange getRangeForIntrinsic(const IntrinsicInst &II, bool UseInstrInfo)
static void computeKnownFPClassForFPTrunc(const Operator *Op, const APInt &DemandedElts, FPClassTest InterestedClasses, KnownFPClass &Known, const SimplifyQuery &Q, unsigned Depth)
static void computeKnownFPClassForFRem(const Operator *Op, const APInt &DemandedElts, FPClassTest InterestedClasses, KnownFPClass &Known, const SimplifyQuery &Q, unsigned Depth)
static Value * BuildSubAggregate(Value *From, Value *To, Type *IndexedType, SmallVectorImpl< unsigned > &Idxs, unsigned IdxSkip, BasicBlock::iterator InsertBefore)
Value * RHS
Value * LHS
static LLVM_ABI bool semanticsHasInf(const fltSemantics &)
Definition APFloat.cpp:368
static LLVM_ABI ExponentType semanticsMinExponent(const fltSemantics &)
Definition APFloat.cpp:343
static LLVM_ABI bool semanticsHasSignedRepr(const fltSemantics &)
Definition APFloat.cpp:364
static LLVM_ABI ExponentType semanticsMaxExponent(const fltSemantics &)
Definition APFloat.cpp:339
static LLVM_ABI unsigned int semanticsPrecision(const fltSemantics &)
Definition APFloat.cpp:335
static LLVM_ABI bool semanticsHasNaN(const fltSemantics &)
Definition APFloat.cpp:372
static LLVM_ABI bool semanticsHasZero(const fltSemantics &)
Definition APFloat.cpp:360
static LLVM_ABI bool isRepresentableAsNormalIn(const fltSemantics &Src, const fltSemantics &Dst)
Definition APFloat.cpp:385
static LLVM_ABI bool isIEEELikeFP(const fltSemantics &)
Definition APFloat.cpp:376
static LLVM_ABI const fltSemantics * getArbitraryFPSemantics(StringRef Format)
Returns the fltSemantics for a given arbitrary FP format string, or nullptr if invalid.
Definition APFloat.cpp:6340
LLVM_READONLY int getExactLog2Abs() const
Definition APFloat.h:1654
bool isFinite() const
Definition APFloat.h:1588
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1242
bool isInteger() const
Definition APFloat.h:1600
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:2018
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1602
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:230
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition APInt.h:1426
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition APInt.h:419
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1560
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition APInt.h:1411
unsigned popcount() const
Count the number of bits set.
Definition APInt.h:1690
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1405
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition APInt.h:202
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition APInt.h:1350
unsigned ceilLogBase2() const
Definition APInt.h:1784
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition APInt.h:367
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:376
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1508
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1115
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:205
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition APInt.h:212
bool isNegative() const
Determine sign of this APInt.
Definition APInt.h:325
bool intersects(const APInt &RHS) const
This operation tests if there are any pairs of corresponding bits between this APInt and RHS that are...
Definition APInt.h:1253
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1673
LLVM_ABI APInt reverseBits() const
Definition APInt.cpp:786
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition APInt.h:1170
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1648
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition APInt.h:1618
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:215
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1086
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition APInt.h:352
unsigned logBase2() const
Definition APInt.h:1781
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:829
bool getBoolValue() const
Convert APInt to a boolean value.
Definition APInt.h:467
bool isMaxSignedValue() const
Determine if this is the largest signed value.
Definition APInt.h:401
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:330
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition APInt.h:1154
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:875
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition APInt.h:1261
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition APInt.h:292
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:196
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition APInt.h:1408
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1241
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:282
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:235
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:853
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1225
an instruction to allocate memory on the stack
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator end() const
Definition ArrayRef.h:130
size_t size() const
Get the array size.
Definition ArrayRef.h:141
iterator begin() const
Definition ArrayRef.h:129
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Definition ArrayRef.h:185
Class to represent array types.
This represents the llvm.assume intrinsic.
A cache of @llvm.assume calls within a function.
MutableArrayRef< ResultElem > assumptionsFor(const Value *V)
Access the list of assumptions which affect this value.
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:107
LLVM_ABI std::optional< unsigned > getVScaleRangeMax() const
Returns the maximum value for the vscale_range attribute or std::nullopt when unknown.
LLVM_ABI unsigned getVScaleRangeMin() const
Returns the minimum value for the vscale_range attribute.
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition Attributes.h:263
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator end()
Definition BasicBlock.h:453
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:440
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
InstListType::const_iterator const_iterator
Definition BasicBlock.h:171
LLVM_ABI bool isEntryBlock() const
Return true if this is the entry block of the containing function.
LLVM_ABI const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
LLVM_ABI const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Definition BasicBlock.h:237
LLVM_ABI Instruction::BinaryOps getBinaryOp() const
Returns the binary operation underlying the intrinsic.
BinaryOps getOpcode() const
Definition InstrTypes.h:409
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
LLVM_ABI bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
LLVM_ABI bool isIndirectCall() const
Return true if the callsite is an indirect call.
bool onlyReadsMemory(unsigned OpNo) const
Value * getCalledOperand() const
Value * getArgOperand(unsigned i) const
LLVM_ABI Intrinsic::ID getIntrinsicID() const
Returns the intrinsic ID of the intrinsic called or Intrinsic::not_intrinsic if the called function i...
unsigned arg_size() const
This class represents a function call, abstracting a target machine's calling convention.
This is the base class for all instructions that perform data casts.
Definition InstrTypes.h:512
This class is the base class for the comparison instructions.
Definition InstrTypes.h:728
static LLVM_ABI Predicate getFlippedStrictnessPredicate(Predicate pred)
This is a static version that you can use without an instruction available.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:740
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition InstrTypes.h:743
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition InstrTypes.h:757
@ ICMP_SLT
signed less than
Definition InstrTypes.h:769
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:770
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:746
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition InstrTypes.h:755
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:744
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:745
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:764
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:763
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:767
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition InstrTypes.h:754
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition InstrTypes.h:751
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:765
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition InstrTypes.h:752
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:747
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition InstrTypes.h:749
@ ICMP_NE
not equal
Definition InstrTypes.h:762
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:768
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:766
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition InstrTypes.h:753
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition InstrTypes.h:742
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition InstrTypes.h:750
bool isSigned() const
Definition InstrTypes.h:993
static LLVM_ABI bool isEquality(Predicate pred)
Determine if this is an equals/not equals predicate.
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition InstrTypes.h:890
bool isTrueWhenEqual() const
This is just a convenience.
static bool isFPPredicate(Predicate P)
Definition InstrTypes.h:833
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition InstrTypes.h:852
Predicate getPredicate() const
Return the predicate for this instruction.
Definition InstrTypes.h:828
static bool isIntPredicate(Predicate P)
Definition InstrTypes.h:839
static LLVM_ABI bool isOrdered(Predicate predicate)
Determine if the predicate is an ordered operation.
bool isUnsigned() const
Definition InstrTypes.h:999
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
static LLVM_ABI std::optional< CmpPredicate > getMatching(CmpPredicate A, CmpPredicate B)
Compares two CmpPredicates taking samesign into account and returns the canonicalized CmpPredicate if...
LLVM_ABI CmpInst::Predicate getPreferredSignedPredicate() const
Attempts to return a signed CmpInst::Predicate from the CmpPredicate.
CmpInst::Predicate dropSameSign() const
Drops samesign information.
bool hasSameSign() const
Query samesign information, for optimizations.
Conditional Branch instruction.
An array constant whose element type is a simple 1/2/4/8-byte integer, bytes or float/double,...
Definition Constants.h:865
ConstantDataSequential - A vector or array constant whose element type is a simple 1/2/4/8-byte integ...
Definition Constants.h:755
StringRef getAsString() const
If this array is isString(), then this method returns the array as a StringRef.
Definition Constants.h:831
A vector constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition Constants.h:951
static LLVM_ABI Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static LLVM_ABI Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static LLVM_ABI std::optional< ConstantFPRange > makeExactFCmpRegion(FCmpInst::Predicate Pred, const APFloat &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:420
This is the shared class of boolean and integer constants.
Definition Constants.h:87
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition Constants.h:168
This class represents a range of values.
PreferredRangeType
If represented precisely, the result of some range operations may consist of multiple disjoint ranges...
static LLVM_ABI ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
LLVM_ABI OverflowResult unsignedSubMayOverflow(const ConstantRange &Other) const
Return whether unsigned sub of the two ranges always/never overflows.
LLVM_ABI bool isAllNegative() const
Return true if all values in this range are negative.
LLVM_ABI OverflowResult unsignedAddMayOverflow(const ConstantRange &Other) const
Return whether unsigned add of the two ranges always/never overflows.
LLVM_ABI KnownBits toKnownBits() const
Return known bits for values in this range.
LLVM_ABI bool icmp(CmpInst::Predicate Pred, const ConstantRange &Other) const
Does the predicate Pred hold between ranges this and Other?
LLVM_ABI ConstantRange zeroExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
LLVM_ABI APInt getSignedMin() const
Return the smallest signed value contained in the ConstantRange.
LLVM_ABI OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const
Return whether unsigned mul of the two ranges always/never overflows.
LLVM_ABI ConstantRange truncate(uint32_t BitWidth, unsigned NoWrapKind=0) const
Return a new range in the specified integer type, which must be strictly smaller than the current typ...
LLVM_ABI bool isAllNonNegative() const
Return true if all values in this range are non-negative.
LLVM_ABI ConstantRange signExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
static LLVM_ABI ConstantRange makeAllowedICmpRegion(CmpInst::Predicate Pred, const ConstantRange &Other)
Produce the smallest range such that all values that may satisfy the given predicate with any value c...
LLVM_ABI ConstantRange multiply(const ConstantRange &Other, unsigned NoWrapKind=0) const
Return a new range representing the possible values resulting from a multiplication of a value in thi...
LLVM_ABI ConstantRange unionWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the union of this range with another range.
static LLVM_ABI ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred, const APInt &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
LLVM_ABI ConstantRange binaryAnd(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a binary-and of a value in this ra...
LLVM_ABI bool contains(const APInt &Val) const
Return true if the specified value is in the set.
LLVM_ABI OverflowResult signedAddMayOverflow(const ConstantRange &Other) const
Return whether signed add of the two ranges always/never overflows.
LLVM_ABI APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
LLVM_ABI ConstantRange intersectWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the intersection of this range with another range.
LLVM_ABI APInt getSignedMax() const
Return the largest signed value contained in the ConstantRange.
OverflowResult
Represents whether an operation on the given constant range is known to always or never overflow.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
static ConstantRange getNonEmpty(APInt Lower, APInt Upper)
Create non-empty constant range with the given bounds.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
LLVM_ABI OverflowResult signedSubMayOverflow(const ConstantRange &Other) const
Return whether signed sub of the two ranges always/never overflows.
LLVM_ABI ConstantRange sub(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a subtraction of a value in this r...
This is an important base class in LLVM.
Definition Constant.h:44
static LLVM_ABI Constant * replaceUndefsWith(Constant *C, Constant *Replacement)
Try to replace undefined constant C or undefined elements in C with Replacement.
LLVM_ABI Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition Constant.h:65
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
LLVM_ABI Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
bool isLittleEndian() const
Layout endianness...
Definition DataLayout.h:220
unsigned getAddressSizeInBits(unsigned AS) const
The size in bits of an address in for the given AS.
Definition DataLayout.h:521
LLVM_ABI const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
LLVM_ABI unsigned getIndexTypeSizeInBits(Type *Ty) const
The size in bits of the index used in GEP calculation for this type.
LLVM_ABI unsigned getPointerTypeSizeInBits(Type *) const
The pointer representation size in bits for this type.
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
Definition DataLayout.h:794
ArrayRef< CondBrInst * > conditionsFor(const Value *V) const
Access the list of branches which affect this value.
DomTreeNodeBase * getIDom() const
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:122
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
This instruction extracts a struct member or array element value from an aggregate value.
ArrayRef< unsigned > getIndices() const
unsigned getNumIndices() const
static LLVM_ABI Type * getIndexedType(Type *Agg, ArrayRef< unsigned > Idxs)
Returns the type of the element that would be extracted with an extractvalue instruction with the spe...
This instruction compares its operands according to the predicate given to the constructor.
Utility class for floating point operations which can have information about relaxed accuracy require...
Definition Operator.h:202
Convenience struct for specifying and reasoning about fast-math flags.
Definition FMF.h:23
bool noSignedZeros() const
Definition FMF.h:67
bool noInfs() const
Definition FMF.h:66
void setNoSignedZeros(bool B=true)
Definition FMF.h:84
void setNoNaNs(bool B=true)
Definition FMF.h:78
bool noNaNs() const
Definition FMF.h:65
const BasicBlock & getEntryBlock() const
Definition Function.h:803
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
PointerType * getType() const
Global values are always pointers.
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this global belongs to.
Definition Globals.cpp:205
Type * getValueType() const
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
This instruction compares its operands according to the predicate given to the constructor.
CmpPredicate getSwappedCmpPredicate() const
CmpPredicate getInverseCmpPredicate() const
Predicate getFlippedSignednessPredicate() const
For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->EQ.
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
static LLVM_ABI std::optional< bool > isImpliedByMatchingCmp(CmpPredicate Pred1, CmpPredicate Pred2)
Determine if Pred1 implies Pred2 is true, false, or if nothing can be inferred about the implication,...
bool isRelational() const
Return true if the predicate is relational (not EQ or NE).
Predicate getUnsignedPredicate() const
For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
This instruction inserts a struct field of array element value into an aggregate value.
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
LLVM_ABI bool hasNoNaNs() const LLVM_READONLY
Determine whether the no-NaNs flag is set.
LLVM_ABI bool hasNoUnsignedWrap() const LLVM_READONLY
Determine whether the no unsigned wrap flag is set.
LLVM_ABI bool hasNoSignedWrap() const LLVM_READONLY
Determine whether the no signed wrap flag is set.
bool isBinaryOp() const
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI bool isExact() const LLVM_READONLY
Determine whether the exact flag is set.
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
LLVM_ABI bool comesBefore(const Instruction *Other) const
Given an instruction Other in the same basic block as this instruction, return true if this instructi...
iterator_range< user_iterator > users()
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
bool isUnaryOp() const
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
A wrapper class for inspecting calls to intrinsic functions.
bool isKnownNeverInfOrNaN() const
Return true if it's known this can never be an infinity or nan.
static LLVM_ABI KnownFPClass atan2(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode)
Report known values for atan2.
static LLVM_ABI KnownFPClass fma(const KnownFPClass &LHS, const KnownFPClass &RHS, const KnownFPClass &Addend, DenormalMode Mode)
Report known values for fma.
static LLVM_ABI KnownFPClass sin(const KnownFPClass &Src)
Report known values for sin.
static constexpr FPClassTest OrderedLessThanZeroMask
static LLVM_ABI KnownFPClass trunc(const KnownFPClass &Src, DenormalMode Mode)
Propagate known class for trunc.
static LLVM_ABI KnownFPClass fadd(const KnownFPClass &LHS, const KnownFPClass &RHS, RoundingMode RM, DenormalMode Mode)
Report known values for fadd.
bool isKnownNeverSubnormal() const
Return true if it's known this can never be a subnormal.
static LLVM_ABI KnownFPClass fsub(const KnownFPClass &LHS, const KnownFPClass &RHS, RoundingMode RM, DenormalMode Mode)
Report known values for fsub.
static LLVM_ABI KnownFPClass fma_square(const KnownFPClass &Squared, const KnownFPClass &Addend, DenormalMode Mode)
Report known values for fma squared, squared, addend.
KnownFPClass unionWith(const KnownFPClass &RHS) const
static LLVM_ABI KnownFPClass fadd_self(const KnownFPClass &Src, RoundingMode RM, DenormalMode Mode)
Report known values for fadd x, x.
LLVM_ABI bool isKnownNeverLogicalZero(DenormalMode Mode) const
Return true if it's known this can never be interpreted as a zero.
static LLVM_ABI KnownFPClass frem(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode)
Report known values for frem.
static LLVM_ABI KnownFPClass atan(const KnownFPClass &Src)
Report known values for atan.
static LLVM_ABI KnownFPClass ldexp(const KnownFPClass &Src, const APInt &ConstantRangeMin, const APInt &ConstantRangeMax, const fltSemantics &Flt, DenormalMode Mode)
Propagate known class for ldexp, assuming the exponent is known to be within [ConstantRangeMin,...
static LLVM_ABI KnownFPClass frexp_mant(const KnownFPClass &Src, DenormalMode Mode)
Propagate known class for mantissa component of frexp.
static LLVM_ABI KnownFPClass roundToIntegral(const KnownFPClass &Src, bool IsMultiUnitFPType, DenormalMode Mode)
Propagate known class for rounding intrinsics (rint, nearbyint, round, roundeven).
static LLVM_ABI KnownFPClass fmuladd(const KnownFPClass &LHS, const KnownFPClass &RHS, const KnownFPClass &Addend, DenormalMode Mode)
Report known values for fmuladd.
static LLVM_ABI KnownFPClass ceil(const KnownFPClass &Src, bool IsMultiUnitFPType, DenormalMode Mode)
Propagate known class for ceil.
static LLVM_ABI KnownFPClass fmuladd_square(const KnownFPClass &Squared, const KnownFPClass &Addend, DenormalMode Mode)
Report known values for fmuladd squared, squared, addend.
static LLVM_ABI KnownFPClass fpext(const KnownFPClass &KnownSrc, const fltSemantics &DstTy, const fltSemantics &SrcTy, DenormalMode DstMode, DenormalMode SrcMode)
Propagate known class for fpext.
static LLVM_ABI KnownFPClass cos(const KnownFPClass &Src)
Report known values for cos.
static LLVM_ABI KnownFPClass fptrunc(const KnownFPClass &KnownSrc, const fltSemantics &DstTy, const fltSemantics &SrcTy, DenormalMode DstMode, DenormalMode SrcMode)
Propagate known class for fptrunc and fptrunc_round.
static LLVM_ABI KnownFPClass cosh(const KnownFPClass &Src)
Report known values for cosh.
bool isUnknown() const
static LLVM_ABI KnownFPClass exp(const KnownFPClass &Src)
Report known values for exp, exp2 and exp10.
static LLVM_ABI KnownFPClass asin(const KnownFPClass &Src)
Report known values for asin.
bool isKnownNeverNaN() const
Return true if it's known this can never be a nan.
static LLVM_ABI KnownFPClass minMaxLike(const KnownFPClass &LHS, const KnownFPClass &RHS, MinMaxKind Kind, DenormalMode Mode)
void knownNot(FPClassMask RuleOut)
static LLVM_ABI KnownFPClass canonicalize(const KnownFPClass &Src, const fltSemantics &Flt, DenormalMode Mode)
Apply the canonicalize intrinsic to this value.
std::optional< bool > getSignBit() const
std::nullopt if the sign bit is unknown, true if the sign bit is definitely set or false if the sign ...
static LLVM_ABI KnownFPClass floor(const KnownFPClass &Src, bool IsMultiUnitFPType, DenormalMode Mode)
Propagate known class for floor.
FPClassTest getKnownFPClasses() const
Floating-point classes the value could be one of.
static KnownFPClass getEmpty()
static LLVM_ABI KnownFPClass tan(const KnownFPClass &Src)
Report known values for tan.
bool cannotBeOrderedLessThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never less than -...
void signBitMustBeOne()
Assume the sign bit is one.
static LLVM_ABI KnownFPClass fdiv_self(const KnownFPClass &Src, DenormalMode Mode)
Report known values for fdiv x, x.
void signBitMustBeZero()
Assume the sign bit is zero.
static LLVM_ABI KnownFPClass sqrt(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for sqrt.
LLVM_ABI bool isKnownNeverLogicalPosZero(DenormalMode Mode) const
Return true if it's known this can never be interpreted as a positive zero.
bool isKnownNever(FPClassMask Mask) const
Return true if it's known this can never be one of the mask entries.
bool isKnownNeverPosInfinity() const
Return true if it's known this can never be +infinity.
LLVM_ABI bool isKnownNeverLogicalNegZero(DenormalMode Mode) const
Return true if it's known this can never be interpreted as a negative zero.
static LLVM_ABI KnownFPClass bitcast(const fltSemantics &FltSemantics, const KnownBits &Bits)
Report known values for a bitcast into a float with provided semantics.
static LLVM_ABI KnownFPClass square(const KnownFPClass &Src, DenormalMode Mode)
static LLVM_ABI KnownFPClass acos(const KnownFPClass &Src)
Report known values for acos.
static LLVM_ABI KnownFPClass log(const KnownFPClass &Src, DenormalMode Mode, bool IsMultiUnitFPType)
Propagate known class for log/log2/log10.
static LLVM_ABI KnownFPClass frem_self(const KnownFPClass &Src, DenormalMode Mode)
Report known values for frem x, x. Valid for all rounding modes.
static LLVM_ABI KnownFPClass powi(const KnownFPClass &Src, const KnownBits &N)
Propagate known class for powi.
static LLVM_ABI KnownFPClass fdiv(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode)
Report known values for fdiv.
static LLVM_ABI KnownFPClass fmul(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode)
Report known values for fmul.
static LLVM_ABI KnownFPClass pow(const KnownFPClass &LHS, const KnownFPClass &RHS)
Propagate known class for pow.
static LLVM_ABI KnownFPClass sinh(const KnownFPClass &Src)
Report known values for sinh.
static LLVM_ABI KnownFPClass tanh(const KnownFPClass &Src)
Report known values for tanh.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:76
An instruction for reading from memory.
Value * getPointerOperand()
Align getAlign() const
Return the alignment of the access that is being performed.
bool isLoopHeader(const BlockT *BB) const
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
Metadata node.
Definition Metadata.h:1087
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1443
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
This is a utility class that provides an abstraction for the common functionality between Instruction...
Definition Operator.h:33
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
Definition Operator.h:43
Utility class for integer operators which may exhibit overflow - Add, Sub, Mul, and Shl.
Definition Operator.h:78
iterator_range< const_block_iterator > blocks() const
Value * getIncomingValueForBlock(const BasicBlock *BB) const
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
unsigned getAddressSpace() const
Return the address space of the Pointer type.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A udiv, sdiv, lshr, or ashr instruction, which can be marked as "exact", indicating that no bits are ...
Definition Operator.h:156
bool isExact() const
Test whether this division is known to be exact, with zero remainder.
Definition Operator.h:175
This class represents the LLVM 'select' instruction.
const Value * getFalseValue() const
const Value * getCondition() const
const Value * getTrueValue() const
This instruction constructs a fixed permutation of two input vectors.
VectorType * getType() const
Overload to return most specific vector type.
static LLVM_ABI void getShuffleMask(const Constant *Mask, SmallVectorImpl< int > &Result)
Convert the input shuffle mask operand to a vector of integers.
size_type size() const
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void reserve(size_type N)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h: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:680
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI bool haveNoCommonBitsSet(const WithCache< const Value * > &LHSCache, const WithCache< const Value * > &RHSCache, const SimplifyQuery &SQ)
Return true if LHS and RHS have no common bits set.
LLVM_ABI bool mustExecuteUBIfPoisonOnPathTo(Instruction *Root, Instruction *OnPathTo, DominatorTree *DT)
Return true if undefined behavior would provable be executed on the path to OnPathTo if Root produced...
LLVM_ABI Intrinsic::ID getInverseMinMaxIntrinsic(Intrinsic::ID MinMaxID)
@ Offset
Definition DWP.cpp:577
@ Length
Definition DWP.cpp:577
@ NeverOverflows
Never overflows.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
LLVM_ABI KnownFPClass computeKnownFPClass(const Value *V, const APInt &DemandedElts, FPClassTest InterestedClasses, const SimplifyQuery &SQ, unsigned Depth=0)
Determine which floating-point classes are valid for V, and return them in KnownFPClass bit sets.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1755
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1685
LLVM_ABI bool canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
LLVM_ABI bool mustTriggerUB(const Instruction *I, const SmallPtrSetImpl< const Value * > &KnownPoison)
Return true if the given instruction must trigger undefined behavior when I is executed with any oper...
LLVM_ABI bool isKnownNeverInfinity(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point scalar value is not an infinity or if the floating-point vector val...
LLVM_ABI void computeKnownBitsFromContext(const Value *V, KnownBits &Known, const SimplifyQuery &Q, unsigned Depth=0)
Merge bits known from context-dependent facts into Known.
RelativeUniformCounterPtr Values
Definition InstrProf.h:91
BundleAttr getBundleAttrFromOBU(OperandBundleUse OBU)
LLVM_ABI bool isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS, bool &TrueIfSigned)
Given an exploded icmp instruction, return true if the comparison only checks the sign bit.
NoCommonBitsSetResult
@ Known
Known to have no common set bits.
@ Unknown
Not known to have no common set bits.
@ OnlyIfUndefIgnored
Known to have no common set bits only if undef values are ignored.
LLVM_ABI bool isAssumeLikeIntrinsic(const Instruction *I)
Return true if it is an intrinsic that cannot be speculated but also cannot trap.
LLVM_ABI AllocaInst * findAllocaForValue(Value *V, bool OffsetZero=false)
Returns unique alloca where the value comes from, or nullptr.
LLVM_ABI APInt getMinMaxLimit(SelectPatternFlavor SPF, unsigned BitWidth)
Return the minimum or maximum constant value for the specified integer min/max flavor and type.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
@ Load
The value being inserted comes from a load (InsertElement only).
LLVM_ABI bool getConstantStringInfo(const Value *V, StringRef &Str, bool TrimAtNul=true)
This function computes the length of a null-terminated C string pointed to by V.
LLVM_ABI Constant * ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset)
LLVM_ABI Value * stripNullTest(Value *V)
Returns the inner value X if the expression has the form f(X) where f(X) == 0 if and only if X == 0,...
LLVM_ABI const Value * getArgumentAliasingToReturnedPointer(const CallBase *Call, bool MustPreserveOffset, bool MustPreserveProvenance=false)
This function returns call pointer argument that is considered the same by aliasing rules.
LLVM_ABI bool getUnderlyingObjectsForCodeGen(const Value *V, SmallVectorImpl< Value * > &Objects)
This is a wrapper around getUnderlyingObjects and adds support for basic ptrtoint+arithmetic+inttoptr...
LLVM_ABI std::pair< Intrinsic::ID, bool > canConvertToMinOrMaxIntrinsic(ArrayRef< Value * > VL)
Check if the values in VL are select instructions that can be converted to a min or max (vector) intr...
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ABI bool getConstantDataArrayInfo(const Value *V, ConstantDataArraySlice &Slice, unsigned ElementSize, uint64_t Offset=0)
Returns true if the value V is a pointer into a ConstantDataArray.
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition bit.h:325
LLVM_ABI bool isGuaranteedToExecuteForEveryIteration(const Instruction *I, const Loop *L)
Return true if this function can prove that the instruction I is executed for every iteration of the ...
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2229
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:244
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI bool assumeBundleImpliesNonNull(const Value *Val, const Function *Context, OperandBundleUse OBU)
LLVM_ABI bool mustSuppressSpeculation(const LoadInst &LI)
Return true if speculation of the given load must be suppressed to avoid ordering or interfering with...
Definition Loads.cpp:452
@ O1
Optimize quickly without destroying debuggability.
@ O2
Optimize for fast execution as much as possible without triggering significant incremental compile ti...
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition MathExtras.h:285
gep_type_iterator gep_type_end(const User *GEP)
int ilogb(const APFloat &Arg)
Returns the exponent of the internal representation of the APFloat.
Definition APFloat.h:1707
LLVM_ABI bool isSafeToSpeculativelyExecute(const Instruction *I, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true, bool IgnoreUBImplyingAttrs=true)
Return true if the instruction does not have any effects besides calculating the result and does not ...
LLVM_ABI Value * getSplatValue(const Value *V)
Get splat value if the input is a splat vector or return nullptr.
LLVM_ABI CmpInst::Predicate getMinMaxPred(SelectPatternFlavor SPF, bool Ordered=false)
Return the canonical comparison predicate for the specified minimum/maximum flavor.
bool isa_and_nonnull(const Y &Val)
Definition Casting.h:676
LLVM_ABI bool isKnownNegative(const WithCache< const Value * > &V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be negative (i.e.
LLVM_ABI unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Return the number of times the sign bit of the register is replicated into the other bits.
LLVM_ABI bool canIgnoreSignBitOfZero(const Use &U)
Return true if the sign bit of the FP value can be ignored by the user when the value is zero.
LLVM_ABI bool isGuaranteedNotToBeUndef(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be undef, but may be poison.
LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
LLVM_ABI unsigned ComputeMaxSignificantBits(const Value *Op, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Get the upper bound on bit size for this Value Op as a signed integer.
std::tuple< Value *, FPClassTest, FPClassTest > fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS, FPClassTest RHSClass, bool LookThroughSrc=true)
const Value * getPointerOperand(const Value *V)
A helper function that returns the pointer operand of a load, store or GEP instruction.
LLVM_ABI bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Return true if the given value is known to have exactly one bit set when defined.
LLVM_ABI bool MaskedValueIsZero(const Value *V, const APInt &Mask, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if 'V & Mask' is known to be zero.
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:204
LLVM_ABI bool isOverflowIntrinsicNoWrap(const WithOverflowInst *WO, const DominatorTree &DT)
Returns true if the arithmetic part of the WO 's result is used only along the paths control dependen...
LLVM_ABI bool matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO, Value *&Start, Value *&Step)
Attempt to match a simple first order recurrence cycle of the form: iv = phi Ty [Start,...
LLVM_ABI bool isValidAssumeForContext(const Instruction *I, const Instruction *CtxI, const DominatorTree *DT=nullptr, bool AllowEphemerals=false)
Return true if it is valid to use the assumptions provided by an assume intrinsic,...
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1762
LLVM_ABI OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ, bool IsNSW=false)
LLVM_ABI bool getShuffleDemandedElts(int SrcWidth, ArrayRef< int > Mask, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS, bool AllowUndefElts=false)
Transform a shuffle mask's output demanded element mask into demanded element masks for the 2 operand...
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:326
LLVM_ABI bool isGuard(const User *U)
Returns true iff U has semantics of a guard expressed in a form of call of llvm.experimental....
int countl_zero(T Val)
Count number of 0's from the most significant bit to the least stopping at the first 1.
Definition bit.h:263
LLVM_ABI SelectPatternFlavor getInverseMinMaxFlavor(SelectPatternFlavor SPF)
Return the inverse minimum/maximum flavor of the specified flavor.
constexpr unsigned MaxAnalysisRecursionDepth
LLVM_ABI void adjustKnownBitsForSelectArm(KnownBits &Known, Value *Cond, Value *Arm, bool Invert, const SimplifyQuery &Q, unsigned Depth=0)
Adjust Known for the given select Arm to include information from the select Cond.
LLVM_ABI NoCommonBitsSetResult getNoCommonBitsSetResult(const WithCache< const Value * > &LHSCache, const WithCache< const Value * > &RHSCache, const SimplifyQuery &SQ)
Return how strongly LHS and RHS are known to have no common set bits.
LLVM_ABI OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
SelectPatternFlavor
Specific patterns of select instructions we can match.
@ SPF_ABS
Floating point maxnum.
@ SPF_NABS
Absolute value.
@ SPF_FMAXNUM
Floating point minnum.
@ SPF_UMIN
Signed minimum.
@ SPF_UMAX
Signed maximum.
@ SPF_SMAX
Unsigned minimum.
@ SPF_UNKNOWN
@ SPF_FMINNUM
Unsigned maximum.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:280
LLVM_ABI bool isKnownPositive(const WithCache< const Value * > &V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be positive (i.e.
LLVM_ABI bool impliesPoison(const Value *ValAssumedPoison, const Value *V)
Return true if V is poison given that ValAssumedPoison is already poison.
LLVM_ABI void getHorizDemandedEltsForFirstOperand(unsigned VectorBitWidth, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS)
Compute the demanded elements mask of horizontal binary operations.
LLVM_ABI SelectPatternResult getSelectPattern(CmpInst::Predicate Pred, SelectPatternNaNBehavior NaNBehavior=SPNB_NA, bool Ordered=false)
Determine the pattern for predicate X Pred Y ? X : Y.
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI bool programUndefinedIfPoison(const Instruction *Inst)
LLVM_ABI bool matchSimpleBinaryIntrinsicRecurrence(const IntrinsicInst *I, PHINode *&P, Value *&Init, Value *&OtherOp)
Attempt to match a simple value-accumulating recurrence of the form: llvm.intrinsic....
LLVM_ABI bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
LLVM_ABI bool cannotBeNegativeZero(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if we can prove that the specified FP value is never equal to -0.0.
LLVM_ABI bool programUndefinedIfUndefOrPoison(const Instruction *Inst)
Return true if this function can prove that if Inst is executed and yields a poison value or undef bi...
LLVM_ABI void adjustKnownFPClassForSelectArm(KnownFPClass &Known, Value *Cond, Value *Arm, bool Invert, const SimplifyQuery &Q, unsigned Depth=0)
Adjust Known for the given select Arm to include information from the select Cond.
generic_gep_type_iterator<> gep_type_iterator
LLVM_ABI bool collectPossibleValues(const Value *V, SmallPtrSetImpl< const Constant * > &Constants, unsigned MaxCount, bool AllowUndefOrPoison=true)
Enumerates all possible immediate values of V and inserts them into the set Constants.
constexpr unsigned MaxLookupSearchDepth
The max limit of the search depth in DecomposeGEPExpression() and getUnderlyingObject().
LLVM_ABI uint64_t GetStringLength(const Value *V, unsigned CharSize=8)
If we can compute the length of the string pointed to by the specified pointer, return 'len+1'.
LLVM_ABI OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
LLVM_ABI ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
LLVM_ABI Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
LLVM_ABI bool canCreateUndefOrPoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
canCreateUndefOrPoison returns true if Op can create undef or poison from non-undef & non-poison oper...
LLVM_ABI bool matchSimpleTernaryIntrinsicRecurrence(const IntrinsicInst *I, PHINode *&P, Value *&Init, Value *&OtherOp0, Value *&OtherOp1)
Attempt to match a simple value-accumulating recurrence of the form: llvm.intrinsic....
LLVM_ABI EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
LLVM_ABI const Value * getUnderlyingObjectAggressive(const Value *V, bool MustPreserveProvenance=false)
Like getUnderlyingObject(), but will try harder to find a single underlying object.
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth, bool MustPreserveProvenance=false)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
LLVM_ABI bool isKnownInversion(const Value *X, const Value *Y)
Return true iff:
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI bool intrinsicPropagatesPoison(Intrinsic::ID IID)
Return whether this intrinsic propagates poison for all operands.
LLVM_ABI bool isNotCrossLaneOperation(const Instruction *I)
Return true if the instruction doesn't potentially cross vector lanes.
bool includesPoison(UndefPoisonKind Kind)
Returns true if Kind includes the Poison bit.
Definition UndefPoison.h:27
LLVM_ABI bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth=0)
Return true if the given value is known to be non-zero when defined.
constexpr int PoisonMaskElem
LLVM_ABI RetainedKnowledge getKnowledgeValidInContext(const Value *V, ArrayRef< Attribute::AttrKind > AttrKinds, AssumptionCache &AC, const Instruction *CtxI, const DominatorTree *DT=nullptr)
Return a valid Knowledge associated to the Value V if its Attribute kind is in AttrKinds and the know...
LLVM_ABI bool isSafeToSpeculativelyExecuteWithOpcode(unsigned Opcode, const Instruction *Inst, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true, bool IgnoreUBImplyingAttrs=true)
This returns the same result as isSafeToSpeculativelyExecute if Opcode is the actual opcode of Inst.
LLVM_ABI Intrinsic::ID getIntrinsicForCallSite(const CallBase &CB, const TargetLibraryInfo *TLI)
Map a call instruction to an intrinsic ID.
@ Other
Any other memory.
Definition ModRef.h:68
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
LLVM_ABI Intrinsic::ID getMinMaxIntrinsic(SelectPatternFlavor SPF)
Convert given SPF to equivalent min/max intrinsic.
bool includesUndef(UndefPoisonKind Kind)
Returns true if Kind includes the Undef bit.
Definition UndefPoison.h:33
LLVM_ABI SelectPatternResult matchDecomposedSelectPattern(CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, FastMathFlags FMF=FastMathFlags(), Instruction::CastOps *CastOp=nullptr)
Determine the pattern that a select with the given compare as its predicate and given values as its t...
LLVM_ABI bool isOnlyUsedInZeroComparison(const Instruction *CtxI)
LLVM_ABI OverflowResult computeOverflowForSignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
LLVM_ABI bool propagatesPoison(const Use &PoisonOp)
Return true if PoisonOp's user yields poison or raises UB if its operand PoisonOp is poison.
@ Add
Sum of integers.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
LLVM_ABI bool isOnlyUsedInZeroEqualityComparison(const Instruction *CtxI)
LLVM_ABI ConstantRange computeConstantRangeIncludingKnownBits(const WithCache< const Value * > &V, bool ForSigned, const SimplifyQuery &SQ)
Combine constant ranges from computeConstantRange() and computeKnownBits().
SelectPatternNaNBehavior
Behavior when a floating point min/max is given one NaN and one non-NaN as input.
@ SPNB_RETURNS_NAN
NaN behavior not applicable.
@ SPNB_RETURNS_OTHER
Given one NaN input, returns the NaN.
@ SPNB_RETURNS_ANY
Given one NaN input, returns the non-NaN.
LLVM_ABI bool isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(const CallBase *Call, bool MustPreserveOffset, bool MustPreserveProvenance=false)
launder.invariant.group and similar intrinsics return a pointer that aliases their argument,...
LLVM_ABI bool isKnownNonEqual(const Value *V1, const Value *V2, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the given values are known to be non-equal when defined.
DWARFExpression::Operation Op
LLVM_ABI bool isDereferenceableAndAlignedPointer(const Value *V, Type *Ty, Align Alignment, const SimplifyQuery &Q, bool IgnoreFree=false)
Returns true if V is always a dereferenceable pointer with alignment greater or equal than requested.
Definition Loads.cpp:246
RoundingMode
Rounding mode.
@ NearestTiesToEven
roundTiesToEven.
@ Dynamic
Denotes mode unknown at compile time.
LLVM_ABI bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI bool willNotFreeBetween(const Instruction *Assume, const Instruction *CtxI, const DominatorTree *DT=nullptr)
Returns true, if no instruction between Assume and CtxI may free (including through synchronization).
constexpr unsigned BitWidth
LLVM_ABI KnownBits analyzeKnownBitsFromAndXorOr(const Operator *I, const KnownBits &KnownLHS, const KnownBits &KnownRHS, const SimplifyQuery &SQ, unsigned Depth=0)
Using KnownBits LHS/RHS produce the known bits for logic op (and/xor/or).
LLVM_ABI OverflowResult computeOverflowForUnsignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
LLVM_ABI bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I)
Return true if this function can prove that the instruction I will always transfer execution to one o...
LLVM_ABI bool isKnownNeverInfOrNaN(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point value can never contain a NaN or infinity.
LLVM_ABI AssumeRangeInfo getAssumeRangeInfo(OperandBundleUse)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI bool isKnownNeverNaN(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point scalar value is not a NaN or if the floating-point vector value has...
gep_type_iterator gep_type_begin(const User *GEP)
UndefPoisonKind
Enumeration to track whether we are interested in Undef, Poison, or both.
Definition UndefPoison.h:20
LLVM_ABI Value * isBytewiseValue(Value *V, const DataLayout &DL)
If the specified value can be set by repeating the same byte in memory, return the i8 value that it i...
auto predecessors(const MachineBasicBlock *BB)
LLVM_ABI std::optional< std::pair< CmpPredicate, Constant * > > getFlippedStrictnessPredicateAndConstant(CmpPredicate Pred, Constant *C)
Convert an integer comparison with a constant RHS into an equivalent form with the strictness flipped...
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1963
LLVM_ABI bool isKnownIntegral(const Value *V, const SimplifyQuery &SQ, FastMathFlags FMF)
Return true if the floating-point value V is known to be an integer value.
LLVM_ABI AssumeAlignInfo getAssumeAlignInfo(OperandBundleUse)
bool pred_empty(const BasicBlock *BB)
Definition CFG.h:107
LLVM_ABI OverflowResult computeOverflowForUnsignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:202
LLVM_ABI std::optional< bool > isImpliedByDomCondition(const Value *Cond, const Instruction *ContextI, const DataLayout &DL)
Return the boolean condition value in the context of the given instruction if it is known based on do...
LLVM_ABI bool isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be poison, but may be undef.
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:320
LLVM_ABI void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, KnownBits &Known)
Compute known bits from the range metadata.
LLVM_ABI Value * FindInsertedValue(Value *V, ArrayRef< unsigned > idx_range, std::optional< BasicBlock::iterator > InsertBefore=std::nullopt)
Given an aggregate and an sequence of indices, see if the scalar value indexed is already around as a...
LLVM_ABI bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW=false, bool AllowPoison=true)
Return true if the two given values are negation.
LLVM_ABI Constant * ConstantFoldIntegerCast(Constant *C, Type *DestTy, bool IsSigned, const DataLayout &DL)
Constant fold a zext, sext or trunc, depending on IsSigned and whether the DestTy is wider or narrowe...
LLVM_ABI bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the give value is known to be non-negative.
LLVM_ABI bool cannotBeOrderedLessThanZero(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if we can prove that the specified FP value is either NaN or never less than -0....
LLVM_ABI void getUnderlyingObjects(const Value *V, SmallVectorImpl< const Value * > &Objects, const LoopInfo *LI=nullptr, unsigned MaxLookup=MaxLookupSearchDepth)
This method is similar to getUnderlyingObject except that it can look through phi and select instruct...
LLVM_ABI SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
LLVM_ABI bool mayHaveNonDefUseDependency(const Instruction &I)
Returns true if the result or effects of the given instructions I depend values not reachable through...
LLVM_ABI bool isTriviallyVectorizable(Intrinsic::ID ID)
Identify if the intrinsic is trivially vectorizable.
LLVM_ABI bool isIdentifiedObject(const Value *V)
Return true if this pointer refers to a distinct and identifiable object.
LLVM_ABI std::optional< bool > isImpliedCondition(const Value *LHS, const Value *RHS, const DataLayout &DL, bool LHSIsTrue=true, unsigned Depth=0)
Return true if RHS is known to be implied true by LHS.
LLVM_ABI std::optional< bool > computeKnownFPSignBit(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return false if we can prove that the specified FP value's sign bit is 0.
LLVM_ABI bool canIgnoreSignBitOfNaN(const Use &U)
Return true if the sign bit of the FP value can be ignored by the user when the value is NaN.
LLVM_ABI ConstantRange computeConstantRange(const Value *V, bool ForSigned, const SimplifyQuery &SQ, unsigned Depth=0)
Determine the possible constant range of an integer or vector of integer value.
LLVM_ABI void findValuesAffectedByCondition(Value *Cond, bool IsAssume, function_ref< void(Value *)> InsertAffected)
Call InsertAffected on all Values whose known bits / value may be affected by the condition Cond.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:881
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
SmallPtrSet< Value *, 4 > AffectedValues
Represents offset+length into a ConstantDataArray.
const ConstantDataArray * Array
ConstantDataArray pointer.
Represent subnormal handling kind for floating point instruction inputs and outputs.
static constexpr DenormalMode getDynamic()
InstrInfoQuery provides an interface to query additional information for instructions like metadata o...
bool isExact(const BinaryOperator *Op) const
MDNode * getMetadata(const Instruction *I, unsigned KindID) const
bool hasNoSignedZeros(const InstT *Op) const
bool hasNoSignedWrap(const InstT *Op) const
bool hasNoUnsignedWrap(const InstT *Op) const
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition KnownBits.h:315
static LLVM_ABI KnownBits sadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)
KnownBits anyextOrTrunc(unsigned BitWidth) const
Return known bits for an "any" extension or truncation of the value we're tracking.
Definition KnownBits.h:190
static LLVM_ABI KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition KnownBits.h:269
static LLVM_ABI KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition KnownBits.h:106
bool isZero() const
Returns true if value is all zero.
Definition KnownBits.h:78
LLVM_ABI KnownBits blsi() const
Compute known bits for X & -X, which has only the lowest bit set of X set.
void makeNonNegative()
Make this value non-negative.
Definition KnownBits.h:125
static LLVM_ABI KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
unsigned countMinLeadingOnes() const
Returns the minimum number of leading one bits.
Definition KnownBits.h:265
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition KnownBits.h:256
static LLVM_ABI KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
static LLVM_ABI KnownBits ssub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.ssub.sat(LHS, RHS)
static LLVM_ABI KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
bool isUnknown() const
Returns true if we don't know any bits.
Definition KnownBits.h:64
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition KnownBits.h:288
LLVM_ABI KnownBits blsmsk() const
Compute known bits for X ^ (X - 1), which has all bits up to and including the lowest set bit of X se...
LLVM_ABI KnownBits ctlz(bool ZeroIsPoison=false) const
Compute known bits for the count of leading zeros.
KnownBits byteSwap() const
Definition KnownBits.h:562
bool hasConflict() const
Returns true if there is conflicting information.
Definition KnownBits.h:51
static LLVM_ABI KnownBits fshl(const KnownBits &LHS, const KnownBits &RHS, const APInt &Amt)
Compute known bits for fshl(LHS, RHS, Amt).
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition KnownBits.h:303
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition KnownBits.h:84
KnownBits reverseBits() const
Definition KnownBits.h:566
unsigned getBitWidth() const
Get the bit width of this value.
Definition KnownBits.h:44
static LLVM_ABI KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition KnownBits.h:176
bool isConstant() const
Returns true if we know the value of all bits.
Definition KnownBits.h:54
static KnownBits add(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false, bool SelfAdd=false)
Compute knownbits resulting from addition of LHS and RHS.
Definition KnownBits.h:361
KnownBits unionWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for either this or RHS or both.
Definition KnownBits.h:335
static LLVM_ABI KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition KnownBits.h:109
bool isEven() const
Return if the value is known even (the low bit is 0).
Definition KnownBits.h:162
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition KnownBits.h:239
static LLVM_ABI KnownBits pdep(const KnownBits &Val, const KnownBits &Mask)
Compute known bits for pdep(Val, Mask).
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition KnownBits.h:325
unsigned countMinTrailingOnes() const
Returns the minimum number of trailing one bits.
Definition KnownBits.h:259
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition KnownBits.h:262
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:146
static LLVM_ABI KnownBits fshr(const KnownBits &LHS, const KnownBits &RHS, const APInt &Amt)
Compute known bits for fshr(LHS, RHS, Amt).
static LLVM_ABI KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
static LLVM_ABI KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
static LLVM_ABI KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
static LLVM_ABI KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
APInt getMinValue() const
Return the minimal unsigned value possible given these KnownBits.
Definition KnownBits.h:130
static LLVM_ABI KnownBits computeForAddSub(bool Add, bool NSW, bool NUW, const KnownBits &LHS, const KnownBits &RHS)
Compute known bits resulting from adding LHS and RHS.
Definition KnownBits.cpp:62
static LLVM_ABI KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition KnownBits.h:340
bool isNegative() const
Returns true if this value is known to be negative.
Definition KnownBits.h:103
static KnownBits sub(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from subtraction of LHS and RHS.
Definition KnownBits.h:376
void setAllOnes()
Make all bits known to be one and discard any previous information.
Definition KnownBits.h:90
static LLVM_ABI KnownBits uadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.uadd.sat(LHS, RHS)
static LLVM_ABI KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
KnownBits anyext(unsigned BitWidth) const
Return known bits for an "any" extension of the value we're tracking, where we don't know anything ab...
Definition KnownBits.h:171
static LLVM_ABI KnownBits clmul(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for clmul(LHS, RHS).
LLVM_ABI KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
static LLVM_ABI std::optional< bool > sgt(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SGT result.
static LLVM_ABI std::optional< bool > uge(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_UGE result.
static LLVM_ABI KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
static LLVM_ABI KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
static LLVM_ABI KnownBits pext(const KnownBits &Val, const KnownBits &Mask)
Compute known bits for pext(Val, Mask).
KnownBits sextOrTrunc(unsigned BitWidth) const
Return known bits for a sign extension or truncation of the value we're tracking.
Definition KnownBits.h:210
SelectPatternFlavor Flavor
static bool isMinOrMax(SelectPatternFlavor SPF)
When implementing this min/max pattern as fcmp; select, does the fcmp have to be ordered?
const DataLayout & DL
SimplifyQuery getWithoutCondContext() const
const DominatorTree * DT
SimplifyQuery getWithInstruction(const Instruction *I) const
AssumptionCache * AC
const DomConditionCache * DC
const Instruction * CtxI
const Function * getFunction() const
const InstrInfoQuery IIQ
const CondContext * CC
fltNanEncoding nanEncoding
Definition APFloat.h:1041