LLVM 23.0.0git
ValueTracking.cpp
Go to the documentation of this file.
1//===- ValueTracking.cpp - Walk computations to compute properties --------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains routines that help analyze properties that chains of
10// computations have.
11//
12//===----------------------------------------------------------------------===//
13
15#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/ScopeExit.h"
23#include "llvm/ADT/StringRef.h"
33#include "llvm/Analysis/Loads.h"
38#include "llvm/IR/Argument.h"
39#include "llvm/IR/Attributes.h"
40#include "llvm/IR/BasicBlock.h"
42#include "llvm/IR/Constant.h"
45#include "llvm/IR/Constants.h"
48#include "llvm/IR/Dominators.h"
50#include "llvm/IR/Function.h"
52#include "llvm/IR/GlobalAlias.h"
53#include "llvm/IR/GlobalValue.h"
55#include "llvm/IR/InstrTypes.h"
56#include "llvm/IR/Instruction.h"
59#include "llvm/IR/Intrinsics.h"
60#include "llvm/IR/IntrinsicsAArch64.h"
61#include "llvm/IR/IntrinsicsAMDGPU.h"
62#include "llvm/IR/IntrinsicsRISCV.h"
63#include "llvm/IR/IntrinsicsX86.h"
64#include "llvm/IR/LLVMContext.h"
65#include "llvm/IR/Metadata.h"
66#include "llvm/IR/Module.h"
67#include "llvm/IR/Operator.h"
69#include "llvm/IR/Type.h"
70#include "llvm/IR/User.h"
71#include "llvm/IR/Value.h"
81#include <algorithm>
82#include <cassert>
83#include <cstdint>
84#include <optional>
85#include <utility>
86
87using namespace llvm;
88using namespace llvm::PatternMatch;
89
90// Controls the number of uses of the value searched for possible
91// dominating comparisons.
92static cl::opt<unsigned> DomConditionsMaxUses("dom-conditions-max-uses",
93 cl::Hidden, cl::init(20));
94
95/// Maximum number of instructions to check between assume and context
96/// instruction.
97static constexpr unsigned MaxInstrsToCheckForFree = 16;
98
99/// Returns the bitwidth of the given scalar or pointer type. For vector types,
100/// returns the element type's bitwidth.
101static unsigned getBitWidth(Type *Ty, const DataLayout &DL) {
102 if (unsigned BitWidth = Ty->getScalarSizeInBits())
103 return BitWidth;
104
105 return DL.getPointerTypeSizeInBits(Ty);
106}
107
108// Given the provided Value and, potentially, a context instruction, return
109// the preferred context instruction (if any).
110static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI) {
111 // If we've been provided with a context instruction, then use that (provided
112 // it has been inserted).
113 if (CxtI && CxtI->getParent())
114 return CxtI;
115
116 // If the value is really an already-inserted instruction, then use that.
117 CxtI = dyn_cast<Instruction>(V);
118 if (CxtI && CxtI->getParent())
119 return CxtI;
120
121 return nullptr;
122}
123
125 const APInt &DemandedElts,
126 APInt &DemandedLHS, APInt &DemandedRHS) {
127 if (isa<ScalableVectorType>(Shuf->getType())) {
128 assert(DemandedElts == APInt(1,1));
129 DemandedLHS = DemandedRHS = DemandedElts;
130 return true;
131 }
132
133 int NumElts =
134 cast<FixedVectorType>(Shuf->getOperand(0)->getType())->getNumElements();
135 return llvm::getShuffleDemandedElts(NumElts, Shuf->getShuffleMask(),
136 DemandedElts, DemandedLHS, DemandedRHS);
137}
138
139static void computeKnownBits(const Value *V, const APInt &DemandedElts,
140 KnownBits &Known, const SimplifyQuery &Q,
141 unsigned Depth);
142
144 const SimplifyQuery &Q, unsigned Depth) {
145 // Since the number of lanes in a scalable vector is unknown at compile time,
146 // we track one bit which is implicitly broadcast to all lanes. This means
147 // that all lanes in a scalable vector are considered demanded.
148 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
149 APInt DemandedElts =
150 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
151 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);
152}
153
155 const DataLayout &DL, AssumptionCache *AC,
156 const Instruction *CxtI, const DominatorTree *DT,
157 bool UseInstrInfo, unsigned Depth) {
158 computeKnownBits(V, Known,
159 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo),
160 Depth);
161}
162
164 AssumptionCache *AC, const Instruction *CxtI,
165 const DominatorTree *DT, bool UseInstrInfo,
166 unsigned Depth) {
167 return computeKnownBits(
168 V, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
169}
170
171KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
172 const DataLayout &DL, AssumptionCache *AC,
173 const Instruction *CxtI,
174 const DominatorTree *DT, bool UseInstrInfo,
175 unsigned Depth) {
176 return computeKnownBits(
177 V, DemandedElts,
178 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
179}
180
182 const SimplifyQuery &SQ) {
183 // Look for an inverted mask: (X & ~M) op (Y & M).
184 {
185 Value *M;
186 if (match(LHS, m_c_And(m_Not(m_Value(M)), m_Value())) &&
188 isGuaranteedNotToBeUndef(M, SQ.AC, SQ.CxtI, SQ.DT))
189 return true;
190 }
191
192 // X op (Y & ~X)
195 return true;
196
197 // X op ((X & Y) ^ Y) -- this is the canonical form of the previous pattern
198 // for constant Y.
199 Value *Y;
200 if (match(RHS,
202 isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT) &&
203 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
204 return true;
205
206 // Peek through extends to find a 'not' of the other side:
207 // (ext Y) op ext(~Y)
208 if (match(LHS, m_ZExtOrSExt(m_Value(Y))) &&
210 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
211 return true;
212
213 // Look for: (A & B) op ~(A | B)
214 {
215 Value *A, *B;
216 if (match(LHS, m_And(m_Value(A), m_Value(B))) &&
218 isGuaranteedNotToBeUndef(A, SQ.AC, SQ.CxtI, SQ.DT) &&
219 isGuaranteedNotToBeUndef(B, SQ.AC, SQ.CxtI, SQ.DT))
220 return true;
221 }
222
223 // Look for: (X << V) op (Y >> (BitWidth - V))
224 // or (X >> V) op (Y << (BitWidth - V))
225 {
226 const Value *V;
227 const APInt *R;
228 if (((match(RHS, m_Shl(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&
229 match(LHS, m_LShr(m_Value(), m_Specific(V)))) ||
230 (match(RHS, m_LShr(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&
231 match(LHS, m_Shl(m_Value(), m_Specific(V))))) &&
232 R->uge(LHS->getType()->getScalarSizeInBits()))
233 return true;
234 }
235
236 return false;
237}
238
240 const WithCache<const Value *> &RHSCache,
241 const SimplifyQuery &SQ) {
242 const Value *LHS = LHSCache.getValue();
243 const Value *RHS = RHSCache.getValue();
244
245 assert(LHS->getType() == RHS->getType() &&
246 "LHS and RHS should have the same type");
247 assert(LHS->getType()->isIntOrIntVectorTy() &&
248 "LHS and RHS should be integers");
249
250 if (haveNoCommonBitsSetSpecialCases(LHS, RHS, SQ) ||
252 return true;
253
255 RHSCache.getKnownBits(SQ));
256}
257
259 return !I->user_empty() &&
260 all_of(I->users(), match_fn(m_ICmp(m_Value(), m_Zero())));
261}
262
264 return !I->user_empty() && all_of(I->users(), [](const User *U) {
265 CmpPredicate P;
266 return match(U, m_ICmp(P, m_Value(), m_Zero())) && ICmpInst::isEquality(P);
267 });
268}
269
271 bool OrZero, AssumptionCache *AC,
272 const Instruction *CxtI,
273 const DominatorTree *DT, bool UseInstrInfo,
274 unsigned Depth) {
275 return ::isKnownToBeAPowerOfTwo(
276 V, OrZero, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo),
277 Depth);
278}
279
280static bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
281 const SimplifyQuery &Q, unsigned Depth);
282
284 unsigned Depth) {
285 return computeKnownBits(V, SQ, Depth).isNonNegative();
286}
287
289 unsigned Depth) {
290 if (auto *CI = dyn_cast<ConstantInt>(V))
291 return CI->getValue().isStrictlyPositive();
292
293 // If `isKnownNonNegative` ever becomes more sophisticated, make sure to keep
294 // this updated.
295 KnownBits Known = computeKnownBits(V, SQ, Depth);
296 return Known.isNonNegative() &&
297 (Known.isNonZero() || isKnownNonZero(V, SQ, Depth));
298}
299
301 unsigned Depth) {
302 return computeKnownBits(V, SQ, Depth).isNegative();
303}
304
305static bool isKnownNonEqual(const Value *V1, const Value *V2,
306 const APInt &DemandedElts, const SimplifyQuery &Q,
307 unsigned Depth);
308
309bool llvm::isKnownNonEqual(const Value *V1, const Value *V2,
310 const SimplifyQuery &Q, unsigned Depth) {
311 // We don't support looking through casts.
312 if (V1 == V2 || V1->getType() != V2->getType())
313 return false;
314 auto *FVTy = dyn_cast<FixedVectorType>(V1->getType());
315 APInt DemandedElts =
316 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
317 return ::isKnownNonEqual(V1, V2, DemandedElts, Q, Depth);
318}
319
320bool llvm::MaskedValueIsZero(const Value *V, const APInt &Mask,
321 const SimplifyQuery &SQ, unsigned Depth) {
322 KnownBits Known(Mask.getBitWidth());
323 computeKnownBits(V, Known, SQ, Depth);
324 return Mask.isSubsetOf(Known.Zero);
325}
326
327static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
328 const SimplifyQuery &Q, unsigned Depth);
329
330static unsigned ComputeNumSignBits(const Value *V, const SimplifyQuery &Q,
331 unsigned Depth = 0) {
332 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
333 APInt DemandedElts =
334 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
335 return ComputeNumSignBits(V, DemandedElts, Q, Depth);
336}
337
338unsigned llvm::ComputeNumSignBits(const Value *V, const DataLayout &DL,
339 AssumptionCache *AC, const Instruction *CxtI,
340 const DominatorTree *DT, bool UseInstrInfo,
341 unsigned Depth) {
342 return ::ComputeNumSignBits(
343 V, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
344}
345
347 AssumptionCache *AC,
348 const Instruction *CxtI,
349 const DominatorTree *DT,
350 unsigned Depth) {
351 unsigned SignBits = ComputeNumSignBits(V, DL, AC, CxtI, DT, Depth);
352 return V->getType()->getScalarSizeInBits() - SignBits + 1;
353}
354
355/// Try to detect the lerp pattern: a * (b - c) + c * d
356/// where a >= 0, b >= 0, c >= 0, d >= 0, and b >= c.
357///
358/// In that particular case, we can use the following chain of reasoning:
359///
360/// a * (b - c) + c * d <= a' * (b - c) + a' * c = a' * b where a' = max(a, d)
361///
362/// Since that is true for arbitrary a, b, c and d within our constraints, we
363/// can conclude that:
364///
365/// max(a * (b - c) + c * d) <= max(max(a), max(d)) * max(b) = U
366///
367/// Considering that any result of the lerp would be less or equal to U, it
368/// would have at least the number of leading 0s as in U.
369///
370/// While being quite a specific situation, it is fairly common in computer
371/// graphics in the shape of alpha blending.
372///
373/// Modifies given KnownOut in-place with the inferred information.
374static void computeKnownBitsFromLerpPattern(const Value *Op0, const Value *Op1,
375 const APInt &DemandedElts,
376 KnownBits &KnownOut,
377 const SimplifyQuery &Q,
378 unsigned Depth) {
379
380 Type *Ty = Op0->getType();
381 const unsigned BitWidth = Ty->getScalarSizeInBits();
382
383 // Only handle scalar types for now
384 if (Ty->isVectorTy())
385 return;
386
387 // Try to match: a * (b - c) + c * d.
388 // When a == 1 => A == nullptr, the same applies to d/D as well.
389 const Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
390 const Instruction *SubBC = nullptr;
391
392 const auto MatchSubBC = [&]() {
393 // (b - c) can have two forms that interest us:
394 //
395 // 1. sub nuw %b, %c
396 // 2. xor %c, %b
397 //
398 // For the first case, nuw flag guarantees our requirement b >= c.
399 //
400 // The second case might happen when the analysis can infer that b is a mask
401 // for c and we can transform sub operation into xor (that is usually true
402 // for constant b's). Even though xor is symmetrical, canonicalization
403 // ensures that the constant will be the RHS. We have additional checks
404 // later on to ensure that this xor operation is equivalent to subtraction.
406 m_Xor(m_Value(C), m_Value(B))));
407 };
408
409 const auto MatchASubBC = [&]() {
410 // Cases:
411 // - a * (b - c)
412 // - (b - c) * a
413 // - (b - c) <- a implicitly equals 1
414 return m_CombineOr(m_c_Mul(m_Value(A), MatchSubBC()), MatchSubBC());
415 };
416
417 const auto MatchCD = [&]() {
418 // Cases:
419 // - d * c
420 // - c * d
421 // - c <- d implicitly equals 1
423 };
424
425 const auto Match = [&](const Value *LHS, const Value *RHS) {
426 // We do use m_Specific(C) in MatchCD, so we have to make sure that
427 // it's bound to anything and match(LHS, MatchASubBC()) absolutely
428 // has to evaluate first and return true.
429 //
430 // If Match returns true, it is guaranteed that B != nullptr, C != nullptr.
431 return match(LHS, MatchASubBC()) && match(RHS, MatchCD());
432 };
433
434 if (!Match(Op0, Op1) && !Match(Op1, Op0))
435 return;
436
437 const auto ComputeKnownBitsOrOne = [&](const Value *V) {
438 // For some of the values we use the convention of leaving
439 // it nullptr to signify an implicit constant 1.
440 return V ? computeKnownBits(V, DemandedElts, Q, Depth + 1)
442 };
443
444 // Check that all operands are non-negative
445 const KnownBits KnownA = ComputeKnownBitsOrOne(A);
446 if (!KnownA.isNonNegative())
447 return;
448
449 const KnownBits KnownD = ComputeKnownBitsOrOne(D);
450 if (!KnownD.isNonNegative())
451 return;
452
453 const KnownBits KnownB = computeKnownBits(B, DemandedElts, Q, Depth + 1);
454 if (!KnownB.isNonNegative())
455 return;
456
457 const KnownBits KnownC = computeKnownBits(C, DemandedElts, Q, Depth + 1);
458 if (!KnownC.isNonNegative())
459 return;
460
461 // If we matched subtraction as xor, we need to actually check that xor
462 // is semantically equivalent to subtraction.
463 //
464 // For that to be true, b has to be a mask for c or that b's known
465 // ones cover all known and possible ones of c.
466 if (SubBC->getOpcode() == Instruction::Xor &&
467 !KnownC.getMaxValue().isSubsetOf(KnownB.getMinValue()))
468 return;
469
470 const APInt MaxA = KnownA.getMaxValue();
471 const APInt MaxD = KnownD.getMaxValue();
472 const APInt MaxAD = APIntOps::umax(MaxA, MaxD);
473 const APInt MaxB = KnownB.getMaxValue();
474
475 // We can't infer leading zeros info if the upper-bound estimate wraps.
476 bool Overflow;
477 const APInt UpperBound = MaxAD.umul_ov(MaxB, Overflow);
478
479 if (Overflow)
480 return;
481
482 // If we know that x <= y and both are positive than x has at least the same
483 // number of leading zeros as y.
484 const unsigned MinimumNumberOfLeadingZeros = UpperBound.countl_zero();
485 KnownOut.Zero.setHighBits(MinimumNumberOfLeadingZeros);
486}
487
488static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1,
489 bool NSW, bool NUW,
490 const APInt &DemandedElts,
491 KnownBits &KnownOut, KnownBits &Known2,
492 const SimplifyQuery &Q, unsigned Depth) {
493 computeKnownBits(Op1, DemandedElts, KnownOut, Q, Depth + 1);
494
495 // If one operand is unknown and we have no nowrap information,
496 // the result will be unknown independently of the second operand.
497 if (KnownOut.isUnknown() && !NSW && !NUW)
498 return;
499
500 computeKnownBits(Op0, DemandedElts, Known2, Q, Depth + 1);
501 KnownOut = KnownBits::computeForAddSub(Add, NSW, NUW, Known2, KnownOut);
502
503 if (!Add && NSW && !KnownOut.isNonNegative() &&
505 .value_or(false) ||
506 match(Op1, m_c_SMin(m_Specific(Op0), m_Value()))))
507 KnownOut.makeNonNegative();
508
509 if (Add)
510 // Try to match lerp pattern and combine results
511 computeKnownBitsFromLerpPattern(Op0, Op1, DemandedElts, KnownOut, Q, Depth);
512}
513
514static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,
515 bool NUW, const APInt &DemandedElts,
516 KnownBits &Known, KnownBits &Known2,
517 const SimplifyQuery &Q, unsigned Depth) {
518 computeKnownBits(Op1, DemandedElts, Known, Q, Depth + 1);
519 computeKnownBits(Op0, DemandedElts, Known2, Q, Depth + 1);
520
521 bool isKnownNegative = false;
522 bool isKnownNonNegative = false;
523 // If the multiplication is known not to overflow, compute the sign bit.
524 if (NSW) {
525 if (Op0 == Op1) {
526 // The product of a number with itself is non-negative.
527 isKnownNonNegative = true;
528 } else {
529 bool isKnownNonNegativeOp1 = Known.isNonNegative();
530 bool isKnownNonNegativeOp0 = Known2.isNonNegative();
531 bool isKnownNegativeOp1 = Known.isNegative();
532 bool isKnownNegativeOp0 = Known2.isNegative();
533 // The product of two numbers with the same sign is non-negative.
534 isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) ||
535 (isKnownNonNegativeOp1 && isKnownNonNegativeOp0);
536 if (!isKnownNonNegative && NUW) {
537 // mul nuw nsw with a factor > 1 is non-negative.
539 isKnownNonNegative = KnownBits::sgt(Known, One).value_or(false) ||
540 KnownBits::sgt(Known2, One).value_or(false);
541 }
542
543 // The product of a negative number and a non-negative number is either
544 // negative or zero.
547 (isKnownNegativeOp1 && isKnownNonNegativeOp0 &&
548 Known2.isNonZero()) ||
549 (isKnownNegativeOp0 && isKnownNonNegativeOp1 && Known.isNonZero());
550 }
551 }
552
553 bool SelfMultiply = Op0 == Op1;
554 if (SelfMultiply)
555 SelfMultiply &=
556 isGuaranteedNotToBeUndef(Op0, Q.AC, Q.CxtI, Q.DT, Depth + 1);
557 Known = KnownBits::mul(Known, Known2, SelfMultiply);
558
559 if (SelfMultiply) {
560 unsigned SignBits = ComputeNumSignBits(Op0, DemandedElts, Q, Depth + 1);
561 unsigned TyBits = Op0->getType()->getScalarSizeInBits();
562 unsigned OutValidBits = 2 * (TyBits - SignBits + 1);
563
564 if (OutValidBits < TyBits) {
565 APInt KnownZeroMask =
566 APInt::getHighBitsSet(TyBits, TyBits - OutValidBits + 1);
567 Known.Zero |= KnownZeroMask;
568 }
569 }
570
571 // Only make use of no-wrap flags if we failed to compute the sign bit
572 // directly. This matters if the multiplication always overflows, in
573 // which case we prefer to follow the result of the direct computation,
574 // though as the program is invoking undefined behaviour we can choose
575 // whatever we like here.
576 if (isKnownNonNegative && !Known.isNegative())
577 Known.makeNonNegative();
578 else if (isKnownNegative && !Known.isNonNegative())
579 Known.makeNegative();
580}
581
583 KnownBits &Known) {
584 unsigned BitWidth = Known.getBitWidth();
585 unsigned NumRanges = Ranges.getNumOperands() / 2;
586 assert(NumRanges >= 1);
587
588 Known.setAllConflict();
589
590 for (unsigned i = 0; i < NumRanges; ++i) {
592 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
594 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
595 ConstantRange Range(Lower->getValue(), Upper->getValue());
596 // BitWidth must equal the Ranges BitWidth for the correct number of high
597 // bits to be set.
598 assert(BitWidth == Range.getBitWidth() &&
599 "Known bit width must match range bit width!");
600
601 // The first CommonPrefixBits of all values in Range are equal.
602 unsigned CommonPrefixBits =
603 (Range.getUnsignedMax() ^ Range.getUnsignedMin()).countl_zero();
604 APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits);
605 APInt UnsignedMax = Range.getUnsignedMax().zextOrTrunc(BitWidth);
606 Known.One &= UnsignedMax & Mask;
607 Known.Zero &= ~UnsignedMax & Mask;
608 }
609}
610
611static bool isEphemeralValueOf(const Instruction *I, const Value *E) {
612 // The instruction defining an assumption's condition itself is always
613 // considered ephemeral to that assumption (even if it has other
614 // non-ephemeral users). See r246696's test case for an example.
615 if (is_contained(I->operands(), E))
616 return true;
617
618 const auto *EI = dyn_cast<Instruction>(E);
619 if (!EI)
620 return false;
621
622 if (EI == I)
623 return true;
624
627 Visited.insert(EI);
628 WorkList.push_back(EI);
629 bool ReachesI = false;
630 while (!WorkList.empty()) {
631 const Instruction *V = WorkList.pop_back_val();
632 for (const User *U : V->users()) {
633 const auto *UI = cast<Instruction>(U);
634 if (UI == I) {
635 ReachesI = true;
636 continue;
637 }
638 if (UI->mayHaveSideEffects() || UI->isTerminator())
639 return false;
640 if (Visited.insert(UI).second)
641 WorkList.push_back(UI);
642 }
643 }
644 return ReachesI;
645}
646
647// Is this an intrinsic that cannot be speculated but also cannot trap?
649 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(I))
650 return CI->isAssumeLikeIntrinsic();
651
652 return false;
653}
654
656 const Instruction *CxtI,
657 const DominatorTree *DT,
658 bool AllowEphemerals) {
659 // There are two restrictions on the use of an assume:
660 // 1. The assume must dominate the context (or the control flow must
661 // reach the assume whenever it reaches the context).
662 // 2. The context must not be in the assume's set of ephemeral values
663 // (otherwise we will use the assume to prove that the condition
664 // feeding the assume is trivially true, thus causing the removal of
665 // the assume).
666
667 if (Inv->getParent() == CxtI->getParent()) {
668 // If Inv and CtxI are in the same block, check if the assume (Inv) is first
669 // in the BB.
670 if (Inv->comesBefore(CxtI))
671 return true;
672
673 // Don't let an assume affect itself - this would cause the problems
674 // `isEphemeralValueOf` is trying to prevent, and it would also make
675 // the loop below go out of bounds.
676 if (!AllowEphemerals && Inv == CxtI)
677 return false;
678
679 // The context comes first, but they're both in the same block.
680 // Make sure there is nothing in between that might interrupt
681 // the control flow, not even CxtI itself.
682 // We limit the scan distance between the assume and its context instruction
683 // to avoid a compile-time explosion. This limit is chosen arbitrarily, so
684 // it can be adjusted if needed (could be turned into a cl::opt).
685 auto Range = make_range(CxtI->getIterator(), Inv->getIterator());
687 return false;
688
689 return AllowEphemerals || !isEphemeralValueOf(Inv, CxtI);
690 }
691
692 // Inv and CxtI are in different blocks.
693 if (DT) {
694 if (DT->dominates(Inv, CxtI))
695 return true;
696 } else if (Inv->getParent() == CxtI->getParent()->getSinglePredecessor() ||
697 Inv->getParent()->isEntryBlock()) {
698 // We don't have a DT, but this trivially dominates.
699 return true;
700 }
701
702 return false;
703}
704
706 const Instruction *CtxI) {
707 // Helper to check if there are any calls in the range that may free memory.
708 auto hasNoFreeInRange = [](auto Range) {
709 for (const auto &[Idx, I] : enumerate(Range)) {
710 if (Idx > MaxInstrsToCheckForFree)
711 return false;
712
713 if (auto *CB = dyn_cast<CallBase>(&I)) {
714 if (!CB->hasFnAttr(Attribute::NoFree))
715 return false;
716 } else if (I.maySynchronize())
717 return false;
718 }
719 return true;
720 };
721
722 // Handle cross-block case: CtxI in a successor of Assume's block.
723 const BasicBlock *CtxBB = CtxI->getParent();
724 const BasicBlock *AssumeBB = Assume->getParent();
725 BasicBlock::const_iterator CtxIter = CtxI->getIterator();
726 if (CtxBB != AssumeBB) {
727 if (CtxBB->getSinglePredecessor() != AssumeBB)
728 return false;
729
730 if (!hasNoFreeInRange(make_range(CtxBB->begin(), CtxIter)))
731 return false;
732
733 CtxIter = AssumeBB->end();
734 } else {
735 // Same block case: check that Assume comes before CtxI.
736 if (Assume != CtxI && !Assume->comesBefore(CtxI))
737 return false;
738 }
739
740 // Check if there are any calls between Assume and CtxIter that may free
741 // memory.
742 return hasNoFreeInRange(make_range(Assume->getIterator(), CtxIter));
743}
744
745// TODO: cmpExcludesZero misses many cases where `RHS` is non-constant but
746// we still have enough information about `RHS` to conclude non-zero. For
747// example Pred=EQ, RHS=isKnownNonZero. cmpExcludesZero is called in loops
748// so the extra compile time may not be worth it, but possibly a second API
749// should be created for use outside of loops.
750static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS) {
751 // v u> y implies v != 0.
752 if (Pred == ICmpInst::ICMP_UGT)
753 return true;
754
755 // Special-case v != 0 to also handle v != null.
756 if (Pred == ICmpInst::ICMP_NE)
757 return match(RHS, m_Zero());
758
759 // All other predicates - rely on generic ConstantRange handling.
760 const APInt *C;
761 auto Zero = APInt::getZero(RHS->getType()->getScalarSizeInBits());
762 if (match(RHS, m_APInt(C))) {
764 return !TrueValues.contains(Zero);
765 }
766
768 if (VC == nullptr)
769 return false;
770
771 for (unsigned ElemIdx = 0, NElem = VC->getNumElements(); ElemIdx < NElem;
772 ++ElemIdx) {
774 Pred, VC->getElementAsAPInt(ElemIdx));
775 if (TrueValues.contains(Zero))
776 return false;
777 }
778 return true;
779}
780
781static void breakSelfRecursivePHI(const Use *U, const PHINode *PHI,
782 Value *&ValOut, Instruction *&CtxIOut,
783 const PHINode **PhiOut = nullptr) {
784 ValOut = U->get();
785 if (ValOut == PHI)
786 return;
787 CtxIOut = PHI->getIncomingBlock(*U)->getTerminator();
788 if (PhiOut)
789 *PhiOut = PHI;
790 Value *V;
791 // If the Use is a select of this phi, compute analysis on other arm to break
792 // recursion.
793 // TODO: Min/Max
794 if (match(ValOut, m_Select(m_Value(), m_Specific(PHI), m_Value(V))) ||
795 match(ValOut, m_Select(m_Value(), m_Value(V), m_Specific(PHI))))
796 ValOut = V;
797
798 // Same for select, if this phi is 2-operand phi, compute analysis on other
799 // incoming value to break recursion.
800 // TODO: We could handle any number of incoming edges as long as we only have
801 // two unique values.
802 if (auto *IncPhi = dyn_cast<PHINode>(ValOut);
803 IncPhi && IncPhi->getNumIncomingValues() == 2) {
804 for (int Idx = 0; Idx < 2; ++Idx) {
805 if (IncPhi->getIncomingValue(Idx) == PHI) {
806 ValOut = IncPhi->getIncomingValue(1 - Idx);
807 if (PhiOut)
808 *PhiOut = IncPhi;
809 CtxIOut = IncPhi->getIncomingBlock(1 - Idx)->getTerminator();
810 break;
811 }
812 }
813 }
814}
815
816static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q) {
817 // Use of assumptions is context-sensitive. If we don't have a context, we
818 // cannot use them!
819 if (!Q.AC || !Q.CxtI)
820 return false;
821
822 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
823 if (!Elem.Assume)
824 continue;
825
826 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
827 assert(I->getFunction() == Q.CxtI->getFunction() &&
828 "Got assumption for the wrong function!");
829
830 if (Elem.Index != AssumptionCache::ExprResultIdx) {
831 bool AssumeImpliesNonNull = [&]() {
832 auto OBU = I->getOperandBundleAt(Elem.Index);
833 switch (getBundleAttrFromOBU(OBU)) {
834 case BundleAttr::Dereferenceable: {
835 auto [Ptr, Count] = getAssumeDereferenceableInfo(OBU);
836 if (Ptr != V ||
839 return false;
840
841 auto *CI = dyn_cast<ConstantInt>(Count);
842 return CI && !CI->isZero();
843 }
844
845 case BundleAttr::NonNull:
846 return getAssumeNonNullInfo(OBU).Ptr == V;
847
848 default:
849 return false;
850 }
851 }();
852 if (AssumeImpliesNonNull && isValidAssumeForContext(I, Q))
853 return true;
854 continue;
855 }
856
857 // Warning: This loop can end up being somewhat performance sensitive.
858 // We're running this loop for once for each value queried resulting in a
859 // runtime of ~O(#assumes * #values).
860
861 Value *RHS;
862 CmpPredicate Pred;
863 auto m_V = m_CombineOr(m_Specific(V), m_PtrToInt(m_Specific(V)));
864 if (!match(I->getArgOperand(0), m_c_ICmp(Pred, m_V, m_Value(RHS))))
865 continue;
866
868 return true;
869 }
870
871 return false;
872}
873
875 Value *LHS, Value *RHS, KnownBits &Known,
876 const SimplifyQuery &Q) {
877 if (RHS->getType()->isPointerTy()) {
878 // Handle comparison of pointer to null explicitly, as it will not be
879 // covered by the m_APInt() logic below.
880 if (LHS == V && match(RHS, m_Zero())) {
881 switch (Pred) {
883 Known.setAllZero();
884 break;
887 Known.makeNonNegative();
888 break;
890 Known.makeNegative();
891 break;
892 default:
893 break;
894 }
895 }
896 return;
897 }
898
899 unsigned BitWidth = Known.getBitWidth();
900 auto m_V =
902
903 Value *Y;
904 const APInt *Mask, *C;
905 if (!match(RHS, m_APInt(C)))
906 return;
907
908 uint64_t ShAmt;
909 switch (Pred) {
911 // assume(V = C)
912 if (match(LHS, m_V)) {
913 Known = Known.unionWith(KnownBits::makeConstant(*C));
914 // assume(V & Mask = C)
915 } else if (match(LHS, m_c_And(m_V, m_Value(Y)))) {
916 // For one bits in Mask, we can propagate bits from C to V.
917 Known.One |= *C;
918 if (match(Y, m_APInt(Mask)))
919 Known.Zero |= ~*C & *Mask;
920 // assume(V | Mask = C)
921 } else if (match(LHS, m_c_Or(m_V, m_Value(Y)))) {
922 // For zero bits in Mask, we can propagate bits from C to V.
923 Known.Zero |= ~*C;
924 if (match(Y, m_APInt(Mask)))
925 Known.One |= *C & ~*Mask;
926 // assume(V << ShAmt = C)
927 } else if (match(LHS, m_Shl(m_V, m_ConstantInt(ShAmt))) &&
928 ShAmt < BitWidth) {
929 // For those bits in C that are known, we can propagate them to known
930 // bits in V shifted to the right by ShAmt.
932 RHSKnown >>= ShAmt;
933 Known = Known.unionWith(RHSKnown);
934 // assume(V >> ShAmt = C)
935 } else if (match(LHS, m_Shr(m_V, m_ConstantInt(ShAmt))) &&
936 ShAmt < BitWidth) {
937 // For those bits in RHS that are known, we can propagate them to known
938 // bits in V shifted to the right by C.
940 RHSKnown <<= ShAmt;
941 Known = Known.unionWith(RHSKnown);
942 }
943 break;
944 case ICmpInst::ICMP_NE: {
945 // assume (V & B != 0) where B is a power of 2
946 const APInt *BPow2;
947 if (C->isZero() && match(LHS, m_And(m_V, m_Power2(BPow2))))
948 Known.One |= *BPow2;
949 break;
950 }
951 default: {
952 const APInt *Offset = nullptr;
953 if (match(LHS, m_CombineOr(m_V, m_AddLike(m_V, m_APInt(Offset))))) {
955 if (Offset)
956 LHSRange = LHSRange.sub(*Offset);
957 Known = Known.unionWith(LHSRange.toKnownBits());
958 }
959 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
960 // X & Y u> C -> X u> C && Y u> C
961 // X nuw- Y u> C -> X u> C
962 if (match(LHS, m_c_And(m_V, m_Value())) ||
963 match(LHS, m_NUWSub(m_V, m_Value())))
964 Known.One.setHighBits(
965 (*C + (Pred == ICmpInst::ICMP_UGT)).countLeadingOnes());
966 }
967 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
968 // X | Y u< C -> X u< C && Y u< C
969 // X nuw+ Y u< C -> X u< C && Y u< C
970 if (match(LHS, m_c_Or(m_V, m_Value())) ||
971 match(LHS, m_c_NUWAdd(m_V, m_Value()))) {
972 Known.Zero.setHighBits(
973 (*C - (Pred == ICmpInst::ICMP_ULT)).countLeadingZeros());
974 }
975 }
976 } break;
977 }
978}
979
980static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp,
981 KnownBits &Known,
982 const SimplifyQuery &SQ, bool Invert) {
984 Invert ? Cmp->getInversePredicate() : Cmp->getPredicate();
985 Value *LHS = Cmp->getOperand(0);
986 Value *RHS = Cmp->getOperand(1);
987
988 // Handle icmp pred (trunc V), C
989 if (match(LHS, m_Trunc(m_Specific(V)))) {
990 KnownBits DstKnown(LHS->getType()->getScalarSizeInBits());
991 computeKnownBitsFromCmp(LHS, Pred, LHS, RHS, DstKnown, SQ);
993 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
994 else
995 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
996 return;
997 }
998
999 computeKnownBitsFromCmp(V, Pred, LHS, RHS, Known, SQ);
1000}
1001
1003 KnownBits &Known, const SimplifyQuery &SQ,
1004 bool Invert, unsigned Depth) {
1005 Value *A, *B;
1008 KnownBits Known2(Known.getBitWidth());
1009 KnownBits Known3(Known.getBitWidth());
1010 computeKnownBitsFromCond(V, A, Known2, SQ, Invert, Depth + 1);
1011 computeKnownBitsFromCond(V, B, Known3, SQ, Invert, Depth + 1);
1012 if (Invert ? match(Cond, m_LogicalOr(m_Value(), m_Value()))
1014 Known2 = Known2.unionWith(Known3);
1015 else
1016 Known2 = Known2.intersectWith(Known3);
1017 Known = Known.unionWith(Known2);
1018 return;
1019 }
1020
1021 if (auto *Cmp = dyn_cast<ICmpInst>(Cond)) {
1022 computeKnownBitsFromICmpCond(V, Cmp, Known, SQ, Invert);
1023 return;
1024 }
1025
1026 if (match(Cond, m_Trunc(m_Specific(V)))) {
1027 KnownBits DstKnown(1);
1028 if (Invert) {
1029 DstKnown.setAllZero();
1030 } else {
1031 DstKnown.setAllOnes();
1032 }
1034 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
1035 return;
1036 }
1037 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
1038 return;
1039 }
1040
1042 computeKnownBitsFromCond(V, A, Known, SQ, !Invert, Depth + 1);
1043}
1044
1046 const SimplifyQuery &Q, unsigned Depth) {
1047 // Handle injected condition.
1048 if (Q.CC && Q.CC->AffectedValues.contains(V))
1049 computeKnownBitsFromCond(V, Q.CC->Cond, Known, Q, Q.CC->Invert, Depth);
1050
1051 if (!Q.CxtI)
1052 return;
1053
1054 if (Q.DC && Q.DT) {
1055 // Handle dominating conditions.
1056 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
1057 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
1058 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
1059 computeKnownBitsFromCond(V, BI->getCondition(), Known, Q,
1060 /*Invert*/ false, Depth);
1061
1062 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
1063 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
1064 computeKnownBitsFromCond(V, BI->getCondition(), Known, Q,
1065 /*Invert*/ true, Depth);
1066 }
1067
1068 if (Known.hasConflict())
1069 Known.resetAll();
1070 }
1071
1072 if (!Q.AC)
1073 return;
1074
1075 unsigned BitWidth = Known.getBitWidth();
1076
1077 // Note that the patterns below need to be kept in sync with the code
1078 // in AssumptionCache::updateAffectedValues.
1079
1080 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
1081 if (!Elem.Assume)
1082 continue;
1083
1084 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
1085 assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() &&
1086 "Got assumption for the wrong function!");
1087
1088 if (Elem.Index != AssumptionCache::ExprResultIdx) {
1089 if (auto OBU = I->getOperandBundleAt(Elem.Index);
1090 getBundleAttrFromOBU(OBU) == BundleAttr::Align) {
1091 auto [Ptr, _, Alignment, Offset] = getAssumeAlignInfo(OBU);
1092 if (Ptr != V || !Alignment || !Offset || !isPowerOf2_64(*Alignment))
1093 continue;
1094 auto AlignVal = MinAlign(*Offset, *Alignment);
1095 if (isValidAssumeForContext(I, Q))
1096 Known.Zero.setLowBits(Log2_64(AlignVal));
1097 }
1098 continue;
1099 }
1100
1101 // Warning: This loop can end up being somewhat performance sensitive.
1102 // We're running this loop for once for each value queried resulting in a
1103 // runtime of ~O(#assumes * #values).
1104
1105 Value *Arg = I->getArgOperand(0);
1106
1107 if (Arg == V && isValidAssumeForContext(I, Q)) {
1108 assert(BitWidth == 1 && "assume operand is not i1?");
1109 (void)BitWidth;
1110 Known.setAllOnes();
1111 return;
1112 }
1113 if (match(Arg, m_Not(m_Specific(V))) &&
1115 assert(BitWidth == 1 && "assume operand is not i1?");
1116 (void)BitWidth;
1117 Known.setAllZero();
1118 return;
1119 }
1120 auto *Trunc = dyn_cast<TruncInst>(Arg);
1121 if (Trunc && Trunc->getOperand(0) == V &&
1123 if (Trunc->hasNoUnsignedWrap()) {
1125 return;
1126 }
1127 Known.One.setBit(0);
1128 return;
1129 }
1130
1131 // The remaining tests are all recursive, so bail out if we hit the limit.
1133 continue;
1134
1135 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
1136 if (!Cmp)
1137 continue;
1138
1139 if (!isValidAssumeForContext(I, Q))
1140 continue;
1141
1142 computeKnownBitsFromICmpCond(V, Cmp, Known, Q, /*Invert=*/false);
1143 }
1144
1145 // Conflicting assumption: Undefined behavior will occur on this execution
1146 // path.
1147 if (Known.hasConflict())
1148 Known.resetAll();
1149}
1150
1151/// Compute known bits from a shift operator, including those with a
1152/// non-constant shift amount. Known is the output of this function. Known2 is a
1153/// pre-allocated temporary with the same bit width as Known and on return
1154/// contains the known bit of the shift value source. KF is an
1155/// operator-specific function that, given the known-bits and a shift amount,
1156/// compute the implied known-bits of the shift operator's result respectively
1157/// for that shift amount. The results from calling KF are conservatively
1158/// combined for all permitted shift amounts.
1160 const Operator *I, const APInt &DemandedElts, KnownBits &Known,
1161 KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth,
1162 function_ref<KnownBits(const KnownBits &, const KnownBits &, bool)> KF) {
1163 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1164 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1165 // To limit compile-time impact, only query isKnownNonZero() if we know at
1166 // least something about the shift amount.
1167 bool ShAmtNonZero =
1168 Known.isNonZero() ||
1169 (Known.getMaxValue().ult(Known.getBitWidth()) &&
1170 isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth + 1));
1171 Known = KF(Known2, Known, ShAmtNonZero);
1172}
1173
1174static KnownBits
1175getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts,
1176 const KnownBits &KnownLHS, const KnownBits &KnownRHS,
1177 const SimplifyQuery &Q, unsigned Depth) {
1178 unsigned BitWidth = KnownLHS.getBitWidth();
1179 KnownBits KnownOut(BitWidth);
1180 bool IsAnd = false;
1181 bool HasKnownOne = !KnownLHS.One.isZero() || !KnownRHS.One.isZero();
1182 Value *X = nullptr, *Y = nullptr;
1183
1184 switch (I->getOpcode()) {
1185 case Instruction::And:
1186 KnownOut = KnownLHS & KnownRHS;
1187 IsAnd = true;
1188 // and(x, -x) is common idioms that will clear all but lowest set
1189 // bit. If we have a single known bit in x, we can clear all bits
1190 // above it.
1191 // TODO: instcombine often reassociates independent `and` which can hide
1192 // this pattern. Try to match and(x, and(-x, y)) / and(and(x, y), -x).
1193 if (HasKnownOne && match(I, m_c_And(m_Value(X), m_Neg(m_Deferred(X))))) {
1194 // -(-x) == x so using whichever (LHS/RHS) gets us a better result.
1195 if (KnownLHS.countMaxTrailingZeros() <= KnownRHS.countMaxTrailingZeros())
1196 KnownOut = KnownLHS.blsi();
1197 else
1198 KnownOut = KnownRHS.blsi();
1199 }
1200 break;
1201 case Instruction::Or:
1202 KnownOut = KnownLHS | KnownRHS;
1203 break;
1204 case Instruction::Xor:
1205 KnownOut = KnownLHS ^ KnownRHS;
1206 // xor(x, x-1) is common idioms that will clear all but lowest set
1207 // bit. If we have a single known bit in x, we can clear all bits
1208 // above it.
1209 // TODO: xor(x, x-1) is often rewritting as xor(x, x-C) where C !=
1210 // -1 but for the purpose of demanded bits (xor(x, x-C) &
1211 // Demanded) == (xor(x, x-1) & Demanded). Extend the xor pattern
1212 // to use arbitrary C if xor(x, x-C) as the same as xor(x, x-1).
1213 if (HasKnownOne &&
1215 const KnownBits &XBits = I->getOperand(0) == X ? KnownLHS : KnownRHS;
1216 KnownOut = XBits.blsmsk();
1217 }
1218 break;
1219 default:
1220 llvm_unreachable("Invalid Op used in 'analyzeKnownBitsFromAndXorOr'");
1221 }
1222
1223 // and(x, add (x, -1)) is a common idiom that always clears the low bit;
1224 // xor/or(x, add (x, -1)) is an idiom that will always set the low bit.
1225 // here we handle the more general case of adding any odd number by
1226 // matching the form and/xor/or(x, add(x, y)) where y is odd.
1227 // TODO: This could be generalized to clearing any bit set in y where the
1228 // following bit is known to be unset in y.
1229 if (!KnownOut.Zero[0] && !KnownOut.One[0] &&
1233 KnownBits KnownY(BitWidth);
1234 computeKnownBits(Y, DemandedElts, KnownY, Q, Depth + 1);
1235 if (KnownY.countMinTrailingOnes() > 0) {
1236 if (IsAnd)
1237 KnownOut.Zero.setBit(0);
1238 else
1239 KnownOut.One.setBit(0);
1240 }
1241 }
1242 return KnownOut;
1243}
1244
1246 const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q,
1247 unsigned Depth,
1248 const function_ref<KnownBits(const KnownBits &, const KnownBits &)>
1249 KnownBitsFunc) {
1250 APInt DemandedEltsLHS, DemandedEltsRHS;
1252 DemandedElts, DemandedEltsLHS,
1253 DemandedEltsRHS);
1254
1255 const auto ComputeForSingleOpFunc =
1256 [Depth, &Q, KnownBitsFunc](const Value *Op, APInt &DemandedEltsOp) {
1257 return KnownBitsFunc(
1258 computeKnownBits(Op, DemandedEltsOp, Q, Depth + 1),
1259 computeKnownBits(Op, DemandedEltsOp << 1, Q, Depth + 1));
1260 };
1261
1262 if (DemandedEltsRHS.isZero())
1263 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS);
1264 if (DemandedEltsLHS.isZero())
1265 return ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS);
1266
1267 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS)
1268 .intersectWith(ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS));
1269}
1270
1271// Public so this can be used in `SimplifyDemandedUseBits`.
1273 const KnownBits &KnownLHS,
1274 const KnownBits &KnownRHS,
1275 const SimplifyQuery &SQ,
1276 unsigned Depth) {
1277 auto *FVTy = dyn_cast<FixedVectorType>(I->getType());
1278 APInt DemandedElts =
1279 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
1280
1281 return getKnownBitsFromAndXorOr(I, DemandedElts, KnownLHS, KnownRHS, SQ,
1282 Depth);
1283}
1284
1286 Attribute Attr = F->getFnAttribute(Attribute::VScaleRange);
1287 // Without vscale_range, we only know that vscale is non-zero.
1288 if (!Attr.isValid())
1290
1291 unsigned AttrMin = Attr.getVScaleRangeMin();
1292 // Minimum is larger than vscale width, result is always poison.
1293 if ((unsigned)llvm::bit_width(AttrMin) > BitWidth)
1294 return ConstantRange::getEmpty(BitWidth);
1295
1296 APInt Min(BitWidth, AttrMin);
1297 std::optional<unsigned> AttrMax = Attr.getVScaleRangeMax();
1298 if (!AttrMax || (unsigned)llvm::bit_width(*AttrMax) > BitWidth)
1300
1301 return ConstantRange(Min, APInt(BitWidth, *AttrMax) + 1);
1302}
1303
1305 Value *Arm, bool Invert,
1306 const SimplifyQuery &Q, unsigned Depth) {
1307 // If we have a constant arm, we are done.
1308 if (Known.isConstant())
1309 return;
1310
1311 // See what condition implies about the bits of the select arm.
1312 KnownBits CondRes(Known.getBitWidth());
1313 computeKnownBitsFromCond(Arm, Cond, CondRes, Q, Invert, Depth + 1);
1314 // If we don't get any information from the condition, no reason to
1315 // proceed.
1316 if (CondRes.isUnknown())
1317 return;
1318
1319 // We can have conflict if the condition is dead. I.e if we have
1320 // (x | 64) < 32 ? (x | 64) : y
1321 // we will have conflict at bit 6 from the condition/the `or`.
1322 // In that case just return. Its not particularly important
1323 // what we do, as this select is going to be simplified soon.
1324 CondRes = CondRes.unionWith(Known);
1325 if (CondRes.hasConflict())
1326 return;
1327
1328 // Finally make sure the information we found is valid. This is relatively
1329 // expensive so it's left for the very end.
1330 if (!isGuaranteedNotToBeUndef(Arm, Q.AC, Q.CxtI, Q.DT, Depth + 1))
1331 return;
1332
1333 // Finally, we know we get information from the condition and its valid,
1334 // so return it.
1335 Known = std::move(CondRes);
1336}
1337
1338// Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow).
1339// Returns the input and lower/upper bounds.
1340static bool isSignedMinMaxClamp(const Value *Select, const Value *&In,
1341 const APInt *&CLow, const APInt *&CHigh) {
1343 cast<Operator>(Select)->getOpcode() == Instruction::Select &&
1344 "Input should be a Select!");
1345
1346 const Value *LHS = nullptr, *RHS = nullptr;
1348 if (SPF != SPF_SMAX && SPF != SPF_SMIN)
1349 return false;
1350
1351 if (!match(RHS, m_APInt(CLow)))
1352 return false;
1353
1354 const Value *LHS2 = nullptr, *RHS2 = nullptr;
1356 if (getInverseMinMaxFlavor(SPF) != SPF2)
1357 return false;
1358
1359 if (!match(RHS2, m_APInt(CHigh)))
1360 return false;
1361
1362 if (SPF == SPF_SMIN)
1363 std::swap(CLow, CHigh);
1364
1365 In = LHS2;
1366 return CLow->sle(*CHigh);
1367}
1368
1370 const APInt *&CLow,
1371 const APInt *&CHigh) {
1372 assert((II->getIntrinsicID() == Intrinsic::smin ||
1373 II->getIntrinsicID() == Intrinsic::smax) &&
1374 "Must be smin/smax");
1375
1376 Intrinsic::ID InverseID = getInverseMinMaxIntrinsic(II->getIntrinsicID());
1377 auto *InnerII = dyn_cast<IntrinsicInst>(II->getArgOperand(0));
1378 if (!InnerII || InnerII->getIntrinsicID() != InverseID ||
1379 !match(II->getArgOperand(1), m_APInt(CLow)) ||
1380 !match(InnerII->getArgOperand(1), m_APInt(CHigh)))
1381 return false;
1382
1383 if (II->getIntrinsicID() == Intrinsic::smin)
1384 std::swap(CLow, CHigh);
1385 return CLow->sle(*CHigh);
1386}
1387
1389 KnownBits &Known) {
1390 const APInt *CLow, *CHigh;
1391 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
1392 Known = Known.unionWith(
1393 ConstantRange::getNonEmpty(*CLow, *CHigh + 1).toKnownBits());
1394}
1395
1397 const APInt &DemandedElts,
1398 KnownBits &Known,
1399 const SimplifyQuery &Q,
1400 unsigned Depth) {
1401 unsigned BitWidth = Known.getBitWidth();
1402
1403 KnownBits Known2(BitWidth);
1404 switch (I->getOpcode()) {
1405 default: break;
1406 case Instruction::Load:
1407 if (MDNode *MD =
1408 Q.IIQ.getMetadata(cast<LoadInst>(I), LLVMContext::MD_range))
1410 break;
1411 case Instruction::And:
1412 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1413 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1414
1415 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1416 break;
1417 case Instruction::Or:
1418 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1419 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1420
1421 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1422 break;
1423 case Instruction::Xor:
1424 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1425 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1426
1427 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1428 break;
1429 case Instruction::Mul: {
1432 computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, NUW,
1433 DemandedElts, Known, Known2, Q, Depth);
1434 break;
1435 }
1436 case Instruction::UDiv: {
1437 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1438 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1439 Known =
1440 KnownBits::udiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1441 break;
1442 }
1443 case Instruction::SDiv: {
1444 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1445 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1446 Known =
1447 KnownBits::sdiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1448 break;
1449 }
1450 case Instruction::Select: {
1451 auto ComputeForArm = [&](Value *Arm, bool Invert) {
1452 KnownBits Res(Known.getBitWidth());
1453 computeKnownBits(Arm, DemandedElts, Res, Q, Depth + 1);
1454 adjustKnownBitsForSelectArm(Res, I->getOperand(0), Arm, Invert, Q, Depth);
1455 return Res;
1456 };
1457 // Only known if known in both the LHS and RHS.
1458 Known =
1459 ComputeForArm(I->getOperand(1), /*Invert=*/false)
1460 .intersectWith(ComputeForArm(I->getOperand(2), /*Invert=*/true));
1461 break;
1462 }
1463 case Instruction::FPTrunc:
1464 case Instruction::FPExt:
1465 case Instruction::FPToUI:
1466 case Instruction::FPToSI:
1467 case Instruction::SIToFP:
1468 case Instruction::UIToFP:
1469 break; // Can't work with floating point.
1470 case Instruction::PtrToInt:
1471 case Instruction::PtrToAddr:
1472 case Instruction::IntToPtr:
1473 // Fall through and handle them the same as zext/trunc.
1474 [[fallthrough]];
1475 case Instruction::ZExt:
1476 case Instruction::Trunc: {
1477 Type *SrcTy = I->getOperand(0)->getType();
1478
1479 unsigned SrcBitWidth;
1480 // Note that we handle pointer operands here because of inttoptr/ptrtoint
1481 // which fall through here.
1482 Type *ScalarTy = SrcTy->getScalarType();
1483 SrcBitWidth = ScalarTy->isPointerTy() ?
1484 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
1485 Q.DL.getTypeSizeInBits(ScalarTy);
1486
1487 assert(SrcBitWidth && "SrcBitWidth can't be zero");
1488 Known = Known.anyextOrTrunc(SrcBitWidth);
1489 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1490 if (auto *Inst = dyn_cast<PossiblyNonNegInst>(I);
1491 Inst && Inst->hasNonNeg() && !Known.isNegative())
1492 Known.makeNonNegative();
1493 Known = Known.zextOrTrunc(BitWidth);
1494 break;
1495 }
1496 case Instruction::BitCast: {
1497 Type *SrcTy = I->getOperand(0)->getType();
1498 if (SrcTy->isIntOrPtrTy() &&
1499 // TODO: For now, not handling conversions like:
1500 // (bitcast i64 %x to <2 x i32>)
1501 !I->getType()->isVectorTy()) {
1502 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1503 break;
1504 }
1505
1506 const Value *V;
1507 // Handle bitcast from floating point to integer.
1508 if (match(I, m_ElementWiseBitCast(m_Value(V))) &&
1509 V->getType()->isFPOrFPVectorTy()) {
1510 Type *FPType = V->getType()->getScalarType();
1511 KnownFPClass Result =
1512 computeKnownFPClass(V, DemandedElts, fcAllFlags, Q, Depth + 1);
1513 FPClassTest FPClasses = Result.KnownFPClasses;
1514
1515 // TODO: Treat it as zero/poison if the use of I is unreachable.
1516 if (FPClasses == fcNone)
1517 break;
1518
1519 if (Result.isKnownNever(fcNormal | fcSubnormal | fcNan)) {
1520 Known.setAllConflict();
1521
1522 if (FPClasses & fcInf)
1524 APFloat::getInf(FPType->getFltSemantics()).bitcastToAPInt()));
1525
1526 if (FPClasses & fcZero)
1528 APInt::getZero(FPType->getScalarSizeInBits())));
1529
1530 Known.Zero.clearSignBit();
1531 Known.One.clearSignBit();
1532 }
1533
1534 if (Result.SignBit) {
1535 if (*Result.SignBit)
1536 Known.makeNegative();
1537 else
1538 Known.makeNonNegative();
1539 }
1540
1541 break;
1542 }
1543
1544 // Handle cast from vector integer type to scalar or vector integer.
1545 auto *SrcVecTy = dyn_cast<FixedVectorType>(SrcTy);
1546 if (!SrcVecTy || !SrcVecTy->getElementType()->isIntegerTy() ||
1547 !I->getType()->isIntOrIntVectorTy() ||
1548 isa<ScalableVectorType>(I->getType()))
1549 break;
1550
1551 unsigned NumElts = DemandedElts.getBitWidth();
1552 bool IsLE = Q.DL.isLittleEndian();
1553 // Look through a cast from narrow vector elements to wider type.
1554 // Examples: v4i32 -> v2i64, v3i8 -> v24
1555 unsigned SubBitWidth = SrcVecTy->getScalarSizeInBits();
1556 if (BitWidth % SubBitWidth == 0) {
1557 // Known bits are automatically intersected across demanded elements of a
1558 // vector. So for example, if a bit is computed as known zero, it must be
1559 // zero across all demanded elements of the vector.
1560 //
1561 // For this bitcast, each demanded element of the output is sub-divided
1562 // across a set of smaller vector elements in the source vector. To get
1563 // the known bits for an entire element of the output, compute the known
1564 // bits for each sub-element sequentially. This is done by shifting the
1565 // one-set-bit demanded elements parameter across the sub-elements for
1566 // consecutive calls to computeKnownBits. We are using the demanded
1567 // elements parameter as a mask operator.
1568 //
1569 // The known bits of each sub-element are then inserted into place
1570 // (dependent on endian) to form the full result of known bits.
1571 unsigned SubScale = BitWidth / SubBitWidth;
1572 APInt SubDemandedElts = APInt::getZero(NumElts * SubScale);
1573 for (unsigned i = 0; i != NumElts; ++i) {
1574 if (DemandedElts[i])
1575 SubDemandedElts.setBit(i * SubScale);
1576 }
1577
1578 KnownBits KnownSrc(SubBitWidth);
1579 for (unsigned i = 0; i != SubScale; ++i) {
1580 computeKnownBits(I->getOperand(0), SubDemandedElts.shl(i), KnownSrc, Q,
1581 Depth + 1);
1582 unsigned ShiftElt = IsLE ? i : SubScale - 1 - i;
1583 Known.insertBits(KnownSrc, ShiftElt * SubBitWidth);
1584 }
1585 }
1586 // Look through a cast from wider vector elements to narrow type.
1587 // Examples: v2i64 -> v4i32
1588 if (SubBitWidth % BitWidth == 0) {
1589 unsigned SubScale = SubBitWidth / BitWidth;
1590 KnownBits KnownSrc(SubBitWidth);
1591 APInt SubDemandedElts =
1592 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
1593 computeKnownBits(I->getOperand(0), SubDemandedElts, KnownSrc, Q,
1594 Depth + 1);
1595
1596 Known.setAllConflict();
1597 for (unsigned i = 0; i != NumElts; ++i) {
1598 if (DemandedElts[i]) {
1599 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
1600 unsigned Offset = (Shifts % SubScale) * BitWidth;
1601 Known = Known.intersectWith(KnownSrc.extractBits(BitWidth, Offset));
1602 if (Known.isUnknown())
1603 break;
1604 }
1605 }
1606 }
1607 break;
1608 }
1609 case Instruction::SExt: {
1610 // Compute the bits in the result that are not present in the input.
1611 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
1612
1613 Known = Known.trunc(SrcBitWidth);
1614 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1615 // If the sign bit of the input is known set or clear, then we know the
1616 // top bits of the result.
1617 Known = Known.sext(BitWidth);
1618 break;
1619 }
1620 case Instruction::Shl: {
1623 auto KF = [NUW, NSW](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1624 bool ShAmtNonZero) {
1625 return KnownBits::shl(KnownVal, KnownAmt, NUW, NSW, ShAmtNonZero);
1626 };
1627 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1628 KF);
1629 // Trailing zeros of a right-shifted constant never decrease.
1630 const APInt *C;
1631 if (match(I->getOperand(0), m_APInt(C)))
1632 Known.Zero.setLowBits(C->countr_zero());
1633
1634 // shl X, sub(Y, xor(ctlz(X, true), BitWidth-1)) shifts X so that its MSB
1635 // lands at bit Y, when BitWidth is a power of 2.
1636 const APInt *YC;
1637 Value *X = I->getOperand(0);
1638 if (isPowerOf2_32(BitWidth) &&
1639 match(I->getOperand(1),
1641 m_SpecificInt(BitWidth - 1)))) &&
1642 YC->ult(BitWidth - 1)) {
1643 unsigned Y = YC->getZExtValue();
1644 Known.One.setBit(Y);
1645 Known.Zero.setBitsFrom(Y + 1);
1646 }
1647 break;
1648 }
1649 case Instruction::LShr: {
1650 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1651 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1652 bool ShAmtNonZero) {
1653 return KnownBits::lshr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1654 };
1655 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1656 KF);
1657 // Leading zeros of a left-shifted constant never decrease.
1658 const APInt *C;
1659 if (match(I->getOperand(0), m_APInt(C)))
1660 Known.Zero.setHighBits(C->countl_zero());
1661 break;
1662 }
1663 case Instruction::AShr: {
1664 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1665 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1666 bool ShAmtNonZero) {
1667 return KnownBits::ashr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1668 };
1669 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1670 KF);
1671 break;
1672 }
1673 case Instruction::Sub: {
1676 computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW, NUW,
1677 DemandedElts, Known, Known2, Q, Depth);
1678 break;
1679 }
1680 case Instruction::Add: {
1683 computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW, NUW,
1684 DemandedElts, Known, Known2, Q, Depth);
1685 break;
1686 }
1687 case Instruction::SRem:
1688 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1689 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1690 Known = KnownBits::srem(Known, Known2);
1691 break;
1692
1693 case Instruction::URem:
1694 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1695 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1696 Known = KnownBits::urem(Known, Known2);
1697 break;
1698 case Instruction::Alloca:
1700 break;
1701 case Instruction::GetElementPtr: {
1702 // Analyze all of the subscripts of this getelementptr instruction
1703 // to determine if we can prove known low zero bits.
1704 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1705 // Accumulate the constant indices in a separate variable
1706 // to minimize the number of calls to computeForAddSub.
1707 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(I->getType());
1708 APInt AccConstIndices(IndexWidth, 0);
1709
1710 auto AddIndexToKnown = [&](KnownBits IndexBits) {
1711 if (IndexWidth == BitWidth) {
1712 // Note that inbounds does *not* guarantee nsw for the addition, as only
1713 // the offset is signed, while the base address is unsigned.
1714 Known = KnownBits::add(Known, IndexBits);
1715 } else {
1716 // If the index width is smaller than the pointer width, only add the
1717 // value to the low bits.
1718 assert(IndexWidth < BitWidth &&
1719 "Index width can't be larger than pointer width");
1720 Known.insertBits(KnownBits::add(Known.trunc(IndexWidth), IndexBits), 0);
1721 }
1722 };
1723
1725 for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
1726 // TrailZ can only become smaller, short-circuit if we hit zero.
1727 if (Known.isUnknown())
1728 break;
1729
1730 Value *Index = I->getOperand(i);
1731
1732 // Handle case when index is zero.
1733 Constant *CIndex = dyn_cast<Constant>(Index);
1734 if (CIndex && CIndex->isNullValue())
1735 continue;
1736
1737 if (StructType *STy = GTI.getStructTypeOrNull()) {
1738 // Handle struct member offset arithmetic.
1739
1740 assert(CIndex &&
1741 "Access to structure field must be known at compile time");
1742
1743 if (CIndex->getType()->isVectorTy())
1744 Index = CIndex->getSplatValue();
1745
1746 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
1747 const StructLayout *SL = Q.DL.getStructLayout(STy);
1748 uint64_t Offset = SL->getElementOffset(Idx);
1749 AccConstIndices += Offset;
1750 continue;
1751 }
1752
1753 // Handle array index arithmetic.
1754 Type *IndexedTy = GTI.getIndexedType();
1755 if (!IndexedTy->isSized()) {
1756 Known.resetAll();
1757 break;
1758 }
1759
1760 TypeSize Stride = GTI.getSequentialElementStride(Q.DL);
1761 uint64_t StrideInBytes = Stride.getKnownMinValue();
1762 if (!Stride.isScalable()) {
1763 // Fast path for constant offset.
1764 if (auto *CI = dyn_cast<ConstantInt>(Index)) {
1765 AccConstIndices +=
1766 CI->getValue().sextOrTrunc(IndexWidth) * StrideInBytes;
1767 continue;
1768 }
1769 }
1770
1771 KnownBits IndexBits =
1772 computeKnownBits(Index, Q, Depth + 1).sextOrTrunc(IndexWidth);
1773 KnownBits ScalingFactor(IndexWidth);
1774 // Multiply by current sizeof type.
1775 // &A[i] == A + i * sizeof(*A[i]).
1776 if (Stride.isScalable()) {
1777 // For scalable types the only thing we know about sizeof is
1778 // that this is a multiple of the minimum size.
1779 ScalingFactor.Zero.setLowBits(llvm::countr_zero(StrideInBytes));
1780 } else {
1781 ScalingFactor =
1782 KnownBits::makeConstant(APInt(IndexWidth, StrideInBytes));
1783 }
1784 AddIndexToKnown(KnownBits::mul(IndexBits, ScalingFactor));
1785 }
1786 if (!Known.isUnknown() && !AccConstIndices.isZero())
1787 AddIndexToKnown(KnownBits::makeConstant(AccConstIndices));
1788 break;
1789 }
1790 case Instruction::PHI: {
1791 const PHINode *P = cast<PHINode>(I);
1792 BinaryOperator *BO = nullptr;
1793 Value *R = nullptr, *L = nullptr;
1794 if (matchSimpleRecurrence(P, BO, R, L)) {
1795 // Handle the case of a simple two-predecessor recurrence PHI.
1796 // There's a lot more that could theoretically be done here, but
1797 // this is sufficient to catch some interesting cases.
1798 unsigned Opcode = BO->getOpcode();
1799
1800 switch (Opcode) {
1801 // If this is a shift recurrence, we know the bits being shifted in. We
1802 // can combine that with information about the start value of the
1803 // recurrence to conclude facts about the result. If this is a udiv
1804 // recurrence, we know that the result can never exceed either the
1805 // numerator or the start value, whichever is greater.
1806 case Instruction::LShr:
1807 case Instruction::AShr:
1808 case Instruction::Shl:
1809 case Instruction::UDiv:
1810 if (BO->getOperand(0) != I)
1811 break;
1812 [[fallthrough]];
1813
1814 // For a urem recurrence, the result can never exceed the start value. The
1815 // phi could either be the numerator or the denominator.
1816 case Instruction::URem: {
1817 // We have matched a recurrence of the form:
1818 // %iv = [R, %entry], [%iv.next, %backedge]
1819 // %iv.next = shift_op %iv, L
1820
1821 // Recurse with the phi context to avoid concern about whether facts
1822 // inferred hold at original context instruction. TODO: It may be
1823 // correct to use the original context. IF warranted, explore and
1824 // add sufficient tests to cover.
1826 RecQ.CxtI = P;
1827 computeKnownBits(R, DemandedElts, Known2, RecQ, Depth + 1);
1828 switch (Opcode) {
1829 case Instruction::Shl:
1830 // A shl recurrence will only increase the tailing zeros
1831 Known.Zero.setLowBits(Known2.countMinTrailingZeros());
1832 break;
1833 case Instruction::LShr:
1834 case Instruction::UDiv:
1835 case Instruction::URem:
1836 // lshr, udiv, and urem recurrences will preserve the leading zeros of
1837 // the start value.
1838 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1839 break;
1840 case Instruction::AShr:
1841 // An ashr recurrence will extend the initial sign bit
1842 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1843 Known.One.setHighBits(Known2.countMinLeadingOnes());
1844 break;
1845 }
1846 break;
1847 }
1848
1849 // Check for operations that have the property that if
1850 // both their operands have low zero bits, the result
1851 // will have low zero bits.
1852 case Instruction::Add:
1853 case Instruction::Sub:
1854 case Instruction::And:
1855 case Instruction::Or:
1856 case Instruction::Mul: {
1857 // Change the context instruction to the "edge" that flows into the
1858 // phi. This is important because that is where the value is actually
1859 // "evaluated" even though it is used later somewhere else. (see also
1860 // D69571).
1862
1863 unsigned OpNum = P->getOperand(0) == R ? 0 : 1;
1864 Instruction *RInst = P->getIncomingBlock(OpNum)->getTerminator();
1865 Instruction *LInst = P->getIncomingBlock(1 - OpNum)->getTerminator();
1866
1867 // Ok, we have a PHI of the form L op= R. Check for low
1868 // zero bits.
1869 RecQ.CxtI = RInst;
1870 computeKnownBits(R, DemandedElts, Known2, RecQ, Depth + 1);
1871
1872 // We need to take the minimum number of known bits
1873 KnownBits Known3(BitWidth);
1874 RecQ.CxtI = LInst;
1875 computeKnownBits(L, DemandedElts, Known3, RecQ, Depth + 1);
1876
1877 Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(),
1878 Known3.countMinTrailingZeros()));
1879
1880 auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(BO);
1881 if (!OverflowOp || !Q.IIQ.hasNoSignedWrap(OverflowOp))
1882 break;
1883
1884 switch (Opcode) {
1885 // If initial value of recurrence is nonnegative, and we are adding
1886 // a nonnegative number with nsw, the result can only be nonnegative
1887 // or poison value regardless of the number of times we execute the
1888 // add in phi recurrence. If initial value is negative and we are
1889 // adding a negative number with nsw, the result can only be
1890 // negative or poison value. Similar arguments apply to sub and mul.
1891 //
1892 // (add non-negative, non-negative) --> non-negative
1893 // (add negative, negative) --> negative
1894 case Instruction::Add: {
1895 if (Known2.isNonNegative() && Known3.isNonNegative())
1896 Known.makeNonNegative();
1897 else if (Known2.isNegative() && Known3.isNegative())
1898 Known.makeNegative();
1899 break;
1900 }
1901
1902 // (sub nsw non-negative, negative) --> non-negative
1903 // (sub nsw negative, non-negative) --> negative
1904 case Instruction::Sub: {
1905 if (BO->getOperand(0) != I)
1906 break;
1907 if (Known2.isNonNegative() && Known3.isNegative())
1908 Known.makeNonNegative();
1909 else if (Known2.isNegative() && Known3.isNonNegative())
1910 Known.makeNegative();
1911 break;
1912 }
1913
1914 // (mul nsw non-negative, non-negative) --> non-negative
1915 case Instruction::Mul:
1916 if (Known2.isNonNegative() && Known3.isNonNegative())
1917 Known.makeNonNegative();
1918 break;
1919
1920 default:
1921 break;
1922 }
1923 break;
1924 }
1925
1926 default:
1927 break;
1928 }
1929 }
1930
1931 // Unreachable blocks may have zero-operand PHI nodes.
1932 if (P->getNumIncomingValues() == 0)
1933 break;
1934
1935 // Otherwise take the unions of the known bit sets of the operands,
1936 // taking conservative care to avoid excessive recursion.
1937 if (Depth < MaxAnalysisRecursionDepth - 1 && Known.isUnknown()) {
1938 // Skip if every incoming value references to ourself.
1939 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
1940 break;
1941
1942 Known.setAllConflict();
1943 for (const Use &U : P->operands()) {
1944 Value *IncValue;
1945 const PHINode *CxtPhi;
1946 Instruction *CxtI;
1947 breakSelfRecursivePHI(&U, P, IncValue, CxtI, &CxtPhi);
1948 // Skip direct self references.
1949 if (IncValue == P)
1950 continue;
1951
1952 // Change the context instruction to the "edge" that flows into the
1953 // phi. This is important because that is where the value is actually
1954 // "evaluated" even though it is used later somewhere else. (see also
1955 // D69571).
1957
1958 Known2 = KnownBits(BitWidth);
1959
1960 // Recurse, but cap the recursion to one level, because we don't
1961 // want to waste time spinning around in loops.
1962 // TODO: See if we can base recursion limiter on number of incoming phi
1963 // edges so we don't overly clamp analysis.
1964 computeKnownBits(IncValue, DemandedElts, Known2, RecQ,
1966
1967 // See if we can further use a conditional branch into the phi
1968 // to help us determine the range of the value.
1969 if (!Known2.isConstant()) {
1970 CmpPredicate Pred;
1971 const APInt *RHSC;
1972 BasicBlock *TrueSucc, *FalseSucc;
1973 // TODO: Use RHS Value and compute range from its known bits.
1974 if (match(RecQ.CxtI,
1975 m_Br(m_c_ICmp(Pred, m_Specific(IncValue), m_APInt(RHSC)),
1976 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
1977 // Check for cases of duplicate successors.
1978 if ((TrueSucc == CxtPhi->getParent()) !=
1979 (FalseSucc == CxtPhi->getParent())) {
1980 // If we're using the false successor, invert the predicate.
1981 if (FalseSucc == CxtPhi->getParent())
1982 Pred = CmpInst::getInversePredicate(Pred);
1983 // Get the knownbits implied by the incoming phi condition.
1984 auto CR = ConstantRange::makeExactICmpRegion(Pred, *RHSC);
1985 KnownBits KnownUnion = Known2.unionWith(CR.toKnownBits());
1986 // We can have conflicts here if we are analyzing deadcode (its
1987 // impossible for us reach this BB based the icmp).
1988 if (KnownUnion.hasConflict()) {
1989 // No reason to continue analyzing in a known dead region, so
1990 // just resetAll and break. This will cause us to also exit the
1991 // outer loop.
1992 Known.resetAll();
1993 break;
1994 }
1995 Known2 = KnownUnion;
1996 }
1997 }
1998 }
1999
2000 Known = Known.intersectWith(Known2);
2001 // If all bits have been ruled out, there's no need to check
2002 // more operands.
2003 if (Known.isUnknown())
2004 break;
2005 }
2006 }
2007 break;
2008 }
2009 case Instruction::Call:
2010 case Instruction::Invoke: {
2011 // If range metadata is attached to this call, set known bits from that,
2012 // and then intersect with known bits based on other properties of the
2013 // function.
2014 if (MDNode *MD =
2015 Q.IIQ.getMetadata(cast<Instruction>(I), LLVMContext::MD_range))
2017
2018 const auto *CB = cast<CallBase>(I);
2019
2020 if (std::optional<ConstantRange> Range = CB->getRange())
2021 Known = Known.unionWith(Range->toKnownBits());
2022
2023 if (const Value *RV = CB->getReturnedArgOperand()) {
2024 if (RV->getType() == I->getType()) {
2025 computeKnownBits(RV, Known2, Q, Depth + 1);
2026 Known = Known.unionWith(Known2);
2027 // If the function doesn't return properly for all input values
2028 // (e.g. unreachable exits) then there might be conflicts between the
2029 // argument value and the range metadata. Simply discard the known bits
2030 // in case of conflicts.
2031 if (Known.hasConflict())
2032 Known.resetAll();
2033 }
2034 }
2035 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
2036 switch (II->getIntrinsicID()) {
2037 default:
2038 break;
2039 case Intrinsic::abs: {
2040 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2041 bool IntMinIsPoison = match(II->getArgOperand(1), m_One());
2042 Known = Known.unionWith(Known2.abs(IntMinIsPoison));
2043 break;
2044 }
2045 case Intrinsic::bitreverse:
2046 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2047 Known = Known.unionWith(Known2.reverseBits());
2048 break;
2049 case Intrinsic::bswap:
2050 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2051 Known = Known.unionWith(Known2.byteSwap());
2052 break;
2053 case Intrinsic::ctlz: {
2054 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2055 // If we have a known 1, its position is our upper bound.
2056 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
2057 // If this call is poison for 0 input, the result will be less than 2^n.
2058 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
2059 PossibleLZ = std::min(PossibleLZ, BitWidth - 1);
2060 unsigned LowBits = llvm::bit_width(PossibleLZ);
2061 Known.Zero.setBitsFrom(LowBits);
2062 break;
2063 }
2064 case Intrinsic::cttz: {
2065 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2066 // If we have a known 1, its position is our upper bound.
2067 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
2068 // If this call is poison for 0 input, the result will be less than 2^n.
2069 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
2070 PossibleTZ = std::min(PossibleTZ, BitWidth - 1);
2071 unsigned LowBits = llvm::bit_width(PossibleTZ);
2072 Known.Zero.setBitsFrom(LowBits);
2073 break;
2074 }
2075 case Intrinsic::ctpop: {
2076 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2077 // We can bound the space the count needs. Also, bits known to be zero
2078 // can't contribute to the population.
2079 unsigned BitsPossiblySet = Known2.countMaxPopulation();
2080 unsigned LowBits = llvm::bit_width(BitsPossiblySet);
2081 Known.Zero.setBitsFrom(LowBits);
2082 // TODO: we could bound KnownOne using the lower bound on the number
2083 // of bits which might be set provided by popcnt KnownOne2.
2084 break;
2085 }
2086 case Intrinsic::fshr:
2087 case Intrinsic::fshl: {
2088 const APInt *SA;
2089 if (!match(I->getOperand(2), m_APInt(SA)))
2090 break;
2091
2092 KnownBits Known3(BitWidth);
2093 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2094 computeKnownBits(I->getOperand(1), DemandedElts, Known3, Q, Depth + 1);
2095 Known = II->getIntrinsicID() == Intrinsic::fshl
2096 ? KnownBits::fshl(Known2, Known3, *SA)
2097 : KnownBits::fshr(Known2, Known3, *SA);
2098 break;
2099 }
2100 case Intrinsic::clmul:
2101 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2102 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2103 Known = KnownBits::clmul(Known, Known2);
2104 break;
2105 case Intrinsic::uadd_sat:
2106 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2107 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2108 Known = KnownBits::uadd_sat(Known, Known2);
2109 break;
2110 case Intrinsic::usub_sat:
2111 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2112 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2113 Known = KnownBits::usub_sat(Known, Known2);
2114 break;
2115 case Intrinsic::sadd_sat:
2116 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2117 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2118 Known = KnownBits::sadd_sat(Known, Known2);
2119 break;
2120 case Intrinsic::ssub_sat:
2121 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2122 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2123 Known = KnownBits::ssub_sat(Known, Known2);
2124 break;
2125 // Vec reverse preserves bits from input vec.
2126 case Intrinsic::vector_reverse:
2127 computeKnownBits(I->getOperand(0), DemandedElts.reverseBits(), Known, Q,
2128 Depth + 1);
2129 break;
2130 // for min/max/and/or reduce, any bit common to each element in the
2131 // input vec is set in the output.
2132 case Intrinsic::vector_reduce_and:
2133 case Intrinsic::vector_reduce_or:
2134 case Intrinsic::vector_reduce_umax:
2135 case Intrinsic::vector_reduce_umin:
2136 case Intrinsic::vector_reduce_smax:
2137 case Intrinsic::vector_reduce_smin:
2138 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2139 break;
2140 case Intrinsic::vector_reduce_xor: {
2141 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2142 // The zeros common to all vecs are zero in the output.
2143 // If the number of elements is odd, then the common ones remain. If the
2144 // number of elements is even, then the common ones becomes zeros.
2145 auto *VecTy = cast<VectorType>(I->getOperand(0)->getType());
2146 // Even, so the ones become zeros.
2147 bool EvenCnt = VecTy->getElementCount().isKnownEven();
2148 if (EvenCnt)
2149 Known.Zero |= Known.One;
2150 // Maybe even element count so need to clear ones.
2151 if (VecTy->isScalableTy() || EvenCnt)
2152 Known.One.clearAllBits();
2153 break;
2154 }
2155 case Intrinsic::vector_reduce_add: {
2156 auto *VecTy = dyn_cast<FixedVectorType>(I->getOperand(0)->getType());
2157 if (!VecTy)
2158 break;
2159 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2160 Known = Known.reduceAdd(VecTy->getNumElements());
2161 break;
2162 }
2163 case Intrinsic::umin:
2164 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2165 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2166 Known = KnownBits::umin(Known, Known2);
2167 break;
2168 case Intrinsic::umax:
2169 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2170 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2171 Known = KnownBits::umax(Known, Known2);
2172 break;
2173 case Intrinsic::smin:
2174 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2175 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2176 Known = KnownBits::smin(Known, Known2);
2178 break;
2179 case Intrinsic::smax:
2180 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2181 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2182 Known = KnownBits::smax(Known, Known2);
2184 break;
2185 case Intrinsic::ptrmask: {
2186 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2187
2188 const Value *Mask = I->getOperand(1);
2189 Known2 = KnownBits(Mask->getType()->getScalarSizeInBits());
2190 computeKnownBits(Mask, DemandedElts, Known2, Q, Depth + 1);
2191 // TODO: 1-extend would be more precise.
2192 Known &= Known2.anyextOrTrunc(BitWidth);
2193 break;
2194 }
2195 case Intrinsic::x86_sse2_pmulh_w:
2196 case Intrinsic::x86_avx2_pmulh_w:
2197 case Intrinsic::x86_avx512_pmulh_w_512:
2198 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2199 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2200 Known = KnownBits::mulhs(Known, Known2);
2201 break;
2202 case Intrinsic::x86_sse2_pmulhu_w:
2203 case Intrinsic::x86_avx2_pmulhu_w:
2204 case Intrinsic::x86_avx512_pmulhu_w_512:
2205 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2206 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2207 Known = KnownBits::mulhu(Known, Known2);
2208 break;
2209 case Intrinsic::x86_sse42_crc32_64_64:
2210 Known.Zero.setBitsFrom(32);
2211 break;
2212 case Intrinsic::x86_ssse3_phadd_d_128:
2213 case Intrinsic::x86_ssse3_phadd_w_128:
2214 case Intrinsic::x86_avx2_phadd_d:
2215 case Intrinsic::x86_avx2_phadd_w: {
2217 I, DemandedElts, Q, Depth,
2218 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2219 return KnownBits::add(KnownLHS, KnownRHS);
2220 });
2221 break;
2222 }
2223 case Intrinsic::x86_ssse3_phadd_sw_128:
2224 case Intrinsic::x86_avx2_phadd_sw: {
2226 I, DemandedElts, Q, Depth, KnownBits::sadd_sat);
2227 break;
2228 }
2229 case Intrinsic::x86_ssse3_phsub_d_128:
2230 case Intrinsic::x86_ssse3_phsub_w_128:
2231 case Intrinsic::x86_avx2_phsub_d:
2232 case Intrinsic::x86_avx2_phsub_w: {
2234 I, DemandedElts, Q, Depth,
2235 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2236 return KnownBits::sub(KnownLHS, KnownRHS);
2237 });
2238 break;
2239 }
2240 case Intrinsic::x86_ssse3_phsub_sw_128:
2241 case Intrinsic::x86_avx2_phsub_sw: {
2243 I, DemandedElts, Q, Depth, KnownBits::ssub_sat);
2244 break;
2245 }
2246 case Intrinsic::riscv_vsetvli:
2247 case Intrinsic::riscv_vsetvlimax: {
2248 bool HasAVL = II->getIntrinsicID() == Intrinsic::riscv_vsetvli;
2249 const ConstantRange Range = getVScaleRange(II->getFunction(), BitWidth);
2251 cast<ConstantInt>(II->getArgOperand(HasAVL))->getZExtValue());
2252 RISCVVType::VLMUL VLMUL = static_cast<RISCVVType::VLMUL>(
2253 cast<ConstantInt>(II->getArgOperand(1 + HasAVL))->getZExtValue());
2254 uint64_t MaxVLEN =
2255 Range.getUnsignedMax().getZExtValue() * RISCV::RVVBitsPerBlock;
2256 uint64_t MaxVL = MaxVLEN / RISCVVType::getSEWLMULRatio(SEW, VLMUL);
2257
2258 // Result of vsetvli must be not larger than AVL.
2259 if (HasAVL)
2260 if (auto *CI = dyn_cast<ConstantInt>(II->getArgOperand(0)))
2261 MaxVL = std::min(MaxVL, CI->getZExtValue());
2262
2263 unsigned KnownZeroFirstBit = Log2_32(MaxVL) + 1;
2264 if (BitWidth > KnownZeroFirstBit)
2265 Known.Zero.setBitsFrom(KnownZeroFirstBit);
2266 break;
2267 }
2268 case Intrinsic::amdgcn_mbcnt_hi:
2269 case Intrinsic::amdgcn_mbcnt_lo: {
2270 // Wave64 mbcnt_lo returns at most 32 + src1. Otherwise these return at
2271 // most 31 + src1.
2272 Known.Zero.setBitsFrom(
2273 II->getIntrinsicID() == Intrinsic::amdgcn_mbcnt_lo ? 6 : 5);
2274 computeKnownBits(I->getOperand(1), Known2, Q, Depth + 1);
2275 Known = KnownBits::add(Known, Known2);
2276 break;
2277 }
2278 case Intrinsic::vscale: {
2279 if (!II->getParent() || !II->getFunction())
2280 break;
2281
2282 Known = getVScaleRange(II->getFunction(), BitWidth).toKnownBits();
2283 break;
2284 }
2285 }
2286 }
2287 break;
2288 }
2289 case Instruction::ShuffleVector: {
2290 if (auto *Splat = getSplatValue(I)) {
2291 computeKnownBits(Splat, Known, Q, Depth + 1);
2292 break;
2293 }
2294
2295 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
2296 // FIXME: Do we need to handle ConstantExpr involving shufflevectors?
2297 if (!Shuf) {
2298 Known.resetAll();
2299 return;
2300 }
2301 // For undef elements, we don't know anything about the common state of
2302 // the shuffle result.
2303 APInt DemandedLHS, DemandedRHS;
2304 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) {
2305 Known.resetAll();
2306 return;
2307 }
2308 Known.setAllConflict();
2309 if (!!DemandedLHS) {
2310 const Value *LHS = Shuf->getOperand(0);
2311 computeKnownBits(LHS, DemandedLHS, Known, Q, Depth + 1);
2312 // If we don't know any bits, early out.
2313 if (Known.isUnknown())
2314 break;
2315 }
2316 if (!!DemandedRHS) {
2317 const Value *RHS = Shuf->getOperand(1);
2318 computeKnownBits(RHS, DemandedRHS, Known2, Q, Depth + 1);
2319 Known = Known.intersectWith(Known2);
2320 }
2321 break;
2322 }
2323 case Instruction::InsertElement: {
2324 if (isa<ScalableVectorType>(I->getType())) {
2325 Known.resetAll();
2326 return;
2327 }
2328 const Value *Vec = I->getOperand(0);
2329 const Value *Elt = I->getOperand(1);
2330 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
2331 unsigned NumElts = DemandedElts.getBitWidth();
2332 APInt DemandedVecElts = DemandedElts;
2333 bool NeedsElt = true;
2334 // If we know the index we are inserting too, clear it from Vec check.
2335 if (CIdx && CIdx->getValue().ult(NumElts)) {
2336 DemandedVecElts.clearBit(CIdx->getZExtValue());
2337 NeedsElt = DemandedElts[CIdx->getZExtValue()];
2338 }
2339
2340 Known.setAllConflict();
2341 if (NeedsElt) {
2342 computeKnownBits(Elt, Known, Q, Depth + 1);
2343 // If we don't know any bits, early out.
2344 if (Known.isUnknown())
2345 break;
2346 }
2347
2348 if (!DemandedVecElts.isZero()) {
2349 computeKnownBits(Vec, DemandedVecElts, Known2, Q, Depth + 1);
2350 Known = Known.intersectWith(Known2);
2351 }
2352 break;
2353 }
2354 case Instruction::ExtractElement: {
2355 // Look through extract element. If the index is non-constant or
2356 // out-of-range demand all elements, otherwise just the extracted element.
2357 const Value *Vec = I->getOperand(0);
2358 const Value *Idx = I->getOperand(1);
2359 auto *CIdx = dyn_cast<ConstantInt>(Idx);
2360 if (isa<ScalableVectorType>(Vec->getType())) {
2361 // FIXME: there's probably *something* we can do with scalable vectors
2362 Known.resetAll();
2363 break;
2364 }
2365 unsigned NumElts = cast<FixedVectorType>(Vec->getType())->getNumElements();
2366 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
2367 if (CIdx && CIdx->getValue().ult(NumElts))
2368 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
2369 computeKnownBits(Vec, DemandedVecElts, Known, Q, Depth + 1);
2370 break;
2371 }
2372 case Instruction::ExtractValue:
2373 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
2375 if (EVI->getNumIndices() != 1) break;
2376 if (EVI->getIndices()[0] == 0) {
2377 switch (II->getIntrinsicID()) {
2378 default: break;
2379 case Intrinsic::uadd_with_overflow:
2380 case Intrinsic::sadd_with_overflow:
2382 true, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2383 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2384 break;
2385 case Intrinsic::usub_with_overflow:
2386 case Intrinsic::ssub_with_overflow:
2388 false, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2389 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2390 break;
2391 case Intrinsic::umul_with_overflow:
2392 case Intrinsic::smul_with_overflow:
2393 computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false,
2394 false, DemandedElts, Known, Known2, Q, Depth);
2395 break;
2396 }
2397 }
2398 }
2399 break;
2400 case Instruction::Freeze:
2401 if (isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
2402 Depth + 1))
2403 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2404 break;
2405 }
2406}
2407
2408/// Determine which bits of V are known to be either zero or one and return
2409/// them.
2410KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
2411 const SimplifyQuery &Q, unsigned Depth) {
2412 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2413 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);
2414 return Known;
2415}
2416
2417/// Determine which bits of V are known to be either zero or one and return
2418/// them.
2420 unsigned Depth) {
2421 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2422 computeKnownBits(V, Known, Q, Depth);
2423 return Known;
2424}
2425
2426/// Determine which bits of V are known to be either zero or one and return
2427/// them in the Known bit set.
2428///
2429/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that
2430/// we cannot optimize based on the assumption that it is zero without changing
2431/// it to be an explicit zero. If we don't change it to zero, other code could
2432/// optimized based on the contradictory assumption that it is non-zero.
2433/// Because instcombine aggressively folds operations with undef args anyway,
2434/// this won't lose us code quality.
2435///
2436/// This function is defined on values with integer type, values with pointer
2437/// type, and vectors of integers. In the case
2438/// where V is a vector, known zero, and known one values are the
2439/// same width as the vector element, and the bit is set only if it is true
2440/// for all of the demanded elements in the vector specified by DemandedElts.
2441void computeKnownBits(const Value *V, const APInt &DemandedElts,
2442 KnownBits &Known, const SimplifyQuery &Q,
2443 unsigned Depth) {
2444 if (!DemandedElts) {
2445 // No demanded elts, better to assume we don't know anything.
2446 Known.resetAll();
2447 return;
2448 }
2449
2450 assert(V && "No Value?");
2451 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2452
2453#ifndef NDEBUG
2454 Type *Ty = V->getType();
2455 unsigned BitWidth = Known.getBitWidth();
2456
2457 assert((Ty->isIntOrIntVectorTy(BitWidth) || Ty->isPtrOrPtrVectorTy()) &&
2458 "Not integer or pointer type!");
2459
2460 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
2461 assert(
2462 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
2463 "DemandedElt width should equal the fixed vector number of elements");
2464 } else {
2465 assert(DemandedElts == APInt(1, 1) &&
2466 "DemandedElt width should be 1 for scalars or scalable vectors");
2467 }
2468
2469 Type *ScalarTy = Ty->getScalarType();
2470 if (ScalarTy->isPointerTy()) {
2471 assert(BitWidth == Q.DL.getPointerTypeSizeInBits(ScalarTy) &&
2472 "V and Known should have same BitWidth");
2473 } else {
2474 assert(BitWidth == Q.DL.getTypeSizeInBits(ScalarTy) &&
2475 "V and Known should have same BitWidth");
2476 }
2477#endif
2478
2479 const APInt *C;
2480 if (match(V, m_APInt(C))) {
2481 // We know all of the bits for a scalar constant or a splat vector constant!
2482 Known = KnownBits::makeConstant(*C);
2483 return;
2484 }
2485 // Null and aggregate-zero are all-zeros.
2487 Known.setAllZero();
2488 return;
2489 }
2490 // Handle a constant vector by taking the intersection of the known bits of
2491 // each element.
2493 assert(!isa<ScalableVectorType>(V->getType()));
2494 // We know that CDV must be a vector of integers. Take the intersection of
2495 // each element.
2496 Known.setAllConflict();
2497 for (unsigned i = 0, e = CDV->getNumElements(); i != e; ++i) {
2498 if (!DemandedElts[i])
2499 continue;
2500 APInt Elt = CDV->getElementAsAPInt(i);
2501 Known.Zero &= ~Elt;
2502 Known.One &= Elt;
2503 }
2504 if (Known.hasConflict())
2505 Known.resetAll();
2506 return;
2507 }
2508
2509 if (const auto *CV = dyn_cast<ConstantVector>(V)) {
2510 assert(!isa<ScalableVectorType>(V->getType()));
2511 // We know that CV must be a vector of integers. Take the intersection of
2512 // each element.
2513 Known.setAllConflict();
2514 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
2515 if (!DemandedElts[i])
2516 continue;
2517 Constant *Element = CV->getAggregateElement(i);
2518 if (isa<PoisonValue>(Element))
2519 continue;
2520 auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
2521 if (!ElementCI) {
2522 Known.resetAll();
2523 return;
2524 }
2525 const APInt &Elt = ElementCI->getValue();
2526 Known.Zero &= ~Elt;
2527 Known.One &= Elt;
2528 }
2529 if (Known.hasConflict())
2530 Known.resetAll();
2531 return;
2532 }
2533
2534 // Start out not knowing anything.
2535 Known.resetAll();
2536
2537 // We can't imply anything about undefs.
2538 if (isa<UndefValue>(V))
2539 return;
2540
2541 // There's no point in looking through other users of ConstantData for
2542 // assumptions. Confirm that we've handled them all.
2543 assert(!isa<ConstantData>(V) && "Unhandled constant data!");
2544
2545 if (const auto *A = dyn_cast<Argument>(V))
2546 if (std::optional<ConstantRange> Range = A->getRange())
2547 Known = Range->toKnownBits();
2548
2549 // All recursive calls that increase depth must come after this.
2551 return;
2552
2553 // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
2554 // the bits of its aliasee.
2555 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
2556 if (!GA->isInterposable())
2557 computeKnownBits(GA->getAliasee(), Known, Q, Depth + 1);
2558 return;
2559 }
2560
2561 if (const Operator *I = dyn_cast<Operator>(V))
2562 computeKnownBitsFromOperator(I, DemandedElts, Known, Q, Depth);
2563 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2564 if (std::optional<ConstantRange> CR = GV->getAbsoluteSymbolRange())
2565 Known = CR->toKnownBits();
2566 }
2567
2568 // Aligned pointers have trailing zeros - refine Known.Zero set
2569 if (isa<PointerType>(V->getType())) {
2570 Align Alignment = V->getPointerAlignment(Q.DL);
2571 Known.Zero.setLowBits(Log2(Alignment));
2572 }
2573
2574 // computeKnownBitsFromContext strictly refines Known.
2575 // Therefore, we run them after computeKnownBitsFromOperator.
2576
2577 // Check whether we can determine known bits from context such as assumes.
2578 computeKnownBitsFromContext(V, Known, Q, Depth);
2579}
2580
2581/// Try to detect a recurrence that the value of the induction variable is
2582/// always a power of two (or zero).
2583static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero,
2584 SimplifyQuery &Q, unsigned Depth) {
2585 BinaryOperator *BO = nullptr;
2586 Value *Start = nullptr, *Step = nullptr;
2587 if (!matchSimpleRecurrence(PN, BO, Start, Step))
2588 return false;
2589
2590 // Initial value must be a power of two.
2591 for (const Use &U : PN->operands()) {
2592 if (U.get() == Start) {
2593 // Initial value comes from a different BB, need to adjust context
2594 // instruction for analysis.
2595 Q.CxtI = PN->getIncomingBlock(U)->getTerminator();
2596 if (!isKnownToBeAPowerOfTwo(Start, OrZero, Q, Depth))
2597 return false;
2598 }
2599 }
2600
2601 // Except for Mul, the induction variable must be on the left side of the
2602 // increment expression, otherwise its value can be arbitrary.
2603 if (BO->getOpcode() != Instruction::Mul && BO->getOperand(1) != Step)
2604 return false;
2605
2606 Q.CxtI = BO->getParent()->getTerminator();
2607 switch (BO->getOpcode()) {
2608 case Instruction::Mul:
2609 // Power of two is closed under multiplication.
2610 return (OrZero || Q.IIQ.hasNoUnsignedWrap(BO) ||
2611 Q.IIQ.hasNoSignedWrap(BO)) &&
2612 isKnownToBeAPowerOfTwo(Step, OrZero, Q, Depth);
2613 case Instruction::SDiv:
2614 // Start value must not be signmask for signed division, so simply being a
2615 // power of two is not sufficient, and it has to be a constant.
2616 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2617 return false;
2618 [[fallthrough]];
2619 case Instruction::UDiv:
2620 // Divisor must be a power of two.
2621 // If OrZero is false, cannot guarantee induction variable is non-zero after
2622 // division, same for Shr, unless it is exact division.
2623 return (OrZero || Q.IIQ.isExact(BO)) &&
2624 isKnownToBeAPowerOfTwo(Step, false, Q, Depth);
2625 case Instruction::Shl:
2626 return OrZero || Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO);
2627 case Instruction::AShr:
2628 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2629 return false;
2630 [[fallthrough]];
2631 case Instruction::LShr:
2632 return OrZero || Q.IIQ.isExact(BO);
2633 default:
2634 return false;
2635 }
2636}
2637
2638/// Return true if we can infer that \p V is known to be a power of 2 from
2639/// dominating condition \p Cond (e.g., ctpop(V) == 1).
2640static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero,
2641 const Value *Cond,
2642 bool CondIsTrue) {
2643 CmpPredicate Pred;
2644 const APInt *RHSC;
2645 if (!match(Cond, m_ICmp(Pred, m_Ctpop(m_Specific(V)), m_APInt(RHSC))))
2646 return false;
2647 if (!CondIsTrue)
2648 Pred = ICmpInst::getInversePredicate(Pred);
2649 // ctpop(V) u< 2
2650 if (OrZero && Pred == ICmpInst::ICMP_ULT && *RHSC == 2)
2651 return true;
2652 // ctpop(V) == 1
2653 return Pred == ICmpInst::ICMP_EQ && *RHSC == 1;
2654}
2655
2656/// Return true if the given value is known to have exactly one
2657/// bit set when defined. For vectors return true if every element is known to
2658/// be a power of two when defined. Supports values with integer or pointer
2659/// types and vectors of integers.
2660bool llvm::isKnownToBeAPowerOfTwo(const Value *V, bool OrZero,
2661 const SimplifyQuery &Q, unsigned Depth) {
2662 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2663
2664 if (isa<Constant>(V))
2665 return OrZero ? match(V, m_Power2OrZero()) : match(V, m_Power2());
2666
2667 // i1 is by definition a power of 2 or zero.
2668 if (OrZero && V->getType()->getScalarSizeInBits() == 1)
2669 return true;
2670
2671 // Try to infer from assumptions.
2672 if (Q.AC && Q.CxtI) {
2673 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
2674 if (!AssumeVH)
2675 continue;
2676 CallInst *I = cast<CallInst>(AssumeVH);
2677 if (isImpliedToBeAPowerOfTwoFromCond(V, OrZero, I->getArgOperand(0),
2678 /*CondIsTrue=*/true) &&
2680 return true;
2681 }
2682 }
2683
2684 // Handle dominating conditions.
2685 if (Q.DC && Q.CxtI && Q.DT) {
2686 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
2687 Value *Cond = BI->getCondition();
2688
2689 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
2691 /*CondIsTrue=*/true) &&
2692 Q.DT->dominates(Edge0, Q.CxtI->getParent()))
2693 return true;
2694
2695 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
2697 /*CondIsTrue=*/false) &&
2698 Q.DT->dominates(Edge1, Q.CxtI->getParent()))
2699 return true;
2700 }
2701 }
2702
2703 auto *I = dyn_cast<Instruction>(V);
2704 if (!I)
2705 return false;
2706
2707 if (Q.CxtI && match(V, m_VScale())) {
2708 const Function *F = Q.CxtI->getFunction();
2709 // The vscale_range indicates vscale is a power-of-two.
2710 return F->hasFnAttribute(Attribute::VScaleRange);
2711 }
2712
2713 // 1 << X is clearly a power of two if the one is not shifted off the end. If
2714 // it is shifted off the end then the result is undefined.
2715 if (match(I, m_Shl(m_One(), m_Value())))
2716 return true;
2717
2718 // (signmask) >>l X is clearly a power of two if the one is not shifted off
2719 // the bottom. If it is shifted off the bottom then the result is undefined.
2720 if (match(I, m_LShr(m_SignMask(), m_Value())))
2721 return true;
2722
2723 // The remaining tests are all recursive, so bail out if we hit the limit.
2725 return false;
2726
2727 switch (I->getOpcode()) {
2728 case Instruction::ZExt:
2729 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2730 case Instruction::Trunc:
2731 return OrZero && isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2732 case Instruction::Shl:
2733 if (OrZero || Q.IIQ.hasNoUnsignedWrap(I) || Q.IIQ.hasNoSignedWrap(I))
2734 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2735 return false;
2736 case Instruction::LShr:
2737 if (OrZero || Q.IIQ.isExact(cast<BinaryOperator>(I)))
2738 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2739 return false;
2740 case Instruction::UDiv:
2742 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2743 return false;
2744 case Instruction::Mul:
2745 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
2746 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth) &&
2747 (OrZero || isKnownNonZero(I, Q, Depth));
2748 case Instruction::And:
2749 // A power of two and'd with anything is a power of two or zero.
2750 if (OrZero &&
2751 (isKnownToBeAPowerOfTwo(I->getOperand(1), /*OrZero*/ true, Q, Depth) ||
2752 isKnownToBeAPowerOfTwo(I->getOperand(0), /*OrZero*/ true, Q, Depth)))
2753 return true;
2754 // X & (-X) is always a power of two or zero.
2755 if (match(I->getOperand(0), m_Neg(m_Specific(I->getOperand(1)))) ||
2756 match(I->getOperand(1), m_Neg(m_Specific(I->getOperand(0)))))
2757 return OrZero || isKnownNonZero(I->getOperand(0), Q, Depth);
2758 return false;
2759 case Instruction::Add: {
2760 // Adding a power-of-two or zero to the same power-of-two or zero yields
2761 // either the original power-of-two, a larger power-of-two or zero.
2763 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) ||
2764 Q.IIQ.hasNoSignedWrap(VOBO)) {
2765 if (match(I->getOperand(0),
2766 m_c_And(m_Specific(I->getOperand(1)), m_Value())) &&
2767 isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth))
2768 return true;
2769 if (match(I->getOperand(1),
2770 m_c_And(m_Specific(I->getOperand(0)), m_Value())) &&
2771 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth))
2772 return true;
2773
2774 unsigned BitWidth = V->getType()->getScalarSizeInBits();
2775 KnownBits LHSBits(BitWidth);
2776 computeKnownBits(I->getOperand(0), LHSBits, Q, Depth);
2777
2778 KnownBits RHSBits(BitWidth);
2779 computeKnownBits(I->getOperand(1), RHSBits, Q, Depth);
2780 // If i8 V is a power of two or zero:
2781 // ZeroBits: 1 1 1 0 1 1 1 1
2782 // ~ZeroBits: 0 0 0 1 0 0 0 0
2783 if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2())
2784 // If OrZero isn't set, we cannot give back a zero result.
2785 // Make sure either the LHS or RHS has a bit set.
2786 if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue())
2787 return true;
2788 }
2789
2790 // LShr(UINT_MAX, Y) + 1 is a power of two (if add is nuw) or zero.
2791 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO))
2792 if (match(I, m_Add(m_LShr(m_AllOnes(), m_Value()), m_One())))
2793 return true;
2794 return false;
2795 }
2796 case Instruction::Select:
2797 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
2798 isKnownToBeAPowerOfTwo(I->getOperand(2), OrZero, Q, Depth);
2799 case Instruction::PHI: {
2800 // A PHI node is power of two if all incoming values are power of two, or if
2801 // it is an induction variable where in each step its value is a power of
2802 // two.
2803 auto *PN = cast<PHINode>(I);
2805
2806 // Check if it is an induction variable and always power of two.
2807 if (isPowerOfTwoRecurrence(PN, OrZero, RecQ, Depth))
2808 return true;
2809
2810 // Recursively check all incoming values. Limit recursion to 2 levels, so
2811 // that search complexity is limited to number of operands^2.
2812 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
2813 return llvm::all_of(PN->operands(), [&](const Use &U) {
2814 // Value is power of 2 if it is coming from PHI node itself by induction.
2815 if (U.get() == PN)
2816 return true;
2817
2818 // Change the context instruction to the incoming block where it is
2819 // evaluated.
2820 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
2821 return isKnownToBeAPowerOfTwo(U.get(), OrZero, RecQ, NewDepth);
2822 });
2823 }
2824 case Instruction::Invoke:
2825 case Instruction::Call: {
2826 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
2827 switch (II->getIntrinsicID()) {
2828 case Intrinsic::umax:
2829 case Intrinsic::smax:
2830 case Intrinsic::umin:
2831 case Intrinsic::smin:
2832 return isKnownToBeAPowerOfTwo(II->getArgOperand(1), OrZero, Q, Depth) &&
2833 isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2834 // bswap/bitreverse just move around bits, but don't change any 1s/0s
2835 // thus dont change pow2/non-pow2 status.
2836 case Intrinsic::bitreverse:
2837 case Intrinsic::bswap:
2838 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2839 case Intrinsic::fshr:
2840 case Intrinsic::fshl:
2841 // If Op0 == Op1, this is a rotate. is_pow2(rotate(x, y)) == is_pow2(x)
2842 if (II->getArgOperand(0) == II->getArgOperand(1))
2843 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2844 break;
2845 default:
2846 break;
2847 }
2848 }
2849 return false;
2850 }
2851 default:
2852 return false;
2853 }
2854}
2855
2856/// Test whether a GEP's result is known to be non-null.
2857///
2858/// Uses properties inherent in a GEP to try to determine whether it is known
2859/// to be non-null.
2860///
2861/// Currently this routine does not support vector GEPs.
2862static bool isGEPKnownNonNull(const GEPOperator *GEP, const SimplifyQuery &Q,
2863 unsigned Depth) {
2864 const Function *F = nullptr;
2865 if (const Instruction *I = dyn_cast<Instruction>(GEP))
2866 F = I->getFunction();
2867
2868 // If the gep is nuw or inbounds with invalid null pointer, then the GEP
2869 // may be null iff the base pointer is null and the offset is zero.
2870 if (!GEP->hasNoUnsignedWrap() &&
2871 !(GEP->isInBounds() &&
2872 !NullPointerIsDefined(F, GEP->getPointerAddressSpace())))
2873 return false;
2874
2875 // FIXME: Support vector-GEPs.
2876 assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP");
2877
2878 // If the base pointer is non-null, we cannot walk to a null address with an
2879 // inbounds GEP in address space zero.
2880 if (isKnownNonZero(GEP->getPointerOperand(), Q, Depth))
2881 return true;
2882
2883 // Walk the GEP operands and see if any operand introduces a non-zero offset.
2884 // If so, then the GEP cannot produce a null pointer, as doing so would
2885 // inherently violate the inbounds contract within address space zero.
2887 GTI != GTE; ++GTI) {
2888 // Struct types are easy -- they must always be indexed by a constant.
2889 if (StructType *STy = GTI.getStructTypeOrNull()) {
2890 ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
2891 unsigned ElementIdx = OpC->getZExtValue();
2892 const StructLayout *SL = Q.DL.getStructLayout(STy);
2893 uint64_t ElementOffset = SL->getElementOffset(ElementIdx);
2894 if (ElementOffset > 0)
2895 return true;
2896 continue;
2897 }
2898
2899 // If we have a zero-sized type, the index doesn't matter. Keep looping.
2900 if (GTI.getSequentialElementStride(Q.DL).isZero())
2901 continue;
2902
2903 // Fast path the constant operand case both for efficiency and so we don't
2904 // increment Depth when just zipping down an all-constant GEP.
2905 if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
2906 if (!OpC->isZero())
2907 return true;
2908 continue;
2909 }
2910
2911 // We post-increment Depth here because while isKnownNonZero increments it
2912 // as well, when we pop back up that increment won't persist. We don't want
2913 // to recurse 10k times just because we have 10k GEP operands. We don't
2914 // bail completely out because we want to handle constant GEPs regardless
2915 // of depth.
2917 continue;
2918
2919 if (isKnownNonZero(GTI.getOperand(), Q, Depth))
2920 return true;
2921 }
2922
2923 return false;
2924}
2925
2927 const Instruction *CtxI,
2928 const DominatorTree *DT) {
2929 assert(!isa<Constant>(V) && "Called for constant?");
2930
2931 if (!CtxI || !DT)
2932 return false;
2933
2934 unsigned NumUsesExplored = 0;
2935 for (auto &U : V->uses()) {
2936 // Avoid massive lists
2937 if (NumUsesExplored >= DomConditionsMaxUses)
2938 break;
2939 NumUsesExplored++;
2940
2941 const Instruction *UI = cast<Instruction>(U.getUser());
2942 // If the value is used as an argument to a call or invoke, then argument
2943 // attributes may provide an answer about null-ness.
2944 if (V->getType()->isPointerTy()) {
2945 if (const auto *CB = dyn_cast<CallBase>(UI)) {
2946 if (CB->isArgOperand(&U) &&
2947 CB->paramHasNonNullAttr(CB->getArgOperandNo(&U),
2948 /*AllowUndefOrPoison=*/false) &&
2949 DT->dominates(CB, CtxI))
2950 return true;
2951 }
2952 }
2953
2954 // If the value is used as a load/store, then the pointer must be non null.
2955 if (V == getLoadStorePointerOperand(UI)) {
2958 DT->dominates(UI, CtxI))
2959 return true;
2960 }
2961
2962 if ((match(UI, m_IDiv(m_Value(), m_Specific(V))) ||
2963 match(UI, m_IRem(m_Value(), m_Specific(V)))) &&
2964 isValidAssumeForContext(UI, CtxI, DT))
2965 return true;
2966
2967 // Consider only compare instructions uniquely controlling a branch
2968 Value *RHS;
2969 CmpPredicate Pred;
2970 if (!match(UI, m_c_ICmp(Pred, m_Specific(V), m_Value(RHS))))
2971 continue;
2972
2973 bool NonNullIfTrue;
2974 if (cmpExcludesZero(Pred, RHS))
2975 NonNullIfTrue = true;
2977 NonNullIfTrue = false;
2978 else
2979 continue;
2980
2983 for (const auto *CmpU : UI->users()) {
2984 assert(WorkList.empty() && "Should be!");
2985 if (Visited.insert(CmpU).second)
2986 WorkList.push_back(CmpU);
2987
2988 while (!WorkList.empty()) {
2989 auto *Curr = WorkList.pop_back_val();
2990
2991 // If a user is an AND, add all its users to the work list. We only
2992 // propagate "pred != null" condition through AND because it is only
2993 // correct to assume that all conditions of AND are met in true branch.
2994 // TODO: Support similar logic of OR and EQ predicate?
2995 if (NonNullIfTrue)
2996 if (match(Curr, m_LogicalAnd(m_Value(), m_Value()))) {
2997 for (const auto *CurrU : Curr->users())
2998 if (Visited.insert(CurrU).second)
2999 WorkList.push_back(CurrU);
3000 continue;
3001 }
3002
3003 if (const CondBrInst *BI = dyn_cast<CondBrInst>(Curr)) {
3004 BasicBlock *NonNullSuccessor =
3005 BI->getSuccessor(NonNullIfTrue ? 0 : 1);
3006 BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor);
3007 if (DT->dominates(Edge, CtxI->getParent()))
3008 return true;
3009 } else if (NonNullIfTrue && isGuard(Curr) &&
3010 DT->dominates(cast<Instruction>(Curr), CtxI)) {
3011 return true;
3012 }
3013 }
3014 }
3015 }
3016
3017 return false;
3018}
3019
3020/// Does the 'Range' metadata (which must be a valid MD_range operand list)
3021/// ensure that the value it's attached to is never Value? 'RangeType' is
3022/// is the type of the value described by the range.
3023static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) {
3024 const unsigned NumRanges = Ranges->getNumOperands() / 2;
3025 assert(NumRanges >= 1);
3026 for (unsigned i = 0; i < NumRanges; ++i) {
3028 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
3030 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
3031 ConstantRange Range(Lower->getValue(), Upper->getValue());
3032 if (Range.contains(Value))
3033 return false;
3034 }
3035 return true;
3036}
3037
3038/// Try to detect a recurrence that monotonically increases/decreases from a
3039/// non-zero starting value. These are common as induction variables.
3040static bool isNonZeroRecurrence(const PHINode *PN) {
3041 BinaryOperator *BO = nullptr;
3042 Value *Start = nullptr, *Step = nullptr;
3043 const APInt *StartC, *StepC;
3044 if (!matchSimpleRecurrence(PN, BO, Start, Step) ||
3045 !match(Start, m_APInt(StartC)) || StartC->isZero())
3046 return false;
3047
3048 switch (BO->getOpcode()) {
3049 case Instruction::Add:
3050 // Starting from non-zero and stepping away from zero can never wrap back
3051 // to zero.
3052 return BO->hasNoUnsignedWrap() ||
3053 (BO->hasNoSignedWrap() && match(Step, m_APInt(StepC)) &&
3054 StartC->isNegative() == StepC->isNegative());
3055 case Instruction::Mul:
3056 return (BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap()) &&
3057 match(Step, m_APInt(StepC)) && !StepC->isZero();
3058 case Instruction::Shl:
3059 return BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap();
3060 case Instruction::AShr:
3061 case Instruction::LShr:
3062 return BO->isExact();
3063 default:
3064 return false;
3065 }
3066}
3067
3068static bool matchOpWithOpEqZero(Value *Op0, Value *Op1) {
3070 m_Specific(Op1), m_Zero()))) ||
3072 m_Specific(Op0), m_Zero())));
3073}
3074
3075static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q,
3076 unsigned BitWidth, Value *X, Value *Y, bool NSW,
3077 bool NUW, unsigned Depth) {
3078 // (X + (X != 0)) is non zero
3079 if (matchOpWithOpEqZero(X, Y))
3080 return true;
3081
3082 if (NUW)
3083 return isKnownNonZero(Y, DemandedElts, Q, Depth) ||
3084 isKnownNonZero(X, DemandedElts, Q, Depth);
3085
3086 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
3087 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
3088
3089 // If X and Y are both non-negative (as signed values) then their sum is not
3090 // zero unless both X and Y are zero.
3091 if (XKnown.isNonNegative() && YKnown.isNonNegative())
3092 if (isKnownNonZero(Y, DemandedElts, Q, Depth) ||
3093 isKnownNonZero(X, DemandedElts, Q, Depth))
3094 return true;
3095
3096 // If X and Y are both negative (as signed values) then their sum is not
3097 // zero unless both X and Y equal INT_MIN.
3098 if (XKnown.isNegative() && YKnown.isNegative()) {
3100 // The sign bit of X is set. If some other bit is set then X is not equal
3101 // to INT_MIN.
3102 if (XKnown.One.intersects(Mask))
3103 return true;
3104 // The sign bit of Y is set. If some other bit is set then Y is not equal
3105 // to INT_MIN.
3106 if (YKnown.One.intersects(Mask))
3107 return true;
3108 }
3109
3110 // The sum of a non-negative number and a power of two is not zero.
3111 if (XKnown.isNonNegative() &&
3112 isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Q, Depth))
3113 return true;
3114 if (YKnown.isNonNegative() &&
3115 isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Q, Depth))
3116 return true;
3117
3118 return KnownBits::add(XKnown, YKnown, NSW, NUW).isNonZero();
3119}
3120
3121static bool isNonZeroSub(const APInt &DemandedElts, const SimplifyQuery &Q,
3122 unsigned BitWidth, Value *X, Value *Y,
3123 unsigned Depth) {
3124 // (X - (X != 0)) is non zero
3125 // ((X != 0) - X) is non zero
3126 if (matchOpWithOpEqZero(X, Y))
3127 return true;
3128
3129 // TODO: Move this case into isKnownNonEqual().
3130 if (auto *C = dyn_cast<Constant>(X))
3131 if (C->isNullValue() && isKnownNonZero(Y, DemandedElts, Q, Depth))
3132 return true;
3133
3134 return ::isKnownNonEqual(X, Y, DemandedElts, Q, Depth);
3135}
3136
3137static bool isNonZeroMul(const APInt &DemandedElts, const SimplifyQuery &Q,
3138 unsigned BitWidth, Value *X, Value *Y, bool NSW,
3139 bool NUW, unsigned Depth) {
3140 // If X and Y are non-zero then so is X * Y as long as the multiplication
3141 // does not overflow.
3142 if (NSW || NUW)
3143 return isKnownNonZero(X, DemandedElts, Q, Depth) &&
3144 isKnownNonZero(Y, DemandedElts, Q, Depth);
3145
3146 // If either X or Y is odd, then if the other is non-zero the result can't
3147 // be zero.
3148 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
3149 if (XKnown.One[0])
3150 return isKnownNonZero(Y, DemandedElts, Q, Depth);
3151
3152 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
3153 if (YKnown.One[0])
3154 return XKnown.isNonZero() || isKnownNonZero(X, DemandedElts, Q, Depth);
3155
3156 // If there exists any subset of X (sX) and subset of Y (sY) s.t sX * sY is
3157 // non-zero, then X * Y is non-zero. We can find sX and sY by just taking
3158 // the lowest known One of X and Y. If they are non-zero, the result
3159 // must be non-zero. We can check if LSB(X) * LSB(Y) != 0 by doing
3160 // X.CountLeadingZeros + Y.CountLeadingZeros < BitWidth.
3161 return (XKnown.countMaxTrailingZeros() + YKnown.countMaxTrailingZeros()) <
3162 BitWidth;
3163}
3164
3165static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts,
3166 const SimplifyQuery &Q, const KnownBits &KnownVal,
3167 unsigned Depth) {
3168 auto ShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
3169 switch (I->getOpcode()) {
3170 case Instruction::Shl:
3171 return Lhs.shl(Rhs);
3172 case Instruction::LShr:
3173 return Lhs.lshr(Rhs);
3174 case Instruction::AShr:
3175 return Lhs.ashr(Rhs);
3176 default:
3177 llvm_unreachable("Unknown Shift Opcode");
3178 }
3179 };
3180
3181 auto InvShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
3182 switch (I->getOpcode()) {
3183 case Instruction::Shl:
3184 return Lhs.lshr(Rhs);
3185 case Instruction::LShr:
3186 case Instruction::AShr:
3187 return Lhs.shl(Rhs);
3188 default:
3189 llvm_unreachable("Unknown Shift Opcode");
3190 }
3191 };
3192
3193 if (KnownVal.isUnknown())
3194 return false;
3195
3196 KnownBits KnownCnt =
3197 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
3198 APInt MaxShift = KnownCnt.getMaxValue();
3199 unsigned NumBits = KnownVal.getBitWidth();
3200 if (MaxShift.uge(NumBits))
3201 return false;
3202
3203 if (!ShiftOp(KnownVal.One, MaxShift).isZero())
3204 return true;
3205
3206 // If all of the bits shifted out are known to be zero, and Val is known
3207 // non-zero then at least one non-zero bit must remain.
3208 if (InvShiftOp(KnownVal.Zero, NumBits - MaxShift)
3209 .eq(InvShiftOp(APInt::getAllOnes(NumBits), NumBits - MaxShift)) &&
3210 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth))
3211 return true;
3212
3213 return false;
3214}
3215
3217 const APInt &DemandedElts,
3218 const SimplifyQuery &Q, unsigned Depth) {
3219 unsigned BitWidth = getBitWidth(I->getType()->getScalarType(), Q.DL);
3220 switch (I->getOpcode()) {
3221 case Instruction::Alloca:
3222 // Alloca never returns null, malloc might.
3223 return I->getType()->getPointerAddressSpace() == 0;
3224 case Instruction::GetElementPtr:
3225 if (I->getType()->isPointerTy())
3227 break;
3228 case Instruction::BitCast: {
3229 // We need to be a bit careful here. We can only peek through the bitcast
3230 // if the scalar size of elements in the operand are smaller than and a
3231 // multiple of the size they are casting too. Take three cases:
3232 //
3233 // 1) Unsafe:
3234 // bitcast <2 x i16> %NonZero to <4 x i8>
3235 //
3236 // %NonZero can have 2 non-zero i16 elements, but isKnownNonZero on a
3237 // <4 x i8> requires that all 4 i8 elements be non-zero which isn't
3238 // guranteed (imagine just sign bit set in the 2 i16 elements).
3239 //
3240 // 2) Unsafe:
3241 // bitcast <4 x i3> %NonZero to <3 x i4>
3242 //
3243 // Even though the scalar size of the src (`i3`) is smaller than the
3244 // scalar size of the dst `i4`, because `i3` is not a multiple of `i4`
3245 // its possible for the `3 x i4` elements to be zero because there are
3246 // some elements in the destination that don't contain any full src
3247 // element.
3248 //
3249 // 3) Safe:
3250 // bitcast <4 x i8> %NonZero to <2 x i16>
3251 //
3252 // This is always safe as non-zero in the 4 i8 elements implies
3253 // non-zero in the combination of any two adjacent ones. Since i8 is a
3254 // multiple of i16, each i16 is guranteed to have 2 full i8 elements.
3255 // This all implies the 2 i16 elements are non-zero.
3256 Type *FromTy = I->getOperand(0)->getType();
3257 if ((FromTy->isIntOrIntVectorTy() || FromTy->isPtrOrPtrVectorTy()) &&
3258 (BitWidth % getBitWidth(FromTy->getScalarType(), Q.DL)) == 0)
3259 return isKnownNonZero(I->getOperand(0), Q, Depth);
3260 } break;
3261 case Instruction::IntToPtr:
3262 // Note that we have to take special care to avoid looking through
3263 // truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well
3264 // as casts that can alter the value, e.g., AddrSpaceCasts.
3265 if (!isa<ScalableVectorType>(I->getType()) &&
3266 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
3267 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
3268 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3269 break;
3270 case Instruction::PtrToAddr:
3271 // isKnownNonZero() for pointers refers to the address bits being non-zero,
3272 // so we can directly forward.
3273 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3274 case Instruction::PtrToInt:
3275 // For inttoptr, make sure the result size is >= the address size. If the
3276 // address is non-zero, any larger value is also non-zero.
3277 if (Q.DL.getAddressSizeInBits(I->getOperand(0)->getType()) <=
3278 I->getType()->getScalarSizeInBits())
3279 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3280 break;
3281 case Instruction::Trunc:
3282 // nuw/nsw trunc preserves zero/non-zero status of input.
3283 if (auto *TI = dyn_cast<TruncInst>(I))
3284 if (TI->hasNoSignedWrap() || TI->hasNoUnsignedWrap())
3285 return isKnownNonZero(TI->getOperand(0), DemandedElts, Q, Depth);
3286 break;
3287
3288 // Iff x - y != 0, then x ^ y != 0
3289 // Therefore we can do the same exact checks
3290 case Instruction::Xor:
3291 case Instruction::Sub:
3292 return isNonZeroSub(DemandedElts, Q, BitWidth, I->getOperand(0),
3293 I->getOperand(1), Depth);
3294 case Instruction::Or:
3295 // (X | (X != 0)) is non zero
3296 if (matchOpWithOpEqZero(I->getOperand(0), I->getOperand(1)))
3297 return true;
3298 // X | Y != 0 if X != Y.
3299 if (isKnownNonEqual(I->getOperand(0), I->getOperand(1), DemandedElts, Q,
3300 Depth))
3301 return true;
3302 // X | Y != 0 if X != 0 or Y != 0.
3303 return isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth) ||
3304 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3305 case Instruction::SExt:
3306 case Instruction::ZExt:
3307 // ext X != 0 if X != 0.
3308 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3309
3310 case Instruction::Shl: {
3311 // shl nsw/nuw can't remove any non-zero bits.
3313 if (Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO))
3314 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3315
3316 // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
3317 // if the lowest bit is shifted off the end.
3318 KnownBits Known(BitWidth);
3319 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth);
3320 if (Known.One[0])
3321 return true;
3322
3323 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3324 }
3325 case Instruction::LShr:
3326 case Instruction::AShr: {
3327 // shr exact can only shift out zero bits.
3329 if (BO->isExact())
3330 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3331
3332 // shr X, Y != 0 if X is negative. Note that the value of the shift is not
3333 // defined if the sign bit is shifted off the end.
3334 KnownBits Known =
3335 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3336 if (Known.isNegative())
3337 return true;
3338
3339 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3340 }
3341 case Instruction::UDiv:
3342 case Instruction::SDiv: {
3343 // X / Y
3344 // div exact can only produce a zero if the dividend is zero.
3345 if (cast<PossiblyExactOperator>(I)->isExact())
3346 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3347
3348 KnownBits XKnown =
3349 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3350 // If X is fully unknown we won't be able to figure anything out so don't
3351 // both computing knownbits for Y.
3352 if (XKnown.isUnknown())
3353 return false;
3354
3355 KnownBits YKnown =
3356 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
3357 if (I->getOpcode() == Instruction::SDiv) {
3358 // For signed division need to compare abs value of the operands.
3359 XKnown = XKnown.abs(/*IntMinIsPoison*/ false);
3360 YKnown = YKnown.abs(/*IntMinIsPoison*/ false);
3361 }
3362 // If X u>= Y then div is non zero (0/0 is UB).
3363 std::optional<bool> XUgeY = KnownBits::uge(XKnown, YKnown);
3364 // If X is total unknown or X u< Y we won't be able to prove non-zero
3365 // with compute known bits so just return early.
3366 return XUgeY && *XUgeY;
3367 }
3368 case Instruction::Add: {
3369 // X + Y.
3370
3371 // If Add has nuw wrap flag, then if either X or Y is non-zero the result is
3372 // non-zero.
3374 return isNonZeroAdd(DemandedElts, Q, BitWidth, I->getOperand(0),
3375 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3376 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3377 }
3378 case Instruction::Mul: {
3380 return isNonZeroMul(DemandedElts, Q, BitWidth, I->getOperand(0),
3381 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3382 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3383 }
3384 case Instruction::Select: {
3385 // (C ? X : Y) != 0 if X != 0 and Y != 0.
3386
3387 // First check if the arm is non-zero using `isKnownNonZero`. If that fails,
3388 // then see if the select condition implies the arm is non-zero. For example
3389 // (X != 0 ? X : Y), we know the true arm is non-zero as the `X` "return" is
3390 // dominated by `X != 0`.
3391 auto SelectArmIsNonZero = [&](bool IsTrueArm) {
3392 Value *Op;
3393 Op = IsTrueArm ? I->getOperand(1) : I->getOperand(2);
3394 // Op is trivially non-zero.
3395 if (isKnownNonZero(Op, DemandedElts, Q, Depth))
3396 return true;
3397
3398 // The condition of the select dominates the true/false arm. Check if the
3399 // condition implies that a given arm is non-zero.
3400 Value *X;
3401 CmpPredicate Pred;
3402 if (!match(I->getOperand(0), m_c_ICmp(Pred, m_Specific(Op), m_Value(X))))
3403 return false;
3404
3405 if (!IsTrueArm)
3406 Pred = ICmpInst::getInversePredicate(Pred);
3407
3408 return cmpExcludesZero(Pred, X);
3409 };
3410
3411 if (SelectArmIsNonZero(/* IsTrueArm */ true) &&
3412 SelectArmIsNonZero(/* IsTrueArm */ false))
3413 return true;
3414 break;
3415 }
3416 case Instruction::PHI: {
3417 auto *PN = cast<PHINode>(I);
3419 return true;
3420
3421 // Check if all incoming values are non-zero using recursion.
3423 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
3424 return llvm::all_of(PN->operands(), [&](const Use &U) {
3425 if (U.get() == PN)
3426 return true;
3427 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
3428 // Check if the branch on the phi excludes zero.
3429 CmpPredicate Pred;
3430 Value *X;
3431 BasicBlock *TrueSucc, *FalseSucc;
3432 if (match(RecQ.CxtI,
3433 m_Br(m_c_ICmp(Pred, m_Specific(U.get()), m_Value(X)),
3434 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
3435 // Check for cases of duplicate successors.
3436 if ((TrueSucc == PN->getParent()) != (FalseSucc == PN->getParent())) {
3437 // If we're using the false successor, invert the predicate.
3438 if (FalseSucc == PN->getParent())
3439 Pred = CmpInst::getInversePredicate(Pred);
3440 if (cmpExcludesZero(Pred, X))
3441 return true;
3442 }
3443 }
3444 // Finally recurse on the edge and check it directly.
3445 return isKnownNonZero(U.get(), DemandedElts, RecQ, NewDepth);
3446 });
3447 }
3448 case Instruction::InsertElement: {
3449 if (isa<ScalableVectorType>(I->getType()))
3450 break;
3451
3452 const Value *Vec = I->getOperand(0);
3453 const Value *Elt = I->getOperand(1);
3454 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
3455
3456 unsigned NumElts = DemandedElts.getBitWidth();
3457 APInt DemandedVecElts = DemandedElts;
3458 bool SkipElt = false;
3459 // If we know the index we are inserting too, clear it from Vec check.
3460 if (CIdx && CIdx->getValue().ult(NumElts)) {
3461 DemandedVecElts.clearBit(CIdx->getZExtValue());
3462 SkipElt = !DemandedElts[CIdx->getZExtValue()];
3463 }
3464
3465 // Result is zero if Elt is non-zero and rest of the demanded elts in Vec
3466 // are non-zero.
3467 return (SkipElt || isKnownNonZero(Elt, Q, Depth)) &&
3468 (DemandedVecElts.isZero() ||
3469 isKnownNonZero(Vec, DemandedVecElts, Q, Depth));
3470 }
3471 case Instruction::ExtractElement:
3472 if (const auto *EEI = dyn_cast<ExtractElementInst>(I)) {
3473 const Value *Vec = EEI->getVectorOperand();
3474 const Value *Idx = EEI->getIndexOperand();
3475 auto *CIdx = dyn_cast<ConstantInt>(Idx);
3476 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
3477 unsigned NumElts = VecTy->getNumElements();
3478 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
3479 if (CIdx && CIdx->getValue().ult(NumElts))
3480 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
3481 return isKnownNonZero(Vec, DemandedVecElts, Q, Depth);
3482 }
3483 }
3484 break;
3485 case Instruction::ShuffleVector: {
3486 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
3487 if (!Shuf)
3488 break;
3489 APInt DemandedLHS, DemandedRHS;
3490 // For undef elements, we don't know anything about the common state of
3491 // the shuffle result.
3492 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
3493 break;
3494 // If demanded elements for both vecs are non-zero, the shuffle is non-zero.
3495 return (DemandedRHS.isZero() ||
3496 isKnownNonZero(Shuf->getOperand(1), DemandedRHS, Q, Depth)) &&
3497 (DemandedLHS.isZero() ||
3498 isKnownNonZero(Shuf->getOperand(0), DemandedLHS, Q, Depth));
3499 }
3500 case Instruction::Freeze:
3501 return isKnownNonZero(I->getOperand(0), Q, Depth) &&
3502 isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
3503 Depth);
3504 case Instruction::Load: {
3505 auto *LI = cast<LoadInst>(I);
3506 // A Load tagged with nonnull or dereferenceable with null pointer undefined
3507 // is never null.
3508 if (auto *PtrT = dyn_cast<PointerType>(I->getType())) {
3509 if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull) ||
3510 (Q.IIQ.getMetadata(LI, LLVMContext::MD_dereferenceable) &&
3511 !NullPointerIsDefined(LI->getFunction(), PtrT->getAddressSpace())))
3512 return true;
3513 } else if (MDNode *Ranges = Q.IIQ.getMetadata(LI, LLVMContext::MD_range)) {
3515 }
3516
3517 // No need to fall through to computeKnownBits as range metadata is already
3518 // handled in isKnownNonZero.
3519 return false;
3520 }
3521 case Instruction::ExtractValue: {
3522 const WithOverflowInst *WO;
3524 switch (WO->getBinaryOp()) {
3525 default:
3526 break;
3527 case Instruction::Add:
3528 return isNonZeroAdd(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3529 WO->getArgOperand(1),
3530 /*NSW=*/false,
3531 /*NUW=*/false, Depth);
3532 case Instruction::Sub:
3533 return isNonZeroSub(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3534 WO->getArgOperand(1), Depth);
3535 case Instruction::Mul:
3536 return isNonZeroMul(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3537 WO->getArgOperand(1),
3538 /*NSW=*/false, /*NUW=*/false, Depth);
3539 break;
3540 }
3541 }
3542 break;
3543 }
3544 case Instruction::Call:
3545 case Instruction::Invoke: {
3546 const auto *Call = cast<CallBase>(I);
3547 if (I->getType()->isPointerTy()) {
3548 if (Call->isReturnNonNull())
3549 return true;
3550 if (const auto *RP = getArgumentAliasingToReturnedPointer(
3551 Call, /*MustPreserveOffset=*/true))
3552 return isKnownNonZero(RP, Q, Depth);
3553 } else {
3554 if (MDNode *Ranges = Q.IIQ.getMetadata(Call, LLVMContext::MD_range))
3556 if (std::optional<ConstantRange> Range = Call->getRange()) {
3557 const APInt ZeroValue(Range->getBitWidth(), 0);
3558 if (!Range->contains(ZeroValue))
3559 return true;
3560 }
3561 if (const Value *RV = Call->getReturnedArgOperand())
3562 if (RV->getType() == I->getType() && isKnownNonZero(RV, Q, Depth))
3563 return true;
3564 }
3565
3566 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
3567 switch (II->getIntrinsicID()) {
3568 case Intrinsic::sshl_sat:
3569 case Intrinsic::ushl_sat:
3570 case Intrinsic::abs:
3571 case Intrinsic::bitreverse:
3572 case Intrinsic::bswap:
3573 case Intrinsic::ctpop:
3574 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3575 // NB: We don't do usub_sat here as in any case we can prove its
3576 // non-zero, we will fold it to `sub nuw` in InstCombine.
3577 case Intrinsic::ssub_sat:
3578 // For most types, if x != y then ssub.sat x, y != 0. But
3579 // ssub.sat.i1 0, -1 = 0, because 1 saturates to 0. This means
3580 // isNonZeroSub will do the wrong thing for ssub.sat.i1.
3581 if (BitWidth == 1)
3582 return false;
3583 return isNonZeroSub(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3584 II->getArgOperand(1), Depth);
3585 case Intrinsic::sadd_sat:
3586 return isNonZeroAdd(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3587 II->getArgOperand(1),
3588 /*NSW=*/true, /* NUW=*/false, Depth);
3589 // Vec reverse preserves zero/non-zero status from input vec.
3590 case Intrinsic::vector_reverse:
3591 return isKnownNonZero(II->getArgOperand(0), DemandedElts.reverseBits(),
3592 Q, Depth);
3593 // umin/smin/smax/smin/or of all non-zero elements is always non-zero.
3594 case Intrinsic::vector_reduce_or:
3595 case Intrinsic::vector_reduce_umax:
3596 case Intrinsic::vector_reduce_umin:
3597 case Intrinsic::vector_reduce_smax:
3598 case Intrinsic::vector_reduce_smin:
3599 return isKnownNonZero(II->getArgOperand(0), Q, Depth);
3600 case Intrinsic::umax:
3601 case Intrinsic::uadd_sat:
3602 // umax(X, (X != 0)) is non zero
3603 // X +usat (X != 0) is non zero
3604 if (matchOpWithOpEqZero(II->getArgOperand(0), II->getArgOperand(1)))
3605 return true;
3606
3607 return isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth) ||
3608 isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3609 case Intrinsic::smax: {
3610 // If either arg is strictly positive the result is non-zero. Otherwise
3611 // the result is non-zero if both ops are non-zero.
3612 auto IsNonZero = [&](Value *Op, std::optional<bool> &OpNonZero,
3613 const KnownBits &OpKnown) {
3614 if (!OpNonZero.has_value())
3615 OpNonZero = OpKnown.isNonZero() ||
3616 isKnownNonZero(Op, DemandedElts, Q, Depth);
3617 return *OpNonZero;
3618 };
3619 // Avoid re-computing isKnownNonZero.
3620 std::optional<bool> Op0NonZero, Op1NonZero;
3621 KnownBits Op1Known =
3622 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3623 if (Op1Known.isNonNegative() &&
3624 IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known))
3625 return true;
3626 KnownBits Op0Known =
3627 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3628 if (Op0Known.isNonNegative() &&
3629 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known))
3630 return true;
3631 return IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known) &&
3632 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known);
3633 }
3634 case Intrinsic::smin: {
3635 // If either arg is negative the result is non-zero. Otherwise
3636 // the result is non-zero if both ops are non-zero.
3637 KnownBits Op1Known =
3638 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3639 if (Op1Known.isNegative())
3640 return true;
3641 KnownBits Op0Known =
3642 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3643 if (Op0Known.isNegative())
3644 return true;
3645
3646 if (Op1Known.isNonZero() && Op0Known.isNonZero())
3647 return true;
3648 }
3649 [[fallthrough]];
3650 case Intrinsic::umin:
3651 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth) &&
3652 isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth);
3653 case Intrinsic::cttz:
3654 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3655 .Zero[0];
3656 case Intrinsic::ctlz:
3657 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3658 .isNonNegative();
3659 case Intrinsic::fshr:
3660 case Intrinsic::fshl:
3661 // If Op0 == Op1, this is a rotate. rotate(x, y) != 0 iff x != 0.
3662 if (II->getArgOperand(0) == II->getArgOperand(1))
3663 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3664 break;
3665 case Intrinsic::vscale:
3666 return true;
3667 case Intrinsic::experimental_get_vector_length:
3668 return isKnownNonZero(I->getOperand(0), Q, Depth);
3669 default:
3670 break;
3671 }
3672 break;
3673 }
3674
3675 return false;
3676 }
3677 }
3678
3679 KnownBits Known(BitWidth);
3680 computeKnownBits(I, DemandedElts, Known, Q, Depth);
3681 return Known.One != 0;
3682}
3683
3684/// Return true if the given value is known to be non-zero when defined. For
3685/// vectors, return true if every demanded element is known to be non-zero when
3686/// defined. For pointers, if the context instruction and dominator tree are
3687/// specified, perform context-sensitive analysis and return true if the
3688/// pointer couldn't possibly be null at the specified instruction.
3689/// Supports values with integer or pointer type and vectors of integers.
3690bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
3691 const SimplifyQuery &Q, unsigned Depth) {
3692 Type *Ty = V->getType();
3693
3694#ifndef NDEBUG
3695 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
3696
3697 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3698 assert(
3699 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
3700 "DemandedElt width should equal the fixed vector number of elements");
3701 } else {
3702 assert(DemandedElts == APInt(1, 1) &&
3703 "DemandedElt width should be 1 for scalars");
3704 }
3705#endif
3706
3707 if (auto *C = dyn_cast<Constant>(V)) {
3708 if (C->isNullValue())
3709 return false;
3710 if (isa<ConstantInt>(C))
3711 // Must be non-zero due to null test above.
3712 return true;
3713
3714 // For constant vectors, check that all elements are poison or known
3715 // non-zero to determine that the whole vector is known non-zero.
3716 if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
3717 for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
3718 if (!DemandedElts[i])
3719 continue;
3720 Constant *Elt = C->getAggregateElement(i);
3721 if (!Elt || Elt->isNullValue())
3722 return false;
3723 if (!isa<PoisonValue>(Elt) && !isa<ConstantInt>(Elt))
3724 return false;
3725 }
3726 return true;
3727 }
3728
3729 // Constant ptrauth can be null, iff the base pointer can be.
3730 if (auto *CPA = dyn_cast<ConstantPtrAuth>(V))
3731 return isKnownNonZero(CPA->getPointer(), DemandedElts, Q, Depth);
3732
3733 // A global variable in address space 0 is non null unless extern weak
3734 // or an absolute symbol reference. Other address spaces may have null as a
3735 // valid address for a global, so we can't assume anything.
3736 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
3737 if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&
3738 GV->getType()->getAddressSpace() == 0)
3739 return true;
3740 }
3741
3742 // For constant expressions, fall through to the Operator code below.
3743 if (!isa<ConstantExpr>(V))
3744 return false;
3745 }
3746
3747 if (const auto *A = dyn_cast<Argument>(V))
3748 if (std::optional<ConstantRange> Range = A->getRange()) {
3749 const APInt ZeroValue(Range->getBitWidth(), 0);
3750 if (!Range->contains(ZeroValue))
3751 return true;
3752 }
3753
3754 if (!isa<Constant>(V) && isKnownNonZeroFromAssume(V, Q))
3755 return true;
3756
3757 // Some of the tests below are recursive, so bail out if we hit the limit.
3759 return false;
3760
3761 // Check for pointer simplifications.
3762
3763 if (PointerType *PtrTy = dyn_cast<PointerType>(Ty)) {
3764 // A byval, inalloca may not be null in a non-default addres space. A
3765 // nonnull argument is assumed never 0.
3766 if (const Argument *A = dyn_cast<Argument>(V)) {
3767 if (((A->hasPassPointeeByValueCopyAttr() &&
3768 !NullPointerIsDefined(A->getParent(), PtrTy->getAddressSpace())) ||
3769 A->hasNonNullAttr()))
3770 return true;
3771 }
3772 }
3773
3774 if (const auto *I = dyn_cast<Operator>(V))
3775 if (isKnownNonZeroFromOperator(I, DemandedElts, Q, Depth))
3776 return true;
3777
3778 if (!isa<Constant>(V) &&
3780 return true;
3781
3782 if (const Value *Stripped = stripNullTest(V))
3783 return isKnownNonZero(Stripped, DemandedElts, Q, Depth);
3784
3785 return false;
3786}
3787
3789 unsigned Depth) {
3790 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
3791 APInt DemandedElts =
3792 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
3793 return ::isKnownNonZero(V, DemandedElts, Q, Depth);
3794}
3795
3796/// If the pair of operators are the same invertible function, return the
3797/// the operands of the function corresponding to each input. Otherwise,
3798/// return std::nullopt. An invertible function is one that is 1-to-1 and maps
3799/// every input value to exactly one output value. This is equivalent to
3800/// saying that Op1 and Op2 are equal exactly when the specified pair of
3801/// operands are equal, (except that Op1 and Op2 may be poison more often.)
3802static std::optional<std::pair<Value*, Value*>>
3804 const Operator *Op2) {
3805 if (Op1->getOpcode() != Op2->getOpcode())
3806 return std::nullopt;
3807
3808 auto getOperands = [&](unsigned OpNum) -> auto {
3809 return std::make_pair(Op1->getOperand(OpNum), Op2->getOperand(OpNum));
3810 };
3811
3812 switch (Op1->getOpcode()) {
3813 default:
3814 break;
3815 case Instruction::Or:
3816 if (!cast<PossiblyDisjointInst>(Op1)->isDisjoint() ||
3817 !cast<PossiblyDisjointInst>(Op2)->isDisjoint())
3818 break;
3819 [[fallthrough]];
3820 case Instruction::Xor:
3821 case Instruction::Add: {
3822 Value *Other;
3823 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(0)), m_Value(Other))))
3824 return std::make_pair(Op1->getOperand(1), Other);
3825 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(1)), m_Value(Other))))
3826 return std::make_pair(Op1->getOperand(0), Other);
3827 break;
3828 }
3829 case Instruction::Sub:
3830 if (Op1->getOperand(0) == Op2->getOperand(0))
3831 return getOperands(1);
3832 if (Op1->getOperand(1) == Op2->getOperand(1))
3833 return getOperands(0);
3834 break;
3835 case Instruction::Mul: {
3836 // invertible if A * B == (A * B) mod 2^N where A, and B are integers
3837 // and N is the bitwdith. The nsw case is non-obvious, but proven by
3838 // alive2: https://alive2.llvm.org/ce/z/Z6D5qK
3839 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3840 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3841 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3842 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3843 break;
3844
3845 // Assume operand order has been canonicalized
3846 if (Op1->getOperand(1) == Op2->getOperand(1) &&
3847 isa<ConstantInt>(Op1->getOperand(1)) &&
3848 !cast<ConstantInt>(Op1->getOperand(1))->isZero())
3849 return getOperands(0);
3850 break;
3851 }
3852 case Instruction::Shl: {
3853 // Same as multiplies, with the difference that we don't need to check
3854 // for a non-zero multiply. Shifts always multiply by non-zero.
3855 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3856 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3857 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3858 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3859 break;
3860
3861 if (Op1->getOperand(1) == Op2->getOperand(1))
3862 return getOperands(0);
3863 break;
3864 }
3865 case Instruction::AShr:
3866 case Instruction::LShr: {
3867 auto *PEO1 = cast<PossiblyExactOperator>(Op1);
3868 auto *PEO2 = cast<PossiblyExactOperator>(Op2);
3869 if (!PEO1->isExact() || !PEO2->isExact())
3870 break;
3871
3872 if (Op1->getOperand(1) == Op2->getOperand(1))
3873 return getOperands(0);
3874 break;
3875 }
3876 case Instruction::SExt:
3877 case Instruction::ZExt:
3878 if (Op1->getOperand(0)->getType() == Op2->getOperand(0)->getType())
3879 return getOperands(0);
3880 break;
3881 case Instruction::PHI: {
3882 const PHINode *PN1 = cast<PHINode>(Op1);
3883 const PHINode *PN2 = cast<PHINode>(Op2);
3884
3885 // If PN1 and PN2 are both recurrences, can we prove the entire recurrences
3886 // are a single invertible function of the start values? Note that repeated
3887 // application of an invertible function is also invertible
3888 BinaryOperator *BO1 = nullptr;
3889 Value *Start1 = nullptr, *Step1 = nullptr;
3890 BinaryOperator *BO2 = nullptr;
3891 Value *Start2 = nullptr, *Step2 = nullptr;
3892 if (PN1->getParent() != PN2->getParent() ||
3893 !matchSimpleRecurrence(PN1, BO1, Start1, Step1) ||
3894 !matchSimpleRecurrence(PN2, BO2, Start2, Step2))
3895 break;
3896
3897 auto Values = getInvertibleOperands(cast<Operator>(BO1),
3898 cast<Operator>(BO2));
3899 if (!Values)
3900 break;
3901
3902 // We have to be careful of mutually defined recurrences here. Ex:
3903 // * X_i = X_(i-1) OP Y_(i-1), and Y_i = X_(i-1) OP V
3904 // * X_i = Y_i = X_(i-1) OP Y_(i-1)
3905 // The invertibility of these is complicated, and not worth reasoning
3906 // about (yet?).
3907 if (Values->first != PN1 || Values->second != PN2)
3908 break;
3909
3910 return std::make_pair(Start1, Start2);
3911 }
3912 }
3913 return std::nullopt;
3914}
3915
3916/// Return true if V1 == (binop V2, X), where X is known non-zero.
3917/// Only handle a small subset of binops where (binop V2, X) with non-zero X
3918/// implies V2 != V1.
3919static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2,
3920 const APInt &DemandedElts,
3921 const SimplifyQuery &Q, unsigned Depth) {
3923 if (!BO)
3924 return false;
3925 switch (BO->getOpcode()) {
3926 default:
3927 break;
3928 case Instruction::Or:
3929 if (!cast<PossiblyDisjointInst>(V1)->isDisjoint())
3930 break;
3931 [[fallthrough]];
3932 case Instruction::Xor:
3933 case Instruction::Add:
3934 Value *Op = nullptr;
3935 if (V2 == BO->getOperand(0))
3936 Op = BO->getOperand(1);
3937 else if (V2 == BO->getOperand(1))
3938 Op = BO->getOperand(0);
3939 else
3940 return false;
3941 return isKnownNonZero(Op, DemandedElts, Q, Depth + 1);
3942 }
3943 return false;
3944}
3945
3946/// Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and
3947/// the multiplication is nuw or nsw.
3948static bool isNonEqualMul(const Value *V1, const Value *V2,
3949 const APInt &DemandedElts, const SimplifyQuery &Q,
3950 unsigned Depth) {
3951 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3952 const APInt *C;
3953 return match(OBO, m_Mul(m_Specific(V1), m_APInt(C))) &&
3954 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3955 !C->isZero() && !C->isOne() &&
3956 isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3957 }
3958 return false;
3959}
3960
3961/// Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and
3962/// the shift is nuw or nsw.
3963static bool isNonEqualShl(const Value *V1, const Value *V2,
3964 const APInt &DemandedElts, const SimplifyQuery &Q,
3965 unsigned Depth) {
3966 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3967 const APInt *C;
3968 return match(OBO, m_Shl(m_Specific(V1), m_APInt(C))) &&
3969 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3970 !C->isZero() && isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3971 }
3972 return false;
3973}
3974
3975static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2,
3976 const APInt &DemandedElts, const SimplifyQuery &Q,
3977 unsigned Depth) {
3978 // Check two PHIs are in same block.
3979 if (PN1->getParent() != PN2->getParent())
3980 return false;
3981
3983 bool UsedFullRecursion = false;
3984 for (const BasicBlock *IncomBB : PN1->blocks()) {
3985 if (!VisitedBBs.insert(IncomBB).second)
3986 continue; // Don't reprocess blocks that we have dealt with already.
3987 const Value *IV1 = PN1->getIncomingValueForBlock(IncomBB);
3988 const Value *IV2 = PN2->getIncomingValueForBlock(IncomBB);
3989 const APInt *C1, *C2;
3990 if (match(IV1, m_APInt(C1)) && match(IV2, m_APInt(C2)) && *C1 != *C2)
3991 continue;
3992
3993 // Only one pair of phi operands is allowed for full recursion.
3994 if (UsedFullRecursion)
3995 return false;
3996
3998 RecQ.CxtI = IncomBB->getTerminator();
3999 if (!isKnownNonEqual(IV1, IV2, DemandedElts, RecQ, Depth + 1))
4000 return false;
4001 UsedFullRecursion = true;
4002 }
4003 return true;
4004}
4005
4006static bool isNonEqualSelect(const Value *V1, const Value *V2,
4007 const APInt &DemandedElts, const SimplifyQuery &Q,
4008 unsigned Depth) {
4009 const SelectInst *SI1 = dyn_cast<SelectInst>(V1);
4010 if (!SI1)
4011 return false;
4012
4013 if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2)) {
4014 const Value *Cond1 = SI1->getCondition();
4015 const Value *Cond2 = SI2->getCondition();
4016 if (Cond1 == Cond2)
4017 return isKnownNonEqual(SI1->getTrueValue(), SI2->getTrueValue(),
4018 DemandedElts, Q, Depth + 1) &&
4019 isKnownNonEqual(SI1->getFalseValue(), SI2->getFalseValue(),
4020 DemandedElts, Q, Depth + 1);
4021 }
4022 return isKnownNonEqual(SI1->getTrueValue(), V2, DemandedElts, Q, Depth + 1) &&
4023 isKnownNonEqual(SI1->getFalseValue(), V2, DemandedElts, Q, Depth + 1);
4024}
4025
4026// Check to see if A is both a GEP and is the incoming value for a PHI in the
4027// loop, and B is either a ptr or another GEP. If the PHI has 2 incoming values,
4028// one of them being the recursive GEP A and the other a ptr at same base and at
4029// the same/higher offset than B we are only incrementing the pointer further in
4030// loop if offset of recursive GEP is greater than 0.
4032 const SimplifyQuery &Q) {
4033 if (!A->getType()->isPointerTy() || !B->getType()->isPointerTy())
4034 return false;
4035
4036 auto *GEPA = dyn_cast<GEPOperator>(A);
4037 if (!GEPA || GEPA->getNumIndices() != 1 || !isa<Constant>(GEPA->idx_begin()))
4038 return false;
4039
4040 // Handle 2 incoming PHI values with one being a recursive GEP.
4041 auto *PN = dyn_cast<PHINode>(GEPA->getPointerOperand());
4042 if (!PN || PN->getNumIncomingValues() != 2)
4043 return false;
4044
4045 // Search for the recursive GEP as an incoming operand, and record that as
4046 // Step.
4047 Value *Start = nullptr;
4048 Value *Step = const_cast<Value *>(A);
4049 if (PN->getIncomingValue(0) == Step)
4050 Start = PN->getIncomingValue(1);
4051 else if (PN->getIncomingValue(1) == Step)
4052 Start = PN->getIncomingValue(0);
4053 else
4054 return false;
4055
4056 // Other incoming node base should match the B base.
4057 // StartOffset >= OffsetB && StepOffset > 0?
4058 // StartOffset <= OffsetB && StepOffset < 0?
4059 // Is non-equal if above are true.
4060 // We use stripAndAccumulateInBoundsConstantOffsets to restrict the
4061 // optimisation to inbounds GEPs only.
4062 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(Start->getType());
4063 APInt StartOffset(IndexWidth, 0);
4064 Start = Start->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StartOffset);
4065 APInt StepOffset(IndexWidth, 0);
4066 Step = Step->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StepOffset);
4067
4068 // Check if Base Pointer of Step matches the PHI.
4069 if (Step != PN)
4070 return false;
4071 APInt OffsetB(IndexWidth, 0);
4072 B = B->stripAndAccumulateInBoundsConstantOffsets(Q.DL, OffsetB);
4073 return Start == B &&
4074 ((StartOffset.sge(OffsetB) && StepOffset.isStrictlyPositive()) ||
4075 (StartOffset.sle(OffsetB) && StepOffset.isNegative()));
4076}
4077
4078static bool isKnownNonEqualFromContext(const Value *V1, const Value *V2,
4079 const SimplifyQuery &Q, unsigned Depth) {
4080 if (!Q.CxtI)
4081 return false;
4082
4083 // Try to infer NonEqual based on information from dominating conditions.
4084 if (Q.DC && Q.DT) {
4085 auto IsKnownNonEqualFromDominatingCondition = [&](const Value *V) {
4086 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
4087 Value *Cond = BI->getCondition();
4088 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4089 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()) &&
4091 /*LHSIsTrue=*/true, Depth)
4092 .value_or(false))
4093 return true;
4094
4095 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4096 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()) &&
4098 /*LHSIsTrue=*/false, Depth)
4099 .value_or(false))
4100 return true;
4101 }
4102
4103 return false;
4104 };
4105
4106 if (IsKnownNonEqualFromDominatingCondition(V1) ||
4107 IsKnownNonEqualFromDominatingCondition(V2))
4108 return true;
4109 }
4110
4111 if (!Q.AC)
4112 return false;
4113
4114 // Try to infer NonEqual based on information from assumptions.
4115 for (auto &AssumeVH : Q.AC->assumptionsFor(V1)) {
4116 if (!AssumeVH)
4117 continue;
4118 CallInst *I = cast<CallInst>(AssumeVH);
4119
4120 assert(I->getFunction() == Q.CxtI->getFunction() &&
4121 "Got assumption for the wrong function!");
4122 assert(I->getIntrinsicID() == Intrinsic::assume &&
4123 "must be an assume intrinsic");
4124
4125 if (isImpliedCondition(I->getArgOperand(0), ICmpInst::ICMP_NE, V1, V2, Q.DL,
4126 /*LHSIsTrue=*/true, Depth)
4127 .value_or(false) &&
4129 return true;
4130 }
4131
4132 return false;
4133}
4134
4135/// Return true if it is known that V1 != V2.
4136static bool isKnownNonEqual(const Value *V1, const Value *V2,
4137 const APInt &DemandedElts, const SimplifyQuery &Q,
4138 unsigned Depth) {
4139 if (V1 == V2)
4140 return false;
4141 if (V1->getType() != V2->getType())
4142 // We can't look through casts yet.
4143 return false;
4144
4146 return false;
4147
4148 // See if we can recurse through (exactly one of) our operands. This
4149 // requires our operation be 1-to-1 and map every input value to exactly
4150 // one output value. Such an operation is invertible.
4151 auto *O1 = dyn_cast<Operator>(V1);
4152 auto *O2 = dyn_cast<Operator>(V2);
4153 if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) {
4154 if (auto Values = getInvertibleOperands(O1, O2))
4155 return isKnownNonEqual(Values->first, Values->second, DemandedElts, Q,
4156 Depth + 1);
4157
4158 if (const PHINode *PN1 = dyn_cast<PHINode>(V1)) {
4159 const PHINode *PN2 = cast<PHINode>(V2);
4160 // FIXME: This is missing a generalization to handle the case where one is
4161 // a PHI and another one isn't.
4162 if (isNonEqualPHIs(PN1, PN2, DemandedElts, Q, Depth))
4163 return true;
4164 };
4165 }
4166
4167 if (isModifyingBinopOfNonZero(V1, V2, DemandedElts, Q, Depth) ||
4168 isModifyingBinopOfNonZero(V2, V1, DemandedElts, Q, Depth))
4169 return true;
4170
4171 if (isNonEqualMul(V1, V2, DemandedElts, Q, Depth) ||
4172 isNonEqualMul(V2, V1, DemandedElts, Q, Depth))
4173 return true;
4174
4175 if (isNonEqualShl(V1, V2, DemandedElts, Q, Depth) ||
4176 isNonEqualShl(V2, V1, DemandedElts, Q, Depth))
4177 return true;
4178
4179 if (V1->getType()->isIntOrIntVectorTy()) {
4180 // Are any known bits in V1 contradictory to known bits in V2? If V1
4181 // has a known zero where V2 has a known one, they must not be equal.
4182 KnownBits Known1 = computeKnownBits(V1, DemandedElts, Q, Depth);
4183 if (!Known1.isUnknown()) {
4184 KnownBits Known2 = computeKnownBits(V2, DemandedElts, Q, Depth);
4185 if (Known1.Zero.intersects(Known2.One) ||
4186 Known2.Zero.intersects(Known1.One))
4187 return true;
4188 }
4189 }
4190
4191 if (isNonEqualSelect(V1, V2, DemandedElts, Q, Depth) ||
4192 isNonEqualSelect(V2, V1, DemandedElts, Q, Depth))
4193 return true;
4194
4195 if (isNonEqualPointersWithRecursiveGEP(V1, V2, Q) ||
4197 return true;
4198
4199 Value *A, *B;
4200 // PtrToInts are NonEqual if their Ptrs are NonEqual.
4201 // Check PtrToInt type matches the pointer size.
4202 if (match(V1, m_PtrToIntSameSize(Q.DL, m_Value(A))) &&
4204 return isKnownNonEqual(A, B, DemandedElts, Q, Depth + 1);
4205
4206 if (isKnownNonEqualFromContext(V1, V2, Q, Depth))
4207 return true;
4208
4209 return false;
4210}
4211
4212/// For vector constants, loop over the elements and find the constant with the
4213/// minimum number of sign bits. Return 0 if the value is not a vector constant
4214/// or if any element was not analyzed; otherwise, return the count for the
4215/// element with the minimum number of sign bits.
4217 const APInt &DemandedElts,
4218 unsigned TyBits) {
4219 const auto *CV = dyn_cast<Constant>(V);
4220 if (!CV || !isa<FixedVectorType>(CV->getType()))
4221 return 0;
4222
4223 unsigned MinSignBits = TyBits;
4224 unsigned NumElts = cast<FixedVectorType>(CV->getType())->getNumElements();
4225 for (unsigned i = 0; i != NumElts; ++i) {
4226 if (!DemandedElts[i])
4227 continue;
4228 // If we find a non-ConstantInt, bail out.
4229 auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));
4230 if (!Elt)
4231 return 0;
4232
4233 MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());
4234 }
4235
4236 return MinSignBits;
4237}
4238
4239static unsigned ComputeNumSignBitsImpl(const Value *V,
4240 const APInt &DemandedElts,
4241 const SimplifyQuery &Q, unsigned Depth);
4242
4243static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
4244 const SimplifyQuery &Q, unsigned Depth) {
4245 unsigned Result = ComputeNumSignBitsImpl(V, DemandedElts, Q, Depth);
4246 assert(Result > 0 && "At least one sign bit needs to be present!");
4247 return Result;
4248}
4249
4250/// Return the number of times the sign bit of the register is replicated into
4251/// the other bits. We know that at least 1 bit is always equal to the sign bit
4252/// (itself), but other cases can give us information. For example, immediately
4253/// after an "ashr X, 2", we know that the top 3 bits are all equal to each
4254/// other, so we return 3. For vectors, return the number of sign bits for the
4255/// vector element with the minimum number of known sign bits of the demanded
4256/// elements in the vector specified by DemandedElts.
4257static unsigned ComputeNumSignBitsImpl(const Value *V,
4258 const APInt &DemandedElts,
4259 const SimplifyQuery &Q, unsigned Depth) {
4260 Type *Ty = V->getType();
4261#ifndef NDEBUG
4262 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4263
4264 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
4265 assert(
4266 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
4267 "DemandedElt width should equal the fixed vector number of elements");
4268 } else {
4269 assert(DemandedElts == APInt(1, 1) &&
4270 "DemandedElt width should be 1 for scalars");
4271 }
4272#endif
4273
4274 // We return the minimum number of sign bits that are guaranteed to be present
4275 // in V, so for undef we have to conservatively return 1. We don't have the
4276 // same behavior for poison though -- that's a FIXME today.
4277
4278 Type *ScalarTy = Ty->getScalarType();
4279 unsigned TyBits = ScalarTy->isPointerTy() ?
4280 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
4281 Q.DL.getTypeSizeInBits(ScalarTy);
4282
4283 unsigned Tmp, Tmp2;
4284 unsigned FirstAnswer = 1;
4285
4286 // Note that ConstantInt is handled by the general computeKnownBits case
4287 // below.
4288
4290 return 1;
4291
4292 if (auto *U = dyn_cast<Operator>(V)) {
4293 switch (Operator::getOpcode(V)) {
4294 default: break;
4295 case Instruction::BitCast: {
4296 Value *Src = U->getOperand(0);
4297 Type *SrcTy = Src->getType();
4298
4299 // Skip if the source type is not an integer or integer vector type
4300 // This ensures we only process integer-like types
4301 if (!SrcTy->isIntOrIntVectorTy())
4302 break;
4303
4304 unsigned SrcBits = SrcTy->getScalarSizeInBits();
4305
4306 // Bitcast 'large element' scalar/vector to 'small element' vector.
4307 if ((SrcBits % TyBits) != 0)
4308 break;
4309
4310 // Only proceed if the destination type is a fixed-size vector
4311 if (isa<FixedVectorType>(Ty)) {
4312 // Fast case - sign splat can be simply split across the small elements.
4313 // This works for both vector and scalar sources
4314 Tmp = ComputeNumSignBits(Src, Q, Depth + 1);
4315 if (Tmp == SrcBits)
4316 return TyBits;
4317 }
4318 break;
4319 }
4320 case Instruction::SExt:
4321 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
4322 return ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1) +
4323 Tmp;
4324
4325 case Instruction::SDiv: {
4326 const APInt *Denominator;
4327 // sdiv X, C -> adds log(C) sign bits.
4328 if (match(U->getOperand(1), m_APInt(Denominator))) {
4329
4330 // Ignore non-positive denominator.
4331 if (!Denominator->isStrictlyPositive())
4332 break;
4333
4334 // Calculate the incoming numerator bits.
4335 unsigned NumBits =
4336 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4337
4338 // Add floor(log(C)) bits to the numerator bits.
4339 return std::min(TyBits, NumBits + Denominator->logBase2());
4340 }
4341 break;
4342 }
4343
4344 case Instruction::SRem: {
4345 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4346
4347 const APInt *Denominator;
4348 // srem X, C -> we know that the result is within [-C+1,C) when C is a
4349 // positive constant. This let us put a lower bound on the number of sign
4350 // bits.
4351 if (match(U->getOperand(1), m_APInt(Denominator))) {
4352
4353 // Ignore non-positive denominator.
4354 if (Denominator->isStrictlyPositive()) {
4355 // Calculate the leading sign bit constraints by examining the
4356 // denominator. Given that the denominator is positive, there are two
4357 // cases:
4358 //
4359 // 1. The numerator is positive. The result range is [0,C) and
4360 // [0,C) u< (1 << ceilLogBase2(C)).
4361 //
4362 // 2. The numerator is negative. Then the result range is (-C,0] and
4363 // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
4364 //
4365 // Thus a lower bound on the number of sign bits is `TyBits -
4366 // ceilLogBase2(C)`.
4367
4368 unsigned ResBits = TyBits - Denominator->ceilLogBase2();
4369 Tmp = std::max(Tmp, ResBits);
4370 }
4371 }
4372 return Tmp;
4373 }
4374
4375 case Instruction::AShr: {
4376 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4377 // ashr X, C -> adds C sign bits. Vectors too.
4378 const APInt *ShAmt;
4379 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4380 if (ShAmt->uge(TyBits))
4381 break; // Bad shift.
4382 unsigned ShAmtLimited = ShAmt->getZExtValue();
4383 Tmp += ShAmtLimited;
4384 if (Tmp > TyBits) Tmp = TyBits;
4385 }
4386 return Tmp;
4387 }
4388 case Instruction::Shl: {
4389 const APInt *ShAmt;
4390 Value *X = nullptr;
4391 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4392 // shl destroys sign bits.
4393 if (ShAmt->uge(TyBits))
4394 break; // Bad shift.
4395 // We can look through a zext (more or less treating it as a sext) if
4396 // all extended bits are shifted out.
4397 if (match(U->getOperand(0), m_ZExt(m_Value(X))) &&
4398 ShAmt->uge(TyBits - X->getType()->getScalarSizeInBits())) {
4399 Tmp = ComputeNumSignBits(X, DemandedElts, Q, Depth + 1);
4400 Tmp += TyBits - X->getType()->getScalarSizeInBits();
4401 } else
4402 Tmp =
4403 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4404 if (ShAmt->uge(Tmp))
4405 break; // Shifted all sign bits out.
4406 Tmp2 = ShAmt->getZExtValue();
4407 return Tmp - Tmp2;
4408 }
4409 break;
4410 }
4411 case Instruction::And:
4412 case Instruction::Or:
4413 case Instruction::Xor: // NOT is handled here.
4414 // Logical binary ops preserve the number of sign bits at the worst.
4415 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4416 if (Tmp != 1) {
4417 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4418 FirstAnswer = std::min(Tmp, Tmp2);
4419 // We computed what we know about the sign bits as our first
4420 // answer. Now proceed to the generic code that uses
4421 // computeKnownBits, and pick whichever answer is better.
4422 }
4423 break;
4424
4425 case Instruction::Select: {
4426 // If we have a clamp pattern, we know that the number of sign bits will
4427 // be the minimum of the clamp min/max range.
4428 const Value *X;
4429 const APInt *CLow, *CHigh;
4430 if (isSignedMinMaxClamp(U, X, CLow, CHigh))
4431 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4432
4433 Tmp = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4434 if (Tmp == 1)
4435 break;
4436 Tmp2 = ComputeNumSignBits(U->getOperand(2), DemandedElts, Q, Depth + 1);
4437 return std::min(Tmp, Tmp2);
4438 }
4439
4440 case Instruction::Add:
4441 // Add can have at most one carry bit. Thus we know that the output
4442 // is, at worst, one more bit than the inputs.
4443 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4444 if (Tmp == 1) break;
4445
4446 // Special case decrementing a value (ADD X, -1):
4447 if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
4448 if (CRHS->isAllOnesValue()) {
4449 KnownBits Known(TyBits);
4450 computeKnownBits(U->getOperand(0), DemandedElts, Known, Q, Depth + 1);
4451
4452 // If the input is known to be 0 or 1, the output is 0/-1, which is
4453 // all sign bits set.
4454 if ((Known.Zero | 1).isAllOnes())
4455 return TyBits;
4456
4457 // If we are subtracting one from a positive number, there is no carry
4458 // out of the result.
4459 if (Known.isNonNegative())
4460 return Tmp;
4461 }
4462
4463 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4464 if (Tmp2 == 1)
4465 break;
4466 return std::min(Tmp, Tmp2) - 1;
4467
4468 case Instruction::Sub:
4469 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4470 if (Tmp2 == 1)
4471 break;
4472
4473 // Handle NEG.
4474 if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
4475 if (CLHS->isNullValue()) {
4476 KnownBits Known(TyBits);
4477 computeKnownBits(U->getOperand(1), DemandedElts, Known, Q, Depth + 1);
4478 // If the input is known to be 0 or 1, the output is 0/-1, which is
4479 // all sign bits set.
4480 if ((Known.Zero | 1).isAllOnes())
4481 return TyBits;
4482
4483 // If the input is known to be positive (the sign bit is known clear),
4484 // the output of the NEG has the same number of sign bits as the
4485 // input.
4486 if (Known.isNonNegative())
4487 return Tmp2;
4488
4489 // Otherwise, we treat this like a SUB.
4490 }
4491
4492 // Sub can have at most one carry bit. Thus we know that the output
4493 // is, at worst, one more bit than the inputs.
4494 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4495 if (Tmp == 1)
4496 break;
4497 return std::min(Tmp, Tmp2) - 1;
4498
4499 case Instruction::Mul: {
4500 // The output of the Mul can be at most twice the valid bits in the
4501 // inputs.
4502 unsigned SignBitsOp0 =
4503 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4504 if (SignBitsOp0 == 1)
4505 break;
4506 unsigned SignBitsOp1 =
4507 ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4508 if (SignBitsOp1 == 1)
4509 break;
4510 unsigned OutValidBits =
4511 (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);
4512 return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
4513 }
4514
4515 case Instruction::PHI: {
4516 const PHINode *PN = cast<PHINode>(U);
4517 unsigned NumIncomingValues = PN->getNumIncomingValues();
4518 // Don't analyze large in-degree PHIs.
4519 if (NumIncomingValues > 4) break;
4520 // Unreachable blocks may have zero-operand PHI nodes.
4521 if (NumIncomingValues == 0) break;
4522
4523 // Take the minimum of all incoming values. This can't infinitely loop
4524 // because of our depth threshold.
4526 Tmp = TyBits;
4527 for (unsigned i = 0, e = NumIncomingValues; i != e; ++i) {
4528 if (Tmp == 1) return Tmp;
4529 RecQ.CxtI = PN->getIncomingBlock(i)->getTerminator();
4530 Tmp = std::min(Tmp, ComputeNumSignBits(PN->getIncomingValue(i),
4531 DemandedElts, RecQ, Depth + 1));
4532 }
4533 return Tmp;
4534 }
4535
4536 case Instruction::Trunc: {
4537 // If the input contained enough sign bits that some remain after the
4538 // truncation, then we can make use of that. Otherwise we don't know
4539 // anything.
4540 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4541 unsigned OperandTyBits = U->getOperand(0)->getType()->getScalarSizeInBits();
4542 if (Tmp > (OperandTyBits - TyBits))
4543 return Tmp - (OperandTyBits - TyBits);
4544
4545 return 1;
4546 }
4547
4548 case Instruction::ExtractElement:
4549 // Look through extract element. At the moment we keep this simple and
4550 // skip tracking the specific element. But at least we might find
4551 // information valid for all elements of the vector (for example if vector
4552 // is sign extended, shifted, etc).
4553 return ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4554
4555 case Instruction::ShuffleVector: {
4556 // Collect the minimum number of sign bits that are shared by every vector
4557 // element referenced by the shuffle.
4558 auto *Shuf = dyn_cast<ShuffleVectorInst>(U);
4559 if (!Shuf) {
4560 // FIXME: Add support for shufflevector constant expressions.
4561 return 1;
4562 }
4563 APInt DemandedLHS, DemandedRHS;
4564 // For undef elements, we don't know anything about the common state of
4565 // the shuffle result.
4566 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
4567 return 1;
4568 Tmp = std::numeric_limits<unsigned>::max();
4569 if (!!DemandedLHS) {
4570 const Value *LHS = Shuf->getOperand(0);
4571 Tmp = ComputeNumSignBits(LHS, DemandedLHS, Q, Depth + 1);
4572 }
4573 // If we don't know anything, early out and try computeKnownBits
4574 // fall-back.
4575 if (Tmp == 1)
4576 break;
4577 if (!!DemandedRHS) {
4578 const Value *RHS = Shuf->getOperand(1);
4579 Tmp2 = ComputeNumSignBits(RHS, DemandedRHS, Q, Depth + 1);
4580 Tmp = std::min(Tmp, Tmp2);
4581 }
4582 // If we don't know anything, early out and try computeKnownBits
4583 // fall-back.
4584 if (Tmp == 1)
4585 break;
4586 assert(Tmp <= TyBits && "Failed to determine minimum sign bits");
4587 return Tmp;
4588 }
4589 case Instruction::Call: {
4590 if (const auto *II = dyn_cast<IntrinsicInst>(U)) {
4591 switch (II->getIntrinsicID()) {
4592 default:
4593 break;
4594 case Intrinsic::abs:
4595 Tmp =
4596 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4597 if (Tmp == 1)
4598 break;
4599
4600 // Absolute value reduces number of sign bits by at most 1.
4601 return Tmp - 1;
4602 case Intrinsic::smin:
4603 case Intrinsic::smax: {
4604 const APInt *CLow, *CHigh;
4605 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
4606 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4607 }
4608 }
4609 }
4610 }
4611 }
4612 }
4613
4614 // Finally, if we can prove that the top bits of the result are 0's or 1's,
4615 // use this information.
4616
4617 // If we can examine all elements of a vector constant successfully, we're
4618 // done (we can't do any better than that). If not, keep trying.
4619 if (unsigned VecSignBits =
4620 computeNumSignBitsVectorConstant(V, DemandedElts, TyBits))
4621 return VecSignBits;
4622
4623 KnownBits Known(TyBits);
4624 computeKnownBits(V, DemandedElts, Known, Q, Depth);
4625
4626 // If we know that the sign bit is either zero or one, determine the number of
4627 // identical bits in the top of the input value.
4628 return std::max(FirstAnswer, Known.countMinSignBits());
4629}
4630
4632 const TargetLibraryInfo *TLI) {
4633 const Function *F = CB.getCalledFunction();
4634 if (!F)
4636
4637 if (F->isIntrinsic())
4638 return F->getIntrinsicID();
4639
4640 // We are going to infer semantics of a library function based on mapping it
4641 // to an LLVM intrinsic. Check that the library function is available from
4642 // this callbase and in this environment.
4643 LibFunc Func;
4644 if (F->hasLocalLinkage() || !TLI || !TLI->getLibFunc(CB, Func) ||
4645 !CB.onlyReadsMemory())
4647
4648 switch (Func) {
4649 default:
4650 break;
4651 case LibFunc_sin:
4652 case LibFunc_sinf:
4653 case LibFunc_sinl:
4654 return Intrinsic::sin;
4655 case LibFunc_cos:
4656 case LibFunc_cosf:
4657 case LibFunc_cosl:
4658 return Intrinsic::cos;
4659 case LibFunc_tan:
4660 case LibFunc_tanf:
4661 case LibFunc_tanl:
4662 return Intrinsic::tan;
4663 case LibFunc_asin:
4664 case LibFunc_asinf:
4665 case LibFunc_asinl:
4666 return Intrinsic::asin;
4667 case LibFunc_acos:
4668 case LibFunc_acosf:
4669 case LibFunc_acosl:
4670 return Intrinsic::acos;
4671 case LibFunc_atan:
4672 case LibFunc_atanf:
4673 case LibFunc_atanl:
4674 return Intrinsic::atan;
4675 case LibFunc_atan2:
4676 case LibFunc_atan2f:
4677 case LibFunc_atan2l:
4678 return Intrinsic::atan2;
4679 case LibFunc_sinh:
4680 case LibFunc_sinhf:
4681 case LibFunc_sinhl:
4682 return Intrinsic::sinh;
4683 case LibFunc_cosh:
4684 case LibFunc_coshf:
4685 case LibFunc_coshl:
4686 return Intrinsic::cosh;
4687 case LibFunc_tanh:
4688 case LibFunc_tanhf:
4689 case LibFunc_tanhl:
4690 return Intrinsic::tanh;
4691 case LibFunc_exp:
4692 case LibFunc_expf:
4693 case LibFunc_expl:
4694 return Intrinsic::exp;
4695 case LibFunc_exp2:
4696 case LibFunc_exp2f:
4697 case LibFunc_exp2l:
4698 return Intrinsic::exp2;
4699 case LibFunc_exp10:
4700 case LibFunc_exp10f:
4701 case LibFunc_exp10l:
4702 return Intrinsic::exp10;
4703 case LibFunc_log:
4704 case LibFunc_logf:
4705 case LibFunc_logl:
4706 return Intrinsic::log;
4707 case LibFunc_log10:
4708 case LibFunc_log10f:
4709 case LibFunc_log10l:
4710 return Intrinsic::log10;
4711 case LibFunc_log2:
4712 case LibFunc_log2f:
4713 case LibFunc_log2l:
4714 return Intrinsic::log2;
4715 case LibFunc_fabs:
4716 case LibFunc_fabsf:
4717 case LibFunc_fabsl:
4718 return Intrinsic::fabs;
4719 case LibFunc_fmin:
4720 case LibFunc_fminf:
4721 case LibFunc_fminl:
4722 return Intrinsic::minnum;
4723 case LibFunc_fmax:
4724 case LibFunc_fmaxf:
4725 case LibFunc_fmaxl:
4726 return Intrinsic::maxnum;
4727 case LibFunc_copysign:
4728 case LibFunc_copysignf:
4729 case LibFunc_copysignl:
4730 return Intrinsic::copysign;
4731 case LibFunc_floor:
4732 case LibFunc_floorf:
4733 case LibFunc_floorl:
4734 return Intrinsic::floor;
4735 case LibFunc_ceil:
4736 case LibFunc_ceilf:
4737 case LibFunc_ceill:
4738 return Intrinsic::ceil;
4739 case LibFunc_trunc:
4740 case LibFunc_truncf:
4741 case LibFunc_truncl:
4742 return Intrinsic::trunc;
4743 case LibFunc_rint:
4744 case LibFunc_rintf:
4745 case LibFunc_rintl:
4746 return Intrinsic::rint;
4747 case LibFunc_nearbyint:
4748 case LibFunc_nearbyintf:
4749 case LibFunc_nearbyintl:
4750 return Intrinsic::nearbyint;
4751 case LibFunc_round:
4752 case LibFunc_roundf:
4753 case LibFunc_roundl:
4754 return Intrinsic::round;
4755 case LibFunc_roundeven:
4756 case LibFunc_roundevenf:
4757 case LibFunc_roundevenl:
4758 return Intrinsic::roundeven;
4759 case LibFunc_pow:
4760 case LibFunc_powf:
4761 case LibFunc_powl:
4762 return Intrinsic::pow;
4763 case LibFunc_sqrt:
4764 case LibFunc_sqrtf:
4765 case LibFunc_sqrtl:
4766 return Intrinsic::sqrt;
4767 }
4768
4770}
4771
4772/// Given an exploded icmp instruction, return true if the comparison only
4773/// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if
4774/// the result of the comparison is true when the input value is signed.
4776 bool &TrueIfSigned) {
4777 switch (Pred) {
4778 case ICmpInst::ICMP_SLT: // True if LHS s< 0
4779 TrueIfSigned = true;
4780 return RHS.isZero();
4781 case ICmpInst::ICMP_SLE: // True if LHS s<= -1
4782 TrueIfSigned = true;
4783 return RHS.isAllOnes();
4784 case ICmpInst::ICMP_SGT: // True if LHS s> -1
4785 TrueIfSigned = false;
4786 return RHS.isAllOnes();
4787 case ICmpInst::ICMP_SGE: // True if LHS s>= 0
4788 TrueIfSigned = false;
4789 return RHS.isZero();
4790 case ICmpInst::ICMP_UGT:
4791 // True if LHS u> RHS and RHS == sign-bit-mask - 1
4792 TrueIfSigned = true;
4793 return RHS.isMaxSignedValue();
4794 case ICmpInst::ICMP_UGE:
4795 // True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4796 TrueIfSigned = true;
4797 return RHS.isMinSignedValue();
4798 case ICmpInst::ICMP_ULT:
4799 // True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4800 TrueIfSigned = false;
4801 return RHS.isMinSignedValue();
4802 case ICmpInst::ICMP_ULE:
4803 // True if LHS u<= RHS and RHS == sign-bit-mask - 1
4804 TrueIfSigned = false;
4805 return RHS.isMaxSignedValue();
4806 default:
4807 return false;
4808 }
4809}
4810
4812 bool CondIsTrue,
4813 const Instruction *CxtI,
4814 KnownFPClass &KnownFromContext,
4815 unsigned Depth = 0) {
4816 Value *A, *B;
4818 (CondIsTrue ? match(Cond, m_LogicalAnd(m_Value(A), m_Value(B)))
4819 : match(Cond, m_LogicalOr(m_Value(A), m_Value(B))))) {
4820 computeKnownFPClassFromCond(V, A, CondIsTrue, CxtI, KnownFromContext,
4821 Depth + 1);
4822 computeKnownFPClassFromCond(V, B, CondIsTrue, CxtI, KnownFromContext,
4823 Depth + 1);
4824 return;
4825 }
4827 computeKnownFPClassFromCond(V, A, !CondIsTrue, CxtI, KnownFromContext,
4828 Depth + 1);
4829 return;
4830 }
4831 CmpPredicate Pred;
4832 Value *LHS;
4833 uint64_t ClassVal = 0;
4834 const APFloat *CRHS;
4835 const APInt *RHS;
4836 if (match(Cond, m_FCmp(Pred, m_Value(LHS), m_APFloat(CRHS)))) {
4837 auto [CmpVal, MaskIfTrue, MaskIfFalse] = fcmpImpliesClass(
4838 Pred, *cast<Instruction>(Cond)->getParent()->getParent(), LHS, *CRHS,
4839 LHS != V);
4840 if (CmpVal == V)
4841 KnownFromContext.knownNot(~(CondIsTrue ? MaskIfTrue : MaskIfFalse));
4843 m_Specific(V), m_ConstantInt(ClassVal)))) {
4844 FPClassTest Mask = static_cast<FPClassTest>(ClassVal);
4845 KnownFromContext.knownNot(CondIsTrue ? ~Mask : Mask);
4846 } else if (match(Cond, m_ICmp(Pred, m_ElementWiseBitCast(m_Specific(V)),
4847 m_APInt(RHS)))) {
4848 bool TrueIfSigned;
4849 if (!isSignBitCheck(Pred, *RHS, TrueIfSigned))
4850 return;
4851 if (TrueIfSigned == CondIsTrue)
4852 KnownFromContext.signBitMustBeOne();
4853 else
4854 KnownFromContext.signBitMustBeZero();
4855 }
4856}
4857
4859 const SimplifyQuery &Q) {
4860 KnownFPClass KnownFromContext;
4861
4862 if (Q.CC && Q.CC->AffectedValues.contains(V))
4864 KnownFromContext);
4865
4866 if (!Q.CxtI)
4867 return KnownFromContext;
4868
4869 if (Q.DC && Q.DT) {
4870 // Handle dominating conditions.
4871 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
4872 Value *Cond = BI->getCondition();
4873
4874 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4875 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
4876 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/true, Q.CxtI,
4877 KnownFromContext);
4878
4879 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4880 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
4881 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/false, Q.CxtI,
4882 KnownFromContext);
4883 }
4884 }
4885
4886 if (!Q.AC)
4887 return KnownFromContext;
4888
4889 // Try to restrict the floating-point classes based on information from
4890 // assumptions.
4891 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
4892 if (!AssumeVH)
4893 continue;
4894 CallInst *I = cast<CallInst>(AssumeVH);
4895
4896 assert(I->getFunction() == Q.CxtI->getParent()->getParent() &&
4897 "Got assumption for the wrong function!");
4898 assert(I->getIntrinsicID() == Intrinsic::assume &&
4899 "must be an assume intrinsic");
4900
4901 if (!isValidAssumeForContext(I, Q))
4902 continue;
4903
4904 computeKnownFPClassFromCond(V, I->getArgOperand(0),
4905 /*CondIsTrue=*/true, Q.CxtI, KnownFromContext);
4906 }
4907
4908 return KnownFromContext;
4909}
4910
4912 Value *Arm, bool Invert,
4913 const SimplifyQuery &SQ,
4914 unsigned Depth) {
4915
4916 KnownFPClass KnownSrc;
4918 /*CondIsTrue=*/!Invert, SQ.CxtI, KnownSrc,
4919 Depth + 1);
4920 KnownSrc = KnownSrc.unionWith(Known);
4921 if (KnownSrc.isUnknown())
4922 return;
4923
4924 if (isGuaranteedNotToBeUndef(Arm, SQ.AC, SQ.CxtI, SQ.DT, Depth + 1))
4925 Known = KnownSrc;
4926}
4927
4928void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4929 FPClassTest InterestedClasses, KnownFPClass &Known,
4930 const SimplifyQuery &Q, unsigned Depth);
4931
4932static void computeKnownFPClass(const Value *V, KnownFPClass &Known,
4933 FPClassTest InterestedClasses,
4934 const SimplifyQuery &Q, unsigned Depth) {
4935 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
4936 APInt DemandedElts =
4937 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
4938 computeKnownFPClass(V, DemandedElts, InterestedClasses, Known, Q, Depth);
4939}
4940
4942 const APInt &DemandedElts,
4943 FPClassTest InterestedClasses,
4944 KnownFPClass &Known,
4945 const SimplifyQuery &Q,
4946 unsigned Depth) {
4947 if ((InterestedClasses &
4949 return;
4950
4951 KnownFPClass KnownSrc;
4952 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
4953 KnownSrc, Q, Depth + 1);
4954 Known = KnownFPClass::fptrunc(KnownSrc);
4955}
4956
4958 switch (IID) {
4959 case Intrinsic::minimum:
4961 case Intrinsic::maximum:
4963 case Intrinsic::minimumnum:
4965 case Intrinsic::maximumnum:
4967 case Intrinsic::minnum:
4969 case Intrinsic::maxnum:
4971 default:
4972 llvm_unreachable("not a floating-point min-max intrinsic");
4973 }
4974}
4975
4976/// \return true if this is a floating point value that is known to have a
4977/// magnitude smaller than 1. i.e., fabs(X) <= 1.0 or is nan.
4978static bool isAbsoluteValueULEOne(const Value *V) {
4979 // TODO: Handle frexp
4980 // TODO: Other rounding intrinsics?
4981
4982 // fabs(x - floor(x)) <= 1
4983 const Value *SubFloorX;
4984 if (match(V, m_FSub(m_Value(SubFloorX),
4986 return true;
4987
4990}
4991
4992void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4993 FPClassTest InterestedClasses, KnownFPClass &Known,
4994 const SimplifyQuery &Q, unsigned Depth) {
4995 assert(Known.isUnknown() && "should not be called with known information");
4996
4997 if (!DemandedElts) {
4998 // No demanded elts, better to assume we don't know anything.
4999 Known.resetAll();
5000 return;
5001 }
5002
5003 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
5004
5005 if (auto *CFP = dyn_cast<ConstantFP>(V)) {
5006 Known = KnownFPClass(CFP->getValueAPF());
5007 return;
5008 }
5009
5011 Known.KnownFPClasses = fcPosZero;
5012 Known.SignBit = false;
5013 return;
5014 }
5015
5016 if (isa<PoisonValue>(V)) {
5017 Known.KnownFPClasses = fcNone;
5018 Known.SignBit = false;
5019 return;
5020 }
5021
5022 // Try to handle fixed width vector constants
5023 auto *VFVTy = dyn_cast<FixedVectorType>(V->getType());
5024 const Constant *CV = dyn_cast<Constant>(V);
5025 if (VFVTy && CV) {
5026 Known.KnownFPClasses = fcNone;
5027 bool SignBitAllZero = true;
5028 bool SignBitAllOne = true;
5029
5030 // For vectors, verify that each element is not NaN.
5031 unsigned NumElts = VFVTy->getNumElements();
5032 for (unsigned i = 0; i != NumElts; ++i) {
5033 if (!DemandedElts[i])
5034 continue;
5035
5036 Constant *Elt = CV->getAggregateElement(i);
5037 if (!Elt) {
5038 Known = KnownFPClass();
5039 return;
5040 }
5041 if (isa<PoisonValue>(Elt))
5042 continue;
5043 auto *CElt = dyn_cast<ConstantFP>(Elt);
5044 if (!CElt) {
5045 Known = KnownFPClass();
5046 return;
5047 }
5048
5049 const APFloat &C = CElt->getValueAPF();
5050 Known.KnownFPClasses |= C.classify();
5051 if (C.isNegative())
5052 SignBitAllZero = false;
5053 else
5054 SignBitAllOne = false;
5055 }
5056 if (SignBitAllOne != SignBitAllZero)
5057 Known.SignBit = SignBitAllOne;
5058 return;
5059 }
5060
5061 if (const auto *CDS = dyn_cast<ConstantDataSequential>(V)) {
5062 Known.KnownFPClasses = fcNone;
5063 for (size_t I = 0, E = CDS->getNumElements(); I != E; ++I)
5064 Known |= CDS->getElementAsAPFloat(I).classify();
5065 return;
5066 }
5067
5068 if (const auto *CA = dyn_cast<ConstantAggregate>(V)) {
5069 // TODO: Handle complex aggregates
5070 Known.KnownFPClasses = fcNone;
5071 for (const Use &Op : CA->operands()) {
5072 auto *CFP = dyn_cast<ConstantFP>(Op.get());
5073 if (!CFP) {
5074 Known = KnownFPClass();
5075 return;
5076 }
5077
5078 Known |= CFP->getValueAPF().classify();
5079 }
5080
5081 return;
5082 }
5083
5084 FPClassTest KnownNotFromFlags = fcNone;
5085 if (const auto *CB = dyn_cast<CallBase>(V))
5086 KnownNotFromFlags |= CB->getRetNoFPClass();
5087 else if (const auto *Arg = dyn_cast<Argument>(V))
5088 KnownNotFromFlags |= Arg->getNoFPClass();
5089
5090 const Operator *Op = dyn_cast<Operator>(V);
5092 if (FPOp->hasNoNaNs())
5093 KnownNotFromFlags |= fcNan;
5094 if (FPOp->hasNoInfs())
5095 KnownNotFromFlags |= fcInf;
5096 }
5097
5098 KnownFPClass AssumedClasses = computeKnownFPClassFromContext(V, Q);
5099 KnownNotFromFlags |= ~AssumedClasses.KnownFPClasses;
5100
5101 // We no longer need to find out about these bits from inputs if we can
5102 // assume this from flags/attributes.
5103 InterestedClasses &= ~KnownNotFromFlags;
5104
5105 llvm::scope_exit ClearClassesFromFlags([=, &Known] {
5106 Known.knownNot(KnownNotFromFlags);
5107 if (!Known.SignBit && AssumedClasses.SignBit) {
5108 if (*AssumedClasses.SignBit)
5109 Known.signBitMustBeOne();
5110 else
5111 Known.signBitMustBeZero();
5112 }
5113 });
5114
5115 if (!Op)
5116 return;
5117
5118 // All recursive calls that increase depth must come after this.
5120 return;
5121
5122 const unsigned Opc = Op->getOpcode();
5123 switch (Opc) {
5124 case Instruction::FNeg: {
5125 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5126 Known, Q, Depth + 1);
5127 Known.fneg();
5128 break;
5129 }
5130 case Instruction::Select: {
5131 auto ComputeForArm = [&](Value *Arm, bool Invert) {
5132 KnownFPClass Res;
5133 computeKnownFPClass(Arm, DemandedElts, InterestedClasses, Res, Q,
5134 Depth + 1);
5135 adjustKnownFPClassForSelectArm(Res, Op->getOperand(0), Arm, Invert, Q,
5136 Depth);
5137 return Res;
5138 };
5139 // Only known if known in both the LHS and RHS.
5140 Known =
5141 ComputeForArm(Op->getOperand(1), /*Invert=*/false)
5142 .intersectWith(ComputeForArm(Op->getOperand(2), /*Invert=*/true));
5143 break;
5144 }
5145 case Instruction::Load: {
5146 const MDNode *NoFPClass =
5147 cast<LoadInst>(Op)->getMetadata(LLVMContext::MD_nofpclass);
5148 if (!NoFPClass)
5149 break;
5150
5151 ConstantInt *MaskVal =
5153 Known.knownNot(static_cast<FPClassTest>(MaskVal->getZExtValue()));
5154 break;
5155 }
5156 case Instruction::Call: {
5157 const CallInst *II = cast<CallInst>(Op);
5158 const Intrinsic::ID IID = II->getIntrinsicID();
5159 switch (IID) {
5160 case Intrinsic::fabs: {
5161 if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) {
5162 // If we only care about the sign bit we don't need to inspect the
5163 // operand.
5164 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5165 InterestedClasses, Known, Q, Depth + 1);
5166 }
5167
5168 Known.fabs();
5169 break;
5170 }
5171 case Intrinsic::copysign: {
5172 KnownFPClass KnownSign;
5173
5174 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5175 Known, Q, Depth + 1);
5176 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5177 KnownSign, Q, Depth + 1);
5178 Known.copysign(KnownSign);
5179 break;
5180 }
5181 case Intrinsic::fma:
5182 case Intrinsic::fmuladd: {
5183 if ((InterestedClasses & fcNegative) == fcNone)
5184 break;
5185
5186 // FIXME: This should check isGuaranteedNotToBeUndef
5187 if (II->getArgOperand(0) == II->getArgOperand(1)) {
5188 KnownFPClass KnownSrc, KnownAddend;
5189 computeKnownFPClass(II->getArgOperand(2), DemandedElts,
5190 InterestedClasses, KnownAddend, Q, Depth + 1);
5191 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5192 InterestedClasses, KnownSrc, Q, Depth + 1);
5193
5194 const Function *F = II->getFunction();
5195 const fltSemantics &FltSem =
5196 II->getType()->getScalarType()->getFltSemantics();
5198 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5199
5200 if (KnownNotFromFlags & fcNan) {
5201 KnownSrc.knownNot(fcNan);
5202 KnownAddend.knownNot(fcNan);
5203 }
5204
5205 if (KnownNotFromFlags & fcInf) {
5206 KnownSrc.knownNot(fcInf);
5207 KnownAddend.knownNot(fcInf);
5208 }
5209
5210 Known = KnownFPClass::fma_square(KnownSrc, KnownAddend, Mode);
5211 break;
5212 }
5213
5214 KnownFPClass KnownSrc[3];
5215 for (int I = 0; I != 3; ++I) {
5216 computeKnownFPClass(II->getArgOperand(I), DemandedElts,
5217 InterestedClasses, KnownSrc[I], Q, Depth + 1);
5218 if (KnownSrc[I].isUnknown())
5219 return;
5220
5221 if (KnownNotFromFlags & fcNan)
5222 KnownSrc[I].knownNot(fcNan);
5223 if (KnownNotFromFlags & fcInf)
5224 KnownSrc[I].knownNot(fcInf);
5225 }
5226
5227 const Function *F = II->getFunction();
5228 const fltSemantics &FltSem =
5229 II->getType()->getScalarType()->getFltSemantics();
5231 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5232 Known = KnownFPClass::fma(KnownSrc[0], KnownSrc[1], KnownSrc[2], Mode);
5233 break;
5234 }
5235 case Intrinsic::sqrt:
5236 case Intrinsic::experimental_constrained_sqrt: {
5237 KnownFPClass KnownSrc;
5238 FPClassTest InterestedSrcs = InterestedClasses;
5239 if (InterestedClasses & fcNan)
5240 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5241
5242 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5243 KnownSrc, Q, Depth + 1);
5244
5246
5247 bool HasNSZ = Q.IIQ.hasNoSignedZeros(II);
5248 if (!HasNSZ) {
5249 const Function *F = II->getFunction();
5250 const fltSemantics &FltSem =
5251 II->getType()->getScalarType()->getFltSemantics();
5252 Mode = F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5253 }
5254
5255 Known = KnownFPClass::sqrt(KnownSrc, Mode);
5256 if (HasNSZ)
5257 Known.knownNot(fcNegZero);
5258
5259 break;
5260 }
5261 case Intrinsic::sin: {
5262 KnownFPClass KnownSrc;
5263 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5264 KnownSrc, Q, Depth + 1);
5265 Known = KnownFPClass::sin(KnownSrc);
5266 break;
5267 }
5268 case Intrinsic::cos: {
5269 KnownFPClass KnownSrc;
5270 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5271 KnownSrc, Q, Depth + 1);
5272 Known = KnownFPClass::cos(KnownSrc);
5273 break;
5274 }
5275 case Intrinsic::tan: {
5276 KnownFPClass KnownSrc;
5277 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5278 KnownSrc, Q, Depth + 1);
5279 Known = KnownFPClass::tan(KnownSrc);
5280 break;
5281 }
5282 case Intrinsic::sinh: {
5283 KnownFPClass KnownSrc;
5284 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5285 KnownSrc, Q, Depth + 1);
5286 Known = KnownFPClass::sinh(KnownSrc);
5287 break;
5288 }
5289 case Intrinsic::cosh: {
5290 KnownFPClass KnownSrc;
5291 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5292 KnownSrc, Q, Depth + 1);
5293 Known = KnownFPClass::cosh(KnownSrc);
5294 break;
5295 }
5296 case Intrinsic::tanh: {
5297 KnownFPClass KnownSrc;
5298 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5299 KnownSrc, Q, Depth + 1);
5300 Known = KnownFPClass::tanh(KnownSrc);
5301 break;
5302 }
5303 case Intrinsic::asin: {
5304 KnownFPClass KnownSrc;
5305 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5306 KnownSrc, Q, Depth + 1);
5307 Known = KnownFPClass::asin(KnownSrc);
5308 break;
5309 }
5310 case Intrinsic::acos: {
5311 KnownFPClass KnownSrc;
5312 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5313 KnownSrc, Q, Depth + 1);
5314 Known = KnownFPClass::acos(KnownSrc);
5315 break;
5316 }
5317 case Intrinsic::atan: {
5318 KnownFPClass KnownSrc;
5319 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5320 KnownSrc, Q, Depth + 1);
5321 Known = KnownFPClass::atan(KnownSrc);
5322 break;
5323 }
5324 case Intrinsic::atan2: {
5325 KnownFPClass KnownLHS, KnownRHS;
5326 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5327 KnownLHS, Q, Depth + 1);
5328 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5329 KnownRHS, Q, Depth + 1);
5330 Known = KnownFPClass::atan2(KnownLHS, KnownRHS);
5331 break;
5332 }
5333 case Intrinsic::maxnum:
5334 case Intrinsic::minnum:
5335 case Intrinsic::minimum:
5336 case Intrinsic::maximum:
5337 case Intrinsic::minimumnum:
5338 case Intrinsic::maximumnum: {
5339 KnownFPClass KnownLHS, KnownRHS;
5340 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5341 KnownLHS, Q, Depth + 1);
5342 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5343 KnownRHS, Q, Depth + 1);
5344
5345 const Function *F = II->getFunction();
5346
5348 F ? F->getDenormalMode(
5349 II->getType()->getScalarType()->getFltSemantics())
5351
5352 Known = KnownFPClass::minMaxLike(KnownLHS, KnownRHS, getMinMaxKind(IID),
5353 Mode);
5354 break;
5355 }
5356 case Intrinsic::canonicalize: {
5357 KnownFPClass KnownSrc;
5358 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5359 KnownSrc, Q, Depth + 1);
5360
5361 const Function *F = II->getFunction();
5362 DenormalMode DenormMode =
5363 F ? F->getDenormalMode(
5364 II->getType()->getScalarType()->getFltSemantics())
5366 Known = KnownFPClass::canonicalize(KnownSrc, DenormMode);
5367 break;
5368 }
5369 case Intrinsic::vector_reduce_fmax:
5370 case Intrinsic::vector_reduce_fmin:
5371 case Intrinsic::vector_reduce_fmaximum:
5372 case Intrinsic::vector_reduce_fminimum: {
5373 // reduce min/max will choose an element from one of the vector elements,
5374 // so we can infer and class information that is common to all elements.
5375 Known = computeKnownFPClass(II->getArgOperand(0), II->getFastMathFlags(),
5376 InterestedClasses, Q, Depth + 1);
5377 // Can only propagate sign if output is never NaN.
5378 if (!Known.isKnownNeverNaN())
5379 Known.SignBit.reset();
5380 break;
5381 }
5382 // reverse preserves all characteristics of the input vec's element.
5383 case Intrinsic::vector_reverse:
5384 Known = computeKnownFPClass(
5385 II->getArgOperand(0), DemandedElts.reverseBits(),
5386 II->getFastMathFlags(), InterestedClasses, Q, Depth + 1);
5387 break;
5388 case Intrinsic::trunc:
5389 case Intrinsic::floor:
5390 case Intrinsic::ceil:
5391 case Intrinsic::rint:
5392 case Intrinsic::nearbyint:
5393 case Intrinsic::round:
5394 case Intrinsic::roundeven: {
5395 KnownFPClass KnownSrc;
5396 FPClassTest InterestedSrcs = InterestedClasses;
5397 if (InterestedSrcs & fcPosFinite)
5398 InterestedSrcs |= fcPosFinite;
5399 if (InterestedSrcs & fcNegFinite)
5400 InterestedSrcs |= fcNegFinite;
5401 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5402 KnownSrc, Q, Depth + 1);
5403
5405 KnownSrc, IID == Intrinsic::trunc,
5406 V->getType()->getScalarType()->isMultiUnitFPType());
5407 break;
5408 }
5409 case Intrinsic::exp:
5410 case Intrinsic::exp2:
5411 case Intrinsic::exp10:
5412 case Intrinsic::amdgcn_exp2: {
5413 KnownFPClass KnownSrc;
5414 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5415 KnownSrc, Q, Depth + 1);
5416
5417 Known = KnownFPClass::exp(KnownSrc);
5418
5419 Type *EltTy = II->getType()->getScalarType();
5420 if (IID == Intrinsic::amdgcn_exp2 && EltTy->isFloatTy())
5421 Known.knownNot(fcSubnormal);
5422
5423 break;
5424 }
5425 case Intrinsic::fptrunc_round: {
5426 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,
5427 Q, Depth);
5428 break;
5429 }
5430 case Intrinsic::log:
5431 case Intrinsic::log10:
5432 case Intrinsic::log2:
5433 case Intrinsic::experimental_constrained_log:
5434 case Intrinsic::experimental_constrained_log10:
5435 case Intrinsic::experimental_constrained_log2:
5436 case Intrinsic::amdgcn_log: {
5437 Type *EltTy = II->getType()->getScalarType();
5438
5439 // log(+inf) -> +inf
5440 // log([+-]0.0) -> -inf
5441 // log(-inf) -> nan
5442 // log(-x) -> nan
5443 if ((InterestedClasses & (fcNan | fcInf)) != fcNone) {
5444 FPClassTest InterestedSrcs = InterestedClasses;
5445 if ((InterestedClasses & fcNegInf) != fcNone)
5446 InterestedSrcs |= fcZero | fcSubnormal;
5447 if ((InterestedClasses & fcNan) != fcNone)
5448 InterestedSrcs |= fcNan | fcNegative;
5449
5450 KnownFPClass KnownSrc;
5451 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5452 KnownSrc, Q, Depth + 1);
5453
5454 const Function *F = II->getFunction();
5455 DenormalMode Mode = F ? F->getDenormalMode(EltTy->getFltSemantics())
5457 Known = KnownFPClass::log(KnownSrc, Mode);
5458 }
5459
5460 break;
5461 }
5462 case Intrinsic::powi: {
5463 if ((InterestedClasses & (fcNan | fcInf | fcNegative)) == fcNone)
5464 break;
5465
5466 const Value *Exp = II->getArgOperand(1);
5467 Type *ExpTy = Exp->getType();
5468 unsigned BitWidth = ExpTy->getScalarType()->getIntegerBitWidth();
5469 KnownBits ExponentKnownBits(BitWidth);
5470 computeKnownBits(Exp, isa<VectorType>(ExpTy) ? DemandedElts : APInt(1, 1),
5471 ExponentKnownBits, Q, Depth + 1);
5472
5473 FPClassTest InterestedSrcs = fcNone;
5474 if (InterestedClasses & fcNan)
5475 InterestedSrcs |= fcNan;
5476 if (!ExponentKnownBits.isZero()) {
5477 if (InterestedClasses & fcInf)
5478 InterestedSrcs |= fcFinite | fcInf;
5479 if ((InterestedClasses & fcNegative) && !ExponentKnownBits.isEven())
5480 InterestedSrcs |= fcNegative;
5481 }
5482
5483 KnownFPClass KnownSrc;
5484 if (InterestedSrcs != fcNone)
5485 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5486 KnownSrc, Q, Depth + 1);
5487
5488 Known = KnownFPClass::powi(KnownSrc, ExponentKnownBits);
5489 break;
5490 }
5491 case Intrinsic::ldexp: {
5492 KnownFPClass KnownSrc;
5493 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5494 KnownSrc, Q, Depth + 1);
5495 // Can refine inf/zero handling based on the exponent operand.
5496 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;
5497
5498 KnownBits ExpBits;
5499 if ((KnownSrc.KnownFPClasses & ExpInfoMask) != fcNone) {
5500 const Value *ExpArg = II->getArgOperand(1);
5501 ExpBits = computeKnownBits(ExpArg, DemandedElts, Q, Depth + 1);
5502 }
5503
5504 const fltSemantics &Flt =
5505 II->getType()->getScalarType()->getFltSemantics();
5506
5507 const Function *F = II->getFunction();
5509 F ? F->getDenormalMode(Flt) : DenormalMode::getDynamic();
5510
5511 Known = KnownFPClass::ldexp(KnownSrc, ExpBits, Flt, Mode);
5512 break;
5513 }
5514 case Intrinsic::arithmetic_fence: {
5515 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5516 Known, Q, Depth + 1);
5517 break;
5518 }
5519 case Intrinsic::experimental_constrained_sitofp:
5520 case Intrinsic::experimental_constrained_uitofp:
5521 // Cannot produce nan
5522 Known.knownNot(fcNan);
5523
5524 // sitofp and uitofp turn into +0.0 for zero.
5525 Known.knownNot(fcNegZero);
5526
5527 // Integers cannot be subnormal
5528 Known.knownNot(fcSubnormal);
5529
5530 if (IID == Intrinsic::experimental_constrained_uitofp)
5531 Known.signBitMustBeZero();
5532
5533 // TODO: Copy inf handling from instructions
5534 break;
5535
5536 case Intrinsic::amdgcn_fract: {
5537 Known.knownNot(fcInf);
5538
5539 if (InterestedClasses & fcNan) {
5540 KnownFPClass KnownSrc;
5541 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5542 InterestedClasses, KnownSrc, Q, Depth + 1);
5543
5544 if (KnownSrc.isKnownNeverInfOrNaN())
5545 Known.knownNot(fcNan);
5546 else if (KnownSrc.isKnownNever(fcSNan))
5547 Known.knownNot(fcSNan);
5548 }
5549
5550 break;
5551 }
5552 case Intrinsic::amdgcn_rcp: {
5553 KnownFPClass KnownSrc;
5554 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5555 KnownSrc, Q, Depth + 1);
5556
5557 Known.propagateNaN(KnownSrc);
5558
5559 Type *EltTy = II->getType()->getScalarType();
5560
5561 // f32 denormal always flushed.
5562 if (EltTy->isFloatTy()) {
5563 Known.knownNot(fcSubnormal);
5564 KnownSrc.knownNot(fcSubnormal);
5565 }
5566
5567 if (KnownSrc.isKnownNever(fcNegative))
5568 Known.knownNot(fcNegative);
5569 if (KnownSrc.isKnownNever(fcPositive))
5570 Known.knownNot(fcPositive);
5571
5572 if (const Function *F = II->getFunction()) {
5573 DenormalMode Mode = F->getDenormalMode(EltTy->getFltSemantics());
5574 if (KnownSrc.isKnownNeverLogicalPosZero(Mode))
5575 Known.knownNot(fcPosInf);
5576 if (KnownSrc.isKnownNeverLogicalNegZero(Mode))
5577 Known.knownNot(fcNegInf);
5578 }
5579
5580 break;
5581 }
5582 case Intrinsic::amdgcn_rsq: {
5583 KnownFPClass KnownSrc;
5584 // The only negative value that can be returned is -inf for -0 inputs.
5586
5587 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5588 KnownSrc, Q, Depth + 1);
5589
5590 // Negative -> nan
5591 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
5592 Known.knownNot(fcNan);
5593 else if (KnownSrc.isKnownNever(fcSNan))
5594 Known.knownNot(fcSNan);
5595
5596 // +inf -> +0
5597 if (KnownSrc.isKnownNeverPosInfinity())
5598 Known.knownNot(fcPosZero);
5599
5600 Type *EltTy = II->getType()->getScalarType();
5601
5602 // f32 denormal always flushed.
5603 if (EltTy->isFloatTy())
5604 Known.knownNot(fcPosSubnormal);
5605
5606 if (const Function *F = II->getFunction()) {
5607 DenormalMode Mode = F->getDenormalMode(EltTy->getFltSemantics());
5608
5609 // -0 -> -inf
5610 if (KnownSrc.isKnownNeverLogicalNegZero(Mode))
5611 Known.knownNot(fcNegInf);
5612
5613 // +0 -> +inf
5614 if (KnownSrc.isKnownNeverLogicalPosZero(Mode))
5615 Known.knownNot(fcPosInf);
5616 }
5617
5618 break;
5619 }
5620 case Intrinsic::amdgcn_trig_preop: {
5621 // Always returns a value [0, 1)
5622 Known.knownNot(fcNan | fcInf | fcNegative);
5623 break;
5624 }
5625 default:
5626 break;
5627 }
5628
5629 break;
5630 }
5631 case Instruction::FAdd:
5632 case Instruction::FSub: {
5633 KnownFPClass KnownLHS, KnownRHS;
5634 bool WantNegative =
5635 Op->getOpcode() == Instruction::FAdd &&
5636 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;
5637 bool WantNaN = (InterestedClasses & fcNan) != fcNone;
5638 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;
5639
5640 if (!WantNaN && !WantNegative && !WantNegZero)
5641 break;
5642
5643 FPClassTest InterestedSrcs = InterestedClasses;
5644 if (WantNegative)
5645 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5646 if (InterestedClasses & fcNan)
5647 InterestedSrcs |= fcInf;
5648 computeKnownFPClass(Op->getOperand(1), DemandedElts, InterestedSrcs,
5649 KnownRHS, Q, Depth + 1);
5650
5651 // Special case fadd x, x, which is the canonical form of fmul x, 2.
5652 bool Self = Op->getOperand(0) == Op->getOperand(1) &&
5653 isGuaranteedNotToBeUndef(Op->getOperand(0), Q.AC, Q.CxtI, Q.DT,
5654 Depth + 1);
5655 if (Self)
5656 KnownLHS = KnownRHS;
5657
5658 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||
5659 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) ||
5660 WantNegZero || Opc == Instruction::FSub) {
5661
5662 // FIXME: Context function should always be passed in separately
5663 const Function *F = cast<Instruction>(Op)->getFunction();
5664 const fltSemantics &FltSem =
5665 Op->getType()->getScalarType()->getFltSemantics();
5667 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5668
5669 if (Self && Opc == Instruction::FAdd) {
5670 Known = KnownFPClass::fadd_self(KnownLHS, Mode);
5671 } else {
5672 // RHS is canonically cheaper to compute. Skip inspecting the LHS if
5673 // there's no point.
5674
5675 if (!Self) {
5676 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedSrcs,
5677 KnownLHS, Q, Depth + 1);
5678 }
5679
5680 Known = Opc == Instruction::FAdd
5681 ? KnownFPClass::fadd(KnownLHS, KnownRHS, Mode)
5682 : KnownFPClass::fsub(KnownLHS, KnownRHS, Mode);
5683 }
5684 }
5685
5686 break;
5687 }
5688 case Instruction::FMul: {
5689 const Function *F = cast<Instruction>(Op)->getFunction();
5691 F ? F->getDenormalMode(
5692 Op->getType()->getScalarType()->getFltSemantics())
5694
5695 Value *LHS = Op->getOperand(0);
5696 Value *RHS = Op->getOperand(1);
5697 // X * X is always non-negative or a NaN.
5698 // FIXME: Should check isGuaranteedNotToBeUndef
5699 if (LHS == RHS) {
5700 KnownFPClass KnownSrc;
5701 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownSrc, Q,
5702 Depth + 1);
5703 Known = KnownFPClass::square(KnownSrc, Mode);
5704 break;
5705 }
5706
5707 KnownFPClass KnownLHS, KnownRHS;
5708
5709 const APFloat *CRHS;
5710 if (match(RHS, m_APFloat(CRHS))) {
5711 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Q,
5712 Depth + 1);
5713 Known = KnownFPClass::fmul(KnownLHS, *CRHS, Mode);
5714 } else {
5715 computeKnownFPClass(RHS, DemandedElts, fcAllFlags, KnownRHS, Q,
5716 Depth + 1);
5717 // TODO: Improve accuracy in unfused FMA pattern. We can prove an
5718 // additional not-nan if the addend is known-not negative infinity if the
5719 // multiply is known-not infinity.
5720
5721 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Q,
5722 Depth + 1);
5723 Known = KnownFPClass::fmul(KnownLHS, KnownRHS, Mode);
5724 }
5725
5726 /// Propgate no-infs if the other source is known smaller than one, such
5727 /// that this cannot introduce overflow.
5728 if (KnownLHS.isKnownNever(fcInf) && isAbsoluteValueULEOne(RHS))
5729 Known.knownNot(fcInf);
5730 else if (KnownRHS.isKnownNever(fcInf) && isAbsoluteValueULEOne(LHS))
5731 Known.knownNot(fcInf);
5732
5733 break;
5734 }
5735 case Instruction::FDiv:
5736 case Instruction::FRem: {
5737 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
5738
5739 if (Op->getOpcode() == Instruction::FRem)
5740 Known.knownNot(fcInf);
5741
5742 if (Op->getOperand(0) == Op->getOperand(1) &&
5743 isGuaranteedNotToBeUndef(Op->getOperand(0), Q.AC, Q.CxtI, Q.DT)) {
5744 if (Op->getOpcode() == Instruction::FDiv) {
5745 // X / X is always exactly 1.0 or a NaN.
5747 } else {
5748 // X % X is always exactly [+-]0.0 or a NaN.
5749 Known.KnownFPClasses = fcNan | fcZero;
5750 }
5751
5752 if (!WantNan)
5753 break;
5754
5755 KnownFPClass KnownSrc;
5756 computeKnownFPClass(Op->getOperand(0), DemandedElts,
5757 fcNan | fcInf | fcZero | fcSubnormal, KnownSrc, Q,
5758 Depth + 1);
5759 const Function *F = cast<Instruction>(Op)->getFunction();
5760 const fltSemantics &FltSem =
5761 Op->getType()->getScalarType()->getFltSemantics();
5762
5764 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5765
5766 Known = Op->getOpcode() == Instruction::FDiv
5767 ? KnownFPClass::fdiv_self(KnownSrc, Mode)
5768 : KnownFPClass::frem_self(KnownSrc, Mode);
5769 break;
5770 }
5771
5772 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
5773 const bool WantPositive =
5774 Opc == Instruction::FRem && (InterestedClasses & fcPositive) != fcNone;
5775 if (!WantNan && !WantNegative && !WantPositive)
5776 break;
5777
5778 KnownFPClass KnownLHS, KnownRHS;
5779
5780 computeKnownFPClass(Op->getOperand(1), DemandedElts,
5781 fcNan | fcInf | fcZero | fcNegative, KnownRHS, Q,
5782 Depth + 1);
5783
5784 bool KnowSomethingUseful = KnownRHS.isKnownNeverNaN() ||
5785 KnownRHS.isKnownNever(fcNegative) ||
5786 KnownRHS.isKnownNever(fcPositive);
5787
5788 if (KnowSomethingUseful || WantPositive) {
5789 computeKnownFPClass(Op->getOperand(0), DemandedElts, fcAllFlags, KnownLHS,
5790 Q, Depth + 1);
5791 }
5792
5793 const Function *F = cast<Instruction>(Op)->getFunction();
5794 const fltSemantics &FltSem =
5795 Op->getType()->getScalarType()->getFltSemantics();
5796
5797 if (Op->getOpcode() == Instruction::FDiv) {
5799 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5800 Known = KnownFPClass::fdiv(KnownLHS, KnownRHS, Mode);
5801 } else {
5802 // Inf REM x and x REM 0 produce NaN.
5803 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5804 KnownLHS.isKnownNeverInfinity() && F &&
5805 KnownRHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))) {
5806 Known.knownNot(fcNan);
5807 }
5808
5809 // The sign for frem is the same as the first operand.
5810 if (KnownLHS.cannotBeOrderedLessThanZero())
5812 if (KnownLHS.cannotBeOrderedGreaterThanZero())
5814
5815 // See if we can be more aggressive about the sign of 0.
5816 if (KnownLHS.isKnownNever(fcNegative))
5817 Known.knownNot(fcNegative);
5818 if (KnownLHS.isKnownNever(fcPositive))
5819 Known.knownNot(fcPositive);
5820 }
5821
5822 break;
5823 }
5824 case Instruction::FPExt: {
5825 KnownFPClass KnownSrc;
5826 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5827 KnownSrc, Q, Depth + 1);
5828
5829 const fltSemantics &DstTy =
5830 Op->getType()->getScalarType()->getFltSemantics();
5831 const fltSemantics &SrcTy =
5832 Op->getOperand(0)->getType()->getScalarType()->getFltSemantics();
5833
5834 Known = KnownFPClass::fpext(KnownSrc, DstTy, SrcTy);
5835 break;
5836 }
5837 case Instruction::FPTrunc: {
5838 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known, Q,
5839 Depth);
5840 break;
5841 }
5842 case Instruction::SIToFP:
5843 case Instruction::UIToFP: {
5844 // Cannot produce nan
5845 Known.knownNot(fcNan);
5846
5847 // Integers cannot be subnormal
5848 Known.knownNot(fcSubnormal);
5849
5850 // sitofp and uitofp turn into +0.0 for zero.
5851 Known.knownNot(fcNegZero);
5852
5853 // UIToFP is always non-negative regardless of known bits.
5854 if (Op->getOpcode() == Instruction::UIToFP)
5855 Known.signBitMustBeZero();
5856
5857 // Only compute known bits if we can learn something useful from them.
5858 if (!(InterestedClasses & (fcPosZero | fcNormal | fcInf)))
5859 break;
5860
5861 KnownBits IntKnown =
5862 computeKnownBits(Op->getOperand(0), DemandedElts, Q, Depth + 1);
5863
5864 // If the integer is non-zero, the result cannot be +0.0
5865 if (IntKnown.isNonZero())
5866 Known.knownNot(fcPosZero);
5867
5868 if (Op->getOpcode() == Instruction::SIToFP) {
5869 // If the signed integer is known non-negative, the result is
5870 // non-negative. If the signed integer is known negative, the result is
5871 // negative.
5872 if (IntKnown.isNonNegative()) {
5873 Known.signBitMustBeZero();
5874 } else if (IntKnown.isNegative()) {
5875 Known.signBitMustBeOne();
5876 }
5877 }
5878
5879 // Guard kept for ilogb()
5880 if (InterestedClasses & fcInf) {
5881 // Get width of largest magnitude integer known.
5882 // This still works for a signed minimum value because the largest FP
5883 // value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx).
5884 int IntSize = IntKnown.getBitWidth();
5885 if (Op->getOpcode() == Instruction::UIToFP)
5886 IntSize -= IntKnown.countMinLeadingZeros();
5887 else if (Op->getOpcode() == Instruction::SIToFP)
5888 IntSize -= IntKnown.countMinSignBits();
5889
5890 // If the exponent of the largest finite FP value can hold the largest
5891 // integer, the result of the cast must be finite.
5892 Type *FPTy = Op->getType()->getScalarType();
5893 if (ilogb(APFloat::getLargest(FPTy->getFltSemantics())) >= IntSize)
5894 Known.knownNot(fcInf);
5895 }
5896
5897 break;
5898 }
5899 case Instruction::ExtractElement: {
5900 // Look through extract element. If the index is non-constant or
5901 // out-of-range demand all elements, otherwise just the extracted element.
5902 const Value *Vec = Op->getOperand(0);
5903
5904 APInt DemandedVecElts;
5905 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
5906 unsigned NumElts = VecTy->getNumElements();
5907 DemandedVecElts = APInt::getAllOnes(NumElts);
5908 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(1));
5909 if (CIdx && CIdx->getValue().ult(NumElts))
5910 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
5911 } else {
5912 DemandedVecElts = APInt(1, 1);
5913 }
5914
5915 return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,
5916 Q, Depth + 1);
5917 }
5918 case Instruction::InsertElement: {
5919 if (isa<ScalableVectorType>(Op->getType()))
5920 return;
5921
5922 const Value *Vec = Op->getOperand(0);
5923 const Value *Elt = Op->getOperand(1);
5924 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(2));
5925 unsigned NumElts = DemandedElts.getBitWidth();
5926 APInt DemandedVecElts = DemandedElts;
5927 bool NeedsElt = true;
5928 // If we know the index we are inserting to, clear it from Vec check.
5929 if (CIdx && CIdx->getValue().ult(NumElts)) {
5930 DemandedVecElts.clearBit(CIdx->getZExtValue());
5931 NeedsElt = DemandedElts[CIdx->getZExtValue()];
5932 }
5933
5934 // Do we demand the inserted element?
5935 if (NeedsElt) {
5936 computeKnownFPClass(Elt, Known, InterestedClasses, Q, Depth + 1);
5937 // If we don't know any bits, early out.
5938 if (Known.isUnknown())
5939 break;
5940 } else {
5941 Known.KnownFPClasses = fcNone;
5942 }
5943
5944 // Do we need anymore elements from Vec?
5945 if (!DemandedVecElts.isZero()) {
5946 KnownFPClass Known2;
5947 computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2, Q,
5948 Depth + 1);
5949 Known |= Known2;
5950 }
5951
5952 break;
5953 }
5954 case Instruction::ShuffleVector: {
5955 // Handle vector splat idiom
5956 if (Value *Splat = getSplatValue(V)) {
5957 computeKnownFPClass(Splat, Known, InterestedClasses, Q, Depth + 1);
5958 break;
5959 }
5960
5961 // For undef elements, we don't know anything about the common state of
5962 // the shuffle result.
5963 APInt DemandedLHS, DemandedRHS;
5964 auto *Shuf = dyn_cast<ShuffleVectorInst>(Op);
5965 if (!Shuf || !getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
5966 return;
5967
5968 if (!!DemandedLHS) {
5969 const Value *LHS = Shuf->getOperand(0);
5970 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known, Q,
5971 Depth + 1);
5972
5973 // If we don't know any bits, early out.
5974 if (Known.isUnknown())
5975 break;
5976 } else {
5977 Known.KnownFPClasses = fcNone;
5978 }
5979
5980 if (!!DemandedRHS) {
5981 KnownFPClass Known2;
5982 const Value *RHS = Shuf->getOperand(1);
5983 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2, Q,
5984 Depth + 1);
5985 Known |= Known2;
5986 }
5987
5988 break;
5989 }
5990 case Instruction::ExtractValue: {
5991 const ExtractValueInst *Extract = cast<ExtractValueInst>(Op);
5992 ArrayRef<unsigned> Indices = Extract->getIndices();
5993 const Value *Src = Extract->getAggregateOperand();
5994 if (isa<StructType>(Src->getType()) && Indices.size() == 1 &&
5995 Indices[0] == 0) {
5996 if (const auto *II = dyn_cast<IntrinsicInst>(Src)) {
5997 switch (II->getIntrinsicID()) {
5998 case Intrinsic::frexp: {
5999 Known.knownNot(fcSubnormal);
6000
6001 KnownFPClass KnownSrc;
6002 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
6003 InterestedClasses, KnownSrc, Q, Depth + 1);
6004
6005 const Function *F = cast<Instruction>(Op)->getFunction();
6006 const fltSemantics &FltSem =
6007 Op->getType()->getScalarType()->getFltSemantics();
6008
6010 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
6011 Known = KnownFPClass::frexp_mant(KnownSrc, Mode);
6012 return;
6013 }
6014 default:
6015 break;
6016 }
6017 }
6018 }
6019
6020 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Q,
6021 Depth + 1);
6022 break;
6023 }
6024 case Instruction::PHI: {
6025 const PHINode *P = cast<PHINode>(Op);
6026 // Unreachable blocks may have zero-operand PHI nodes.
6027 if (P->getNumIncomingValues() == 0)
6028 break;
6029
6030 // Otherwise take the unions of the known bit sets of the operands,
6031 // taking conservative care to avoid excessive recursion.
6032 const unsigned PhiRecursionLimit = MaxAnalysisRecursionDepth - 2;
6033
6034 if (Depth < PhiRecursionLimit) {
6035 // Skip if every incoming value references to ourself.
6036 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
6037 break;
6038
6039 bool First = true;
6040
6041 for (const Use &U : P->operands()) {
6042 Value *IncValue;
6043 Instruction *CxtI;
6044 breakSelfRecursivePHI(&U, P, IncValue, CxtI);
6045 // Skip direct self references.
6046 if (IncValue == P)
6047 continue;
6048
6049 KnownFPClass KnownSrc;
6050 // Recurse, but cap the recursion to two levels, because we don't want
6051 // to waste time spinning around in loops. We need at least depth 2 to
6052 // detect known sign bits.
6053 computeKnownFPClass(IncValue, DemandedElts, InterestedClasses, KnownSrc,
6055 PhiRecursionLimit);
6056
6057 if (First) {
6058 Known = KnownSrc;
6059 First = false;
6060 } else {
6061 Known |= KnownSrc;
6062 }
6063
6064 if (Known.KnownFPClasses == fcAllFlags)
6065 break;
6066 }
6067 }
6068
6069 // Look for the case of a for loop which has a positive
6070 // initial value and is incremented by a squared value.
6071 // This will propagate sign information out of such loops.
6072 if (P->getNumIncomingValues() != 2 || Known.cannotBeOrderedLessThanZero())
6073 break;
6074 for (unsigned I = 0; I < 2; I++) {
6075 Value *RecurValue = P->getIncomingValue(1 - I);
6077 if (!II)
6078 continue;
6079 Value *R, *L, *Init;
6080 PHINode *PN;
6082 PN == P) {
6083 switch (II->getIntrinsicID()) {
6084 case Intrinsic::fma:
6085 case Intrinsic::fmuladd: {
6086 KnownFPClass KnownStart;
6087 computeKnownFPClass(Init, DemandedElts, InterestedClasses, KnownStart,
6088 Q, Depth + 1);
6089 if (KnownStart.cannotBeOrderedLessThanZero() && L == R &&
6090 isGuaranteedNotToBeUndef(L, Q.AC, Q.CxtI, Q.DT, Depth + 1))
6092 break;
6093 }
6094 }
6095 }
6096 }
6097 break;
6098 }
6099 case Instruction::BitCast: {
6100 const Value *Src;
6101 if (!match(Op, m_ElementWiseBitCast(m_Value(Src))) ||
6102 !Src->getType()->isIntOrIntVectorTy())
6103 break;
6104
6105 const Type *Ty = Op->getType();
6106
6107 Value *CastLHS, *CastRHS;
6108
6109 // Match bitcast(umax(bitcast(a), bitcast(b)))
6110 if (match(Src, m_c_MaxOrMin(m_BitCast(m_Value(CastLHS)),
6111 m_BitCast(m_Value(CastRHS)))) &&
6112 CastLHS->getType() == Ty && CastRHS->getType() == Ty) {
6113 KnownFPClass KnownLHS, KnownRHS;
6114 computeKnownFPClass(CastRHS, DemandedElts, InterestedClasses, KnownRHS, Q,
6115 Depth + 1);
6116 if (!KnownRHS.isUnknown()) {
6117 computeKnownFPClass(CastLHS, DemandedElts, InterestedClasses, KnownLHS,
6118 Q, Depth + 1);
6119 Known = KnownLHS | KnownRHS;
6120 }
6121
6122 return;
6123 }
6124
6125 const Type *EltTy = Ty->getScalarType();
6126 KnownBits Bits(EltTy->getPrimitiveSizeInBits());
6127 computeKnownBits(Src, DemandedElts, Bits, Q, Depth + 1);
6128
6129 Known = KnownFPClass::bitcast(EltTy->getFltSemantics(), Bits);
6130 break;
6131 }
6132 default:
6133 break;
6134 }
6135}
6136
6138 const APInt &DemandedElts,
6139 FPClassTest InterestedClasses,
6140 const SimplifyQuery &SQ,
6141 unsigned Depth) {
6142 KnownFPClass KnownClasses;
6143 ::computeKnownFPClass(V, DemandedElts, InterestedClasses, KnownClasses, SQ,
6144 Depth);
6145 return KnownClasses;
6146}
6147
6149 FPClassTest InterestedClasses,
6150 const SimplifyQuery &SQ,
6151 unsigned Depth) {
6152 KnownFPClass Known;
6153 ::computeKnownFPClass(V, Known, InterestedClasses, SQ, Depth);
6154 return Known;
6155}
6156
6158 const Value *V, const DataLayout &DL, FPClassTest InterestedClasses,
6159 const TargetLibraryInfo *TLI, AssumptionCache *AC, const Instruction *CxtI,
6160 const DominatorTree *DT, bool UseInstrInfo, unsigned Depth) {
6161 return computeKnownFPClass(V, InterestedClasses,
6162 SimplifyQuery(DL, TLI, DT, AC, CxtI, UseInstrInfo),
6163 Depth);
6164}
6165
6167llvm::computeKnownFPClass(const Value *V, const APInt &DemandedElts,
6168 FastMathFlags FMF, FPClassTest InterestedClasses,
6169 const SimplifyQuery &SQ, unsigned Depth) {
6170 if (FMF.noNaNs())
6171 InterestedClasses &= ~fcNan;
6172 if (FMF.noInfs())
6173 InterestedClasses &= ~fcInf;
6174
6175 KnownFPClass Result =
6176 computeKnownFPClass(V, DemandedElts, InterestedClasses, SQ, Depth);
6177
6178 if (FMF.noNaNs())
6179 Result.KnownFPClasses &= ~fcNan;
6180 if (FMF.noInfs())
6181 Result.KnownFPClasses &= ~fcInf;
6182 return Result;
6183}
6184
6186 FPClassTest InterestedClasses,
6187 const SimplifyQuery &SQ,
6188 unsigned Depth) {
6189 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
6190 APInt DemandedElts =
6191 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
6192 return computeKnownFPClass(V, DemandedElts, FMF, InterestedClasses, SQ,
6193 Depth);
6194}
6195
6197 unsigned Depth) {
6199 return Known.isKnownNeverNegZero();
6200}
6201
6208
6210 unsigned Depth) {
6212 return Known.isKnownNeverInfinity();
6213}
6214
6215/// Return true if the floating-point value can never contain a NaN or infinity.
6217 unsigned Depth) {
6219 return Known.isKnownNeverNaN() && Known.isKnownNeverInfinity();
6220}
6221
6222/// Return true if the floating-point scalar value is not a NaN or if the
6223/// floating-point vector value has no NaN elements. Return false if a value
6224/// could ever be NaN.
6226 unsigned Depth) {
6228 return Known.isKnownNeverNaN();
6229}
6230
6231/// Return false if we can prove that the specified FP value's sign bit is 0.
6232/// Return true if we can prove that the specified FP value's sign bit is 1.
6233/// Otherwise return std::nullopt.
6234std::optional<bool> llvm::computeKnownFPSignBit(const Value *V,
6235 const SimplifyQuery &SQ,
6236 unsigned Depth) {
6238 return Known.SignBit;
6239}
6240
6242 auto *User = cast<Instruction>(U.getUser());
6243 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6244 if (FPOp->hasNoSignedZeros())
6245 return true;
6246 }
6247
6248 switch (User->getOpcode()) {
6249 case Instruction::FPToSI:
6250 case Instruction::FPToUI:
6251 return true;
6252 case Instruction::FCmp:
6253 // fcmp treats both positive and negative zero as equal.
6254 return true;
6255 case Instruction::Call:
6256 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6257 switch (II->getIntrinsicID()) {
6258 case Intrinsic::fabs:
6259 return true;
6260 case Intrinsic::copysign:
6261 return U.getOperandNo() == 0;
6262 case Intrinsic::is_fpclass:
6263 case Intrinsic::vp_is_fpclass: {
6264 auto Test =
6265 static_cast<FPClassTest>(
6266 cast<ConstantInt>(II->getArgOperand(1))->getZExtValue()) &
6269 }
6270 default:
6271 return false;
6272 }
6273 }
6274 return false;
6275 default:
6276 return false;
6277 }
6278}
6279
6281 auto *User = cast<Instruction>(U.getUser());
6282 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6283 if (FPOp->hasNoNaNs())
6284 return true;
6285 }
6286
6287 switch (User->getOpcode()) {
6288 case Instruction::FPToSI:
6289 case Instruction::FPToUI:
6290 return true;
6291 // Proper FP math operations ignore the sign bit of NaN.
6292 case Instruction::FAdd:
6293 case Instruction::FSub:
6294 case Instruction::FMul:
6295 case Instruction::FDiv:
6296 case Instruction::FRem:
6297 case Instruction::FPTrunc:
6298 case Instruction::FPExt:
6299 case Instruction::FCmp:
6300 return true;
6301 // Bitwise FP operations should preserve the sign bit of NaN.
6302 case Instruction::FNeg:
6303 case Instruction::Select:
6304 case Instruction::PHI:
6305 return false;
6306 case Instruction::Ret:
6307 return User->getFunction()->getAttributes().getRetNoFPClass() &
6309 case Instruction::Call:
6310 case Instruction::Invoke: {
6311 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6312 switch (II->getIntrinsicID()) {
6313 case Intrinsic::fabs:
6314 return true;
6315 case Intrinsic::copysign:
6316 return U.getOperandNo() == 0;
6317 // Other proper FP math intrinsics ignore the sign bit of NaN.
6318 case Intrinsic::maxnum:
6319 case Intrinsic::minnum:
6320 case Intrinsic::maximum:
6321 case Intrinsic::minimum:
6322 case Intrinsic::maximumnum:
6323 case Intrinsic::minimumnum:
6324 case Intrinsic::canonicalize:
6325 case Intrinsic::fma:
6326 case Intrinsic::fmuladd:
6327 case Intrinsic::sqrt:
6328 case Intrinsic::pow:
6329 case Intrinsic::powi:
6330 case Intrinsic::fptoui_sat:
6331 case Intrinsic::fptosi_sat:
6332 case Intrinsic::is_fpclass:
6333 case Intrinsic::vp_is_fpclass:
6334 return true;
6335 default:
6336 return false;
6337 }
6338 }
6339
6340 FPClassTest NoFPClass =
6341 cast<CallBase>(User)->getParamNoFPClass(U.getOperandNo());
6342 return NoFPClass & FPClassTest::fcNan;
6343 }
6344 default:
6345 return false;
6346 }
6347}
6348
6350 FastMathFlags FMF) {
6351 if (isa<PoisonValue>(V))
6352 return true;
6353 if (isa<UndefValue>(V))
6354 return false;
6355
6356 if (match(V, m_CheckedFp([](const APFloat &Val) { return Val.isInteger(); })))
6357 return true;
6358
6360 if (!I)
6361 return false;
6362
6363 switch (I->getOpcode()) {
6364 case Instruction::SIToFP:
6365 case Instruction::UIToFP:
6366 // TODO: Could check nofpclass(inf) on incoming argument
6367 if (FMF.noInfs())
6368 return true;
6369
6370 // Need to check int size cannot produce infinity, which computeKnownFPClass
6371 // knows how to do already.
6372 return isKnownNeverInfinity(I, SQ);
6373 case Instruction::Call: {
6374 const CallInst *CI = cast<CallInst>(I);
6375 switch (CI->getIntrinsicID()) {
6376 case Intrinsic::trunc:
6377 case Intrinsic::floor:
6378 case Intrinsic::ceil:
6379 case Intrinsic::rint:
6380 case Intrinsic::nearbyint:
6381 case Intrinsic::round:
6382 case Intrinsic::roundeven:
6383 return (FMF.noInfs() && FMF.noNaNs()) || isKnownNeverInfOrNaN(I, SQ);
6384 default:
6385 break;
6386 }
6387
6388 break;
6389 }
6390 default:
6391 break;
6392 }
6393
6394 return false;
6395}
6396
6398
6399 // All byte-wide stores are splatable, even of arbitrary variables.
6400 if (V->getType()->isIntegerTy(8))
6401 return V;
6402
6403 LLVMContext &Ctx = V->getContext();
6404
6405 // Undef don't care.
6406 auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
6407 if (isa<UndefValue>(V))
6408 return UndefInt8;
6409
6410 // Return poison for zero-sized type.
6411 if (DL.getTypeStoreSize(V->getType()).isZero())
6412 return PoisonValue::get(Type::getInt8Ty(Ctx));
6413
6415 if (!C) {
6416 // Conceptually, we could handle things like:
6417 // %a = zext i8 %X to i16
6418 // %b = shl i16 %a, 8
6419 // %c = or i16 %a, %b
6420 // but until there is an example that actually needs this, it doesn't seem
6421 // worth worrying about.
6422 return nullptr;
6423 }
6424
6425 // Handle 'null' ConstantArrayZero etc.
6426 if (C->isNullValue())
6428
6429 // Constant floating-point values can be handled as integer values if the
6430 // corresponding integer value is "byteable". An important case is 0.0.
6431 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
6432 Type *ScalarTy = CFP->getType()->getScalarType();
6433 if (ScalarTy->isHalfTy() || ScalarTy->isFloatTy() || ScalarTy->isDoubleTy())
6434 return isBytewiseValue(
6435 ConstantInt::get(Ctx, CFP->getValue().bitcastToAPInt()), DL);
6436
6437 // Don't handle long double formats, which have strange constraints.
6438 return nullptr;
6439 }
6440
6441 // We can handle constant integers that are multiple of 8 bits.
6442 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
6443 if (CI->getBitWidth() % 8 == 0) {
6444 if (!CI->getValue().isSplat(8))
6445 return nullptr;
6446 return ConstantInt::get(Ctx, CI->getValue().trunc(8));
6447 }
6448 }
6449
6450 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
6451 if (CE->getOpcode() == Instruction::IntToPtr) {
6452 if (auto *PtrTy = dyn_cast<PointerType>(CE->getType())) {
6453 unsigned BitWidth = DL.getPointerSizeInBits(PtrTy->getAddressSpace());
6455 CE->getOperand(0), Type::getIntNTy(Ctx, BitWidth), false, DL))
6456 return isBytewiseValue(Op, DL);
6457 }
6458 }
6459 }
6460
6461 auto Merge = [&](Value *LHS, Value *RHS) -> Value * {
6462 if (LHS == RHS)
6463 return LHS;
6464 if (!LHS || !RHS)
6465 return nullptr;
6466 if (LHS == UndefInt8)
6467 return RHS;
6468 if (RHS == UndefInt8)
6469 return LHS;
6470 return nullptr;
6471 };
6472
6474 Value *Val = UndefInt8;
6475 for (uint64_t I = 0, E = CA->getNumElements(); I != E; ++I)
6476 if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))
6477 return nullptr;
6478 return Val;
6479 }
6480
6482 Value *Val = UndefInt8;
6483 for (Value *Op : C->operands())
6484 if (!(Val = Merge(Val, isBytewiseValue(Op, DL))))
6485 return nullptr;
6486 return Val;
6487 }
6488
6489 // Don't try to handle the handful of other constants.
6490 return nullptr;
6491}
6492
6493// This is the recursive version of BuildSubAggregate. It takes a few different
6494// arguments. Idxs is the index within the nested struct From that we are
6495// looking at now (which is of type IndexedType). IdxSkip is the number of
6496// indices from Idxs that should be left out when inserting into the resulting
6497// struct. To is the result struct built so far, new insertvalue instructions
6498// build on that.
6499static Value *BuildSubAggregate(Value *From, Value *To, Type *IndexedType,
6501 unsigned IdxSkip,
6502 BasicBlock::iterator InsertBefore) {
6503 StructType *STy = dyn_cast<StructType>(IndexedType);
6504 if (STy) {
6505 // Save the original To argument so we can modify it
6506 Value *OrigTo = To;
6507 // General case, the type indexed by Idxs is a struct
6508 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
6509 // Process each struct element recursively
6510 Idxs.push_back(i);
6511 Value *PrevTo = To;
6512 To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
6513 InsertBefore);
6514 Idxs.pop_back();
6515 if (!To) {
6516 // Couldn't find any inserted value for this index? Cleanup
6517 while (PrevTo != OrigTo) {
6519 PrevTo = Del->getAggregateOperand();
6520 Del->eraseFromParent();
6521 }
6522 // Stop processing elements
6523 break;
6524 }
6525 }
6526 // If we successfully found a value for each of our subaggregates
6527 if (To)
6528 return To;
6529 }
6530 // Base case, the type indexed by SourceIdxs is not a struct, or not all of
6531 // the struct's elements had a value that was inserted directly. In the latter
6532 // case, perhaps we can't determine each of the subelements individually, but
6533 // we might be able to find the complete struct somewhere.
6534
6535 // Find the value that is at that particular spot
6536 Value *V = FindInsertedValue(From, Idxs);
6537
6538 if (!V)
6539 return nullptr;
6540
6541 // Insert the value in the new (sub) aggregate
6542 return InsertValueInst::Create(To, V, ArrayRef(Idxs).slice(IdxSkip), "tmp",
6543 InsertBefore);
6544}
6545
6546// This helper takes a nested struct and extracts a part of it (which is again a
6547// struct) into a new value. For example, given the struct:
6548// { a, { b, { c, d }, e } }
6549// and the indices "1, 1" this returns
6550// { c, d }.
6551//
6552// It does this by inserting an insertvalue for each element in the resulting
6553// struct, as opposed to just inserting a single struct. This will only work if
6554// each of the elements of the substruct are known (ie, inserted into From by an
6555// insertvalue instruction somewhere).
6556//
6557// All inserted insertvalue instructions are inserted before InsertBefore
6559 BasicBlock::iterator InsertBefore) {
6560 Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
6561 idx_range);
6562 Value *To = PoisonValue::get(IndexedType);
6563 SmallVector<unsigned, 10> Idxs(idx_range);
6564 unsigned IdxSkip = Idxs.size();
6565
6566 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
6567}
6568
6569/// Given an aggregate and a sequence of indices, see if the scalar value
6570/// indexed is already around as a register, for example if it was inserted
6571/// directly into the aggregate.
6572///
6573/// If InsertBefore is not null, this function will duplicate (modified)
6574/// insertvalues when a part of a nested struct is extracted.
6575Value *
6577 std::optional<BasicBlock::iterator> InsertBefore) {
6578 // Nothing to index? Just return V then (this is useful at the end of our
6579 // recursion).
6580 if (idx_range.empty())
6581 return V;
6582 // We have indices, so V should have an indexable type.
6583 assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
6584 "Not looking at a struct or array?");
6585 assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
6586 "Invalid indices for type?");
6587
6588 if (Constant *C = dyn_cast<Constant>(V)) {
6589 C = C->getAggregateElement(idx_range[0]);
6590 if (!C) return nullptr;
6591 return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
6592 }
6593
6595 // Loop the indices for the insertvalue instruction in parallel with the
6596 // requested indices
6597 const unsigned *req_idx = idx_range.begin();
6598 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
6599 i != e; ++i, ++req_idx) {
6600 if (req_idx == idx_range.end()) {
6601 // We can't handle this without inserting insertvalues
6602 if (!InsertBefore)
6603 return nullptr;
6604
6605 // The requested index identifies a part of a nested aggregate. Handle
6606 // this specially. For example,
6607 // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
6608 // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
6609 // %C = extractvalue {i32, { i32, i32 } } %B, 1
6610 // This can be changed into
6611 // %A = insertvalue {i32, i32 } undef, i32 10, 0
6612 // %C = insertvalue {i32, i32 } %A, i32 11, 1
6613 // which allows the unused 0,0 element from the nested struct to be
6614 // removed.
6615 return BuildSubAggregate(V, ArrayRef(idx_range.begin(), req_idx),
6616 *InsertBefore);
6617 }
6618
6619 // This insert value inserts something else than what we are looking for.
6620 // See if the (aggregate) value inserted into has the value we are
6621 // looking for, then.
6622 if (*req_idx != *i)
6623 return FindInsertedValue(I->getAggregateOperand(), idx_range,
6624 InsertBefore);
6625 }
6626 // If we end up here, the indices of the insertvalue match with those
6627 // requested (though possibly only partially). Now we recursively look at
6628 // the inserted value, passing any remaining indices.
6629 return FindInsertedValue(I->getInsertedValueOperand(),
6630 ArrayRef(req_idx, idx_range.end()), InsertBefore);
6631 }
6632
6634 // If we're extracting a value from an aggregate that was extracted from
6635 // something else, we can extract from that something else directly instead.
6636 // However, we will need to chain I's indices with the requested indices.
6637
6638 // Calculate the number of indices required
6639 unsigned size = I->getNumIndices() + idx_range.size();
6640 // Allocate some space to put the new indices in
6642 Idxs.reserve(size);
6643 // Add indices from the extract value instruction
6644 Idxs.append(I->idx_begin(), I->idx_end());
6645
6646 // Add requested indices
6647 Idxs.append(idx_range.begin(), idx_range.end());
6648
6649 assert(Idxs.size() == size
6650 && "Number of indices added not correct?");
6651
6652 return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
6653 }
6654 // Otherwise, we don't know (such as, extracting from a function return value
6655 // or load instruction)
6656 return nullptr;
6657}
6658
6659// If V refers to an initialized global constant, set Slice either to
6660// its initializer if the size of its elements equals ElementSize, or,
6661// for ElementSize == 8, to its representation as an array of unsiged
6662// char. Return true on success.
6663// Offset is in the unit "nr of ElementSize sized elements".
6666 unsigned ElementSize, uint64_t Offset) {
6667 assert(V && "V should not be null.");
6668 assert((ElementSize % 8) == 0 &&
6669 "ElementSize expected to be a multiple of the size of a byte.");
6670 unsigned ElementSizeInBytes = ElementSize / 8;
6671
6672 // Drill down into the pointer expression V, ignoring any intervening
6673 // casts, and determine the identity of the object it references along
6674 // with the cumulative byte offset into it.
6675 const GlobalVariable *GV =
6677 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
6678 // Fail if V is not based on constant global object.
6679 return false;
6680
6681 const DataLayout &DL = GV->getDataLayout();
6682 APInt Off(DL.getIndexTypeSizeInBits(V->getType()), 0);
6683
6684 if (GV != V->stripAndAccumulateConstantOffsets(DL, Off,
6685 /*AllowNonInbounds*/ true))
6686 // Fail if a constant offset could not be determined.
6687 return false;
6688
6689 uint64_t StartIdx = Off.getLimitedValue();
6690 if (StartIdx == UINT64_MAX)
6691 // Fail if the constant offset is excessive.
6692 return false;
6693
6694 // Off/StartIdx is in the unit of bytes. So we need to convert to number of
6695 // elements. Simply bail out if that isn't possible.
6696 if ((StartIdx % ElementSizeInBytes) != 0)
6697 return false;
6698
6699 Offset += StartIdx / ElementSizeInBytes;
6700 ConstantDataArray *Array = nullptr;
6701 ArrayType *ArrayTy = nullptr;
6702
6703 if (GV->getInitializer()->isNullValue()) {
6704 Type *GVTy = GV->getValueType();
6705 uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy).getFixedValue();
6706 uint64_t Length = SizeInBytes / ElementSizeInBytes;
6707
6708 Slice.Array = nullptr;
6709 Slice.Offset = 0;
6710 // Return an empty Slice for undersized constants to let callers
6711 // transform even undefined library calls into simpler, well-defined
6712 // expressions. This is preferable to making the calls although it
6713 // prevents sanitizers from detecting such calls.
6714 Slice.Length = Length < Offset ? 0 : Length - Offset;
6715 return true;
6716 }
6717
6718 auto *Init = const_cast<Constant *>(GV->getInitializer());
6719 if (auto *ArrayInit = dyn_cast<ConstantDataArray>(Init)) {
6720 Type *InitElTy = ArrayInit->getElementType();
6721 if (InitElTy->isIntegerTy(ElementSize)) {
6722 // If Init is an initializer for an array of the expected type
6723 // and size, use it as is.
6724 Array = ArrayInit;
6725 ArrayTy = ArrayInit->getType();
6726 }
6727 }
6728
6729 if (!Array) {
6730 if (ElementSize != 8)
6731 // TODO: Handle conversions to larger integral types.
6732 return false;
6733
6734 // Otherwise extract the portion of the initializer starting
6735 // at Offset as an array of bytes, and reset Offset.
6737 if (!Init)
6738 return false;
6739
6740 Offset = 0;
6742 ArrayTy = dyn_cast<ArrayType>(Init->getType());
6743 }
6744
6745 uint64_t NumElts = ArrayTy->getArrayNumElements();
6746 if (Offset > NumElts)
6747 return false;
6748
6749 Slice.Array = Array;
6750 Slice.Offset = Offset;
6751 Slice.Length = NumElts - Offset;
6752 return true;
6753}
6754
6755/// Extract bytes from the initializer of the constant array V, which need
6756/// not be a nul-terminated string. On success, store the bytes in Str and
6757/// return true. When TrimAtNul is set, Str will contain only the bytes up
6758/// to but not including the first nul. Return false on failure.
6760 bool TrimAtNul) {
6762 if (!getConstantDataArrayInfo(V, Slice, 8))
6763 return false;
6764
6765 if (Slice.Array == nullptr) {
6766 if (TrimAtNul) {
6767 // Return a nul-terminated string even for an empty Slice. This is
6768 // safe because all existing SimplifyLibcalls callers require string
6769 // arguments and the behavior of the functions they fold is undefined
6770 // otherwise. Folding the calls this way is preferable to making
6771 // the undefined library calls, even though it prevents sanitizers
6772 // from reporting such calls.
6773 Str = StringRef();
6774 return true;
6775 }
6776 if (Slice.Length == 1) {
6777 Str = StringRef("", 1);
6778 return true;
6779 }
6780 // We cannot instantiate a StringRef as we do not have an appropriate string
6781 // of 0s at hand.
6782 return false;
6783 }
6784
6785 // Start out with the entire array in the StringRef.
6786 Str = Slice.Array->getAsString();
6787 // Skip over 'offset' bytes.
6788 Str = Str.substr(Slice.Offset);
6789
6790 if (TrimAtNul) {
6791 // Trim off the \0 and anything after it. If the array is not nul
6792 // terminated, we just return the whole end of string. The client may know
6793 // some other way that the string is length-bound.
6794 Str = Str.substr(0, Str.find('\0'));
6795 }
6796 return true;
6797}
6798
6799// These next two are very similar to the above, but also look through PHI
6800// nodes.
6801// TODO: See if we can integrate these two together.
6802
6803/// If we can compute the length of the string pointed to by
6804/// the specified pointer, return 'len+1'. If we can't, return 0.
6807 unsigned CharSize) {
6808 // Look through noop bitcast instructions.
6809 V = V->stripPointerCasts();
6810
6811 // If this is a PHI node, there are two cases: either we have already seen it
6812 // or we haven't.
6813 if (const PHINode *PN = dyn_cast<PHINode>(V)) {
6814 if (!PHIs.insert(PN).second)
6815 return ~0ULL; // already in the set.
6816
6817 // If it was new, see if all the input strings are the same length.
6818 uint64_t LenSoFar = ~0ULL;
6819 for (Value *IncValue : PN->incoming_values()) {
6820 uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize);
6821 if (Len == 0) return 0; // Unknown length -> unknown.
6822
6823 if (Len == ~0ULL) continue;
6824
6825 if (Len != LenSoFar && LenSoFar != ~0ULL)
6826 return 0; // Disagree -> unknown.
6827 LenSoFar = Len;
6828 }
6829
6830 // Success, all agree.
6831 return LenSoFar;
6832 }
6833
6834 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
6835 if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
6836 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);
6837 if (Len1 == 0) return 0;
6838 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);
6839 if (Len2 == 0) return 0;
6840 if (Len1 == ~0ULL) return Len2;
6841 if (Len2 == ~0ULL) return Len1;
6842 if (Len1 != Len2) return 0;
6843 return Len1;
6844 }
6845
6846 // Otherwise, see if we can read the string.
6848 if (!getConstantDataArrayInfo(V, Slice, CharSize))
6849 return 0;
6850
6851 if (Slice.Array == nullptr)
6852 // Zeroinitializer (including an empty one).
6853 return 1;
6854
6855 // Search for the first nul character. Return a conservative result even
6856 // when there is no nul. This is safe since otherwise the string function
6857 // being folded such as strlen is undefined, and can be preferable to
6858 // making the undefined library call.
6859 unsigned NullIndex = 0;
6860 for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) {
6861 if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0)
6862 break;
6863 }
6864
6865 return NullIndex + 1;
6866}
6867
6868/// If we can compute the length of the string pointed to by
6869/// the specified pointer, return 'len+1'. If we can't, return 0.
6870uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) {
6871 if (!V->getType()->isPointerTy())
6872 return 0;
6873
6875 uint64_t Len = GetStringLengthH(V, PHIs, CharSize);
6876 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
6877 // an empty string as a length.
6878 return Len == ~0ULL ? 1 : Len;
6879}
6880
6881const Value *
6883 bool MustPreserveOffset) {
6884 assert(Call &&
6885 "getArgumentAliasingToReturnedPointer only works on nonnull calls");
6886 if (const Value *RV = Call->getReturnedArgOperand())
6887 return RV;
6888 // This can be used only as a aliasing property.
6890 Call, MustPreserveOffset))
6891 return Call->getArgOperand(0);
6892 return nullptr;
6893}
6894
6896 const CallBase *Call, bool MustPreserveOffset) {
6897 switch (Call->getIntrinsicID()) {
6898 case Intrinsic::launder_invariant_group:
6899 case Intrinsic::strip_invariant_group:
6900 case Intrinsic::aarch64_irg:
6901 case Intrinsic::aarch64_tagp:
6902 // The amdgcn_make_buffer_rsrc function does not alter the address of the
6903 // input pointer (and thus preserves the byte offset, which is the property
6904 // the MustPreserveOffset flag selects). However, it will not necessarily
6905 // map ptr addrspace(N) null to ptr addrspace(8) null, aka the "null
6906 // descriptor", which has "all loads return 0, all stores are dropped"
6907 // semantics. Given the context of this intrinsic list, no one should be
6908 // relying on such a strict bit-exact null mapping (and, at time of
6909 // writing, they are not), but we document this fact out of an abundance
6910 // of caution.
6911 case Intrinsic::amdgcn_make_buffer_rsrc:
6912 return true;
6913 case Intrinsic::ptrmask:
6914 return !MustPreserveOffset;
6915 case Intrinsic::threadlocal_address:
6916 // The underlying variable changes with thread ID. The Thread ID may change
6917 // at coroutine suspend points.
6918 return !Call->getParent()->getParent()->isPresplitCoroutine();
6919 default:
6920 return false;
6921 }
6922}
6923
6924/// \p PN defines a loop-variant pointer to an object. Check if the
6925/// previous iteration of the loop was referring to the same object as \p PN.
6927 const LoopInfo *LI) {
6928 // Find the loop-defined value.
6929 Loop *L = LI->getLoopFor(PN->getParent());
6930 if (PN->getNumIncomingValues() != 2)
6931 return true;
6932
6933 // Find the value from previous iteration.
6934 auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
6935 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6936 PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
6937 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6938 return true;
6939
6940 // If a new pointer is loaded in the loop, the pointer references a different
6941 // object in every iteration. E.g.:
6942 // for (i)
6943 // int *p = a[i];
6944 // ...
6945 if (auto *Load = dyn_cast<LoadInst>(PrevValue))
6946 if (!L->isLoopInvariant(Load->getPointerOperand()))
6947 return false;
6948 return true;
6949}
6950
6951const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup) {
6952 for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
6953 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
6954 const Value *PtrOp = GEP->getPointerOperand();
6955 if (!PtrOp->getType()->isPointerTy()) // Only handle scalar pointer base.
6956 return V;
6957 V = PtrOp;
6958 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
6959 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
6960 Value *NewV = cast<Operator>(V)->getOperand(0);
6961 if (!NewV->getType()->isPointerTy())
6962 return V;
6963 V = NewV;
6964 } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
6965 if (GA->isInterposable())
6966 return V;
6967 V = GA->getAliasee();
6968 } else {
6969 if (auto *PHI = dyn_cast<PHINode>(V)) {
6970 // Look through single-arg phi nodes created by LCSSA.
6971 if (PHI->getNumIncomingValues() == 1) {
6972 V = PHI->getIncomingValue(0);
6973 continue;
6974 }
6975 } else if (auto *Call = dyn_cast<CallBase>(V)) {
6976 // CaptureTracking can know about special capturing properties of some
6977 // intrinsics like launder.invariant.group, that can't be expressed with
6978 // the attributes, but have properties like returning aliasing pointer.
6979 // Because some analysis may assume that nocaptured pointer is not
6980 // returned from some special intrinsic (because function would have to
6981 // be marked with returns attribute), it is crucial to use this function
6982 // because it should be in sync with CaptureTracking. Not using it may
6983 // cause weird miscompilations where 2 aliasing pointers are assumed to
6984 // noalias.
6986 Call, /*MustPreserveOffset=*/false)) {
6987 V = RP;
6988 continue;
6989 }
6990 }
6991
6992 return V;
6993 }
6994 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
6995 }
6996 return V;
6997}
6998
7001 const LoopInfo *LI, unsigned MaxLookup) {
7004 Worklist.push_back(V);
7005 do {
7006 const Value *P = Worklist.pop_back_val();
7007 P = getUnderlyingObject(P, MaxLookup);
7008
7009 if (!Visited.insert(P).second)
7010 continue;
7011
7012 if (auto *SI = dyn_cast<SelectInst>(P)) {
7013 Worklist.push_back(SI->getTrueValue());
7014 Worklist.push_back(SI->getFalseValue());
7015 continue;
7016 }
7017
7018 if (auto *PN = dyn_cast<PHINode>(P)) {
7019 // If this PHI changes the underlying object in every iteration of the
7020 // loop, don't look through it. Consider:
7021 // int **A;
7022 // for (i) {
7023 // Prev = Curr; // Prev = PHI (Prev_0, Curr)
7024 // Curr = A[i];
7025 // *Prev, *Curr;
7026 //
7027 // Prev is tracking Curr one iteration behind so they refer to different
7028 // underlying objects.
7029 if (!LI || !LI->isLoopHeader(PN->getParent()) ||
7031 append_range(Worklist, PN->incoming_values());
7032 else
7033 Objects.push_back(P);
7034 continue;
7035 }
7036
7037 Objects.push_back(P);
7038 } while (!Worklist.empty());
7039}
7040
7042 const unsigned MaxVisited = 8;
7043
7046 Worklist.push_back(V);
7047 const Value *Object = nullptr;
7048 // Used as fallback if we can't find a common underlying object through
7049 // recursion.
7050 bool First = true;
7051 const Value *FirstObject = getUnderlyingObject(V);
7052 do {
7053 const Value *P = Worklist.pop_back_val();
7054 P = First ? FirstObject : getUnderlyingObject(P);
7055 First = false;
7056
7057 if (!Visited.insert(P).second)
7058 continue;
7059
7060 if (Visited.size() == MaxVisited)
7061 return FirstObject;
7062
7063 if (auto *SI = dyn_cast<SelectInst>(P)) {
7064 Worklist.push_back(SI->getTrueValue());
7065 Worklist.push_back(SI->getFalseValue());
7066 continue;
7067 }
7068
7069 if (auto *PN = dyn_cast<PHINode>(P)) {
7070 append_range(Worklist, PN->incoming_values());
7071 continue;
7072 }
7073
7074 if (!Object)
7075 Object = P;
7076 else if (Object != P)
7077 return FirstObject;
7078 } while (!Worklist.empty());
7079
7080 return Object ? Object : FirstObject;
7081}
7082
7083/// This is the function that does the work of looking through basic
7084/// ptrtoint+arithmetic+inttoptr sequences.
7085static const Value *getUnderlyingObjectFromInt(const Value *V) {
7086 do {
7087 if (const Operator *U = dyn_cast<Operator>(V)) {
7088 // If we find a ptrtoint, we can transfer control back to the
7089 // regular getUnderlyingObjectFromInt.
7090 if (U->getOpcode() == Instruction::PtrToInt)
7091 return U->getOperand(0);
7092 // If we find an add of a constant, a multiplied value, or a phi, it's
7093 // likely that the other operand will lead us to the base
7094 // object. We don't have to worry about the case where the
7095 // object address is somehow being computed by the multiply,
7096 // because our callers only care when the result is an
7097 // identifiable object.
7098 if (U->getOpcode() != Instruction::Add ||
7099 (!isa<ConstantInt>(U->getOperand(1)) &&
7100 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
7101 !isa<PHINode>(U->getOperand(1))))
7102 return V;
7103 V = U->getOperand(0);
7104 } else {
7105 return V;
7106 }
7107 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
7108 } while (true);
7109}
7110
7111/// This is a wrapper around getUnderlyingObjects and adds support for basic
7112/// ptrtoint+arithmetic+inttoptr sequences.
7113/// It returns false if unidentified object is found in getUnderlyingObjects.
7115 SmallVectorImpl<Value *> &Objects) {
7117 SmallVector<const Value *, 4> Working(1, V);
7118 do {
7119 V = Working.pop_back_val();
7120
7122 getUnderlyingObjects(V, Objs);
7123
7124 for (const Value *V : Objs) {
7125 if (!Visited.insert(V).second)
7126 continue;
7127 if (Operator::getOpcode(V) == Instruction::IntToPtr) {
7128 const Value *O =
7129 getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
7130 if (O->getType()->isPointerTy()) {
7131 Working.push_back(O);
7132 continue;
7133 }
7134 }
7135 // If getUnderlyingObjects fails to find an identifiable object,
7136 // getUnderlyingObjectsForCodeGen also fails for safety.
7137 if (!isIdentifiedObject(V)) {
7138 Objects.clear();
7139 return false;
7140 }
7141 Objects.push_back(const_cast<Value *>(V));
7142 }
7143 } while (!Working.empty());
7144 return true;
7145}
7146
7148 AllocaInst *Result = nullptr;
7150 SmallVector<Value *, 4> Worklist;
7151
7152 auto AddWork = [&](Value *V) {
7153 if (Visited.insert(V).second)
7154 Worklist.push_back(V);
7155 };
7156
7157 AddWork(V);
7158 do {
7159 V = Worklist.pop_back_val();
7160 assert(Visited.count(V));
7161
7162 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
7163 if (Result && Result != AI)
7164 return nullptr;
7165 Result = AI;
7166 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
7167 AddWork(CI->getOperand(0));
7168 } else if (PHINode *PN = dyn_cast<PHINode>(V)) {
7169 for (Value *IncValue : PN->incoming_values())
7170 AddWork(IncValue);
7171 } else if (auto *SI = dyn_cast<SelectInst>(V)) {
7172 AddWork(SI->getTrueValue());
7173 AddWork(SI->getFalseValue());
7175 if (OffsetZero && !GEP->hasAllZeroIndices())
7176 return nullptr;
7177 AddWork(GEP->getPointerOperand());
7178 } else if (CallBase *CB = dyn_cast<CallBase>(V)) {
7179 Value *Returned = CB->getReturnedArgOperand();
7180 if (Returned)
7181 AddWork(Returned);
7182 else
7183 return nullptr;
7184 } else {
7185 return nullptr;
7186 }
7187 } while (!Worklist.empty());
7188
7189 return Result;
7190}
7191
7193 const Value *V, bool AllowLifetime, bool AllowDroppable) {
7194 for (const User *U : V->users()) {
7196 if (!II)
7197 return false;
7198
7199 if (AllowLifetime && II->isLifetimeStartOrEnd())
7200 continue;
7201
7202 if (AllowDroppable && II->isDroppable())
7203 continue;
7204
7205 return false;
7206 }
7207 return true;
7208}
7209
7212 V, /* AllowLifetime */ true, /* AllowDroppable */ false);
7213}
7216 V, /* AllowLifetime */ true, /* AllowDroppable */ true);
7217}
7218
7220 if (auto *II = dyn_cast<IntrinsicInst>(I))
7221 return isTriviallyVectorizable(II->getIntrinsicID());
7222 auto *Shuffle = dyn_cast<ShuffleVectorInst>(I);
7223 return (!Shuffle || Shuffle->isSelect()) &&
7225}
7226
7228 const Instruction *Inst, const Instruction *CtxI, AssumptionCache *AC,
7229 const DominatorTree *DT, const TargetLibraryInfo *TLI, bool UseVariableInfo,
7230 bool IgnoreUBImplyingAttrs) {
7231 return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI,
7232 AC, DT, TLI, UseVariableInfo,
7233 IgnoreUBImplyingAttrs);
7234}
7235
7237 unsigned Opcode, const Instruction *Inst, const Instruction *CtxI,
7238 AssumptionCache *AC, const DominatorTree *DT, const TargetLibraryInfo *TLI,
7239 bool UseVariableInfo, bool IgnoreUBImplyingAttrs) {
7240#ifndef NDEBUG
7241 if (Inst->getOpcode() != Opcode) {
7242 // Check that the operands are actually compatible with the Opcode override.
7243 auto hasEqualReturnAndLeadingOperandTypes =
7244 [](const Instruction *Inst, unsigned NumLeadingOperands) {
7245 if (Inst->getNumOperands() < NumLeadingOperands)
7246 return false;
7247 const Type *ExpectedType = Inst->getType();
7248 for (unsigned ItOp = 0; ItOp < NumLeadingOperands; ++ItOp)
7249 if (Inst->getOperand(ItOp)->getType() != ExpectedType)
7250 return false;
7251 return true;
7252 };
7254 hasEqualReturnAndLeadingOperandTypes(Inst, 2));
7255 assert(!Instruction::isUnaryOp(Opcode) ||
7256 hasEqualReturnAndLeadingOperandTypes(Inst, 1));
7257 }
7258#endif
7259
7260 switch (Opcode) {
7261 default:
7262 return true;
7263 case Instruction::UDiv:
7264 case Instruction::URem: {
7265 // x / y is undefined if y == 0.
7266 const APInt *V;
7267 if (match(Inst->getOperand(1), m_APInt(V)))
7268 return *V != 0;
7269 return false;
7270 }
7271 case Instruction::SDiv:
7272 case Instruction::SRem: {
7273 // x / y is undefined if y == 0 or x == INT_MIN and y == -1
7274 const APInt *Numerator, *Denominator;
7275 if (!match(Inst->getOperand(1), m_APInt(Denominator)))
7276 return false;
7277 // We cannot hoist this division if the denominator is 0.
7278 if (*Denominator == 0)
7279 return false;
7280 // It's safe to hoist if the denominator is not 0 or -1.
7281 if (!Denominator->isAllOnes())
7282 return true;
7283 // At this point we know that the denominator is -1. It is safe to hoist as
7284 // long we know that the numerator is not INT_MIN.
7285 if (match(Inst->getOperand(0), m_APInt(Numerator)))
7286 return !Numerator->isMinSignedValue();
7287 // The numerator *might* be MinSignedValue.
7288 return false;
7289 }
7290 case Instruction::Load: {
7291 if (!UseVariableInfo)
7292 return false;
7293
7294 const LoadInst *LI = dyn_cast<LoadInst>(Inst);
7295 if (!LI)
7296 return false;
7297 if (mustSuppressSpeculation(*LI))
7298 return false;
7299 const DataLayout &DL = LI->getDataLayout();
7301 LI->getType(), LI->getAlign(), DL,
7302 CtxI, AC, DT, TLI);
7303 }
7304 case Instruction::Call: {
7305 auto *CI = dyn_cast<const CallInst>(Inst);
7306 if (!CI)
7307 return false;
7308 const Function *Callee = CI->getCalledFunction();
7309
7310 // The called function could have undefined behavior or side-effects, even
7311 // if marked readnone nounwind.
7312 if (!Callee || !Callee->isSpeculatable())
7313 return false;
7314 // Since the operands may be changed after hoisting, undefined behavior may
7315 // be triggered by some UB-implying attributes.
7316 return IgnoreUBImplyingAttrs || !CI->hasUBImplyingAttrs();
7317 }
7318 case Instruction::VAArg:
7319 case Instruction::Alloca:
7320 case Instruction::Invoke:
7321 case Instruction::CallBr:
7322 case Instruction::PHI:
7323 case Instruction::Store:
7324 case Instruction::Ret:
7325 case Instruction::UncondBr:
7326 case Instruction::CondBr:
7327 case Instruction::IndirectBr:
7328 case Instruction::Switch:
7329 case Instruction::Unreachable:
7330 case Instruction::Fence:
7331 case Instruction::AtomicRMW:
7332 case Instruction::AtomicCmpXchg:
7333 case Instruction::LandingPad:
7334 case Instruction::Resume:
7335 case Instruction::CatchSwitch:
7336 case Instruction::CatchPad:
7337 case Instruction::CatchRet:
7338 case Instruction::CleanupPad:
7339 case Instruction::CleanupRet:
7340 return false; // Misc instructions which have effects
7341 }
7342}
7343
7345 if (I.mayReadOrWriteMemory())
7346 // Memory dependency possible
7347 return true;
7349 // Can't move above a maythrow call or infinite loop. Or if an
7350 // inalloca alloca, above a stacksave call.
7351 return true;
7353 // 1) Can't reorder two inf-loop calls, even if readonly
7354 // 2) Also can't reorder an inf-loop call below a instruction which isn't
7355 // safe to speculative execute. (Inverse of above)
7356 return true;
7357 return false;
7358}
7359
7360/// Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
7374
7375/// Combine constant ranges from computeConstantRange() and computeKnownBits().
7378 bool ForSigned,
7379 const SimplifyQuery &SQ) {
7380 ConstantRange CR1 =
7381 ConstantRange::fromKnownBits(V.getKnownBits(SQ), ForSigned);
7382 ConstantRange CR2 = computeConstantRange(V, ForSigned, SQ);
7385 return CR1.intersectWith(CR2, RangeType);
7386}
7387
7389 const Value *RHS,
7390 const SimplifyQuery &SQ,
7391 bool IsNSW) {
7392 ConstantRange LHSRange =
7393 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7394 ConstantRange RHSRange =
7395 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7396
7397 // mul nsw of two non-negative numbers is also nuw.
7398 if (IsNSW && LHSRange.isAllNonNegative() && RHSRange.isAllNonNegative())
7400
7401 return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange));
7402}
7403
7405 const Value *RHS,
7406 const SimplifyQuery &SQ) {
7407 // Multiplying n * m significant bits yields a result of n + m significant
7408 // bits. If the total number of significant bits does not exceed the
7409 // result bit width (minus 1), there is no overflow.
7410 // This means if we have enough leading sign bits in the operands
7411 // we can guarantee that the result does not overflow.
7412 // Ref: "Hacker's Delight" by Henry Warren
7413 unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
7414
7415 // Note that underestimating the number of sign bits gives a more
7416 // conservative answer.
7417 unsigned SignBits =
7418 ::ComputeNumSignBits(LHS, SQ) + ::ComputeNumSignBits(RHS, SQ);
7419
7420 // First handle the easy case: if we have enough sign bits there's
7421 // definitely no overflow.
7422 if (SignBits > BitWidth + 1)
7424
7425 // There are two ambiguous cases where there can be no overflow:
7426 // SignBits == BitWidth + 1 and
7427 // SignBits == BitWidth
7428 // The second case is difficult to check, therefore we only handle the
7429 // first case.
7430 if (SignBits == BitWidth + 1) {
7431 // It overflows only when both arguments are negative and the true
7432 // product is exactly the minimum negative number.
7433 // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
7434 // For simplicity we just check if at least one side is not negative.
7435 KnownBits LHSKnown = computeKnownBits(LHS, SQ);
7436 KnownBits RHSKnown = computeKnownBits(RHS, SQ);
7437 if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative())
7439 }
7441}
7442
7445 const WithCache<const Value *> &RHS,
7446 const SimplifyQuery &SQ) {
7447 ConstantRange LHSRange =
7448 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7449 ConstantRange RHSRange =
7450 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7451 return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange));
7452}
7453
7454static OverflowResult
7457 const AddOperator *Add, const SimplifyQuery &SQ) {
7458 if (Add && Add->hasNoSignedWrap()) {
7460 }
7461
7462 // If LHS and RHS each have at least two sign bits, the addition will look
7463 // like
7464 //
7465 // XX..... +
7466 // YY.....
7467 //
7468 // If the carry into the most significant position is 0, X and Y can't both
7469 // be 1 and therefore the carry out of the addition is also 0.
7470 //
7471 // If the carry into the most significant position is 1, X and Y can't both
7472 // be 0 and therefore the carry out of the addition is also 1.
7473 //
7474 // Since the carry into the most significant position is always equal to
7475 // the carry out of the addition, there is no signed overflow.
7476 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
7478
7479 ConstantRange LHSRange =
7480 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7481 ConstantRange RHSRange =
7482 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7483 OverflowResult OR =
7484 mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange));
7486 return OR;
7487
7488 // The remaining code needs Add to be available. Early returns if not so.
7489 if (!Add)
7491
7492 // If the sign of Add is the same as at least one of the operands, this add
7493 // CANNOT overflow. If this can be determined from the known bits of the
7494 // operands the above signedAddMayOverflow() check will have already done so.
7495 // The only other way to improve on the known bits is from an assumption, so
7496 // call computeKnownBitsFromContext() directly.
7497 bool LHSOrRHSKnownNonNegative =
7498 (LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative());
7499 bool LHSOrRHSKnownNegative =
7500 (LHSRange.isAllNegative() || RHSRange.isAllNegative());
7501 if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
7502 KnownBits AddKnown(LHSRange.getBitWidth());
7503 computeKnownBitsFromContext(Add, AddKnown, SQ);
7504 if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||
7505 (AddKnown.isNegative() && LHSOrRHSKnownNegative))
7507 }
7508
7510}
7511
7513 const Value *RHS,
7514 const SimplifyQuery &SQ) {
7515 // X - (X % ?)
7516 // The remainder of a value can't have greater magnitude than itself,
7517 // so the subtraction can't overflow.
7518
7519 // X - (X -nuw ?)
7520 // In the minimal case, this would simplify to "?", so there's no subtract
7521 // at all. But if this analysis is used to peek through casts, for example,
7522 // then determining no-overflow may allow other transforms.
7523
7524 // TODO: There are other patterns like this.
7525 // See simplifyICmpWithBinOpOnLHS() for candidates.
7526 if (match(RHS, m_URem(m_Specific(LHS), m_Value())) ||
7527 match(RHS, m_NUWSub(m_Specific(LHS), m_Value())))
7528 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7530
7531 if (auto C = isImpliedByDomCondition(CmpInst::ICMP_UGE, LHS, RHS, SQ.CxtI,
7532 SQ.DL)) {
7533 if (*C)
7536 }
7537
7538 ConstantRange LHSRange =
7539 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7540 ConstantRange RHSRange =
7541 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7542 return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange));
7543}
7544
7546 const Value *RHS,
7547 const SimplifyQuery &SQ) {
7548 // X - (X % ?)
7549 // The remainder of a value can't have greater magnitude than itself,
7550 // so the subtraction can't overflow.
7551
7552 // X - (X -nsw ?)
7553 // In the minimal case, this would simplify to "?", so there's no subtract
7554 // at all. But if this analysis is used to peek through casts, for example,
7555 // then determining no-overflow may allow other transforms.
7556 if (match(RHS, m_SRem(m_Specific(LHS), m_Value())) ||
7557 match(RHS, m_NSWSub(m_Specific(LHS), m_Value())))
7558 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7560
7561 // If LHS and RHS each have at least two sign bits, the subtraction
7562 // cannot overflow.
7563 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
7565
7566 ConstantRange LHSRange =
7567 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7568 ConstantRange RHSRange =
7569 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7570 return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange));
7571}
7572
7574 const DominatorTree &DT) {
7575 SmallVector<const CondBrInst *, 2> GuardingBranches;
7577
7578 for (const User *U : WO->users()) {
7579 if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) {
7580 assert(EVI->getNumIndices() == 1 && "Obvious from CI's type");
7581
7582 if (EVI->getIndices()[0] == 0)
7583 Results.push_back(EVI);
7584 else {
7585 assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type");
7586
7587 for (const auto *U : EVI->users())
7588 if (const auto *B = dyn_cast<CondBrInst>(U))
7589 GuardingBranches.push_back(B);
7590 }
7591 } else {
7592 // We are using the aggregate directly in a way we don't want to analyze
7593 // here (storing it to a global, say).
7594 return false;
7595 }
7596 }
7597
7598 auto AllUsesGuardedByBranch = [&](const CondBrInst *BI) {
7599 BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1));
7600
7601 // Check if all users of the add are provably no-wrap.
7602 for (const auto *Result : Results) {
7603 // If the extractvalue itself is not executed on overflow, the we don't
7604 // need to check each use separately, since domination is transitive.
7605 if (DT.dominates(NoWrapEdge, Result->getParent()))
7606 continue;
7607
7608 for (const auto &RU : Result->uses())
7609 if (!DT.dominates(NoWrapEdge, RU))
7610 return false;
7611 }
7612
7613 return true;
7614 };
7615
7616 return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
7617}
7618
7619/// Shifts return poison if shiftwidth is larger than the bitwidth.
7620static bool shiftAmountKnownInRange(const Value *ShiftAmount) {
7621 auto *C = dyn_cast<Constant>(ShiftAmount);
7622 if (!C)
7623 return false;
7624
7625 // Shifts return poison if shiftwidth is larger than the bitwidth.
7627 if (auto *FVTy = dyn_cast<FixedVectorType>(C->getType())) {
7628 unsigned NumElts = FVTy->getNumElements();
7629 for (unsigned i = 0; i < NumElts; ++i)
7630 ShiftAmounts.push_back(C->getAggregateElement(i));
7631 } else if (isa<ScalableVectorType>(C->getType()))
7632 return false; // Can't tell, just return false to be safe
7633 else
7634 ShiftAmounts.push_back(C);
7635
7636 bool Safe = llvm::all_of(ShiftAmounts, [](const Constant *C) {
7637 auto *CI = dyn_cast_or_null<ConstantInt>(C);
7638 return CI && CI->getValue().ult(C->getType()->getIntegerBitWidth());
7639 });
7640
7641 return Safe;
7642}
7643
7645 bool ConsiderFlagsAndMetadata) {
7646
7647 if (ConsiderFlagsAndMetadata && includesPoison(Kind) &&
7648 Op->hasPoisonGeneratingAnnotations())
7649 return true;
7650
7651 unsigned Opcode = Op->getOpcode();
7652
7653 // Check whether opcode is a poison/undef-generating operation
7654 switch (Opcode) {
7655 case Instruction::Shl:
7656 case Instruction::AShr:
7657 case Instruction::LShr:
7658 return includesPoison(Kind) && !shiftAmountKnownInRange(Op->getOperand(1));
7659 case Instruction::FPToSI:
7660 case Instruction::FPToUI:
7661 // fptosi/ui yields poison if the resulting value does not fit in the
7662 // destination type.
7663 return true;
7664 case Instruction::Call:
7665 if (auto *II = dyn_cast<IntrinsicInst>(Op)) {
7666 switch (II->getIntrinsicID()) {
7667 // NOTE: Use IntrNoCreateUndefOrPoison when possible.
7668 case Intrinsic::ctlz:
7669 case Intrinsic::cttz:
7670 case Intrinsic::abs:
7671 // We're not considering flags so it is safe to just return false.
7672 return false;
7673 case Intrinsic::sshl_sat:
7674 case Intrinsic::ushl_sat:
7675 if (!includesPoison(Kind) ||
7676 shiftAmountKnownInRange(II->getArgOperand(1)))
7677 return false;
7678 break;
7679 }
7680 }
7681 [[fallthrough]];
7682 case Instruction::CallBr:
7683 case Instruction::Invoke: {
7684 const auto *CB = cast<CallBase>(Op);
7685 return !CB->hasRetAttr(Attribute::NoUndef) &&
7686 !CB->hasFnAttr(Attribute::NoCreateUndefOrPoison);
7687 }
7688 case Instruction::InsertElement:
7689 case Instruction::ExtractElement: {
7690 // If index exceeds the length of the vector, it returns poison
7691 auto *VTy = cast<VectorType>(Op->getOperand(0)->getType());
7692 unsigned IdxOp = Op->getOpcode() == Instruction::InsertElement ? 2 : 1;
7693 auto *Idx = dyn_cast<ConstantInt>(Op->getOperand(IdxOp));
7694 if (includesPoison(Kind))
7695 return !Idx ||
7696 Idx->getValue().uge(VTy->getElementCount().getKnownMinValue());
7697 return false;
7698 }
7699 case Instruction::ShuffleVector: {
7701 ? cast<ConstantExpr>(Op)->getShuffleMask()
7702 : cast<ShuffleVectorInst>(Op)->getShuffleMask();
7703 return includesPoison(Kind) && is_contained(Mask, PoisonMaskElem);
7704 }
7705 case Instruction::FNeg:
7706 case Instruction::PHI:
7707 case Instruction::Select:
7708 case Instruction::ExtractValue:
7709 case Instruction::InsertValue:
7710 case Instruction::Freeze:
7711 case Instruction::ICmp:
7712 case Instruction::FCmp:
7713 case Instruction::GetElementPtr:
7714 return false;
7715 case Instruction::AddrSpaceCast:
7716 return true;
7717 default: {
7718 const auto *CE = dyn_cast<ConstantExpr>(Op);
7719 if (isa<CastInst>(Op) || (CE && CE->isCast()))
7720 return false;
7721 else if (Instruction::isBinaryOp(Opcode))
7722 return false;
7723 // Be conservative and return true.
7724 return true;
7725 }
7726 }
7727}
7728
7730 bool ConsiderFlagsAndMetadata) {
7731 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::UndefOrPoison,
7732 ConsiderFlagsAndMetadata);
7733}
7734
7735bool llvm::canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata) {
7736 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::PoisonOnly,
7737 ConsiderFlagsAndMetadata);
7738}
7739
7740static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V,
7741 unsigned Depth) {
7742 if (ValAssumedPoison == V)
7743 return true;
7744
7745 const unsigned MaxDepth = 2;
7746 if (Depth >= MaxDepth)
7747 return false;
7748
7749 if (const auto *I = dyn_cast<Instruction>(V)) {
7750 if (any_of(I->operands(), [=](const Use &Op) {
7751 return propagatesPoison(Op) &&
7752 directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1);
7753 }))
7754 return true;
7755
7756 // V = extractvalue V0, idx
7757 // V2 = extractvalue V0, idx2
7758 // V0's elements are all poison or not. (e.g., add_with_overflow)
7759 const WithOverflowInst *II;
7761 (match(ValAssumedPoison, m_ExtractValue(m_Specific(II))) ||
7762 llvm::is_contained(II->args(), ValAssumedPoison)))
7763 return true;
7764 }
7765 return false;
7766}
7767
7768static bool impliesPoison(const Value *ValAssumedPoison, const Value *V,
7769 unsigned Depth) {
7770 if (isGuaranteedNotToBePoison(ValAssumedPoison))
7771 return true;
7772
7773 if (directlyImpliesPoison(ValAssumedPoison, V, /* Depth */ 0))
7774 return true;
7775
7776 const unsigned MaxDepth = 2;
7777 if (Depth >= MaxDepth)
7778 return false;
7779
7780 const auto *I = dyn_cast<Instruction>(ValAssumedPoison);
7781 if (I && !canCreatePoison(cast<Operator>(I))) {
7782 return all_of(I->operands(), [=](const Value *Op) {
7783 return impliesPoison(Op, V, Depth + 1);
7784 });
7785 }
7786 return false;
7787}
7788
7789bool llvm::impliesPoison(const Value *ValAssumedPoison, const Value *V) {
7790 return ::impliesPoison(ValAssumedPoison, V, /* Depth */ 0);
7791}
7792
7793static bool programUndefinedIfUndefOrPoison(const Value *V, bool PoisonOnly);
7794
7796 const Value *V, AssumptionCache *AC, const Instruction *CtxI,
7797 const DominatorTree *DT, unsigned Depth, UndefPoisonKind Kind) {
7799 return false;
7800
7801 if (isa<MetadataAsValue>(V))
7802 return false;
7803
7804 if (const auto *A = dyn_cast<Argument>(V)) {
7805 if (A->hasAttribute(Attribute::NoUndef) ||
7806 A->hasAttribute(Attribute::Dereferenceable) ||
7807 A->hasAttribute(Attribute::DereferenceableOrNull))
7808 return true;
7809 }
7810
7811 if (auto *C = dyn_cast<Constant>(V)) {
7812 if (isa<PoisonValue>(C))
7813 return !includesPoison(Kind);
7814
7815 if (isa<UndefValue>(C))
7816 return !includesUndef(Kind);
7817
7820 return true;
7821
7822 if (C->getType()->isVectorTy()) {
7823 if (isa<ConstantExpr>(C)) {
7824 // Scalable vectors can use a ConstantExpr to build a splat.
7825 if (Constant *SplatC = C->getSplatValue())
7826 if (isa<ConstantInt>(SplatC) || isa<ConstantFP>(SplatC))
7827 return true;
7828 } else {
7829 if (includesUndef(Kind) && C->containsUndefElement())
7830 return false;
7831 if (includesPoison(Kind) && C->containsPoisonElement())
7832 return false;
7833 return !C->containsConstantExpression();
7834 }
7835 }
7836 }
7837
7838 // Strip cast operations from a pointer value.
7839 // Note that stripPointerCastsSameRepresentation can strip off getelementptr
7840 // inbounds with zero offset. To guarantee that the result isn't poison, the
7841 // stripped pointer is checked as it has to be pointing into an allocated
7842 // object or be null `null` to ensure `inbounds` getelement pointers with a
7843 // zero offset could not produce poison.
7844 // It can strip off addrspacecast that do not change bit representation as
7845 // well. We believe that such addrspacecast is equivalent to no-op.
7846 auto *StrippedV = V->stripPointerCastsSameRepresentation();
7847 if (isa<AllocaInst>(StrippedV) || isa<GlobalVariable>(StrippedV) ||
7848 isa<Function>(StrippedV) || isa<ConstantPointerNull>(StrippedV))
7849 return true;
7850
7851 auto OpCheck = [&](const Value *V) {
7852 return isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth + 1, Kind);
7853 };
7854
7855 if (auto *Opr = dyn_cast<Operator>(V)) {
7856 // If the value is a freeze instruction, then it can never
7857 // be undef or poison.
7858 if (isa<FreezeInst>(V))
7859 return true;
7860
7861 if (const auto *CB = dyn_cast<CallBase>(V)) {
7862 if (CB->hasRetAttr(Attribute::NoUndef) ||
7863 CB->hasRetAttr(Attribute::Dereferenceable) ||
7864 CB->hasRetAttr(Attribute::DereferenceableOrNull))
7865 return true;
7866 }
7867
7868 if (!::canCreateUndefOrPoison(Opr, Kind,
7869 /*ConsiderFlagsAndMetadata=*/true)) {
7870 if (const auto *PN = dyn_cast<PHINode>(V)) {
7871 unsigned Num = PN->getNumIncomingValues();
7872 bool IsWellDefined = true;
7873 for (unsigned i = 0; i < Num; ++i) {
7874 if (PN == PN->getIncomingValue(i))
7875 continue;
7876 auto *TI = PN->getIncomingBlock(i)->getTerminator();
7877 if (!isGuaranteedNotToBeUndefOrPoison(PN->getIncomingValue(i), AC, TI,
7878 DT, Depth + 1, Kind)) {
7879 IsWellDefined = false;
7880 break;
7881 }
7882 }
7883 if (IsWellDefined)
7884 return true;
7885 } else if (auto *Splat = isa<ShuffleVectorInst>(Opr) ? getSplatValue(Opr)
7886 : nullptr) {
7887 // For splats we only need to check the value being splatted.
7888 if (OpCheck(Splat))
7889 return true;
7890 } else if (all_of(Opr->operands(), OpCheck))
7891 return true;
7892 }
7893 }
7894
7895 if (auto *I = dyn_cast<LoadInst>(V))
7896 if (I->hasMetadata(LLVMContext::MD_noundef) ||
7897 I->hasMetadata(LLVMContext::MD_dereferenceable) ||
7898 I->hasMetadata(LLVMContext::MD_dereferenceable_or_null))
7899 return true;
7900
7902 return true;
7903
7904 // CxtI may be null or a cloned instruction.
7905 if (!CtxI || !CtxI->getParent() || !DT)
7906 return false;
7907
7908 auto *DNode = DT->getNode(CtxI->getParent());
7909 if (!DNode)
7910 // Unreachable block
7911 return false;
7912
7913 // If V is used as a branch condition before reaching CtxI, V cannot be
7914 // undef or poison.
7915 // br V, BB1, BB2
7916 // BB1:
7917 // CtxI ; V cannot be undef or poison here
7918 auto *Dominator = DNode->getIDom();
7919 // This check is purely for compile time reasons: we can skip the IDom walk
7920 // if what we are checking for includes undef and the value is not an integer.
7921 if (!includesUndef(Kind) || V->getType()->isIntegerTy())
7922 while (Dominator) {
7923 auto *TI = Dominator->getBlock()->getTerminatorOrNull();
7924
7925 Value *Cond = nullptr;
7926 if (auto BI = dyn_cast_or_null<CondBrInst>(TI)) {
7927 Cond = BI->getCondition();
7928 } else if (auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
7929 Cond = SI->getCondition();
7930 }
7931
7932 if (Cond) {
7933 if (Cond == V)
7934 return true;
7935 else if (!includesUndef(Kind) && isa<Operator>(Cond)) {
7936 // For poison, we can analyze further
7937 auto *Opr = cast<Operator>(Cond);
7938 if (any_of(Opr->operands(), [V](const Use &U) {
7939 return V == U && propagatesPoison(U);
7940 }))
7941 return true;
7942 }
7943 }
7944
7945 Dominator = Dominator->getIDom();
7946 }
7947
7948 if (AC && getKnowledgeValidInContext(V, {Attribute::NoUndef}, *AC, CtxI, DT))
7949 return true;
7950
7951 return false;
7952}
7953
7955 const Instruction *CtxI,
7956 const DominatorTree *DT,
7957 unsigned Depth) {
7958 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7960}
7961
7963 const Instruction *CtxI,
7964 const DominatorTree *DT, unsigned Depth) {
7965 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7967}
7968
7970 const Instruction *CtxI,
7971 const DominatorTree *DT, unsigned Depth) {
7972 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7974}
7975
7976/// Return true if undefined behavior would provably be executed on the path to
7977/// OnPathTo if Root produced a posion result. Note that this doesn't say
7978/// anything about whether OnPathTo is actually executed or whether Root is
7979/// actually poison. This can be used to assess whether a new use of Root can
7980/// be added at a location which is control equivalent with OnPathTo (such as
7981/// immediately before it) without introducing UB which didn't previously
7982/// exist. Note that a false result conveys no information.
7984 Instruction *OnPathTo,
7985 DominatorTree *DT) {
7986 // Basic approach is to assume Root is poison, propagate poison forward
7987 // through all users we can easily track, and then check whether any of those
7988 // users are provable UB and must execute before out exiting block might
7989 // exit.
7990
7991 // The set of all recursive users we've visited (which are assumed to all be
7992 // poison because of said visit)
7995 Worklist.push_back(Root);
7996 while (!Worklist.empty()) {
7997 const Instruction *I = Worklist.pop_back_val();
7998
7999 // If we know this must trigger UB on a path leading our target.
8000 if (mustTriggerUB(I, KnownPoison) && DT->dominates(I, OnPathTo))
8001 return true;
8002
8003 // If we can't analyze propagation through this instruction, just skip it
8004 // and transitive users. Safe as false is a conservative result.
8005 if (I != Root && !any_of(I->operands(), [&KnownPoison](const Use &U) {
8006 return KnownPoison.contains(U) && propagatesPoison(U);
8007 }))
8008 continue;
8009
8010 if (KnownPoison.insert(I).second)
8011 for (const User *User : I->users())
8012 Worklist.push_back(cast<Instruction>(User));
8013 }
8014
8015 // Might be non-UB, or might have a path we couldn't prove must execute on
8016 // way to exiting bb.
8017 return false;
8018}
8019
8021 const SimplifyQuery &SQ) {
8022 return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1),
8023 Add, SQ);
8024}
8025
8028 const WithCache<const Value *> &RHS,
8029 const SimplifyQuery &SQ) {
8030 return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, SQ);
8031}
8032
8034 // Note: An atomic operation isn't guaranteed to return in a reasonable amount
8035 // of time because it's possible for another thread to interfere with it for an
8036 // arbitrary length of time, but programs aren't allowed to rely on that.
8037
8038 // If there is no successor, then execution can't transfer to it.
8039 if (isa<ReturnInst>(I))
8040 return false;
8042 return false;
8043
8044 // Note: Do not add new checks here; instead, change Instruction::mayThrow or
8045 // Instruction::willReturn.
8046 //
8047 // FIXME: Move this check into Instruction::willReturn.
8048 if (isa<CatchPadInst>(I)) {
8049 switch (classifyEHPersonality(I->getFunction()->getPersonalityFn())) {
8050 default:
8051 // A catchpad may invoke exception object constructors and such, which
8052 // in some languages can be arbitrary code, so be conservative by default.
8053 return false;
8055 // For CoreCLR, it just involves a type test.
8056 return true;
8057 }
8058 }
8059
8060 // An instruction that returns without throwing must transfer control flow
8061 // to a successor.
8062 return !I->mayThrow() && I->willReturn();
8063}
8064
8066 // TODO: This is slightly conservative for invoke instruction since exiting
8067 // via an exception *is* normal control for them.
8068 for (const Instruction &I : *BB)
8070 return false;
8071 return true;
8072}
8073
8080
8083 assert(ScanLimit && "scan limit must be non-zero");
8084 for (const Instruction &I : Range) {
8085 if (--ScanLimit == 0)
8086 return false;
8088 return false;
8089 }
8090 return true;
8091}
8092
8094 const Loop *L) {
8095 // The loop header is guaranteed to be executed for every iteration.
8096 //
8097 // FIXME: Relax this constraint to cover all basic blocks that are
8098 // guaranteed to be executed at every iteration.
8099 if (I->getParent() != L->getHeader()) return false;
8100
8101 for (const Instruction &LI : *L->getHeader()) {
8102 if (&LI == I) return true;
8103 if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;
8104 }
8105 llvm_unreachable("Instruction not contained in its own parent basic block.");
8106}
8107
8109 switch (IID) {
8110 // TODO: Add more intrinsics.
8111 case Intrinsic::sadd_with_overflow:
8112 case Intrinsic::ssub_with_overflow:
8113 case Intrinsic::smul_with_overflow:
8114 case Intrinsic::uadd_with_overflow:
8115 case Intrinsic::usub_with_overflow:
8116 case Intrinsic::umul_with_overflow:
8117 // If an input is a vector containing a poison element, the
8118 // two output vectors (calculated results, overflow bits)'
8119 // corresponding lanes are poison.
8120 return true;
8121 case Intrinsic::ctpop:
8122 case Intrinsic::ctlz:
8123 case Intrinsic::cttz:
8124 case Intrinsic::abs:
8125 case Intrinsic::smax:
8126 case Intrinsic::smin:
8127 case Intrinsic::umax:
8128 case Intrinsic::umin:
8129 case Intrinsic::scmp:
8130 case Intrinsic::is_fpclass:
8131 case Intrinsic::ptrmask:
8132 case Intrinsic::ucmp:
8133 case Intrinsic::bitreverse:
8134 case Intrinsic::bswap:
8135 case Intrinsic::sadd_sat:
8136 case Intrinsic::ssub_sat:
8137 case Intrinsic::sshl_sat:
8138 case Intrinsic::uadd_sat:
8139 case Intrinsic::usub_sat:
8140 case Intrinsic::ushl_sat:
8141 case Intrinsic::smul_fix:
8142 case Intrinsic::smul_fix_sat:
8143 case Intrinsic::umul_fix:
8144 case Intrinsic::umul_fix_sat:
8145 case Intrinsic::pow:
8146 case Intrinsic::powi:
8147 case Intrinsic::sin:
8148 case Intrinsic::sinh:
8149 case Intrinsic::cos:
8150 case Intrinsic::cosh:
8151 case Intrinsic::sincos:
8152 case Intrinsic::sincospi:
8153 case Intrinsic::tan:
8154 case Intrinsic::tanh:
8155 case Intrinsic::asin:
8156 case Intrinsic::acos:
8157 case Intrinsic::atan:
8158 case Intrinsic::atan2:
8159 case Intrinsic::canonicalize:
8160 case Intrinsic::sqrt:
8161 case Intrinsic::exp:
8162 case Intrinsic::exp2:
8163 case Intrinsic::exp10:
8164 case Intrinsic::log:
8165 case Intrinsic::log2:
8166 case Intrinsic::log10:
8167 case Intrinsic::modf:
8168 case Intrinsic::floor:
8169 case Intrinsic::ceil:
8170 case Intrinsic::trunc:
8171 case Intrinsic::rint:
8172 case Intrinsic::nearbyint:
8173 case Intrinsic::round:
8174 case Intrinsic::roundeven:
8175 case Intrinsic::lrint:
8176 case Intrinsic::llrint:
8177 case Intrinsic::fshl:
8178 case Intrinsic::fshr:
8179 return true;
8180 default:
8181 return false;
8182 }
8183}
8184
8185bool llvm::propagatesPoison(const Use &PoisonOp) {
8186 const Operator *I = cast<Operator>(PoisonOp.getUser());
8187 switch (I->getOpcode()) {
8188 case Instruction::Freeze:
8189 case Instruction::PHI:
8190 case Instruction::Invoke:
8191 return false;
8192 case Instruction::Select:
8193 return PoisonOp.getOperandNo() == 0;
8194 case Instruction::Call:
8195 if (auto *II = dyn_cast<IntrinsicInst>(I))
8196 return intrinsicPropagatesPoison(II->getIntrinsicID());
8197 return false;
8198 case Instruction::ICmp:
8199 case Instruction::FCmp:
8200 case Instruction::GetElementPtr:
8201 return true;
8202 default:
8204 return true;
8205
8206 // Be conservative and return false.
8207 return false;
8208 }
8209}
8210
8211/// Enumerates all operands of \p I that are guaranteed to not be undef or
8212/// poison. If the callback \p Handle returns true, stop processing and return
8213/// true. Otherwise, return false.
8214template <typename CallableT>
8216 const CallableT &Handle) {
8217 switch (I->getOpcode()) {
8218 case Instruction::Store:
8219 if (Handle(cast<StoreInst>(I)->getPointerOperand()))
8220 return true;
8221 break;
8222
8223 case Instruction::Load:
8224 if (Handle(cast<LoadInst>(I)->getPointerOperand()))
8225 return true;
8226 break;
8227
8228 // Since dereferenceable attribute imply noundef, atomic operations
8229 // also implicitly have noundef pointers too
8230 case Instruction::AtomicCmpXchg:
8232 return true;
8233 break;
8234
8235 case Instruction::AtomicRMW:
8236 if (Handle(cast<AtomicRMWInst>(I)->getPointerOperand()))
8237 return true;
8238 break;
8239
8240 case Instruction::Call:
8241 case Instruction::Invoke: {
8242 const CallBase *CB = cast<CallBase>(I);
8243 if (CB->isIndirectCall() && Handle(CB->getCalledOperand()))
8244 return true;
8245 for (unsigned i = 0; i < CB->arg_size(); ++i)
8246 if ((CB->paramHasAttr(i, Attribute::NoUndef) ||
8247 CB->paramHasAttr(i, Attribute::Dereferenceable) ||
8248 CB->paramHasAttr(i, Attribute::DereferenceableOrNull)) &&
8249 Handle(CB->getArgOperand(i)))
8250 return true;
8251 break;
8252 }
8253 case Instruction::Ret:
8254 if (I->getFunction()->hasRetAttribute(Attribute::NoUndef) &&
8255 Handle(I->getOperand(0)))
8256 return true;
8257 break;
8258 case Instruction::Switch:
8259 if (Handle(cast<SwitchInst>(I)->getCondition()))
8260 return true;
8261 break;
8262 case Instruction::CondBr:
8263 if (Handle(cast<CondBrInst>(I)->getCondition()))
8264 return true;
8265 break;
8266 default:
8267 break;
8268 }
8269
8270 return false;
8271}
8272
8273/// Enumerates all operands of \p I that are guaranteed to not be poison.
8274template <typename CallableT>
8276 const CallableT &Handle) {
8277 if (handleGuaranteedWellDefinedOps(I, Handle))
8278 return true;
8279 switch (I->getOpcode()) {
8280 // Divisors of these operations are allowed to be partially undef.
8281 case Instruction::UDiv:
8282 case Instruction::SDiv:
8283 case Instruction::URem:
8284 case Instruction::SRem:
8285 return Handle(I->getOperand(1));
8286 default:
8287 return false;
8288 }
8289}
8290
8292 const SmallPtrSetImpl<const Value *> &KnownPoison) {
8294 I, [&](const Value *V) { return KnownPoison.count(V); });
8295}
8296
8298 bool PoisonOnly) {
8299 // We currently only look for uses of values within the same basic
8300 // block, as that makes it easier to guarantee that the uses will be
8301 // executed given that Inst is executed.
8302 //
8303 // FIXME: Expand this to consider uses beyond the same basic block. To do
8304 // this, look out for the distinction between post-dominance and strong
8305 // post-dominance.
8306 const BasicBlock *BB = nullptr;
8308 if (const auto *Inst = dyn_cast<Instruction>(V)) {
8309 BB = Inst->getParent();
8310 Begin = Inst->getIterator();
8311 Begin++;
8312 } else if (const auto *Arg = dyn_cast<Argument>(V)) {
8313 if (Arg->getParent()->isDeclaration())
8314 return false;
8315 BB = &Arg->getParent()->getEntryBlock();
8316 Begin = BB->begin();
8317 } else {
8318 return false;
8319 }
8320
8321 // Limit number of instructions we look at, to avoid scanning through large
8322 // blocks. The current limit is chosen arbitrarily.
8323 unsigned ScanLimit = 32;
8324 BasicBlock::const_iterator End = BB->end();
8325
8326 if (!PoisonOnly) {
8327 // Since undef does not propagate eagerly, be conservative & just check
8328 // whether a value is directly passed to an instruction that must take
8329 // well-defined operands.
8330
8331 for (const auto &I : make_range(Begin, End)) {
8332 if (--ScanLimit == 0)
8333 break;
8334
8335 if (handleGuaranteedWellDefinedOps(&I, [V](const Value *WellDefinedOp) {
8336 return WellDefinedOp == V;
8337 }))
8338 return true;
8339
8341 break;
8342 }
8343 return false;
8344 }
8345
8346 // Set of instructions that we have proved will yield poison if Inst
8347 // does.
8348 SmallPtrSet<const Value *, 16> YieldsPoison;
8350
8351 YieldsPoison.insert(V);
8352 Visited.insert(BB);
8353
8354 while (true) {
8355 for (const auto &I : make_range(Begin, End)) {
8356 if (--ScanLimit == 0)
8357 return false;
8358 if (mustTriggerUB(&I, YieldsPoison))
8359 return true;
8361 return false;
8362
8363 // If an operand is poison and propagates it, mark I as yielding poison.
8364 for (const Use &Op : I.operands()) {
8365 if (YieldsPoison.count(Op) && propagatesPoison(Op)) {
8366 YieldsPoison.insert(&I);
8367 break;
8368 }
8369 }
8370
8371 // Special handling for select, which returns poison if its operand 0 is
8372 // poison (handled in the loop above) *or* if both its true/false operands
8373 // are poison (handled here).
8374 if (I.getOpcode() == Instruction::Select &&
8375 YieldsPoison.count(I.getOperand(1)) &&
8376 YieldsPoison.count(I.getOperand(2))) {
8377 YieldsPoison.insert(&I);
8378 }
8379 }
8380
8381 BB = BB->getSingleSuccessor();
8382 if (!BB || !Visited.insert(BB).second)
8383 break;
8384
8385 Begin = BB->getFirstNonPHIIt();
8386 End = BB->end();
8387 }
8388 return false;
8389}
8390
8392 return ::programUndefinedIfUndefOrPoison(Inst, false);
8393}
8394
8396 return ::programUndefinedIfUndefOrPoison(Inst, true);
8397}
8398
8399static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) {
8400 if (FMF.noNaNs())
8401 return true;
8402
8403 if (auto *C = dyn_cast<ConstantFP>(V))
8404 return !C->isNaN();
8405
8406 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8407 if (!C->getElementType()->isFloatingPointTy())
8408 return false;
8409 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8410 if (C->getElementAsAPFloat(I).isNaN())
8411 return false;
8412 }
8413 return true;
8414 }
8415
8417 return true;
8418
8419 return false;
8420}
8421
8422static bool isKnownNonZero(const Value *V) {
8423 if (auto *C = dyn_cast<ConstantFP>(V))
8424 return !C->isZero();
8425
8426 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8427 if (!C->getElementType()->isFloatingPointTy())
8428 return false;
8429 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8430 if (C->getElementAsAPFloat(I).isZero())
8431 return false;
8432 }
8433 return true;
8434 }
8435
8436 return false;
8437}
8438
8439/// Match clamp pattern for float types without care about NaNs or signed zeros.
8440/// Given non-min/max outer cmp/select from the clamp pattern this
8441/// function recognizes if it can be substitued by a "canonical" min/max
8442/// pattern.
8444 Value *CmpLHS, Value *CmpRHS,
8445 Value *TrueVal, Value *FalseVal,
8446 Value *&LHS, Value *&RHS) {
8447 // Try to match
8448 // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2))
8449 // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2))
8450 // and return description of the outer Max/Min.
8451
8452 // First, check if select has inverse order:
8453 if (CmpRHS == FalseVal) {
8454 std::swap(TrueVal, FalseVal);
8455 Pred = CmpInst::getInversePredicate(Pred);
8456 }
8457
8458 // Assume success now. If there's no match, callers should not use these anyway.
8459 LHS = TrueVal;
8460 RHS = FalseVal;
8461
8462 const APFloat *FC1;
8463 if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite())
8464 return {SPF_UNKNOWN, SPNB_NA, false};
8465
8466 const APFloat *FC2;
8467 switch (Pred) {
8468 case CmpInst::FCMP_OLT:
8469 case CmpInst::FCMP_OLE:
8470 case CmpInst::FCMP_ULT:
8471 case CmpInst::FCMP_ULE:
8472 if (match(FalseVal, m_OrdOrUnordFMin(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8473 *FC1 < *FC2)
8474 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
8475 if (match(FalseVal, m_FMinNum(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8476 *FC1 < *FC2)
8477 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
8478 break;
8479 case CmpInst::FCMP_OGT:
8480 case CmpInst::FCMP_OGE:
8481 case CmpInst::FCMP_UGT:
8482 case CmpInst::FCMP_UGE:
8483 if (match(FalseVal, m_OrdOrUnordFMax(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8484 *FC1 > *FC2)
8485 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
8486 if (match(FalseVal, m_FMaxNum(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8487 *FC1 > *FC2)
8488 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
8489 break;
8490 default:
8491 break;
8492 }
8493
8494 return {SPF_UNKNOWN, SPNB_NA, false};
8495}
8496
8497/// Recognize variations of:
8498/// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
8500 Value *CmpLHS, Value *CmpRHS,
8501 Value *TrueVal, Value *FalseVal) {
8502 // Swap the select operands and predicate to match the patterns below.
8503 if (CmpRHS != TrueVal) {
8504 Pred = ICmpInst::getSwappedPredicate(Pred);
8505 std::swap(TrueVal, FalseVal);
8506 }
8507 const APInt *C1;
8508 if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) {
8509 const APInt *C2;
8510 // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
8511 if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8512 C1->slt(*C2) && Pred == CmpInst::ICMP_SLT)
8513 return {SPF_SMAX, SPNB_NA, false};
8514
8515 // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
8516 if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8517 C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT)
8518 return {SPF_SMIN, SPNB_NA, false};
8519
8520 // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
8521 if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8522 C1->ult(*C2) && Pred == CmpInst::ICMP_ULT)
8523 return {SPF_UMAX, SPNB_NA, false};
8524
8525 // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
8526 if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8527 C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT)
8528 return {SPF_UMIN, SPNB_NA, false};
8529 }
8530 return {SPF_UNKNOWN, SPNB_NA, false};
8531}
8532
8533/// Recognize variations of:
8534/// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c))
8536 Value *CmpLHS, Value *CmpRHS,
8537 Value *TVal, Value *FVal,
8538 unsigned Depth) {
8539 // TODO: Allow FP min/max with nnan/nsz.
8540 assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison");
8541
8542 Value *A = nullptr, *B = nullptr;
8543 SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1);
8544 if (!SelectPatternResult::isMinOrMax(L.Flavor))
8545 return {SPF_UNKNOWN, SPNB_NA, false};
8546
8547 Value *C = nullptr, *D = nullptr;
8548 SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1);
8549 if (L.Flavor != R.Flavor)
8550 return {SPF_UNKNOWN, SPNB_NA, false};
8551
8552 // We have something like: x Pred y ? min(a, b) : min(c, d).
8553 // Try to match the compare to the min/max operations of the select operands.
8554 // First, make sure we have the right compare predicate.
8555 switch (L.Flavor) {
8556 case SPF_SMIN:
8557 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) {
8558 Pred = ICmpInst::getSwappedPredicate(Pred);
8559 std::swap(CmpLHS, CmpRHS);
8560 }
8561 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
8562 break;
8563 return {SPF_UNKNOWN, SPNB_NA, false};
8564 case SPF_SMAX:
8565 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) {
8566 Pred = ICmpInst::getSwappedPredicate(Pred);
8567 std::swap(CmpLHS, CmpRHS);
8568 }
8569 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE)
8570 break;
8571 return {SPF_UNKNOWN, SPNB_NA, false};
8572 case SPF_UMIN:
8573 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
8574 Pred = ICmpInst::getSwappedPredicate(Pred);
8575 std::swap(CmpLHS, CmpRHS);
8576 }
8577 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE)
8578 break;
8579 return {SPF_UNKNOWN, SPNB_NA, false};
8580 case SPF_UMAX:
8581 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
8582 Pred = ICmpInst::getSwappedPredicate(Pred);
8583 std::swap(CmpLHS, CmpRHS);
8584 }
8585 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
8586 break;
8587 return {SPF_UNKNOWN, SPNB_NA, false};
8588 default:
8589 return {SPF_UNKNOWN, SPNB_NA, false};
8590 }
8591
8592 // If there is a common operand in the already matched min/max and the other
8593 // min/max operands match the compare operands (either directly or inverted),
8594 // then this is min/max of the same flavor.
8595
8596 // a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8597 // ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8598 if (D == B) {
8599 if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8600 match(A, m_Not(m_Specific(CmpRHS)))))
8601 return {L.Flavor, SPNB_NA, false};
8602 }
8603 // a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8604 // ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8605 if (C == B) {
8606 if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8607 match(A, m_Not(m_Specific(CmpRHS)))))
8608 return {L.Flavor, SPNB_NA, false};
8609 }
8610 // b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8611 // ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8612 if (D == A) {
8613 if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8614 match(B, m_Not(m_Specific(CmpRHS)))))
8615 return {L.Flavor, SPNB_NA, false};
8616 }
8617 // b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8618 // ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8619 if (C == A) {
8620 if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8621 match(B, m_Not(m_Specific(CmpRHS)))))
8622 return {L.Flavor, SPNB_NA, false};
8623 }
8624
8625 return {SPF_UNKNOWN, SPNB_NA, false};
8626}
8627
8628/// If the input value is the result of a 'not' op, constant integer, or vector
8629/// splat of a constant integer, return the bitwise-not source value.
8630/// TODO: This could be extended to handle non-splat vector integer constants.
8632 Value *NotV;
8633 if (match(V, m_Not(m_Value(NotV))))
8634 return NotV;
8635
8636 const APInt *C;
8637 if (match(V, m_APInt(C)))
8638 return ConstantInt::get(V->getType(), ~(*C));
8639
8640 return nullptr;
8641}
8642
8643/// Match non-obvious integer minimum and maximum sequences.
8645 Value *CmpLHS, Value *CmpRHS,
8646 Value *TrueVal, Value *FalseVal,
8647 Value *&LHS, Value *&RHS,
8648 unsigned Depth) {
8649 // Assume success. If there's no match, callers should not use these anyway.
8650 LHS = TrueVal;
8651 RHS = FalseVal;
8652
8653 SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);
8655 return SPR;
8656
8657 SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth);
8659 return SPR;
8660
8661 // Look through 'not' ops to find disguised min/max.
8662 // (X > Y) ? ~X : ~Y ==> (~X < ~Y) ? ~X : ~Y ==> MIN(~X, ~Y)
8663 // (X < Y) ? ~X : ~Y ==> (~X > ~Y) ? ~X : ~Y ==> MAX(~X, ~Y)
8664 if (CmpLHS == getNotValue(TrueVal) && CmpRHS == getNotValue(FalseVal)) {
8665 switch (Pred) {
8666 case CmpInst::ICMP_SGT: return {SPF_SMIN, SPNB_NA, false};
8667 case CmpInst::ICMP_SLT: return {SPF_SMAX, SPNB_NA, false};
8668 case CmpInst::ICMP_UGT: return {SPF_UMIN, SPNB_NA, false};
8669 case CmpInst::ICMP_ULT: return {SPF_UMAX, SPNB_NA, false};
8670 default: break;
8671 }
8672 }
8673
8674 // (X > Y) ? ~Y : ~X ==> (~X < ~Y) ? ~Y : ~X ==> MAX(~Y, ~X)
8675 // (X < Y) ? ~Y : ~X ==> (~X > ~Y) ? ~Y : ~X ==> MIN(~Y, ~X)
8676 if (CmpLHS == getNotValue(FalseVal) && CmpRHS == getNotValue(TrueVal)) {
8677 switch (Pred) {
8678 case CmpInst::ICMP_SGT: return {SPF_SMAX, SPNB_NA, false};
8679 case CmpInst::ICMP_SLT: return {SPF_SMIN, SPNB_NA, false};
8680 case CmpInst::ICMP_UGT: return {SPF_UMAX, SPNB_NA, false};
8681 case CmpInst::ICMP_ULT: return {SPF_UMIN, SPNB_NA, false};
8682 default: break;
8683 }
8684 }
8685
8686 if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT)
8687 return {SPF_UNKNOWN, SPNB_NA, false};
8688
8689 const APInt *C1;
8690 if (!match(CmpRHS, m_APInt(C1)))
8691 return {SPF_UNKNOWN, SPNB_NA, false};
8692
8693 // An unsigned min/max can be written with a signed compare.
8694 const APInt *C2;
8695 if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) ||
8696 (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) {
8697 // Is the sign bit set?
8698 // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX
8699 // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN
8700 if (Pred == CmpInst::ICMP_SLT && C1->isZero() && C2->isMaxSignedValue())
8701 return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8702
8703 // Is the sign bit clear?
8704 // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX
8705 // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN
8706 if (Pred == CmpInst::ICMP_SGT && C1->isAllOnes() && C2->isMinSignedValue())
8707 return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8708 }
8709
8710 return {SPF_UNKNOWN, SPNB_NA, false};
8711}
8712
8713bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW,
8714 bool AllowPoison) {
8715 assert(X && Y && "Invalid operand");
8716
8717 auto IsNegationOf = [&](const Value *X, const Value *Y) {
8718 if (!match(X, m_Neg(m_Specific(Y))))
8719 return false;
8720
8721 auto *BO = cast<BinaryOperator>(X);
8722 if (NeedNSW && !BO->hasNoSignedWrap())
8723 return false;
8724
8725 auto *Zero = cast<Constant>(BO->getOperand(0));
8726 if (!AllowPoison && !Zero->isNullValue())
8727 return false;
8728
8729 return true;
8730 };
8731
8732 // X = -Y or Y = -X
8733 if (IsNegationOf(X, Y) || IsNegationOf(Y, X))
8734 return true;
8735
8736 // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A)
8737 Value *A, *B;
8738 return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) &&
8739 match(Y, m_Sub(m_Specific(B), m_Specific(A))))) ||
8740 (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) &&
8742}
8743
8744bool llvm::isKnownInversion(const Value *X, const Value *Y) {
8745 // Handle X = icmp pred A, B, Y = icmp pred A, C.
8746 Value *A, *B, *C;
8747 CmpPredicate Pred1, Pred2;
8748 if (!match(X, m_ICmp(Pred1, m_Value(A), m_Value(B))) ||
8749 !match(Y, m_c_ICmp(Pred2, m_Specific(A), m_Value(C))))
8750 return false;
8751
8752 // They must both have samesign flag or not.
8753 if (Pred1.hasSameSign() != Pred2.hasSameSign())
8754 return false;
8755
8756 if (B == C)
8757 return Pred1 == ICmpInst::getInversePredicate(Pred2);
8758
8759 // Try to infer the relationship from constant ranges.
8760 const APInt *RHSC1, *RHSC2;
8761 if (!match(B, m_APInt(RHSC1)) || !match(C, m_APInt(RHSC2)))
8762 return false;
8763
8764 // Sign bits of two RHSCs should match.
8765 if (Pred1.hasSameSign() && RHSC1->isNonNegative() != RHSC2->isNonNegative())
8766 return false;
8767
8768 const auto CR1 = ConstantRange::makeExactICmpRegion(Pred1, *RHSC1);
8769 const auto CR2 = ConstantRange::makeExactICmpRegion(Pred2, *RHSC2);
8770
8771 return CR1.inverse() == CR2;
8772}
8773
8775 SelectPatternNaNBehavior NaNBehavior,
8776 bool Ordered) {
8777 switch (Pred) {
8778 default:
8779 return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.
8780 case ICmpInst::ICMP_UGT:
8781 case ICmpInst::ICMP_UGE:
8782 return {SPF_UMAX, SPNB_NA, false};
8783 case ICmpInst::ICMP_SGT:
8784 case ICmpInst::ICMP_SGE:
8785 return {SPF_SMAX, SPNB_NA, false};
8786 case ICmpInst::ICMP_ULT:
8787 case ICmpInst::ICMP_ULE:
8788 return {SPF_UMIN, SPNB_NA, false};
8789 case ICmpInst::ICMP_SLT:
8790 case ICmpInst::ICMP_SLE:
8791 return {SPF_SMIN, SPNB_NA, false};
8792 case FCmpInst::FCMP_UGT:
8793 case FCmpInst::FCMP_UGE:
8794 case FCmpInst::FCMP_OGT:
8795 case FCmpInst::FCMP_OGE:
8796 return {SPF_FMAXNUM, NaNBehavior, Ordered};
8797 case FCmpInst::FCMP_ULT:
8798 case FCmpInst::FCMP_ULE:
8799 case FCmpInst::FCMP_OLT:
8800 case FCmpInst::FCMP_OLE:
8801 return {SPF_FMINNUM, NaNBehavior, Ordered};
8802 }
8803}
8804
8805std::optional<std::pair<CmpPredicate, Constant *>>
8808 "Only for relational integer predicates.");
8809 if (isa<UndefValue>(C))
8810 return std::nullopt;
8811
8812 Type *Type = C->getType();
8813 bool IsSigned = ICmpInst::isSigned(Pred);
8814
8816 bool WillIncrement =
8817 UnsignedPred == ICmpInst::ICMP_ULE || UnsignedPred == ICmpInst::ICMP_UGT;
8818
8819 // Check if the constant operand can be safely incremented/decremented
8820 // without overflowing/underflowing.
8821 auto ConstantIsOk = [WillIncrement, IsSigned](ConstantInt *C) {
8822 return WillIncrement ? !C->isMaxValue(IsSigned) : !C->isMinValue(IsSigned);
8823 };
8824
8825 Constant *SafeReplacementConstant = nullptr;
8826 if (auto *CI = dyn_cast<ConstantInt>(C)) {
8827 // Bail out if the constant can't be safely incremented/decremented.
8828 if (!ConstantIsOk(CI))
8829 return std::nullopt;
8830 } else if (auto *FVTy = dyn_cast<FixedVectorType>(Type)) {
8831 unsigned NumElts = FVTy->getNumElements();
8832 for (unsigned i = 0; i != NumElts; ++i) {
8833 Constant *Elt = C->getAggregateElement(i);
8834 if (!Elt)
8835 return std::nullopt;
8836
8837 if (isa<UndefValue>(Elt))
8838 continue;
8839
8840 // Bail out if we can't determine if this constant is min/max or if we
8841 // know that this constant is min/max.
8842 auto *CI = dyn_cast<ConstantInt>(Elt);
8843 if (!CI || !ConstantIsOk(CI))
8844 return std::nullopt;
8845
8846 if (!SafeReplacementConstant)
8847 SafeReplacementConstant = CI;
8848 }
8849 } else if (isa<VectorType>(C->getType())) {
8850 // Handle scalable splat
8851 Value *SplatC = C->getSplatValue();
8852 auto *CI = dyn_cast_or_null<ConstantInt>(SplatC);
8853 // Bail out if the constant can't be safely incremented/decremented.
8854 if (!CI || !ConstantIsOk(CI))
8855 return std::nullopt;
8856 } else {
8857 // ConstantExpr?
8858 return std::nullopt;
8859 }
8860
8861 // It may not be safe to change a compare predicate in the presence of
8862 // undefined elements, so replace those elements with the first safe constant
8863 // that we found.
8864 // TODO: in case of poison, it is safe; let's replace undefs only.
8865 if (C->containsUndefOrPoisonElement()) {
8866 assert(SafeReplacementConstant && "Replacement constant not set");
8867 C = Constant::replaceUndefsWith(C, SafeReplacementConstant);
8868 }
8869
8871
8872 // Increment or decrement the constant.
8873 Constant *OneOrNegOne = ConstantInt::get(Type, WillIncrement ? 1 : -1, true);
8874 Constant *NewC = ConstantExpr::getAdd(C, OneOrNegOne);
8875
8876 return std::make_pair(NewPred, NewC);
8877}
8878
8880 FastMathFlags FMF,
8881 Value *CmpLHS, Value *CmpRHS,
8882 Value *TrueVal, Value *FalseVal,
8883 Value *&LHS, Value *&RHS,
8884 unsigned Depth) {
8885 bool HasMismatchedZeros = false;
8886 if (CmpInst::isFPPredicate(Pred)) {
8887 // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one
8888 // 0.0 operand, set the compare's 0.0 operands to that same value for the
8889 // purpose of identifying min/max. Disregard vector constants with undefined
8890 // elements because those can not be back-propagated for analysis.
8891 Value *OutputZeroVal = nullptr;
8892 if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) &&
8893 !cast<Constant>(TrueVal)->containsUndefOrPoisonElement())
8894 OutputZeroVal = TrueVal;
8895 else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) &&
8896 !cast<Constant>(FalseVal)->containsUndefOrPoisonElement())
8897 OutputZeroVal = FalseVal;
8898
8899 if (OutputZeroVal) {
8900 if (match(CmpLHS, m_AnyZeroFP()) && CmpLHS != OutputZeroVal) {
8901 HasMismatchedZeros = true;
8902 CmpLHS = OutputZeroVal;
8903 }
8904 if (match(CmpRHS, m_AnyZeroFP()) && CmpRHS != OutputZeroVal) {
8905 HasMismatchedZeros = true;
8906 CmpRHS = OutputZeroVal;
8907 }
8908 }
8909 }
8910
8911 LHS = CmpLHS;
8912 RHS = CmpRHS;
8913
8914 // Signed zero may return inconsistent results between implementations.
8915 // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0
8916 // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)
8917 // Therefore, we behave conservatively and only proceed if at least one of the
8918 // operands is known to not be zero or if we don't care about signed zero.
8919 switch (Pred) {
8920 default: break;
8923 if (!HasMismatchedZeros)
8924 break;
8925 [[fallthrough]];
8928 if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8929 !isKnownNonZero(CmpRHS))
8930 return {SPF_UNKNOWN, SPNB_NA, false};
8931 }
8932
8933 SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
8934 bool Ordered = false;
8935
8936 // When given one NaN and one non-NaN input:
8937 // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
8938 // - A simple C99 (a < b ? a : b) construction will return 'b' (as the
8939 // ordered comparison fails), which could be NaN or non-NaN.
8940 // so here we discover exactly what NaN behavior is required/accepted.
8941 if (CmpInst::isFPPredicate(Pred)) {
8942 bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
8943 bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
8944
8945 if (LHSSafe && RHSSafe) {
8946 // Both operands are known non-NaN.
8947 NaNBehavior = SPNB_RETURNS_ANY;
8948 Ordered = CmpInst::isOrdered(Pred);
8949 } else if (CmpInst::isOrdered(Pred)) {
8950 // An ordered comparison will return false when given a NaN, so it
8951 // returns the RHS.
8952 Ordered = true;
8953 if (LHSSafe)
8954 // LHS is non-NaN, so if RHS is NaN then NaN will be returned.
8955 NaNBehavior = SPNB_RETURNS_NAN;
8956 else if (RHSSafe)
8957 NaNBehavior = SPNB_RETURNS_OTHER;
8958 else
8959 // Completely unsafe.
8960 return {SPF_UNKNOWN, SPNB_NA, false};
8961 } else {
8962 Ordered = false;
8963 // An unordered comparison will return true when given a NaN, so it
8964 // returns the LHS.
8965 if (LHSSafe)
8966 // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
8967 NaNBehavior = SPNB_RETURNS_OTHER;
8968 else if (RHSSafe)
8969 NaNBehavior = SPNB_RETURNS_NAN;
8970 else
8971 // Completely unsafe.
8972 return {SPF_UNKNOWN, SPNB_NA, false};
8973 }
8974 }
8975
8976 if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
8977 std::swap(CmpLHS, CmpRHS);
8978 Pred = CmpInst::getSwappedPredicate(Pred);
8979 if (NaNBehavior == SPNB_RETURNS_NAN)
8980 NaNBehavior = SPNB_RETURNS_OTHER;
8981 else if (NaNBehavior == SPNB_RETURNS_OTHER)
8982 NaNBehavior = SPNB_RETURNS_NAN;
8983 Ordered = !Ordered;
8984 }
8985
8986 // ([if]cmp X, Y) ? X : Y
8987 if (TrueVal == CmpLHS && FalseVal == CmpRHS)
8988 return getSelectPattern(Pred, NaNBehavior, Ordered);
8989
8990 if (isKnownNegation(TrueVal, FalseVal)) {
8991 // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can
8992 // match against either LHS or sext(LHS).
8993 auto MaybeSExtCmpLHS =
8994 m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS)));
8995 auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes());
8996 auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One());
8997 if (match(TrueVal, MaybeSExtCmpLHS)) {
8998 // Set the return values. If the compare uses the negated value (-X >s 0),
8999 // swap the return values because the negated value is always 'RHS'.
9000 LHS = TrueVal;
9001 RHS = FalseVal;
9002 if (match(CmpLHS, m_Neg(m_Specific(FalseVal))))
9003 std::swap(LHS, RHS);
9004
9005 // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X)
9006 // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X)
9007 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
9008 return {SPF_ABS, SPNB_NA, false};
9009
9010 // (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X)
9011 if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne))
9012 return {SPF_ABS, SPNB_NA, false};
9013
9014 // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X)
9015 // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X)
9016 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
9017 return {SPF_NABS, SPNB_NA, false};
9018 }
9019 else if (match(FalseVal, MaybeSExtCmpLHS)) {
9020 // Set the return values. If the compare uses the negated value (-X >s 0),
9021 // swap the return values because the negated value is always 'RHS'.
9022 LHS = FalseVal;
9023 RHS = TrueVal;
9024 if (match(CmpLHS, m_Neg(m_Specific(TrueVal))))
9025 std::swap(LHS, RHS);
9026
9027 // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X)
9028 // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X)
9029 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
9030 return {SPF_NABS, SPNB_NA, false};
9031
9032 // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X)
9033 // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X)
9034 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
9035 return {SPF_ABS, SPNB_NA, false};
9036 }
9037 }
9038
9039 if (CmpInst::isIntPredicate(Pred))
9040 return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth);
9041
9042 // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar
9043 // may return either -0.0 or 0.0, so fcmp/select pair has stricter
9044 // semantics than minNum. Be conservative in such case.
9045 if (NaNBehavior != SPNB_RETURNS_ANY ||
9046 (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
9047 !isKnownNonZero(CmpRHS)))
9048 return {SPF_UNKNOWN, SPNB_NA, false};
9049
9050 return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
9051}
9052
9054 Instruction::CastOps *CastOp) {
9055 const DataLayout &DL = CmpI->getDataLayout();
9056
9057 Constant *CastedTo = nullptr;
9058 switch (*CastOp) {
9059 case Instruction::ZExt:
9060 if (CmpI->isUnsigned())
9061 CastedTo = ConstantExpr::getTrunc(C, SrcTy);
9062 break;
9063 case Instruction::SExt:
9064 if (CmpI->isSigned())
9065 CastedTo = ConstantExpr::getTrunc(C, SrcTy, true);
9066 break;
9067 case Instruction::Trunc:
9068 Constant *CmpConst;
9069 if (match(CmpI->getOperand(1), m_Constant(CmpConst)) &&
9070 CmpConst->getType() == SrcTy) {
9071 // Here we have the following case:
9072 //
9073 // %cond = cmp iN %x, CmpConst
9074 // %tr = trunc iN %x to iK
9075 // %narrowsel = select i1 %cond, iK %t, iK C
9076 //
9077 // We can always move trunc after select operation:
9078 //
9079 // %cond = cmp iN %x, CmpConst
9080 // %widesel = select i1 %cond, iN %x, iN CmpConst
9081 // %tr = trunc iN %widesel to iK
9082 //
9083 // Note that C could be extended in any way because we don't care about
9084 // upper bits after truncation. It can't be abs pattern, because it would
9085 // look like:
9086 //
9087 // select i1 %cond, x, -x.
9088 //
9089 // So only min/max pattern could be matched. Such match requires widened C
9090 // == CmpConst. That is why set widened C = CmpConst, condition trunc
9091 // CmpConst == C is checked below.
9092 CastedTo = CmpConst;
9093 } else {
9094 unsigned ExtOp = CmpI->isSigned() ? Instruction::SExt : Instruction::ZExt;
9095 CastedTo = ConstantFoldCastOperand(ExtOp, C, SrcTy, DL);
9096 }
9097 break;
9098 case Instruction::FPTrunc:
9099 CastedTo = ConstantFoldCastOperand(Instruction::FPExt, C, SrcTy, DL);
9100 break;
9101 case Instruction::FPExt:
9102 CastedTo = ConstantFoldCastOperand(Instruction::FPTrunc, C, SrcTy, DL);
9103 break;
9104 case Instruction::FPToUI:
9105 CastedTo = ConstantFoldCastOperand(Instruction::UIToFP, C, SrcTy, DL);
9106 break;
9107 case Instruction::FPToSI:
9108 CastedTo = ConstantFoldCastOperand(Instruction::SIToFP, C, SrcTy, DL);
9109 break;
9110 case Instruction::UIToFP:
9111 CastedTo = ConstantFoldCastOperand(Instruction::FPToUI, C, SrcTy, DL);
9112 break;
9113 case Instruction::SIToFP:
9114 CastedTo = ConstantFoldCastOperand(Instruction::FPToSI, C, SrcTy, DL);
9115 break;
9116 default:
9117 break;
9118 }
9119
9120 if (!CastedTo)
9121 return nullptr;
9122
9123 // Make sure the cast doesn't lose any information.
9124 Constant *CastedBack =
9125 ConstantFoldCastOperand(*CastOp, CastedTo, C->getType(), DL);
9126 if (CastedBack && CastedBack != C)
9127 return nullptr;
9128
9129 return CastedTo;
9130}
9131
9132/// Helps to match a select pattern in case of a type mismatch.
9133///
9134/// The function processes the case when type of true and false values of a
9135/// select instruction differs from type of the cmp instruction operands because
9136/// of a cast instruction. The function checks if it is legal to move the cast
9137/// operation after "select". If yes, it returns the new second value of
9138/// "select" (with the assumption that cast is moved):
9139/// 1. As operand of cast instruction when both values of "select" are same cast
9140/// instructions.
9141/// 2. As restored constant (by applying reverse cast operation) when the first
9142/// value of the "select" is a cast operation and the second value is a
9143/// constant. It is implemented in lookThroughCastConst().
9144/// 3. As one operand is cast instruction and the other is not. The operands in
9145/// sel(cmp) are in different type integer.
9146/// NOTE: We return only the new second value because the first value could be
9147/// accessed as operand of cast instruction.
9148static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2,
9149 Instruction::CastOps *CastOp) {
9150 auto *Cast1 = dyn_cast<CastInst>(V1);
9151 if (!Cast1)
9152 return nullptr;
9153
9154 *CastOp = Cast1->getOpcode();
9155 Type *SrcTy = Cast1->getSrcTy();
9156 if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
9157 // If V1 and V2 are both the same cast from the same type, look through V1.
9158 if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
9159 return Cast2->getOperand(0);
9160 return nullptr;
9161 }
9162
9163 auto *C = dyn_cast<Constant>(V2);
9164 if (C)
9165 return lookThroughCastConst(CmpI, SrcTy, C, CastOp);
9166
9167 Value *CastedTo = nullptr;
9168 if (*CastOp == Instruction::Trunc) {
9169 if (match(CmpI->getOperand(1), m_ZExtOrSExt(m_Specific(V2)))) {
9170 // Here we have the following case:
9171 // %y_ext = sext iK %y to iN
9172 // %cond = cmp iN %x, %y_ext
9173 // %tr = trunc iN %x to iK
9174 // %narrowsel = select i1 %cond, iK %tr, iK %y
9175 //
9176 // We can always move trunc after select operation:
9177 // %y_ext = sext iK %y to iN
9178 // %cond = cmp iN %x, %y_ext
9179 // %widesel = select i1 %cond, iN %x, iN %y_ext
9180 // %tr = trunc iN %widesel to iK
9181 assert(V2->getType() == Cast1->getType() &&
9182 "V2 and Cast1 should be the same type.");
9183 CastedTo = CmpI->getOperand(1);
9184 }
9185 }
9186
9187 return CastedTo;
9188}
9190 Instruction::CastOps *CastOp,
9191 unsigned Depth) {
9193 return {SPF_UNKNOWN, SPNB_NA, false};
9194
9196 if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
9197
9198 CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
9199 if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};
9200
9201 Value *TrueVal = SI->getTrueValue();
9202 Value *FalseVal = SI->getFalseValue();
9203
9204 return llvm::matchDecomposedSelectPattern(CmpI, TrueVal, FalseVal, LHS, RHS,
9205 SI->getFastMathFlagsOrNone(),
9206 CastOp, Depth);
9207}
9208
9210 CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS,
9211 FastMathFlags FMF, Instruction::CastOps *CastOp, unsigned Depth) {
9212 CmpInst::Predicate Pred = CmpI->getPredicate();
9213 Value *CmpLHS = CmpI->getOperand(0);
9214 Value *CmpRHS = CmpI->getOperand(1);
9215 if (isa<FPMathOperator>(CmpI) && CmpI->hasNoNaNs())
9216 FMF.setNoNaNs();
9217
9218 // Bail out early.
9219 if (CmpI->isEquality())
9220 return {SPF_UNKNOWN, SPNB_NA, false};
9221
9222 // Deal with type mismatches.
9223 if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
9224 if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) {
9225 // If this is a potential fmin/fmax with a cast to integer, then ignore
9226 // -0.0 because there is no corresponding integer value.
9227 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9228 FMF.setNoSignedZeros();
9229 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9230 cast<CastInst>(TrueVal)->getOperand(0), C,
9231 LHS, RHS, Depth);
9232 }
9233 if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) {
9234 // If this is a potential fmin/fmax with a cast to integer, then ignore
9235 // -0.0 because there is no corresponding integer value.
9236 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9237 FMF.setNoSignedZeros();
9238 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9239 C, cast<CastInst>(FalseVal)->getOperand(0),
9240 LHS, RHS, Depth);
9241 }
9242 }
9243 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
9244 LHS, RHS, Depth);
9245}
9246
9248 if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT;
9249 if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT;
9250 if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT;
9251 if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT;
9252 if (SPF == SPF_FMINNUM)
9253 return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT;
9254 if (SPF == SPF_FMAXNUM)
9255 return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT;
9256 llvm_unreachable("unhandled!");
9257}
9258
9260 switch (SPF) {
9262 return Intrinsic::umin;
9264 return Intrinsic::umax;
9266 return Intrinsic::smin;
9268 return Intrinsic::smax;
9269 default:
9270 llvm_unreachable("Unexpected SPF");
9271 }
9272}
9273
9275 if (SPF == SPF_SMIN) return SPF_SMAX;
9276 if (SPF == SPF_UMIN) return SPF_UMAX;
9277 if (SPF == SPF_SMAX) return SPF_SMIN;
9278 if (SPF == SPF_UMAX) return SPF_UMIN;
9279 llvm_unreachable("unhandled!");
9280}
9281
9283 switch (MinMaxID) {
9284 case Intrinsic::smax: return Intrinsic::smin;
9285 case Intrinsic::smin: return Intrinsic::smax;
9286 case Intrinsic::umax: return Intrinsic::umin;
9287 case Intrinsic::umin: return Intrinsic::umax;
9288 // Please note that next four intrinsics may produce the same result for
9289 // original and inverted case even if X != Y due to NaN is handled specially.
9290 case Intrinsic::maximum: return Intrinsic::minimum;
9291 case Intrinsic::minimum: return Intrinsic::maximum;
9292 case Intrinsic::maxnum: return Intrinsic::minnum;
9293 case Intrinsic::minnum: return Intrinsic::maxnum;
9294 case Intrinsic::maximumnum:
9295 return Intrinsic::minimumnum;
9296 case Intrinsic::minimumnum:
9297 return Intrinsic::maximumnum;
9298 default: llvm_unreachable("Unexpected intrinsic");
9299 }
9300}
9301
9303 switch (SPF) {
9306 case SPF_UMAX: return APInt::getMaxValue(BitWidth);
9307 case SPF_UMIN: return APInt::getMinValue(BitWidth);
9308 default: llvm_unreachable("Unexpected flavor");
9309 }
9310}
9311
9312std::pair<Intrinsic::ID, bool>
9314 // Check if VL contains select instructions that can be folded into a min/max
9315 // vector intrinsic and return the intrinsic if it is possible.
9316 // TODO: Support floating point min/max.
9317 bool AllCmpSingleUse = true;
9318 SelectPatternResult SelectPattern;
9319 SelectPattern.Flavor = SPF_UNKNOWN;
9320 if (all_of(VL, [&SelectPattern, &AllCmpSingleUse](Value *I) {
9321 Value *LHS, *RHS;
9322 auto CurrentPattern = matchSelectPattern(I, LHS, RHS);
9323 if (!SelectPatternResult::isMinOrMax(CurrentPattern.Flavor))
9324 return false;
9325 if (SelectPattern.Flavor != SPF_UNKNOWN &&
9326 SelectPattern.Flavor != CurrentPattern.Flavor)
9327 return false;
9328 SelectPattern = CurrentPattern;
9329 AllCmpSingleUse &=
9331 return true;
9332 })) {
9333 switch (SelectPattern.Flavor) {
9334 case SPF_SMIN:
9335 return {Intrinsic::smin, AllCmpSingleUse};
9336 case SPF_UMIN:
9337 return {Intrinsic::umin, AllCmpSingleUse};
9338 case SPF_SMAX:
9339 return {Intrinsic::smax, AllCmpSingleUse};
9340 case SPF_UMAX:
9341 return {Intrinsic::umax, AllCmpSingleUse};
9342 case SPF_FMAXNUM:
9343 return {Intrinsic::maxnum, AllCmpSingleUse};
9344 case SPF_FMINNUM:
9345 return {Intrinsic::minnum, AllCmpSingleUse};
9346 default:
9347 llvm_unreachable("unexpected select pattern flavor");
9348 }
9349 }
9350 return {Intrinsic::not_intrinsic, false};
9351}
9352
9353template <typename InstTy>
9354static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst,
9355 Value *&Init, Value *&OtherOp) {
9356 // Handle the case of a simple two-predecessor recurrence PHI.
9357 // There's a lot more that could theoretically be done here, but
9358 // this is sufficient to catch some interesting cases.
9359 // TODO: Expand list -- gep, uadd.sat etc.
9360 if (PN->getNumIncomingValues() != 2)
9361 return false;
9362
9363 for (unsigned I = 0; I != 2; ++I) {
9364 if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
9365 Operation && Operation->getNumOperands() >= 2) {
9366 Value *LHS = Operation->getOperand(0);
9367 Value *RHS = Operation->getOperand(1);
9368 if (LHS != PN && RHS != PN)
9369 continue;
9370
9371 Inst = Operation;
9372 Init = PN->getIncomingValue(!I);
9373 OtherOp = (LHS == PN) ? RHS : LHS;
9374 return true;
9375 }
9376 }
9377 return false;
9378}
9379
9380template <typename InstTy>
9381static bool matchThreeInputRecurrence(const PHINode *PN, InstTy *&Inst,
9382 Value *&Init, Value *&OtherOp0,
9383 Value *&OtherOp1) {
9384 if (PN->getNumIncomingValues() != 2)
9385 return false;
9386
9387 for (unsigned I = 0; I != 2; ++I) {
9388 if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
9389 Operation && Operation->getNumOperands() >= 3) {
9390 Value *Op0 = Operation->getOperand(0);
9391 Value *Op1 = Operation->getOperand(1);
9392 Value *Op2 = Operation->getOperand(2);
9393
9394 if (Op0 != PN && Op1 != PN && Op2 != PN)
9395 continue;
9396
9397 Inst = Operation;
9398 Init = PN->getIncomingValue(!I);
9399 if (Op0 == PN) {
9400 OtherOp0 = Op1;
9401 OtherOp1 = Op2;
9402 } else if (Op1 == PN) {
9403 OtherOp0 = Op0;
9404 OtherOp1 = Op2;
9405 } else {
9406 OtherOp0 = Op0;
9407 OtherOp1 = Op1;
9408 }
9409 return true;
9410 }
9411 }
9412 return false;
9413}
9415 Value *&Start, Value *&Step) {
9416 // We try to match a recurrence of the form:
9417 // %iv = [Start, %entry], [%iv.next, %backedge]
9418 // %iv.next = binop %iv, Step
9419 // Or:
9420 // %iv = [Start, %entry], [%iv.next, %backedge]
9421 // %iv.next = binop Step, %iv
9422 return matchTwoInputRecurrence(P, BO, Start, Step);
9423}
9424
9426 Value *&Start, Value *&Step) {
9427 BinaryOperator *BO = nullptr;
9428 P = dyn_cast<PHINode>(I->getOperand(0));
9429 if (!P)
9430 P = dyn_cast<PHINode>(I->getOperand(1));
9431 return P && matchSimpleRecurrence(P, BO, Start, Step) && BO == I;
9432}
9433
9435 PHINode *&P, Value *&Init,
9436 Value *&OtherOp) {
9437 // Binary intrinsics only supported for now.
9438 if (I->arg_size() != 2 || I->getType() != I->getArgOperand(0)->getType() ||
9439 I->getType() != I->getArgOperand(1)->getType())
9440 return false;
9441
9442 IntrinsicInst *II = nullptr;
9443 P = dyn_cast<PHINode>(I->getArgOperand(0));
9444 if (!P)
9445 P = dyn_cast<PHINode>(I->getArgOperand(1));
9446
9447 return P && matchTwoInputRecurrence(P, II, Init, OtherOp) && II == I;
9448}
9449
9451 PHINode *&P, Value *&Init,
9452 Value *&OtherOp0,
9453 Value *&OtherOp1) {
9454 if (I->arg_size() != 3 || I->getType() != I->getArgOperand(0)->getType() ||
9455 I->getType() != I->getArgOperand(1)->getType() ||
9456 I->getType() != I->getArgOperand(2)->getType())
9457 return false;
9458 IntrinsicInst *II = nullptr;
9459 P = dyn_cast<PHINode>(I->getArgOperand(0));
9460 if (!P) {
9461 P = dyn_cast<PHINode>(I->getArgOperand(1));
9462 if (!P)
9463 P = dyn_cast<PHINode>(I->getArgOperand(2));
9464 }
9465 return P && matchThreeInputRecurrence(P, II, Init, OtherOp0, OtherOp1) &&
9466 II == I;
9467}
9468
9469/// Return true if "icmp Pred LHS RHS" is always true.
9471 const Value *RHS) {
9472 if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS)
9473 return true;
9474
9475 switch (Pred) {
9476 default:
9477 return false;
9478
9479 case CmpInst::ICMP_SLE: {
9480 const APInt *C;
9481
9482 // LHS s<= LHS +_{nsw} C if C >= 0
9483 // LHS s<= LHS | C if C >= 0
9484 if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C))) ||
9486 return !C->isNegative();
9487
9488 // LHS s<= smax(LHS, V) for any V
9490 return true;
9491
9492 // smin(RHS, V) s<= RHS for any V
9494 return true;
9495
9496 // Match A to (X +_{nsw} CA) and B to (X +_{nsw} CB)
9497 const Value *X;
9498 const APInt *CLHS, *CRHS;
9499 if (match(LHS, m_NSWAddLike(m_Value(X), m_APInt(CLHS))) &&
9501 return CLHS->sle(*CRHS);
9502
9503 return false;
9504 }
9505
9506 case CmpInst::ICMP_ULE: {
9507 // LHS u<= LHS +_{nuw} V for any V
9508 if (match(RHS, m_c_Add(m_Specific(LHS), m_Value())) &&
9510 return true;
9511
9512 // LHS u<= LHS | V for any V
9513 if (match(RHS, m_c_Or(m_Specific(LHS), m_Value())))
9514 return true;
9515
9516 // LHS u<= umax(LHS, V) for any V
9518 return true;
9519
9520 // RHS >> V u<= RHS for any V
9521 if (match(LHS, m_LShr(m_Specific(RHS), m_Value())))
9522 return true;
9523
9524 // RHS u/ C_ugt_1 u<= RHS
9525 const APInt *C;
9526 if (match(LHS, m_UDiv(m_Specific(RHS), m_APInt(C))) && C->ugt(1))
9527 return true;
9528
9529 // RHS & V u<= RHS for any V
9531 return true;
9532
9533 // umin(RHS, V) u<= RHS for any V
9535 return true;
9536
9537 // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
9538 const Value *X;
9539 const APInt *CLHS, *CRHS;
9540 if (match(LHS, m_NUWAddLike(m_Value(X), m_APInt(CLHS))) &&
9542 return CLHS->ule(*CRHS);
9543
9544 return false;
9545 }
9546 }
9547}
9548
9549/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
9550/// ALHS ARHS" is true. Otherwise, return std::nullopt.
9551static std::optional<bool>
9553 const Value *ARHS, const Value *BLHS, const Value *BRHS) {
9554 switch (Pred) {
9555 default:
9556 return std::nullopt;
9557
9558 case CmpInst::ICMP_SLT:
9559 case CmpInst::ICMP_SLE:
9560 if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS) &&
9562 return true;
9563 return std::nullopt;
9564
9565 case CmpInst::ICMP_SGT:
9566 case CmpInst::ICMP_SGE:
9567 if (isTruePredicate(CmpInst::ICMP_SLE, ALHS, BLHS) &&
9569 return true;
9570 return std::nullopt;
9571
9572 case CmpInst::ICMP_ULT:
9573 case CmpInst::ICMP_ULE:
9574 if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS) &&
9576 return true;
9577 return std::nullopt;
9578
9579 case CmpInst::ICMP_UGT:
9580 case CmpInst::ICMP_UGE:
9581 if (isTruePredicate(CmpInst::ICMP_ULE, ALHS, BLHS) &&
9583 return true;
9584 return std::nullopt;
9585 }
9586}
9587
9588/// Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
9589/// Return false if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is false.
9590/// Otherwise, return std::nullopt if we can't infer anything.
9591static std::optional<bool>
9593 CmpPredicate RPred, const ConstantRange &RCR) {
9594 auto CRImpliesPred = [&](ConstantRange CR,
9595 CmpInst::Predicate Pred) -> std::optional<bool> {
9596 // If all true values for lhs and true for rhs, lhs implies rhs
9597 if (CR.icmp(Pred, RCR))
9598 return true;
9599
9600 // If there is no overlap, lhs implies not rhs
9601 if (CR.icmp(CmpInst::getInversePredicate(Pred), RCR))
9602 return false;
9603
9604 return std::nullopt;
9605 };
9606 if (auto Res = CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9607 RPred))
9608 return Res;
9609 if (LPred.hasSameSign() ^ RPred.hasSameSign()) {
9611 : LPred.dropSameSign();
9613 : RPred.dropSameSign();
9614 return CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9615 RPred);
9616 }
9617 return std::nullopt;
9618}
9619
9620/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
9621/// is true. Return false if LHS implies RHS is false. Otherwise, return
9622/// std::nullopt if we can't infer anything.
9623static std::optional<bool>
9624isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1,
9625 CmpPredicate RPred, const Value *R0, const Value *R1,
9626 const DataLayout &DL, bool LHSIsTrue) {
9627 // The rest of the logic assumes the LHS condition is true. If that's not the
9628 // case, invert the predicate to make it so.
9629 if (!LHSIsTrue)
9630 LPred = ICmpInst::getInverseCmpPredicate(LPred);
9631
9632 // We can have non-canonical operands, so try to normalize any common operand
9633 // to L0/R0.
9634 if (L0 == R1) {
9635 std::swap(R0, R1);
9636 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9637 }
9638 if (R0 == L1) {
9639 std::swap(L0, L1);
9640 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9641 }
9642 if (L1 == R1) {
9643 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
9644 if (L0 != R0 || match(L0, m_ImmConstant())) {
9645 std::swap(L0, L1);
9646 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9647 std::swap(R0, R1);
9648 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9649 }
9650 }
9651
9652 // See if we can infer anything if operand-0 matches and we have at least one
9653 // constant.
9654 const APInt *Unused;
9655 if (L0 == R0 && (match(L1, m_APInt(Unused)) || match(R1, m_APInt(Unused)))) {
9656 // Potential TODO: We could also further use the constant range of L0/R0 to
9657 // further constraint the constant ranges. At the moment this leads to
9658 // several regressions related to not transforming `multi_use(A + C0) eq/ne
9659 // C1` (see discussion: D58633).
9660 SimplifyQuery SQ(DL);
9665
9666 // Even if L1/R1 are not both constant, we can still sometimes deduce
9667 // relationship from a single constant. For example X u> Y implies X != 0.
9668 if (auto R = isImpliedCondCommonOperandWithCR(LPred, LCR, RPred, RCR))
9669 return R;
9670 // If both L1/R1 were exact constant ranges and we didn't get anything
9671 // here, we won't be able to deduce this.
9672 if (match(L1, m_APInt(Unused)) && match(R1, m_APInt(Unused)))
9673 return std::nullopt;
9674 }
9675
9676 // Can we infer anything when the two compares have matching operands?
9677 if (L0 == R0 && L1 == R1)
9678 return ICmpInst::isImpliedByMatchingCmp(LPred, RPred);
9679
9680 // It only really makes sense in the context of signed comparison for "X - Y
9681 // must be positive if X >= Y and no overflow".
9682 // Take SGT as an example: L0:x > L1:y and C >= 0
9683 // ==> R0:(x -nsw y) < R1:(-C) is false
9684 CmpInst::Predicate SignedLPred = LPred.getPreferredSignedPredicate();
9685 if ((SignedLPred == ICmpInst::ICMP_SGT ||
9686 SignedLPred == ICmpInst::ICMP_SGE) &&
9687 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9688 if (match(R1, m_NonPositive()) &&
9689 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == false)
9690 return false;
9691 }
9692
9693 // Take SLT as an example: L0:x < L1:y and C <= 0
9694 // ==> R0:(x -nsw y) < R1:(-C) is true
9695 if ((SignedLPred == ICmpInst::ICMP_SLT ||
9696 SignedLPred == ICmpInst::ICMP_SLE) &&
9697 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9698 if (match(R1, m_NonNegative()) &&
9699 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == true)
9700 return true;
9701 }
9702
9703 // a - b == NonZero -> a != b
9704 // ptrtoint(a) - ptrtoint(b) == NonZero -> a != b
9705 const APInt *L1C;
9706 Value *A, *B;
9707 if (LPred == ICmpInst::ICMP_EQ && ICmpInst::isEquality(RPred) &&
9708 match(L1, m_APInt(L1C)) && !L1C->isZero() &&
9709 match(L0, m_Sub(m_Value(A), m_Value(B))) &&
9710 ((A == R0 && B == R1) || (A == R1 && B == R0) ||
9715 return RPred.dropSameSign() == ICmpInst::ICMP_NE;
9716 }
9717
9718 // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1
9719 if (L0 == R0 &&
9720 (LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) &&
9721 (RPred == ICmpInst::ICMP_ULT || RPred == ICmpInst::ICMP_UGE) &&
9722 match(L0, m_c_Add(m_Specific(L1), m_Specific(R1))))
9723 return CmpPredicate::getMatching(LPred, RPred).has_value();
9724
9725 if (auto P = CmpPredicate::getMatching(LPred, RPred))
9726 return isImpliedCondOperands(*P, L0, L1, R0, R1);
9727
9728 return std::nullopt;
9729}
9730
9731/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
9732/// is true. Return false if LHS implies RHS is false. Otherwise, return
9733/// std::nullopt if we can't infer anything.
9734static std::optional<bool>
9736 FCmpInst::Predicate RPred, const Value *R0, const Value *R1,
9737 const DataLayout &DL, bool LHSIsTrue) {
9738 // The rest of the logic assumes the LHS condition is true. If that's not the
9739 // case, invert the predicate to make it so.
9740 if (!LHSIsTrue)
9741 LPred = FCmpInst::getInversePredicate(LPred);
9742
9743 // We can have non-canonical operands, so try to normalize any common operand
9744 // to L0/R0.
9745 if (L0 == R1) {
9746 std::swap(R0, R1);
9747 RPred = FCmpInst::getSwappedPredicate(RPred);
9748 }
9749 if (R0 == L1) {
9750 std::swap(L0, L1);
9751 LPred = FCmpInst::getSwappedPredicate(LPred);
9752 }
9753 if (L1 == R1) {
9754 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
9755 if (L0 != R0 || match(L0, m_ImmConstant())) {
9756 std::swap(L0, L1);
9757 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9758 std::swap(R0, R1);
9759 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9760 }
9761 }
9762
9763 // Can we infer anything when the two compares have matching operands?
9764 if (L0 == R0 && L1 == R1) {
9765 if ((LPred & RPred) == LPred)
9766 return true;
9767 if ((LPred & ~RPred) == LPred)
9768 return false;
9769 }
9770
9771 // See if we can infer anything if operand-0 matches and we have at least one
9772 // constant.
9773 const APFloat *L1C, *R1C;
9774 if (L0 == R0 && match(L1, m_APFloat(L1C)) && match(R1, m_APFloat(R1C))) {
9775 if (std::optional<ConstantFPRange> DomCR =
9777 if (std::optional<ConstantFPRange> ImpliedCR =
9779 if (ImpliedCR->contains(*DomCR))
9780 return true;
9781 }
9782 if (std::optional<ConstantFPRange> ImpliedCR =
9784 FCmpInst::getInversePredicate(RPred), *R1C)) {
9785 if (ImpliedCR->contains(*DomCR))
9786 return false;
9787 }
9788 }
9789 }
9790
9791 return std::nullopt;
9792}
9793
9794/// Return true if LHS implies RHS is true. Return false if LHS implies RHS is
9795/// false. Otherwise, return std::nullopt if we can't infer anything. We
9796/// expect the RHS to be an icmp and the LHS to be an 'and', 'or', or a 'select'
9797/// instruction.
9798static std::optional<bool>
9800 const Value *RHSOp0, const Value *RHSOp1,
9801 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9802 // The LHS must be an 'or', 'and', or a 'select' instruction.
9803 assert((LHS->getOpcode() == Instruction::And ||
9804 LHS->getOpcode() == Instruction::Or ||
9805 LHS->getOpcode() == Instruction::Select) &&
9806 "Expected LHS to be 'and', 'or', or 'select'.");
9807
9808 assert(Depth <= MaxAnalysisRecursionDepth && "Hit recursion limit");
9809
9810 // If the result of an 'or' is false, then we know both legs of the 'or' are
9811 // false. Similarly, if the result of an 'and' is true, then we know both
9812 // legs of the 'and' are true.
9813 const Value *ALHS, *ARHS;
9814 if ((!LHSIsTrue && match(LHS, m_LogicalOr(m_Value(ALHS), m_Value(ARHS)))) ||
9815 (LHSIsTrue && match(LHS, m_LogicalAnd(m_Value(ALHS), m_Value(ARHS))))) {
9816 // FIXME: Make this non-recursion.
9817 if (std::optional<bool> Implication = isImpliedCondition(
9818 ALHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9819 return Implication;
9820 if (std::optional<bool> Implication = isImpliedCondition(
9821 ARHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9822 return Implication;
9823 return std::nullopt;
9824 }
9825 return std::nullopt;
9826}
9827
9828std::optional<bool>
9830 const Value *RHSOp0, const Value *RHSOp1,
9831 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9832 // Bail out when we hit the limit.
9834 return std::nullopt;
9835
9836 // A mismatch occurs when we compare a scalar cmp to a vector cmp, for
9837 // example.
9838 if (RHSOp0->getType()->isVectorTy() != LHS->getType()->isVectorTy())
9839 return std::nullopt;
9840
9841 assert(LHS->getType()->isIntOrIntVectorTy(1) &&
9842 "Expected integer type only!");
9843
9844 // Match not
9845 if (match(LHS, m_Not(m_Value(LHS))))
9846 LHSIsTrue = !LHSIsTrue;
9847
9848 // Both LHS and RHS are icmps.
9849 if (RHSOp0->getType()->getScalarType()->isIntOrPtrTy()) {
9850 CmpPredicate LHSPred;
9851 Value *LHSOp0, *LHSOp1;
9852 if (match(LHS, m_ICmpLike(LHSPred, m_Value(LHSOp0), m_Value(LHSOp1))))
9853 return isImpliedCondICmps(LHSPred, LHSOp0, LHSOp1, RHSPred, RHSOp0,
9854 RHSOp1, DL, LHSIsTrue);
9855 } else {
9856 assert(RHSOp0->getType()->isFPOrFPVectorTy() &&
9857 "Expected floating point type only!");
9858 if (const auto *LHSCmp = dyn_cast<FCmpInst>(LHS))
9859 return isImpliedCondFCmps(LHSCmp->getPredicate(), LHSCmp->getOperand(0),
9860 LHSCmp->getOperand(1), RHSPred, RHSOp0, RHSOp1,
9861 DL, LHSIsTrue);
9862 }
9863
9864 /// The LHS should be an 'or', 'and', or a 'select' instruction. We expect
9865 /// the RHS to be an icmp.
9866 /// FIXME: Add support for and/or/select on the RHS.
9867 if (const Instruction *LHSI = dyn_cast<Instruction>(LHS)) {
9868 if ((LHSI->getOpcode() == Instruction::And ||
9869 LHSI->getOpcode() == Instruction::Or ||
9870 LHSI->getOpcode() == Instruction::Select))
9871 return isImpliedCondAndOr(LHSI, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue,
9872 Depth);
9873 }
9874 return std::nullopt;
9875}
9876
9877std::optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS,
9878 const DataLayout &DL,
9879 bool LHSIsTrue, unsigned Depth) {
9880 // LHS ==> RHS by definition
9881 if (LHS == RHS)
9882 return LHSIsTrue;
9883
9884 // Match not
9885 bool InvertRHS = false;
9886 if (match(RHS, m_Not(m_Value(RHS)))) {
9887 if (LHS == RHS)
9888 return !LHSIsTrue;
9889 InvertRHS = true;
9890 }
9891
9892 CmpPredicate RHSPred;
9893 Value *RHSOp0, *RHSOp1;
9894 if (match(RHS, m_ICmpLike(RHSPred, m_Value(RHSOp0), m_Value(RHSOp1)))) {
9895 if (auto Implied = isImpliedCondition(LHS, RHSPred, RHSOp0, RHSOp1, DL,
9896 LHSIsTrue, Depth))
9897 return InvertRHS ? !*Implied : *Implied;
9898 return std::nullopt;
9899 }
9900 if (const FCmpInst *RHSCmp = dyn_cast<FCmpInst>(RHS)) {
9901 if (auto Implied = isImpliedCondition(
9902 LHS, RHSCmp->getPredicate(), RHSCmp->getOperand(0),
9903 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))
9904 return InvertRHS ? !*Implied : *Implied;
9905 return std::nullopt;
9906 }
9907
9909 return std::nullopt;
9910
9911 // LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS2
9912 // LHS ==> !(RHS1 && RHS2) if LHS ==> !RHS1 or LHS ==> !RHS2
9913 const Value *RHS1, *RHS2;
9914 if (match(RHS, m_LogicalOr(m_Value(RHS1), m_Value(RHS2)))) {
9915 if (std::optional<bool> Imp =
9916 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9917 if (*Imp == true)
9918 return !InvertRHS;
9919 if (std::optional<bool> Imp =
9920 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9921 if (*Imp == true)
9922 return !InvertRHS;
9923 }
9924 if (match(RHS, m_LogicalAnd(m_Value(RHS1), m_Value(RHS2)))) {
9925 if (std::optional<bool> Imp =
9926 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9927 if (*Imp == false)
9928 return InvertRHS;
9929 if (std::optional<bool> Imp =
9930 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9931 if (*Imp == false)
9932 return InvertRHS;
9933 }
9934
9935 return std::nullopt;
9936}
9937
9938// Returns a pair (Condition, ConditionIsTrue), where Condition is a branch
9939// condition dominating ContextI or nullptr, if no condition is found.
9940static std::pair<Value *, bool>
9942 if (!ContextI || !ContextI->getParent())
9943 return {nullptr, false};
9944
9945 // TODO: This is a poor/cheap way to determine dominance. Should we use a
9946 // dominator tree (eg, from a SimplifyQuery) instead?
9947 const BasicBlock *ContextBB = ContextI->getParent();
9948 const BasicBlock *PredBB = ContextBB->getSinglePredecessor();
9949 if (!PredBB)
9950 return {nullptr, false};
9951
9952 // We need a conditional branch in the predecessor.
9953 Value *PredCond;
9954 BasicBlock *TrueBB, *FalseBB;
9955 if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))
9956 return {nullptr, false};
9957
9958 // The branch should get simplified. Don't bother simplifying this condition.
9959 if (TrueBB == FalseBB)
9960 return {nullptr, false};
9961
9962 assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
9963 "Predecessor block does not point to successor?");
9964
9965 // Is this condition implied by the predecessor condition?
9966 return {PredCond, TrueBB == ContextBB};
9967}
9968
9969std::optional<bool> llvm::isImpliedByDomCondition(const Value *Cond,
9970 const Instruction *ContextI,
9971 const DataLayout &DL) {
9972 assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool");
9973 auto PredCond = getDomPredecessorCondition(ContextI);
9974 if (PredCond.first)
9975 return isImpliedCondition(PredCond.first, Cond, DL, PredCond.second);
9976 return std::nullopt;
9977}
9978
9980 const Value *LHS,
9981 const Value *RHS,
9982 const Instruction *ContextI,
9983 const DataLayout &DL) {
9984 auto PredCond = getDomPredecessorCondition(ContextI);
9985 if (PredCond.first)
9986 return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL,
9987 PredCond.second);
9988 return std::nullopt;
9989}
9990
9992 APInt &Upper, const InstrInfoQuery &IIQ,
9993 bool PreferSignedRange) {
9994 unsigned Width = Lower.getBitWidth();
9995 const APInt *C;
9996 switch (BO.getOpcode()) {
9997 case Instruction::Sub:
9998 if (match(BO.getOperand(0), m_APInt(C))) {
9999 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
10000 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
10001
10002 // If the caller expects a signed compare, then try to use a signed range.
10003 // Otherwise if both no-wraps are set, use the unsigned range because it
10004 // is never larger than the signed range. Example:
10005 // "sub nuw nsw i8 -2, x" is unsigned [0, 254] vs. signed [-128, 126].
10006 // "sub nuw nsw i8 2, x" is unsigned [0, 2] vs. signed [-125, 127].
10007 if (PreferSignedRange && HasNSW && HasNUW)
10008 HasNUW = false;
10009
10010 if (HasNUW) {
10011 // 'sub nuw c, x' produces [0, C].
10012 Upper = *C + 1;
10013 } else if (HasNSW) {
10014 if (C->isNegative()) {
10015 // 'sub nsw -C, x' produces [SINT_MIN, -C - SINT_MIN].
10017 Upper = *C - APInt::getSignedMaxValue(Width);
10018 } else {
10019 // Note that sub 0, INT_MIN is not NSW. It techically is a signed wrap
10020 // 'sub nsw C, x' produces [C - SINT_MAX, SINT_MAX].
10021 Lower = *C - APInt::getSignedMaxValue(Width);
10023 }
10024 }
10025 }
10026 break;
10027 case Instruction::Add:
10028 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
10029 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
10030 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
10031
10032 // If the caller expects a signed compare, then try to use a signed
10033 // range. Otherwise if both no-wraps are set, use the unsigned range
10034 // because it is never larger than the signed range. Example: "add nuw
10035 // nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125].
10036 if (PreferSignedRange && HasNSW && HasNUW)
10037 HasNUW = false;
10038
10039 if (HasNUW) {
10040 // 'add nuw x, C' produces [C, UINT_MAX].
10041 Lower = *C;
10042 } else if (HasNSW) {
10043 if (C->isNegative()) {
10044 // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C].
10046 Upper = APInt::getSignedMaxValue(Width) + *C + 1;
10047 } else {
10048 // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX].
10049 Lower = APInt::getSignedMinValue(Width) + *C;
10050 Upper = APInt::getSignedMaxValue(Width) + 1;
10051 }
10052 }
10053 }
10054 break;
10055
10056 case Instruction::And:
10057 if (match(BO.getOperand(1), m_APInt(C)))
10058 // 'and x, C' produces [0, C].
10059 Upper = *C + 1;
10060 // X & -X is a power of two or zero. So we can cap the value at max power of
10061 // two.
10062 if (match(BO.getOperand(0), m_Neg(m_Specific(BO.getOperand(1)))) ||
10063 match(BO.getOperand(1), m_Neg(m_Specific(BO.getOperand(0)))))
10064 Upper = APInt::getSignedMinValue(Width) + 1;
10065 break;
10066
10067 case Instruction::Or:
10068 if (match(BO.getOperand(1), m_APInt(C)))
10069 // 'or x, C' produces [C, UINT_MAX].
10070 Lower = *C;
10071 break;
10072
10073 case Instruction::AShr:
10074 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10075 // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C].
10077 Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1;
10078 } else if (match(BO.getOperand(0), m_APInt(C))) {
10079 unsigned ShiftAmount = Width - 1;
10080 if (!C->isZero() && IIQ.isExact(&BO))
10081 ShiftAmount = C->countr_zero();
10082 if (C->isNegative()) {
10083 // 'ashr C, x' produces [C, C >> (Width-1)]
10084 Lower = *C;
10085 Upper = C->ashr(ShiftAmount) + 1;
10086 } else {
10087 // 'ashr C, x' produces [C >> (Width-1), C]
10088 Lower = C->ashr(ShiftAmount);
10089 Upper = *C + 1;
10090 }
10091 }
10092 break;
10093
10094 case Instruction::LShr:
10095 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10096 // 'lshr x, C' produces [0, UINT_MAX >> C].
10097 Upper = APInt::getAllOnes(Width).lshr(*C) + 1;
10098 } else if (match(BO.getOperand(0), m_APInt(C))) {
10099 // 'lshr C, x' produces [C >> (Width-1), C].
10100 unsigned ShiftAmount = Width - 1;
10101 if (!C->isZero() && IIQ.isExact(&BO))
10102 ShiftAmount = C->countr_zero();
10103 Lower = C->lshr(ShiftAmount);
10104 Upper = *C + 1;
10105 }
10106 break;
10107
10108 case Instruction::Shl:
10109 if (match(BO.getOperand(0), m_APInt(C))) {
10110 if (IIQ.hasNoUnsignedWrap(&BO)) {
10111 // 'shl nuw C, x' produces [C, C << CLZ(C)]
10112 Lower = *C;
10113 Upper = Lower.shl(Lower.countl_zero()) + 1;
10114 } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw?
10115 if (C->isNegative()) {
10116 // 'shl nsw C, x' produces [C << CLO(C)-1, C]
10117 unsigned ShiftAmount = C->countl_one() - 1;
10118 Lower = C->shl(ShiftAmount);
10119 Upper = *C + 1;
10120 } else {
10121 // 'shl nsw C, x' produces [C, C << CLZ(C)-1]
10122 unsigned ShiftAmount = C->countl_zero() - 1;
10123 Lower = *C;
10124 Upper = C->shl(ShiftAmount) + 1;
10125 }
10126 } else {
10127 // If lowbit is set, value can never be zero.
10128 if ((*C)[0])
10129 Lower = APInt::getOneBitSet(Width, 0);
10130 // If we are shifting a constant the largest it can be is if the longest
10131 // sequence of consecutive ones is shifted to the highbits (breaking
10132 // ties for which sequence is higher). At the moment we take a liberal
10133 // upper bound on this by just popcounting the constant.
10134 // TODO: There may be a bitwise trick for it longest/highest
10135 // consecutative sequence of ones (naive method is O(Width) loop).
10136 Upper = APInt::getHighBitsSet(Width, C->popcount()) + 1;
10137 }
10138 } else if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10139 Upper = APInt::getBitsSetFrom(Width, C->getZExtValue()) + 1;
10140 }
10141 break;
10142
10143 case Instruction::SDiv:
10144 if (match(BO.getOperand(1), m_APInt(C))) {
10145 APInt IntMin = APInt::getSignedMinValue(Width);
10146 APInt IntMax = APInt::getSignedMaxValue(Width);
10147 if (C->isAllOnes()) {
10148 // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX]
10149 // where C != -1 and C != 0 and C != 1
10150 Lower = IntMin + 1;
10151 Upper = IntMax + 1;
10152 } else if (C->countl_zero() < Width - 1) {
10153 // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C]
10154 // where C != -1 and C != 0 and C != 1
10155 Lower = IntMin.sdiv(*C);
10156 Upper = IntMax.sdiv(*C);
10157 if (Lower.sgt(Upper))
10159 Upper = Upper + 1;
10160 assert(Upper != Lower && "Upper part of range has wrapped!");
10161 }
10162 } else if (match(BO.getOperand(0), m_APInt(C))) {
10163 if (C->isMinSignedValue()) {
10164 // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2].
10165 Lower = *C;
10166 Upper = Lower.lshr(1) + 1;
10167 } else {
10168 // 'sdiv C, x' produces [-|C|, |C|].
10169 Upper = C->abs() + 1;
10170 Lower = (-Upper) + 1;
10171 }
10172 }
10173 break;
10174
10175 case Instruction::UDiv:
10176 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
10177 // 'udiv x, C' produces [0, UINT_MAX / C].
10178 Upper = APInt::getMaxValue(Width).udiv(*C) + 1;
10179 } else if (match(BO.getOperand(0), m_APInt(C))) {
10180 // 'udiv C, x' produces [0, C].
10181 Upper = *C + 1;
10182 }
10183 break;
10184
10185 case Instruction::SRem:
10186 if (match(BO.getOperand(1), m_APInt(C))) {
10187 // 'srem x, C' produces (-|C|, |C|).
10188 Upper = C->abs();
10189 Lower = (-Upper) + 1;
10190 } else if (match(BO.getOperand(0), m_APInt(C))) {
10191 if (C->isNegative()) {
10192 // 'srem -|C|, x' produces [-|C|, 0].
10193 Upper = 1;
10194 Lower = *C;
10195 } else {
10196 // 'srem |C|, x' produces [0, |C|].
10197 Upper = *C + 1;
10198 }
10199 }
10200 break;
10201
10202 case Instruction::URem:
10203 if (match(BO.getOperand(1), m_APInt(C)))
10204 // 'urem x, C' produces [0, C).
10205 Upper = *C;
10206 else if (match(BO.getOperand(0), m_APInt(C)))
10207 // 'urem C, x' produces [0, C].
10208 Upper = *C + 1;
10209 break;
10210
10211 default:
10212 break;
10213 }
10214}
10215
10217 bool UseInstrInfo) {
10218 unsigned Width = II.getType()->getScalarSizeInBits();
10219 const APInt *C;
10220 switch (II.getIntrinsicID()) {
10221 case Intrinsic::ctlz:
10222 case Intrinsic::cttz: {
10223 APInt Upper(Width, Width);
10224 if (!UseInstrInfo || !match(II.getArgOperand(1), m_One()))
10225 Upper += 1;
10226 // Maximum of set/clear bits is the bit width.
10228 }
10229 case Intrinsic::ctpop:
10230 // Maximum of set/clear bits is the bit width.
10232 APInt(Width, Width) + 1);
10233 case Intrinsic::uadd_sat:
10234 // uadd.sat(x, C) produces [C, UINT_MAX].
10235 if (match(II.getOperand(0), m_APInt(C)) ||
10236 match(II.getOperand(1), m_APInt(C)))
10238 break;
10239 case Intrinsic::sadd_sat:
10240 if (match(II.getOperand(0), m_APInt(C)) ||
10241 match(II.getOperand(1), m_APInt(C))) {
10242 if (C->isNegative())
10243 // sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)].
10245 APInt::getSignedMaxValue(Width) + *C +
10246 1);
10247
10248 // sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].
10250 APInt::getSignedMaxValue(Width) + 1);
10251 }
10252 break;
10253 case Intrinsic::usub_sat:
10254 // usub.sat(C, x) produces [0, C].
10255 if (match(II.getOperand(0), m_APInt(C)))
10256 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
10257
10258 // usub.sat(x, C) produces [0, UINT_MAX - C].
10259 if (match(II.getOperand(1), m_APInt(C)))
10261 APInt::getMaxValue(Width) - *C + 1);
10262 break;
10263 case Intrinsic::ssub_sat:
10264 if (match(II.getOperand(0), m_APInt(C))) {
10265 if (C->isNegative())
10266 // ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)].
10268 *C - APInt::getSignedMinValue(Width) +
10269 1);
10270
10271 // ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].
10273 APInt::getSignedMaxValue(Width) + 1);
10274 } else if (match(II.getOperand(1), m_APInt(C))) {
10275 if (C->isNegative())
10276 // ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]:
10278 APInt::getSignedMaxValue(Width) + 1);
10279
10280 // ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].
10282 APInt::getSignedMaxValue(Width) - *C +
10283 1);
10284 }
10285 break;
10286 case Intrinsic::umin:
10287 case Intrinsic::umax:
10288 case Intrinsic::smin:
10289 case Intrinsic::smax:
10290 if (!match(II.getOperand(0), m_APInt(C)) &&
10291 !match(II.getOperand(1), m_APInt(C)))
10292 break;
10293
10294 switch (II.getIntrinsicID()) {
10295 case Intrinsic::umin:
10296 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
10297 case Intrinsic::umax:
10299 case Intrinsic::smin:
10301 *C + 1);
10302 case Intrinsic::smax:
10304 APInt::getSignedMaxValue(Width) + 1);
10305 default:
10306 llvm_unreachable("Must be min/max intrinsic");
10307 }
10308 break;
10309 case Intrinsic::abs:
10310 // If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX],
10311 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10312 if (match(II.getOperand(1), m_One()))
10314 APInt::getSignedMaxValue(Width) + 1);
10315
10317 APInt::getSignedMinValue(Width) + 1);
10318 case Intrinsic::vscale:
10319 if (!II.getParent() || !II.getFunction())
10320 break;
10321 return getVScaleRange(II.getFunction(), Width);
10322 default:
10323 break;
10324 }
10325
10326 return ConstantRange::getFull(Width);
10327}
10328
10330 const InstrInfoQuery &IIQ) {
10331 unsigned BitWidth = SI.getType()->getScalarSizeInBits();
10332 const Value *LHS = nullptr, *RHS = nullptr;
10334 if (R.Flavor == SPF_UNKNOWN)
10335 return ConstantRange::getFull(BitWidth);
10336
10337 if (R.Flavor == SelectPatternFlavor::SPF_ABS) {
10338 // If the negation part of the abs (in RHS) has the NSW flag,
10339 // then the result of abs(X) is [0..SIGNED_MAX],
10340 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10341 if (match(RHS, m_Neg(m_Specific(LHS))) &&
10345
10348 }
10349
10350 if (R.Flavor == SelectPatternFlavor::SPF_NABS) {
10351 // The result of -abs(X) is <= 0.
10353 APInt(BitWidth, 1));
10354 }
10355
10356 const APInt *C;
10357 if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C)))
10358 return ConstantRange::getFull(BitWidth);
10359
10360 switch (R.Flavor) {
10361 case SPF_UMIN:
10363 case SPF_UMAX:
10365 case SPF_SMIN:
10367 *C + 1);
10368 case SPF_SMAX:
10371 default:
10372 return ConstantRange::getFull(BitWidth);
10373 }
10374}
10375
10377 // The maximum representable value of a half is 65504. For floats the maximum
10378 // value is 3.4e38 which requires roughly 129 bits.
10379 unsigned BitWidth = I->getType()->getScalarSizeInBits();
10380 if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy())
10381 return;
10382 if (isa<FPToSIInst>(I) && BitWidth >= 17) {
10383 Lower = APInt(BitWidth, -65504, true);
10384 Upper = APInt(BitWidth, 65505);
10385 }
10386
10387 if (isa<FPToUIInst>(I) && BitWidth >= 16) {
10388 // For a fptoui the lower limit is left as 0.
10389 Upper = APInt(BitWidth, 65505);
10390 }
10391}
10392
10394 const SimplifyQuery &SQ,
10395 unsigned Depth) {
10396 assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction");
10397
10399 return ConstantRange::getFull(V->getType()->getScalarSizeInBits());
10400
10401 if (auto *C = dyn_cast<Constant>(V))
10402 return C->toConstantRange();
10403
10404 unsigned BitWidth = V->getType()->getScalarSizeInBits();
10405 ConstantRange CR = ConstantRange::getFull(BitWidth);
10406 if (auto *BO = dyn_cast<BinaryOperator>(V)) {
10407 APInt Lower = APInt(BitWidth, 0);
10408 APInt Upper = APInt(BitWidth, 0);
10409 // TODO: Return ConstantRange.
10410 setLimitsForBinOp(*BO, Lower, Upper, SQ.IIQ, ForSigned);
10412 } else if (auto *II = dyn_cast<IntrinsicInst>(V))
10414 else if (auto *SI = dyn_cast<SelectInst>(V)) {
10415 ConstantRange CRTrue =
10416 computeConstantRange(SI->getTrueValue(), ForSigned, SQ, Depth + 1);
10417 ConstantRange CRFalse =
10418 computeConstantRange(SI->getFalseValue(), ForSigned, SQ, Depth + 1);
10419 CR = CRTrue.unionWith(CRFalse);
10421 } else if (auto *TI = dyn_cast<TruncInst>(V)) {
10422 ConstantRange SrcCR =
10423 computeConstantRange(TI->getOperand(0), ForSigned, SQ, Depth + 1);
10424 CR = SrcCR.truncate(BitWidth);
10425 } else if (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {
10426 APInt Lower = APInt(BitWidth, 0);
10427 APInt Upper = APInt(BitWidth, 0);
10428 // TODO: Return ConstantRange.
10431 } else if (const auto *A = dyn_cast<Argument>(V))
10432 if (std::optional<ConstantRange> Range = A->getRange())
10433 CR = *Range;
10434
10435 if (auto *I = dyn_cast<Instruction>(V)) {
10436 if (auto *Range = SQ.IIQ.getMetadata(I, LLVMContext::MD_range))
10438
10439 Value *FrexpSrc;
10440 if (const auto *CB = dyn_cast<CallBase>(V)) {
10441 if (std::optional<ConstantRange> Range = CB->getRange())
10442 CR = CR.intersectWith(*Range);
10444 m_Value(FrexpSrc))))) {
10445 const fltSemantics &FltSem =
10446 FrexpSrc->getType()->getScalarType()->getFltSemantics();
10447 // It should be possible to implement this for any type, but this logic
10448 // only computes the range assuming standard subnormal handling.
10449 if (APFloat::isIEEELikeFP(FltSem)) {
10450 KnownFPClass KnownSrc =
10451 computeKnownFPClass(FrexpSrc, fcSubnormal, SQ, Depth + 1);
10452
10453 // Exponent result is (src == 0) ? 0 : ilogb(src) + 1, and unspecified
10454 // for inf/nan.
10455 int MinExp = APFloat::semanticsMinExponent(FltSem) + 1;
10456
10457 // Offset to find the true minimum exponent value for a denormal.
10458 if (!KnownSrc.isKnownNeverSubnormal())
10459 MinExp -= (APFloat::semanticsPrecision(FltSem) - 1);
10460
10461 int MaxExp = APFloat::semanticsMaxExponent(FltSem) + 1;
10463 APInt(BitWidth, MinExp, /*isSigned=*/true),
10464 APInt(BitWidth, MaxExp + 1, /*isSigned=*/true));
10465 }
10466 }
10467 }
10468
10469 if (SQ.CxtI && SQ.AC) {
10470 // Try to restrict the range based on information from assumptions.
10471 for (auto &AssumeVH : SQ.AC->assumptionsFor(V)) {
10472 if (!AssumeVH)
10473 continue;
10474 CallInst *I = cast<CallInst>(AssumeVH);
10475 assert(I->getParent()->getParent() == SQ.CxtI->getParent()->getParent() &&
10476 "Got assumption for the wrong function!");
10477 assert(I->getIntrinsicID() == Intrinsic::assume &&
10478 "must be an assume intrinsic");
10479
10480 if (!isValidAssumeForContext(I, SQ))
10481 continue;
10482 Value *Arg = I->getArgOperand(0);
10483 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
10484 // Currently we just use information from comparisons.
10485 if (!Cmp || Cmp->getOperand(0) != V)
10486 continue;
10487 // TODO: Set "ForSigned" parameter via Cmp->isSigned()?
10488 ConstantRange RHS =
10489 computeConstantRange(Cmp->getOperand(1), /*ForSigned=*/false,
10490 SQ.getWithInstruction(I), Depth + 1);
10491 CR = CR.intersectWith(
10492 ConstantRange::makeAllowedICmpRegion(Cmp->getCmpPredicate(), RHS));
10493 }
10494 }
10495
10496 return CR;
10497}
10498
10499static void
10501 function_ref<void(Value *)> InsertAffected) {
10502 assert(V != nullptr);
10503 if (isa<Argument>(V) || isa<GlobalValue>(V)) {
10504 InsertAffected(V);
10505 } else if (auto *I = dyn_cast<Instruction>(V)) {
10506 InsertAffected(V);
10507
10508 // Peek through unary operators to find the source of the condition.
10509 Value *Op;
10511 m_Trunc(m_Value(Op))))) {
10513 InsertAffected(Op);
10514 }
10515 }
10516}
10517
10519 Value *Cond, bool IsAssume, function_ref<void(Value *)> InsertAffected) {
10520 auto AddAffected = [&InsertAffected](Value *V) {
10521 addValueAffectedByCondition(V, InsertAffected);
10522 };
10523
10524 auto AddCmpOperands = [&AddAffected, IsAssume](Value *LHS, Value *RHS) {
10525 if (IsAssume) {
10526 AddAffected(LHS);
10527 AddAffected(RHS);
10528 } else if (match(RHS, m_Constant()))
10529 AddAffected(LHS);
10530 };
10531
10532 SmallVector<Value *, 8> Worklist;
10534 Worklist.push_back(Cond);
10535 while (!Worklist.empty()) {
10536 Value *V = Worklist.pop_back_val();
10537 if (!Visited.insert(V).second)
10538 continue;
10539
10540 CmpPredicate Pred;
10541 Value *A, *B, *X;
10542
10543 if (IsAssume) {
10544 AddAffected(V);
10545 if (match(V, m_Not(m_Value(X))))
10546 AddAffected(X);
10547 }
10548
10549 if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
10550 // assume(A && B) is split to -> assume(A); assume(B);
10551 // assume(!(A || B)) is split to -> assume(!A); assume(!B);
10552 // Finally, assume(A || B) / assume(!(A && B)) generally don't provide
10553 // enough information to be worth handling (intersection of information as
10554 // opposed to union).
10555 if (!IsAssume) {
10556 Worklist.push_back(A);
10557 Worklist.push_back(B);
10558 }
10559 } else if (match(V, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
10560 bool HasRHSC = match(B, m_ConstantInt());
10561 if (ICmpInst::isEquality(Pred)) {
10562 AddAffected(A);
10563 if (IsAssume)
10564 AddAffected(B);
10565 if (HasRHSC) {
10566 Value *Y;
10567 // (X << C) or (X >>_s C) or (X >>_u C).
10568 if (match(A, m_Shift(m_Value(X), m_ConstantInt())))
10569 AddAffected(X);
10570 // (X & C) or (X | C).
10571 else if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10572 match(A, m_Or(m_Value(X), m_Value(Y)))) {
10573 AddAffected(X);
10574 AddAffected(Y);
10575 }
10576 // X - Y
10577 else if (match(A, m_Sub(m_Value(X), m_Value(Y)))) {
10578 AddAffected(X);
10579 AddAffected(Y);
10580 }
10581 }
10582 } else {
10583 AddCmpOperands(A, B);
10584 if (HasRHSC) {
10585 // Handle (A + C1) u< C2, which is the canonical form of
10586 // A > C3 && A < C4.
10588 AddAffected(X);
10589
10590 if (ICmpInst::isUnsigned(Pred)) {
10591 Value *Y;
10592 // X & Y u> C -> X >u C && Y >u C
10593 // X | Y u< C -> X u< C && Y u< C
10594 // X nuw+ Y u< C -> X u< C && Y u< C
10595 if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10596 match(A, m_Or(m_Value(X), m_Value(Y))) ||
10597 match(A, m_NUWAdd(m_Value(X), m_Value(Y)))) {
10598 AddAffected(X);
10599 AddAffected(Y);
10600 }
10601 // X nuw- Y u> C -> X u> C
10602 if (match(A, m_NUWSub(m_Value(X), m_Value())))
10603 AddAffected(X);
10604 }
10605 }
10606
10607 // Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported
10608 // by computeKnownFPClass().
10610 if (Pred == ICmpInst::ICMP_SLT && match(B, m_Zero()))
10611 InsertAffected(X);
10612 else if (Pred == ICmpInst::ICMP_SGT && match(B, m_AllOnes()))
10613 InsertAffected(X);
10614 }
10615 }
10616
10617 if (HasRHSC && match(A, m_Ctpop(m_Value(X))))
10618 AddAffected(X);
10619 } else if (match(V, m_FCmp(Pred, m_Value(A), m_Value(B)))) {
10620 AddCmpOperands(A, B);
10621
10622 // fcmp fneg(x), y
10623 // fcmp fabs(x), y
10624 // fcmp fneg(fabs(x)), y
10625 if (match(A, m_FNeg(m_Value(A))))
10626 AddAffected(A);
10627 if (match(A, m_FAbs(m_Value(A))))
10628 AddAffected(A);
10629
10631 m_Value()))) {
10632 // Handle patterns that computeKnownFPClass() support.
10633 AddAffected(A);
10634 } else if (!IsAssume && match(V, m_Trunc(m_Value(X)))) {
10635 // Assume is checked here as X is already added above for assumes in
10636 // addValueAffectedByCondition
10637 AddAffected(X);
10638 } else if (!IsAssume && match(V, m_Not(m_Value(X)))) {
10639 // Assume is checked here to avoid issues with ephemeral values
10640 Worklist.push_back(X);
10641 }
10642 }
10643}
10644
10646 // (X >> C) or/add (X & mask(C) != 0)
10647 if (const auto *BO = dyn_cast<BinaryOperator>(V)) {
10648 if (BO->getOpcode() == Instruction::Add ||
10649 BO->getOpcode() == Instruction::Or) {
10650 const Value *X;
10651 const APInt *C1, *C2;
10652 if (match(BO, m_c_BinOp(m_LShr(m_Value(X), m_APInt(C1)),
10656 m_Zero())))) &&
10657 C2->popcount() == C1->getZExtValue())
10658 return X;
10659 }
10660 }
10661 return nullptr;
10662}
10663
10665 return const_cast<Value *>(stripNullTest(const_cast<const Value *>(V)));
10666}
10667
10670 unsigned MaxCount, bool AllowUndefOrPoison) {
10673 auto Push = [&](const Value *V) -> bool {
10674 Constant *C;
10675 if (match(const_cast<Value *>(V), m_ImmConstant(C))) {
10676 if (!AllowUndefOrPoison && !isGuaranteedNotToBeUndefOrPoison(C))
10677 return false;
10678 // Check existence first to avoid unnecessary allocations.
10679 if (Constants.contains(C))
10680 return true;
10681 if (Constants.size() == MaxCount)
10682 return false;
10683 Constants.insert(C);
10684 return true;
10685 }
10686
10687 if (auto *Inst = dyn_cast<Instruction>(V)) {
10688 if (Visited.insert(Inst).second)
10689 Worklist.push_back(Inst);
10690 return true;
10691 }
10692 return false;
10693 };
10694 if (!Push(V))
10695 return false;
10696 while (!Worklist.empty()) {
10697 const Instruction *CurInst = Worklist.pop_back_val();
10698 switch (CurInst->getOpcode()) {
10699 case Instruction::Select:
10700 if (!Push(CurInst->getOperand(1)))
10701 return false;
10702 if (!Push(CurInst->getOperand(2)))
10703 return false;
10704 break;
10705 case Instruction::PHI:
10706 for (Value *IncomingValue : cast<PHINode>(CurInst)->incoming_values()) {
10707 // Fast path for recurrence PHI.
10708 if (IncomingValue == CurInst)
10709 continue;
10710 if (!Push(IncomingValue))
10711 return false;
10712 }
10713 break;
10714 default:
10715 return false;
10716 }
10717 }
10718 return true;
10719}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Register Bank Select
Rewrite undef for PHI
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis Results
This file contains the simple types necessary to represent the attributes associated with functions a...
static const Function * getParent(const Value *V)
#define X(NUM, ENUM, NAME)
Definition ELF.h:853
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 _
Module.h This file contains the declarations for the Module class.
static bool hasNoUnsignedWrap(BinaryOperator &I)
static Value * getOpcode(Value &V, Type &Ty, InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB)
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
This file contains the declarations for metadata subclasses.
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
#define P(N)
PowerPC Reduce CR logical Operation
R600 Clause Merge
const SmallVectorImpl< MachineOperand > & Cond
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
std::pair< BasicBlock *, BasicBlock * > Edge
This file contains some templates that are useful if you are working with the STL at all.
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
This file contains the UndefPoisonKind enum and helper functions.
static void computeKnownFPClassFromCond(const Value *V, Value *Cond, bool CondIsTrue, const Instruction *CxtI, KnownFPClass &KnownFromContext, unsigned Depth=0)
static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero, SimplifyQuery &Q, unsigned Depth)
Try to detect a recurrence that the value of the induction variable is always a power of two (or zero...
static cl::opt< unsigned > DomConditionsMaxUses("dom-conditions-max-uses", cl::Hidden, cl::init(20))
static unsigned computeNumSignBitsVectorConstant(const Value *V, const APInt &DemandedElts, unsigned TyBits)
For vector constants, loop over the elements and find the constant with the minimum number of sign bi...
static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS, const Value *RHS)
Return true if "icmp Pred LHS RHS" is always true.
static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if V1 == (binop V2, X), where X is known non-zero.
static bool isGEPKnownNonNull(const GEPOperator *GEP, const SimplifyQuery &Q, unsigned Depth)
Test whether a GEP's result is known to be non-null.
static bool isNonEqualShl(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and the shift is nuw or nsw.
static bool isKnownNonNullFromDominatingCondition(const Value *V, const Instruction *CtxI, const DominatorTree *DT)
static const Value * getUnderlyingObjectFromInt(const Value *V)
This is the function that does the work of looking through basic ptrtoint+arithmetic+inttoptr sequenc...
static bool isNonZeroMul(const APInt &DemandedElts, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, bool NSW, bool NUW, unsigned Depth)
static bool rangeMetadataExcludesValue(const MDNode *Ranges, const APInt &Value)
Does the 'Range' metadata (which must be a valid MD_range operand list) ensure that the value it's at...
static KnownBits getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts, const KnownBits &KnownLHS, const KnownBits &KnownRHS, const SimplifyQuery &Q, unsigned Depth)
static void breakSelfRecursivePHI(const Use *U, const PHINode *PHI, Value *&ValOut, Instruction *&CtxIOut, const PHINode **PhiOut=nullptr)
static bool isNonZeroSub(const APInt &DemandedElts, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, unsigned Depth)
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR)
Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
static void addValueAffectedByCondition(Value *V, function_ref< void(Value *)> InsertAffected)
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
static bool haveNoCommonBitsSetSpecialCases(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
static void setLimitsForBinOp(const BinaryOperator &BO, APInt &Lower, APInt &Upper, const InstrInfoQuery &IIQ, bool PreferSignedRange)
static Value * lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2, Instruction::CastOps *CastOp)
Helps to match a select pattern in case of a type mismatch.
static std::pair< Value *, bool > getDomPredecessorCondition(const Instruction *ContextI)
static constexpr unsigned MaxInstrsToCheckForFree
Maximum number of instructions to check between assume and context instruction.
static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, const KnownBits &KnownVal, unsigned Depth)
static std::optional< bool > isImpliedCondFCmps(FCmpInst::Predicate LPred, const Value *L0, const Value *L1, FCmpInst::Predicate RPred, const Value *R0, const Value *R1, const DataLayout &DL, bool LHSIsTrue)
Return true if LHS implies RHS (expanded to its components as "R0 RPred R1") is true.
static bool isKnownNonEqualFromContext(const Value *V1, const Value *V2, const SimplifyQuery &Q, unsigned Depth)
static SelectPatternResult matchFastFloatClamp(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS)
Match clamp pattern for float types without care about NaNs or signed zeros.
static std::optional< bool > isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1, CmpPredicate RPred, const Value *R0, const Value *R1, const DataLayout &DL, bool LHSIsTrue)
Return true if LHS implies RHS (expanded to its components as "R0 RPred R1") is true.
static std::optional< bool > isImpliedCondCommonOperandWithCR(CmpPredicate LPred, const ConstantRange &LCR, CmpPredicate RPred, const ConstantRange &RCR)
Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
static ConstantRange getRangeForSelectPattern(const SelectInst &SI, const InstrInfoQuery &IIQ)
static void computeKnownBitsFromOperator(const Operator *I, const APInt &DemandedElts, KnownBits &Known, const SimplifyQuery &Q, unsigned Depth)
static uint64_t GetStringLengthH(const Value *V, SmallPtrSetImpl< const PHINode * > &PHIs, unsigned CharSize)
If we can compute the length of the string pointed to by the specified pointer, return 'len+1'.
static void computeKnownBitsFromShiftOperator(const Operator *I, const APInt &DemandedElts, KnownBits &Known, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth, function_ref< KnownBits(const KnownBits &, const KnownBits &, bool)> KF)
Compute known bits from a shift operator, including those with a non-constant shift amount.
static bool onlyUsedByLifetimeMarkersOrDroppableInstsHelper(const Value *V, bool AllowLifetime, bool AllowDroppable)
static std::optional< bool > isImpliedCondAndOr(const Instruction *LHS, CmpPredicate RHSPred, const Value *RHSOp0, const Value *RHSOp1, const DataLayout &DL, bool LHSIsTrue, unsigned Depth)
Return true if LHS implies RHS is true.
static bool isSignedMinMaxClamp(const Value *Select, const Value *&In, const APInt *&CLow, const APInt *&CHigh)
static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, bool NSW, bool NUW, unsigned Depth)
static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V, unsigned Depth)
static bool isNonEqualSelect(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst, Value *&Init, Value *&OtherOp)
static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static void computeKnownBitsFromCmp(const Value *V, CmpInst::Predicate Pred, Value *LHS, Value *RHS, KnownBits &Known, const SimplifyQuery &Q)
static SelectPatternResult matchMinMaxOfMinMax(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TVal, Value *FVal, unsigned Depth)
Recognize variations of: a < c ?
static void unionWithMinMaxIntrinsicClamp(const IntrinsicInst *II, KnownBits &Known)
static void setLimitForFPToI(const Instruction *I, APInt &Lower, APInt &Upper)
static bool isSameUnderlyingObjectInLoop(const PHINode *PN, const LoopInfo *LI)
PN defines a loop-variant pointer to an object.
static bool isNonEqualPointersWithRecursiveGEP(const Value *A, const Value *B, const SimplifyQuery &Q)
static bool isSignedMinMaxIntrinsicClamp(const IntrinsicInst *II, const APInt *&CLow, const APInt *&CHigh)
static Value * lookThroughCastConst(CmpInst *CmpI, Type *SrcTy, Constant *C, Instruction::CastOps *CastOp)
static bool handleGuaranteedWellDefinedOps(const Instruction *I, const CallableT &Handle)
Enumerates all operands of I that are guaranteed to not be undef or poison.
static bool isAbsoluteValueULEOne(const Value *V)
static void computeKnownBitsFromLerpPattern(const Value *Op0, const Value *Op1, const APInt &DemandedElts, KnownBits &KnownOut, const SimplifyQuery &Q, unsigned Depth)
Try to detect the lerp pattern: a * (b - c) + c * d where a >= 0, b >= 0, c >= 0, d >= 0,...
static KnownFPClass computeKnownFPClassFromContext(const Value *V, const SimplifyQuery &Q)
static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1, bool NSW, bool NUW, const APInt &DemandedElts, KnownBits &KnownOut, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth)
static Value * getNotValue(Value *V)
If the input value is the result of a 'not' op, constant integer, or vector splat of a constant integ...
static constexpr KnownFPClass::MinMaxKind getMinMaxKind(Intrinsic::ID IID)
static unsigned ComputeNumSignBitsImpl(const Value *V, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return the number of times the sign bit of the register is replicated into the other bits.
static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp, KnownBits &Known, const SimplifyQuery &SQ, bool Invert)
static bool isKnownNonZeroFromOperator(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static bool matchOpWithOpEqZero(Value *Op0, Value *Op1)
static bool isNonZeroRecurrence(const PHINode *PN)
Try to detect a recurrence that monotonically increases/decreases from a non-zero starting value.
static SelectPatternResult matchClamp(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal)
Recognize variations of: CLAMP(v,l,h) ==> ((v) < (l) ?
static bool shiftAmountKnownInRange(const Value *ShiftAmount)
Shifts return poison if shiftwidth is larger than the bitwidth.
static bool isEphemeralValueOf(const Instruction *I, const Value *E)
static SelectPatternResult matchMinMax(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, unsigned Depth)
Match non-obvious integer minimum and maximum sequences.
static KnownBits computeKnownBitsForHorizontalOperation(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth, const function_ref< KnownBits(const KnownBits &, const KnownBits &)> KnownBitsFunc)
static bool handleGuaranteedNonPoisonOps(const Instruction *I, const CallableT &Handle)
Enumerates all operands of I that are guaranteed to not be poison.
static std::optional< std::pair< Value *, Value * > > getInvertibleOperands(const Operator *Op1, const Operator *Op2)
If the pair of operators are the same invertible function, return the the operands of the function co...
static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS)
static void computeKnownBitsFromCond(const Value *V, Value *Cond, KnownBits &Known, const SimplifyQuery &SQ, bool Invert, unsigned Depth)
static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q)
static std::optional< bool > isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS, const Value *ARHS, const Value *BLHS, const Value *BRHS)
Return true if "icmp Pred BLHS BRHS" is true whenever "icmp PredALHS ARHS" is true.
static const Instruction * safeCxtI(const Value *V, const Instruction *CxtI)
static bool isNonEqualMul(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and the multiplication is nuw o...
static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero, const Value *Cond, bool CondIsTrue)
Return true if we can infer that V is known to be a power of 2 from dominating condition Cond (e....
static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW, bool NUW, const APInt &DemandedElts, KnownBits &Known, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth)
static bool matchThreeInputRecurrence(const PHINode *PN, InstTy *&Inst, Value *&Init, Value *&OtherOp0, Value *&OtherOp1)
static bool isKnownNonNaN(const Value *V, FastMathFlags FMF)
static ConstantRange getRangeForIntrinsic(const IntrinsicInst &II, bool UseInstrInfo)
static void computeKnownFPClassForFPTrunc(const Operator *Op, const APInt &DemandedElts, FPClassTest InterestedClasses, KnownFPClass &Known, const SimplifyQuery &Q, unsigned Depth)
static Value * BuildSubAggregate(Value *From, Value *To, Type *IndexedType, SmallVectorImpl< unsigned > &Idxs, unsigned IdxSkip, BasicBlock::iterator InsertBefore)
Value * RHS
Value * LHS
static LLVM_ABI ExponentType semanticsMinExponent(const fltSemantics &)
Definition APFloat.cpp:235
static LLVM_ABI ExponentType semanticsMaxExponent(const fltSemantics &)
Definition APFloat.cpp:231
static LLVM_ABI unsigned int semanticsPrecision(const fltSemantics &)
Definition APFloat.cpp:227
static LLVM_ABI bool isIEEELikeFP(const fltSemantics &)
Definition APFloat.cpp:268
bool isFinite() const
Definition APFloat.h:1543
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1197
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1157
bool isInteger() const
Definition APFloat.h:1555
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:2023
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1616
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:235
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition APInt.h:1429
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition APInt.h:424
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1563
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition APInt.h:1414
unsigned popcount() const
Count the number of bits set.
Definition APInt.h:1693
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1408
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition APInt.h:207
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition APInt.h:1353
unsigned ceilLogBase2() const
Definition APInt.h:1787
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition APInt.h:1208
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition APInt.h:372
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1189
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:381
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1511
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1118
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:210
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition APInt.h:217
bool isNegative() const
Determine sign of this APInt.
Definition APInt.h:330
bool intersects(const APInt &RHS) const
This operation tests if there are any pairs of corresponding bits between this APInt and RHS that are...
Definition APInt.h:1256
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1687
void clearAllBits()
Set every bit to 0.
Definition APInt.h:1419
LLVM_ABI APInt reverseBits() const
Definition APInt.cpp:790
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition APInt.h:1173
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1651
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition APInt.h:1621
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:220
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1084
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition APInt.h:357
unsigned logBase2() const
Definition APInt.h:1784
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:834
bool getBoolValue() const
Convert APInt to a boolean value.
Definition APInt.h:472
bool isMaxSignedValue() const
Determine if this is the largest signed value.
Definition APInt.h:406
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:335
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition APInt.h:1157
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:880
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition APInt.h:1264
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition APInt.h:1137
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition APInt.h:297
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:201
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition APInt.h:1411
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1244
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:287
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:240
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:858
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1228
void clearSignBit()
Set the sign bit to 0.
Definition APInt.h:1472
an instruction to allocate memory on the stack
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator end() const
Definition ArrayRef.h:130
size_t size() const
Get the array size.
Definition ArrayRef.h:141
iterator begin() const
Definition ArrayRef.h:129
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Definition ArrayRef.h:185
Class to represent array types.
This represents the llvm.assume intrinsic.
A cache of @llvm.assume calls within a function.
MutableArrayRef< ResultElem > assumptionsFor(const Value *V)
Access the list of assumptions which affect this value.
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:105
LLVM_ABI std::optional< unsigned > getVScaleRangeMax() const
Returns the maximum value for the vscale_range attribute or std::nullopt when unknown.
LLVM_ABI unsigned getVScaleRangeMin() const
Returns the minimum value for the vscale_range attribute.
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition Attributes.h:261
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator end()
Definition BasicBlock.h:474
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:461
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
InstListType::const_iterator const_iterator
Definition BasicBlock.h:171
LLVM_ABI const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
LLVM_ABI const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Definition BasicBlock.h:237
LLVM_ABI Instruction::BinaryOps getBinaryOp() const
Returns the binary operation underlying the intrinsic.
BinaryOps getOpcode() const
Definition InstrTypes.h:409
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
LLVM_ABI bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
LLVM_ABI bool isIndirectCall() const
Return true if the callsite is an indirect call.
bool onlyReadsMemory(unsigned OpNo) const
Value * getCalledOperand() const
Value * getArgOperand(unsigned i) const
LLVM_ABI Intrinsic::ID getIntrinsicID() const
Returns the intrinsic ID of the intrinsic called or Intrinsic::not_intrinsic if the called function i...
unsigned arg_size() const
This class represents a function call, abstracting a target machine's calling convention.
This is the base class for all instructions that perform data casts.
Definition InstrTypes.h:512
This class is the base class for the comparison instructions.
Definition InstrTypes.h:728
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:740
@ 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
@ 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
@ 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
bool isSigned() const
Definition InstrTypes.h:993
static LLVM_ABI bool isEquality(Predicate pred)
Determine if this is an equals/not equals predicate.
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition InstrTypes.h:890
bool isTrueWhenEqual() const
This is just a convenience.
static bool isFPPredicate(Predicate P)
Definition InstrTypes.h:833
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition InstrTypes.h:852
Predicate getPredicate() const
Return the predicate for this instruction.
Definition InstrTypes.h:828
Predicate getFlippedStrictnessPredicate() const
For predicate of kind "is X or equal to 0" returns the predicate "is X".
Definition InstrTypes.h:956
static bool isIntPredicate(Predicate P)
Definition InstrTypes.h:839
static LLVM_ABI bool isOrdered(Predicate predicate)
Determine if the predicate is an ordered operation.
bool isUnsigned() const
Definition InstrTypes.h:999
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
static LLVM_ABI std::optional< CmpPredicate > getMatching(CmpPredicate A, CmpPredicate B)
Compares two CmpPredicates taking samesign into account and returns the canonicalized CmpPredicate if...
LLVM_ABI CmpInst::Predicate getPreferredSignedPredicate() const
Attempts to return a signed CmpInst::Predicate from the CmpPredicate.
CmpInst::Predicate dropSameSign() const
Drops samesign information.
bool hasSameSign() const
Query samesign information, for optimizations.
Conditional Branch instruction.
An array constant whose element type is a simple 1/2/4/8-byte integer, bytes or float/double,...
Definition Constants.h:859
ConstantDataSequential - A vector or array constant whose element type is a simple 1/2/4/8-byte integ...
Definition Constants.h:749
StringRef getAsString() const
If this array is isString(), then this method returns the array as a StringRef.
Definition Constants.h:825
A vector constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition Constants.h:945
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 OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const
Return whether unsigned mul of the two ranges always/never overflows.
LLVM_ABI ConstantRange truncate(uint32_t BitWidth, unsigned NoWrapKind=0) const
Return a new range in the specified integer type, which must be strictly smaller than the current typ...
LLVM_ABI bool isAllNonNegative() const
Return true if all values in this range are non-negative.
static LLVM_ABI ConstantRange makeAllowedICmpRegion(CmpInst::Predicate Pred, const ConstantRange &Other)
Produce the smallest range such that all values that may satisfy the given predicate with any value c...
LLVM_ABI ConstantRange unionWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the union of this range with another range.
static LLVM_ABI ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred, const APInt &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
LLVM_ABI bool contains(const APInt &Val) const
Return true if the specified value is in the set.
LLVM_ABI OverflowResult signedAddMayOverflow(const ConstantRange &Other) const
Return whether signed add of the two ranges always/never overflows.
LLVM_ABI ConstantRange intersectWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the intersection of this range with another range.
OverflowResult
Represents whether an operation on the given constant range is known to always or never overflow.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
static ConstantRange getNonEmpty(APInt Lower, APInt Upper)
Create non-empty constant range with the given bounds.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
LLVM_ABI OverflowResult signedSubMayOverflow(const ConstantRange &Other) const
Return whether signed sub of the two ranges always/never overflows.
LLVM_ABI ConstantRange sub(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a subtraction of a value in this r...
This is an important base class in LLVM.
Definition Constant.h:43
static LLVM_ABI Constant * replaceUndefsWith(Constant *C, Constant *Replacement)
Try to replace undefined constant C or undefined elements in C with Replacement.
LLVM_ABI Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition Constant.h:64
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
LLVM_ABI Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
bool isLittleEndian() const
Layout endianness...
Definition DataLayout.h:217
unsigned getAddressSizeInBits(unsigned AS) const
The size in bits of an address in for the given AS.
Definition DataLayout.h:518
LLVM_ABI const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
LLVM_ABI unsigned getIndexTypeSizeInBits(Type *Ty) const
The size in bits of the index used in GEP calculation for this type.
LLVM_ABI unsigned getPointerTypeSizeInBits(Type *) const
The pointer representation size in bits for this type.
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
Definition DataLayout.h:791
ArrayRef< CondBrInst * > conditionsFor(const Value *V) const
Access the list of branches which affect this value.
DomTreeNodeBase * getIDom() const
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:151
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
This instruction extracts a struct member or array element value from an aggregate value.
ArrayRef< unsigned > getIndices() const
unsigned getNumIndices() const
static LLVM_ABI Type * getIndexedType(Type *Agg, ArrayRef< unsigned > Idxs)
Returns the type of the element that would be extracted with an extractvalue instruction with the spe...
This instruction compares its operands according to the predicate given to the constructor.
Utility class for floating point operations which can have information about relaxed accuracy require...
Definition Operator.h:202
Convenience struct for specifying and reasoning about fast-math flags.
Definition FMF.h:23
bool noSignedZeros() const
Definition FMF.h:67
bool noInfs() const
Definition FMF.h:66
void setNoSignedZeros(bool B=true)
Definition FMF.h:84
void setNoNaNs(bool B=true)
Definition FMF.h:78
bool noNaNs() const
Definition FMF.h:65
const BasicBlock & getEntryBlock() const
Definition Function.h:809
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:141
Type * getValueType() const
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
This instruction compares its operands according to the predicate given to the constructor.
CmpPredicate getSwappedCmpPredicate() const
CmpPredicate getInverseCmpPredicate() const
Predicate getFlippedSignednessPredicate() const
For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->EQ.
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
static LLVM_ABI std::optional< bool > isImpliedByMatchingCmp(CmpPredicate Pred1, CmpPredicate Pred2)
Determine if Pred1 implies Pred2 is true, false, or if nothing can be inferred about the implication,...
bool isRelational() const
Return true if the predicate is relational (not EQ or NE).
Predicate getUnsignedPredicate() const
For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
This instruction inserts a struct field of array element value into an aggregate value.
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
LLVM_ABI bool hasNoNaNs() const LLVM_READONLY
Determine whether the no-NaNs flag is set.
LLVM_ABI bool hasNoUnsignedWrap() const LLVM_READONLY
Determine whether the no unsigned wrap flag is set.
LLVM_ABI bool hasNoSignedWrap() const LLVM_READONLY
Determine whether the no signed wrap flag is set.
bool isBinaryOp() const
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI bool isExact() const LLVM_READONLY
Determine whether the exact flag is set.
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
LLVM_ABI bool comesBefore(const Instruction *Other) const
Given an instruction Other in the same basic block as this instruction, return true if this instructi...
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
bool isUnaryOp() const
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
A wrapper class for inspecting calls to intrinsic functions.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
An instruction for reading from memory.
Value * getPointerOperand()
Align getAlign() const
Return the alignment of the access that is being performed.
bool isLoopHeader(const BlockT *BB) const
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
Metadata node.
Definition Metadata.h:1069
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1433
This is a utility class that provides an abstraction for the common functionality between Instruction...
Definition Operator.h:33
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
Definition Operator.h:43
Utility class for integer operators which may exhibit overflow - Add, Sub, Mul, and Shl.
Definition Operator.h:78
iterator_range< const_block_iterator > blocks() const
Value * getIncomingValueForBlock(const BasicBlock *BB) const
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A udiv, sdiv, lshr, or ashr instruction, which can be marked as "exact", indicating that no bits are ...
Definition Operator.h:156
bool isExact() const
Test whether this division is known to be exact, with zero remainder.
Definition Operator.h:175
This class represents the LLVM 'select' instruction.
const Value * getFalseValue() const
const Value * getCondition() const
const Value * getTrueValue() const
This instruction constructs a fixed permutation of two input vectors.
VectorType * getType() const
Overload to return most specific vector type.
static LLVM_ABI void getShuffleMask(const Constant *Mask, SmallVectorImpl< int > &Result)
Convert the input shuffle mask operand to a vector of integers.
size_type size() const
Definition SmallPtrSet.h:99
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void reserve(size_type N)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:591
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition DataLayout.h:743
TypeSize getElementOffset(unsigned Idx) const
Definition DataLayout.h:774
Class to represent struct types.
unsigned getNumElements() const
Random access to the elements.
Type * getElementType(unsigned N) const
Provides information about what library functions are available for the current target.
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM_ABI unsigned getIntegerBitWidth() const
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:288
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition Type.h:263
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:282
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
Definition Type.h:155
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
LLVM_ABI uint64_t getArrayNumElements() const
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:307
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:368
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:197
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition Type.h:326
bool isHalfTy() const
Return true if this is 'half', a 16-bit IEEE fp type.
Definition Type.h:144
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:232
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
Definition Type.h:158
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
Definition Type.h:285
bool isIntOrPtrTy() const
Return true if this is an integer type or a pointer type.
Definition Type.h:270
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:257
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition Type.cpp:313
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
Definition Type.h:227
LLVM_ABI const fltSemantics & getFltSemantics() const
Definition Type.cpp:106
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM_ABI unsigned getOperandNo() const
Return the operand # of this use in its User.
Definition Use.cpp:36
User * getUser() const
Returns the User that contains this Use.
Definition Use.h:61
op_range operands()
Definition User.h:267
Value * getOperand(unsigned i) const
Definition User.h:207
unsigned getNumOperands() const
Definition User.h:229
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
const Value * stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, APInt &Offset) const
This is a wrapper around stripAndAccumulateConstantOffsets with the in-bounds requirement set to fals...
Definition Value.h:727
iterator_range< user_iterator > users()
Definition Value.h:426
LLVM_ABI const Value * stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, bool AllowInvariantGroup=false, function_ref< bool(Value &Value, APInt &Offset)> ExternalAnalysis=nullptr, bool LookThroughIntToPtr=false) const
Accumulate the constant offset this value has compared to a base pointer.
const KnownBits & getKnownBits(const SimplifyQuery &Q) const
Definition WithCache.h:59
PointerType getValue() const
Definition WithCache.h:57
Represents an op.with.overflow intrinsic.
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:168
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:165
An efficient, type-erasing, non-owning reference to a callable.
TypeSize getSequentialElementStride(const DataLayout &DL) const
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:123
A range adaptor for a pair of iterators.
CallInst * Call
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
#define UINT64_MAX
Definition DataTypes.h:77
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI APInt ScaleBitMask(const APInt &A, unsigned NewBitWidth, bool MatchAllBits=false)
Splat/Merge neighboring bits to widen/narrow the bitmask represented by.
Definition APInt.cpp:3061
const APInt & umax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be unsigned.
Definition APInt.h:2292
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
SpecificConstantMatch m_ZeroInt()
Convenience matchers for specific integer values.
BinaryOp_match< SpecificConstantMatch, SrcTy, TargetOpcode::G_SUB > m_Neg(const SrcTy &&Src)
Matches a register negated by a G_SUB.
BinaryOp_match< SrcTy, SpecificConstantMatch, TargetOpcode::G_XOR, true > m_Not(const SrcTy &&Src)
Matches a register not-ed by a G_XOR.
OneUse_match< SubPat > m_OneUse(const SubPat &SP)
match_combine_or< Ty... > m_CombineOr(const Ty &...Ps)
Combine pattern matchers matching any of Ps patterns.
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
cst_pred_ty< is_lowbit_mask > m_LowBitMask()
Match an integer or vector with only the low bit(s) set.
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)
cst_pred_ty< is_sign_mask > m_SignMask()
Match an integer or vector with only the sign bit(s) set.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWAdd(const LHS &L, const RHS &R)
auto m_PtrToIntOrAddr(const OpTy &Op)
Matches PtrToInt or PtrToAddr.
BinaryOp_match< LHS, RHS, Instruction::FSub > m_FSub(const LHS &L, const RHS &R)
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
BinaryOp_match< LHS, RHS, Instruction::URem > m_URem(const LHS &L, const RHS &R)
auto m_LogicalOp()
Matches either L && R or L || R where L and R are arbitrary values.
ap_match< APInt > m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
BinaryOp_match< LHS, RHS, Instruction::And, true > m_c_And(const LHS &L, const RHS &R)
Matches an And with LHS and RHS in either order.
cst_pred_ty< is_power2_or_zero > m_Power2OrZero()
Match an integer or vector of 0 or power-of-2 values.
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWSub(const LHS &L, const RHS &R)
specific_intval< false > m_SpecificInt(const APInt &V)
Match a specific integer value or vector with all elements equal to the value.
bool match(Val *V, const Pattern &P)
BinOpPred_match< LHS, RHS, is_idiv_op > m_IDiv(const LHS &L, const RHS &R)
Matches integer division operations.
match_bind< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
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()...
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMaxNum(const Opnd0 &Op0, const Opnd1 &Op1)
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)
cst_pred_ty< is_nonnegative > m_NonNegative()
Match an integer or vector of non-negative values.
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
IntrinsicID_match m_Intrinsic()
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
IntrinsicID_match m_VScale()
Matches a call to llvm.vscale().
match_combine_or< MaxMin_match< FCmpInst, LHS, RHS, ofmin_pred_ty >, MaxMin_match< FCmpInst, LHS, RHS, ufmin_pred_ty > > m_OrdOrUnordFMin(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point minimum function.
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.
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > m_SMin(const LHS &L, const RHS &R)
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.
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
auto m_Constant()
Match an arbitrary Constant and ignore it.
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty, true > m_c_SMin(const LHS &L, const RHS &R)
Matches an SMin with LHS and RHS in either order.
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty, true > m_c_UMax(const LHS &L, const RHS &R)
Matches a UMax with LHS and RHS in either order.
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.
BinaryOp_match< LHS, RHS, Instruction::UDiv > m_UDiv(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty > m_UMax(const LHS &L, const RHS &R)
match_immconstant_ty m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty, true > m_c_UMin(const LHS &L, const RHS &R)
Matches a UMin with LHS and RHS in either order.
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".
match_combine_or< MaxMin_match< FCmpInst, LHS, RHS, ofmax_pred_ty >, MaxMin_match< FCmpInst, LHS, RHS, ufmax_pred_ty > > m_OrdOrUnordFMax(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point maximum function.
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty, true > m_c_SMax(const LHS &L, const RHS &R)
Matches an SMax with LHS and RHS in either order.
CastOperator_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
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.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWSub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty > m_SMax(const LHS &L, const RHS &R)
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".
m_Intrinsic_Ty< Opnd0 >::Ty m_Ctpop(const Opnd0 &Op0)
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap > m_NSWAdd(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, SExtInst > > m_ZExtOrSExt(const OpTy &Op)
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match 'fneg X' as 'fsub -0.0, X'.
BinOpPred_match< LHS, RHS, is_shift_op > m_Shift(const LHS &L, const RHS &R)
Matches shift operations.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
BinOpPred_match< LHS, RHS, is_irem_op > m_IRem(const LHS &L, const RHS &R)
Matches integer remainder operations.
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
brc_match< Cond_t, match_bind< BasicBlock >, match_bind< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_Ctlz(const Opnd0 &Op0, const Opnd1 &Op1)
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMinNum(const Opnd0 &Op0, const Opnd1 &Op1)
cst_pred_ty< is_nonpositive > m_NonPositive()
Match an integer or vector of non-positive values.
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap >, DisjointOr_match< LHS, RHS > > m_NUWAddLike(const LHS &L, const RHS &R)
Match either "add nuw" or "or disjoint".
ElementWiseBitCast_match< OpTy > m_ElementWiseBitCast(const OpTy &Op)
m_Intrinsic_Ty< Opnd0 >::Ty m_FAbs(const Opnd0 &Op0)
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.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > m_UMin(const LHS &L, const RHS &R)
auto m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
static unsigned decodeVSEW(unsigned VSEW)
LLVM_ABI unsigned getSEWLMULRatio(unsigned SEW, VLMUL VLMul)
static constexpr unsigned RVVBitsPerBlock
initializer< Ty > init(const Ty &Val)
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:668
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI bool haveNoCommonBitsSet(const WithCache< const Value * > &LHSCache, const WithCache< const Value * > &RHSCache, const SimplifyQuery &SQ)
Return true if LHS and RHS have no common bits set.
LLVM_ABI bool mustExecuteUBIfPoisonOnPathTo(Instruction *Root, Instruction *OnPathTo, DominatorTree *DT)
Return true if undefined behavior would provable be executed on the path to OnPathTo if Root produced...
LLVM_ABI Intrinsic::ID getInverseMinMaxIntrinsic(Intrinsic::ID MinMaxID)
LLVM_ABI bool willNotFreeBetween(const Instruction *Assume, const Instruction *CtxI)
Returns true, if no instruction between Assume and CtxI may free (including through synchronization).
@ Offset
Definition DWP.cpp:558
@ Length
Definition DWP.cpp:558
@ 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:1738
MaybeAlign getAlign(const CallInst &I, unsigned Index)
LLVM_ABI bool isValidAssumeForContext(const Instruction *I, const Instruction *CxtI, const DominatorTree *DT=nullptr, bool AllowEphemerals=false)
Return true if it is valid to use the assumptions provided by an assume intrinsic,...
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1668
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.
BundleAttr getBundleAttrFromOBU(OperandBundleUse OBU)
LLVM_ABI bool isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI)
LLVM_ABI bool isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS, bool &TrueIfSigned)
Given an exploded icmp instruction, return true if the comparison only checks the sign bit.
LLVM_ABI bool isAssumeLikeIntrinsic(const Instruction *I)
Return true if it is an intrinsic that cannot be speculated but also cannot trap.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2553
LLVM_ABI AllocaInst * findAllocaForValue(Value *V, bool OffsetZero=false)
Returns unique alloca where the value comes from, or nullptr.
LLVM_ABI APInt getMinMaxLimit(SelectPatternFlavor SPF, unsigned BitWidth)
Return the minimum or maximum constant value for the specified integer min/max flavor and type.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI bool isOnlyUsedInZeroComparison(const Instruction *CxtI)
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
LLVM_ABI bool getConstantStringInfo(const Value *V, StringRef &Str, bool TrimAtNul=true)
This function computes the length of a null-terminated C string pointed to by V.
LLVM_ABI bool isDereferenceableAndAlignedPointer(const Value *V, Type *Ty, Align Alignment, const DataLayout &DL, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Returns true if V is always a dereferenceable pointer with alignment greater or equal than requested.
Definition Loads.cpp:251
LLVM_ABI bool onlyUsedByLifetimeMarkersOrDroppableInsts(const Value *V)
Return true if the only users of this pointer are lifetime markers or droppable instructions.
LLVM_ABI Constant * ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset)
LLVM_ABI Value * stripNullTest(Value *V)
Returns the inner value X if the expression has the form f(X) where f(X) == 0 if and only if X == 0,...
LLVM_ABI bool getUnderlyingObjectsForCodeGen(const Value *V, SmallVectorImpl< Value * > &Objects)
This is a wrapper around getUnderlyingObjects and adds support for basic ptrtoint+arithmetic+inttoptr...
LLVM_ABI std::pair< Intrinsic::ID, bool > canConvertToMinOrMaxIntrinsic(ArrayRef< Value * > VL)
Check if the values in VL are select instructions that can be converted to a min or max (vector) intr...
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ABI bool getConstantDataArrayInfo(const Value *V, ConstantDataArraySlice &Slice, unsigned ElementSize, uint64_t Offset=0)
Returns true if the value V is a pointer into a ConstantDataArray.
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition bit.h:325
LLVM_ABI bool isGuaranteedToExecuteForEveryIteration(const Instruction *I, const Loop *L)
Return true if this function can prove that the instruction I is executed for every iteration of the ...
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2207
LLVM_ABI bool isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(const CallBase *Call, bool MustPreserveOffset)
{launder,strip}.invariant.group returns pointer that aliases its argument, and it only captures point...
LLVM_ABI bool mustSuppressSpeculation(const LoadInst &LI)
Return true if speculation of the given load must be suppressed to avoid ordering or interfering with...
Definition Loads.cpp:453
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition MathExtras.h:284
gep_type_iterator gep_type_end(const User *GEP)
LLVM_ABI const Value * getArgumentAliasingToReturnedPointer(const CallBase *Call, bool MustPreserveOffset)
This function returns call pointer argument that is considered the same by aliasing rules.
int ilogb(const APFloat &Arg)
Returns the exponent of the internal representation of the APFloat.
Definition APFloat.h:1631
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
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:337
LLVM_ABI bool canIgnoreSignBitOfZero(const Use &U)
Return true if the sign bit of the FP value can be ignored by the user when the value is zero.
LLVM_ABI bool isGuaranteedNotToBeUndef(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be undef, but may be poison.
LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
std::tuple< Value *, FPClassTest, FPClassTest > fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS, FPClassTest RHSClass, bool LookThroughSrc=true)
const Value * getPointerOperand(const Value *V)
A helper function that returns the pointer operand of a load, store or GEP instruction.
LLVM_ABI bool MaskedValueIsZero(const Value *V, const APInt &Mask, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if 'V & Mask' is known to be zero.
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:204
LLVM_ABI bool isOverflowIntrinsicNoWrap(const WithOverflowInst *WO, const DominatorTree &DT)
Returns true if the arithmetic part of the WO 's result is used only along the paths control dependen...
constexpr T MinAlign(U A, V B)
A and B are either alignments or offsets.
Definition MathExtras.h:357
LLVM_ABI bool matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO, Value *&Start, Value *&Step)
Attempt to match a simple first order recurrence cycle of the form: iv = phi Ty [Start,...
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1745
LLVM_ABI OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ, bool IsNSW=false)
LLVM_ABI bool getShuffleDemandedElts(int SrcWidth, ArrayRef< int > Mask, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS, bool AllowUndefElts=false)
Transform a shuffle mask's output demanded element mask into demanded element masks for the 2 operand...
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:331
LLVM_ABI bool isGuard(const User *U)
Returns true iff U has semantics of a guard expressed in a form of call of llvm.experimental....
LLVM_ABI SelectPatternFlavor getInverseMinMaxFlavor(SelectPatternFlavor SPF)
Return the inverse minimum/maximum flavor of the specified flavor.
constexpr unsigned MaxAnalysisRecursionDepth
LLVM_ABI void adjustKnownBitsForSelectArm(KnownBits &Known, Value *Cond, Value *Arm, bool Invert, const SimplifyQuery &Q, unsigned Depth=0)
Adjust Known for the given select Arm to include information from the select Cond.
LLVM_ABI bool isKnownNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be negative (i.e.
LLVM_ABI OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
SelectPatternFlavor
Specific patterns of select instructions we can match.
@ SPF_ABS
Floating point maxnum.
@ SPF_NABS
Absolute value.
@ SPF_FMAXNUM
Floating point minnum.
@ SPF_UMIN
Signed minimum.
@ SPF_UMAX
Signed maximum.
@ SPF_SMAX
Unsigned minimum.
@ SPF_UNKNOWN
@ SPF_FMINNUM
Unsigned maximum.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:279
LLVM_ABI bool impliesPoison(const Value *ValAssumedPoison, const Value *V)
Return true if V is poison given that ValAssumedPoison is already poison.
LLVM_ABI void getHorizDemandedEltsForFirstOperand(unsigned VectorBitWidth, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS)
Compute the demanded elements mask of horizontal binary operations.
LLVM_ABI SelectPatternResult getSelectPattern(CmpInst::Predicate Pred, SelectPatternNaNBehavior NaNBehavior=SPNB_NA, bool Ordered=false)
Determine the pattern for predicate X Pred Y ? X : Y.
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI bool programUndefinedIfPoison(const Instruction *Inst)
LLVM_ABI SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
LLVM_ABI bool matchSimpleBinaryIntrinsicRecurrence(const IntrinsicInst *I, PHINode *&P, Value *&Init, Value *&OtherOp)
Attempt to match a simple value-accumulating recurrence of the form: llvm.intrinsic....
LLVM_ABI bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
LLVM_ABI bool cannotBeNegativeZero(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if we can prove that the specified FP value is never equal to -0.0.
LLVM_ABI bool programUndefinedIfUndefOrPoison(const Instruction *Inst)
Return true if this function can prove that if Inst is executed and yields a poison value or undef bi...
LLVM_ABI void adjustKnownFPClassForSelectArm(KnownFPClass &Known, Value *Cond, Value *Arm, bool Invert, const SimplifyQuery &Q, unsigned Depth=0)
Adjust Known for the given select Arm to include information from the select Cond.
generic_gep_type_iterator<> gep_type_iterator
LLVM_ABI bool collectPossibleValues(const Value *V, SmallPtrSetImpl< const Constant * > &Constants, unsigned MaxCount, bool AllowUndefOrPoison=true)
Enumerates all possible immediate values of V and inserts them into the set Constants.
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
LLVM_ABI uint64_t GetStringLength(const Value *V, unsigned CharSize=8)
If we can compute the length of the string pointed to by the specified pointer, return 'len+1'.
LLVM_ABI OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
LLVM_ABI ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
LLVM_ABI Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
LLVM_ABI bool canCreateUndefOrPoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
canCreateUndefOrPoison returns true if Op can create undef or poison from non-undef & non-poison oper...
LLVM_ABI bool matchSimpleTernaryIntrinsicRecurrence(const IntrinsicInst *I, PHINode *&P, Value *&Init, Value *&OtherOp0, Value *&OtherOp1)
Attempt to match a simple value-accumulating recurrence of the form: llvm.intrinsic....
LLVM_ABI EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
LLVM_ABI bool isKnownInversion(const Value *X, const Value *Y)
Return true iff:
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI bool intrinsicPropagatesPoison(Intrinsic::ID IID)
Return whether this intrinsic propagates poison for all operands.
LLVM_ABI bool isNotCrossLaneOperation(const Instruction *I)
Return true if the instruction doesn't potentially cross vector lanes.
bool includesPoison(UndefPoisonKind Kind)
Returns true if Kind includes the Poison bit.
Definition UndefPoison.h:27
LLVM_ABI bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth=0)
Return true if the given value is known to be non-zero when defined.
constexpr int PoisonMaskElem
LLVM_ABI RetainedKnowledge getKnowledgeValidInContext(const Value *V, ArrayRef< Attribute::AttrKind > AttrKinds, AssumptionCache &AC, const Instruction *CtxI, const DominatorTree *DT=nullptr)
Return a valid Knowledge associated to the Value V if its Attribute kind is in AttrKinds and the know...
LLVM_ABI bool isSafeToSpeculativelyExecuteWithOpcode(unsigned Opcode, const Instruction *Inst, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true, bool IgnoreUBImplyingAttrs=true)
This returns the same result as isSafeToSpeculativelyExecute if Opcode is the actual opcode of Inst.
LLVM_ABI bool onlyUsedByLifetimeMarkers(const Value *V)
Return true if the only users of this pointer are lifetime markers.
LLVM_ABI Intrinsic::ID getIntrinsicForCallSite(const CallBase &CB, const TargetLibraryInfo *TLI)
Map a call instruction to an intrinsic ID.
@ Other
Any other memory.
Definition ModRef.h:68
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
LLVM_ABI const Value * getUnderlyingObjectAggressive(const Value *V)
Like getUnderlyingObject(), but will try harder to find a single underlying object.
LLVM_ABI Intrinsic::ID getMinMaxIntrinsic(SelectPatternFlavor SPF)
Convert given SPF to equivalent min/max intrinsic.
LLVM_ABI SelectPatternResult matchDecomposedSelectPattern(CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, FastMathFlags FMF=FastMathFlags(), Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Determine the pattern that a select with the given compare as its predicate and given values as its t...
bool includesUndef(UndefPoisonKind Kind)
Returns true if Kind includes the Undef bit.
Definition UndefPoison.h:33
LLVM_ABI OverflowResult computeOverflowForSignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
LLVM_ABI bool propagatesPoison(const Use &PoisonOp)
Return true if PoisonOp's user yields poison or raises UB if its operand PoisonOp is poison.
LLVM_ABI AssumeNonNullInfo getAssumeNonNullInfo(OperandBundleUse)
@ Add
Sum of integers.
LLVM_ABI ConstantRange computeConstantRangeIncludingKnownBits(const WithCache< const Value * > &V, bool ForSigned, const SimplifyQuery &SQ)
Combine constant ranges from computeConstantRange() and computeKnownBits().
SelectPatternNaNBehavior
Behavior when a floating point min/max is given one NaN and one non-NaN as input.
@ SPNB_RETURNS_NAN
NaN behavior not applicable.
@ SPNB_RETURNS_OTHER
Given one NaN input, returns the NaN.
@ SPNB_RETURNS_ANY
Given one NaN input, returns the non-NaN.
LLVM_ABI bool isKnownNonEqual(const Value *V1, const Value *V2, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the given values are known to be non-equal when defined.
DWARFExpression::Operation Op
LLVM_ABI bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Return the number of times the sign bit of the register is replicated into the other bits.
constexpr unsigned BitWidth
LLVM_ABI KnownBits analyzeKnownBitsFromAndXorOr(const Operator *I, const KnownBits &KnownLHS, const KnownBits &KnownRHS, const SimplifyQuery &SQ, unsigned Depth=0)
Using KnownBits LHS/RHS produce the known bits for logic op (and/xor/or).
LLVM_ABI OverflowResult computeOverflowForUnsignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
LLVM_ABI bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I)
Return true if this function can prove that the instruction I will always transfer execution to one o...
LLVM_ABI bool isKnownNeverInfOrNaN(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point value can never contain a NaN or infinity.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI bool isKnownNeverNaN(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point scalar value is not a NaN or if the floating-point vector value has...
gep_type_iterator gep_type_begin(const User *GEP)
UndefPoisonKind
Enumeration to track whether we are interested in Undef, Poison, or both.
Definition UndefPoison.h:20
LLVM_ABI Value * isBytewiseValue(Value *V, const DataLayout &DL)
If the specified value can be set by repeating the same byte in memory, return the i8 value that it i...
LLVM_ABI std::optional< std::pair< CmpPredicate, Constant * > > getFlippedStrictnessPredicateAndConstant(CmpPredicate Pred, Constant *C)
Convert an integer comparison with a constant RHS into an equivalent form with the strictness flipped...
LLVM_ABI unsigned ComputeMaxSignificantBits(const Value *Op, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Get the upper bound on bit size for this Value Op as a signed integer.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1946
LLVM_ABI bool isKnownIntegral(const Value *V, const SimplifyQuery &SQ, FastMathFlags FMF)
Return true if the floating-point value V is known to be an integer value.
LLVM_ABI AssumeAlignInfo getAssumeAlignInfo(OperandBundleUse)
LLVM_ABI OverflowResult computeOverflowForUnsignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:197
LLVM_ABI bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Return true if the given value is known to have exactly one bit set when defined.
LLVM_ABI std::optional< bool > isImpliedByDomCondition(const Value *Cond, const Instruction *ContextI, const DataLayout &DL)
Return the boolean condition value in the context of the given instruction if it is known based on do...
LLVM_ABI bool isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be poison, but may be undef.
LLVM_ABI void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, KnownBits &Known)
Compute known bits from the range metadata.
LLVM_ABI Value * FindInsertedValue(Value *V, ArrayRef< unsigned > idx_range, std::optional< BasicBlock::iterator > InsertBefore=std::nullopt)
Given an aggregate and an sequence of indices, see if the scalar value indexed is already around as a...
LLVM_ABI bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW=false, bool AllowPoison=true)
Return true if the two given values are negation.
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
LLVM_ABI bool isKnownPositive(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be positive (i.e.
LLVM_ABI Constant * ConstantFoldIntegerCast(Constant *C, Type *DestTy, bool IsSigned, const DataLayout &DL)
Constant fold a zext, sext or trunc, depending on IsSigned and whether the DestTy is wider or narrowe...
LLVM_ABI AssumeDereferenceableInfo getAssumeDereferenceableInfo(OperandBundleUse)
LLVM_ABI bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the give value is known to be non-negative.
LLVM_ABI bool cannotBeOrderedLessThanZero(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if we can prove that the specified FP value is either NaN or never less than -0....
LLVM_ABI void getUnderlyingObjects(const Value *V, SmallVectorImpl< const Value * > &Objects, const LoopInfo *LI=nullptr, unsigned MaxLookup=MaxLookupSearchDepth)
This method is similar to getUnderlyingObject except that it can look through phi and select instruct...
LLVM_ABI bool mayHaveNonDefUseDependency(const Instruction &I)
Returns true if the result or effects of the given instructions I depend values not reachable through...
LLVM_ABI bool isTriviallyVectorizable(Intrinsic::ID ID)
Identify if the intrinsic is trivially vectorizable.
LLVM_ABI bool isIdentifiedObject(const Value *V)
Return true if this pointer refers to a distinct and identifiable object.
LLVM_ABI std::optional< bool > isImpliedCondition(const Value *LHS, const Value *RHS, const DataLayout &DL, bool LHSIsTrue=true, unsigned Depth=0)
Return true if RHS is known to be implied true by LHS.
LLVM_ABI std::optional< bool > computeKnownFPSignBit(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return false if we can prove that the specified FP value's sign bit is 0.
LLVM_ABI bool canIgnoreSignBitOfNaN(const Use &U)
Return true if the sign bit of the FP value can be ignored by the user when the value is NaN.
LLVM_ABI ConstantRange computeConstantRange(const Value *V, bool ForSigned, const SimplifyQuery &SQ, unsigned Depth=0)
Determine the possible constant range of an integer or vector of integer value.
LLVM_ABI void findValuesAffectedByCondition(Value *Cond, bool IsAssume, function_ref< void(Value *)> InsertAffected)
Call InsertAffected on all Values whose known bits / value may be affected by the condition Cond.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:862
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
SmallPtrSet< Value *, 4 > AffectedValues
Represents offset+length into a ConstantDataArray.
const ConstantDataArray * Array
ConstantDataArray pointer.
Represent subnormal handling kind for floating point instruction inputs and outputs.
static constexpr DenormalMode getDynamic()
InstrInfoQuery provides an interface to query additional information for instructions like metadata o...
bool isExact(const BinaryOperator *Op) const
MDNode * getMetadata(const Instruction *I, unsigned KindID) const
bool hasNoSignedZeros(const InstT *Op) const
bool hasNoSignedWrap(const InstT *Op) const
bool hasNoUnsignedWrap(const InstT *Op) const
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition KnownBits.h:315
static LLVM_ABI KnownBits sadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)
KnownBits anyextOrTrunc(unsigned BitWidth) const
Return known bits for an "any" extension or truncation of the value we're tracking.
Definition KnownBits.h:190
static LLVM_ABI KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition KnownBits.h:269
static LLVM_ABI KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition KnownBits.h:106
bool isZero() const
Returns true if value is all zero.
Definition KnownBits.h:78
LLVM_ABI KnownBits blsi() const
Compute known bits for X & -X, which has only the lowest bit set of X set.
void makeNonNegative()
Make this value non-negative.
Definition KnownBits.h:125
static LLVM_ABI KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
unsigned countMinLeadingOnes() const
Returns the minimum number of leading one bits.
Definition KnownBits.h:265
LLVM_ABI KnownBits reduceAdd(unsigned NumElts) const
Compute known bits for horizontal add for a vector with NumElts elements, where each element has the ...
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...
void makeNegative()
Make this value negative.
Definition KnownBits.h:120
void setAllConflict()
Make all bits known to be both zero and one.
Definition KnownBits.h:97
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition KnownBits.h:165
KnownBits byteSwap() const
Definition KnownBits.h:553
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:557
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
void resetAll()
Resets the known state of all bits.
Definition KnownBits.h:72
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
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition KnownBits.h:325
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition KnownBits.h:184
unsigned countMinTrailingOnes() const
Returns the minimum number of trailing one bits.
Definition KnownBits.h:259
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition KnownBits.h:200
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition KnownBits.h:262
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:146
static LLVM_ABI KnownBits fshr(const KnownBits &LHS, const KnownBits &RHS, const APInt &Amt)
Compute known bits for fshr(LHS, RHS, Amt).
static LLVM_ABI KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
static LLVM_ABI KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
static LLVM_ABI KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
static LLVM_ABI KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
APInt getMinValue() const
Return the minimal unsigned value possible given these KnownBits.
Definition KnownBits.h:130
static LLVM_ABI KnownBits computeForAddSub(bool Add, bool NSW, bool NUW, const KnownBits &LHS, const KnownBits &RHS)
Compute known bits resulting from adding LHS and RHS.
Definition KnownBits.cpp:61
static LLVM_ABI KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition KnownBits.h:340
bool isNegative() const
Returns true if this value is known to be negative.
Definition KnownBits.h:103
static KnownBits sub(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from subtraction of LHS and RHS.
Definition KnownBits.h:376
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition KnownBits.h:294
void setAllOnes()
Make all bits known to be one and discard any previous information.
Definition KnownBits.h:90
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition KnownBits.h:233
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).
KnownBits sextOrTrunc(unsigned BitWidth) const
Return known bits for a sign extension or truncation of the value we're tracking.
Definition KnownBits.h:210
bool isKnownNeverInfOrNaN() const
Return true if it's known this can never be an infinity or nan.
FPClassTest KnownFPClasses
Floating-point classes the value could be one of.
bool isKnownNeverInfinity() const
Return true if it's known this can never be an infinity.
bool cannotBeOrderedGreaterThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never greater tha...
static LLVM_ABI KnownFPClass sin(const KnownFPClass &Src)
Report known values for sin.
static LLVM_ABI KnownFPClass fdiv_self(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fdiv x, x.
static constexpr FPClassTest OrderedGreaterThanZeroMask
static constexpr FPClassTest OrderedLessThanZeroMask
void knownNot(FPClassTest RuleOut)
static LLVM_ABI KnownFPClass fmul(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fmul.
static LLVM_ABI KnownFPClass fadd_self(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fadd x, x.
void copysign(const KnownFPClass &Sign)
static KnownFPClass square(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
static LLVM_ABI KnownFPClass fsub(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fsub.
bool isKnownNeverSubnormal() const
Return true if it's known this can never be a subnormal.
KnownFPClass unionWith(const KnownFPClass &RHS) const
static LLVM_ABI KnownFPClass canonicalize(const KnownFPClass &Src, DenormalMode DenormMode=DenormalMode::getDynamic())
Apply the canonicalize intrinsic to this value.
LLVM_ABI bool isKnownNeverLogicalZero(DenormalMode Mode) const
Return true if it's known this can never be interpreted as a zero.
static LLVM_ABI KnownFPClass log(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for log/log2/log10.
static LLVM_ABI KnownFPClass atan(const KnownFPClass &Src)
Report known values for atan.
static LLVM_ABI KnownFPClass atan2(const KnownFPClass &LHS, const KnownFPClass &RHS)
Report known values for atan2.
static LLVM_ABI KnownFPClass fdiv(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fdiv.
static LLVM_ABI KnownFPClass roundToIntegral(const KnownFPClass &Src, bool IsTrunc, bool IsMultiUnitFPType)
Propagate known class for rounding intrinsics (trunc, floor, ceil, rint, nearbyint,...
static LLVM_ABI KnownFPClass cos(const KnownFPClass &Src)
Report known values for cos.
static LLVM_ABI KnownFPClass ldexp(const KnownFPClass &Src, const KnownBits &N, const fltSemantics &Flt, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for ldexp.
static LLVM_ABI KnownFPClass cosh(const KnownFPClass &Src)
Report known values for cosh.
static LLVM_ABI KnownFPClass minMaxLike(const KnownFPClass &LHS, const KnownFPClass &RHS, MinMaxKind Kind, DenormalMode DenormMode=DenormalMode::getDynamic())
bool isUnknown() const
KnownFPClass intersectWith(const KnownFPClass &RHS) const
static LLVM_ABI KnownFPClass exp(const KnownFPClass &Src)
Report known values for exp, exp2 and exp10.
static LLVM_ABI KnownFPClass frexp_mant(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for mantissa component of frexp.
std::optional< bool > SignBit
std::nullopt if the sign bit is unknown, true if the sign bit is definitely set or false if the sign ...
static LLVM_ABI KnownFPClass asin(const KnownFPClass &Src)
Report known values for asin.
bool isKnownNeverNaN() const
Return true if it's known this can never be a nan.
bool isKnownNever(FPClassTest Mask) const
Return true if it's known this can never be one of the mask entries.
static LLVM_ABI KnownFPClass fpext(const KnownFPClass &KnownSrc, const fltSemantics &DstTy, const fltSemantics &SrcTy)
Propagate known class for fpext.
bool isKnownNeverNegZero() const
Return true if it's known this can never be a negative zero.
static LLVM_ABI KnownFPClass fma(const KnownFPClass &LHS, const KnownFPClass &RHS, const KnownFPClass &Addend, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fma.
static LLVM_ABI KnownFPClass tan(const KnownFPClass &Src)
Report known values for tan.
void propagateNaN(const KnownFPClass &Src, bool PreserveSign=false)
static LLVM_ABI KnownFPClass fptrunc(const KnownFPClass &KnownSrc)
Propagate known class for fptrunc.
bool cannotBeOrderedLessThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never less than -...
void signBitMustBeOne()
Assume the sign bit is one.
void signBitMustBeZero()
Assume the sign bit is zero.
static LLVM_ABI KnownFPClass sqrt(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for sqrt.
LLVM_ABI bool isKnownNeverLogicalPosZero(DenormalMode Mode) const
Return true if it's known this can never be interpreted as a positive zero.
bool isKnownNeverPosInfinity() const
Return true if it's known this can never be +infinity.
static LLVM_ABI KnownFPClass fadd(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fadd.
LLVM_ABI bool isKnownNeverLogicalNegZero(DenormalMode Mode) const
Return true if it's known this can never be interpreted as a negative zero.
static LLVM_ABI KnownFPClass bitcast(const fltSemantics &FltSemantics, const KnownBits &Bits)
Report known values for a bitcast into a float with provided semantics.
static LLVM_ABI KnownFPClass fma_square(const KnownFPClass &Squared, const KnownFPClass &Addend, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fma squared, squared, addend.
static LLVM_ABI KnownFPClass acos(const KnownFPClass &Src)
Report known values for acos.
static LLVM_ABI KnownFPClass frem_self(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for frem.
static LLVM_ABI KnownFPClass powi(const KnownFPClass &Src, const KnownBits &N)
Propagate known class for powi.
static LLVM_ABI KnownFPClass sinh(const KnownFPClass &Src)
Report known values for sinh.
static LLVM_ABI KnownFPClass tanh(const KnownFPClass &Src)
Report known values for tanh.
SelectPatternFlavor Flavor
static bool isMinOrMax(SelectPatternFlavor SPF)
When implementing this min/max pattern as fcmp; select, does the fcmp have to be ordered?
const DataLayout & DL
SimplifyQuery getWithoutCondContext() const
const Instruction * CxtI
const DominatorTree * DT
SimplifyQuery getWithInstruction(const Instruction *I) const
AssumptionCache * AC
const DomConditionCache * DC
const InstrInfoQuery IIQ
const CondContext * CC