LLVM 23.0.0git
ValueTracking.cpp
Go to the documentation of this file.
1//===- ValueTracking.cpp - Walk computations to compute properties --------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains routines that help analyze properties that chains of
10// computations have.
11//
12//===----------------------------------------------------------------------===//
13
15#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/ScopeExit.h"
23#include "llvm/ADT/StringRef.h"
33#include "llvm/Analysis/Loads.h"
38#include "llvm/IR/Argument.h"
39#include "llvm/IR/Attributes.h"
40#include "llvm/IR/BasicBlock.h"
42#include "llvm/IR/Constant.h"
45#include "llvm/IR/Constants.h"
48#include "llvm/IR/Dominators.h"
50#include "llvm/IR/Function.h"
52#include "llvm/IR/GlobalAlias.h"
53#include "llvm/IR/GlobalValue.h"
55#include "llvm/IR/InstrTypes.h"
56#include "llvm/IR/Instruction.h"
59#include "llvm/IR/Intrinsics.h"
60#include "llvm/IR/IntrinsicsAArch64.h"
61#include "llvm/IR/IntrinsicsAMDGPU.h"
62#include "llvm/IR/IntrinsicsRISCV.h"
63#include "llvm/IR/IntrinsicsX86.h"
64#include "llvm/IR/LLVMContext.h"
65#include "llvm/IR/Metadata.h"
66#include "llvm/IR/Module.h"
67#include "llvm/IR/Operator.h"
69#include "llvm/IR/Type.h"
70#include "llvm/IR/User.h"
71#include "llvm/IR/Value.h"
81#include <algorithm>
82#include <cassert>
83#include <cstdint>
84#include <optional>
85#include <utility>
86
87using namespace llvm;
88using namespace llvm::PatternMatch;
89
90// Controls the number of uses of the value searched for possible
91// dominating comparisons.
92static cl::opt<unsigned> DomConditionsMaxUses("dom-conditions-max-uses",
93 cl::Hidden, cl::init(20));
94
95/// Maximum number of instructions to check between assume and context
96/// instruction.
97static constexpr unsigned MaxInstrsToCheckForFree = 32;
98
99/// Returns the bitwidth of the given scalar or pointer type. For vector types,
100/// returns the element type's bitwidth.
101static unsigned getBitWidth(Type *Ty, const DataLayout &DL) {
102 if (unsigned BitWidth = Ty->getScalarSizeInBits())
103 return BitWidth;
104
105 return DL.getPointerTypeSizeInBits(Ty);
106}
107
108// Given the provided Value and, potentially, a context instruction, return
109// the preferred context instruction (if any).
110static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI) {
111 // If we've been provided with a context instruction, then use that (provided
112 // it has been inserted).
113 if (CxtI && CxtI->getParent())
114 return CxtI;
115
116 // If the value is really an already-inserted instruction, then use that.
117 CxtI = dyn_cast<Instruction>(V);
118 if (CxtI && CxtI->getParent())
119 return CxtI;
120
121 return nullptr;
122}
123
125 const APInt &DemandedElts,
126 APInt &DemandedLHS, APInt &DemandedRHS) {
127 if (isa<ScalableVectorType>(Shuf->getType())) {
128 assert(DemandedElts == APInt(1,1));
129 DemandedLHS = DemandedRHS = DemandedElts;
130 return true;
131 }
132
133 int NumElts =
134 cast<FixedVectorType>(Shuf->getOperand(0)->getType())->getNumElements();
135 return llvm::getShuffleDemandedElts(NumElts, Shuf->getShuffleMask(),
136 DemandedElts, DemandedLHS, DemandedRHS);
137}
138
139static void computeKnownBits(const Value *V, const APInt &DemandedElts,
140 KnownBits &Known, const SimplifyQuery &Q,
141 unsigned Depth);
142
144 const SimplifyQuery &Q, unsigned Depth) {
145 // Since the number of lanes in a scalable vector is unknown at compile time,
146 // we track one bit which is implicitly broadcast to all lanes. This means
147 // that all lanes in a scalable vector are considered demanded.
148 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
149 APInt DemandedElts =
150 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
151 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);
152}
153
155 const DataLayout &DL, AssumptionCache *AC,
156 const Instruction *CxtI, const DominatorTree *DT,
157 bool UseInstrInfo, unsigned Depth) {
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 unsigned NumChecked = 0;
709 auto hasNoFreeInRange = [&NumChecked](auto Range) {
710 for (const Instruction &I : Range) {
711 if (NumChecked++ > MaxInstrsToCheckForFree)
712 return false;
713
714 if (auto *CB = dyn_cast<CallBase>(&I)) {
715 if (!CB->hasFnAttr(Attribute::NoFree))
716 return false;
717 } else if (I.maySynchronize())
718 return false;
719 }
720 return true;
721 };
722
723 const BasicBlock *CtxBB = CtxI->getParent();
724 const BasicBlock *AssumeBB = Assume->getParent();
725 BasicBlock::const_iterator CtxIter = CtxI->getIterator();
726 if (CtxBB == AssumeBB) {
727 // Same block case: check that Assume comes before CtxI.
728 if (Assume != CtxI && !Assume->comesBefore(CtxI))
729 return false;
730 return hasNoFreeInRange(make_range(Assume->getIterator(), CtxIter));
731 }
732
733 // Handle chain of single-predecessor blocks.
734 const BasicBlock *CurBB = CtxBB;
735 while (true) {
736 if (CurBB == AssumeBB)
737 return hasNoFreeInRange(
738 make_range(Assume->getIterator(), AssumeBB->end()));
739
740 const BasicBlock *PredBB = CurBB->getSinglePredecessor();
741 if (!PredBB)
742 return false;
743
744 if (!hasNoFreeInRange(make_range(CurBB->begin(),
745 CurBB == CtxBB ? CtxIter : CurBB->end())))
746 return false;
747 CurBB = PredBB;
748 }
749}
750
751// TODO: cmpExcludesZero misses many cases where `RHS` is non-constant but
752// we still have enough information about `RHS` to conclude non-zero. For
753// example Pred=EQ, RHS=isKnownNonZero. cmpExcludesZero is called in loops
754// so the extra compile time may not be worth it, but possibly a second API
755// should be created for use outside of loops.
756static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS) {
757 // v u> y implies v != 0.
758 if (Pred == ICmpInst::ICMP_UGT)
759 return true;
760
761 // Special-case v != 0 to also handle v != null.
762 if (Pred == ICmpInst::ICMP_NE)
763 return match(RHS, m_Zero());
764
765 // All other predicates - rely on generic ConstantRange handling.
766 const APInt *C;
767 auto Zero = APInt::getZero(RHS->getType()->getScalarSizeInBits());
768 if (match(RHS, m_APInt(C))) {
770 return !TrueValues.contains(Zero);
771 }
772
774 if (VC == nullptr)
775 return false;
776
777 for (unsigned ElemIdx = 0, NElem = VC->getNumElements(); ElemIdx < NElem;
778 ++ElemIdx) {
780 Pred, VC->getElementAsAPInt(ElemIdx));
781 if (TrueValues.contains(Zero))
782 return false;
783 }
784 return true;
785}
786
787static void breakSelfRecursivePHI(const Use *U, const PHINode *PHI,
788 Value *&ValOut, Instruction *&CtxIOut,
789 const PHINode **PhiOut = nullptr) {
790 ValOut = U->get();
791 if (ValOut == PHI)
792 return;
793 CtxIOut = PHI->getIncomingBlock(*U)->getTerminator();
794 if (PhiOut)
795 *PhiOut = PHI;
796 Value *V;
797 // If the Use is a select of this phi, compute analysis on other arm to break
798 // recursion.
799 // TODO: Min/Max
800 if (match(ValOut, m_Select(m_Value(), m_Specific(PHI), m_Value(V))) ||
801 match(ValOut, m_Select(m_Value(), m_Value(V), m_Specific(PHI))))
802 ValOut = V;
803
804 // Same for select, if this phi is 2-operand phi, compute analysis on other
805 // incoming value to break recursion.
806 // TODO: We could handle any number of incoming edges as long as we only have
807 // two unique values.
808 if (auto *IncPhi = dyn_cast<PHINode>(ValOut);
809 IncPhi && IncPhi->getNumIncomingValues() == 2) {
810 for (int Idx = 0; Idx < 2; ++Idx) {
811 if (IncPhi->getIncomingValue(Idx) == PHI) {
812 ValOut = IncPhi->getIncomingValue(1 - Idx);
813 if (PhiOut)
814 *PhiOut = IncPhi;
815 CtxIOut = IncPhi->getIncomingBlock(1 - Idx)->getTerminator();
816 break;
817 }
818 }
819 }
820}
821
822static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q) {
823 // Use of assumptions is context-sensitive. If we don't have a context, we
824 // cannot use them!
825 if (!Q.AC || !Q.CxtI)
826 return false;
827
828 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
829 if (!Elem.Assume)
830 continue;
831
832 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
833 assert(I->getFunction() == Q.CxtI->getFunction() &&
834 "Got assumption for the wrong function!");
835
836 if (Elem.Index != AssumptionCache::ExprResultIdx) {
837 bool AssumeImpliesNonNull = [&]() {
838 auto OBU = I->getOperandBundleAt(Elem.Index);
839 switch (getBundleAttrFromOBU(OBU)) {
840 case BundleAttr::Dereferenceable: {
841 auto [Ptr, _, Count] = getAssumeDereferenceableInfo(OBU);
842 return Ptr == V && Count && *Count != 0 &&
845 }
846
847 case BundleAttr::NonNull:
848 return getAssumeNonNullInfo(OBU).Ptr == V;
849
850 default:
851 return false;
852 }
853 }();
854 if (AssumeImpliesNonNull && isValidAssumeForContext(I, Q))
855 return true;
856 continue;
857 }
858
859 // Warning: This loop can end up being somewhat performance sensitive.
860 // We're running this loop for once for each value queried resulting in a
861 // runtime of ~O(#assumes * #values).
862
863 Value *RHS;
864 CmpPredicate Pred;
865 auto m_V = m_CombineOr(m_Specific(V), m_PtrToInt(m_Specific(V)));
866 if (!match(I->getArgOperand(0), m_c_ICmp(Pred, m_V, m_Value(RHS))))
867 continue;
868
870 return true;
871 }
872
873 return false;
874}
875
877 Value *LHS, Value *RHS, KnownBits &Known,
878 const SimplifyQuery &Q) {
879 if (RHS->getType()->isPointerTy()) {
880 // Handle comparison of pointer to null explicitly, as it will not be
881 // covered by the m_APInt() logic below.
882 if (LHS == V && match(RHS, m_Zero())) {
883 switch (Pred) {
885 Known.setAllZero();
886 break;
889 Known.makeNonNegative();
890 break;
892 Known.makeNegative();
893 break;
894 default:
895 break;
896 }
897 }
898 return;
899 }
900
901 unsigned BitWidth = Known.getBitWidth();
902 auto m_V =
904
905 Value *Y;
906 const APInt *Mask, *C;
907 if (!match(RHS, m_APInt(C)))
908 return;
909
910 uint64_t ShAmt;
911 switch (Pred) {
913 // assume(V = C)
914 if (match(LHS, m_V)) {
915 Known = Known.unionWith(KnownBits::makeConstant(*C));
916 // assume(V & Mask = C)
917 } else if (match(LHS, m_c_And(m_V, m_Value(Y)))) {
918 // For one bits in Mask, we can propagate bits from C to V.
919 Known.One |= *C;
920 if (match(Y, m_APInt(Mask)))
921 Known.Zero |= ~*C & *Mask;
922 // assume(V | Mask = C)
923 } else if (match(LHS, m_c_Or(m_V, m_Value(Y)))) {
924 // For zero bits in Mask, we can propagate bits from C to V.
925 Known.Zero |= ~*C;
926 if (match(Y, m_APInt(Mask)))
927 Known.One |= *C & ~*Mask;
928 // assume(V << ShAmt = C)
929 } else if (match(LHS, m_Shl(m_V, m_ConstantInt(ShAmt))) &&
930 ShAmt < BitWidth) {
931 // For those bits in C that are known, we can propagate them to known
932 // bits in V shifted to the right by ShAmt.
934 RHSKnown >>= ShAmt;
935 Known = Known.unionWith(RHSKnown);
936 // assume(V >> ShAmt = C)
937 } else if (match(LHS, m_Shr(m_V, m_ConstantInt(ShAmt))) &&
938 ShAmt < BitWidth) {
939 // For those bits in RHS that are known, we can propagate them to known
940 // bits in V shifted to the right by C.
942 RHSKnown <<= ShAmt;
943 Known = Known.unionWith(RHSKnown);
944 }
945 break;
946 case ICmpInst::ICMP_NE: {
947 // assume (V & B != 0) where B is a power of 2
948 const APInt *BPow2;
949 if (C->isZero() && match(LHS, m_And(m_V, m_Power2(BPow2))))
950 Known.One |= *BPow2;
951 break;
952 }
953 default: {
954 const APInt *Offset = nullptr;
955 if (match(LHS, m_CombineOr(m_V, m_AddLike(m_V, m_APInt(Offset))))) {
957 if (Offset)
958 LHSRange = LHSRange.sub(*Offset);
959 Known = Known.unionWith(LHSRange.toKnownBits());
960 }
961 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
962 // X & Y u> C -> X u> C && Y u> C
963 // X nuw- Y u> C -> X u> C
964 if (match(LHS, m_c_And(m_V, m_Value())) ||
965 match(LHS, m_NUWSub(m_V, m_Value())))
966 Known.One.setHighBits(
967 (*C + (Pred == ICmpInst::ICMP_UGT)).countLeadingOnes());
968 }
969 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
970 // X | Y u< C -> X u< C && Y u< C
971 // X nuw+ Y u< C -> X u< C && Y u< C
972 if (match(LHS, m_c_Or(m_V, m_Value())) ||
973 match(LHS, m_c_NUWAdd(m_V, m_Value()))) {
974 Known.Zero.setHighBits(
975 (*C - (Pred == ICmpInst::ICMP_ULT)).countLeadingZeros());
976 }
977 }
978 } break;
979 }
980}
981
982static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp,
983 KnownBits &Known,
984 const SimplifyQuery &SQ, bool Invert) {
986 Invert ? Cmp->getInversePredicate() : Cmp->getPredicate();
987 Value *LHS = Cmp->getOperand(0);
988 Value *RHS = Cmp->getOperand(1);
989
990 // Handle icmp pred (trunc V), C
991 if (match(LHS, m_Trunc(m_Specific(V)))) {
992 KnownBits DstKnown(LHS->getType()->getScalarSizeInBits());
993 computeKnownBitsFromCmp(LHS, Pred, LHS, RHS, DstKnown, SQ);
995 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
996 else
997 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
998 return;
999 }
1000
1001 computeKnownBitsFromCmp(V, Pred, LHS, RHS, Known, SQ);
1002}
1003
1005 KnownBits &Known, const SimplifyQuery &SQ,
1006 bool Invert, unsigned Depth) {
1007 Value *A, *B;
1010 KnownBits Known2(Known.getBitWidth());
1011 KnownBits Known3(Known.getBitWidth());
1012 computeKnownBitsFromCond(V, A, Known2, SQ, Invert, Depth + 1);
1013 computeKnownBitsFromCond(V, B, Known3, SQ, Invert, Depth + 1);
1014 if (Invert ? match(Cond, m_LogicalOr(m_Value(), m_Value()))
1016 Known2 = Known2.unionWith(Known3);
1017 else
1018 Known2 = Known2.intersectWith(Known3);
1019 Known = Known.unionWith(Known2);
1020 return;
1021 }
1022
1023 if (auto *Cmp = dyn_cast<ICmpInst>(Cond)) {
1024 computeKnownBitsFromICmpCond(V, Cmp, Known, SQ, Invert);
1025 return;
1026 }
1027
1028 if (match(Cond, m_Trunc(m_Specific(V)))) {
1029 KnownBits DstKnown(1);
1030 if (Invert) {
1031 DstKnown.setAllZero();
1032 } else {
1033 DstKnown.setAllOnes();
1034 }
1036 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
1037 return;
1038 }
1039 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
1040 return;
1041 }
1042
1044 computeKnownBitsFromCond(V, A, Known, SQ, !Invert, Depth + 1);
1045}
1046
1048 const SimplifyQuery &Q, unsigned Depth) {
1049 // Handle injected condition.
1050 if (Q.CC && Q.CC->AffectedValues.contains(V))
1051 computeKnownBitsFromCond(V, Q.CC->Cond, Known, Q, Q.CC->Invert, Depth);
1052
1053 if (!Q.CxtI)
1054 return;
1055
1056 if (Q.DC && Q.DT) {
1057 // Handle dominating conditions.
1058 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
1059 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
1060 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
1061 computeKnownBitsFromCond(V, BI->getCondition(), Known, Q,
1062 /*Invert*/ false, Depth);
1063
1064 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
1065 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
1066 computeKnownBitsFromCond(V, BI->getCondition(), Known, Q,
1067 /*Invert*/ true, Depth);
1068 }
1069
1070 if (Known.hasConflict())
1071 Known.resetAll();
1072 }
1073
1074 if (!Q.AC)
1075 return;
1076
1077 unsigned BitWidth = Known.getBitWidth();
1078
1079 // Note that the patterns below need to be kept in sync with the code
1080 // in AssumptionCache::updateAffectedValues.
1081
1082 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
1083 if (!Elem.Assume)
1084 continue;
1085
1086 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
1087 assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() &&
1088 "Got assumption for the wrong function!");
1089
1090 if (Elem.Index != AssumptionCache::ExprResultIdx) {
1091 if (auto OBU = I->getOperandBundleAt(Elem.Index);
1092 getBundleAttrFromOBU(OBU) == BundleAttr::Align) {
1093 auto [Ptr, _, Alignment, Offset] = getAssumeAlignInfo(OBU);
1094 if (Ptr != V || !Alignment || !Offset || !isPowerOf2_64(*Alignment))
1095 continue;
1096 auto AlignVal = MinAlign(*Offset, *Alignment);
1097 if (isValidAssumeForContext(I, Q))
1098 Known.Zero.setLowBits(Log2_64(AlignVal));
1099 }
1100 continue;
1101 }
1102
1103 // Warning: This loop can end up being somewhat performance sensitive.
1104 // We're running this loop for once for each value queried resulting in a
1105 // runtime of ~O(#assumes * #values).
1106
1107 Value *Arg = I->getArgOperand(0);
1108
1109 if (Arg == V && isValidAssumeForContext(I, Q)) {
1110 assert(BitWidth == 1 && "assume operand is not i1?");
1111 (void)BitWidth;
1112 Known.setAllOnes();
1113 return;
1114 }
1115 if (match(Arg, m_Not(m_Specific(V))) &&
1117 assert(BitWidth == 1 && "assume operand is not i1?");
1118 (void)BitWidth;
1119 Known.setAllZero();
1120 return;
1121 }
1122 auto *Trunc = dyn_cast<TruncInst>(Arg);
1123 if (Trunc && Trunc->getOperand(0) == V &&
1125 if (Trunc->hasNoUnsignedWrap()) {
1127 return;
1128 }
1129 Known.One.setBit(0);
1130 return;
1131 }
1132
1133 // The remaining tests are all recursive, so bail out if we hit the limit.
1135 continue;
1136
1137 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
1138 if (!Cmp)
1139 continue;
1140
1141 if (!isValidAssumeForContext(I, Q))
1142 continue;
1143
1144 computeKnownBitsFromICmpCond(V, Cmp, Known, Q, /*Invert=*/false);
1145 }
1146
1147 // Conflicting assumption: Undefined behavior will occur on this execution
1148 // path.
1149 if (Known.hasConflict())
1150 Known.resetAll();
1151}
1152
1153/// Compute known bits from a shift operator, including those with a
1154/// non-constant shift amount. Known is the output of this function. Known2 is a
1155/// pre-allocated temporary with the same bit width as Known and on return
1156/// contains the known bit of the shift value source. KF is an
1157/// operator-specific function that, given the known-bits and a shift amount,
1158/// compute the implied known-bits of the shift operator's result respectively
1159/// for that shift amount. The results from calling KF are conservatively
1160/// combined for all permitted shift amounts.
1162 const Operator *I, const APInt &DemandedElts, KnownBits &Known,
1163 KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth,
1164 function_ref<KnownBits(const KnownBits &, const KnownBits &, bool)> KF) {
1165 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1166 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1167 // To limit compile-time impact, only query isKnownNonZero() if we know at
1168 // least something about the shift amount.
1169 bool ShAmtNonZero =
1170 Known.isNonZero() ||
1171 (Known.getMaxValue().ult(Known.getBitWidth()) &&
1172 isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth + 1));
1173 Known = KF(Known2, Known, ShAmtNonZero);
1174}
1175
1176static KnownBits
1177getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts,
1178 const KnownBits &KnownLHS, const KnownBits &KnownRHS,
1179 const SimplifyQuery &Q, unsigned Depth) {
1180 unsigned BitWidth = KnownLHS.getBitWidth();
1181 KnownBits KnownOut(BitWidth);
1182 bool IsAnd = false;
1183 bool HasKnownOne = !KnownLHS.One.isZero() || !KnownRHS.One.isZero();
1184 Value *X = nullptr, *Y = nullptr;
1185
1186 switch (I->getOpcode()) {
1187 case Instruction::And:
1188 KnownOut = KnownLHS & KnownRHS;
1189 IsAnd = true;
1190 // and(x, -x) is common idioms that will clear all but lowest set
1191 // bit. If we have a single known bit in x, we can clear all bits
1192 // above it.
1193 // TODO: instcombine often reassociates independent `and` which can hide
1194 // this pattern. Try to match and(x, and(-x, y)) / and(and(x, y), -x).
1195 if (HasKnownOne && match(I, m_c_And(m_Value(X), m_Neg(m_Deferred(X))))) {
1196 // -(-x) == x so using whichever (LHS/RHS) gets us a better result.
1197 if (KnownLHS.countMaxTrailingZeros() <= KnownRHS.countMaxTrailingZeros())
1198 KnownOut = KnownLHS.blsi();
1199 else
1200 KnownOut = KnownRHS.blsi();
1201 }
1202 break;
1203 case Instruction::Or:
1204 KnownOut = KnownLHS | KnownRHS;
1205 break;
1206 case Instruction::Xor:
1207 KnownOut = KnownLHS ^ KnownRHS;
1208 // xor(x, x-1) is common idioms that will clear all but lowest set
1209 // bit. If we have a single known bit in x, we can clear all bits
1210 // above it.
1211 // TODO: xor(x, x-1) is often rewritting as xor(x, x-C) where C !=
1212 // -1 but for the purpose of demanded bits (xor(x, x-C) &
1213 // Demanded) == (xor(x, x-1) & Demanded). Extend the xor pattern
1214 // to use arbitrary C if xor(x, x-C) as the same as xor(x, x-1).
1215 if (HasKnownOne &&
1217 const KnownBits &XBits = I->getOperand(0) == X ? KnownLHS : KnownRHS;
1218 KnownOut = XBits.blsmsk();
1219 }
1220 break;
1221 default:
1222 llvm_unreachable("Invalid Op used in 'analyzeKnownBitsFromAndXorOr'");
1223 }
1224
1225 // and(x, add (x, -1)) is a common idiom that always clears the low bit;
1226 // xor/or(x, add (x, -1)) is an idiom that will always set the low bit.
1227 // here we handle the more general case of adding any odd number by
1228 // matching the form and/xor/or(x, add(x, y)) where y is odd.
1229 // TODO: This could be generalized to clearing any bit set in y where the
1230 // following bit is known to be unset in y.
1231 if (!KnownOut.Zero[0] && !KnownOut.One[0] &&
1235 KnownBits KnownY(BitWidth);
1236 computeKnownBits(Y, DemandedElts, KnownY, Q, Depth + 1);
1237 if (KnownY.countMinTrailingOnes() > 0) {
1238 if (IsAnd)
1239 KnownOut.Zero.setBit(0);
1240 else
1241 KnownOut.One.setBit(0);
1242 }
1243 }
1244 return KnownOut;
1245}
1246
1248 const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q,
1249 unsigned Depth,
1250 const function_ref<KnownBits(const KnownBits &, const KnownBits &)>
1251 KnownBitsFunc) {
1252 APInt DemandedEltsLHS, DemandedEltsRHS;
1254 DemandedElts, DemandedEltsLHS,
1255 DemandedEltsRHS);
1256
1257 const auto ComputeForSingleOpFunc =
1258 [Depth, &Q, KnownBitsFunc](const Value *Op, APInt &DemandedEltsOp) {
1259 return KnownBitsFunc(
1260 computeKnownBits(Op, DemandedEltsOp, Q, Depth + 1),
1261 computeKnownBits(Op, DemandedEltsOp << 1, Q, Depth + 1));
1262 };
1263
1264 if (DemandedEltsRHS.isZero())
1265 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS);
1266 if (DemandedEltsLHS.isZero())
1267 return ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS);
1268
1269 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS)
1270 .intersectWith(ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS));
1271}
1272
1273// Public so this can be used in `SimplifyDemandedUseBits`.
1275 const KnownBits &KnownLHS,
1276 const KnownBits &KnownRHS,
1277 const SimplifyQuery &SQ,
1278 unsigned Depth) {
1279 auto *FVTy = dyn_cast<FixedVectorType>(I->getType());
1280 APInt DemandedElts =
1281 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
1282
1283 return getKnownBitsFromAndXorOr(I, DemandedElts, KnownLHS, KnownRHS, SQ,
1284 Depth);
1285}
1286
1288 Attribute Attr = F->getFnAttribute(Attribute::VScaleRange);
1289 // Without vscale_range, we only know that vscale is non-zero.
1290 if (!Attr.isValid())
1292
1293 unsigned AttrMin = Attr.getVScaleRangeMin();
1294 // Minimum is larger than vscale width, result is always poison.
1295 if ((unsigned)llvm::bit_width(AttrMin) > BitWidth)
1296 return ConstantRange::getEmpty(BitWidth);
1297
1298 APInt Min(BitWidth, AttrMin);
1299 std::optional<unsigned> AttrMax = Attr.getVScaleRangeMax();
1300 if (!AttrMax || (unsigned)llvm::bit_width(*AttrMax) > BitWidth)
1302
1303 return ConstantRange(Min, APInt(BitWidth, *AttrMax) + 1);
1304}
1305
1307 Value *Arm, bool Invert,
1308 const SimplifyQuery &Q, unsigned Depth) {
1309 // If we have a constant arm, we are done.
1310 if (Known.isConstant())
1311 return;
1312
1313 // See what condition implies about the bits of the select arm.
1314 KnownBits CondRes(Known.getBitWidth());
1315 computeKnownBitsFromCond(Arm, Cond, CondRes, Q, Invert, Depth + 1);
1316 // If we don't get any information from the condition, no reason to
1317 // proceed.
1318 if (CondRes.isUnknown())
1319 return;
1320
1321 // We can have conflict if the condition is dead. I.e if we have
1322 // (x | 64) < 32 ? (x | 64) : y
1323 // we will have conflict at bit 6 from the condition/the `or`.
1324 // In that case just return. Its not particularly important
1325 // what we do, as this select is going to be simplified soon.
1326 CondRes = CondRes.unionWith(Known);
1327 if (CondRes.hasConflict())
1328 return;
1329
1330 // Finally make sure the information we found is valid. This is relatively
1331 // expensive so it's left for the very end.
1332 if (!isGuaranteedNotToBeUndef(Arm, Q.AC, Q.CxtI, Q.DT, Depth + 1))
1333 return;
1334
1335 // Finally, we know we get information from the condition and its valid,
1336 // so return it.
1337 Known = std::move(CondRes);
1338}
1339
1340// Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow).
1341// Returns the input and lower/upper bounds.
1342static bool isSignedMinMaxClamp(const Value *Select, const Value *&In,
1343 const APInt *&CLow, const APInt *&CHigh) {
1345 cast<Operator>(Select)->getOpcode() == Instruction::Select &&
1346 "Input should be a Select!");
1347
1348 const Value *LHS = nullptr, *RHS = nullptr;
1350 if (SPF != SPF_SMAX && SPF != SPF_SMIN)
1351 return false;
1352
1353 if (!match(RHS, m_APInt(CLow)))
1354 return false;
1355
1356 const Value *LHS2 = nullptr, *RHS2 = nullptr;
1358 if (getInverseMinMaxFlavor(SPF) != SPF2)
1359 return false;
1360
1361 if (!match(RHS2, m_APInt(CHigh)))
1362 return false;
1363
1364 if (SPF == SPF_SMIN)
1365 std::swap(CLow, CHigh);
1366
1367 In = LHS2;
1368 return CLow->sle(*CHigh);
1369}
1370
1372 const APInt *&CLow,
1373 const APInt *&CHigh) {
1374 assert((II->getIntrinsicID() == Intrinsic::smin ||
1375 II->getIntrinsicID() == Intrinsic::smax) &&
1376 "Must be smin/smax");
1377
1378 Intrinsic::ID InverseID = getInverseMinMaxIntrinsic(II->getIntrinsicID());
1379 auto *InnerII = dyn_cast<IntrinsicInst>(II->getArgOperand(0));
1380 if (!InnerII || InnerII->getIntrinsicID() != InverseID ||
1381 !match(II->getArgOperand(1), m_APInt(CLow)) ||
1382 !match(InnerII->getArgOperand(1), m_APInt(CHigh)))
1383 return false;
1384
1385 if (II->getIntrinsicID() == Intrinsic::smin)
1386 std::swap(CLow, CHigh);
1387 return CLow->sle(*CHigh);
1388}
1389
1391 KnownBits &Known) {
1392 const APInt *CLow, *CHigh;
1393 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
1394 Known = Known.unionWith(
1395 ConstantRange::getNonEmpty(*CLow, *CHigh + 1).toKnownBits());
1396}
1397
1399 const APInt &DemandedElts,
1400 KnownBits &Known,
1401 const SimplifyQuery &Q,
1402 unsigned Depth) {
1403 unsigned BitWidth = Known.getBitWidth();
1404
1405 KnownBits Known2(BitWidth);
1406 switch (I->getOpcode()) {
1407 default: break;
1408 case Instruction::Load:
1409 if (MDNode *MD =
1410 Q.IIQ.getMetadata(cast<LoadInst>(I), LLVMContext::MD_range))
1412 break;
1413 case Instruction::And:
1414 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1415 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1416
1417 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1418 break;
1419 case Instruction::Or:
1420 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1421 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1422
1423 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1424 break;
1425 case Instruction::Xor:
1426 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1427 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1428
1429 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1430 break;
1431 case Instruction::Mul: {
1434 computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, NUW,
1435 DemandedElts, Known, Known2, Q, Depth);
1436 break;
1437 }
1438 case Instruction::UDiv: {
1439 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1440 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1441 Known =
1442 KnownBits::udiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1443 break;
1444 }
1445 case Instruction::SDiv: {
1446 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1447 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1448 Known =
1449 KnownBits::sdiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1450 break;
1451 }
1452 case Instruction::Select: {
1453 auto ComputeForArm = [&](Value *Arm, bool Invert) {
1454 KnownBits Res(Known.getBitWidth());
1455 computeKnownBits(Arm, DemandedElts, Res, Q, Depth + 1);
1456 adjustKnownBitsForSelectArm(Res, I->getOperand(0), Arm, Invert, Q, Depth);
1457 return Res;
1458 };
1459 // Only known if known in both the LHS and RHS.
1460 Known =
1461 ComputeForArm(I->getOperand(1), /*Invert=*/false)
1462 .intersectWith(ComputeForArm(I->getOperand(2), /*Invert=*/true));
1463 break;
1464 }
1465 case Instruction::FPTrunc:
1466 case Instruction::FPExt:
1467 case Instruction::FPToUI:
1468 case Instruction::FPToSI:
1469 case Instruction::SIToFP:
1470 case Instruction::UIToFP:
1471 break; // Can't work with floating point.
1472 case Instruction::PtrToInt:
1473 case Instruction::PtrToAddr:
1474 case Instruction::IntToPtr:
1475 // Fall through and handle them the same as zext/trunc.
1476 [[fallthrough]];
1477 case Instruction::ZExt:
1478 case Instruction::Trunc: {
1479 Type *SrcTy = I->getOperand(0)->getType();
1480
1481 unsigned SrcBitWidth;
1482 // Note that we handle pointer operands here because of inttoptr/ptrtoint
1483 // which fall through here.
1484 Type *ScalarTy = SrcTy->getScalarType();
1485 SrcBitWidth = ScalarTy->isPointerTy() ?
1486 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
1487 Q.DL.getTypeSizeInBits(ScalarTy);
1488
1489 assert(SrcBitWidth && "SrcBitWidth can't be zero");
1490 Known = Known.anyextOrTrunc(SrcBitWidth);
1491 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1492 if (auto *Inst = dyn_cast<PossiblyNonNegInst>(I);
1493 Inst && Inst->hasNonNeg() && !Known.isNegative())
1494 Known.makeNonNegative();
1495 Known = Known.zextOrTrunc(BitWidth);
1496 break;
1497 }
1498 case Instruction::BitCast: {
1499 Type *SrcTy = I->getOperand(0)->getType();
1500 if (SrcTy->isIntOrPtrTy() &&
1501 // TODO: For now, not handling conversions like:
1502 // (bitcast i64 %x to <2 x i32>)
1503 !I->getType()->isVectorTy()) {
1504 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1505 break;
1506 }
1507
1508 const Value *V;
1509 // Handle bitcast from floating point to integer.
1510 if (match(I, m_ElementWiseBitCast(m_Value(V))) &&
1511 V->getType()->isFPOrFPVectorTy()) {
1512 Type *FPType = V->getType()->getScalarType();
1513 KnownFPClass Result =
1514 computeKnownFPClass(V, DemandedElts, fcAllFlags, Q, Depth + 1);
1515 FPClassTest FPClasses = Result.KnownFPClasses;
1516
1517 // TODO: Treat it as zero/poison if the use of I is unreachable.
1518 if (FPClasses == fcNone)
1519 break;
1520
1521 if (Result.isKnownNever(fcNormal | fcSubnormal | fcNan)) {
1522 Known.setAllConflict();
1523
1524 if (FPClasses & fcInf)
1526 APFloat::getInf(FPType->getFltSemantics()).bitcastToAPInt()));
1527
1528 if (FPClasses & fcZero)
1530 APInt::getZero(FPType->getScalarSizeInBits())));
1531
1532 Known.Zero.clearSignBit();
1533 Known.One.clearSignBit();
1534 }
1535
1536 if (Result.SignBit) {
1537 if (*Result.SignBit)
1538 Known.makeNegative();
1539 else
1540 Known.makeNonNegative();
1541 }
1542
1543 break;
1544 }
1545
1546 // Handle cast from vector integer type to scalar or vector integer.
1547 auto *SrcVecTy = dyn_cast<FixedVectorType>(SrcTy);
1548 if (!SrcVecTy || !SrcVecTy->getElementType()->isIntegerTy() ||
1549 !I->getType()->isIntOrIntVectorTy() ||
1550 isa<ScalableVectorType>(I->getType()))
1551 break;
1552
1553 unsigned NumElts = DemandedElts.getBitWidth();
1554 bool IsLE = Q.DL.isLittleEndian();
1555 // Look through a cast from narrow vector elements to wider type.
1556 // Examples: v4i32 -> v2i64, v3i8 -> v24
1557 unsigned SubBitWidth = SrcVecTy->getScalarSizeInBits();
1558 if (BitWidth % SubBitWidth == 0) {
1559 // Known bits are automatically intersected across demanded elements of a
1560 // vector. So for example, if a bit is computed as known zero, it must be
1561 // zero across all demanded elements of the vector.
1562 //
1563 // For this bitcast, each demanded element of the output is sub-divided
1564 // across a set of smaller vector elements in the source vector. To get
1565 // the known bits for an entire element of the output, compute the known
1566 // bits for each sub-element sequentially. This is done by shifting the
1567 // one-set-bit demanded elements parameter across the sub-elements for
1568 // consecutive calls to computeKnownBits. We are using the demanded
1569 // elements parameter as a mask operator.
1570 //
1571 // The known bits of each sub-element are then inserted into place
1572 // (dependent on endian) to form the full result of known bits.
1573 unsigned SubScale = BitWidth / SubBitWidth;
1574 APInt SubDemandedElts = APInt::getZero(NumElts * SubScale);
1575 for (unsigned i = 0; i != NumElts; ++i) {
1576 if (DemandedElts[i])
1577 SubDemandedElts.setBit(i * SubScale);
1578 }
1579
1580 KnownBits KnownSrc(SubBitWidth);
1581 for (unsigned i = 0; i != SubScale; ++i) {
1582 computeKnownBits(I->getOperand(0), SubDemandedElts.shl(i), KnownSrc, Q,
1583 Depth + 1);
1584 unsigned ShiftElt = IsLE ? i : SubScale - 1 - i;
1585 Known.insertBits(KnownSrc, ShiftElt * SubBitWidth);
1586 }
1587 }
1588 // Look through a cast from wider vector elements to narrow type.
1589 // Examples: v2i64 -> v4i32
1590 if (SubBitWidth % BitWidth == 0) {
1591 unsigned SubScale = SubBitWidth / BitWidth;
1592 KnownBits KnownSrc(SubBitWidth);
1593 APInt SubDemandedElts =
1594 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
1595 computeKnownBits(I->getOperand(0), SubDemandedElts, KnownSrc, Q,
1596 Depth + 1);
1597
1598 Known.setAllConflict();
1599 for (unsigned i = 0; i != NumElts; ++i) {
1600 if (DemandedElts[i]) {
1601 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
1602 unsigned Offset = (Shifts % SubScale) * BitWidth;
1603 Known = Known.intersectWith(KnownSrc.extractBits(BitWidth, Offset));
1604 if (Known.isUnknown())
1605 break;
1606 }
1607 }
1608 }
1609 break;
1610 }
1611 case Instruction::SExt: {
1612 // Compute the bits in the result that are not present in the input.
1613 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
1614
1615 Known = Known.trunc(SrcBitWidth);
1616 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1617 // If the sign bit of the input is known set or clear, then we know the
1618 // top bits of the result.
1619 Known = Known.sext(BitWidth);
1620 break;
1621 }
1622 case Instruction::Shl: {
1625 auto KF = [NUW, NSW](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1626 bool ShAmtNonZero) {
1627 return KnownBits::shl(KnownVal, KnownAmt, NUW, NSW, ShAmtNonZero);
1628 };
1629 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1630 KF);
1631 // Trailing zeros of a right-shifted constant never decrease.
1632 const APInt *C;
1633 if (match(I->getOperand(0), m_APInt(C)))
1634 Known.Zero.setLowBits(C->countr_zero());
1635
1636 // shl X, sub(Y, xor(ctlz(X, true), BitWidth-1)) shifts X so that its MSB
1637 // lands at bit Y, when BitWidth is a power of 2.
1638 const APInt *YC;
1639 Value *X = I->getOperand(0);
1640 if (isPowerOf2_32(BitWidth) &&
1641 match(I->getOperand(1),
1643 m_SpecificInt(BitWidth - 1)))) &&
1644 YC->ult(BitWidth - 1)) {
1645 unsigned Y = YC->getZExtValue();
1646 Known.One.setBit(Y);
1647 Known.Zero.setBitsFrom(Y + 1);
1648 }
1649 break;
1650 }
1651 case Instruction::LShr: {
1652 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1653 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1654 bool ShAmtNonZero) {
1655 return KnownBits::lshr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1656 };
1657 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1658 KF);
1659 // Leading zeros of a left-shifted constant never decrease.
1660 const APInt *C;
1661 if (match(I->getOperand(0), m_APInt(C)))
1662 Known.Zero.setHighBits(C->countl_zero());
1663 break;
1664 }
1665 case Instruction::AShr: {
1666 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1667 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1668 bool ShAmtNonZero) {
1669 return KnownBits::ashr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1670 };
1671 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1672 KF);
1673 break;
1674 }
1675 case Instruction::Sub: {
1678 computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW, NUW,
1679 DemandedElts, Known, Known2, Q, Depth);
1680 break;
1681 }
1682 case Instruction::Add: {
1685 computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW, NUW,
1686 DemandedElts, Known, Known2, Q, Depth);
1687 break;
1688 }
1689 case Instruction::SRem:
1690 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1691 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1692 Known = KnownBits::srem(Known, Known2);
1693 break;
1694
1695 case Instruction::URem:
1696 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1697 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1698 Known = KnownBits::urem(Known, Known2);
1699 break;
1700 case Instruction::Alloca:
1702 break;
1703 case Instruction::GetElementPtr: {
1704 // Analyze all of the subscripts of this getelementptr instruction
1705 // to determine if we can prove known low zero bits.
1706 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1707 // Accumulate the constant indices in a separate variable
1708 // to minimize the number of calls to computeForAddSub.
1709 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(I->getType());
1710 APInt AccConstIndices(IndexWidth, 0);
1711
1712 auto AddIndexToKnown = [&](KnownBits IndexBits) {
1713 if (IndexWidth == BitWidth) {
1714 // Note that inbounds does *not* guarantee nsw for the addition, as only
1715 // the offset is signed, while the base address is unsigned.
1716 Known = KnownBits::add(Known, IndexBits);
1717 } else {
1718 // If the index width is smaller than the pointer width, only add the
1719 // value to the low bits.
1720 assert(IndexWidth < BitWidth &&
1721 "Index width can't be larger than pointer width");
1722 Known.insertBits(KnownBits::add(Known.trunc(IndexWidth), IndexBits), 0);
1723 }
1724 };
1725
1727 for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
1728 // TrailZ can only become smaller, short-circuit if we hit zero.
1729 if (Known.isUnknown())
1730 break;
1731
1732 Value *Index = I->getOperand(i);
1733
1734 // Handle case when index is zero.
1735 Constant *CIndex = dyn_cast<Constant>(Index);
1736 if (CIndex && CIndex->isNullValue())
1737 continue;
1738
1739 if (StructType *STy = GTI.getStructTypeOrNull()) {
1740 // Handle struct member offset arithmetic.
1741
1742 assert(CIndex &&
1743 "Access to structure field must be known at compile time");
1744
1745 if (CIndex->getType()->isVectorTy())
1746 Index = CIndex->getSplatValue();
1747
1748 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
1749 const StructLayout *SL = Q.DL.getStructLayout(STy);
1750 uint64_t Offset = SL->getElementOffset(Idx);
1751 AccConstIndices += Offset;
1752 continue;
1753 }
1754
1755 // Handle array index arithmetic.
1756 Type *IndexedTy = GTI.getIndexedType();
1757 if (!IndexedTy->isSized()) {
1758 Known.resetAll();
1759 break;
1760 }
1761
1762 TypeSize Stride = GTI.getSequentialElementStride(Q.DL);
1763 uint64_t StrideInBytes = Stride.getKnownMinValue();
1764 if (!Stride.isScalable()) {
1765 // Fast path for constant offset.
1766 if (auto *CI = dyn_cast<ConstantInt>(Index)) {
1767 AccConstIndices +=
1768 CI->getValue().sextOrTrunc(IndexWidth) * StrideInBytes;
1769 continue;
1770 }
1771 }
1772
1773 KnownBits IndexBits =
1774 computeKnownBits(Index, Q, Depth + 1).sextOrTrunc(IndexWidth);
1775 KnownBits ScalingFactor(IndexWidth);
1776 // Multiply by current sizeof type.
1777 // &A[i] == A + i * sizeof(*A[i]).
1778 if (Stride.isScalable()) {
1779 // For scalable types the only thing we know about sizeof is
1780 // that this is a multiple of the minimum size.
1781 ScalingFactor.Zero.setLowBits(llvm::countr_zero(StrideInBytes));
1782 } else {
1783 ScalingFactor =
1784 KnownBits::makeConstant(APInt(IndexWidth, StrideInBytes));
1785 }
1786 AddIndexToKnown(KnownBits::mul(IndexBits, ScalingFactor));
1787 }
1788 if (!Known.isUnknown() && !AccConstIndices.isZero())
1789 AddIndexToKnown(KnownBits::makeConstant(AccConstIndices));
1790 break;
1791 }
1792 case Instruction::PHI: {
1793 const PHINode *P = cast<PHINode>(I);
1794 BinaryOperator *BO = nullptr;
1795 Value *R = nullptr, *L = nullptr;
1796 if (matchSimpleRecurrence(P, BO, R, L)) {
1797 // Handle the case of a simple two-predecessor recurrence PHI.
1798 // There's a lot more that could theoretically be done here, but
1799 // this is sufficient to catch some interesting cases.
1800 unsigned Opcode = BO->getOpcode();
1801
1802 switch (Opcode) {
1803 // If this is a shift recurrence, we know the bits being shifted in. We
1804 // can combine that with information about the start value of the
1805 // recurrence to conclude facts about the result. If this is a udiv
1806 // recurrence, we know that the result can never exceed either the
1807 // numerator or the start value, whichever is greater.
1808 case Instruction::LShr:
1809 case Instruction::AShr:
1810 case Instruction::Shl:
1811 case Instruction::UDiv:
1812 if (BO->getOperand(0) != I)
1813 break;
1814 [[fallthrough]];
1815
1816 // For a urem recurrence, the result can never exceed the start value. The
1817 // phi could either be the numerator or the denominator.
1818 case Instruction::URem: {
1819 // We have matched a recurrence of the form:
1820 // %iv = [R, %entry], [%iv.next, %backedge]
1821 // %iv.next = shift_op %iv, L
1822
1823 // Recurse with the phi context to avoid concern about whether facts
1824 // inferred hold at original context instruction. TODO: It may be
1825 // correct to use the original context. IF warranted, explore and
1826 // add sufficient tests to cover.
1828 RecQ.CxtI = P;
1829 computeKnownBits(R, DemandedElts, Known2, RecQ, Depth + 1);
1830 switch (Opcode) {
1831 case Instruction::Shl:
1832 // A shl recurrence will only increase the tailing zeros
1833 Known.Zero.setLowBits(Known2.countMinTrailingZeros());
1834 break;
1835 case Instruction::LShr:
1836 case Instruction::UDiv:
1837 case Instruction::URem:
1838 // lshr, udiv, and urem recurrences will preserve the leading zeros of
1839 // the start value.
1840 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1841 break;
1842 case Instruction::AShr:
1843 // An ashr recurrence will extend the initial sign bit
1844 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1845 Known.One.setHighBits(Known2.countMinLeadingOnes());
1846 break;
1847 }
1848 break;
1849 }
1850
1851 // Check for operations that have the property that if
1852 // both their operands have low zero bits, the result
1853 // will have low zero bits.
1854 case Instruction::Add:
1855 case Instruction::Sub:
1856 case Instruction::And:
1857 case Instruction::Or:
1858 case Instruction::Mul: {
1859 // Change the context instruction to the "edge" that flows into the
1860 // phi. This is important because that is where the value is actually
1861 // "evaluated" even though it is used later somewhere else. (see also
1862 // D69571).
1864
1865 unsigned OpNum = P->getOperand(0) == R ? 0 : 1;
1866 Instruction *RInst = P->getIncomingBlock(OpNum)->getTerminator();
1867 Instruction *LInst = P->getIncomingBlock(1 - OpNum)->getTerminator();
1868
1869 // Ok, we have a PHI of the form L op= R. Check for low
1870 // zero bits.
1871 RecQ.CxtI = RInst;
1872 computeKnownBits(R, DemandedElts, Known2, RecQ, Depth + 1);
1873
1874 // We need to take the minimum number of known bits
1875 KnownBits Known3(BitWidth);
1876 RecQ.CxtI = LInst;
1877 computeKnownBits(L, DemandedElts, Known3, RecQ, Depth + 1);
1878
1879 Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(),
1880 Known3.countMinTrailingZeros()));
1881
1882 auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(BO);
1883 if (!OverflowOp || !Q.IIQ.hasNoSignedWrap(OverflowOp))
1884 break;
1885
1886 switch (Opcode) {
1887 // If initial value of recurrence is nonnegative, and we are adding
1888 // a nonnegative number with nsw, the result can only be nonnegative
1889 // or poison value regardless of the number of times we execute the
1890 // add in phi recurrence. If initial value is negative and we are
1891 // adding a negative number with nsw, the result can only be
1892 // negative or poison value. Similar arguments apply to sub and mul.
1893 //
1894 // (add non-negative, non-negative) --> non-negative
1895 // (add negative, negative) --> negative
1896 case Instruction::Add: {
1897 if (Known2.isNonNegative() && Known3.isNonNegative())
1898 Known.makeNonNegative();
1899 else if (Known2.isNegative() && Known3.isNegative())
1900 Known.makeNegative();
1901 break;
1902 }
1903
1904 // (sub nsw non-negative, negative) --> non-negative
1905 // (sub nsw negative, non-negative) --> negative
1906 case Instruction::Sub: {
1907 if (BO->getOperand(0) != I)
1908 break;
1909 if (Known2.isNonNegative() && Known3.isNegative())
1910 Known.makeNonNegative();
1911 else if (Known2.isNegative() && Known3.isNonNegative())
1912 Known.makeNegative();
1913 break;
1914 }
1915
1916 // (mul nsw non-negative, non-negative) --> non-negative
1917 case Instruction::Mul:
1918 if (Known2.isNonNegative() && Known3.isNonNegative())
1919 Known.makeNonNegative();
1920 break;
1921
1922 default:
1923 break;
1924 }
1925 break;
1926 }
1927
1928 default:
1929 break;
1930 }
1931 }
1932
1933 // Unreachable blocks may have zero-operand PHI nodes.
1934 if (P->getNumIncomingValues() == 0)
1935 break;
1936
1937 // Otherwise take the unions of the known bit sets of the operands,
1938 // taking conservative care to avoid excessive recursion.
1939 if (Depth < MaxAnalysisRecursionDepth - 1 && Known.isUnknown()) {
1940 // Skip if every incoming value references to ourself.
1941 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
1942 break;
1943
1944 Known.setAllConflict();
1945 for (const Use &U : P->operands()) {
1946 Value *IncValue;
1947 const PHINode *CxtPhi;
1948 Instruction *CxtI;
1949 breakSelfRecursivePHI(&U, P, IncValue, CxtI, &CxtPhi);
1950 // Skip direct self references.
1951 if (IncValue == P)
1952 continue;
1953
1954 // Change the context instruction to the "edge" that flows into the
1955 // phi. This is important because that is where the value is actually
1956 // "evaluated" even though it is used later somewhere else. (see also
1957 // D69571).
1959
1960 Known2 = KnownBits(BitWidth);
1961
1962 // Recurse, but cap the recursion to one level, because we don't
1963 // want to waste time spinning around in loops.
1964 // TODO: See if we can base recursion limiter on number of incoming phi
1965 // edges so we don't overly clamp analysis.
1966 computeKnownBits(IncValue, DemandedElts, Known2, RecQ,
1968
1969 // See if we can further use a conditional branch into the phi
1970 // to help us determine the range of the value.
1971 if (!Known2.isConstant()) {
1972 CmpPredicate Pred;
1973 const APInt *RHSC;
1974 BasicBlock *TrueSucc, *FalseSucc;
1975 // TODO: Use RHS Value and compute range from its known bits.
1976 if (match(RecQ.CxtI,
1977 m_Br(m_c_ICmp(Pred, m_Specific(IncValue), m_APInt(RHSC)),
1978 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
1979 // Check for cases of duplicate successors.
1980 if ((TrueSucc == CxtPhi->getParent()) !=
1981 (FalseSucc == CxtPhi->getParent())) {
1982 // If we're using the false successor, invert the predicate.
1983 if (FalseSucc == CxtPhi->getParent())
1984 Pred = CmpInst::getInversePredicate(Pred);
1985 // Get the knownbits implied by the incoming phi condition.
1986 auto CR = ConstantRange::makeExactICmpRegion(Pred, *RHSC);
1987 KnownBits KnownUnion = Known2.unionWith(CR.toKnownBits());
1988 // We can have conflicts here if we are analyzing deadcode (its
1989 // impossible for us reach this BB based the icmp).
1990 if (KnownUnion.hasConflict()) {
1991 // No reason to continue analyzing in a known dead region, so
1992 // just resetAll and break. This will cause us to also exit the
1993 // outer loop.
1994 Known.resetAll();
1995 break;
1996 }
1997 Known2 = KnownUnion;
1998 }
1999 }
2000 }
2001
2002 Known = Known.intersectWith(Known2);
2003 // If all bits have been ruled out, there's no need to check
2004 // more operands.
2005 if (Known.isUnknown())
2006 break;
2007 }
2008 }
2009 break;
2010 }
2011 case Instruction::Call:
2012 case Instruction::Invoke: {
2013 // If range metadata is attached to this call, set known bits from that,
2014 // and then intersect with known bits based on other properties of the
2015 // function.
2016 if (MDNode *MD =
2017 Q.IIQ.getMetadata(cast<Instruction>(I), LLVMContext::MD_range))
2019
2020 const auto *CB = cast<CallBase>(I);
2021
2022 if (std::optional<ConstantRange> Range = CB->getRange())
2023 Known = Known.unionWith(Range->toKnownBits());
2024
2025 if (const Value *RV = CB->getReturnedArgOperand()) {
2026 if (RV->getType() == I->getType()) {
2027 computeKnownBits(RV, Known2, Q, Depth + 1);
2028 Known = Known.unionWith(Known2);
2029 // If the function doesn't return properly for all input values
2030 // (e.g. unreachable exits) then there might be conflicts between the
2031 // argument value and the range metadata. Simply discard the known bits
2032 // in case of conflicts.
2033 if (Known.hasConflict())
2034 Known.resetAll();
2035 }
2036 }
2037 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
2038 switch (II->getIntrinsicID()) {
2039 default:
2040 break;
2041 case Intrinsic::abs: {
2042 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2043 bool IntMinIsPoison = match(II->getArgOperand(1), m_One());
2044 Known = Known.unionWith(Known2.abs(IntMinIsPoison));
2045 break;
2046 }
2047 case Intrinsic::bitreverse:
2048 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2049 Known = Known.unionWith(Known2.reverseBits());
2050 break;
2051 case Intrinsic::bswap:
2052 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2053 Known = Known.unionWith(Known2.byteSwap());
2054 break;
2055 case Intrinsic::ctlz: {
2056 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2057 // If we have a known 1, its position is our upper bound.
2058 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
2059 // If this call is poison for 0 input, the result will be less than 2^n.
2060 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
2061 PossibleLZ = std::min(PossibleLZ, BitWidth - 1);
2062 unsigned LowBits = llvm::bit_width(PossibleLZ);
2063 Known.Zero.setBitsFrom(LowBits);
2064 break;
2065 }
2066 case Intrinsic::cttz: {
2067 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2068 // If we have a known 1, its position is our upper bound.
2069 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
2070 // If this call is poison for 0 input, the result will be less than 2^n.
2071 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
2072 PossibleTZ = std::min(PossibleTZ, BitWidth - 1);
2073 unsigned LowBits = llvm::bit_width(PossibleTZ);
2074 Known.Zero.setBitsFrom(LowBits);
2075 break;
2076 }
2077 case Intrinsic::ctpop: {
2078 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2079 // We can bound the space the count needs. Also, bits known to be zero
2080 // can't contribute to the population.
2081 unsigned BitsPossiblySet = Known2.countMaxPopulation();
2082 unsigned LowBits = llvm::bit_width(BitsPossiblySet);
2083 Known.Zero.setBitsFrom(LowBits);
2084 // TODO: we could bound KnownOne using the lower bound on the number
2085 // of bits which might be set provided by popcnt KnownOne2.
2086 break;
2087 }
2088 case Intrinsic::fshr:
2089 case Intrinsic::fshl: {
2090 const APInt *SA;
2091 if (!match(I->getOperand(2), m_APInt(SA)))
2092 break;
2093
2094 KnownBits Known3(BitWidth);
2095 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2096 computeKnownBits(I->getOperand(1), DemandedElts, Known3, Q, Depth + 1);
2097 Known = II->getIntrinsicID() == Intrinsic::fshl
2098 ? KnownBits::fshl(Known2, Known3, *SA)
2099 : KnownBits::fshr(Known2, Known3, *SA);
2100 break;
2101 }
2102 case Intrinsic::clmul:
2103 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2104 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2105 Known = KnownBits::clmul(Known, Known2);
2106 break;
2107 case Intrinsic::uadd_sat:
2108 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2109 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2110 Known = KnownBits::uadd_sat(Known, Known2);
2111 break;
2112 case Intrinsic::usub_sat:
2113 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2114 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2115 Known = KnownBits::usub_sat(Known, Known2);
2116 break;
2117 case Intrinsic::sadd_sat:
2118 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2119 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2120 Known = KnownBits::sadd_sat(Known, Known2);
2121 break;
2122 case Intrinsic::ssub_sat:
2123 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2124 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2125 Known = KnownBits::ssub_sat(Known, Known2);
2126 break;
2127 // Vec reverse preserves bits from input vec.
2128 case Intrinsic::vector_reverse:
2129 computeKnownBits(I->getOperand(0), DemandedElts.reverseBits(), Known, Q,
2130 Depth + 1);
2131 break;
2132 // for min/max/and/or reduce, any bit common to each element in the
2133 // input vec is set in the output.
2134 case Intrinsic::vector_reduce_and:
2135 case Intrinsic::vector_reduce_or:
2136 case Intrinsic::vector_reduce_umax:
2137 case Intrinsic::vector_reduce_umin:
2138 case Intrinsic::vector_reduce_smax:
2139 case Intrinsic::vector_reduce_smin:
2140 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2141 break;
2142 case Intrinsic::vector_reduce_xor: {
2143 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2144 // The zeros common to all vecs are zero in the output.
2145 // If the number of elements is odd, then the common ones remain. If the
2146 // number of elements is even, then the common ones becomes zeros.
2147 auto *VecTy = cast<VectorType>(I->getOperand(0)->getType());
2148 // Even, so the ones become zeros.
2149 bool EvenCnt = VecTy->getElementCount().isKnownEven();
2150 if (EvenCnt)
2151 Known.Zero |= Known.One;
2152 // Maybe even element count so need to clear ones.
2153 if (VecTy->isScalableTy() || EvenCnt)
2154 Known.One.clearAllBits();
2155 break;
2156 }
2157 case Intrinsic::vector_reduce_add: {
2158 auto *VecTy = dyn_cast<FixedVectorType>(I->getOperand(0)->getType());
2159 if (!VecTy)
2160 break;
2161 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2162 Known = Known.reduceAdd(VecTy->getNumElements());
2163 break;
2164 }
2165 case Intrinsic::umin:
2166 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2167 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2168 Known = KnownBits::umin(Known, Known2);
2169 break;
2170 case Intrinsic::umax:
2171 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2172 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2173 Known = KnownBits::umax(Known, Known2);
2174 break;
2175 case Intrinsic::smin:
2176 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2177 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2178 Known = KnownBits::smin(Known, Known2);
2180 break;
2181 case Intrinsic::smax:
2182 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2183 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2184 Known = KnownBits::smax(Known, Known2);
2186 break;
2187 case Intrinsic::ptrmask: {
2188 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2189
2190 const Value *Mask = I->getOperand(1);
2191 Known2 = KnownBits(Mask->getType()->getScalarSizeInBits());
2192 computeKnownBits(Mask, DemandedElts, Known2, Q, Depth + 1);
2193 // TODO: 1-extend would be more precise.
2194 Known &= Known2.anyextOrTrunc(BitWidth);
2195 break;
2196 }
2197 case Intrinsic::x86_sse2_pmulh_w:
2198 case Intrinsic::x86_avx2_pmulh_w:
2199 case Intrinsic::x86_avx512_pmulh_w_512:
2200 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2201 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2202 Known = KnownBits::mulhs(Known, Known2);
2203 break;
2204 case Intrinsic::x86_sse2_pmulhu_w:
2205 case Intrinsic::x86_avx2_pmulhu_w:
2206 case Intrinsic::x86_avx512_pmulhu_w_512:
2207 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2208 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2209 Known = KnownBits::mulhu(Known, Known2);
2210 break;
2211 case Intrinsic::x86_sse42_crc32_64_64:
2212 Known.Zero.setBitsFrom(32);
2213 break;
2214 case Intrinsic::x86_ssse3_phadd_d_128:
2215 case Intrinsic::x86_ssse3_phadd_w_128:
2216 case Intrinsic::x86_avx2_phadd_d:
2217 case Intrinsic::x86_avx2_phadd_w: {
2219 I, DemandedElts, Q, Depth,
2220 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2221 return KnownBits::add(KnownLHS, KnownRHS);
2222 });
2223 break;
2224 }
2225 case Intrinsic::x86_ssse3_phadd_sw_128:
2226 case Intrinsic::x86_avx2_phadd_sw: {
2228 I, DemandedElts, Q, Depth, KnownBits::sadd_sat);
2229 break;
2230 }
2231 case Intrinsic::x86_ssse3_phsub_d_128:
2232 case Intrinsic::x86_ssse3_phsub_w_128:
2233 case Intrinsic::x86_avx2_phsub_d:
2234 case Intrinsic::x86_avx2_phsub_w: {
2236 I, DemandedElts, Q, Depth,
2237 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2238 return KnownBits::sub(KnownLHS, KnownRHS);
2239 });
2240 break;
2241 }
2242 case Intrinsic::x86_ssse3_phsub_sw_128:
2243 case Intrinsic::x86_avx2_phsub_sw: {
2245 I, DemandedElts, Q, Depth, KnownBits::ssub_sat);
2246 break;
2247 }
2248 case Intrinsic::riscv_vsetvli:
2249 case Intrinsic::riscv_vsetvlimax: {
2250 bool HasAVL = II->getIntrinsicID() == Intrinsic::riscv_vsetvli;
2251 const ConstantRange Range = getVScaleRange(II->getFunction(), BitWidth);
2253 cast<ConstantInt>(II->getArgOperand(HasAVL))->getZExtValue());
2254 RISCVVType::VLMUL VLMUL = static_cast<RISCVVType::VLMUL>(
2255 cast<ConstantInt>(II->getArgOperand(1 + HasAVL))->getZExtValue());
2256 uint64_t MaxVLEN =
2257 Range.getUnsignedMax().getZExtValue() * RISCV::RVVBitsPerBlock;
2258 uint64_t MaxVL = MaxVLEN / RISCVVType::getSEWLMULRatio(SEW, VLMUL);
2259
2260 // Result of vsetvli must be not larger than AVL.
2261 if (HasAVL)
2262 if (auto *CI = dyn_cast<ConstantInt>(II->getArgOperand(0)))
2263 MaxVL = std::min(MaxVL, CI->getZExtValue());
2264
2265 unsigned KnownZeroFirstBit = Log2_32(MaxVL) + 1;
2266 if (BitWidth > KnownZeroFirstBit)
2267 Known.Zero.setBitsFrom(KnownZeroFirstBit);
2268 break;
2269 }
2270 case Intrinsic::amdgcn_mbcnt_hi:
2271 case Intrinsic::amdgcn_mbcnt_lo: {
2272 // Wave64 mbcnt_lo returns at most 32 + src1. Otherwise these return at
2273 // most 31 + src1.
2274 Known.Zero.setBitsFrom(
2275 II->getIntrinsicID() == Intrinsic::amdgcn_mbcnt_lo ? 6 : 5);
2276 computeKnownBits(I->getOperand(1), Known2, Q, Depth + 1);
2277 Known = KnownBits::add(Known, Known2);
2278 break;
2279 }
2280 case Intrinsic::vscale: {
2281 if (!II->getParent() || !II->getFunction())
2282 break;
2283
2284 Known = getVScaleRange(II->getFunction(), BitWidth).toKnownBits();
2285 break;
2286 }
2287 }
2288 }
2289 break;
2290 }
2291 case Instruction::ShuffleVector: {
2292 if (auto *Splat = getSplatValue(I)) {
2293 computeKnownBits(Splat, Known, Q, Depth + 1);
2294 break;
2295 }
2296
2297 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
2298 // FIXME: Do we need to handle ConstantExpr involving shufflevectors?
2299 if (!Shuf) {
2300 Known.resetAll();
2301 return;
2302 }
2303 // For undef elements, we don't know anything about the common state of
2304 // the shuffle result.
2305 APInt DemandedLHS, DemandedRHS;
2306 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) {
2307 Known.resetAll();
2308 return;
2309 }
2310 Known.setAllConflict();
2311 if (!!DemandedLHS) {
2312 const Value *LHS = Shuf->getOperand(0);
2313 computeKnownBits(LHS, DemandedLHS, Known, Q, Depth + 1);
2314 // If we don't know any bits, early out.
2315 if (Known.isUnknown())
2316 break;
2317 }
2318 if (!!DemandedRHS) {
2319 const Value *RHS = Shuf->getOperand(1);
2320 computeKnownBits(RHS, DemandedRHS, Known2, Q, Depth + 1);
2321 Known = Known.intersectWith(Known2);
2322 }
2323 break;
2324 }
2325 case Instruction::InsertElement: {
2326 if (isa<ScalableVectorType>(I->getType())) {
2327 Known.resetAll();
2328 return;
2329 }
2330 const Value *Vec = I->getOperand(0);
2331 const Value *Elt = I->getOperand(1);
2332 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
2333 unsigned NumElts = DemandedElts.getBitWidth();
2334 APInt DemandedVecElts = DemandedElts;
2335 bool NeedsElt = true;
2336 // If we know the index we are inserting too, clear it from Vec check.
2337 if (CIdx && CIdx->getValue().ult(NumElts)) {
2338 DemandedVecElts.clearBit(CIdx->getZExtValue());
2339 NeedsElt = DemandedElts[CIdx->getZExtValue()];
2340 }
2341
2342 Known.setAllConflict();
2343 if (NeedsElt) {
2344 computeKnownBits(Elt, Known, Q, Depth + 1);
2345 // If we don't know any bits, early out.
2346 if (Known.isUnknown())
2347 break;
2348 }
2349
2350 if (!DemandedVecElts.isZero()) {
2351 computeKnownBits(Vec, DemandedVecElts, Known2, Q, Depth + 1);
2352 Known = Known.intersectWith(Known2);
2353 }
2354 break;
2355 }
2356 case Instruction::ExtractElement: {
2357 // Look through extract element. If the index is non-constant or
2358 // out-of-range demand all elements, otherwise just the extracted element.
2359 const Value *Vec = I->getOperand(0);
2360 const Value *Idx = I->getOperand(1);
2361 auto *CIdx = dyn_cast<ConstantInt>(Idx);
2362 if (isa<ScalableVectorType>(Vec->getType())) {
2363 // FIXME: there's probably *something* we can do with scalable vectors
2364 Known.resetAll();
2365 break;
2366 }
2367 unsigned NumElts = cast<FixedVectorType>(Vec->getType())->getNumElements();
2368 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
2369 if (CIdx && CIdx->getValue().ult(NumElts))
2370 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
2371 computeKnownBits(Vec, DemandedVecElts, Known, Q, Depth + 1);
2372 break;
2373 }
2374 case Instruction::ExtractValue:
2375 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
2377 if (EVI->getNumIndices() != 1) break;
2378 if (EVI->getIndices()[0] == 0) {
2379 switch (II->getIntrinsicID()) {
2380 default: break;
2381 case Intrinsic::uadd_with_overflow:
2382 case Intrinsic::sadd_with_overflow:
2384 true, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2385 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2386 break;
2387 case Intrinsic::usub_with_overflow:
2388 case Intrinsic::ssub_with_overflow:
2390 false, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2391 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2392 break;
2393 case Intrinsic::umul_with_overflow:
2394 case Intrinsic::smul_with_overflow:
2395 computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false,
2396 false, DemandedElts, Known, Known2, Q, Depth);
2397 break;
2398 }
2399 }
2400 }
2401 break;
2402 case Instruction::Freeze:
2403 if (isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
2404 Depth + 1))
2405 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2406 break;
2407 }
2408}
2409
2410/// Determine which bits of V are known to be either zero or one and return
2411/// them.
2412KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
2413 const SimplifyQuery &Q, unsigned Depth) {
2414 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2415 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);
2416 return Known;
2417}
2418
2419/// Determine which bits of V are known to be either zero or one and return
2420/// them.
2422 unsigned Depth) {
2423 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2424 computeKnownBits(V, Known, Q, Depth);
2425 return Known;
2426}
2427
2428/// Determine which bits of V are known to be either zero or one and return
2429/// them in the Known bit set.
2430///
2431/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that
2432/// we cannot optimize based on the assumption that it is zero without changing
2433/// it to be an explicit zero. If we don't change it to zero, other code could
2434/// optimized based on the contradictory assumption that it is non-zero.
2435/// Because instcombine aggressively folds operations with undef args anyway,
2436/// this won't lose us code quality.
2437///
2438/// This function is defined on values with integer type, values with pointer
2439/// type, and vectors of integers. In the case
2440/// where V is a vector, known zero, and known one values are the
2441/// same width as the vector element, and the bit is set only if it is true
2442/// for all of the demanded elements in the vector specified by DemandedElts.
2443void computeKnownBits(const Value *V, const APInt &DemandedElts,
2444 KnownBits &Known, const SimplifyQuery &Q,
2445 unsigned Depth) {
2446 if (!DemandedElts) {
2447 // No demanded elts, better to assume we don't know anything.
2448 Known.resetAll();
2449 return;
2450 }
2451
2452 assert(V && "No Value?");
2453 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2454
2455#ifndef NDEBUG
2456 Type *Ty = V->getType();
2457 unsigned BitWidth = Known.getBitWidth();
2458
2459 assert((Ty->isIntOrIntVectorTy(BitWidth) || Ty->isPtrOrPtrVectorTy()) &&
2460 "Not integer or pointer type!");
2461
2462 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
2463 assert(
2464 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
2465 "DemandedElt width should equal the fixed vector number of elements");
2466 } else {
2467 assert(DemandedElts == APInt(1, 1) &&
2468 "DemandedElt width should be 1 for scalars or scalable vectors");
2469 }
2470
2471 Type *ScalarTy = Ty->getScalarType();
2472 if (ScalarTy->isPointerTy()) {
2473 assert(BitWidth == Q.DL.getPointerTypeSizeInBits(ScalarTy) &&
2474 "V and Known should have same BitWidth");
2475 } else {
2476 assert(BitWidth == Q.DL.getTypeSizeInBits(ScalarTy) &&
2477 "V and Known should have same BitWidth");
2478 }
2479#endif
2480
2481 const APInt *C;
2482 if (match(V, m_APInt(C))) {
2483 // We know all of the bits for a scalar constant or a splat vector constant!
2484 Known = KnownBits::makeConstant(*C);
2485 return;
2486 }
2487 // Null and aggregate-zero are all-zeros.
2489 Known.setAllZero();
2490 return;
2491 }
2492 // Handle a constant vector by taking the intersection of the known bits of
2493 // each element.
2495 assert(!isa<ScalableVectorType>(V->getType()));
2496 // We know that CDV must be a vector of integers. Take the intersection of
2497 // each element.
2498 Known.setAllConflict();
2499 for (unsigned i = 0, e = CDV->getNumElements(); i != e; ++i) {
2500 if (!DemandedElts[i])
2501 continue;
2502 APInt Elt = CDV->getElementAsAPInt(i);
2503 Known.Zero &= ~Elt;
2504 Known.One &= Elt;
2505 }
2506 if (Known.hasConflict())
2507 Known.resetAll();
2508 return;
2509 }
2510
2511 if (const auto *CV = dyn_cast<ConstantVector>(V)) {
2512 assert(!isa<ScalableVectorType>(V->getType()));
2513 // We know that CV must be a vector of integers. Take the intersection of
2514 // each element.
2515 Known.setAllConflict();
2516 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
2517 if (!DemandedElts[i])
2518 continue;
2519 Constant *Element = CV->getAggregateElement(i);
2520 if (isa<PoisonValue>(Element))
2521 continue;
2522 auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
2523 if (!ElementCI) {
2524 Known.resetAll();
2525 return;
2526 }
2527 const APInt &Elt = ElementCI->getValue();
2528 Known.Zero &= ~Elt;
2529 Known.One &= Elt;
2530 }
2531 if (Known.hasConflict())
2532 Known.resetAll();
2533 return;
2534 }
2535
2536 // Start out not knowing anything.
2537 Known.resetAll();
2538
2539 // We can't imply anything about undefs.
2540 if (isa<UndefValue>(V))
2541 return;
2542
2543 // There's no point in looking through other users of ConstantData for
2544 // assumptions. Confirm that we've handled them all.
2545 assert(!isa<ConstantData>(V) && "Unhandled constant data!");
2546
2547 if (const auto *A = dyn_cast<Argument>(V))
2548 if (std::optional<ConstantRange> Range = A->getRange())
2549 Known = Range->toKnownBits();
2550
2551 // All recursive calls that increase depth must come after this.
2553 return;
2554
2555 // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
2556 // the bits of its aliasee.
2557 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
2558 if (!GA->isInterposable())
2559 computeKnownBits(GA->getAliasee(), Known, Q, Depth + 1);
2560 return;
2561 }
2562
2563 if (const Operator *I = dyn_cast<Operator>(V))
2564 computeKnownBitsFromOperator(I, DemandedElts, Known, Q, Depth);
2565 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2566 if (std::optional<ConstantRange> CR = GV->getAbsoluteSymbolRange())
2567 Known = CR->toKnownBits();
2568 }
2569
2570 // Aligned pointers have trailing zeros - refine Known.Zero set
2571 if (isa<PointerType>(V->getType())) {
2572 Align Alignment = V->getPointerAlignment(Q.DL);
2573 Known.Zero.setLowBits(Log2(Alignment));
2574 }
2575
2576 // computeKnownBitsFromContext strictly refines Known.
2577 // Therefore, we run them after computeKnownBitsFromOperator.
2578
2579 // Check whether we can determine known bits from context such as assumes.
2580 computeKnownBitsFromContext(V, Known, Q, Depth);
2581}
2582
2583/// Try to detect a recurrence that the value of the induction variable is
2584/// always a power of two (or zero).
2585static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero,
2586 SimplifyQuery &Q, unsigned Depth) {
2587 BinaryOperator *BO = nullptr;
2588 Value *Start = nullptr, *Step = nullptr;
2589 if (!matchSimpleRecurrence(PN, BO, Start, Step))
2590 return false;
2591
2592 // Initial value must be a power of two.
2593 for (const Use &U : PN->operands()) {
2594 if (U.get() == Start) {
2595 // Initial value comes from a different BB, need to adjust context
2596 // instruction for analysis.
2597 Q.CxtI = PN->getIncomingBlock(U)->getTerminator();
2598 if (!isKnownToBeAPowerOfTwo(Start, OrZero, Q, Depth))
2599 return false;
2600 }
2601 }
2602
2603 // Except for Mul, the induction variable must be on the left side of the
2604 // increment expression, otherwise its value can be arbitrary.
2605 if (BO->getOpcode() != Instruction::Mul && BO->getOperand(1) != Step)
2606 return false;
2607
2608 Q.CxtI = BO->getParent()->getTerminator();
2609 switch (BO->getOpcode()) {
2610 case Instruction::Mul:
2611 // Power of two is closed under multiplication.
2612 return (OrZero || Q.IIQ.hasNoUnsignedWrap(BO) ||
2613 Q.IIQ.hasNoSignedWrap(BO)) &&
2614 isKnownToBeAPowerOfTwo(Step, OrZero, Q, Depth);
2615 case Instruction::SDiv:
2616 // Start value must not be signmask for signed division, so simply being a
2617 // power of two is not sufficient, and it has to be a constant.
2618 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2619 return false;
2620 [[fallthrough]];
2621 case Instruction::UDiv:
2622 // Divisor must be a power of two.
2623 // If OrZero is false, cannot guarantee induction variable is non-zero after
2624 // division, same for Shr, unless it is exact division.
2625 return (OrZero || Q.IIQ.isExact(BO)) &&
2626 isKnownToBeAPowerOfTwo(Step, false, Q, Depth);
2627 case Instruction::Shl:
2628 return OrZero || Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO);
2629 case Instruction::AShr:
2630 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2631 return false;
2632 [[fallthrough]];
2633 case Instruction::LShr:
2634 return OrZero || Q.IIQ.isExact(BO);
2635 default:
2636 return false;
2637 }
2638}
2639
2640/// Return true if we can infer that \p V is known to be a power of 2 from
2641/// dominating condition \p Cond (e.g., ctpop(V) == 1).
2642static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero,
2643 const Value *Cond,
2644 bool CondIsTrue) {
2645 CmpPredicate Pred;
2646 const APInt *RHSC;
2647 if (!match(Cond, m_ICmp(Pred, m_Ctpop(m_Specific(V)), m_APInt(RHSC))))
2648 return false;
2649 if (!CondIsTrue)
2650 Pred = ICmpInst::getInversePredicate(Pred);
2651 // ctpop(V) u< 2
2652 if (OrZero && Pred == ICmpInst::ICMP_ULT && *RHSC == 2)
2653 return true;
2654 // ctpop(V) == 1
2655 return Pred == ICmpInst::ICMP_EQ && *RHSC == 1;
2656}
2657
2658/// Return true if the given value is known to have exactly one
2659/// bit set when defined. For vectors return true if every element is known to
2660/// be a power of two when defined. Supports values with integer or pointer
2661/// types and vectors of integers.
2662bool llvm::isKnownToBeAPowerOfTwo(const Value *V, bool OrZero,
2663 const SimplifyQuery &Q, unsigned Depth) {
2664 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2665
2666 if (isa<Constant>(V))
2667 return OrZero ? match(V, m_Power2OrZero()) : match(V, m_Power2());
2668
2669 // i1 is by definition a power of 2 or zero.
2670 if (OrZero && V->getType()->getScalarSizeInBits() == 1)
2671 return true;
2672
2673 // Try to infer from assumptions.
2674 if (Q.AC && Q.CxtI) {
2675 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
2676 if (!AssumeVH)
2677 continue;
2678 CallInst *I = cast<CallInst>(AssumeVH);
2679 if (isImpliedToBeAPowerOfTwoFromCond(V, OrZero, I->getArgOperand(0),
2680 /*CondIsTrue=*/true) &&
2682 return true;
2683 }
2684 }
2685
2686 // Handle dominating conditions.
2687 if (Q.DC && Q.CxtI && Q.DT) {
2688 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
2689 Value *Cond = BI->getCondition();
2690
2691 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
2693 /*CondIsTrue=*/true) &&
2694 Q.DT->dominates(Edge0, Q.CxtI->getParent()))
2695 return true;
2696
2697 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
2699 /*CondIsTrue=*/false) &&
2700 Q.DT->dominates(Edge1, Q.CxtI->getParent()))
2701 return true;
2702 }
2703 }
2704
2705 auto *I = dyn_cast<Instruction>(V);
2706 if (!I)
2707 return false;
2708
2709 if (Q.CxtI && match(V, m_VScale())) {
2710 const Function *F = Q.CxtI->getFunction();
2711 // The vscale_range indicates vscale is a power-of-two.
2712 return F->hasFnAttribute(Attribute::VScaleRange);
2713 }
2714
2715 // 1 << X is clearly a power of two if the one is not shifted off the end. If
2716 // it is shifted off the end then the result is undefined.
2717 if (match(I, m_Shl(m_One(), m_Value())))
2718 return true;
2719
2720 // (signmask) >>l X is clearly a power of two if the one is not shifted off
2721 // the bottom. If it is shifted off the bottom then the result is undefined.
2722 if (match(I, m_LShr(m_SignMask(), m_Value())))
2723 return true;
2724
2725 // The remaining tests are all recursive, so bail out if we hit the limit.
2727 return false;
2728
2729 switch (I->getOpcode()) {
2730 case Instruction::ZExt:
2731 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2732 case Instruction::Trunc:
2733 return OrZero && isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2734 case Instruction::Shl:
2735 if (OrZero || Q.IIQ.hasNoUnsignedWrap(I) || Q.IIQ.hasNoSignedWrap(I))
2736 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2737 return false;
2738 case Instruction::LShr:
2739 if (OrZero || Q.IIQ.isExact(cast<BinaryOperator>(I)))
2740 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2741 return false;
2742 case Instruction::UDiv:
2744 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2745 return false;
2746 case Instruction::Mul:
2747 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
2748 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth) &&
2749 (OrZero || isKnownNonZero(I, Q, Depth));
2750 case Instruction::And:
2751 // A power of two and'd with anything is a power of two or zero.
2752 if (OrZero &&
2753 (isKnownToBeAPowerOfTwo(I->getOperand(1), /*OrZero*/ true, Q, Depth) ||
2754 isKnownToBeAPowerOfTwo(I->getOperand(0), /*OrZero*/ true, Q, Depth)))
2755 return true;
2756 // X & (-X) is always a power of two or zero.
2757 if (match(I->getOperand(0), m_Neg(m_Specific(I->getOperand(1)))) ||
2758 match(I->getOperand(1), m_Neg(m_Specific(I->getOperand(0)))))
2759 return OrZero || isKnownNonZero(I->getOperand(0), Q, Depth);
2760 return false;
2761 case Instruction::Add: {
2762 // Adding a power-of-two or zero to the same power-of-two or zero yields
2763 // either the original power-of-two, a larger power-of-two or zero.
2765 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) ||
2766 Q.IIQ.hasNoSignedWrap(VOBO)) {
2767 if (match(I->getOperand(0),
2768 m_c_And(m_Specific(I->getOperand(1)), m_Value())) &&
2769 isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth))
2770 return true;
2771 if (match(I->getOperand(1),
2772 m_c_And(m_Specific(I->getOperand(0)), m_Value())) &&
2773 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth))
2774 return true;
2775
2776 unsigned BitWidth = V->getType()->getScalarSizeInBits();
2777 KnownBits LHSBits(BitWidth);
2778 computeKnownBits(I->getOperand(0), LHSBits, Q, Depth);
2779
2780 KnownBits RHSBits(BitWidth);
2781 computeKnownBits(I->getOperand(1), RHSBits, Q, Depth);
2782 // If i8 V is a power of two or zero:
2783 // ZeroBits: 1 1 1 0 1 1 1 1
2784 // ~ZeroBits: 0 0 0 1 0 0 0 0
2785 if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2())
2786 // If OrZero isn't set, we cannot give back a zero result.
2787 // Make sure either the LHS or RHS has a bit set.
2788 if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue())
2789 return true;
2790 }
2791
2792 // LShr(UINT_MAX, Y) + 1 is a power of two (if add is nuw) or zero.
2793 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO))
2794 if (match(I, m_Add(m_LShr(m_AllOnes(), m_Value()), m_One())))
2795 return true;
2796 return false;
2797 }
2798 case Instruction::Select:
2799 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
2800 isKnownToBeAPowerOfTwo(I->getOperand(2), OrZero, Q, Depth);
2801 case Instruction::PHI: {
2802 // A PHI node is power of two if all incoming values are power of two, or if
2803 // it is an induction variable where in each step its value is a power of
2804 // two.
2805 auto *PN = cast<PHINode>(I);
2807
2808 // Check if it is an induction variable and always power of two.
2809 if (isPowerOfTwoRecurrence(PN, OrZero, RecQ, Depth))
2810 return true;
2811
2812 // Recursively check all incoming values. Limit recursion to 2 levels, so
2813 // that search complexity is limited to number of operands^2.
2814 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
2815 return llvm::all_of(PN->operands(), [&](const Use &U) {
2816 // Value is power of 2 if it is coming from PHI node itself by induction.
2817 if (U.get() == PN)
2818 return true;
2819
2820 // Change the context instruction to the incoming block where it is
2821 // evaluated.
2822 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
2823 return isKnownToBeAPowerOfTwo(U.get(), OrZero, RecQ, NewDepth);
2824 });
2825 }
2826 case Instruction::Invoke:
2827 case Instruction::Call: {
2828 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
2829 switch (II->getIntrinsicID()) {
2830 case Intrinsic::umax:
2831 case Intrinsic::smax:
2832 case Intrinsic::umin:
2833 case Intrinsic::smin:
2834 return isKnownToBeAPowerOfTwo(II->getArgOperand(1), OrZero, Q, Depth) &&
2835 isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2836 // bswap/bitreverse just move around bits, but don't change any 1s/0s
2837 // thus dont change pow2/non-pow2 status.
2838 case Intrinsic::bitreverse:
2839 case Intrinsic::bswap:
2840 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2841 case Intrinsic::fshr:
2842 case Intrinsic::fshl:
2843 // If Op0 == Op1, this is a rotate. is_pow2(rotate(x, y)) == is_pow2(x)
2844 if (II->getArgOperand(0) == II->getArgOperand(1))
2845 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2846 break;
2847 default:
2848 break;
2849 }
2850 }
2851 return false;
2852 }
2853 default:
2854 return false;
2855 }
2856}
2857
2858/// Test whether a GEP's result is known to be non-null.
2859///
2860/// Uses properties inherent in a GEP to try to determine whether it is known
2861/// to be non-null.
2862///
2863/// Currently this routine does not support vector GEPs.
2864static bool isGEPKnownNonNull(const GEPOperator *GEP, const SimplifyQuery &Q,
2865 unsigned Depth) {
2866 const Function *F = nullptr;
2867 if (const Instruction *I = dyn_cast<Instruction>(GEP))
2868 F = I->getFunction();
2869
2870 // If the gep is nuw or inbounds with invalid null pointer, then the GEP
2871 // may be null iff the base pointer is null and the offset is zero.
2872 if (!GEP->hasNoUnsignedWrap() &&
2873 !(GEP->isInBounds() &&
2874 !NullPointerIsDefined(F, GEP->getPointerAddressSpace())))
2875 return false;
2876
2877 // FIXME: Support vector-GEPs.
2878 assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP");
2879
2880 // If the base pointer is non-null, we cannot walk to a null address with an
2881 // inbounds GEP in address space zero.
2882 if (isKnownNonZero(GEP->getPointerOperand(), Q, Depth))
2883 return true;
2884
2885 // Walk the GEP operands and see if any operand introduces a non-zero offset.
2886 // If so, then the GEP cannot produce a null pointer, as doing so would
2887 // inherently violate the inbounds contract within address space zero.
2889 GTI != GTE; ++GTI) {
2890 // Struct types are easy -- they must always be indexed by a constant.
2891 if (StructType *STy = GTI.getStructTypeOrNull()) {
2892 ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
2893 unsigned ElementIdx = OpC->getZExtValue();
2894 const StructLayout *SL = Q.DL.getStructLayout(STy);
2895 uint64_t ElementOffset = SL->getElementOffset(ElementIdx);
2896 if (ElementOffset > 0)
2897 return true;
2898 continue;
2899 }
2900
2901 // If we have a zero-sized type, the index doesn't matter. Keep looping.
2902 if (GTI.getSequentialElementStride(Q.DL).isZero())
2903 continue;
2904
2905 // Fast path the constant operand case both for efficiency and so we don't
2906 // increment Depth when just zipping down an all-constant GEP.
2907 if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
2908 if (!OpC->isZero())
2909 return true;
2910 continue;
2911 }
2912
2913 // We post-increment Depth here because while isKnownNonZero increments it
2914 // as well, when we pop back up that increment won't persist. We don't want
2915 // to recurse 10k times just because we have 10k GEP operands. We don't
2916 // bail completely out because we want to handle constant GEPs regardless
2917 // of depth.
2919 continue;
2920
2921 if (isKnownNonZero(GTI.getOperand(), Q, Depth))
2922 return true;
2923 }
2924
2925 return false;
2926}
2927
2929 const Instruction *CtxI,
2930 const DominatorTree *DT) {
2931 assert(!isa<Constant>(V) && "Called for constant?");
2932
2933 if (!CtxI || !DT)
2934 return false;
2935
2936 unsigned NumUsesExplored = 0;
2937 for (auto &U : V->uses()) {
2938 // Avoid massive lists
2939 if (NumUsesExplored >= DomConditionsMaxUses)
2940 break;
2941 NumUsesExplored++;
2942
2943 const Instruction *UI = cast<Instruction>(U.getUser());
2944 // If the value is used as an argument to a call or invoke, then argument
2945 // attributes may provide an answer about null-ness.
2946 if (V->getType()->isPointerTy()) {
2947 if (const auto *CB = dyn_cast<CallBase>(UI)) {
2948 if (CB->isArgOperand(&U) &&
2949 CB->paramHasNonNullAttr(CB->getArgOperandNo(&U),
2950 /*AllowUndefOrPoison=*/false) &&
2951 DT->dominates(CB, CtxI))
2952 return true;
2953 }
2954 }
2955
2956 // If the value is used as a load/store, then the pointer must be non null.
2957 if (V == getLoadStorePointerOperand(UI)) {
2960 DT->dominates(UI, CtxI))
2961 return true;
2962 }
2963
2964 if ((match(UI, m_IDiv(m_Value(), m_Specific(V))) ||
2965 match(UI, m_IRem(m_Value(), m_Specific(V)))) &&
2966 isValidAssumeForContext(UI, CtxI, DT))
2967 return true;
2968
2969 // Consider only compare instructions uniquely controlling a branch
2970 Value *RHS;
2971 CmpPredicate Pred;
2972 if (!match(UI, m_c_ICmp(Pred, m_Specific(V), m_Value(RHS))))
2973 continue;
2974
2975 bool NonNullIfTrue;
2976 if (cmpExcludesZero(Pred, RHS))
2977 NonNullIfTrue = true;
2979 NonNullIfTrue = false;
2980 else
2981 continue;
2982
2985 for (const auto *CmpU : UI->users()) {
2986 assert(WorkList.empty() && "Should be!");
2987 if (Visited.insert(CmpU).second)
2988 WorkList.push_back(CmpU);
2989
2990 while (!WorkList.empty()) {
2991 auto *Curr = WorkList.pop_back_val();
2992
2993 // If a user is an AND, add all its users to the work list. We only
2994 // propagate "pred != null" condition through AND because it is only
2995 // correct to assume that all conditions of AND are met in true branch.
2996 // TODO: Support similar logic of OR and EQ predicate?
2997 if (NonNullIfTrue)
2998 if (match(Curr, m_LogicalAnd(m_Value(), m_Value()))) {
2999 for (const auto *CurrU : Curr->users())
3000 if (Visited.insert(CurrU).second)
3001 WorkList.push_back(CurrU);
3002 continue;
3003 }
3004
3005 if (const CondBrInst *BI = dyn_cast<CondBrInst>(Curr)) {
3006 BasicBlock *NonNullSuccessor =
3007 BI->getSuccessor(NonNullIfTrue ? 0 : 1);
3008 BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor);
3009 if (DT->dominates(Edge, CtxI->getParent()))
3010 return true;
3011 } else if (NonNullIfTrue && isGuard(Curr) &&
3012 DT->dominates(cast<Instruction>(Curr), CtxI)) {
3013 return true;
3014 }
3015 }
3016 }
3017 }
3018
3019 return false;
3020}
3021
3022/// Does the 'Range' metadata (which must be a valid MD_range operand list)
3023/// ensure that the value it's attached to is never Value? 'RangeType' is
3024/// is the type of the value described by the range.
3025static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) {
3026 const unsigned NumRanges = Ranges->getNumOperands() / 2;
3027 assert(NumRanges >= 1);
3028 for (unsigned i = 0; i < NumRanges; ++i) {
3030 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
3032 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
3033 ConstantRange Range(Lower->getValue(), Upper->getValue());
3034 if (Range.contains(Value))
3035 return false;
3036 }
3037 return true;
3038}
3039
3040/// Try to detect a recurrence that monotonically increases/decreases from a
3041/// non-zero starting value. These are common as induction variables.
3042static bool isNonZeroRecurrence(const PHINode *PN) {
3043 BinaryOperator *BO = nullptr;
3044 Value *Start = nullptr, *Step = nullptr;
3045 const APInt *StartC, *StepC;
3046 if (!matchSimpleRecurrence(PN, BO, Start, Step) ||
3047 !match(Start, m_APInt(StartC)) || StartC->isZero())
3048 return false;
3049
3050 switch (BO->getOpcode()) {
3051 case Instruction::Add:
3052 // Starting from non-zero and stepping away from zero can never wrap back
3053 // to zero.
3054 return BO->hasNoUnsignedWrap() ||
3055 (BO->hasNoSignedWrap() && match(Step, m_APInt(StepC)) &&
3056 StartC->isNegative() == StepC->isNegative());
3057 case Instruction::Mul:
3058 return (BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap()) &&
3059 match(Step, m_APInt(StepC)) && !StepC->isZero();
3060 case Instruction::Shl:
3061 return BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap();
3062 case Instruction::AShr:
3063 case Instruction::LShr:
3064 return BO->isExact();
3065 default:
3066 return false;
3067 }
3068}
3069
3070static bool matchOpWithOpEqZero(Value *Op0, Value *Op1) {
3072 m_Specific(Op1), m_Zero()))) ||
3074 m_Specific(Op0), m_Zero())));
3075}
3076
3077static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q,
3078 unsigned BitWidth, Value *X, Value *Y, bool NSW,
3079 bool NUW, unsigned Depth) {
3080 // (X + (X != 0)) is non zero
3081 if (matchOpWithOpEqZero(X, Y))
3082 return true;
3083
3084 if (NUW)
3085 return isKnownNonZero(Y, DemandedElts, Q, Depth) ||
3086 isKnownNonZero(X, DemandedElts, Q, Depth);
3087
3088 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
3089 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
3090
3091 // If X and Y are both non-negative (as signed values) then their sum is not
3092 // zero unless both X and Y are zero.
3093 if (XKnown.isNonNegative() && YKnown.isNonNegative())
3094 if (isKnownNonZero(Y, DemandedElts, Q, Depth) ||
3095 isKnownNonZero(X, DemandedElts, Q, Depth))
3096 return true;
3097
3098 // If X and Y are both negative (as signed values) then their sum is not
3099 // zero unless both X and Y equal INT_MIN.
3100 if (XKnown.isNegative() && YKnown.isNegative()) {
3102 // The sign bit of X is set. If some other bit is set then X is not equal
3103 // to INT_MIN.
3104 if (XKnown.One.intersects(Mask))
3105 return true;
3106 // The sign bit of Y is set. If some other bit is set then Y is not equal
3107 // to INT_MIN.
3108 if (YKnown.One.intersects(Mask))
3109 return true;
3110 }
3111
3112 // The sum of a non-negative number and a power of two is not zero.
3113 if (XKnown.isNonNegative() &&
3114 isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Q, Depth))
3115 return true;
3116 if (YKnown.isNonNegative() &&
3117 isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Q, Depth))
3118 return true;
3119
3120 return KnownBits::add(XKnown, YKnown, NSW, NUW).isNonZero();
3121}
3122
3123static bool isNonZeroSub(const APInt &DemandedElts, const SimplifyQuery &Q,
3124 unsigned BitWidth, Value *X, Value *Y,
3125 unsigned Depth) {
3126 // (X - (X != 0)) is non zero
3127 // ((X != 0) - X) is non zero
3128 if (matchOpWithOpEqZero(X, Y))
3129 return true;
3130
3131 // TODO: Move this case into isKnownNonEqual().
3132 if (auto *C = dyn_cast<Constant>(X))
3133 if (C->isNullValue() && isKnownNonZero(Y, DemandedElts, Q, Depth))
3134 return true;
3135
3136 return ::isKnownNonEqual(X, Y, DemandedElts, Q, Depth);
3137}
3138
3139static bool isNonZeroMul(const APInt &DemandedElts, const SimplifyQuery &Q,
3140 unsigned BitWidth, Value *X, Value *Y, bool NSW,
3141 bool NUW, unsigned Depth) {
3142 // If X and Y are non-zero then so is X * Y as long as the multiplication
3143 // does not overflow.
3144 if (NSW || NUW)
3145 return isKnownNonZero(X, DemandedElts, Q, Depth) &&
3146 isKnownNonZero(Y, DemandedElts, Q, Depth);
3147
3148 // If either X or Y is odd, then if the other is non-zero the result can't
3149 // be zero.
3150 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
3151 if (XKnown.One[0])
3152 return isKnownNonZero(Y, DemandedElts, Q, Depth);
3153
3154 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
3155 if (YKnown.One[0])
3156 return XKnown.isNonZero() || isKnownNonZero(X, DemandedElts, Q, Depth);
3157
3158 // If there exists any subset of X (sX) and subset of Y (sY) s.t sX * sY is
3159 // non-zero, then X * Y is non-zero. We can find sX and sY by just taking
3160 // the lowest known One of X and Y. If they are non-zero, the result
3161 // must be non-zero. We can check if LSB(X) * LSB(Y) != 0 by doing
3162 // X.CountLeadingZeros + Y.CountLeadingZeros < BitWidth.
3163 return (XKnown.countMaxTrailingZeros() + YKnown.countMaxTrailingZeros()) <
3164 BitWidth;
3165}
3166
3167static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts,
3168 const SimplifyQuery &Q, const KnownBits &KnownVal,
3169 unsigned Depth) {
3170 auto ShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
3171 switch (I->getOpcode()) {
3172 case Instruction::Shl:
3173 return Lhs.shl(Rhs);
3174 case Instruction::LShr:
3175 return Lhs.lshr(Rhs);
3176 case Instruction::AShr:
3177 return Lhs.ashr(Rhs);
3178 default:
3179 llvm_unreachable("Unknown Shift Opcode");
3180 }
3181 };
3182
3183 auto InvShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
3184 switch (I->getOpcode()) {
3185 case Instruction::Shl:
3186 return Lhs.lshr(Rhs);
3187 case Instruction::LShr:
3188 case Instruction::AShr:
3189 return Lhs.shl(Rhs);
3190 default:
3191 llvm_unreachable("Unknown Shift Opcode");
3192 }
3193 };
3194
3195 if (KnownVal.isUnknown())
3196 return false;
3197
3198 KnownBits KnownCnt =
3199 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
3200 APInt MaxShift = KnownCnt.getMaxValue();
3201 unsigned NumBits = KnownVal.getBitWidth();
3202 if (MaxShift.uge(NumBits))
3203 return false;
3204
3205 if (!ShiftOp(KnownVal.One, MaxShift).isZero())
3206 return true;
3207
3208 // If all of the bits shifted out are known to be zero, and Val is known
3209 // non-zero then at least one non-zero bit must remain.
3210 if (InvShiftOp(KnownVal.Zero, NumBits - MaxShift)
3211 .eq(InvShiftOp(APInt::getAllOnes(NumBits), NumBits - MaxShift)) &&
3212 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth))
3213 return true;
3214
3215 return false;
3216}
3217
3219 const APInt &DemandedElts,
3220 const SimplifyQuery &Q, unsigned Depth) {
3221 unsigned BitWidth = getBitWidth(I->getType()->getScalarType(), Q.DL);
3222 switch (I->getOpcode()) {
3223 case Instruction::Alloca:
3224 // Alloca never returns null, malloc might.
3225 return I->getType()->getPointerAddressSpace() == 0;
3226 case Instruction::GetElementPtr:
3227 if (I->getType()->isPointerTy())
3229 break;
3230 case Instruction::BitCast: {
3231 // We need to be a bit careful here. We can only peek through the bitcast
3232 // if the scalar size of elements in the operand are smaller than and a
3233 // multiple of the size they are casting too. Take three cases:
3234 //
3235 // 1) Unsafe:
3236 // bitcast <2 x i16> %NonZero to <4 x i8>
3237 //
3238 // %NonZero can have 2 non-zero i16 elements, but isKnownNonZero on a
3239 // <4 x i8> requires that all 4 i8 elements be non-zero which isn't
3240 // guranteed (imagine just sign bit set in the 2 i16 elements).
3241 //
3242 // 2) Unsafe:
3243 // bitcast <4 x i3> %NonZero to <3 x i4>
3244 //
3245 // Even though the scalar size of the src (`i3`) is smaller than the
3246 // scalar size of the dst `i4`, because `i3` is not a multiple of `i4`
3247 // its possible for the `3 x i4` elements to be zero because there are
3248 // some elements in the destination that don't contain any full src
3249 // element.
3250 //
3251 // 3) Safe:
3252 // bitcast <4 x i8> %NonZero to <2 x i16>
3253 //
3254 // This is always safe as non-zero in the 4 i8 elements implies
3255 // non-zero in the combination of any two adjacent ones. Since i8 is a
3256 // multiple of i16, each i16 is guranteed to have 2 full i8 elements.
3257 // This all implies the 2 i16 elements are non-zero.
3258 Type *FromTy = I->getOperand(0)->getType();
3259 if ((FromTy->isIntOrIntVectorTy() || FromTy->isPtrOrPtrVectorTy()) &&
3260 (BitWidth % getBitWidth(FromTy->getScalarType(), Q.DL)) == 0)
3261 return isKnownNonZero(I->getOperand(0), Q, Depth);
3262 } break;
3263 case Instruction::IntToPtr:
3264 // Note that we have to take special care to avoid looking through
3265 // truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well
3266 // as casts that can alter the value, e.g., AddrSpaceCasts.
3267 if (!isa<ScalableVectorType>(I->getType()) &&
3268 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
3269 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
3270 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3271 break;
3272 case Instruction::PtrToAddr:
3273 // isKnownNonZero() for pointers refers to the address bits being non-zero,
3274 // so we can directly forward.
3275 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3276 case Instruction::PtrToInt:
3277 // For inttoptr, make sure the result size is >= the address size. If the
3278 // address is non-zero, any larger value is also non-zero.
3279 if (Q.DL.getAddressSizeInBits(I->getOperand(0)->getType()) <=
3280 I->getType()->getScalarSizeInBits())
3281 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3282 break;
3283 case Instruction::Trunc:
3284 // nuw/nsw trunc preserves zero/non-zero status of input.
3285 if (auto *TI = dyn_cast<TruncInst>(I))
3286 if (TI->hasNoSignedWrap() || TI->hasNoUnsignedWrap())
3287 return isKnownNonZero(TI->getOperand(0), DemandedElts, Q, Depth);
3288 break;
3289
3290 // Iff x - y != 0, then x ^ y != 0
3291 // Therefore we can do the same exact checks
3292 case Instruction::Xor:
3293 case Instruction::Sub:
3294 return isNonZeroSub(DemandedElts, Q, BitWidth, I->getOperand(0),
3295 I->getOperand(1), Depth);
3296 case Instruction::Or:
3297 // (X | (X != 0)) is non zero
3298 if (matchOpWithOpEqZero(I->getOperand(0), I->getOperand(1)))
3299 return true;
3300 // X | Y != 0 if X != Y.
3301 if (isKnownNonEqual(I->getOperand(0), I->getOperand(1), DemandedElts, Q,
3302 Depth))
3303 return true;
3304 // X | Y != 0 if X != 0 or Y != 0.
3305 return isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth) ||
3306 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3307 case Instruction::SExt:
3308 case Instruction::ZExt:
3309 // ext X != 0 if X != 0.
3310 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3311
3312 case Instruction::Shl: {
3313 // shl nsw/nuw can't remove any non-zero bits.
3315 if (Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO))
3316 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3317
3318 // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
3319 // if the lowest bit is shifted off the end.
3320 KnownBits Known(BitWidth);
3321 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth);
3322 if (Known.One[0])
3323 return true;
3324
3325 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3326 }
3327 case Instruction::LShr:
3328 case Instruction::AShr: {
3329 // shr exact can only shift out zero bits.
3331 if (BO->isExact())
3332 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3333
3334 // shr X, Y != 0 if X is negative. Note that the value of the shift is not
3335 // defined if the sign bit is shifted off the end.
3336 KnownBits Known =
3337 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3338 if (Known.isNegative())
3339 return true;
3340
3341 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3342 }
3343 case Instruction::UDiv:
3344 case Instruction::SDiv: {
3345 // X / Y
3346 // div exact can only produce a zero if the dividend is zero.
3347 if (cast<PossiblyExactOperator>(I)->isExact())
3348 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3349
3350 KnownBits XKnown =
3351 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3352 // If X is fully unknown we won't be able to figure anything out so don't
3353 // both computing knownbits for Y.
3354 if (XKnown.isUnknown())
3355 return false;
3356
3357 KnownBits YKnown =
3358 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
3359 if (I->getOpcode() == Instruction::SDiv) {
3360 // For signed division need to compare abs value of the operands.
3361 XKnown = XKnown.abs(/*IntMinIsPoison*/ false);
3362 YKnown = YKnown.abs(/*IntMinIsPoison*/ false);
3363 }
3364 // If X u>= Y then div is non zero (0/0 is UB).
3365 std::optional<bool> XUgeY = KnownBits::uge(XKnown, YKnown);
3366 // If X is total unknown or X u< Y we won't be able to prove non-zero
3367 // with compute known bits so just return early.
3368 return XUgeY && *XUgeY;
3369 }
3370 case Instruction::Add: {
3371 // X + Y.
3372
3373 // If Add has nuw wrap flag, then if either X or Y is non-zero the result is
3374 // non-zero.
3376 return isNonZeroAdd(DemandedElts, Q, BitWidth, I->getOperand(0),
3377 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3378 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3379 }
3380 case Instruction::Mul: {
3382 return isNonZeroMul(DemandedElts, Q, BitWidth, I->getOperand(0),
3383 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3384 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3385 }
3386 case Instruction::Select: {
3387 // (C ? X : Y) != 0 if X != 0 and Y != 0.
3388
3389 // First check if the arm is non-zero using `isKnownNonZero`. If that fails,
3390 // then see if the select condition implies the arm is non-zero. For example
3391 // (X != 0 ? X : Y), we know the true arm is non-zero as the `X` "return" is
3392 // dominated by `X != 0`.
3393 auto SelectArmIsNonZero = [&](bool IsTrueArm) {
3394 Value *Op;
3395 Op = IsTrueArm ? I->getOperand(1) : I->getOperand(2);
3396 // Op is trivially non-zero.
3397 if (isKnownNonZero(Op, DemandedElts, Q, Depth))
3398 return true;
3399
3400 // The condition of the select dominates the true/false arm. Check if the
3401 // condition implies that a given arm is non-zero.
3402 Value *X;
3403 CmpPredicate Pred;
3404 if (!match(I->getOperand(0), m_c_ICmp(Pred, m_Specific(Op), m_Value(X))))
3405 return false;
3406
3407 if (!IsTrueArm)
3408 Pred = ICmpInst::getInversePredicate(Pred);
3409
3410 return cmpExcludesZero(Pred, X);
3411 };
3412
3413 if (SelectArmIsNonZero(/* IsTrueArm */ true) &&
3414 SelectArmIsNonZero(/* IsTrueArm */ false))
3415 return true;
3416 break;
3417 }
3418 case Instruction::PHI: {
3419 auto *PN = cast<PHINode>(I);
3421 return true;
3422
3423 // Check if all incoming values are non-zero using recursion.
3425 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
3426 return llvm::all_of(PN->operands(), [&](const Use &U) {
3427 if (U.get() == PN)
3428 return true;
3429 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
3430 // Check if the branch on the phi excludes zero.
3431 CmpPredicate Pred;
3432 Value *X;
3433 BasicBlock *TrueSucc, *FalseSucc;
3434 if (match(RecQ.CxtI,
3435 m_Br(m_c_ICmp(Pred, m_Specific(U.get()), m_Value(X)),
3436 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
3437 // Check for cases of duplicate successors.
3438 if ((TrueSucc == PN->getParent()) != (FalseSucc == PN->getParent())) {
3439 // If we're using the false successor, invert the predicate.
3440 if (FalseSucc == PN->getParent())
3441 Pred = CmpInst::getInversePredicate(Pred);
3442 if (cmpExcludesZero(Pred, X))
3443 return true;
3444 }
3445 }
3446 // Finally recurse on the edge and check it directly.
3447 return isKnownNonZero(U.get(), DemandedElts, RecQ, NewDepth);
3448 });
3449 }
3450 case Instruction::InsertElement: {
3451 if (isa<ScalableVectorType>(I->getType()))
3452 break;
3453
3454 const Value *Vec = I->getOperand(0);
3455 const Value *Elt = I->getOperand(1);
3456 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
3457
3458 unsigned NumElts = DemandedElts.getBitWidth();
3459 APInt DemandedVecElts = DemandedElts;
3460 bool SkipElt = false;
3461 // If we know the index we are inserting too, clear it from Vec check.
3462 if (CIdx && CIdx->getValue().ult(NumElts)) {
3463 DemandedVecElts.clearBit(CIdx->getZExtValue());
3464 SkipElt = !DemandedElts[CIdx->getZExtValue()];
3465 }
3466
3467 // Result is zero if Elt is non-zero and rest of the demanded elts in Vec
3468 // are non-zero.
3469 return (SkipElt || isKnownNonZero(Elt, Q, Depth)) &&
3470 (DemandedVecElts.isZero() ||
3471 isKnownNonZero(Vec, DemandedVecElts, Q, Depth));
3472 }
3473 case Instruction::ExtractElement:
3474 if (const auto *EEI = dyn_cast<ExtractElementInst>(I)) {
3475 const Value *Vec = EEI->getVectorOperand();
3476 const Value *Idx = EEI->getIndexOperand();
3477 auto *CIdx = dyn_cast<ConstantInt>(Idx);
3478 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
3479 unsigned NumElts = VecTy->getNumElements();
3480 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
3481 if (CIdx && CIdx->getValue().ult(NumElts))
3482 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
3483 return isKnownNonZero(Vec, DemandedVecElts, Q, Depth);
3484 }
3485 }
3486 break;
3487 case Instruction::ShuffleVector: {
3488 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
3489 if (!Shuf)
3490 break;
3491 APInt DemandedLHS, DemandedRHS;
3492 // For undef elements, we don't know anything about the common state of
3493 // the shuffle result.
3494 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
3495 break;
3496 // If demanded elements for both vecs are non-zero, the shuffle is non-zero.
3497 return (DemandedRHS.isZero() ||
3498 isKnownNonZero(Shuf->getOperand(1), DemandedRHS, Q, Depth)) &&
3499 (DemandedLHS.isZero() ||
3500 isKnownNonZero(Shuf->getOperand(0), DemandedLHS, Q, Depth));
3501 }
3502 case Instruction::Freeze:
3503 return isKnownNonZero(I->getOperand(0), Q, Depth) &&
3504 isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
3505 Depth);
3506 case Instruction::Load: {
3507 auto *LI = cast<LoadInst>(I);
3508 // A Load tagged with nonnull or dereferenceable with null pointer undefined
3509 // is never null.
3510 if (auto *PtrT = dyn_cast<PointerType>(I->getType())) {
3511 if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull) ||
3512 (Q.IIQ.getMetadata(LI, LLVMContext::MD_dereferenceable) &&
3513 !NullPointerIsDefined(LI->getFunction(), PtrT->getAddressSpace())))
3514 return true;
3515 } else if (MDNode *Ranges = Q.IIQ.getMetadata(LI, LLVMContext::MD_range)) {
3517 }
3518
3519 // No need to fall through to computeKnownBits as range metadata is already
3520 // handled in isKnownNonZero.
3521 return false;
3522 }
3523 case Instruction::ExtractValue: {
3524 const WithOverflowInst *WO;
3526 switch (WO->getBinaryOp()) {
3527 default:
3528 break;
3529 case Instruction::Add:
3530 return isNonZeroAdd(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3531 WO->getArgOperand(1),
3532 /*NSW=*/false,
3533 /*NUW=*/false, Depth);
3534 case Instruction::Sub:
3535 return isNonZeroSub(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3536 WO->getArgOperand(1), Depth);
3537 case Instruction::Mul:
3538 return isNonZeroMul(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3539 WO->getArgOperand(1),
3540 /*NSW=*/false, /*NUW=*/false, Depth);
3541 break;
3542 }
3543 }
3544 break;
3545 }
3546 case Instruction::Call:
3547 case Instruction::Invoke: {
3548 const auto *Call = cast<CallBase>(I);
3549 if (I->getType()->isPointerTy()) {
3550 if (Call->isReturnNonNull())
3551 return true;
3552 if (const auto *RP = getArgumentAliasingToReturnedPointer(
3553 Call, /*MustPreserveOffset=*/true))
3554 return isKnownNonZero(RP, Q, Depth);
3555 } else {
3556 if (MDNode *Ranges = Q.IIQ.getMetadata(Call, LLVMContext::MD_range))
3558 if (std::optional<ConstantRange> Range = Call->getRange()) {
3559 const APInt ZeroValue(Range->getBitWidth(), 0);
3560 if (!Range->contains(ZeroValue))
3561 return true;
3562 }
3563 if (const Value *RV = Call->getReturnedArgOperand())
3564 if (RV->getType() == I->getType() && isKnownNonZero(RV, Q, Depth))
3565 return true;
3566 }
3567
3568 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
3569 switch (II->getIntrinsicID()) {
3570 case Intrinsic::sshl_sat:
3571 case Intrinsic::ushl_sat:
3572 case Intrinsic::abs:
3573 case Intrinsic::bitreverse:
3574 case Intrinsic::bswap:
3575 case Intrinsic::ctpop:
3576 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3577 // NB: We don't do usub_sat here as in any case we can prove its
3578 // non-zero, we will fold it to `sub nuw` in InstCombine.
3579 case Intrinsic::ssub_sat:
3580 // For most types, if x != y then ssub.sat x, y != 0. But
3581 // ssub.sat.i1 0, -1 = 0, because 1 saturates to 0. This means
3582 // isNonZeroSub will do the wrong thing for ssub.sat.i1.
3583 if (BitWidth == 1)
3584 return false;
3585 return isNonZeroSub(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3586 II->getArgOperand(1), Depth);
3587 case Intrinsic::sadd_sat:
3588 return isNonZeroAdd(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3589 II->getArgOperand(1),
3590 /*NSW=*/true, /* NUW=*/false, Depth);
3591 // Vec reverse preserves zero/non-zero status from input vec.
3592 case Intrinsic::vector_reverse:
3593 return isKnownNonZero(II->getArgOperand(0), DemandedElts.reverseBits(),
3594 Q, Depth);
3595 // umin/smin/smax/smin/or of all non-zero elements is always non-zero.
3596 case Intrinsic::vector_reduce_or:
3597 case Intrinsic::vector_reduce_umax:
3598 case Intrinsic::vector_reduce_umin:
3599 case Intrinsic::vector_reduce_smax:
3600 case Intrinsic::vector_reduce_smin:
3601 return isKnownNonZero(II->getArgOperand(0), Q, Depth);
3602 case Intrinsic::umax:
3603 case Intrinsic::uadd_sat:
3604 // umax(X, (X != 0)) is non zero
3605 // X +usat (X != 0) is non zero
3606 if (matchOpWithOpEqZero(II->getArgOperand(0), II->getArgOperand(1)))
3607 return true;
3608
3609 return isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth) ||
3610 isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3611 case Intrinsic::smax: {
3612 // If either arg is strictly positive the result is non-zero. Otherwise
3613 // the result is non-zero if both ops are non-zero.
3614 auto IsNonZero = [&](Value *Op, std::optional<bool> &OpNonZero,
3615 const KnownBits &OpKnown) {
3616 if (!OpNonZero.has_value())
3617 OpNonZero = OpKnown.isNonZero() ||
3618 isKnownNonZero(Op, DemandedElts, Q, Depth);
3619 return *OpNonZero;
3620 };
3621 // Avoid re-computing isKnownNonZero.
3622 std::optional<bool> Op0NonZero, Op1NonZero;
3623 KnownBits Op1Known =
3624 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3625 if (Op1Known.isNonNegative() &&
3626 IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known))
3627 return true;
3628 KnownBits Op0Known =
3629 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3630 if (Op0Known.isNonNegative() &&
3631 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known))
3632 return true;
3633 return IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known) &&
3634 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known);
3635 }
3636 case Intrinsic::smin: {
3637 // If either arg is negative the result is non-zero. Otherwise
3638 // the result is non-zero if both ops are non-zero.
3639 KnownBits Op1Known =
3640 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3641 if (Op1Known.isNegative())
3642 return true;
3643 KnownBits Op0Known =
3644 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3645 if (Op0Known.isNegative())
3646 return true;
3647
3648 if (Op1Known.isNonZero() && Op0Known.isNonZero())
3649 return true;
3650 }
3651 [[fallthrough]];
3652 case Intrinsic::umin:
3653 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth) &&
3654 isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth);
3655 case Intrinsic::cttz:
3656 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3657 .Zero[0];
3658 case Intrinsic::ctlz:
3659 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3660 .isNonNegative();
3661 case Intrinsic::fshr:
3662 case Intrinsic::fshl:
3663 // If Op0 == Op1, this is a rotate. rotate(x, y) != 0 iff x != 0.
3664 if (II->getArgOperand(0) == II->getArgOperand(1))
3665 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3666 break;
3667 case Intrinsic::vscale:
3668 return true;
3669 case Intrinsic::experimental_get_vector_length:
3670 return isKnownNonZero(I->getOperand(0), Q, Depth);
3671 default:
3672 break;
3673 }
3674 break;
3675 }
3676
3677 return false;
3678 }
3679 }
3680
3681 KnownBits Known(BitWidth);
3682 computeKnownBits(I, DemandedElts, Known, Q, Depth);
3683 return Known.One != 0;
3684}
3685
3686/// Return true if the given value is known to be non-zero when defined. For
3687/// vectors, return true if every demanded element is known to be non-zero when
3688/// defined. For pointers, if the context instruction and dominator tree are
3689/// specified, perform context-sensitive analysis and return true if the
3690/// pointer couldn't possibly be null at the specified instruction.
3691/// Supports values with integer or pointer type and vectors of integers.
3692bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
3693 const SimplifyQuery &Q, unsigned Depth) {
3694 Type *Ty = V->getType();
3695
3696#ifndef NDEBUG
3697 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
3698
3699 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3700 assert(
3701 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
3702 "DemandedElt width should equal the fixed vector number of elements");
3703 } else {
3704 assert(DemandedElts == APInt(1, 1) &&
3705 "DemandedElt width should be 1 for scalars");
3706 }
3707#endif
3708
3709 if (auto *C = dyn_cast<Constant>(V)) {
3710 if (C->isNullValue())
3711 return false;
3712 if (isa<ConstantInt>(C))
3713 // Must be non-zero due to null test above.
3714 return true;
3715
3716 // For constant vectors, check that all elements are poison or known
3717 // non-zero to determine that the whole vector is known non-zero.
3718 if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
3719 for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
3720 if (!DemandedElts[i])
3721 continue;
3722 Constant *Elt = C->getAggregateElement(i);
3723 if (!Elt || Elt->isNullValue())
3724 return false;
3725 if (!isa<PoisonValue>(Elt) && !isa<ConstantInt>(Elt))
3726 return false;
3727 }
3728 return true;
3729 }
3730
3731 // Constant ptrauth can be null, iff the base pointer can be.
3732 if (auto *CPA = dyn_cast<ConstantPtrAuth>(V))
3733 return isKnownNonZero(CPA->getPointer(), DemandedElts, Q, Depth);
3734
3735 // A global variable in address space 0 is non null unless extern weak
3736 // or an absolute symbol reference. Other address spaces may have null as a
3737 // valid address for a global, so we can't assume anything.
3738 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
3739 if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&
3740 GV->getType()->getAddressSpace() == 0)
3741 return true;
3742 }
3743
3744 // For constant expressions, fall through to the Operator code below.
3745 if (!isa<ConstantExpr>(V))
3746 return false;
3747 }
3748
3749 if (const auto *A = dyn_cast<Argument>(V))
3750 if (std::optional<ConstantRange> Range = A->getRange()) {
3751 const APInt ZeroValue(Range->getBitWidth(), 0);
3752 if (!Range->contains(ZeroValue))
3753 return true;
3754 }
3755
3756 if (!isa<Constant>(V) && isKnownNonZeroFromAssume(V, Q))
3757 return true;
3758
3759 // Some of the tests below are recursive, so bail out if we hit the limit.
3761 return false;
3762
3763 // Check for pointer simplifications.
3764
3765 if (PointerType *PtrTy = dyn_cast<PointerType>(Ty)) {
3766 // A byval, inalloca may not be null in a non-default addres space. A
3767 // nonnull argument is assumed never 0.
3768 if (const Argument *A = dyn_cast<Argument>(V)) {
3769 if (((A->hasPassPointeeByValueCopyAttr() &&
3770 !NullPointerIsDefined(A->getParent(), PtrTy->getAddressSpace())) ||
3771 A->hasNonNullAttr()))
3772 return true;
3773 }
3774 }
3775
3776 if (const auto *I = dyn_cast<Operator>(V))
3777 if (isKnownNonZeroFromOperator(I, DemandedElts, Q, Depth))
3778 return true;
3779
3780 if (!isa<Constant>(V) &&
3782 return true;
3783
3784 if (const Value *Stripped = stripNullTest(V))
3785 return isKnownNonZero(Stripped, DemandedElts, Q, Depth);
3786
3787 return false;
3788}
3789
3791 unsigned Depth) {
3792 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
3793 APInt DemandedElts =
3794 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
3795 return ::isKnownNonZero(V, DemandedElts, Q, Depth);
3796}
3797
3798/// If the pair of operators are the same invertible function, return the
3799/// the operands of the function corresponding to each input. Otherwise,
3800/// return std::nullopt. An invertible function is one that is 1-to-1 and maps
3801/// every input value to exactly one output value. This is equivalent to
3802/// saying that Op1 and Op2 are equal exactly when the specified pair of
3803/// operands are equal, (except that Op1 and Op2 may be poison more often.)
3804static std::optional<std::pair<Value*, Value*>>
3806 const Operator *Op2) {
3807 if (Op1->getOpcode() != Op2->getOpcode())
3808 return std::nullopt;
3809
3810 auto getOperands = [&](unsigned OpNum) -> auto {
3811 return std::make_pair(Op1->getOperand(OpNum), Op2->getOperand(OpNum));
3812 };
3813
3814 switch (Op1->getOpcode()) {
3815 default:
3816 break;
3817 case Instruction::Or:
3818 if (!cast<PossiblyDisjointInst>(Op1)->isDisjoint() ||
3819 !cast<PossiblyDisjointInst>(Op2)->isDisjoint())
3820 break;
3821 [[fallthrough]];
3822 case Instruction::Xor:
3823 case Instruction::Add: {
3824 Value *Other;
3825 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(0)), m_Value(Other))))
3826 return std::make_pair(Op1->getOperand(1), Other);
3827 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(1)), m_Value(Other))))
3828 return std::make_pair(Op1->getOperand(0), Other);
3829 break;
3830 }
3831 case Instruction::Sub:
3832 if (Op1->getOperand(0) == Op2->getOperand(0))
3833 return getOperands(1);
3834 if (Op1->getOperand(1) == Op2->getOperand(1))
3835 return getOperands(0);
3836 break;
3837 case Instruction::Mul: {
3838 // invertible if A * B == (A * B) mod 2^N where A, and B are integers
3839 // and N is the bitwdith. The nsw case is non-obvious, but proven by
3840 // alive2: https://alive2.llvm.org/ce/z/Z6D5qK
3841 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3842 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3843 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3844 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3845 break;
3846
3847 // Assume operand order has been canonicalized
3848 if (Op1->getOperand(1) == Op2->getOperand(1) &&
3849 isa<ConstantInt>(Op1->getOperand(1)) &&
3850 !cast<ConstantInt>(Op1->getOperand(1))->isZero())
3851 return getOperands(0);
3852 break;
3853 }
3854 case Instruction::Shl: {
3855 // Same as multiplies, with the difference that we don't need to check
3856 // for a non-zero multiply. Shifts always multiply by non-zero.
3857 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3858 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3859 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3860 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3861 break;
3862
3863 if (Op1->getOperand(1) == Op2->getOperand(1))
3864 return getOperands(0);
3865 break;
3866 }
3867 case Instruction::AShr:
3868 case Instruction::LShr: {
3869 auto *PEO1 = cast<PossiblyExactOperator>(Op1);
3870 auto *PEO2 = cast<PossiblyExactOperator>(Op2);
3871 if (!PEO1->isExact() || !PEO2->isExact())
3872 break;
3873
3874 if (Op1->getOperand(1) == Op2->getOperand(1))
3875 return getOperands(0);
3876 break;
3877 }
3878 case Instruction::SExt:
3879 case Instruction::ZExt:
3880 if (Op1->getOperand(0)->getType() == Op2->getOperand(0)->getType())
3881 return getOperands(0);
3882 break;
3883 case Instruction::PHI: {
3884 const PHINode *PN1 = cast<PHINode>(Op1);
3885 const PHINode *PN2 = cast<PHINode>(Op2);
3886
3887 // If PN1 and PN2 are both recurrences, can we prove the entire recurrences
3888 // are a single invertible function of the start values? Note that repeated
3889 // application of an invertible function is also invertible
3890 BinaryOperator *BO1 = nullptr;
3891 Value *Start1 = nullptr, *Step1 = nullptr;
3892 BinaryOperator *BO2 = nullptr;
3893 Value *Start2 = nullptr, *Step2 = nullptr;
3894 if (PN1->getParent() != PN2->getParent() ||
3895 !matchSimpleRecurrence(PN1, BO1, Start1, Step1) ||
3896 !matchSimpleRecurrence(PN2, BO2, Start2, Step2))
3897 break;
3898
3899 auto Values = getInvertibleOperands(cast<Operator>(BO1),
3900 cast<Operator>(BO2));
3901 if (!Values)
3902 break;
3903
3904 // We have to be careful of mutually defined recurrences here. Ex:
3905 // * X_i = X_(i-1) OP Y_(i-1), and Y_i = X_(i-1) OP V
3906 // * X_i = Y_i = X_(i-1) OP Y_(i-1)
3907 // The invertibility of these is complicated, and not worth reasoning
3908 // about (yet?).
3909 if (Values->first != PN1 || Values->second != PN2)
3910 break;
3911
3912 return std::make_pair(Start1, Start2);
3913 }
3914 }
3915 return std::nullopt;
3916}
3917
3918/// Return true if V1 == (binop V2, X), where X is known non-zero.
3919/// Only handle a small subset of binops where (binop V2, X) with non-zero X
3920/// implies V2 != V1.
3921static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2,
3922 const APInt &DemandedElts,
3923 const SimplifyQuery &Q, unsigned Depth) {
3925 if (!BO)
3926 return false;
3927 switch (BO->getOpcode()) {
3928 default:
3929 break;
3930 case Instruction::Or:
3931 if (!cast<PossiblyDisjointInst>(V1)->isDisjoint())
3932 break;
3933 [[fallthrough]];
3934 case Instruction::Xor:
3935 case Instruction::Add:
3936 Value *Op = nullptr;
3937 if (V2 == BO->getOperand(0))
3938 Op = BO->getOperand(1);
3939 else if (V2 == BO->getOperand(1))
3940 Op = BO->getOperand(0);
3941 else
3942 return false;
3943 return isKnownNonZero(Op, DemandedElts, Q, Depth + 1);
3944 }
3945 return false;
3946}
3947
3948/// Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and
3949/// the multiplication is nuw or nsw.
3950static bool isNonEqualMul(const Value *V1, const Value *V2,
3951 const APInt &DemandedElts, const SimplifyQuery &Q,
3952 unsigned Depth) {
3953 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3954 const APInt *C;
3955 return match(OBO, m_Mul(m_Specific(V1), m_APInt(C))) &&
3956 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3957 !C->isZero() && !C->isOne() &&
3958 isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3959 }
3960 return false;
3961}
3962
3963/// Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and
3964/// the shift is nuw or nsw.
3965static bool isNonEqualShl(const Value *V1, const Value *V2,
3966 const APInt &DemandedElts, const SimplifyQuery &Q,
3967 unsigned Depth) {
3968 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3969 const APInt *C;
3970 return match(OBO, m_Shl(m_Specific(V1), m_APInt(C))) &&
3971 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3972 !C->isZero() && isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3973 }
3974 return false;
3975}
3976
3977static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2,
3978 const APInt &DemandedElts, const SimplifyQuery &Q,
3979 unsigned Depth) {
3980 // Check two PHIs are in same block.
3981 if (PN1->getParent() != PN2->getParent())
3982 return false;
3983
3985 bool UsedFullRecursion = false;
3986 for (const BasicBlock *IncomBB : PN1->blocks()) {
3987 if (!VisitedBBs.insert(IncomBB).second)
3988 continue; // Don't reprocess blocks that we have dealt with already.
3989 const Value *IV1 = PN1->getIncomingValueForBlock(IncomBB);
3990 const Value *IV2 = PN2->getIncomingValueForBlock(IncomBB);
3991 const APInt *C1, *C2;
3992 if (match(IV1, m_APInt(C1)) && match(IV2, m_APInt(C2)) && *C1 != *C2)
3993 continue;
3994
3995 // Only one pair of phi operands is allowed for full recursion.
3996 if (UsedFullRecursion)
3997 return false;
3998
4000 RecQ.CxtI = IncomBB->getTerminator();
4001 if (!isKnownNonEqual(IV1, IV2, DemandedElts, RecQ, Depth + 1))
4002 return false;
4003 UsedFullRecursion = true;
4004 }
4005 return true;
4006}
4007
4008static bool isNonEqualSelect(const Value *V1, const Value *V2,
4009 const APInt &DemandedElts, const SimplifyQuery &Q,
4010 unsigned Depth) {
4011 const SelectInst *SI1 = dyn_cast<SelectInst>(V1);
4012 if (!SI1)
4013 return false;
4014
4015 if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2)) {
4016 const Value *Cond1 = SI1->getCondition();
4017 const Value *Cond2 = SI2->getCondition();
4018 if (Cond1 == Cond2)
4019 return isKnownNonEqual(SI1->getTrueValue(), SI2->getTrueValue(),
4020 DemandedElts, Q, Depth + 1) &&
4021 isKnownNonEqual(SI1->getFalseValue(), SI2->getFalseValue(),
4022 DemandedElts, Q, Depth + 1);
4023 }
4024 return isKnownNonEqual(SI1->getTrueValue(), V2, DemandedElts, Q, Depth + 1) &&
4025 isKnownNonEqual(SI1->getFalseValue(), V2, DemandedElts, Q, Depth + 1);
4026}
4027
4028// Check to see if A is both a GEP and is the incoming value for a PHI in the
4029// loop, and B is either a ptr or another GEP. If the PHI has 2 incoming values,
4030// one of them being the recursive GEP A and the other a ptr at same base and at
4031// the same/higher offset than B we are only incrementing the pointer further in
4032// loop if offset of recursive GEP is greater than 0.
4034 const SimplifyQuery &Q) {
4035 if (!A->getType()->isPointerTy() || !B->getType()->isPointerTy())
4036 return false;
4037
4038 auto *GEPA = dyn_cast<GEPOperator>(A);
4039 if (!GEPA || GEPA->getNumIndices() != 1 || !isa<Constant>(GEPA->idx_begin()))
4040 return false;
4041
4042 // Handle 2 incoming PHI values with one being a recursive GEP.
4043 auto *PN = dyn_cast<PHINode>(GEPA->getPointerOperand());
4044 if (!PN || PN->getNumIncomingValues() != 2)
4045 return false;
4046
4047 // Search for the recursive GEP as an incoming operand, and record that as
4048 // Step.
4049 Value *Start = nullptr;
4050 Value *Step = const_cast<Value *>(A);
4051 if (PN->getIncomingValue(0) == Step)
4052 Start = PN->getIncomingValue(1);
4053 else if (PN->getIncomingValue(1) == Step)
4054 Start = PN->getIncomingValue(0);
4055 else
4056 return false;
4057
4058 // Other incoming node base should match the B base.
4059 // StartOffset >= OffsetB && StepOffset > 0?
4060 // StartOffset <= OffsetB && StepOffset < 0?
4061 // Is non-equal if above are true.
4062 // We use stripAndAccumulateInBoundsConstantOffsets to restrict the
4063 // optimisation to inbounds GEPs only.
4064 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(Start->getType());
4065 APInt StartOffset(IndexWidth, 0);
4066 Start = Start->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StartOffset);
4067 APInt StepOffset(IndexWidth, 0);
4068 Step = Step->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StepOffset);
4069
4070 // Check if Base Pointer of Step matches the PHI.
4071 if (Step != PN)
4072 return false;
4073 APInt OffsetB(IndexWidth, 0);
4074 B = B->stripAndAccumulateInBoundsConstantOffsets(Q.DL, OffsetB);
4075 return Start == B &&
4076 ((StartOffset.sge(OffsetB) && StepOffset.isStrictlyPositive()) ||
4077 (StartOffset.sle(OffsetB) && StepOffset.isNegative()));
4078}
4079
4080static bool isKnownNonEqualFromContext(const Value *V1, const Value *V2,
4081 const SimplifyQuery &Q, unsigned Depth) {
4082 if (!Q.CxtI)
4083 return false;
4084
4085 // Try to infer NonEqual based on information from dominating conditions.
4086 if (Q.DC && Q.DT) {
4087 auto IsKnownNonEqualFromDominatingCondition = [&](const Value *V) {
4088 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
4089 Value *Cond = BI->getCondition();
4090 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4091 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()) &&
4093 /*LHSIsTrue=*/true, Depth)
4094 .value_or(false))
4095 return true;
4096
4097 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4098 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()) &&
4100 /*LHSIsTrue=*/false, Depth)
4101 .value_or(false))
4102 return true;
4103 }
4104
4105 return false;
4106 };
4107
4108 if (IsKnownNonEqualFromDominatingCondition(V1) ||
4109 IsKnownNonEqualFromDominatingCondition(V2))
4110 return true;
4111 }
4112
4113 if (!Q.AC)
4114 return false;
4115
4116 // Try to infer NonEqual based on information from assumptions.
4117 for (auto &AssumeVH : Q.AC->assumptionsFor(V1)) {
4118 if (!AssumeVH)
4119 continue;
4120 CallInst *I = cast<CallInst>(AssumeVH);
4121
4122 assert(I->getFunction() == Q.CxtI->getFunction() &&
4123 "Got assumption for the wrong function!");
4124 assert(I->getIntrinsicID() == Intrinsic::assume &&
4125 "must be an assume intrinsic");
4126
4127 if (isImpliedCondition(I->getArgOperand(0), ICmpInst::ICMP_NE, V1, V2, Q.DL,
4128 /*LHSIsTrue=*/true, Depth)
4129 .value_or(false) &&
4131 return true;
4132 }
4133
4134 return false;
4135}
4136
4137/// Return true if it is known that V1 != V2.
4138static bool isKnownNonEqual(const Value *V1, const Value *V2,
4139 const APInt &DemandedElts, const SimplifyQuery &Q,
4140 unsigned Depth) {
4141 if (V1 == V2)
4142 return false;
4143 if (V1->getType() != V2->getType())
4144 // We can't look through casts yet.
4145 return false;
4146
4148 return false;
4149
4150 // See if we can recurse through (exactly one of) our operands. This
4151 // requires our operation be 1-to-1 and map every input value to exactly
4152 // one output value. Such an operation is invertible.
4153 auto *O1 = dyn_cast<Operator>(V1);
4154 auto *O2 = dyn_cast<Operator>(V2);
4155 if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) {
4156 if (auto Values = getInvertibleOperands(O1, O2))
4157 return isKnownNonEqual(Values->first, Values->second, DemandedElts, Q,
4158 Depth + 1);
4159
4160 if (const PHINode *PN1 = dyn_cast<PHINode>(V1)) {
4161 const PHINode *PN2 = cast<PHINode>(V2);
4162 // FIXME: This is missing a generalization to handle the case where one is
4163 // a PHI and another one isn't.
4164 if (isNonEqualPHIs(PN1, PN2, DemandedElts, Q, Depth))
4165 return true;
4166 };
4167 }
4168
4169 if (isModifyingBinopOfNonZero(V1, V2, DemandedElts, Q, Depth) ||
4170 isModifyingBinopOfNonZero(V2, V1, DemandedElts, Q, Depth))
4171 return true;
4172
4173 if (isNonEqualMul(V1, V2, DemandedElts, Q, Depth) ||
4174 isNonEqualMul(V2, V1, DemandedElts, Q, Depth))
4175 return true;
4176
4177 if (isNonEqualShl(V1, V2, DemandedElts, Q, Depth) ||
4178 isNonEqualShl(V2, V1, DemandedElts, Q, Depth))
4179 return true;
4180
4181 if (V1->getType()->isIntOrIntVectorTy()) {
4182 // Are any known bits in V1 contradictory to known bits in V2? If V1
4183 // has a known zero where V2 has a known one, they must not be equal.
4184 KnownBits Known1 = computeKnownBits(V1, DemandedElts, Q, Depth);
4185 if (!Known1.isUnknown()) {
4186 KnownBits Known2 = computeKnownBits(V2, DemandedElts, Q, Depth);
4187 if (Known1.Zero.intersects(Known2.One) ||
4188 Known2.Zero.intersects(Known1.One))
4189 return true;
4190 }
4191 }
4192
4193 if (isNonEqualSelect(V1, V2, DemandedElts, Q, Depth) ||
4194 isNonEqualSelect(V2, V1, DemandedElts, Q, Depth))
4195 return true;
4196
4199 return true;
4200
4201 Value *A, *B;
4202 // PtrToInts are NonEqual if their Ptrs are NonEqual.
4203 // Check PtrToInt type matches the pointer size.
4204 if (match(V1, m_PtrToIntSameSize(Q.DL, m_Value(A))) &&
4206 return isKnownNonEqual(A, B, DemandedElts, Q, Depth + 1);
4207
4208 if (isKnownNonEqualFromContext(V1, V2, Q, Depth))
4209 return true;
4210
4211 return false;
4212}
4213
4214/// For vector constants, loop over the elements and find the constant with the
4215/// minimum number of sign bits. Return 0 if the value is not a vector constant
4216/// or if any element was not analyzed; otherwise, return the count for the
4217/// element with the minimum number of sign bits.
4219 const APInt &DemandedElts,
4220 unsigned TyBits) {
4221 const auto *CV = dyn_cast<Constant>(V);
4222 if (!CV || !isa<FixedVectorType>(CV->getType()))
4223 return 0;
4224
4225 unsigned MinSignBits = TyBits;
4226 unsigned NumElts = cast<FixedVectorType>(CV->getType())->getNumElements();
4227 for (unsigned i = 0; i != NumElts; ++i) {
4228 if (!DemandedElts[i])
4229 continue;
4230 // If we find a non-ConstantInt, bail out.
4231 auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));
4232 if (!Elt)
4233 return 0;
4234
4235 MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());
4236 }
4237
4238 return MinSignBits;
4239}
4240
4241static unsigned ComputeNumSignBitsImpl(const Value *V,
4242 const APInt &DemandedElts,
4243 const SimplifyQuery &Q, unsigned Depth);
4244
4245static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
4246 const SimplifyQuery &Q, unsigned Depth) {
4247 unsigned Result = ComputeNumSignBitsImpl(V, DemandedElts, Q, Depth);
4248 assert(Result > 0 && "At least one sign bit needs to be present!");
4249 return Result;
4250}
4251
4252/// Return the number of times the sign bit of the register is replicated into
4253/// the other bits. We know that at least 1 bit is always equal to the sign bit
4254/// (itself), but other cases can give us information. For example, immediately
4255/// after an "ashr X, 2", we know that the top 3 bits are all equal to each
4256/// other, so we return 3. For vectors, return the number of sign bits for the
4257/// vector element with the minimum number of known sign bits of the demanded
4258/// elements in the vector specified by DemandedElts.
4259static unsigned ComputeNumSignBitsImpl(const Value *V,
4260 const APInt &DemandedElts,
4261 const SimplifyQuery &Q, unsigned Depth) {
4262 Type *Ty = V->getType();
4263#ifndef NDEBUG
4264 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4265
4266 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
4267 assert(
4268 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
4269 "DemandedElt width should equal the fixed vector number of elements");
4270 } else {
4271 assert(DemandedElts == APInt(1, 1) &&
4272 "DemandedElt width should be 1 for scalars");
4273 }
4274#endif
4275
4276 // We return the minimum number of sign bits that are guaranteed to be present
4277 // in V, so for undef we have to conservatively return 1. We don't have the
4278 // same behavior for poison though -- that's a FIXME today.
4279
4280 Type *ScalarTy = Ty->getScalarType();
4281 unsigned TyBits = ScalarTy->isPointerTy() ?
4282 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
4283 Q.DL.getTypeSizeInBits(ScalarTy);
4284
4285 unsigned Tmp, Tmp2;
4286 unsigned FirstAnswer = 1;
4287
4288 // Note that ConstantInt is handled by the general computeKnownBits case
4289 // below.
4290
4292 return 1;
4293
4294 if (auto *U = dyn_cast<Operator>(V)) {
4295 switch (Operator::getOpcode(V)) {
4296 default: break;
4297 case Instruction::BitCast: {
4298 Value *Src = U->getOperand(0);
4299 Type *SrcTy = Src->getType();
4300
4301 // Skip if the source type is not an integer or integer vector type
4302 // This ensures we only process integer-like types
4303 if (!SrcTy->isIntOrIntVectorTy())
4304 break;
4305
4306 unsigned SrcBits = SrcTy->getScalarSizeInBits();
4307
4308 // Bitcast 'large element' scalar/vector to 'small element' vector.
4309 if ((SrcBits % TyBits) != 0)
4310 break;
4311
4312 // Only proceed if the destination type is a fixed-size vector
4313 if (isa<FixedVectorType>(Ty)) {
4314 // Fast case - sign splat can be simply split across the small elements.
4315 // This works for both vector and scalar sources
4316 Tmp = ComputeNumSignBits(Src, Q, Depth + 1);
4317 if (Tmp == SrcBits)
4318 return TyBits;
4319 }
4320 break;
4321 }
4322 case Instruction::SExt:
4323 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
4324 return ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1) +
4325 Tmp;
4326
4327 case Instruction::SDiv: {
4328 const APInt *Denominator;
4329 // sdiv X, C -> adds log(C) sign bits.
4330 if (match(U->getOperand(1), m_APInt(Denominator))) {
4331
4332 // Ignore non-positive denominator.
4333 if (!Denominator->isStrictlyPositive())
4334 break;
4335
4336 // Calculate the incoming numerator bits.
4337 unsigned NumBits =
4338 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4339
4340 // Add floor(log(C)) bits to the numerator bits.
4341 return std::min(TyBits, NumBits + Denominator->logBase2());
4342 }
4343 break;
4344 }
4345
4346 case Instruction::SRem: {
4347 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4348
4349 const APInt *Denominator;
4350 // srem X, C -> we know that the result is within [-C+1,C) when C is a
4351 // positive constant. This let us put a lower bound on the number of sign
4352 // bits.
4353 if (match(U->getOperand(1), m_APInt(Denominator))) {
4354
4355 // Ignore non-positive denominator.
4356 if (Denominator->isStrictlyPositive()) {
4357 // Calculate the leading sign bit constraints by examining the
4358 // denominator. Given that the denominator is positive, there are two
4359 // cases:
4360 //
4361 // 1. The numerator is positive. The result range is [0,C) and
4362 // [0,C) u< (1 << ceilLogBase2(C)).
4363 //
4364 // 2. The numerator is negative. Then the result range is (-C,0] and
4365 // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
4366 //
4367 // Thus a lower bound on the number of sign bits is `TyBits -
4368 // ceilLogBase2(C)`.
4369
4370 unsigned ResBits = TyBits - Denominator->ceilLogBase2();
4371 Tmp = std::max(Tmp, ResBits);
4372 }
4373 }
4374 return Tmp;
4375 }
4376
4377 case Instruction::AShr: {
4378 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4379 // ashr X, C -> adds C sign bits. Vectors too.
4380 const APInt *ShAmt;
4381 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4382 if (ShAmt->uge(TyBits))
4383 break; // Bad shift.
4384 unsigned ShAmtLimited = ShAmt->getZExtValue();
4385 Tmp += ShAmtLimited;
4386 if (Tmp > TyBits) Tmp = TyBits;
4387 }
4388 return Tmp;
4389 }
4390 case Instruction::Shl: {
4391 const APInt *ShAmt;
4392 Value *X = nullptr;
4393 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4394 // shl destroys sign bits.
4395 if (ShAmt->uge(TyBits))
4396 break; // Bad shift.
4397 // We can look through a zext (more or less treating it as a sext) if
4398 // all extended bits are shifted out.
4399 if (match(U->getOperand(0), m_ZExt(m_Value(X))) &&
4400 ShAmt->uge(TyBits - X->getType()->getScalarSizeInBits())) {
4401 Tmp = ComputeNumSignBits(X, DemandedElts, Q, Depth + 1);
4402 Tmp += TyBits - X->getType()->getScalarSizeInBits();
4403 } else
4404 Tmp =
4405 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4406 if (ShAmt->uge(Tmp))
4407 break; // Shifted all sign bits out.
4408 Tmp2 = ShAmt->getZExtValue();
4409 return Tmp - Tmp2;
4410 }
4411 break;
4412 }
4413 case Instruction::And:
4414 case Instruction::Or:
4415 case Instruction::Xor: // NOT is handled here.
4416 // Logical binary ops preserve the number of sign bits at the worst.
4417 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4418 if (Tmp != 1) {
4419 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4420 FirstAnswer = std::min(Tmp, Tmp2);
4421 // We computed what we know about the sign bits as our first
4422 // answer. Now proceed to the generic code that uses
4423 // computeKnownBits, and pick whichever answer is better.
4424 }
4425 break;
4426
4427 case Instruction::Select: {
4428 // If we have a clamp pattern, we know that the number of sign bits will
4429 // be the minimum of the clamp min/max range.
4430 const Value *X;
4431 const APInt *CLow, *CHigh;
4432 if (isSignedMinMaxClamp(U, X, CLow, CHigh))
4433 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4434
4435 Tmp = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4436 if (Tmp == 1)
4437 break;
4438 Tmp2 = ComputeNumSignBits(U->getOperand(2), DemandedElts, Q, Depth + 1);
4439 return std::min(Tmp, Tmp2);
4440 }
4441
4442 case Instruction::Add:
4443 // Add can have at most one carry bit. Thus we know that the output
4444 // is, at worst, one more bit than the inputs.
4445 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4446 if (Tmp == 1) break;
4447
4448 // Special case decrementing a value (ADD X, -1):
4449 if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
4450 if (CRHS->isAllOnesValue()) {
4451 KnownBits Known(TyBits);
4452 computeKnownBits(U->getOperand(0), DemandedElts, Known, Q, Depth + 1);
4453
4454 // If the input is known to be 0 or 1, the output is 0/-1, which is
4455 // all sign bits set.
4456 if ((Known.Zero | 1).isAllOnes())
4457 return TyBits;
4458
4459 // If we are subtracting one from a positive number, there is no carry
4460 // out of the result.
4461 if (Known.isNonNegative())
4462 return Tmp;
4463 }
4464
4465 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4466 if (Tmp2 == 1)
4467 break;
4468 return std::min(Tmp, Tmp2) - 1;
4469
4470 case Instruction::Sub:
4471 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4472 if (Tmp2 == 1)
4473 break;
4474
4475 // Handle NEG.
4476 if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
4477 if (CLHS->isNullValue()) {
4478 KnownBits Known(TyBits);
4479 computeKnownBits(U->getOperand(1), DemandedElts, Known, Q, Depth + 1);
4480 // If the input is known to be 0 or 1, the output is 0/-1, which is
4481 // all sign bits set.
4482 if ((Known.Zero | 1).isAllOnes())
4483 return TyBits;
4484
4485 // If the input is known to be positive (the sign bit is known clear),
4486 // the output of the NEG has the same number of sign bits as the
4487 // input.
4488 if (Known.isNonNegative())
4489 return Tmp2;
4490
4491 // Otherwise, we treat this like a SUB.
4492 }
4493
4494 // Sub can have at most one carry bit. Thus we know that the output
4495 // is, at worst, one more bit than the inputs.
4496 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4497 if (Tmp == 1)
4498 break;
4499 return std::min(Tmp, Tmp2) - 1;
4500
4501 case Instruction::Mul: {
4502 // The output of the Mul can be at most twice the valid bits in the
4503 // inputs.
4504 unsigned SignBitsOp0 =
4505 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4506 if (SignBitsOp0 == 1)
4507 break;
4508 unsigned SignBitsOp1 =
4509 ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4510 if (SignBitsOp1 == 1)
4511 break;
4512 unsigned OutValidBits =
4513 (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);
4514 return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
4515 }
4516
4517 case Instruction::PHI: {
4518 const PHINode *PN = cast<PHINode>(U);
4519 unsigned NumIncomingValues = PN->getNumIncomingValues();
4520 // Don't analyze large in-degree PHIs.
4521 if (NumIncomingValues > 4) break;
4522 // Unreachable blocks may have zero-operand PHI nodes.
4523 if (NumIncomingValues == 0) break;
4524
4525 // Take the minimum of all incoming values. This can't infinitely loop
4526 // because of our depth threshold.
4528 Tmp = TyBits;
4529 for (unsigned i = 0, e = NumIncomingValues; i != e; ++i) {
4530 if (Tmp == 1) return Tmp;
4531 RecQ.CxtI = PN->getIncomingBlock(i)->getTerminator();
4532 Tmp = std::min(Tmp, ComputeNumSignBits(PN->getIncomingValue(i),
4533 DemandedElts, RecQ, Depth + 1));
4534 }
4535 return Tmp;
4536 }
4537
4538 case Instruction::Trunc: {
4539 // If the input contained enough sign bits that some remain after the
4540 // truncation, then we can make use of that. Otherwise we don't know
4541 // anything.
4542 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4543 unsigned OperandTyBits = U->getOperand(0)->getType()->getScalarSizeInBits();
4544 if (Tmp > (OperandTyBits - TyBits))
4545 return Tmp - (OperandTyBits - TyBits);
4546
4547 return 1;
4548 }
4549
4550 case Instruction::ExtractElement:
4551 // Look through extract element. At the moment we keep this simple and
4552 // skip tracking the specific element. But at least we might find
4553 // information valid for all elements of the vector (for example if vector
4554 // is sign extended, shifted, etc).
4555 return ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4556
4557 case Instruction::ShuffleVector: {
4558 // Collect the minimum number of sign bits that are shared by every vector
4559 // element referenced by the shuffle.
4560 auto *Shuf = dyn_cast<ShuffleVectorInst>(U);
4561 if (!Shuf) {
4562 // FIXME: Add support for shufflevector constant expressions.
4563 return 1;
4564 }
4565 APInt DemandedLHS, DemandedRHS;
4566 // For undef elements, we don't know anything about the common state of
4567 // the shuffle result.
4568 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
4569 return 1;
4570 Tmp = std::numeric_limits<unsigned>::max();
4571 if (!!DemandedLHS) {
4572 const Value *LHS = Shuf->getOperand(0);
4573 Tmp = ComputeNumSignBits(LHS, DemandedLHS, Q, Depth + 1);
4574 }
4575 // If we don't know anything, early out and try computeKnownBits
4576 // fall-back.
4577 if (Tmp == 1)
4578 break;
4579 if (!!DemandedRHS) {
4580 const Value *RHS = Shuf->getOperand(1);
4581 Tmp2 = ComputeNumSignBits(RHS, DemandedRHS, Q, Depth + 1);
4582 Tmp = std::min(Tmp, Tmp2);
4583 }
4584 // If we don't know anything, early out and try computeKnownBits
4585 // fall-back.
4586 if (Tmp == 1)
4587 break;
4588 assert(Tmp <= TyBits && "Failed to determine minimum sign bits");
4589 return Tmp;
4590 }
4591 case Instruction::Call: {
4592 if (const auto *II = dyn_cast<IntrinsicInst>(U)) {
4593 switch (II->getIntrinsicID()) {
4594 default:
4595 break;
4596 case Intrinsic::abs:
4597 Tmp =
4598 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4599 if (Tmp == 1)
4600 break;
4601
4602 // Absolute value reduces number of sign bits by at most 1.
4603 return Tmp - 1;
4604 case Intrinsic::smin:
4605 case Intrinsic::smax: {
4606 const APInt *CLow, *CHigh;
4607 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
4608 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4609 }
4610 }
4611 }
4612 }
4613 }
4614 }
4615
4616 // Finally, if we can prove that the top bits of the result are 0's or 1's,
4617 // use this information.
4618
4619 // If we can examine all elements of a vector constant successfully, we're
4620 // done (we can't do any better than that). If not, keep trying.
4621 if (unsigned VecSignBits =
4622 computeNumSignBitsVectorConstant(V, DemandedElts, TyBits))
4623 return VecSignBits;
4624
4625 KnownBits Known(TyBits);
4626 computeKnownBits(V, DemandedElts, Known, Q, Depth);
4627
4628 // If we know that the sign bit is either zero or one, determine the number of
4629 // identical bits in the top of the input value.
4630 return std::max(FirstAnswer, Known.countMinSignBits());
4631}
4632
4634 const TargetLibraryInfo *TLI) {
4635 const Function *F = CB.getCalledFunction();
4636 if (!F)
4638
4639 if (F->isIntrinsic())
4640 return F->getIntrinsicID();
4641
4642 // We are going to infer semantics of a library function based on mapping it
4643 // to an LLVM intrinsic. Check that the library function is available from
4644 // this callbase and in this environment.
4645 LibFunc Func;
4646 if (F->hasLocalLinkage() || !TLI || !TLI->getLibFunc(CB, Func) ||
4647 !CB.onlyReadsMemory())
4649
4650 switch (Func) {
4651 default:
4652 break;
4653 case LibFunc_sin:
4654 case LibFunc_sinf:
4655 case LibFunc_sinl:
4656 return Intrinsic::sin;
4657 case LibFunc_cos:
4658 case LibFunc_cosf:
4659 case LibFunc_cosl:
4660 return Intrinsic::cos;
4661 case LibFunc_tan:
4662 case LibFunc_tanf:
4663 case LibFunc_tanl:
4664 return Intrinsic::tan;
4665 case LibFunc_asin:
4666 case LibFunc_asinf:
4667 case LibFunc_asinl:
4668 return Intrinsic::asin;
4669 case LibFunc_acos:
4670 case LibFunc_acosf:
4671 case LibFunc_acosl:
4672 return Intrinsic::acos;
4673 case LibFunc_atan:
4674 case LibFunc_atanf:
4675 case LibFunc_atanl:
4676 return Intrinsic::atan;
4677 case LibFunc_atan2:
4678 case LibFunc_atan2f:
4679 case LibFunc_atan2l:
4680 return Intrinsic::atan2;
4681 case LibFunc_sinh:
4682 case LibFunc_sinhf:
4683 case LibFunc_sinhl:
4684 return Intrinsic::sinh;
4685 case LibFunc_cosh:
4686 case LibFunc_coshf:
4687 case LibFunc_coshl:
4688 return Intrinsic::cosh;
4689 case LibFunc_tanh:
4690 case LibFunc_tanhf:
4691 case LibFunc_tanhl:
4692 return Intrinsic::tanh;
4693 case LibFunc_exp:
4694 case LibFunc_expf:
4695 case LibFunc_expl:
4696 return Intrinsic::exp;
4697 case LibFunc_exp2:
4698 case LibFunc_exp2f:
4699 case LibFunc_exp2l:
4700 return Intrinsic::exp2;
4701 case LibFunc_exp10:
4702 case LibFunc_exp10f:
4703 case LibFunc_exp10l:
4704 return Intrinsic::exp10;
4705 case LibFunc_log:
4706 case LibFunc_logf:
4707 case LibFunc_logl:
4708 return Intrinsic::log;
4709 case LibFunc_log10:
4710 case LibFunc_log10f:
4711 case LibFunc_log10l:
4712 return Intrinsic::log10;
4713 case LibFunc_log2:
4714 case LibFunc_log2f:
4715 case LibFunc_log2l:
4716 return Intrinsic::log2;
4717 case LibFunc_fabs:
4718 case LibFunc_fabsf:
4719 case LibFunc_fabsl:
4720 return Intrinsic::fabs;
4721 case LibFunc_fmin:
4722 case LibFunc_fminf:
4723 case LibFunc_fminl:
4724 return Intrinsic::minnum;
4725 case LibFunc_fmax:
4726 case LibFunc_fmaxf:
4727 case LibFunc_fmaxl:
4728 return Intrinsic::maxnum;
4729 case LibFunc_copysign:
4730 case LibFunc_copysignf:
4731 case LibFunc_copysignl:
4732 return Intrinsic::copysign;
4733 case LibFunc_floor:
4734 case LibFunc_floorf:
4735 case LibFunc_floorl:
4736 return Intrinsic::floor;
4737 case LibFunc_ceil:
4738 case LibFunc_ceilf:
4739 case LibFunc_ceill:
4740 return Intrinsic::ceil;
4741 case LibFunc_trunc:
4742 case LibFunc_truncf:
4743 case LibFunc_truncl:
4744 return Intrinsic::trunc;
4745 case LibFunc_rint:
4746 case LibFunc_rintf:
4747 case LibFunc_rintl:
4748 return Intrinsic::rint;
4749 case LibFunc_nearbyint:
4750 case LibFunc_nearbyintf:
4751 case LibFunc_nearbyintl:
4752 return Intrinsic::nearbyint;
4753 case LibFunc_round:
4754 case LibFunc_roundf:
4755 case LibFunc_roundl:
4756 return Intrinsic::round;
4757 case LibFunc_roundeven:
4758 case LibFunc_roundevenf:
4759 case LibFunc_roundevenl:
4760 return Intrinsic::roundeven;
4761 case LibFunc_pow:
4762 case LibFunc_powf:
4763 case LibFunc_powl:
4764 return Intrinsic::pow;
4765 case LibFunc_sqrt:
4766 case LibFunc_sqrtf:
4767 case LibFunc_sqrtl:
4768 return Intrinsic::sqrt;
4769 }
4770
4772}
4773
4774/// Given an exploded icmp instruction, return true if the comparison only
4775/// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if
4776/// the result of the comparison is true when the input value is signed.
4778 bool &TrueIfSigned) {
4779 switch (Pred) {
4780 case ICmpInst::ICMP_SLT: // True if LHS s< 0
4781 TrueIfSigned = true;
4782 return RHS.isZero();
4783 case ICmpInst::ICMP_SLE: // True if LHS s<= -1
4784 TrueIfSigned = true;
4785 return RHS.isAllOnes();
4786 case ICmpInst::ICMP_SGT: // True if LHS s> -1
4787 TrueIfSigned = false;
4788 return RHS.isAllOnes();
4789 case ICmpInst::ICMP_SGE: // True if LHS s>= 0
4790 TrueIfSigned = false;
4791 return RHS.isZero();
4792 case ICmpInst::ICMP_UGT:
4793 // True if LHS u> RHS and RHS == sign-bit-mask - 1
4794 TrueIfSigned = true;
4795 return RHS.isMaxSignedValue();
4796 case ICmpInst::ICMP_UGE:
4797 // True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4798 TrueIfSigned = true;
4799 return RHS.isMinSignedValue();
4800 case ICmpInst::ICMP_ULT:
4801 // True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4802 TrueIfSigned = false;
4803 return RHS.isMinSignedValue();
4804 case ICmpInst::ICMP_ULE:
4805 // True if LHS u<= RHS and RHS == sign-bit-mask - 1
4806 TrueIfSigned = false;
4807 return RHS.isMaxSignedValue();
4808 default:
4809 return false;
4810 }
4811}
4812
4814 bool CondIsTrue,
4815 const Instruction *CxtI,
4816 KnownFPClass &KnownFromContext,
4817 unsigned Depth = 0) {
4818 Value *A, *B;
4820 (CondIsTrue ? match(Cond, m_LogicalAnd(m_Value(A), m_Value(B)))
4821 : match(Cond, m_LogicalOr(m_Value(A), m_Value(B))))) {
4822 computeKnownFPClassFromCond(V, A, CondIsTrue, CxtI, KnownFromContext,
4823 Depth + 1);
4824 computeKnownFPClassFromCond(V, B, CondIsTrue, CxtI, KnownFromContext,
4825 Depth + 1);
4826 return;
4827 }
4829 computeKnownFPClassFromCond(V, A, !CondIsTrue, CxtI, KnownFromContext,
4830 Depth + 1);
4831 return;
4832 }
4833 CmpPredicate Pred;
4834 Value *LHS;
4835 uint64_t ClassVal = 0;
4836 const APFloat *CRHS;
4837 const APInt *RHS;
4838 if (match(Cond, m_FCmp(Pred, m_Value(LHS), m_APFloat(CRHS)))) {
4839 auto [CmpVal, MaskIfTrue, MaskIfFalse] = fcmpImpliesClass(
4840 Pred, *cast<Instruction>(Cond)->getParent()->getParent(), LHS, *CRHS,
4841 LHS != V);
4842 if (CmpVal == V)
4843 KnownFromContext.knownNot(~(CondIsTrue ? MaskIfTrue : MaskIfFalse));
4845 m_Specific(V), m_ConstantInt(ClassVal)))) {
4846 FPClassTest Mask = static_cast<FPClassTest>(ClassVal);
4847 KnownFromContext.knownNot(CondIsTrue ? ~Mask : Mask);
4848 } else if (match(Cond, m_ICmp(Pred, m_ElementWiseBitCast(m_Specific(V)),
4849 m_APInt(RHS)))) {
4850 bool TrueIfSigned;
4851 if (!isSignBitCheck(Pred, *RHS, TrueIfSigned))
4852 return;
4853 if (TrueIfSigned == CondIsTrue)
4854 KnownFromContext.signBitMustBeOne();
4855 else
4856 KnownFromContext.signBitMustBeZero();
4857 }
4858}
4859
4861 const SimplifyQuery &Q) {
4862 KnownFPClass KnownFromContext;
4863
4864 if (Q.CC && Q.CC->AffectedValues.contains(V))
4866 KnownFromContext);
4867
4868 if (!Q.CxtI)
4869 return KnownFromContext;
4870
4871 if (Q.DC && Q.DT) {
4872 // Handle dominating conditions.
4873 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
4874 Value *Cond = BI->getCondition();
4875
4876 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4877 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
4878 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/true, Q.CxtI,
4879 KnownFromContext);
4880
4881 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4882 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
4883 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/false, Q.CxtI,
4884 KnownFromContext);
4885 }
4886 }
4887
4888 if (!Q.AC)
4889 return KnownFromContext;
4890
4891 // Try to restrict the floating-point classes based on information from
4892 // assumptions.
4893 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
4894 if (!AssumeVH)
4895 continue;
4896 CallInst *I = cast<CallInst>(AssumeVH);
4897
4898 assert(I->getFunction() == Q.CxtI->getParent()->getParent() &&
4899 "Got assumption for the wrong function!");
4900 assert(I->getIntrinsicID() == Intrinsic::assume &&
4901 "must be an assume intrinsic");
4902
4903 if (!isValidAssumeForContext(I, Q))
4904 continue;
4905
4906 computeKnownFPClassFromCond(V, I->getArgOperand(0),
4907 /*CondIsTrue=*/true, Q.CxtI, KnownFromContext);
4908 }
4909
4910 return KnownFromContext;
4911}
4912
4914 Value *Arm, bool Invert,
4915 const SimplifyQuery &SQ,
4916 unsigned Depth) {
4917
4918 KnownFPClass KnownSrc;
4920 /*CondIsTrue=*/!Invert, SQ.CxtI, KnownSrc,
4921 Depth + 1);
4922 KnownSrc = KnownSrc.unionWith(Known);
4923 if (KnownSrc.isUnknown())
4924 return;
4925
4926 if (isGuaranteedNotToBeUndef(Arm, SQ.AC, SQ.CxtI, SQ.DT, Depth + 1))
4927 Known = KnownSrc;
4928}
4929
4930void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4931 FPClassTest InterestedClasses, KnownFPClass &Known,
4932 const SimplifyQuery &Q, unsigned Depth);
4933
4934static void computeKnownFPClass(const Value *V, KnownFPClass &Known,
4935 FPClassTest InterestedClasses,
4936 const SimplifyQuery &Q, unsigned Depth) {
4937 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
4938 APInt DemandedElts =
4939 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
4940 computeKnownFPClass(V, DemandedElts, InterestedClasses, Known, Q, Depth);
4941}
4942
4944 const APInt &DemandedElts,
4945 FPClassTest InterestedClasses,
4946 KnownFPClass &Known,
4947 const SimplifyQuery &Q,
4948 unsigned Depth) {
4949 if ((InterestedClasses &
4951 return;
4952
4953 KnownFPClass KnownSrc;
4954 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
4955 KnownSrc, Q, Depth + 1);
4956 Known = KnownFPClass::fptrunc(KnownSrc);
4957}
4958
4960 switch (IID) {
4961 case Intrinsic::minimum:
4963 case Intrinsic::maximum:
4965 case Intrinsic::minimumnum:
4967 case Intrinsic::maximumnum:
4969 case Intrinsic::minnum:
4971 case Intrinsic::maxnum:
4973 default:
4974 llvm_unreachable("not a floating-point min-max intrinsic");
4975 }
4976}
4977
4978/// \return true if this is a floating point value that is known to have a
4979/// magnitude smaller than 1. i.e., fabs(X) <= 1.0 or is nan.
4980static bool isAbsoluteValueULEOne(const Value *V) {
4981 // TODO: Handle frexp
4982 // TODO: Other rounding intrinsics?
4983
4984 // fabs(x - floor(x)) <= 1
4985 const Value *SubFloorX;
4986 if (match(V, m_FSub(m_Value(SubFloorX),
4988 return true;
4989
4992}
4993
4994void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4995 FPClassTest InterestedClasses, KnownFPClass &Known,
4996 const SimplifyQuery &Q, unsigned Depth) {
4997 assert(Known.isUnknown() && "should not be called with known information");
4998
4999 if (!DemandedElts) {
5000 // No demanded elts, better to assume we don't know anything.
5001 Known.resetAll();
5002 return;
5003 }
5004
5005 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
5006
5007 if (auto *CFP = dyn_cast<ConstantFP>(V)) {
5008 Known = KnownFPClass(CFP->getValueAPF());
5009 return;
5010 }
5011
5013 Known.KnownFPClasses = fcPosZero;
5014 Known.SignBit = false;
5015 return;
5016 }
5017
5018 if (isa<PoisonValue>(V)) {
5019 Known.KnownFPClasses = fcNone;
5020 Known.SignBit = false;
5021 return;
5022 }
5023
5024 // Try to handle fixed width vector constants
5025 auto *VFVTy = dyn_cast<FixedVectorType>(V->getType());
5026 const Constant *CV = dyn_cast<Constant>(V);
5027 if (VFVTy && CV) {
5028 Known.KnownFPClasses = fcNone;
5029 bool SignBitAllZero = true;
5030 bool SignBitAllOne = true;
5031
5032 // For vectors, verify that each element is not NaN.
5033 unsigned NumElts = VFVTy->getNumElements();
5034 for (unsigned i = 0; i != NumElts; ++i) {
5035 if (!DemandedElts[i])
5036 continue;
5037
5038 Constant *Elt = CV->getAggregateElement(i);
5039 if (!Elt) {
5040 Known = KnownFPClass();
5041 return;
5042 }
5043 if (isa<PoisonValue>(Elt))
5044 continue;
5045 auto *CElt = dyn_cast<ConstantFP>(Elt);
5046 if (!CElt) {
5047 Known = KnownFPClass();
5048 return;
5049 }
5050
5051 const APFloat &C = CElt->getValueAPF();
5052 Known.KnownFPClasses |= C.classify();
5053 if (C.isNegative())
5054 SignBitAllZero = false;
5055 else
5056 SignBitAllOne = false;
5057 }
5058 if (SignBitAllOne != SignBitAllZero)
5059 Known.SignBit = SignBitAllOne;
5060 return;
5061 }
5062
5063 if (const auto *CDS = dyn_cast<ConstantDataSequential>(V)) {
5064 Known.KnownFPClasses = fcNone;
5065 for (size_t I = 0, E = CDS->getNumElements(); I != E; ++I)
5066 Known |= CDS->getElementAsAPFloat(I).classify();
5067 return;
5068 }
5069
5070 if (const auto *CA = dyn_cast<ConstantAggregate>(V)) {
5071 // TODO: Handle complex aggregates
5072 Known.KnownFPClasses = fcNone;
5073 for (const Use &Op : CA->operands()) {
5074 auto *CFP = dyn_cast<ConstantFP>(Op.get());
5075 if (!CFP) {
5076 Known = KnownFPClass();
5077 return;
5078 }
5079
5080 Known |= CFP->getValueAPF().classify();
5081 }
5082
5083 return;
5084 }
5085
5086 FPClassTest KnownNotFromFlags = fcNone;
5087 if (const auto *CB = dyn_cast<CallBase>(V))
5088 KnownNotFromFlags |= CB->getRetNoFPClass();
5089 else if (const auto *Arg = dyn_cast<Argument>(V))
5090 KnownNotFromFlags |= Arg->getNoFPClass();
5091
5092 const Operator *Op = dyn_cast<Operator>(V);
5094 if (FPOp->hasNoNaNs())
5095 KnownNotFromFlags |= fcNan;
5096 if (FPOp->hasNoInfs())
5097 KnownNotFromFlags |= fcInf;
5098 }
5099
5100 KnownFPClass AssumedClasses = computeKnownFPClassFromContext(V, Q);
5101 KnownNotFromFlags |= ~AssumedClasses.KnownFPClasses;
5102
5103 // We no longer need to find out about these bits from inputs if we can
5104 // assume this from flags/attributes.
5105 InterestedClasses &= ~KnownNotFromFlags;
5106
5107 llvm::scope_exit ClearClassesFromFlags([=, &Known] {
5108 Known.knownNot(KnownNotFromFlags);
5109 if (!Known.SignBit && AssumedClasses.SignBit) {
5110 if (*AssumedClasses.SignBit)
5111 Known.signBitMustBeOne();
5112 else
5113 Known.signBitMustBeZero();
5114 }
5115 });
5116
5117 if (!Op)
5118 return;
5119
5120 // All recursive calls that increase depth must come after this.
5122 return;
5123
5124 const unsigned Opc = Op->getOpcode();
5125 switch (Opc) {
5126 case Instruction::FNeg: {
5127 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5128 Known, Q, Depth + 1);
5129 Known.fneg();
5130 break;
5131 }
5132 case Instruction::Select: {
5133 auto ComputeForArm = [&](Value *Arm, bool Invert) {
5134 KnownFPClass Res;
5135 computeKnownFPClass(Arm, DemandedElts, InterestedClasses, Res, Q,
5136 Depth + 1);
5137 adjustKnownFPClassForSelectArm(Res, Op->getOperand(0), Arm, Invert, Q,
5138 Depth);
5139 return Res;
5140 };
5141 // Only known if known in both the LHS and RHS.
5142 Known =
5143 ComputeForArm(Op->getOperand(1), /*Invert=*/false)
5144 .intersectWith(ComputeForArm(Op->getOperand(2), /*Invert=*/true));
5145 break;
5146 }
5147 case Instruction::Load: {
5148 const MDNode *NoFPClass =
5149 cast<LoadInst>(Op)->getMetadata(LLVMContext::MD_nofpclass);
5150 if (!NoFPClass)
5151 break;
5152
5153 ConstantInt *MaskVal =
5155 Known.knownNot(static_cast<FPClassTest>(MaskVal->getZExtValue()));
5156 break;
5157 }
5158 case Instruction::Call: {
5159 const CallInst *II = cast<CallInst>(Op);
5160 const Intrinsic::ID IID = II->getIntrinsicID();
5161 switch (IID) {
5162 case Intrinsic::fabs: {
5163 if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) {
5164 // If we only care about the sign bit we don't need to inspect the
5165 // operand.
5166 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5167 InterestedClasses, Known, Q, Depth + 1);
5168 }
5169
5170 Known.fabs();
5171 break;
5172 }
5173 case Intrinsic::copysign: {
5174 KnownFPClass KnownSign;
5175
5176 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5177 Known, Q, Depth + 1);
5178 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5179 KnownSign, Q, Depth + 1);
5180 Known.copysign(KnownSign);
5181 break;
5182 }
5183 case Intrinsic::fma:
5184 case Intrinsic::fmuladd: {
5185 if ((InterestedClasses & fcNegative) == fcNone)
5186 break;
5187
5188 // FIXME: This should check isGuaranteedNotToBeUndef
5189 if (II->getArgOperand(0) == II->getArgOperand(1)) {
5190 KnownFPClass KnownSrc, KnownAddend;
5191 computeKnownFPClass(II->getArgOperand(2), DemandedElts,
5192 InterestedClasses, KnownAddend, Q, Depth + 1);
5193 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5194 InterestedClasses, KnownSrc, Q, Depth + 1);
5195
5196 const Function *F = II->getFunction();
5197 const fltSemantics &FltSem =
5198 II->getType()->getScalarType()->getFltSemantics();
5200 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5201
5202 if (KnownNotFromFlags & fcNan) {
5203 KnownSrc.knownNot(fcNan);
5204 KnownAddend.knownNot(fcNan);
5205 }
5206
5207 if (KnownNotFromFlags & fcInf) {
5208 KnownSrc.knownNot(fcInf);
5209 KnownAddend.knownNot(fcInf);
5210 }
5211
5212 Known = KnownFPClass::fma_square(KnownSrc, KnownAddend, Mode);
5213 break;
5214 }
5215
5216 KnownFPClass KnownSrc[3];
5217 for (int I = 0; I != 3; ++I) {
5218 computeKnownFPClass(II->getArgOperand(I), DemandedElts,
5219 InterestedClasses, KnownSrc[I], Q, Depth + 1);
5220 if (KnownSrc[I].isUnknown())
5221 return;
5222
5223 if (KnownNotFromFlags & fcNan)
5224 KnownSrc[I].knownNot(fcNan);
5225 if (KnownNotFromFlags & fcInf)
5226 KnownSrc[I].knownNot(fcInf);
5227 }
5228
5229 const Function *F = II->getFunction();
5230 const fltSemantics &FltSem =
5231 II->getType()->getScalarType()->getFltSemantics();
5233 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5234 Known = KnownFPClass::fma(KnownSrc[0], KnownSrc[1], KnownSrc[2], Mode);
5235 break;
5236 }
5237 case Intrinsic::sqrt:
5238 case Intrinsic::experimental_constrained_sqrt: {
5239 KnownFPClass KnownSrc;
5240 FPClassTest InterestedSrcs = InterestedClasses;
5241 if (InterestedClasses & fcNan)
5242 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5243
5244 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5245 KnownSrc, Q, Depth + 1);
5246
5248
5249 bool HasNSZ = Q.IIQ.hasNoSignedZeros(II);
5250 if (!HasNSZ) {
5251 const Function *F = II->getFunction();
5252 const fltSemantics &FltSem =
5253 II->getType()->getScalarType()->getFltSemantics();
5254 Mode = F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5255 }
5256
5257 Known = KnownFPClass::sqrt(KnownSrc, Mode);
5258 if (HasNSZ)
5259 Known.knownNot(fcNegZero);
5260
5261 break;
5262 }
5263 case Intrinsic::sin: {
5264 KnownFPClass KnownSrc;
5265 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5266 KnownSrc, Q, Depth + 1);
5267 Known = KnownFPClass::sin(KnownSrc);
5268 break;
5269 }
5270 case Intrinsic::cos: {
5271 KnownFPClass KnownSrc;
5272 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5273 KnownSrc, Q, Depth + 1);
5274 Known = KnownFPClass::cos(KnownSrc);
5275 break;
5276 }
5277 case Intrinsic::tan: {
5278 KnownFPClass KnownSrc;
5279 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5280 KnownSrc, Q, Depth + 1);
5281 Known = KnownFPClass::tan(KnownSrc);
5282 break;
5283 }
5284 case Intrinsic::sinh: {
5285 KnownFPClass KnownSrc;
5286 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5287 KnownSrc, Q, Depth + 1);
5288 Known = KnownFPClass::sinh(KnownSrc);
5289 break;
5290 }
5291 case Intrinsic::cosh: {
5292 KnownFPClass KnownSrc;
5293 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5294 KnownSrc, Q, Depth + 1);
5295 Known = KnownFPClass::cosh(KnownSrc);
5296 break;
5297 }
5298 case Intrinsic::tanh: {
5299 KnownFPClass KnownSrc;
5300 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5301 KnownSrc, Q, Depth + 1);
5302 Known = KnownFPClass::tanh(KnownSrc);
5303 break;
5304 }
5305 case Intrinsic::asin: {
5306 KnownFPClass KnownSrc;
5307 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5308 KnownSrc, Q, Depth + 1);
5309 Known = KnownFPClass::asin(KnownSrc);
5310 break;
5311 }
5312 case Intrinsic::acos: {
5313 KnownFPClass KnownSrc;
5314 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5315 KnownSrc, Q, Depth + 1);
5316 Known = KnownFPClass::acos(KnownSrc);
5317 break;
5318 }
5319 case Intrinsic::atan: {
5320 KnownFPClass KnownSrc;
5321 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5322 KnownSrc, Q, Depth + 1);
5323 Known = KnownFPClass::atan(KnownSrc);
5324 break;
5325 }
5326 case Intrinsic::atan2: {
5327 KnownFPClass KnownLHS, KnownRHS;
5328 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5329 KnownLHS, Q, Depth + 1);
5330 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5331 KnownRHS, Q, Depth + 1);
5332 Known = KnownFPClass::atan2(KnownLHS, KnownRHS);
5333 break;
5334 }
5335 case Intrinsic::maxnum:
5336 case Intrinsic::minnum:
5337 case Intrinsic::minimum:
5338 case Intrinsic::maximum:
5339 case Intrinsic::minimumnum:
5340 case Intrinsic::maximumnum: {
5341 KnownFPClass KnownLHS, KnownRHS;
5342 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5343 KnownLHS, Q, Depth + 1);
5344 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5345 KnownRHS, Q, Depth + 1);
5346
5347 const Function *F = II->getFunction();
5348
5350 F ? F->getDenormalMode(
5351 II->getType()->getScalarType()->getFltSemantics())
5353
5354 Known = KnownFPClass::minMaxLike(KnownLHS, KnownRHS, getMinMaxKind(IID),
5355 Mode);
5356 break;
5357 }
5358 case Intrinsic::canonicalize: {
5359 KnownFPClass KnownSrc;
5360 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5361 KnownSrc, Q, Depth + 1);
5362
5363 const Function *F = II->getFunction();
5364 DenormalMode DenormMode =
5365 F ? F->getDenormalMode(
5366 II->getType()->getScalarType()->getFltSemantics())
5368 Known = KnownFPClass::canonicalize(KnownSrc, DenormMode);
5369 break;
5370 }
5371 case Intrinsic::vector_reduce_fmax:
5372 case Intrinsic::vector_reduce_fmin:
5373 case Intrinsic::vector_reduce_fmaximum:
5374 case Intrinsic::vector_reduce_fminimum: {
5375 // reduce min/max will choose an element from one of the vector elements,
5376 // so we can infer and class information that is common to all elements.
5377 Known = computeKnownFPClass(II->getArgOperand(0), II->getFastMathFlags(),
5378 InterestedClasses, Q, Depth + 1);
5379 // Can only propagate sign if output is never NaN.
5380 if (!Known.isKnownNeverNaN())
5381 Known.SignBit.reset();
5382 break;
5383 }
5384 // reverse preserves all characteristics of the input vec's element.
5385 case Intrinsic::vector_reverse:
5386 Known = computeKnownFPClass(
5387 II->getArgOperand(0), DemandedElts.reverseBits(),
5388 II->getFastMathFlags(), InterestedClasses, Q, Depth + 1);
5389 break;
5390 case Intrinsic::trunc:
5391 case Intrinsic::floor:
5392 case Intrinsic::ceil:
5393 case Intrinsic::rint:
5394 case Intrinsic::nearbyint:
5395 case Intrinsic::round:
5396 case Intrinsic::roundeven: {
5397 KnownFPClass KnownSrc;
5398 FPClassTest InterestedSrcs = InterestedClasses;
5399 if (InterestedSrcs & fcPosFinite)
5400 InterestedSrcs |= fcPosFinite;
5401 if (InterestedSrcs & fcNegFinite)
5402 InterestedSrcs |= fcNegFinite;
5403 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5404 KnownSrc, Q, Depth + 1);
5405
5407 KnownSrc, IID == Intrinsic::trunc,
5408 V->getType()->getScalarType()->isMultiUnitFPType());
5409 break;
5410 }
5411 case Intrinsic::exp:
5412 case Intrinsic::exp2:
5413 case Intrinsic::exp10:
5414 case Intrinsic::amdgcn_exp2: {
5415 KnownFPClass KnownSrc;
5416 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5417 KnownSrc, Q, Depth + 1);
5418
5419 Known = KnownFPClass::exp(KnownSrc);
5420
5421 Type *EltTy = II->getType()->getScalarType();
5422 if (IID == Intrinsic::amdgcn_exp2 && EltTy->isFloatTy())
5423 Known.knownNot(fcSubnormal);
5424
5425 break;
5426 }
5427 case Intrinsic::fptrunc_round: {
5428 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,
5429 Q, Depth);
5430 break;
5431 }
5432 case Intrinsic::log:
5433 case Intrinsic::log10:
5434 case Intrinsic::log2:
5435 case Intrinsic::experimental_constrained_log:
5436 case Intrinsic::experimental_constrained_log10:
5437 case Intrinsic::experimental_constrained_log2:
5438 case Intrinsic::amdgcn_log: {
5439 Type *EltTy = II->getType()->getScalarType();
5440
5441 // log(+inf) -> +inf
5442 // log([+-]0.0) -> -inf
5443 // log(-inf) -> nan
5444 // log(-x) -> nan
5445 if ((InterestedClasses & (fcNan | fcInf)) != fcNone) {
5446 FPClassTest InterestedSrcs = InterestedClasses;
5447 if ((InterestedClasses & fcNegInf) != fcNone)
5448 InterestedSrcs |= fcZero | fcSubnormal;
5449 if ((InterestedClasses & fcNan) != fcNone)
5450 InterestedSrcs |= fcNan | fcNegative;
5451
5452 KnownFPClass KnownSrc;
5453 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5454 KnownSrc, Q, Depth + 1);
5455
5456 const Function *F = II->getFunction();
5457 DenormalMode Mode = F ? F->getDenormalMode(EltTy->getFltSemantics())
5459 Known = KnownFPClass::log(KnownSrc, Mode);
5460 }
5461
5462 break;
5463 }
5464 case Intrinsic::powi: {
5465 if ((InterestedClasses & (fcNan | fcInf | fcNegative)) == fcNone)
5466 break;
5467
5468 const Value *Exp = II->getArgOperand(1);
5469 Type *ExpTy = Exp->getType();
5470 unsigned BitWidth = ExpTy->getScalarType()->getIntegerBitWidth();
5471 KnownBits ExponentKnownBits(BitWidth);
5472 computeKnownBits(Exp, isa<VectorType>(ExpTy) ? DemandedElts : APInt(1, 1),
5473 ExponentKnownBits, Q, Depth + 1);
5474
5475 FPClassTest InterestedSrcs = fcNone;
5476 if (InterestedClasses & fcNan)
5477 InterestedSrcs |= fcNan;
5478 if (!ExponentKnownBits.isZero()) {
5479 if (InterestedClasses & fcInf)
5480 InterestedSrcs |= fcFinite | fcInf;
5481 if ((InterestedClasses & fcNegative) && !ExponentKnownBits.isEven())
5482 InterestedSrcs |= fcNegative;
5483 }
5484
5485 KnownFPClass KnownSrc;
5486 if (InterestedSrcs != fcNone)
5487 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5488 KnownSrc, Q, Depth + 1);
5489
5490 Known = KnownFPClass::powi(KnownSrc, ExponentKnownBits);
5491 break;
5492 }
5493 case Intrinsic::ldexp: {
5494 KnownFPClass KnownSrc;
5495 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5496 KnownSrc, Q, Depth + 1);
5497 // Can refine inf/zero handling based on the exponent operand.
5498 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;
5499
5500 KnownBits ExpBits;
5501 if ((KnownSrc.KnownFPClasses & ExpInfoMask) != fcNone) {
5502 const Value *ExpArg = II->getArgOperand(1);
5503 ExpBits = computeKnownBits(ExpArg, DemandedElts, Q, Depth + 1);
5504 }
5505
5506 const fltSemantics &Flt =
5507 II->getType()->getScalarType()->getFltSemantics();
5508
5509 const Function *F = II->getFunction();
5511 F ? F->getDenormalMode(Flt) : DenormalMode::getDynamic();
5512
5513 Known = KnownFPClass::ldexp(KnownSrc, ExpBits, Flt, Mode);
5514 break;
5515 }
5516 case Intrinsic::arithmetic_fence: {
5517 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5518 Known, Q, Depth + 1);
5519 break;
5520 }
5521 case Intrinsic::experimental_constrained_sitofp:
5522 case Intrinsic::experimental_constrained_uitofp:
5523 // Cannot produce nan
5524 Known.knownNot(fcNan);
5525
5526 // sitofp and uitofp turn into +0.0 for zero.
5527 Known.knownNot(fcNegZero);
5528
5529 // Integers cannot be subnormal
5530 Known.knownNot(fcSubnormal);
5531
5532 if (IID == Intrinsic::experimental_constrained_uitofp)
5533 Known.signBitMustBeZero();
5534
5535 // TODO: Copy inf handling from instructions
5536 break;
5537
5538 case Intrinsic::amdgcn_fract: {
5539 Known.knownNot(fcInf);
5540
5541 if (InterestedClasses & fcNan) {
5542 KnownFPClass KnownSrc;
5543 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5544 InterestedClasses, KnownSrc, Q, Depth + 1);
5545
5546 if (KnownSrc.isKnownNeverInfOrNaN())
5547 Known.knownNot(fcNan);
5548 else if (KnownSrc.isKnownNever(fcSNan))
5549 Known.knownNot(fcSNan);
5550 }
5551
5552 break;
5553 }
5554 case Intrinsic::amdgcn_rcp: {
5555 KnownFPClass KnownSrc;
5556 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5557 KnownSrc, Q, Depth + 1);
5558
5559 Known.propagateNaN(KnownSrc);
5560
5561 Type *EltTy = II->getType()->getScalarType();
5562
5563 // f32 denormal always flushed.
5564 if (EltTy->isFloatTy()) {
5565 Known.knownNot(fcSubnormal);
5566 KnownSrc.knownNot(fcSubnormal);
5567 }
5568
5569 if (KnownSrc.isKnownNever(fcNegative))
5570 Known.knownNot(fcNegative);
5571 if (KnownSrc.isKnownNever(fcPositive))
5572 Known.knownNot(fcPositive);
5573
5574 if (const Function *F = II->getFunction()) {
5575 DenormalMode Mode = F->getDenormalMode(EltTy->getFltSemantics());
5576 if (KnownSrc.isKnownNeverLogicalPosZero(Mode))
5577 Known.knownNot(fcPosInf);
5578 if (KnownSrc.isKnownNeverLogicalNegZero(Mode))
5579 Known.knownNot(fcNegInf);
5580 }
5581
5582 break;
5583 }
5584 case Intrinsic::amdgcn_rsq: {
5585 KnownFPClass KnownSrc;
5586 // The only negative value that can be returned is -inf for -0 inputs.
5588
5589 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5590 KnownSrc, Q, Depth + 1);
5591
5592 // Negative -> nan
5593 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
5594 Known.knownNot(fcNan);
5595 else if (KnownSrc.isKnownNever(fcSNan))
5596 Known.knownNot(fcSNan);
5597
5598 // +inf -> +0
5599 if (KnownSrc.isKnownNeverPosInfinity())
5600 Known.knownNot(fcPosZero);
5601
5602 Type *EltTy = II->getType()->getScalarType();
5603
5604 // f32 denormal always flushed.
5605 if (EltTy->isFloatTy())
5606 Known.knownNot(fcPosSubnormal);
5607
5608 if (const Function *F = II->getFunction()) {
5609 DenormalMode Mode = F->getDenormalMode(EltTy->getFltSemantics());
5610
5611 // -0 -> -inf
5612 if (KnownSrc.isKnownNeverLogicalNegZero(Mode))
5613 Known.knownNot(fcNegInf);
5614
5615 // +0 -> +inf
5616 if (KnownSrc.isKnownNeverLogicalPosZero(Mode))
5617 Known.knownNot(fcPosInf);
5618 }
5619
5620 break;
5621 }
5622 case Intrinsic::amdgcn_trig_preop: {
5623 // Always returns a value [0, 1)
5624 Known.knownNot(fcNan | fcInf | fcNegative);
5625 break;
5626 }
5627 default:
5628 break;
5629 }
5630
5631 break;
5632 }
5633 case Instruction::FAdd:
5634 case Instruction::FSub: {
5635 KnownFPClass KnownLHS, KnownRHS;
5636 bool WantNegative =
5637 Op->getOpcode() == Instruction::FAdd &&
5638 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;
5639 bool WantNaN = (InterestedClasses & fcNan) != fcNone;
5640 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;
5641
5642 if (!WantNaN && !WantNegative && !WantNegZero)
5643 break;
5644
5645 FPClassTest InterestedSrcs = InterestedClasses;
5646 if (WantNegative)
5647 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5648 if (InterestedClasses & fcNan)
5649 InterestedSrcs |= fcInf;
5650 computeKnownFPClass(Op->getOperand(1), DemandedElts, InterestedSrcs,
5651 KnownRHS, Q, Depth + 1);
5652
5653 // Special case fadd x, x, which is the canonical form of fmul x, 2.
5654 bool Self = Op->getOperand(0) == Op->getOperand(1) &&
5655 isGuaranteedNotToBeUndef(Op->getOperand(0), Q.AC, Q.CxtI, Q.DT,
5656 Depth + 1);
5657 if (Self)
5658 KnownLHS = KnownRHS;
5659
5660 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||
5661 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) ||
5662 WantNegZero || Opc == Instruction::FSub) {
5663
5664 // FIXME: Context function should always be passed in separately
5665 const Function *F = cast<Instruction>(Op)->getFunction();
5666 const fltSemantics &FltSem =
5667 Op->getType()->getScalarType()->getFltSemantics();
5669 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5670
5671 if (Self && Opc == Instruction::FAdd) {
5672 Known = KnownFPClass::fadd_self(KnownLHS, Mode);
5673 } else {
5674 // RHS is canonically cheaper to compute. Skip inspecting the LHS if
5675 // there's no point.
5676
5677 if (!Self) {
5678 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedSrcs,
5679 KnownLHS, Q, Depth + 1);
5680 }
5681
5682 Known = Opc == Instruction::FAdd
5683 ? KnownFPClass::fadd(KnownLHS, KnownRHS, Mode)
5684 : KnownFPClass::fsub(KnownLHS, KnownRHS, Mode);
5685 }
5686 }
5687
5688 break;
5689 }
5690 case Instruction::FMul: {
5691 const Function *F = cast<Instruction>(Op)->getFunction();
5693 F ? F->getDenormalMode(
5694 Op->getType()->getScalarType()->getFltSemantics())
5696
5697 Value *LHS = Op->getOperand(0);
5698 Value *RHS = Op->getOperand(1);
5699 // X * X is always non-negative or a NaN.
5700 // FIXME: Should check isGuaranteedNotToBeUndef
5701 if (LHS == RHS) {
5702 KnownFPClass KnownSrc;
5703 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownSrc, Q,
5704 Depth + 1);
5705 Known = KnownFPClass::square(KnownSrc, Mode);
5706 break;
5707 }
5708
5709 KnownFPClass KnownLHS, KnownRHS;
5710
5711 const APFloat *CRHS;
5712 if (match(RHS, m_APFloat(CRHS))) {
5713 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Q,
5714 Depth + 1);
5715 Known = KnownFPClass::fmul(KnownLHS, *CRHS, Mode);
5716 } else {
5717 computeKnownFPClass(RHS, DemandedElts, fcAllFlags, KnownRHS, Q,
5718 Depth + 1);
5719 // TODO: Improve accuracy in unfused FMA pattern. We can prove an
5720 // additional not-nan if the addend is known-not negative infinity if the
5721 // multiply is known-not infinity.
5722
5723 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Q,
5724 Depth + 1);
5725 Known = KnownFPClass::fmul(KnownLHS, KnownRHS, Mode);
5726 }
5727
5728 /// Propgate no-infs if the other source is known smaller than one, such
5729 /// that this cannot introduce overflow.
5730 if (KnownLHS.isKnownNever(fcInf) && isAbsoluteValueULEOne(RHS))
5731 Known.knownNot(fcInf);
5732 else if (KnownRHS.isKnownNever(fcInf) && isAbsoluteValueULEOne(LHS))
5733 Known.knownNot(fcInf);
5734
5735 break;
5736 }
5737 case Instruction::FDiv:
5738 case Instruction::FRem: {
5739 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
5740
5741 if (Op->getOpcode() == Instruction::FRem)
5742 Known.knownNot(fcInf);
5743
5744 if (Op->getOperand(0) == Op->getOperand(1) &&
5745 isGuaranteedNotToBeUndef(Op->getOperand(0), Q.AC, Q.CxtI, Q.DT)) {
5746 if (Op->getOpcode() == Instruction::FDiv) {
5747 // X / X is always exactly 1.0 or a NaN.
5749 } else {
5750 // X % X is always exactly [+-]0.0 or a NaN.
5751 Known.KnownFPClasses = fcNan | fcZero;
5752 }
5753
5754 if (!WantNan)
5755 break;
5756
5757 KnownFPClass KnownSrc;
5758 computeKnownFPClass(Op->getOperand(0), DemandedElts,
5759 fcNan | fcInf | fcZero | fcSubnormal, KnownSrc, Q,
5760 Depth + 1);
5761 const Function *F = cast<Instruction>(Op)->getFunction();
5762 const fltSemantics &FltSem =
5763 Op->getType()->getScalarType()->getFltSemantics();
5764
5766 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5767
5768 Known = Op->getOpcode() == Instruction::FDiv
5769 ? KnownFPClass::fdiv_self(KnownSrc, Mode)
5770 : KnownFPClass::frem_self(KnownSrc, Mode);
5771 break;
5772 }
5773
5774 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
5775 const bool WantPositive =
5776 Opc == Instruction::FRem && (InterestedClasses & fcPositive) != fcNone;
5777 if (!WantNan && !WantNegative && !WantPositive)
5778 break;
5779
5780 KnownFPClass KnownLHS, KnownRHS;
5781
5782 computeKnownFPClass(Op->getOperand(1), DemandedElts,
5783 fcNan | fcInf | fcZero | fcNegative, KnownRHS, Q,
5784 Depth + 1);
5785
5786 bool KnowSomethingUseful = KnownRHS.isKnownNeverNaN() ||
5787 KnownRHS.isKnownNever(fcNegative) ||
5788 KnownRHS.isKnownNever(fcPositive);
5789
5790 if (KnowSomethingUseful || WantPositive) {
5791 computeKnownFPClass(Op->getOperand(0), DemandedElts, fcAllFlags, KnownLHS,
5792 Q, Depth + 1);
5793 }
5794
5795 const Function *F = cast<Instruction>(Op)->getFunction();
5796 const fltSemantics &FltSem =
5797 Op->getType()->getScalarType()->getFltSemantics();
5798
5799 if (Op->getOpcode() == Instruction::FDiv) {
5801 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5802 Known = KnownFPClass::fdiv(KnownLHS, KnownRHS, Mode);
5803 } else {
5804 // Inf REM x and x REM 0 produce NaN.
5805 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5806 KnownLHS.isKnownNeverInfinity() && F &&
5807 KnownRHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))) {
5808 Known.knownNot(fcNan);
5809 }
5810
5811 // The sign for frem is the same as the first operand.
5812 if (KnownLHS.cannotBeOrderedLessThanZero())
5814 if (KnownLHS.cannotBeOrderedGreaterThanZero())
5816
5817 // See if we can be more aggressive about the sign of 0.
5818 if (KnownLHS.isKnownNever(fcNegative))
5819 Known.knownNot(fcNegative);
5820 if (KnownLHS.isKnownNever(fcPositive))
5821 Known.knownNot(fcPositive);
5822 }
5823
5824 break;
5825 }
5826 case Instruction::FPExt: {
5827 KnownFPClass KnownSrc;
5828 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5829 KnownSrc, Q, Depth + 1);
5830
5831 const fltSemantics &DstTy =
5832 Op->getType()->getScalarType()->getFltSemantics();
5833 const fltSemantics &SrcTy =
5834 Op->getOperand(0)->getType()->getScalarType()->getFltSemantics();
5835
5836 Known = KnownFPClass::fpext(KnownSrc, DstTy, SrcTy);
5837 break;
5838 }
5839 case Instruction::FPTrunc: {
5840 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known, Q,
5841 Depth);
5842 break;
5843 }
5844 case Instruction::SIToFP:
5845 case Instruction::UIToFP: {
5846 // Cannot produce nan
5847 Known.knownNot(fcNan);
5848
5849 // Integers cannot be subnormal
5850 Known.knownNot(fcSubnormal);
5851
5852 // sitofp and uitofp turn into +0.0 for zero.
5853 Known.knownNot(fcNegZero);
5854
5855 // UIToFP is always non-negative regardless of known bits.
5856 if (Op->getOpcode() == Instruction::UIToFP)
5857 Known.signBitMustBeZero();
5858
5859 // Only compute known bits if we can learn something useful from them.
5860 if (!(InterestedClasses & (fcPosZero | fcNormal | fcInf)))
5861 break;
5862
5863 KnownBits IntKnown =
5864 computeKnownBits(Op->getOperand(0), DemandedElts, Q, Depth + 1);
5865
5866 // If the integer is non-zero, the result cannot be +0.0
5867 if (IntKnown.isNonZero())
5868 Known.knownNot(fcPosZero);
5869
5870 if (Op->getOpcode() == Instruction::SIToFP) {
5871 // If the signed integer is known non-negative, the result is
5872 // non-negative. If the signed integer is known negative, the result is
5873 // negative.
5874 if (IntKnown.isNonNegative()) {
5875 Known.signBitMustBeZero();
5876 } else if (IntKnown.isNegative()) {
5877 Known.signBitMustBeOne();
5878 }
5879 }
5880
5881 // Guard kept for ilogb()
5882 if (InterestedClasses & fcInf) {
5883 // Get width of largest magnitude integer known.
5884 // This still works for a signed minimum value because the largest FP
5885 // value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx).
5886 int IntSize = IntKnown.getBitWidth();
5887 if (Op->getOpcode() == Instruction::UIToFP)
5888 IntSize -= IntKnown.countMinLeadingZeros();
5889 else if (Op->getOpcode() == Instruction::SIToFP)
5890 IntSize -= IntKnown.countMinSignBits();
5891
5892 // If the exponent of the largest finite FP value can hold the largest
5893 // integer, the result of the cast must be finite.
5894 Type *FPTy = Op->getType()->getScalarType();
5895 if (ilogb(APFloat::getLargest(FPTy->getFltSemantics())) >= IntSize)
5896 Known.knownNot(fcInf);
5897 }
5898
5899 break;
5900 }
5901 case Instruction::ExtractElement: {
5902 // Look through extract element. If the index is non-constant or
5903 // out-of-range demand all elements, otherwise just the extracted element.
5904 const Value *Vec = Op->getOperand(0);
5905
5906 APInt DemandedVecElts;
5907 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
5908 unsigned NumElts = VecTy->getNumElements();
5909 DemandedVecElts = APInt::getAllOnes(NumElts);
5910 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(1));
5911 if (CIdx && CIdx->getValue().ult(NumElts))
5912 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
5913 } else {
5914 DemandedVecElts = APInt(1, 1);
5915 }
5916
5917 return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,
5918 Q, Depth + 1);
5919 }
5920 case Instruction::InsertElement: {
5921 if (isa<ScalableVectorType>(Op->getType()))
5922 return;
5923
5924 const Value *Vec = Op->getOperand(0);
5925 const Value *Elt = Op->getOperand(1);
5926 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(2));
5927 unsigned NumElts = DemandedElts.getBitWidth();
5928 APInt DemandedVecElts = DemandedElts;
5929 bool NeedsElt = true;
5930 // If we know the index we are inserting to, clear it from Vec check.
5931 if (CIdx && CIdx->getValue().ult(NumElts)) {
5932 DemandedVecElts.clearBit(CIdx->getZExtValue());
5933 NeedsElt = DemandedElts[CIdx->getZExtValue()];
5934 }
5935
5936 // Do we demand the inserted element?
5937 if (NeedsElt) {
5938 computeKnownFPClass(Elt, Known, InterestedClasses, Q, Depth + 1);
5939 // If we don't know any bits, early out.
5940 if (Known.isUnknown())
5941 break;
5942 } else {
5943 Known.KnownFPClasses = fcNone;
5944 }
5945
5946 // Do we need anymore elements from Vec?
5947 if (!DemandedVecElts.isZero()) {
5948 KnownFPClass Known2;
5949 computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2, Q,
5950 Depth + 1);
5951 Known |= Known2;
5952 }
5953
5954 break;
5955 }
5956 case Instruction::ShuffleVector: {
5957 // Handle vector splat idiom
5958 if (Value *Splat = getSplatValue(V)) {
5959 computeKnownFPClass(Splat, Known, InterestedClasses, Q, Depth + 1);
5960 break;
5961 }
5962
5963 // For undef elements, we don't know anything about the common state of
5964 // the shuffle result.
5965 APInt DemandedLHS, DemandedRHS;
5966 auto *Shuf = dyn_cast<ShuffleVectorInst>(Op);
5967 if (!Shuf || !getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
5968 return;
5969
5970 if (!!DemandedLHS) {
5971 const Value *LHS = Shuf->getOperand(0);
5972 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known, Q,
5973 Depth + 1);
5974
5975 // If we don't know any bits, early out.
5976 if (Known.isUnknown())
5977 break;
5978 } else {
5979 Known.KnownFPClasses = fcNone;
5980 }
5981
5982 if (!!DemandedRHS) {
5983 KnownFPClass Known2;
5984 const Value *RHS = Shuf->getOperand(1);
5985 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2, Q,
5986 Depth + 1);
5987 Known |= Known2;
5988 }
5989
5990 break;
5991 }
5992 case Instruction::ExtractValue: {
5993 const ExtractValueInst *Extract = cast<ExtractValueInst>(Op);
5994 ArrayRef<unsigned> Indices = Extract->getIndices();
5995 const Value *Src = Extract->getAggregateOperand();
5996 if (isa<StructType>(Src->getType()) && Indices.size() == 1 &&
5997 Indices[0] == 0) {
5998 if (const auto *II = dyn_cast<IntrinsicInst>(Src)) {
5999 switch (II->getIntrinsicID()) {
6000 case Intrinsic::frexp: {
6001 Known.knownNot(fcSubnormal);
6002
6003 KnownFPClass KnownSrc;
6004 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
6005 InterestedClasses, KnownSrc, Q, Depth + 1);
6006
6007 const Function *F = cast<Instruction>(Op)->getFunction();
6008 const fltSemantics &FltSem =
6009 Op->getType()->getScalarType()->getFltSemantics();
6010
6012 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
6013 Known = KnownFPClass::frexp_mant(KnownSrc, Mode);
6014 return;
6015 }
6016 default:
6017 break;
6018 }
6019 }
6020 }
6021
6022 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Q,
6023 Depth + 1);
6024 break;
6025 }
6026 case Instruction::PHI: {
6027 const PHINode *P = cast<PHINode>(Op);
6028 // Unreachable blocks may have zero-operand PHI nodes.
6029 if (P->getNumIncomingValues() == 0)
6030 break;
6031
6032 // Otherwise take the unions of the known bit sets of the operands,
6033 // taking conservative care to avoid excessive recursion.
6034 const unsigned PhiRecursionLimit = MaxAnalysisRecursionDepth - 2;
6035
6036 if (Depth < PhiRecursionLimit) {
6037 // Skip if every incoming value references to ourself.
6038 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
6039 break;
6040
6041 bool First = true;
6042
6043 for (const Use &U : P->operands()) {
6044 Value *IncValue;
6045 Instruction *CxtI;
6046 breakSelfRecursivePHI(&U, P, IncValue, CxtI);
6047 // Skip direct self references.
6048 if (IncValue == P)
6049 continue;
6050
6051 KnownFPClass KnownSrc;
6052 // Recurse, but cap the recursion to two levels, because we don't want
6053 // to waste time spinning around in loops. We need at least depth 2 to
6054 // detect known sign bits.
6055 computeKnownFPClass(IncValue, DemandedElts, InterestedClasses, KnownSrc,
6057 PhiRecursionLimit);
6058
6059 if (First) {
6060 Known = KnownSrc;
6061 First = false;
6062 } else {
6063 Known |= KnownSrc;
6064 }
6065
6066 if (Known.KnownFPClasses == fcAllFlags)
6067 break;
6068 }
6069 }
6070
6071 // Look for the case of a for loop which has a positive
6072 // initial value and is incremented by a squared value.
6073 // This will propagate sign information out of such loops.
6074 if (P->getNumIncomingValues() != 2 || Known.cannotBeOrderedLessThanZero())
6075 break;
6076 for (unsigned I = 0; I < 2; I++) {
6077 Value *RecurValue = P->getIncomingValue(1 - I);
6079 if (!II)
6080 continue;
6081 Value *R, *L, *Init;
6082 PHINode *PN;
6084 PN == P) {
6085 switch (II->getIntrinsicID()) {
6086 case Intrinsic::fma:
6087 case Intrinsic::fmuladd: {
6088 KnownFPClass KnownStart;
6089 computeKnownFPClass(Init, DemandedElts, InterestedClasses, KnownStart,
6090 Q, Depth + 1);
6091 if (KnownStart.cannotBeOrderedLessThanZero() && L == R &&
6092 isGuaranteedNotToBeUndef(L, Q.AC, Q.CxtI, Q.DT, Depth + 1))
6094 break;
6095 }
6096 }
6097 }
6098 }
6099 break;
6100 }
6101 case Instruction::BitCast: {
6102 const Value *Src;
6103 if (!match(Op, m_ElementWiseBitCast(m_Value(Src))) ||
6104 !Src->getType()->isIntOrIntVectorTy())
6105 break;
6106
6107 const Type *Ty = Op->getType();
6108
6109 Value *CastLHS, *CastRHS;
6110
6111 // Match bitcast(umax(bitcast(a), bitcast(b)))
6112 if (match(Src, m_c_MaxOrMin(m_BitCast(m_Value(CastLHS)),
6113 m_BitCast(m_Value(CastRHS)))) &&
6114 CastLHS->getType() == Ty && CastRHS->getType() == Ty) {
6115 KnownFPClass KnownLHS, KnownRHS;
6116 computeKnownFPClass(CastRHS, DemandedElts, InterestedClasses, KnownRHS, Q,
6117 Depth + 1);
6118 if (!KnownRHS.isUnknown()) {
6119 computeKnownFPClass(CastLHS, DemandedElts, InterestedClasses, KnownLHS,
6120 Q, Depth + 1);
6121 Known = KnownLHS | KnownRHS;
6122 }
6123
6124 return;
6125 }
6126
6127 const Type *EltTy = Ty->getScalarType();
6128 KnownBits Bits(EltTy->getPrimitiveSizeInBits());
6129 computeKnownBits(Src, DemandedElts, Bits, Q, Depth + 1);
6130
6131 Known = KnownFPClass::bitcast(EltTy->getFltSemantics(), Bits);
6132 break;
6133 }
6134 default:
6135 break;
6136 }
6137}
6138
6140 const APInt &DemandedElts,
6141 FPClassTest InterestedClasses,
6142 const SimplifyQuery &SQ,
6143 unsigned Depth) {
6144 KnownFPClass KnownClasses;
6145 ::computeKnownFPClass(V, DemandedElts, InterestedClasses, KnownClasses, SQ,
6146 Depth);
6147 return KnownClasses;
6148}
6149
6151 FPClassTest InterestedClasses,
6152 const SimplifyQuery &SQ,
6153 unsigned Depth) {
6154 KnownFPClass Known;
6155 ::computeKnownFPClass(V, Known, InterestedClasses, SQ, Depth);
6156 return Known;
6157}
6158
6160 const Value *V, const DataLayout &DL, FPClassTest InterestedClasses,
6161 const TargetLibraryInfo *TLI, AssumptionCache *AC, const Instruction *CxtI,
6162 const DominatorTree *DT, bool UseInstrInfo, unsigned Depth) {
6163 return computeKnownFPClass(V, InterestedClasses,
6164 SimplifyQuery(DL, TLI, DT, AC, CxtI, UseInstrInfo),
6165 Depth);
6166}
6167
6169llvm::computeKnownFPClass(const Value *V, const APInt &DemandedElts,
6170 FastMathFlags FMF, FPClassTest InterestedClasses,
6171 const SimplifyQuery &SQ, unsigned Depth) {
6172 if (FMF.noNaNs())
6173 InterestedClasses &= ~fcNan;
6174 if (FMF.noInfs())
6175 InterestedClasses &= ~fcInf;
6176
6177 KnownFPClass Result =
6178 computeKnownFPClass(V, DemandedElts, InterestedClasses, SQ, Depth);
6179
6180 if (FMF.noNaNs())
6181 Result.KnownFPClasses &= ~fcNan;
6182 if (FMF.noInfs())
6183 Result.KnownFPClasses &= ~fcInf;
6184 return Result;
6185}
6186
6188 FPClassTest InterestedClasses,
6189 const SimplifyQuery &SQ,
6190 unsigned Depth) {
6191 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
6192 APInt DemandedElts =
6193 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
6194 return computeKnownFPClass(V, DemandedElts, FMF, InterestedClasses, SQ,
6195 Depth);
6196}
6197
6199 unsigned Depth) {
6201 return Known.isKnownNeverNegZero();
6202}
6203
6210
6212 unsigned Depth) {
6214 return Known.isKnownNeverInfinity();
6215}
6216
6217/// Return true if the floating-point value can never contain a NaN or infinity.
6219 unsigned Depth) {
6221 return Known.isKnownNeverNaN() && Known.isKnownNeverInfinity();
6222}
6223
6224/// Return true if the floating-point scalar value is not a NaN or if the
6225/// floating-point vector value has no NaN elements. Return false if a value
6226/// could ever be NaN.
6228 unsigned Depth) {
6230 return Known.isKnownNeverNaN();
6231}
6232
6233/// Return false if we can prove that the specified FP value's sign bit is 0.
6234/// Return true if we can prove that the specified FP value's sign bit is 1.
6235/// Otherwise return std::nullopt.
6236std::optional<bool> llvm::computeKnownFPSignBit(const Value *V,
6237 const SimplifyQuery &SQ,
6238 unsigned Depth) {
6240 return Known.SignBit;
6241}
6242
6244 auto *User = cast<Instruction>(U.getUser());
6245 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6246 if (FPOp->hasNoSignedZeros())
6247 return true;
6248 }
6249
6250 switch (User->getOpcode()) {
6251 case Instruction::FPToSI:
6252 case Instruction::FPToUI:
6253 return true;
6254 case Instruction::FCmp:
6255 // fcmp treats both positive and negative zero as equal.
6256 return true;
6257 case Instruction::Call:
6258 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6259 switch (II->getIntrinsicID()) {
6260 case Intrinsic::fabs:
6261 return true;
6262 case Intrinsic::copysign:
6263 return U.getOperandNo() == 0;
6264 case Intrinsic::is_fpclass:
6265 case Intrinsic::vp_is_fpclass: {
6266 auto Test =
6267 static_cast<FPClassTest>(
6268 cast<ConstantInt>(II->getArgOperand(1))->getZExtValue()) &
6271 }
6272 default:
6273 return false;
6274 }
6275 }
6276 return false;
6277 default:
6278 return false;
6279 }
6280}
6281
6283 auto *User = cast<Instruction>(U.getUser());
6284 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6285 if (FPOp->hasNoNaNs())
6286 return true;
6287 }
6288
6289 switch (User->getOpcode()) {
6290 case Instruction::FPToSI:
6291 case Instruction::FPToUI:
6292 return true;
6293 // Proper FP math operations ignore the sign bit of NaN.
6294 case Instruction::FAdd:
6295 case Instruction::FSub:
6296 case Instruction::FMul:
6297 case Instruction::FDiv:
6298 case Instruction::FRem:
6299 case Instruction::FPTrunc:
6300 case Instruction::FPExt:
6301 case Instruction::FCmp:
6302 return true;
6303 // Bitwise FP operations should preserve the sign bit of NaN.
6304 case Instruction::FNeg:
6305 case Instruction::Select:
6306 case Instruction::PHI:
6307 return false;
6308 case Instruction::Ret:
6309 return User->getFunction()->getAttributes().getRetNoFPClass() &
6311 case Instruction::Call:
6312 case Instruction::Invoke: {
6313 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6314 switch (II->getIntrinsicID()) {
6315 case Intrinsic::fabs:
6316 return true;
6317 case Intrinsic::copysign:
6318 return U.getOperandNo() == 0;
6319 // Other proper FP math intrinsics ignore the sign bit of NaN.
6320 case Intrinsic::maxnum:
6321 case Intrinsic::minnum:
6322 case Intrinsic::maximum:
6323 case Intrinsic::minimum:
6324 case Intrinsic::maximumnum:
6325 case Intrinsic::minimumnum:
6326 case Intrinsic::canonicalize:
6327 case Intrinsic::fma:
6328 case Intrinsic::fmuladd:
6329 case Intrinsic::sqrt:
6330 case Intrinsic::pow:
6331 case Intrinsic::powi:
6332 case Intrinsic::fptoui_sat:
6333 case Intrinsic::fptosi_sat:
6334 case Intrinsic::is_fpclass:
6335 case Intrinsic::vp_is_fpclass:
6336 return true;
6337 default:
6338 return false;
6339 }
6340 }
6341
6342 FPClassTest NoFPClass =
6343 cast<CallBase>(User)->getParamNoFPClass(U.getOperandNo());
6344 return NoFPClass & FPClassTest::fcNan;
6345 }
6346 default:
6347 return false;
6348 }
6349}
6350
6352 FastMathFlags FMF) {
6353 if (isa<PoisonValue>(V))
6354 return true;
6355 if (isa<UndefValue>(V))
6356 return false;
6357
6358 if (match(V, m_CheckedFp([](const APFloat &Val) { return Val.isInteger(); })))
6359 return true;
6360
6362 if (!I)
6363 return false;
6364
6365 switch (I->getOpcode()) {
6366 case Instruction::SIToFP:
6367 case Instruction::UIToFP:
6368 // TODO: Could check nofpclass(inf) on incoming argument
6369 if (FMF.noInfs())
6370 return true;
6371
6372 // Need to check int size cannot produce infinity, which computeKnownFPClass
6373 // knows how to do already.
6374 return isKnownNeverInfinity(I, SQ);
6375 case Instruction::Call: {
6376 const CallInst *CI = cast<CallInst>(I);
6377 switch (CI->getIntrinsicID()) {
6378 case Intrinsic::trunc:
6379 case Intrinsic::floor:
6380 case Intrinsic::ceil:
6381 case Intrinsic::rint:
6382 case Intrinsic::nearbyint:
6383 case Intrinsic::round:
6384 case Intrinsic::roundeven:
6385 return (FMF.noInfs() && FMF.noNaNs()) || isKnownNeverInfOrNaN(I, SQ);
6386 default:
6387 break;
6388 }
6389
6390 break;
6391 }
6392 default:
6393 break;
6394 }
6395
6396 return false;
6397}
6398
6400
6401 // All byte-wide stores are splatable, even of arbitrary variables.
6402 if (V->getType()->isIntegerTy(8))
6403 return V;
6404
6405 LLVMContext &Ctx = V->getContext();
6406
6407 // Undef don't care.
6408 auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
6409 if (isa<UndefValue>(V))
6410 return UndefInt8;
6411
6412 // Return poison for zero-sized type.
6413 if (DL.getTypeStoreSize(V->getType()).isZero())
6414 return PoisonValue::get(Type::getInt8Ty(Ctx));
6415
6417 if (!C) {
6418 // Conceptually, we could handle things like:
6419 // %a = zext i8 %X to i16
6420 // %b = shl i16 %a, 8
6421 // %c = or i16 %a, %b
6422 // but until there is an example that actually needs this, it doesn't seem
6423 // worth worrying about.
6424 return nullptr;
6425 }
6426
6427 // Handle 'null' ConstantArrayZero etc.
6428 if (C->isNullValue())
6430
6431 // Constant floating-point values can be handled as integer values if the
6432 // corresponding integer value is "byteable". An important case is 0.0.
6433 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
6434 Type *ScalarTy = CFP->getType()->getScalarType();
6435 if (ScalarTy->isHalfTy() || ScalarTy->isFloatTy() || ScalarTy->isDoubleTy())
6436 return isBytewiseValue(
6437 ConstantInt::get(Ctx, CFP->getValue().bitcastToAPInt()), DL);
6438
6439 // Don't handle long double formats, which have strange constraints.
6440 return nullptr;
6441 }
6442
6443 // We can handle constant integers that are multiple of 8 bits.
6444 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
6445 if (CI->getBitWidth() % 8 == 0) {
6446 if (!CI->getValue().isSplat(8))
6447 return nullptr;
6448 return ConstantInt::get(Ctx, CI->getValue().trunc(8));
6449 }
6450 }
6451
6452 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
6453 if (CE->getOpcode() == Instruction::IntToPtr) {
6454 if (auto *PtrTy = dyn_cast<PointerType>(CE->getType())) {
6455 unsigned BitWidth = DL.getPointerSizeInBits(PtrTy->getAddressSpace());
6457 CE->getOperand(0), Type::getIntNTy(Ctx, BitWidth), false, DL))
6458 return isBytewiseValue(Op, DL);
6459 }
6460 }
6461 }
6462
6463 auto Merge = [&](Value *LHS, Value *RHS) -> Value * {
6464 if (LHS == RHS)
6465 return LHS;
6466 if (!LHS || !RHS)
6467 return nullptr;
6468 if (LHS == UndefInt8)
6469 return RHS;
6470 if (RHS == UndefInt8)
6471 return LHS;
6472 return nullptr;
6473 };
6474
6476 Value *Val = UndefInt8;
6477 for (uint64_t I = 0, E = CA->getNumElements(); I != E; ++I)
6478 if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))
6479 return nullptr;
6480 return Val;
6481 }
6482
6484 Value *Val = UndefInt8;
6485 for (Value *Op : C->operands())
6486 if (!(Val = Merge(Val, isBytewiseValue(Op, DL))))
6487 return nullptr;
6488 return Val;
6489 }
6490
6491 // Don't try to handle the handful of other constants.
6492 return nullptr;
6493}
6494
6495// This is the recursive version of BuildSubAggregate. It takes a few different
6496// arguments. Idxs is the index within the nested struct From that we are
6497// looking at now (which is of type IndexedType). IdxSkip is the number of
6498// indices from Idxs that should be left out when inserting into the resulting
6499// struct. To is the result struct built so far, new insertvalue instructions
6500// build on that.
6501static Value *BuildSubAggregate(Value *From, Value *To, Type *IndexedType,
6503 unsigned IdxSkip,
6504 BasicBlock::iterator InsertBefore) {
6505 StructType *STy = dyn_cast<StructType>(IndexedType);
6506 if (STy) {
6507 // Save the original To argument so we can modify it
6508 Value *OrigTo = To;
6509 // General case, the type indexed by Idxs is a struct
6510 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
6511 // Process each struct element recursively
6512 Idxs.push_back(i);
6513 Value *PrevTo = To;
6514 To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
6515 InsertBefore);
6516 Idxs.pop_back();
6517 if (!To) {
6518 // Couldn't find any inserted value for this index? Cleanup
6519 while (PrevTo != OrigTo) {
6521 PrevTo = Del->getAggregateOperand();
6522 Del->eraseFromParent();
6523 }
6524 // Stop processing elements
6525 break;
6526 }
6527 }
6528 // If we successfully found a value for each of our subaggregates
6529 if (To)
6530 return To;
6531 }
6532 // Base case, the type indexed by SourceIdxs is not a struct, or not all of
6533 // the struct's elements had a value that was inserted directly. In the latter
6534 // case, perhaps we can't determine each of the subelements individually, but
6535 // we might be able to find the complete struct somewhere.
6536
6537 // Find the value that is at that particular spot
6538 Value *V = FindInsertedValue(From, Idxs);
6539
6540 if (!V)
6541 return nullptr;
6542
6543 // Insert the value in the new (sub) aggregate
6544 return InsertValueInst::Create(To, V, ArrayRef(Idxs).slice(IdxSkip), "tmp",
6545 InsertBefore);
6546}
6547
6548// This helper takes a nested struct and extracts a part of it (which is again a
6549// struct) into a new value. For example, given the struct:
6550// { a, { b, { c, d }, e } }
6551// and the indices "1, 1" this returns
6552// { c, d }.
6553//
6554// It does this by inserting an insertvalue for each element in the resulting
6555// struct, as opposed to just inserting a single struct. This will only work if
6556// each of the elements of the substruct are known (ie, inserted into From by an
6557// insertvalue instruction somewhere).
6558//
6559// All inserted insertvalue instructions are inserted before InsertBefore
6561 BasicBlock::iterator InsertBefore) {
6562 Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
6563 idx_range);
6564 Value *To = PoisonValue::get(IndexedType);
6565 SmallVector<unsigned, 10> Idxs(idx_range);
6566 unsigned IdxSkip = Idxs.size();
6567
6568 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
6569}
6570
6571/// Given an aggregate and a sequence of indices, see if the scalar value
6572/// indexed is already around as a register, for example if it was inserted
6573/// directly into the aggregate.
6574///
6575/// If InsertBefore is not null, this function will duplicate (modified)
6576/// insertvalues when a part of a nested struct is extracted.
6577Value *
6579 std::optional<BasicBlock::iterator> InsertBefore) {
6580 // Nothing to index? Just return V then (this is useful at the end of our
6581 // recursion).
6582 if (idx_range.empty())
6583 return V;
6584 // We have indices, so V should have an indexable type.
6585 assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
6586 "Not looking at a struct or array?");
6587 assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
6588 "Invalid indices for type?");
6589
6590 if (Constant *C = dyn_cast<Constant>(V)) {
6591 C = C->getAggregateElement(idx_range[0]);
6592 if (!C) return nullptr;
6593 return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
6594 }
6595
6597 // Loop the indices for the insertvalue instruction in parallel with the
6598 // requested indices
6599 const unsigned *req_idx = idx_range.begin();
6600 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
6601 i != e; ++i, ++req_idx) {
6602 if (req_idx == idx_range.end()) {
6603 // We can't handle this without inserting insertvalues
6604 if (!InsertBefore)
6605 return nullptr;
6606
6607 // The requested index identifies a part of a nested aggregate. Handle
6608 // this specially. For example,
6609 // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
6610 // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
6611 // %C = extractvalue {i32, { i32, i32 } } %B, 1
6612 // This can be changed into
6613 // %A = insertvalue {i32, i32 } undef, i32 10, 0
6614 // %C = insertvalue {i32, i32 } %A, i32 11, 1
6615 // which allows the unused 0,0 element from the nested struct to be
6616 // removed.
6617 return BuildSubAggregate(V, ArrayRef(idx_range.begin(), req_idx),
6618 *InsertBefore);
6619 }
6620
6621 // This insert value inserts something else than what we are looking for.
6622 // See if the (aggregate) value inserted into has the value we are
6623 // looking for, then.
6624 if (*req_idx != *i)
6625 return FindInsertedValue(I->getAggregateOperand(), idx_range,
6626 InsertBefore);
6627 }
6628 // If we end up here, the indices of the insertvalue match with those
6629 // requested (though possibly only partially). Now we recursively look at
6630 // the inserted value, passing any remaining indices.
6631 return FindInsertedValue(I->getInsertedValueOperand(),
6632 ArrayRef(req_idx, idx_range.end()), InsertBefore);
6633 }
6634
6636 // If we're extracting a value from an aggregate that was extracted from
6637 // something else, we can extract from that something else directly instead.
6638 // However, we will need to chain I's indices with the requested indices.
6639
6640 // Calculate the number of indices required
6641 unsigned size = I->getNumIndices() + idx_range.size();
6642 // Allocate some space to put the new indices in
6644 Idxs.reserve(size);
6645 // Add indices from the extract value instruction
6646 Idxs.append(I->idx_begin(), I->idx_end());
6647
6648 // Add requested indices
6649 Idxs.append(idx_range.begin(), idx_range.end());
6650
6651 assert(Idxs.size() == size
6652 && "Number of indices added not correct?");
6653
6654 return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
6655 }
6656 // Otherwise, we don't know (such as, extracting from a function return value
6657 // or load instruction)
6658 return nullptr;
6659}
6660
6661// If V refers to an initialized global constant, set Slice either to
6662// its initializer if the size of its elements equals ElementSize, or,
6663// for ElementSize == 8, to its representation as an array of unsiged
6664// char. Return true on success.
6665// Offset is in the unit "nr of ElementSize sized elements".
6668 unsigned ElementSize, uint64_t Offset) {
6669 assert(V && "V should not be null.");
6670 assert((ElementSize % 8) == 0 &&
6671 "ElementSize expected to be a multiple of the size of a byte.");
6672 unsigned ElementSizeInBytes = ElementSize / 8;
6673
6674 // Drill down into the pointer expression V, ignoring any intervening
6675 // casts, and determine the identity of the object it references along
6676 // with the cumulative byte offset into it.
6677 const GlobalVariable *GV =
6679 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
6680 // Fail if V is not based on constant global object.
6681 return false;
6682
6683 const DataLayout &DL = GV->getDataLayout();
6684 APInt Off(DL.getIndexTypeSizeInBits(V->getType()), 0);
6685
6686 if (GV != V->stripAndAccumulateConstantOffsets(DL, Off,
6687 /*AllowNonInbounds*/ true))
6688 // Fail if a constant offset could not be determined.
6689 return false;
6690
6691 uint64_t StartIdx = Off.getLimitedValue();
6692 if (StartIdx == UINT64_MAX)
6693 // Fail if the constant offset is excessive.
6694 return false;
6695
6696 // Off/StartIdx is in the unit of bytes. So we need to convert to number of
6697 // elements. Simply bail out if that isn't possible.
6698 if ((StartIdx % ElementSizeInBytes) != 0)
6699 return false;
6700
6701 Offset += StartIdx / ElementSizeInBytes;
6702 ConstantDataArray *Array = nullptr;
6703 ArrayType *ArrayTy = nullptr;
6704
6705 if (GV->getInitializer()->isNullValue()) {
6706 Type *GVTy = GV->getValueType();
6707 uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy).getFixedValue();
6708 uint64_t Length = SizeInBytes / ElementSizeInBytes;
6709
6710 Slice.Array = nullptr;
6711 Slice.Offset = 0;
6712 // Return an empty Slice for undersized constants to let callers
6713 // transform even undefined library calls into simpler, well-defined
6714 // expressions. This is preferable to making the calls although it
6715 // prevents sanitizers from detecting such calls.
6716 Slice.Length = Length < Offset ? 0 : Length - Offset;
6717 return true;
6718 }
6719
6720 auto *Init = const_cast<Constant *>(GV->getInitializer());
6721 if (auto *ArrayInit = dyn_cast<ConstantDataArray>(Init)) {
6722 Type *InitElTy = ArrayInit->getElementType();
6723 if (InitElTy->isIntegerTy(ElementSize)) {
6724 // If Init is an initializer for an array of the expected type
6725 // and size, use it as is.
6726 Array = ArrayInit;
6727 ArrayTy = ArrayInit->getType();
6728 }
6729 }
6730
6731 if (!Array) {
6732 if (ElementSize != 8)
6733 // TODO: Handle conversions to larger integral types.
6734 return false;
6735
6736 // Otherwise extract the portion of the initializer starting
6737 // at Offset as an array of bytes, and reset Offset.
6739 if (!Init)
6740 return false;
6741
6742 Offset = 0;
6744 ArrayTy = dyn_cast<ArrayType>(Init->getType());
6745 }
6746
6747 uint64_t NumElts = ArrayTy->getArrayNumElements();
6748 if (Offset > NumElts)
6749 return false;
6750
6751 Slice.Array = Array;
6752 Slice.Offset = Offset;
6753 Slice.Length = NumElts - Offset;
6754 return true;
6755}
6756
6757/// Extract bytes from the initializer of the constant array V, which need
6758/// not be a nul-terminated string. On success, store the bytes in Str and
6759/// return true. When TrimAtNul is set, Str will contain only the bytes up
6760/// to but not including the first nul. Return false on failure.
6762 bool TrimAtNul) {
6764 if (!getConstantDataArrayInfo(V, Slice, 8))
6765 return false;
6766
6767 if (Slice.Array == nullptr) {
6768 if (TrimAtNul) {
6769 // Return a nul-terminated string even for an empty Slice. This is
6770 // safe because all existing SimplifyLibcalls callers require string
6771 // arguments and the behavior of the functions they fold is undefined
6772 // otherwise. Folding the calls this way is preferable to making
6773 // the undefined library calls, even though it prevents sanitizers
6774 // from reporting such calls.
6775 Str = StringRef();
6776 return true;
6777 }
6778 if (Slice.Length == 1) {
6779 Str = StringRef("", 1);
6780 return true;
6781 }
6782 // We cannot instantiate a StringRef as we do not have an appropriate string
6783 // of 0s at hand.
6784 return false;
6785 }
6786
6787 // Start out with the entire array in the StringRef.
6788 Str = Slice.Array->getAsString();
6789 // Skip over 'offset' bytes.
6790 Str = Str.substr(Slice.Offset);
6791
6792 if (TrimAtNul) {
6793 // Trim off the \0 and anything after it. If the array is not nul
6794 // terminated, we just return the whole end of string. The client may know
6795 // some other way that the string is length-bound.
6796 Str = Str.substr(0, Str.find('\0'));
6797 }
6798 return true;
6799}
6800
6801// These next two are very similar to the above, but also look through PHI
6802// nodes.
6803// TODO: See if we can integrate these two together.
6804
6805/// If we can compute the length of the string pointed to by
6806/// the specified pointer, return 'len+1'. If we can't, return 0.
6809 unsigned CharSize) {
6810 // Look through noop bitcast instructions.
6811 V = V->stripPointerCasts();
6812
6813 // If this is a PHI node, there are two cases: either we have already seen it
6814 // or we haven't.
6815 if (const PHINode *PN = dyn_cast<PHINode>(V)) {
6816 if (!PHIs.insert(PN).second)
6817 return ~0ULL; // already in the set.
6818
6819 // If it was new, see if all the input strings are the same length.
6820 uint64_t LenSoFar = ~0ULL;
6821 for (Value *IncValue : PN->incoming_values()) {
6822 uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize);
6823 if (Len == 0) return 0; // Unknown length -> unknown.
6824
6825 if (Len == ~0ULL) continue;
6826
6827 if (Len != LenSoFar && LenSoFar != ~0ULL)
6828 return 0; // Disagree -> unknown.
6829 LenSoFar = Len;
6830 }
6831
6832 // Success, all agree.
6833 return LenSoFar;
6834 }
6835
6836 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
6837 if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
6838 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);
6839 if (Len1 == 0) return 0;
6840 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);
6841 if (Len2 == 0) return 0;
6842 if (Len1 == ~0ULL) return Len2;
6843 if (Len2 == ~0ULL) return Len1;
6844 if (Len1 != Len2) return 0;
6845 return Len1;
6846 }
6847
6848 // Otherwise, see if we can read the string.
6850 if (!getConstantDataArrayInfo(V, Slice, CharSize))
6851 return 0;
6852
6853 if (Slice.Array == nullptr)
6854 // Zeroinitializer (including an empty one).
6855 return 1;
6856
6857 // Search for the first nul character. Return a conservative result even
6858 // when there is no nul. This is safe since otherwise the string function
6859 // being folded such as strlen is undefined, and can be preferable to
6860 // making the undefined library call.
6861 unsigned NullIndex = 0;
6862 for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) {
6863 if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0)
6864 break;
6865 }
6866
6867 return NullIndex + 1;
6868}
6869
6870/// If we can compute the length of the string pointed to by
6871/// the specified pointer, return 'len+1'. If we can't, return 0.
6872uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) {
6873 if (!V->getType()->isPointerTy())
6874 return 0;
6875
6877 uint64_t Len = GetStringLengthH(V, PHIs, CharSize);
6878 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
6879 // an empty string as a length.
6880 return Len == ~0ULL ? 1 : Len;
6881}
6882
6883const Value *
6885 bool MustPreserveOffset) {
6886 assert(Call &&
6887 "getArgumentAliasingToReturnedPointer only works on nonnull calls");
6888 if (const Value *RV = Call->getReturnedArgOperand())
6889 return RV;
6890 // This can be used only as a aliasing property.
6892 Call, MustPreserveOffset))
6893 return Call->getArgOperand(0);
6894 return nullptr;
6895}
6896
6898 const CallBase *Call, bool MustPreserveOffset) {
6899 switch (Call->getIntrinsicID()) {
6900 case Intrinsic::launder_invariant_group:
6901 case Intrinsic::strip_invariant_group:
6902 case Intrinsic::aarch64_irg:
6903 case Intrinsic::aarch64_tagp:
6904 // The amdgcn_make_buffer_rsrc function does not alter the address of the
6905 // input pointer (and thus preserves the byte offset, which is the property
6906 // the MustPreserveOffset flag selects). However, it will not necessarily
6907 // map ptr addrspace(N) null to ptr addrspace(8) null, aka the "null
6908 // descriptor", which has "all loads return 0, all stores are dropped"
6909 // semantics. Given the context of this intrinsic list, no one should be
6910 // relying on such a strict bit-exact null mapping (and, at time of
6911 // writing, they are not), but we document this fact out of an abundance
6912 // of caution.
6913 case Intrinsic::amdgcn_make_buffer_rsrc:
6914 return true;
6915 case Intrinsic::ptrmask:
6916 return !MustPreserveOffset;
6917 case Intrinsic::threadlocal_address:
6918 // The underlying variable changes with thread ID. The Thread ID may change
6919 // at coroutine suspend points.
6920 return !Call->getParent()->getParent()->isPresplitCoroutine();
6921 default:
6922 return false;
6923 }
6924}
6925
6926/// \p PN defines a loop-variant pointer to an object. Check if the
6927/// previous iteration of the loop was referring to the same object as \p PN.
6929 const LoopInfo *LI) {
6930 // Find the loop-defined value.
6931 Loop *L = LI->getLoopFor(PN->getParent());
6932 if (PN->getNumIncomingValues() != 2)
6933 return true;
6934
6935 // Find the value from previous iteration.
6936 auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
6937 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6938 PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
6939 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6940 return true;
6941
6942 // If a new pointer is loaded in the loop, the pointer references a different
6943 // object in every iteration. E.g.:
6944 // for (i)
6945 // int *p = a[i];
6946 // ...
6947 if (auto *Load = dyn_cast<LoadInst>(PrevValue))
6948 if (!L->isLoopInvariant(Load->getPointerOperand()))
6949 return false;
6950 return true;
6951}
6952
6953const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup) {
6954 for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
6955 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
6956 const Value *PtrOp = GEP->getPointerOperand();
6957 if (!PtrOp->getType()->isPointerTy()) // Only handle scalar pointer base.
6958 return V;
6959 V = PtrOp;
6960 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
6961 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
6962 Value *NewV = cast<Operator>(V)->getOperand(0);
6963 if (!NewV->getType()->isPointerTy())
6964 return V;
6965 V = NewV;
6966 } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
6967 if (GA->isInterposable())
6968 return V;
6969 V = GA->getAliasee();
6970 } else {
6971 if (auto *PHI = dyn_cast<PHINode>(V)) {
6972 // Look through single-arg phi nodes created by LCSSA.
6973 if (PHI->getNumIncomingValues() == 1) {
6974 V = PHI->getIncomingValue(0);
6975 continue;
6976 }
6977 } else if (auto *Call = dyn_cast<CallBase>(V)) {
6978 // CaptureTracking can know about special capturing properties of some
6979 // intrinsics like launder.invariant.group, that can't be expressed with
6980 // the attributes, but have properties like returning aliasing pointer.
6981 // Because some analysis may assume that nocaptured pointer is not
6982 // returned from some special intrinsic (because function would have to
6983 // be marked with returns attribute), it is crucial to use this function
6984 // because it should be in sync with CaptureTracking. Not using it may
6985 // cause weird miscompilations where 2 aliasing pointers are assumed to
6986 // noalias.
6988 Call, /*MustPreserveOffset=*/false)) {
6989 V = RP;
6990 continue;
6991 }
6992 }
6993
6994 return V;
6995 }
6996 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
6997 }
6998 return V;
6999}
7000
7003 const LoopInfo *LI, unsigned MaxLookup) {
7006 Worklist.push_back(V);
7007 do {
7008 const Value *P = Worklist.pop_back_val();
7009 P = getUnderlyingObject(P, MaxLookup);
7010
7011 if (!Visited.insert(P).second)
7012 continue;
7013
7014 if (auto *SI = dyn_cast<SelectInst>(P)) {
7015 Worklist.push_back(SI->getTrueValue());
7016 Worklist.push_back(SI->getFalseValue());
7017 continue;
7018 }
7019
7020 if (auto *PN = dyn_cast<PHINode>(P)) {
7021 // If this PHI changes the underlying object in every iteration of the
7022 // loop, don't look through it. Consider:
7023 // int **A;
7024 // for (i) {
7025 // Prev = Curr; // Prev = PHI (Prev_0, Curr)
7026 // Curr = A[i];
7027 // *Prev, *Curr;
7028 //
7029 // Prev is tracking Curr one iteration behind so they refer to different
7030 // underlying objects.
7031 if (!LI || !LI->isLoopHeader(PN->getParent()) ||
7033 append_range(Worklist, PN->incoming_values());
7034 else
7035 Objects.push_back(P);
7036 continue;
7037 }
7038
7039 Objects.push_back(P);
7040 } while (!Worklist.empty());
7041}
7042
7044 const unsigned MaxVisited = 8;
7045
7048 Worklist.push_back(V);
7049 const Value *Object = nullptr;
7050 // Used as fallback if we can't find a common underlying object through
7051 // recursion.
7052 bool First = true;
7053 const Value *FirstObject = getUnderlyingObject(V);
7054 do {
7055 const Value *P = Worklist.pop_back_val();
7056 P = First ? FirstObject : getUnderlyingObject(P);
7057 First = false;
7058
7059 if (!Visited.insert(P).second)
7060 continue;
7061
7062 if (Visited.size() == MaxVisited)
7063 return FirstObject;
7064
7065 if (auto *SI = dyn_cast<SelectInst>(P)) {
7066 Worklist.push_back(SI->getTrueValue());
7067 Worklist.push_back(SI->getFalseValue());
7068 continue;
7069 }
7070
7071 if (auto *PN = dyn_cast<PHINode>(P)) {
7072 append_range(Worklist, PN->incoming_values());
7073 continue;
7074 }
7075
7076 if (!Object)
7077 Object = P;
7078 else if (Object != P)
7079 return FirstObject;
7080 } while (!Worklist.empty());
7081
7082 return Object ? Object : FirstObject;
7083}
7084
7085/// This is the function that does the work of looking through basic
7086/// ptrtoint+arithmetic+inttoptr sequences.
7087static const Value *getUnderlyingObjectFromInt(const Value *V) {
7088 do {
7089 if (const Operator *U = dyn_cast<Operator>(V)) {
7090 // If we find a ptrtoint, we can transfer control back to the
7091 // regular getUnderlyingObjectFromInt.
7092 if (U->getOpcode() == Instruction::PtrToInt)
7093 return U->getOperand(0);
7094 // If we find an add of a constant, a multiplied value, or a phi, it's
7095 // likely that the other operand will lead us to the base
7096 // object. We don't have to worry about the case where the
7097 // object address is somehow being computed by the multiply,
7098 // because our callers only care when the result is an
7099 // identifiable object.
7100 if (U->getOpcode() != Instruction::Add ||
7101 (!isa<ConstantInt>(U->getOperand(1)) &&
7102 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
7103 !isa<PHINode>(U->getOperand(1))))
7104 return V;
7105 V = U->getOperand(0);
7106 } else {
7107 return V;
7108 }
7109 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
7110 } while (true);
7111}
7112
7113/// This is a wrapper around getUnderlyingObjects and adds support for basic
7114/// ptrtoint+arithmetic+inttoptr sequences.
7115/// It returns false if unidentified object is found in getUnderlyingObjects.
7117 SmallVectorImpl<Value *> &Objects) {
7119 SmallVector<const Value *, 4> Working(1, V);
7120 do {
7121 V = Working.pop_back_val();
7122
7124 getUnderlyingObjects(V, Objs);
7125
7126 for (const Value *V : Objs) {
7127 if (!Visited.insert(V).second)
7128 continue;
7129 if (Operator::getOpcode(V) == Instruction::IntToPtr) {
7130 const Value *O =
7131 getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
7132 if (O->getType()->isPointerTy()) {
7133 Working.push_back(O);
7134 continue;
7135 }
7136 }
7137 // If getUnderlyingObjects fails to find an identifiable object,
7138 // getUnderlyingObjectsForCodeGen also fails for safety.
7139 if (!isIdentifiedObject(V)) {
7140 Objects.clear();
7141 return false;
7142 }
7143 Objects.push_back(const_cast<Value *>(V));
7144 }
7145 } while (!Working.empty());
7146 return true;
7147}
7148
7150 AllocaInst *Result = nullptr;
7152 SmallVector<Value *, 4> Worklist;
7153
7154 auto AddWork = [&](Value *V) {
7155 if (Visited.insert(V).second)
7156 Worklist.push_back(V);
7157 };
7158
7159 AddWork(V);
7160 do {
7161 V = Worklist.pop_back_val();
7162 assert(Visited.count(V));
7163
7164 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
7165 if (Result && Result != AI)
7166 return nullptr;
7167 Result = AI;
7168 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
7169 AddWork(CI->getOperand(0));
7170 } else if (PHINode *PN = dyn_cast<PHINode>(V)) {
7171 for (Value *IncValue : PN->incoming_values())
7172 AddWork(IncValue);
7173 } else if (auto *SI = dyn_cast<SelectInst>(V)) {
7174 AddWork(SI->getTrueValue());
7175 AddWork(SI->getFalseValue());
7177 if (OffsetZero && !GEP->hasAllZeroIndices())
7178 return nullptr;
7179 AddWork(GEP->getPointerOperand());
7180 } else if (CallBase *CB = dyn_cast<CallBase>(V)) {
7181 Value *Returned = CB->getReturnedArgOperand();
7182 if (Returned)
7183 AddWork(Returned);
7184 else
7185 return nullptr;
7186 } else {
7187 return nullptr;
7188 }
7189 } while (!Worklist.empty());
7190
7191 return Result;
7192}
7193
7195 const Value *V, bool AllowLifetime, bool AllowDroppable) {
7196 for (const User *U : V->users()) {
7198 if (!II)
7199 return false;
7200
7201 if (AllowLifetime && II->isLifetimeStartOrEnd())
7202 continue;
7203
7204 if (AllowDroppable && II->isDroppable())
7205 continue;
7206
7207 return false;
7208 }
7209 return true;
7210}
7211
7214 V, /* AllowLifetime */ true, /* AllowDroppable */ false);
7215}
7218 V, /* AllowLifetime */ true, /* AllowDroppable */ true);
7219}
7220
7222 if (auto *II = dyn_cast<IntrinsicInst>(I))
7223 return isTriviallyVectorizable(II->getIntrinsicID());
7224 auto *Shuffle = dyn_cast<ShuffleVectorInst>(I);
7225 return (!Shuffle || Shuffle->isSelect()) &&
7227}
7228
7230 const Instruction *Inst, const Instruction *CtxI, AssumptionCache *AC,
7231 const DominatorTree *DT, const TargetLibraryInfo *TLI, bool UseVariableInfo,
7232 bool IgnoreUBImplyingAttrs) {
7233 return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI,
7234 AC, DT, TLI, UseVariableInfo,
7235 IgnoreUBImplyingAttrs);
7236}
7237
7239 unsigned Opcode, const Instruction *Inst, const Instruction *CtxI,
7240 AssumptionCache *AC, const DominatorTree *DT, const TargetLibraryInfo *TLI,
7241 bool UseVariableInfo, bool IgnoreUBImplyingAttrs) {
7242#ifndef NDEBUG
7243 if (Inst->getOpcode() != Opcode) {
7244 // Check that the operands are actually compatible with the Opcode override.
7245 auto hasEqualReturnAndLeadingOperandTypes =
7246 [](const Instruction *Inst, unsigned NumLeadingOperands) {
7247 if (Inst->getNumOperands() < NumLeadingOperands)
7248 return false;
7249 const Type *ExpectedType = Inst->getType();
7250 for (unsigned ItOp = 0; ItOp < NumLeadingOperands; ++ItOp)
7251 if (Inst->getOperand(ItOp)->getType() != ExpectedType)
7252 return false;
7253 return true;
7254 };
7256 hasEqualReturnAndLeadingOperandTypes(Inst, 2));
7257 assert(!Instruction::isUnaryOp(Opcode) ||
7258 hasEqualReturnAndLeadingOperandTypes(Inst, 1));
7259 }
7260#endif
7261
7262 switch (Opcode) {
7263 default:
7264 return true;
7265 case Instruction::UDiv:
7266 case Instruction::URem: {
7267 // x / y is undefined if y == 0.
7268 const APInt *V;
7269 if (match(Inst->getOperand(1), m_APInt(V)))
7270 return *V != 0;
7271 return false;
7272 }
7273 case Instruction::SDiv:
7274 case Instruction::SRem: {
7275 // x / y is undefined if y == 0 or x == INT_MIN and y == -1
7276 const APInt *Numerator, *Denominator;
7277 if (!match(Inst->getOperand(1), m_APInt(Denominator)))
7278 return false;
7279 // We cannot hoist this division if the denominator is 0.
7280 if (*Denominator == 0)
7281 return false;
7282 // It's safe to hoist if the denominator is not 0 or -1.
7283 if (!Denominator->isAllOnes())
7284 return true;
7285 // At this point we know that the denominator is -1. It is safe to hoist as
7286 // long we know that the numerator is not INT_MIN.
7287 if (match(Inst->getOperand(0), m_APInt(Numerator)))
7288 return !Numerator->isMinSignedValue();
7289 // The numerator *might* be MinSignedValue.
7290 return false;
7291 }
7292 case Instruction::Load: {
7293 if (!UseVariableInfo)
7294 return false;
7295
7296 const LoadInst *LI = dyn_cast<LoadInst>(Inst);
7297 if (!LI)
7298 return false;
7299 if (mustSuppressSpeculation(*LI))
7300 return false;
7301 const DataLayout &DL = LI->getDataLayout();
7303 LI->getPointerOperand(), LI->getType(), LI->getAlign(),
7304 SimplifyQuery(DL, TLI, DT, AC, CtxI));
7305 }
7306 case Instruction::Call: {
7307 auto *CI = dyn_cast<const CallInst>(Inst);
7308 if (!CI)
7309 return false;
7310 const Function *Callee = CI->getCalledFunction();
7311
7312 // The called function could have undefined behavior or side-effects, even
7313 // if marked readnone nounwind.
7314 if (!Callee || !Callee->isSpeculatable())
7315 return false;
7316 // Since the operands may be changed after hoisting, undefined behavior may
7317 // be triggered by some UB-implying attributes.
7318 return IgnoreUBImplyingAttrs || !CI->hasUBImplyingAttrs();
7319 }
7320 case Instruction::VAArg:
7321 case Instruction::Alloca:
7322 case Instruction::Invoke:
7323 case Instruction::CallBr:
7324 case Instruction::PHI:
7325 case Instruction::Store:
7326 case Instruction::Ret:
7327 case Instruction::UncondBr:
7328 case Instruction::CondBr:
7329 case Instruction::IndirectBr:
7330 case Instruction::Switch:
7331 case Instruction::Unreachable:
7332 case Instruction::Fence:
7333 case Instruction::AtomicRMW:
7334 case Instruction::AtomicCmpXchg:
7335 case Instruction::LandingPad:
7336 case Instruction::Resume:
7337 case Instruction::CatchSwitch:
7338 case Instruction::CatchPad:
7339 case Instruction::CatchRet:
7340 case Instruction::CleanupPad:
7341 case Instruction::CleanupRet:
7342 return false; // Misc instructions which have effects
7343 }
7344}
7345
7347 if (I.mayReadOrWriteMemory())
7348 // Memory dependency possible
7349 return true;
7351 // Can't move above a maythrow call or infinite loop. Or if an
7352 // inalloca alloca, above a stacksave call.
7353 return true;
7355 // 1) Can't reorder two inf-loop calls, even if readonly
7356 // 2) Also can't reorder an inf-loop call below a instruction which isn't
7357 // safe to speculative execute. (Inverse of above)
7358 return true;
7359 return false;
7360}
7361
7362/// Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
7376
7377/// Combine constant ranges from computeConstantRange() and computeKnownBits().
7380 bool ForSigned,
7381 const SimplifyQuery &SQ) {
7382 ConstantRange CR1 =
7383 ConstantRange::fromKnownBits(V.getKnownBits(SQ), ForSigned);
7384 ConstantRange CR2 = computeConstantRange(V, ForSigned, SQ);
7387 return CR1.intersectWith(CR2, RangeType);
7388}
7389
7391 const Value *RHS,
7392 const SimplifyQuery &SQ,
7393 bool IsNSW) {
7394 ConstantRange LHSRange =
7395 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7396 ConstantRange RHSRange =
7397 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7398
7399 // mul nsw of two non-negative numbers is also nuw.
7400 if (IsNSW && LHSRange.isAllNonNegative() && RHSRange.isAllNonNegative())
7402
7403 return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange));
7404}
7405
7407 const Value *RHS,
7408 const SimplifyQuery &SQ) {
7409 // Multiplying n * m significant bits yields a result of n + m significant
7410 // bits. If the total number of significant bits does not exceed the
7411 // result bit width (minus 1), there is no overflow.
7412 // This means if we have enough leading sign bits in the operands
7413 // we can guarantee that the result does not overflow.
7414 // Ref: "Hacker's Delight" by Henry Warren
7415 unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
7416
7417 // Note that underestimating the number of sign bits gives a more
7418 // conservative answer.
7419 unsigned SignBits =
7420 ::ComputeNumSignBits(LHS, SQ) + ::ComputeNumSignBits(RHS, SQ);
7421
7422 // First handle the easy case: if we have enough sign bits there's
7423 // definitely no overflow.
7424 if (SignBits > BitWidth + 1)
7426
7427 // There are two ambiguous cases where there can be no overflow:
7428 // SignBits == BitWidth + 1 and
7429 // SignBits == BitWidth
7430 // The second case is difficult to check, therefore we only handle the
7431 // first case.
7432 if (SignBits == BitWidth + 1) {
7433 // It overflows only when both arguments are negative and the true
7434 // product is exactly the minimum negative number.
7435 // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
7436 // For simplicity we just check if at least one side is not negative.
7437 KnownBits LHSKnown = computeKnownBits(LHS, SQ);
7438 KnownBits RHSKnown = computeKnownBits(RHS, SQ);
7439 if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative())
7441 }
7443}
7444
7447 const WithCache<const Value *> &RHS,
7448 const SimplifyQuery &SQ) {
7449 ConstantRange LHSRange =
7450 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7451 ConstantRange RHSRange =
7452 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7453 return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange));
7454}
7455
7456static OverflowResult
7459 const AddOperator *Add, const SimplifyQuery &SQ) {
7460 if (Add && Add->hasNoSignedWrap()) {
7462 }
7463
7464 // If LHS and RHS each have at least two sign bits, the addition will look
7465 // like
7466 //
7467 // XX..... +
7468 // YY.....
7469 //
7470 // If the carry into the most significant position is 0, X and Y can't both
7471 // be 1 and therefore the carry out of the addition is also 0.
7472 //
7473 // If the carry into the most significant position is 1, X and Y can't both
7474 // be 0 and therefore the carry out of the addition is also 1.
7475 //
7476 // Since the carry into the most significant position is always equal to
7477 // the carry out of the addition, there is no signed overflow.
7478 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
7480
7481 ConstantRange LHSRange =
7482 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7483 ConstantRange RHSRange =
7484 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7485 OverflowResult OR =
7486 mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange));
7488 return OR;
7489
7490 // The remaining code needs Add to be available. Early returns if not so.
7491 if (!Add)
7493
7494 // If the sign of Add is the same as at least one of the operands, this add
7495 // CANNOT overflow. If this can be determined from the known bits of the
7496 // operands the above signedAddMayOverflow() check will have already done so.
7497 // The only other way to improve on the known bits is from an assumption, so
7498 // call computeKnownBitsFromContext() directly.
7499 bool LHSOrRHSKnownNonNegative =
7500 (LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative());
7501 bool LHSOrRHSKnownNegative =
7502 (LHSRange.isAllNegative() || RHSRange.isAllNegative());
7503 if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
7504 KnownBits AddKnown(LHSRange.getBitWidth());
7505 computeKnownBitsFromContext(Add, AddKnown, SQ);
7506 if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||
7507 (AddKnown.isNegative() && LHSOrRHSKnownNegative))
7509 }
7510
7512}
7513
7515 const Value *RHS,
7516 const SimplifyQuery &SQ) {
7517 // X - (X % ?)
7518 // The remainder of a value can't have greater magnitude than itself,
7519 // so the subtraction can't overflow.
7520
7521 // X - (X -nuw ?)
7522 // In the minimal case, this would simplify to "?", so there's no subtract
7523 // at all. But if this analysis is used to peek through casts, for example,
7524 // then determining no-overflow may allow other transforms.
7525
7526 // TODO: There are other patterns like this.
7527 // See simplifyICmpWithBinOpOnLHS() for candidates.
7528 if (match(RHS, m_URem(m_Specific(LHS), m_Value())) ||
7529 match(RHS, m_NUWSub(m_Specific(LHS), m_Value())))
7530 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7532
7533 if (auto C = isImpliedByDomCondition(CmpInst::ICMP_UGE, LHS, RHS, SQ.CxtI,
7534 SQ.DL)) {
7535 if (*C)
7538 }
7539
7540 ConstantRange LHSRange =
7541 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7542 ConstantRange RHSRange =
7543 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7544 return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange));
7545}
7546
7548 const Value *RHS,
7549 const SimplifyQuery &SQ) {
7550 // X - (X % ?)
7551 // The remainder of a value can't have greater magnitude than itself,
7552 // so the subtraction can't overflow.
7553
7554 // X - (X -nsw ?)
7555 // In the minimal case, this would simplify to "?", so there's no subtract
7556 // at all. But if this analysis is used to peek through casts, for example,
7557 // then determining no-overflow may allow other transforms.
7558 if (match(RHS, m_SRem(m_Specific(LHS), m_Value())) ||
7559 match(RHS, m_NSWSub(m_Specific(LHS), m_Value())))
7560 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7562
7563 // If LHS and RHS each have at least two sign bits, the subtraction
7564 // cannot overflow.
7565 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
7567
7568 ConstantRange LHSRange =
7569 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7570 ConstantRange RHSRange =
7571 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7572 return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange));
7573}
7574
7576 const DominatorTree &DT) {
7577 SmallVector<const CondBrInst *, 2> GuardingBranches;
7579
7580 for (const User *U : WO->users()) {
7581 if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) {
7582 assert(EVI->getNumIndices() == 1 && "Obvious from CI's type");
7583
7584 if (EVI->getIndices()[0] == 0)
7585 Results.push_back(EVI);
7586 else {
7587 assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type");
7588
7589 for (const auto *U : EVI->users())
7590 if (const auto *B = dyn_cast<CondBrInst>(U))
7591 GuardingBranches.push_back(B);
7592 }
7593 } else {
7594 // We are using the aggregate directly in a way we don't want to analyze
7595 // here (storing it to a global, say).
7596 return false;
7597 }
7598 }
7599
7600 auto AllUsesGuardedByBranch = [&](const CondBrInst *BI) {
7601 BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1));
7602
7603 // Check if all users of the add are provably no-wrap.
7604 for (const auto *Result : Results) {
7605 // If the extractvalue itself is not executed on overflow, the we don't
7606 // need to check each use separately, since domination is transitive.
7607 if (DT.dominates(NoWrapEdge, Result->getParent()))
7608 continue;
7609
7610 for (const auto &RU : Result->uses())
7611 if (!DT.dominates(NoWrapEdge, RU))
7612 return false;
7613 }
7614
7615 return true;
7616 };
7617
7618 return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
7619}
7620
7621/// Shifts return poison if shiftwidth is larger than the bitwidth.
7622static bool shiftAmountKnownInRange(const Value *ShiftAmount) {
7623 auto *C = dyn_cast<Constant>(ShiftAmount);
7624 if (!C)
7625 return false;
7626
7627 // Shifts return poison if shiftwidth is larger than the bitwidth.
7629 if (auto *FVTy = dyn_cast<FixedVectorType>(C->getType())) {
7630 unsigned NumElts = FVTy->getNumElements();
7631 for (unsigned i = 0; i < NumElts; ++i)
7632 ShiftAmounts.push_back(C->getAggregateElement(i));
7633 } else if (isa<ScalableVectorType>(C->getType()))
7634 return false; // Can't tell, just return false to be safe
7635 else
7636 ShiftAmounts.push_back(C);
7637
7638 bool Safe = llvm::all_of(ShiftAmounts, [](const Constant *C) {
7639 auto *CI = dyn_cast_or_null<ConstantInt>(C);
7640 return CI && CI->getValue().ult(C->getType()->getIntegerBitWidth());
7641 });
7642
7643 return Safe;
7644}
7645
7647 bool ConsiderFlagsAndMetadata) {
7648
7649 if (ConsiderFlagsAndMetadata && includesPoison(Kind) &&
7650 Op->hasPoisonGeneratingAnnotations())
7651 return true;
7652
7653 unsigned Opcode = Op->getOpcode();
7654
7655 // Check whether opcode is a poison/undef-generating operation
7656 switch (Opcode) {
7657 case Instruction::Shl:
7658 case Instruction::AShr:
7659 case Instruction::LShr:
7660 return includesPoison(Kind) && !shiftAmountKnownInRange(Op->getOperand(1));
7661 case Instruction::FPToSI:
7662 case Instruction::FPToUI:
7663 // fptosi/ui yields poison if the resulting value does not fit in the
7664 // destination type.
7665 return true;
7666 case Instruction::Call:
7667 if (auto *II = dyn_cast<IntrinsicInst>(Op)) {
7668 switch (II->getIntrinsicID()) {
7669 // NOTE: Use IntrNoCreateUndefOrPoison when possible.
7670 case Intrinsic::ctlz:
7671 case Intrinsic::cttz:
7672 case Intrinsic::abs:
7673 // We're not considering flags so it is safe to just return false.
7674 return false;
7675 case Intrinsic::sshl_sat:
7676 case Intrinsic::ushl_sat:
7677 if (!includesPoison(Kind) ||
7678 shiftAmountKnownInRange(II->getArgOperand(1)))
7679 return false;
7680 break;
7681 }
7682 }
7683 [[fallthrough]];
7684 case Instruction::CallBr:
7685 case Instruction::Invoke: {
7686 const auto *CB = cast<CallBase>(Op);
7687 return !CB->hasRetAttr(Attribute::NoUndef) &&
7688 !CB->hasFnAttr(Attribute::NoCreateUndefOrPoison);
7689 }
7690 case Instruction::InsertElement:
7691 case Instruction::ExtractElement: {
7692 // If index exceeds the length of the vector, it returns poison
7693 auto *VTy = cast<VectorType>(Op->getOperand(0)->getType());
7694 unsigned IdxOp = Op->getOpcode() == Instruction::InsertElement ? 2 : 1;
7695 auto *Idx = dyn_cast<ConstantInt>(Op->getOperand(IdxOp));
7696 if (includesPoison(Kind))
7697 return !Idx ||
7698 Idx->getValue().uge(VTy->getElementCount().getKnownMinValue());
7699 return false;
7700 }
7701 case Instruction::ShuffleVector: {
7703 ? cast<ConstantExpr>(Op)->getShuffleMask()
7704 : cast<ShuffleVectorInst>(Op)->getShuffleMask();
7705 return includesPoison(Kind) && is_contained(Mask, PoisonMaskElem);
7706 }
7707 case Instruction::FNeg:
7708 case Instruction::PHI:
7709 case Instruction::Select:
7710 case Instruction::ExtractValue:
7711 case Instruction::InsertValue:
7712 case Instruction::Freeze:
7713 case Instruction::ICmp:
7714 case Instruction::FCmp:
7715 case Instruction::GetElementPtr:
7716 return false;
7717 case Instruction::AddrSpaceCast:
7718 return true;
7719 default: {
7720 const auto *CE = dyn_cast<ConstantExpr>(Op);
7721 if (isa<CastInst>(Op) || (CE && CE->isCast()))
7722 return false;
7723 else if (Instruction::isBinaryOp(Opcode))
7724 return false;
7725 // Be conservative and return true.
7726 return true;
7727 }
7728 }
7729}
7730
7732 bool ConsiderFlagsAndMetadata) {
7733 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::UndefOrPoison,
7734 ConsiderFlagsAndMetadata);
7735}
7736
7737bool llvm::canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata) {
7738 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::PoisonOnly,
7739 ConsiderFlagsAndMetadata);
7740}
7741
7742static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V,
7743 unsigned Depth) {
7744 if (ValAssumedPoison == V)
7745 return true;
7746
7747 const unsigned MaxDepth = 2;
7748 if (Depth >= MaxDepth)
7749 return false;
7750
7751 if (const auto *I = dyn_cast<Instruction>(V)) {
7752 if (any_of(I->operands(), [=](const Use &Op) {
7753 return propagatesPoison(Op) &&
7754 directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1);
7755 }))
7756 return true;
7757
7758 // V = extractvalue V0, idx
7759 // V2 = extractvalue V0, idx2
7760 // V0's elements are all poison or not. (e.g., add_with_overflow)
7761 const WithOverflowInst *II;
7763 (match(ValAssumedPoison, m_ExtractValue(m_Specific(II))) ||
7764 llvm::is_contained(II->args(), ValAssumedPoison)))
7765 return true;
7766 }
7767 return false;
7768}
7769
7770static bool impliesPoison(const Value *ValAssumedPoison, const Value *V,
7771 unsigned Depth) {
7772 if (isGuaranteedNotToBePoison(ValAssumedPoison))
7773 return true;
7774
7775 if (directlyImpliesPoison(ValAssumedPoison, V, /* Depth */ 0))
7776 return true;
7777
7778 const unsigned MaxDepth = 2;
7779 if (Depth >= MaxDepth)
7780 return false;
7781
7782 const auto *I = dyn_cast<Instruction>(ValAssumedPoison);
7783 if (I && !canCreatePoison(cast<Operator>(I))) {
7784 return all_of(I->operands(), [=](const Value *Op) {
7785 return impliesPoison(Op, V, Depth + 1);
7786 });
7787 }
7788 return false;
7789}
7790
7791bool llvm::impliesPoison(const Value *ValAssumedPoison, const Value *V) {
7792 return ::impliesPoison(ValAssumedPoison, V, /* Depth */ 0);
7793}
7794
7795static bool programUndefinedIfUndefOrPoison(const Value *V, bool PoisonOnly);
7796
7798 const Value *V, AssumptionCache *AC, const Instruction *CtxI,
7799 const DominatorTree *DT, unsigned Depth, UndefPoisonKind Kind) {
7801 return false;
7802
7803 if (isa<MetadataAsValue>(V))
7804 return false;
7805
7806 if (const auto *A = dyn_cast<Argument>(V)) {
7807 if (A->hasAttribute(Attribute::NoUndef) ||
7808 A->hasAttribute(Attribute::Dereferenceable) ||
7809 A->hasAttribute(Attribute::DereferenceableOrNull))
7810 return true;
7811 }
7812
7813 if (auto *C = dyn_cast<Constant>(V)) {
7814 if (isa<PoisonValue>(C))
7815 return !includesPoison(Kind);
7816
7817 if (isa<UndefValue>(C))
7818 return !includesUndef(Kind);
7819
7822 return true;
7823
7824 if (C->getType()->isVectorTy()) {
7825 if (isa<ConstantExpr>(C)) {
7826 // Scalable vectors can use a ConstantExpr to build a splat.
7827 if (Constant *SplatC = C->getSplatValue())
7828 if (isa<ConstantInt>(SplatC) || isa<ConstantFP>(SplatC))
7829 return true;
7830 } else {
7831 if (includesUndef(Kind) && C->containsUndefElement())
7832 return false;
7833 if (includesPoison(Kind) && C->containsPoisonElement())
7834 return false;
7835 return !C->containsConstantExpression();
7836 }
7837 }
7838 }
7839
7840 // Strip cast operations from a pointer value.
7841 // Note that stripPointerCastsSameRepresentation can strip off getelementptr
7842 // inbounds with zero offset. To guarantee that the result isn't poison, the
7843 // stripped pointer is checked as it has to be pointing into an allocated
7844 // object or be null `null` to ensure `inbounds` getelement pointers with a
7845 // zero offset could not produce poison.
7846 // It can strip off addrspacecast that do not change bit representation as
7847 // well. We believe that such addrspacecast is equivalent to no-op.
7848 auto *StrippedV = V->stripPointerCastsSameRepresentation();
7849 if (isa<AllocaInst>(StrippedV) || isa<GlobalVariable>(StrippedV) ||
7850 isa<Function>(StrippedV) || isa<ConstantPointerNull>(StrippedV))
7851 return true;
7852
7853 auto OpCheck = [&](const Value *V) {
7854 return isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth + 1, Kind);
7855 };
7856
7857 if (auto *Opr = dyn_cast<Operator>(V)) {
7858 // If the value is a freeze instruction, then it can never
7859 // be undef or poison.
7860 if (isa<FreezeInst>(V))
7861 return true;
7862
7863 if (const auto *CB = dyn_cast<CallBase>(V)) {
7864 if (CB->hasRetAttr(Attribute::NoUndef) ||
7865 CB->hasRetAttr(Attribute::Dereferenceable) ||
7866 CB->hasRetAttr(Attribute::DereferenceableOrNull))
7867 return true;
7868 }
7869
7870 if (!::canCreateUndefOrPoison(Opr, Kind,
7871 /*ConsiderFlagsAndMetadata=*/true)) {
7872 if (const auto *PN = dyn_cast<PHINode>(V)) {
7873 unsigned Num = PN->getNumIncomingValues();
7874 bool IsWellDefined = true;
7875 for (unsigned i = 0; i < Num; ++i) {
7876 if (PN == PN->getIncomingValue(i))
7877 continue;
7878 auto *TI = PN->getIncomingBlock(i)->getTerminator();
7879 if (!isGuaranteedNotToBeUndefOrPoison(PN->getIncomingValue(i), AC, TI,
7880 DT, Depth + 1, Kind)) {
7881 IsWellDefined = false;
7882 break;
7883 }
7884 }
7885 if (IsWellDefined)
7886 return true;
7887 } else if (auto *Splat = isa<ShuffleVectorInst>(Opr) ? getSplatValue(Opr)
7888 : nullptr) {
7889 // For splats we only need to check the value being splatted.
7890 if (OpCheck(Splat))
7891 return true;
7892 } else if (all_of(Opr->operands(), OpCheck))
7893 return true;
7894 }
7895 }
7896
7897 if (auto *I = dyn_cast<LoadInst>(V))
7898 if (I->hasMetadata(LLVMContext::MD_noundef) ||
7899 I->hasMetadata(LLVMContext::MD_dereferenceable) ||
7900 I->hasMetadata(LLVMContext::MD_dereferenceable_or_null))
7901 return true;
7902
7904 return true;
7905
7906 // CxtI may be null or a cloned instruction.
7907 if (!CtxI || !CtxI->getParent() || !DT)
7908 return false;
7909
7910 auto *DNode = DT->getNode(CtxI->getParent());
7911 if (!DNode)
7912 // Unreachable block
7913 return false;
7914
7915 // If V is used as a branch condition before reaching CtxI, V cannot be
7916 // undef or poison.
7917 // br V, BB1, BB2
7918 // BB1:
7919 // CtxI ; V cannot be undef or poison here
7920 auto *Dominator = DNode->getIDom();
7921 // This check is purely for compile time reasons: we can skip the IDom walk
7922 // if what we are checking for includes undef and the value is not an integer.
7923 if (!includesUndef(Kind) || V->getType()->isIntegerTy())
7924 while (Dominator) {
7925 auto *TI = Dominator->getBlock()->getTerminatorOrNull();
7926
7927 Value *Cond = nullptr;
7928 if (auto BI = dyn_cast_or_null<CondBrInst>(TI)) {
7929 Cond = BI->getCondition();
7930 } else if (auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
7931 Cond = SI->getCondition();
7932 }
7933
7934 if (Cond) {
7935 if (Cond == V)
7936 return true;
7937 else if (!includesUndef(Kind) && isa<Operator>(Cond)) {
7938 // For poison, we can analyze further
7939 auto *Opr = cast<Operator>(Cond);
7940 if (any_of(Opr->operands(), [V](const Use &U) {
7941 return V == U && propagatesPoison(U);
7942 }))
7943 return true;
7944 }
7945 }
7946
7947 Dominator = Dominator->getIDom();
7948 }
7949
7950 if (AC && getKnowledgeValidInContext(V, {Attribute::NoUndef}, *AC, CtxI, DT))
7951 return true;
7952
7953 return false;
7954}
7955
7957 const Instruction *CtxI,
7958 const DominatorTree *DT,
7959 unsigned Depth) {
7960 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7962}
7963
7965 const Instruction *CtxI,
7966 const DominatorTree *DT, unsigned Depth) {
7967 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7969}
7970
7972 const Instruction *CtxI,
7973 const DominatorTree *DT, unsigned Depth) {
7974 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7976}
7977
7978/// Return true if undefined behavior would provably be executed on the path to
7979/// OnPathTo if Root produced a posion result. Note that this doesn't say
7980/// anything about whether OnPathTo is actually executed or whether Root is
7981/// actually poison. This can be used to assess whether a new use of Root can
7982/// be added at a location which is control equivalent with OnPathTo (such as
7983/// immediately before it) without introducing UB which didn't previously
7984/// exist. Note that a false result conveys no information.
7986 Instruction *OnPathTo,
7987 DominatorTree *DT) {
7988 // Basic approach is to assume Root is poison, propagate poison forward
7989 // through all users we can easily track, and then check whether any of those
7990 // users are provable UB and must execute before out exiting block might
7991 // exit.
7992
7993 // The set of all recursive users we've visited (which are assumed to all be
7994 // poison because of said visit)
7997 Worklist.push_back(Root);
7998 while (!Worklist.empty()) {
7999 const Instruction *I = Worklist.pop_back_val();
8000
8001 // If we know this must trigger UB on a path leading our target.
8002 if (mustTriggerUB(I, KnownPoison) && DT->dominates(I, OnPathTo))
8003 return true;
8004
8005 // If we can't analyze propagation through this instruction, just skip it
8006 // and transitive users. Safe as false is a conservative result.
8007 if (I != Root && !any_of(I->operands(), [&KnownPoison](const Use &U) {
8008 return KnownPoison.contains(U) && propagatesPoison(U);
8009 }))
8010 continue;
8011
8012 if (KnownPoison.insert(I).second)
8013 for (const User *User : I->users())
8014 Worklist.push_back(cast<Instruction>(User));
8015 }
8016
8017 // Might be non-UB, or might have a path we couldn't prove must execute on
8018 // way to exiting bb.
8019 return false;
8020}
8021
8023 const SimplifyQuery &SQ) {
8024 return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1),
8025 Add, SQ);
8026}
8027
8030 const WithCache<const Value *> &RHS,
8031 const SimplifyQuery &SQ) {
8032 return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, SQ);
8033}
8034
8036 // Note: An atomic operation isn't guaranteed to return in a reasonable amount
8037 // of time because it's possible for another thread to interfere with it for an
8038 // arbitrary length of time, but programs aren't allowed to rely on that.
8039
8040 // If there is no successor, then execution can't transfer to it.
8041 if (isa<ReturnInst>(I))
8042 return false;
8044 return false;
8045
8046 // Note: Do not add new checks here; instead, change Instruction::mayThrow or
8047 // Instruction::willReturn.
8048 //
8049 // FIXME: Move this check into Instruction::willReturn.
8050 if (isa<CatchPadInst>(I)) {
8051 switch (classifyEHPersonality(I->getFunction()->getPersonalityFn())) {
8052 default:
8053 // A catchpad may invoke exception object constructors and such, which
8054 // in some languages can be arbitrary code, so be conservative by default.
8055 return false;
8057 // For CoreCLR, it just involves a type test.
8058 return true;
8059 }
8060 }
8061
8062 // An instruction that returns without throwing must transfer control flow
8063 // to a successor.
8064 return !I->mayThrow() && I->willReturn();
8065}
8066
8068 // TODO: This is slightly conservative for invoke instruction since exiting
8069 // via an exception *is* normal control for them.
8070 for (const Instruction &I : *BB)
8072 return false;
8073 return true;
8074}
8075
8082
8085 assert(ScanLimit && "scan limit must be non-zero");
8086 for (const Instruction &I : Range) {
8087 if (--ScanLimit == 0)
8088 return false;
8090 return false;
8091 }
8092 return true;
8093}
8094
8096 const Loop *L) {
8097 // The loop header is guaranteed to be executed for every iteration.
8098 //
8099 // FIXME: Relax this constraint to cover all basic blocks that are
8100 // guaranteed to be executed at every iteration.
8101 if (I->getParent() != L->getHeader()) return false;
8102
8103 for (const Instruction &LI : *L->getHeader()) {
8104 if (&LI == I) return true;
8105 if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;
8106 }
8107 llvm_unreachable("Instruction not contained in its own parent basic block.");
8108}
8109
8111 switch (IID) {
8112 // TODO: Add more intrinsics.
8113 case Intrinsic::sadd_with_overflow:
8114 case Intrinsic::ssub_with_overflow:
8115 case Intrinsic::smul_with_overflow:
8116 case Intrinsic::uadd_with_overflow:
8117 case Intrinsic::usub_with_overflow:
8118 case Intrinsic::umul_with_overflow:
8119 // If an input is a vector containing a poison element, the
8120 // two output vectors (calculated results, overflow bits)'
8121 // corresponding lanes are poison.
8122 return true;
8123 case Intrinsic::ctpop:
8124 case Intrinsic::ctlz:
8125 case Intrinsic::cttz:
8126 case Intrinsic::abs:
8127 case Intrinsic::smax:
8128 case Intrinsic::smin:
8129 case Intrinsic::umax:
8130 case Intrinsic::umin:
8131 case Intrinsic::scmp:
8132 case Intrinsic::is_fpclass:
8133 case Intrinsic::ptrmask:
8134 case Intrinsic::ucmp:
8135 case Intrinsic::bitreverse:
8136 case Intrinsic::bswap:
8137 case Intrinsic::sadd_sat:
8138 case Intrinsic::ssub_sat:
8139 case Intrinsic::sshl_sat:
8140 case Intrinsic::uadd_sat:
8141 case Intrinsic::usub_sat:
8142 case Intrinsic::ushl_sat:
8143 case Intrinsic::smul_fix:
8144 case Intrinsic::smul_fix_sat:
8145 case Intrinsic::umul_fix:
8146 case Intrinsic::umul_fix_sat:
8147 case Intrinsic::pow:
8148 case Intrinsic::powi:
8149 case Intrinsic::sin:
8150 case Intrinsic::sinh:
8151 case Intrinsic::cos:
8152 case Intrinsic::cosh:
8153 case Intrinsic::sincos:
8154 case Intrinsic::sincospi:
8155 case Intrinsic::tan:
8156 case Intrinsic::tanh:
8157 case Intrinsic::asin:
8158 case Intrinsic::acos:
8159 case Intrinsic::atan:
8160 case Intrinsic::atan2:
8161 case Intrinsic::canonicalize:
8162 case Intrinsic::sqrt:
8163 case Intrinsic::exp:
8164 case Intrinsic::exp2:
8165 case Intrinsic::exp10:
8166 case Intrinsic::log:
8167 case Intrinsic::log2:
8168 case Intrinsic::log10:
8169 case Intrinsic::modf:
8170 case Intrinsic::floor:
8171 case Intrinsic::ceil:
8172 case Intrinsic::trunc:
8173 case Intrinsic::rint:
8174 case Intrinsic::nearbyint:
8175 case Intrinsic::round:
8176 case Intrinsic::roundeven:
8177 case Intrinsic::lrint:
8178 case Intrinsic::llrint:
8179 case Intrinsic::fshl:
8180 case Intrinsic::fshr:
8181 return true;
8182 default:
8183 return false;
8184 }
8185}
8186
8187bool llvm::propagatesPoison(const Use &PoisonOp) {
8188 const Operator *I = cast<Operator>(PoisonOp.getUser());
8189 switch (I->getOpcode()) {
8190 case Instruction::Freeze:
8191 case Instruction::PHI:
8192 case Instruction::Invoke:
8193 return false;
8194 case Instruction::Select:
8195 return PoisonOp.getOperandNo() == 0;
8196 case Instruction::Call:
8197 if (auto *II = dyn_cast<IntrinsicInst>(I))
8198 return intrinsicPropagatesPoison(II->getIntrinsicID());
8199 return false;
8200 case Instruction::ICmp:
8201 case Instruction::FCmp:
8202 case Instruction::GetElementPtr:
8203 return true;
8204 default:
8206 return true;
8207
8208 // Be conservative and return false.
8209 return false;
8210 }
8211}
8212
8213/// Enumerates all operands of \p I that are guaranteed to not be undef or
8214/// poison. If the callback \p Handle returns true, stop processing and return
8215/// true. Otherwise, return false.
8216template <typename CallableT>
8218 const CallableT &Handle) {
8219 switch (I->getOpcode()) {
8220 case Instruction::Store:
8221 if (Handle(cast<StoreInst>(I)->getPointerOperand()))
8222 return true;
8223 break;
8224
8225 case Instruction::Load:
8226 if (Handle(cast<LoadInst>(I)->getPointerOperand()))
8227 return true;
8228 break;
8229
8230 // Since dereferenceable attribute imply noundef, atomic operations
8231 // also implicitly have noundef pointers too
8232 case Instruction::AtomicCmpXchg:
8234 return true;
8235 break;
8236
8237 case Instruction::AtomicRMW:
8238 if (Handle(cast<AtomicRMWInst>(I)->getPointerOperand()))
8239 return true;
8240 break;
8241
8242 case Instruction::Call:
8243 case Instruction::Invoke: {
8244 const CallBase *CB = cast<CallBase>(I);
8245 if (CB->isIndirectCall() && Handle(CB->getCalledOperand()))
8246 return true;
8247 for (unsigned i = 0; i < CB->arg_size(); ++i)
8248 if ((CB->paramHasAttr(i, Attribute::NoUndef) ||
8249 CB->paramHasAttr(i, Attribute::Dereferenceable) ||
8250 CB->paramHasAttr(i, Attribute::DereferenceableOrNull)) &&
8251 Handle(CB->getArgOperand(i)))
8252 return true;
8253 break;
8254 }
8255 case Instruction::Ret:
8256 if (I->getFunction()->hasRetAttribute(Attribute::NoUndef) &&
8257 Handle(I->getOperand(0)))
8258 return true;
8259 break;
8260 case Instruction::Switch:
8261 if (Handle(cast<SwitchInst>(I)->getCondition()))
8262 return true;
8263 break;
8264 case Instruction::CondBr:
8265 if (Handle(cast<CondBrInst>(I)->getCondition()))
8266 return true;
8267 break;
8268 default:
8269 break;
8270 }
8271
8272 return false;
8273}
8274
8275/// Enumerates all operands of \p I that are guaranteed to not be poison.
8276template <typename CallableT>
8278 const CallableT &Handle) {
8279 if (handleGuaranteedWellDefinedOps(I, Handle))
8280 return true;
8281 switch (I->getOpcode()) {
8282 // Divisors of these operations are allowed to be partially undef.
8283 case Instruction::UDiv:
8284 case Instruction::SDiv:
8285 case Instruction::URem:
8286 case Instruction::SRem:
8287 return Handle(I->getOperand(1));
8288 default:
8289 return false;
8290 }
8291}
8292
8294 const SmallPtrSetImpl<const Value *> &KnownPoison) {
8296 I, [&](const Value *V) { return KnownPoison.count(V); });
8297}
8298
8300 bool PoisonOnly) {
8301 // We currently only look for uses of values within the same basic
8302 // block, as that makes it easier to guarantee that the uses will be
8303 // executed given that Inst is executed.
8304 //
8305 // FIXME: Expand this to consider uses beyond the same basic block. To do
8306 // this, look out for the distinction between post-dominance and strong
8307 // post-dominance.
8308 const BasicBlock *BB = nullptr;
8310 if (const auto *Inst = dyn_cast<Instruction>(V)) {
8311 BB = Inst->getParent();
8312 Begin = Inst->getIterator();
8313 Begin++;
8314 } else if (const auto *Arg = dyn_cast<Argument>(V)) {
8315 if (Arg->getParent()->isDeclaration())
8316 return false;
8317 BB = &Arg->getParent()->getEntryBlock();
8318 Begin = BB->begin();
8319 } else {
8320 return false;
8321 }
8322
8323 // Limit number of instructions we look at, to avoid scanning through large
8324 // blocks. The current limit is chosen arbitrarily.
8325 unsigned ScanLimit = 32;
8326 BasicBlock::const_iterator End = BB->end();
8327
8328 if (!PoisonOnly) {
8329 // Since undef does not propagate eagerly, be conservative & just check
8330 // whether a value is directly passed to an instruction that must take
8331 // well-defined operands.
8332
8333 for (const auto &I : make_range(Begin, End)) {
8334 if (--ScanLimit == 0)
8335 break;
8336
8337 if (handleGuaranteedWellDefinedOps(&I, [V](const Value *WellDefinedOp) {
8338 return WellDefinedOp == V;
8339 }))
8340 return true;
8341
8343 break;
8344 }
8345 return false;
8346 }
8347
8348 // Set of instructions that we have proved will yield poison if Inst
8349 // does.
8350 SmallPtrSet<const Value *, 16> YieldsPoison;
8352
8353 YieldsPoison.insert(V);
8354 Visited.insert(BB);
8355
8356 while (true) {
8357 for (const auto &I : make_range(Begin, End)) {
8358 if (--ScanLimit == 0)
8359 return false;
8360 if (mustTriggerUB(&I, YieldsPoison))
8361 return true;
8363 return false;
8364
8365 // If an operand is poison and propagates it, mark I as yielding poison.
8366 for (const Use &Op : I.operands()) {
8367 if (YieldsPoison.count(Op) && propagatesPoison(Op)) {
8368 YieldsPoison.insert(&I);
8369 break;
8370 }
8371 }
8372
8373 // Special handling for select, which returns poison if its operand 0 is
8374 // poison (handled in the loop above) *or* if both its true/false operands
8375 // are poison (handled here).
8376 if (I.getOpcode() == Instruction::Select &&
8377 YieldsPoison.count(I.getOperand(1)) &&
8378 YieldsPoison.count(I.getOperand(2))) {
8379 YieldsPoison.insert(&I);
8380 }
8381 }
8382
8383 BB = BB->getSingleSuccessor();
8384 if (!BB || !Visited.insert(BB).second)
8385 break;
8386
8387 Begin = BB->getFirstNonPHIIt();
8388 End = BB->end();
8389 }
8390 return false;
8391}
8392
8394 return ::programUndefinedIfUndefOrPoison(Inst, false);
8395}
8396
8398 return ::programUndefinedIfUndefOrPoison(Inst, true);
8399}
8400
8401static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) {
8402 if (FMF.noNaNs())
8403 return true;
8404
8405 if (auto *C = dyn_cast<ConstantFP>(V))
8406 return !C->isNaN();
8407
8408 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8409 if (!C->getElementType()->isFloatingPointTy())
8410 return false;
8411 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8412 if (C->getElementAsAPFloat(I).isNaN())
8413 return false;
8414 }
8415 return true;
8416 }
8417
8419 return true;
8420
8421 return false;
8422}
8423
8424static bool isKnownNonZero(const Value *V) {
8425 if (auto *C = dyn_cast<ConstantFP>(V))
8426 return !C->isZero();
8427
8428 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8429 if (!C->getElementType()->isFloatingPointTy())
8430 return false;
8431 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8432 if (C->getElementAsAPFloat(I).isZero())
8433 return false;
8434 }
8435 return true;
8436 }
8437
8438 return false;
8439}
8440
8441/// Match clamp pattern for float types without care about NaNs or signed zeros.
8442/// Given non-min/max outer cmp/select from the clamp pattern this
8443/// function recognizes if it can be substitued by a "canonical" min/max
8444/// pattern.
8446 Value *CmpLHS, Value *CmpRHS,
8447 Value *TrueVal, Value *FalseVal,
8448 Value *&LHS, Value *&RHS) {
8449 // Try to match
8450 // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2))
8451 // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2))
8452 // and return description of the outer Max/Min.
8453
8454 // First, check if select has inverse order:
8455 if (CmpRHS == FalseVal) {
8456 std::swap(TrueVal, FalseVal);
8457 Pred = CmpInst::getInversePredicate(Pred);
8458 }
8459
8460 // Assume success now. If there's no match, callers should not use these anyway.
8461 LHS = TrueVal;
8462 RHS = FalseVal;
8463
8464 const APFloat *FC1;
8465 if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite())
8466 return {SPF_UNKNOWN, SPNB_NA, false};
8467
8468 const APFloat *FC2;
8469 switch (Pred) {
8470 case CmpInst::FCMP_OLT:
8471 case CmpInst::FCMP_OLE:
8472 case CmpInst::FCMP_ULT:
8473 case CmpInst::FCMP_ULE:
8474 if (match(FalseVal, m_OrdOrUnordFMin(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8475 *FC1 < *FC2)
8476 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
8477 if (match(FalseVal, m_FMinNum(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8478 *FC1 < *FC2)
8479 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
8480 break;
8481 case CmpInst::FCMP_OGT:
8482 case CmpInst::FCMP_OGE:
8483 case CmpInst::FCMP_UGT:
8484 case CmpInst::FCMP_UGE:
8485 if (match(FalseVal, m_OrdOrUnordFMax(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8486 *FC1 > *FC2)
8487 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
8488 if (match(FalseVal, m_FMaxNum(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8489 *FC1 > *FC2)
8490 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
8491 break;
8492 default:
8493 break;
8494 }
8495
8496 return {SPF_UNKNOWN, SPNB_NA, false};
8497}
8498
8499/// Recognize variations of:
8500/// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
8502 Value *CmpLHS, Value *CmpRHS,
8503 Value *TrueVal, Value *FalseVal) {
8504 // Swap the select operands and predicate to match the patterns below.
8505 if (CmpRHS != TrueVal) {
8506 Pred = ICmpInst::getSwappedPredicate(Pred);
8507 std::swap(TrueVal, FalseVal);
8508 }
8509 const APInt *C1;
8510 if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) {
8511 const APInt *C2;
8512 // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
8513 if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8514 C1->slt(*C2) && Pred == CmpInst::ICMP_SLT)
8515 return {SPF_SMAX, SPNB_NA, false};
8516
8517 // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
8518 if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8519 C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT)
8520 return {SPF_SMIN, SPNB_NA, false};
8521
8522 // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
8523 if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8524 C1->ult(*C2) && Pred == CmpInst::ICMP_ULT)
8525 return {SPF_UMAX, SPNB_NA, false};
8526
8527 // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
8528 if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8529 C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT)
8530 return {SPF_UMIN, SPNB_NA, false};
8531 }
8532 return {SPF_UNKNOWN, SPNB_NA, false};
8533}
8534
8535/// Recognize variations of:
8536/// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c))
8538 Value *CmpLHS, Value *CmpRHS,
8539 Value *TVal, Value *FVal,
8540 unsigned Depth) {
8541 // TODO: Allow FP min/max with nnan/nsz.
8542 assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison");
8543
8544 Value *A = nullptr, *B = nullptr;
8545 SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1);
8546 if (!SelectPatternResult::isMinOrMax(L.Flavor))
8547 return {SPF_UNKNOWN, SPNB_NA, false};
8548
8549 Value *C = nullptr, *D = nullptr;
8550 SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1);
8551 if (L.Flavor != R.Flavor)
8552 return {SPF_UNKNOWN, SPNB_NA, false};
8553
8554 // We have something like: x Pred y ? min(a, b) : min(c, d).
8555 // Try to match the compare to the min/max operations of the select operands.
8556 // First, make sure we have the right compare predicate.
8557 switch (L.Flavor) {
8558 case SPF_SMIN:
8559 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) {
8560 Pred = ICmpInst::getSwappedPredicate(Pred);
8561 std::swap(CmpLHS, CmpRHS);
8562 }
8563 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
8564 break;
8565 return {SPF_UNKNOWN, SPNB_NA, false};
8566 case SPF_SMAX:
8567 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) {
8568 Pred = ICmpInst::getSwappedPredicate(Pred);
8569 std::swap(CmpLHS, CmpRHS);
8570 }
8571 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE)
8572 break;
8573 return {SPF_UNKNOWN, SPNB_NA, false};
8574 case SPF_UMIN:
8575 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
8576 Pred = ICmpInst::getSwappedPredicate(Pred);
8577 std::swap(CmpLHS, CmpRHS);
8578 }
8579 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE)
8580 break;
8581 return {SPF_UNKNOWN, SPNB_NA, false};
8582 case SPF_UMAX:
8583 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
8584 Pred = ICmpInst::getSwappedPredicate(Pred);
8585 std::swap(CmpLHS, CmpRHS);
8586 }
8587 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
8588 break;
8589 return {SPF_UNKNOWN, SPNB_NA, false};
8590 default:
8591 return {SPF_UNKNOWN, SPNB_NA, false};
8592 }
8593
8594 // If there is a common operand in the already matched min/max and the other
8595 // min/max operands match the compare operands (either directly or inverted),
8596 // then this is min/max of the same flavor.
8597
8598 // a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8599 // ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8600 if (D == B) {
8601 if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8602 match(A, m_Not(m_Specific(CmpRHS)))))
8603 return {L.Flavor, SPNB_NA, false};
8604 }
8605 // a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8606 // ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8607 if (C == B) {
8608 if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8609 match(A, m_Not(m_Specific(CmpRHS)))))
8610 return {L.Flavor, SPNB_NA, false};
8611 }
8612 // b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8613 // ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8614 if (D == A) {
8615 if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8616 match(B, m_Not(m_Specific(CmpRHS)))))
8617 return {L.Flavor, SPNB_NA, false};
8618 }
8619 // b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8620 // ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8621 if (C == A) {
8622 if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8623 match(B, m_Not(m_Specific(CmpRHS)))))
8624 return {L.Flavor, SPNB_NA, false};
8625 }
8626
8627 return {SPF_UNKNOWN, SPNB_NA, false};
8628}
8629
8630/// If the input value is the result of a 'not' op, constant integer, or vector
8631/// splat of a constant integer, return the bitwise-not source value.
8632/// TODO: This could be extended to handle non-splat vector integer constants.
8634 Value *NotV;
8635 if (match(V, m_Not(m_Value(NotV))))
8636 return NotV;
8637
8638 const APInt *C;
8639 if (match(V, m_APInt(C)))
8640 return ConstantInt::get(V->getType(), ~(*C));
8641
8642 return nullptr;
8643}
8644
8645/// Match non-obvious integer minimum and maximum sequences.
8647 Value *CmpLHS, Value *CmpRHS,
8648 Value *TrueVal, Value *FalseVal,
8649 Value *&LHS, Value *&RHS,
8650 unsigned Depth) {
8651 // Assume success. If there's no match, callers should not use these anyway.
8652 LHS = TrueVal;
8653 RHS = FalseVal;
8654
8655 SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);
8657 return SPR;
8658
8659 SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth);
8661 return SPR;
8662
8663 // Look through 'not' ops to find disguised min/max.
8664 // (X > Y) ? ~X : ~Y ==> (~X < ~Y) ? ~X : ~Y ==> MIN(~X, ~Y)
8665 // (X < Y) ? ~X : ~Y ==> (~X > ~Y) ? ~X : ~Y ==> MAX(~X, ~Y)
8666 if (CmpLHS == getNotValue(TrueVal) && CmpRHS == getNotValue(FalseVal)) {
8667 switch (Pred) {
8668 case CmpInst::ICMP_SGT: return {SPF_SMIN, SPNB_NA, false};
8669 case CmpInst::ICMP_SLT: return {SPF_SMAX, SPNB_NA, false};
8670 case CmpInst::ICMP_UGT: return {SPF_UMIN, SPNB_NA, false};
8671 case CmpInst::ICMP_ULT: return {SPF_UMAX, SPNB_NA, false};
8672 default: break;
8673 }
8674 }
8675
8676 // (X > Y) ? ~Y : ~X ==> (~X < ~Y) ? ~Y : ~X ==> MAX(~Y, ~X)
8677 // (X < Y) ? ~Y : ~X ==> (~X > ~Y) ? ~Y : ~X ==> MIN(~Y, ~X)
8678 if (CmpLHS == getNotValue(FalseVal) && CmpRHS == getNotValue(TrueVal)) {
8679 switch (Pred) {
8680 case CmpInst::ICMP_SGT: return {SPF_SMAX, SPNB_NA, false};
8681 case CmpInst::ICMP_SLT: return {SPF_SMIN, SPNB_NA, false};
8682 case CmpInst::ICMP_UGT: return {SPF_UMAX, SPNB_NA, false};
8683 case CmpInst::ICMP_ULT: return {SPF_UMIN, SPNB_NA, false};
8684 default: break;
8685 }
8686 }
8687
8688 if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT)
8689 return {SPF_UNKNOWN, SPNB_NA, false};
8690
8691 const APInt *C1;
8692 if (!match(CmpRHS, m_APInt(C1)))
8693 return {SPF_UNKNOWN, SPNB_NA, false};
8694
8695 // An unsigned min/max can be written with a signed compare.
8696 const APInt *C2;
8697 if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) ||
8698 (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) {
8699 // Is the sign bit set?
8700 // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX
8701 // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN
8702 if (Pred == CmpInst::ICMP_SLT && C1->isZero() && C2->isMaxSignedValue())
8703 return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8704
8705 // Is the sign bit clear?
8706 // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX
8707 // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN
8708 if (Pred == CmpInst::ICMP_SGT && C1->isAllOnes() && C2->isMinSignedValue())
8709 return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8710 }
8711
8712 return {SPF_UNKNOWN, SPNB_NA, false};
8713}
8714
8715bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW,
8716 bool AllowPoison) {
8717 assert(X && Y && "Invalid operand");
8718
8719 auto IsNegationOf = [&](const Value *X, const Value *Y) {
8720 if (!match(X, m_Neg(m_Specific(Y))))
8721 return false;
8722
8723 auto *BO = cast<BinaryOperator>(X);
8724 if (NeedNSW && !BO->hasNoSignedWrap())
8725 return false;
8726
8727 auto *Zero = cast<Constant>(BO->getOperand(0));
8728 if (!AllowPoison && !Zero->isNullValue())
8729 return false;
8730
8731 return true;
8732 };
8733
8734 // X = -Y or Y = -X
8735 if (IsNegationOf(X, Y) || IsNegationOf(Y, X))
8736 return true;
8737
8738 // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A)
8739 Value *A, *B;
8740 return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) &&
8741 match(Y, m_Sub(m_Specific(B), m_Specific(A))))) ||
8742 (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) &&
8744}
8745
8746bool llvm::isKnownInversion(const Value *X, const Value *Y) {
8747 // Handle X = icmp pred A, B, Y = icmp pred A, C.
8748 Value *A, *B, *C;
8749 CmpPredicate Pred1, Pred2;
8750 if (!match(X, m_ICmp(Pred1, m_Value(A), m_Value(B))) ||
8751 !match(Y, m_c_ICmp(Pred2, m_Specific(A), m_Value(C))))
8752 return false;
8753
8754 // They must both have samesign flag or not.
8755 if (Pred1.hasSameSign() != Pred2.hasSameSign())
8756 return false;
8757
8758 if (B == C)
8759 return Pred1 == ICmpInst::getInversePredicate(Pred2);
8760
8761 // Try to infer the relationship from constant ranges.
8762 const APInt *RHSC1, *RHSC2;
8763 if (!match(B, m_APInt(RHSC1)) || !match(C, m_APInt(RHSC2)))
8764 return false;
8765
8766 // Sign bits of two RHSCs should match.
8767 if (Pred1.hasSameSign() && RHSC1->isNonNegative() != RHSC2->isNonNegative())
8768 return false;
8769
8770 const auto CR1 = ConstantRange::makeExactICmpRegion(Pred1, *RHSC1);
8771 const auto CR2 = ConstantRange::makeExactICmpRegion(Pred2, *RHSC2);
8772
8773 return CR1.inverse() == CR2;
8774}
8775
8777 SelectPatternNaNBehavior NaNBehavior,
8778 bool Ordered) {
8779 switch (Pred) {
8780 default:
8781 return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.
8782 case ICmpInst::ICMP_UGT:
8783 case ICmpInst::ICMP_UGE:
8784 return {SPF_UMAX, SPNB_NA, false};
8785 case ICmpInst::ICMP_SGT:
8786 case ICmpInst::ICMP_SGE:
8787 return {SPF_SMAX, SPNB_NA, false};
8788 case ICmpInst::ICMP_ULT:
8789 case ICmpInst::ICMP_ULE:
8790 return {SPF_UMIN, SPNB_NA, false};
8791 case ICmpInst::ICMP_SLT:
8792 case ICmpInst::ICMP_SLE:
8793 return {SPF_SMIN, SPNB_NA, false};
8794 case FCmpInst::FCMP_UGT:
8795 case FCmpInst::FCMP_UGE:
8796 case FCmpInst::FCMP_OGT:
8797 case FCmpInst::FCMP_OGE:
8798 return {SPF_FMAXNUM, NaNBehavior, Ordered};
8799 case FCmpInst::FCMP_ULT:
8800 case FCmpInst::FCMP_ULE:
8801 case FCmpInst::FCMP_OLT:
8802 case FCmpInst::FCMP_OLE:
8803 return {SPF_FMINNUM, NaNBehavior, Ordered};
8804 }
8805}
8806
8807std::optional<std::pair<CmpPredicate, Constant *>>
8810 "Only for relational integer predicates.");
8811 if (isa<UndefValue>(C))
8812 return std::nullopt;
8813
8814 Type *Type = C->getType();
8815 bool IsSigned = ICmpInst::isSigned(Pred);
8816
8818 bool WillIncrement =
8819 UnsignedPred == ICmpInst::ICMP_ULE || UnsignedPred == ICmpInst::ICMP_UGT;
8820
8821 // Check if the constant operand can be safely incremented/decremented
8822 // without overflowing/underflowing.
8823 auto ConstantIsOk = [WillIncrement, IsSigned](ConstantInt *C) {
8824 return WillIncrement ? !C->isMaxValue(IsSigned) : !C->isMinValue(IsSigned);
8825 };
8826
8827 Constant *SafeReplacementConstant = nullptr;
8828 if (auto *CI = dyn_cast<ConstantInt>(C)) {
8829 // Bail out if the constant can't be safely incremented/decremented.
8830 if (!ConstantIsOk(CI))
8831 return std::nullopt;
8832 } else if (auto *FVTy = dyn_cast<FixedVectorType>(Type)) {
8833 unsigned NumElts = FVTy->getNumElements();
8834 for (unsigned i = 0; i != NumElts; ++i) {
8835 Constant *Elt = C->getAggregateElement(i);
8836 if (!Elt)
8837 return std::nullopt;
8838
8839 if (isa<UndefValue>(Elt))
8840 continue;
8841
8842 // Bail out if we can't determine if this constant is min/max or if we
8843 // know that this constant is min/max.
8844 auto *CI = dyn_cast<ConstantInt>(Elt);
8845 if (!CI || !ConstantIsOk(CI))
8846 return std::nullopt;
8847
8848 if (!SafeReplacementConstant)
8849 SafeReplacementConstant = CI;
8850 }
8851 } else if (isa<VectorType>(C->getType())) {
8852 // Handle scalable splat
8853 Value *SplatC = C->getSplatValue();
8854 auto *CI = dyn_cast_or_null<ConstantInt>(SplatC);
8855 // Bail out if the constant can't be safely incremented/decremented.
8856 if (!CI || !ConstantIsOk(CI))
8857 return std::nullopt;
8858 } else {
8859 // ConstantExpr?
8860 return std::nullopt;
8861 }
8862
8863 // It may not be safe to change a compare predicate in the presence of
8864 // undefined elements, so replace those elements with the first safe constant
8865 // that we found.
8866 // TODO: in case of poison, it is safe; let's replace undefs only.
8867 if (C->containsUndefOrPoisonElement()) {
8868 assert(SafeReplacementConstant && "Replacement constant not set");
8869 C = Constant::replaceUndefsWith(C, SafeReplacementConstant);
8870 }
8871
8873
8874 // Increment or decrement the constant.
8875 Constant *OneOrNegOne = ConstantInt::get(Type, WillIncrement ? 1 : -1, true);
8876 Constant *NewC = ConstantExpr::getAdd(C, OneOrNegOne);
8877
8878 return std::make_pair(NewPred, NewC);
8879}
8880
8882 FastMathFlags FMF,
8883 Value *CmpLHS, Value *CmpRHS,
8884 Value *TrueVal, Value *FalseVal,
8885 Value *&LHS, Value *&RHS,
8886 unsigned Depth) {
8887 bool HasMismatchedZeros = false;
8888 if (CmpInst::isFPPredicate(Pred)) {
8889 // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one
8890 // 0.0 operand, set the compare's 0.0 operands to that same value for the
8891 // purpose of identifying min/max. Disregard vector constants with undefined
8892 // elements because those can not be back-propagated for analysis.
8893 Value *OutputZeroVal = nullptr;
8894 if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) &&
8895 !cast<Constant>(TrueVal)->containsUndefOrPoisonElement())
8896 OutputZeroVal = TrueVal;
8897 else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) &&
8898 !cast<Constant>(FalseVal)->containsUndefOrPoisonElement())
8899 OutputZeroVal = FalseVal;
8900
8901 if (OutputZeroVal) {
8902 if (match(CmpLHS, m_AnyZeroFP()) && CmpLHS != OutputZeroVal) {
8903 HasMismatchedZeros = true;
8904 CmpLHS = OutputZeroVal;
8905 }
8906 if (match(CmpRHS, m_AnyZeroFP()) && CmpRHS != OutputZeroVal) {
8907 HasMismatchedZeros = true;
8908 CmpRHS = OutputZeroVal;
8909 }
8910 }
8911 }
8912
8913 LHS = CmpLHS;
8914 RHS = CmpRHS;
8915
8916 // Signed zero may return inconsistent results between implementations.
8917 // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0
8918 // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)
8919 // Therefore, we behave conservatively and only proceed if at least one of the
8920 // operands is known to not be zero or if we don't care about signed zero.
8921 switch (Pred) {
8922 default: break;
8925 if (!HasMismatchedZeros)
8926 break;
8927 [[fallthrough]];
8930 if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8931 !isKnownNonZero(CmpRHS))
8932 return {SPF_UNKNOWN, SPNB_NA, false};
8933 }
8934
8935 SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
8936 bool Ordered = false;
8937
8938 // When given one NaN and one non-NaN input:
8939 // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
8940 // - A simple C99 (a < b ? a : b) construction will return 'b' (as the
8941 // ordered comparison fails), which could be NaN or non-NaN.
8942 // so here we discover exactly what NaN behavior is required/accepted.
8943 if (CmpInst::isFPPredicate(Pred)) {
8944 bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
8945 bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
8946
8947 if (LHSSafe && RHSSafe) {
8948 // Both operands are known non-NaN.
8949 NaNBehavior = SPNB_RETURNS_ANY;
8950 Ordered = CmpInst::isOrdered(Pred);
8951 } else if (CmpInst::isOrdered(Pred)) {
8952 // An ordered comparison will return false when given a NaN, so it
8953 // returns the RHS.
8954 Ordered = true;
8955 if (LHSSafe)
8956 // LHS is non-NaN, so if RHS is NaN then NaN will be returned.
8957 NaNBehavior = SPNB_RETURNS_NAN;
8958 else if (RHSSafe)
8959 NaNBehavior = SPNB_RETURNS_OTHER;
8960 else
8961 // Completely unsafe.
8962 return {SPF_UNKNOWN, SPNB_NA, false};
8963 } else {
8964 Ordered = false;
8965 // An unordered comparison will return true when given a NaN, so it
8966 // returns the LHS.
8967 if (LHSSafe)
8968 // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
8969 NaNBehavior = SPNB_RETURNS_OTHER;
8970 else if (RHSSafe)
8971 NaNBehavior = SPNB_RETURNS_NAN;
8972 else
8973 // Completely unsafe.
8974 return {SPF_UNKNOWN, SPNB_NA, false};
8975 }
8976 }
8977
8978 if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
8979 std::swap(CmpLHS, CmpRHS);
8980 Pred = CmpInst::getSwappedPredicate(Pred);
8981 if (NaNBehavior == SPNB_RETURNS_NAN)
8982 NaNBehavior = SPNB_RETURNS_OTHER;
8983 else if (NaNBehavior == SPNB_RETURNS_OTHER)
8984 NaNBehavior = SPNB_RETURNS_NAN;
8985 Ordered = !Ordered;
8986 }
8987
8988 // ([if]cmp X, Y) ? X : Y
8989 if (TrueVal == CmpLHS && FalseVal == CmpRHS)
8990 return getSelectPattern(Pred, NaNBehavior, Ordered);
8991
8992 if (isKnownNegation(TrueVal, FalseVal)) {
8993 // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can
8994 // match against either LHS or sext(LHS).
8995 auto MaybeSExtCmpLHS =
8996 m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS)));
8997 auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes());
8998 auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One());
8999 if (match(TrueVal, MaybeSExtCmpLHS)) {
9000 // Set the return values. If the compare uses the negated value (-X >s 0),
9001 // swap the return values because the negated value is always 'RHS'.
9002 LHS = TrueVal;
9003 RHS = FalseVal;
9004 if (match(CmpLHS, m_Neg(m_Specific(FalseVal))))
9005 std::swap(LHS, RHS);
9006
9007 // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X)
9008 // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X)
9009 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
9010 return {SPF_ABS, SPNB_NA, false};
9011
9012 // (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X)
9013 if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne))
9014 return {SPF_ABS, SPNB_NA, false};
9015
9016 // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X)
9017 // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X)
9018 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
9019 return {SPF_NABS, SPNB_NA, false};
9020 }
9021 else if (match(FalseVal, MaybeSExtCmpLHS)) {
9022 // Set the return values. If the compare uses the negated value (-X >s 0),
9023 // swap the return values because the negated value is always 'RHS'.
9024 LHS = FalseVal;
9025 RHS = TrueVal;
9026 if (match(CmpLHS, m_Neg(m_Specific(TrueVal))))
9027 std::swap(LHS, RHS);
9028
9029 // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X)
9030 // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X)
9031 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
9032 return {SPF_NABS, SPNB_NA, false};
9033
9034 // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X)
9035 // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X)
9036 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
9037 return {SPF_ABS, SPNB_NA, false};
9038 }
9039 }
9040
9041 if (CmpInst::isIntPredicate(Pred))
9042 return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth);
9043
9044 // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar
9045 // may return either -0.0 or 0.0, so fcmp/select pair has stricter
9046 // semantics than minNum. Be conservative in such case.
9047 if (NaNBehavior != SPNB_RETURNS_ANY ||
9048 (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
9049 !isKnownNonZero(CmpRHS)))
9050 return {SPF_UNKNOWN, SPNB_NA, false};
9051
9052 return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
9053}
9054
9056 Instruction::CastOps *CastOp) {
9057 const DataLayout &DL = CmpI->getDataLayout();
9058
9059 Constant *CastedTo = nullptr;
9060 switch (*CastOp) {
9061 case Instruction::ZExt:
9062 if (CmpI->isUnsigned())
9063 CastedTo = ConstantExpr::getTrunc(C, SrcTy);
9064 break;
9065 case Instruction::SExt:
9066 if (CmpI->isSigned())
9067 CastedTo = ConstantExpr::getTrunc(C, SrcTy, true);
9068 break;
9069 case Instruction::Trunc:
9070 Constant *CmpConst;
9071 if (match(CmpI->getOperand(1), m_Constant(CmpConst)) &&
9072 CmpConst->getType() == SrcTy) {
9073 // Here we have the following case:
9074 //
9075 // %cond = cmp iN %x, CmpConst
9076 // %tr = trunc iN %x to iK
9077 // %narrowsel = select i1 %cond, iK %t, iK C
9078 //
9079 // We can always move trunc after select operation:
9080 //
9081 // %cond = cmp iN %x, CmpConst
9082 // %widesel = select i1 %cond, iN %x, iN CmpConst
9083 // %tr = trunc iN %widesel to iK
9084 //
9085 // Note that C could be extended in any way because we don't care about
9086 // upper bits after truncation. It can't be abs pattern, because it would
9087 // look like:
9088 //
9089 // select i1 %cond, x, -x.
9090 //
9091 // So only min/max pattern could be matched. Such match requires widened C
9092 // == CmpConst. That is why set widened C = CmpConst, condition trunc
9093 // CmpConst == C is checked below.
9094 CastedTo = CmpConst;
9095 } else {
9096 unsigned ExtOp = CmpI->isSigned() ? Instruction::SExt : Instruction::ZExt;
9097 CastedTo = ConstantFoldCastOperand(ExtOp, C, SrcTy, DL);
9098 }
9099 break;
9100 case Instruction::FPTrunc:
9101 CastedTo = ConstantFoldCastOperand(Instruction::FPExt, C, SrcTy, DL);
9102 break;
9103 case Instruction::FPExt:
9104 CastedTo = ConstantFoldCastOperand(Instruction::FPTrunc, C, SrcTy, DL);
9105 break;
9106 case Instruction::FPToUI:
9107 CastedTo = ConstantFoldCastOperand(Instruction::UIToFP, C, SrcTy, DL);
9108 break;
9109 case Instruction::FPToSI:
9110 CastedTo = ConstantFoldCastOperand(Instruction::SIToFP, C, SrcTy, DL);
9111 break;
9112 case Instruction::UIToFP:
9113 CastedTo = ConstantFoldCastOperand(Instruction::FPToUI, C, SrcTy, DL);
9114 break;
9115 case Instruction::SIToFP:
9116 CastedTo = ConstantFoldCastOperand(Instruction::FPToSI, C, SrcTy, DL);
9117 break;
9118 default:
9119 break;
9120 }
9121
9122 if (!CastedTo)
9123 return nullptr;
9124
9125 // Make sure the cast doesn't lose any information.
9126 Constant *CastedBack =
9127 ConstantFoldCastOperand(*CastOp, CastedTo, C->getType(), DL);
9128 if (CastedBack && CastedBack != C)
9129 return nullptr;
9130
9131 return CastedTo;
9132}
9133
9134/// Helps to match a select pattern in case of a type mismatch.
9135///
9136/// The function processes the case when type of true and false values of a
9137/// select instruction differs from type of the cmp instruction operands because
9138/// of a cast instruction. The function checks if it is legal to move the cast
9139/// operation after "select". If yes, it returns the new second value of
9140/// "select" (with the assumption that cast is moved):
9141/// 1. As operand of cast instruction when both values of "select" are same cast
9142/// instructions.
9143/// 2. As restored constant (by applying reverse cast operation) when the first
9144/// value of the "select" is a cast operation and the second value is a
9145/// constant. It is implemented in lookThroughCastConst().
9146/// 3. As one operand is cast instruction and the other is not. The operands in
9147/// sel(cmp) are in different type integer.
9148/// NOTE: We return only the new second value because the first value could be
9149/// accessed as operand of cast instruction.
9151 Instruction::CastOps *CastOp) {
9152 auto *Cast1 = dyn_cast<CastInst>(V1);
9153 if (!Cast1)
9154 return nullptr;
9155
9156 *CastOp = Cast1->getOpcode();
9157 Type *SrcTy = Cast1->getSrcTy();
9158 if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
9159 // If V1 and V2 are both the same cast from the same type, look through V1.
9160 if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
9161 return Cast2->getOperand(0);
9162 return nullptr;
9163 }
9164
9165 auto *C = dyn_cast<Constant>(V2);
9166 if (C)
9167 return lookThroughCastConst(CmpI, SrcTy, C, CastOp);
9168
9169 Value *CastedTo = nullptr;
9170 if (*CastOp == Instruction::Trunc) {
9171 if (match(CmpI->getOperand(1), m_ZExtOrSExt(m_Specific(V2)))) {
9172 // Here we have the following case:
9173 // %y_ext = sext iK %y to iN
9174 // %cond = cmp iN %x, %y_ext
9175 // %tr = trunc iN %x to iK
9176 // %narrowsel = select i1 %cond, iK %tr, iK %y
9177 //
9178 // We can always move trunc after select operation:
9179 // %y_ext = sext iK %y to iN
9180 // %cond = cmp iN %x, %y_ext
9181 // %widesel = select i1 %cond, iN %x, iN %y_ext
9182 // %tr = trunc iN %widesel to iK
9183 assert(V2->getType() == Cast1->getType() &&
9184 "V2 and Cast1 should be the same type.");
9185 CastedTo = CmpI->getOperand(1);
9186 }
9187 }
9188
9189 return CastedTo;
9190}
9192 Instruction::CastOps *CastOp,
9193 unsigned Depth) {
9195 return {SPF_UNKNOWN, SPNB_NA, false};
9196
9198 if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
9199
9200 CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
9201 if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};
9202
9203 Value *TrueVal = SI->getTrueValue();
9204 Value *FalseVal = SI->getFalseValue();
9205
9206 return llvm::matchDecomposedSelectPattern(CmpI, TrueVal, FalseVal, LHS, RHS,
9207 SI->getFastMathFlagsOrNone(),
9208 CastOp, Depth);
9209}
9210
9212 CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS,
9213 FastMathFlags FMF, Instruction::CastOps *CastOp, unsigned Depth) {
9214 CmpInst::Predicate Pred = CmpI->getPredicate();
9215 Value *CmpLHS = CmpI->getOperand(0);
9216 Value *CmpRHS = CmpI->getOperand(1);
9217 if (isa<FPMathOperator>(CmpI) && CmpI->hasNoNaNs())
9218 FMF.setNoNaNs();
9219
9220 // Bail out early.
9221 if (CmpI->isEquality())
9222 return {SPF_UNKNOWN, SPNB_NA, false};
9223
9224 // Deal with type mismatches.
9225 if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
9226 if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) {
9227 // If this is a potential fmin/fmax with a cast to integer, then ignore
9228 // -0.0 because there is no corresponding integer value.
9229 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9230 FMF.setNoSignedZeros();
9231 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9232 cast<CastInst>(TrueVal)->getOperand(0), C,
9233 LHS, RHS, Depth);
9234 }
9235 if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) {
9236 // If this is a potential fmin/fmax with a cast to integer, then ignore
9237 // -0.0 because there is no corresponding integer value.
9238 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9239 FMF.setNoSignedZeros();
9240 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9241 C, cast<CastInst>(FalseVal)->getOperand(0),
9242 LHS, RHS, Depth);
9243 }
9244 }
9245 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
9246 LHS, RHS, Depth);
9247}
9248
9250 if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT;
9251 if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT;
9252 if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT;
9253 if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT;
9254 if (SPF == SPF_FMINNUM)
9255 return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT;
9256 if (SPF == SPF_FMAXNUM)
9257 return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT;
9258 llvm_unreachable("unhandled!");
9259}
9260
9262 switch (SPF) {
9264 return Intrinsic::umin;
9266 return Intrinsic::umax;
9268 return Intrinsic::smin;
9270 return Intrinsic::smax;
9271 default:
9272 llvm_unreachable("Unexpected SPF");
9273 }
9274}
9275
9277 if (SPF == SPF_SMIN) return SPF_SMAX;
9278 if (SPF == SPF_UMIN) return SPF_UMAX;
9279 if (SPF == SPF_SMAX) return SPF_SMIN;
9280 if (SPF == SPF_UMAX) return SPF_UMIN;
9281 llvm_unreachable("unhandled!");
9282}
9283
9285 switch (MinMaxID) {
9286 case Intrinsic::smax: return Intrinsic::smin;
9287 case Intrinsic::smin: return Intrinsic::smax;
9288 case Intrinsic::umax: return Intrinsic::umin;
9289 case Intrinsic::umin: return Intrinsic::umax;
9290 // Please note that next four intrinsics may produce the same result for
9291 // original and inverted case even if X != Y due to NaN is handled specially.
9292 case Intrinsic::maximum: return Intrinsic::minimum;
9293 case Intrinsic::minimum: return Intrinsic::maximum;
9294 case Intrinsic::maxnum: return Intrinsic::minnum;
9295 case Intrinsic::minnum: return Intrinsic::maxnum;
9296 case Intrinsic::maximumnum:
9297 return Intrinsic::minimumnum;
9298 case Intrinsic::minimumnum:
9299 return Intrinsic::maximumnum;
9300 default: llvm_unreachable("Unexpected intrinsic");
9301 }
9302}
9303
9305 switch (SPF) {
9308 case SPF_UMAX: return APInt::getMaxValue(BitWidth);
9309 case SPF_UMIN: return APInt::getMinValue(BitWidth);
9310 default: llvm_unreachable("Unexpected flavor");
9311 }
9312}
9313
9314std::pair<Intrinsic::ID, bool>
9316 // Check if VL contains select instructions that can be folded into a min/max
9317 // vector intrinsic and return the intrinsic if it is possible.
9318 // TODO: Support floating point min/max.
9319 bool AllCmpSingleUse = true;
9320 SelectPatternResult SelectPattern;
9321 SelectPattern.Flavor = SPF_UNKNOWN;
9322 if (all_of(VL, [&SelectPattern, &AllCmpSingleUse](Value *I) {
9323 Value *LHS, *RHS;
9324 auto CurrentPattern = matchSelectPattern(I, LHS, RHS);
9325 if (!SelectPatternResult::isMinOrMax(CurrentPattern.Flavor))
9326 return false;
9327 if (SelectPattern.Flavor != SPF_UNKNOWN &&
9328 SelectPattern.Flavor != CurrentPattern.Flavor)
9329 return false;
9330 SelectPattern = CurrentPattern;
9331 AllCmpSingleUse &=
9333 return true;
9334 })) {
9335 switch (SelectPattern.Flavor) {
9336 case SPF_SMIN:
9337 return {Intrinsic::smin, AllCmpSingleUse};
9338 case SPF_UMIN:
9339 return {Intrinsic::umin, AllCmpSingleUse};
9340 case SPF_SMAX:
9341 return {Intrinsic::smax, AllCmpSingleUse};
9342 case SPF_UMAX:
9343 return {Intrinsic::umax, AllCmpSingleUse};
9344 case SPF_FMAXNUM:
9345 return {Intrinsic::maxnum, AllCmpSingleUse};
9346 case SPF_FMINNUM:
9347 return {Intrinsic::minnum, AllCmpSingleUse};
9348 default:
9349 llvm_unreachable("unexpected select pattern flavor");
9350 }
9351 }
9352 return {Intrinsic::not_intrinsic, false};
9353}
9354
9355template <typename InstTy>
9356static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst,
9357 Value *&Init, Value *&OtherOp) {
9358 // Handle the case of a simple two-predecessor recurrence PHI.
9359 // There's a lot more that could theoretically be done here, but
9360 // this is sufficient to catch some interesting cases.
9361 // TODO: Expand list -- gep, uadd.sat etc.
9362 if (PN->getNumIncomingValues() != 2)
9363 return false;
9364
9365 for (unsigned I = 0; I != 2; ++I) {
9366 if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
9367 Operation && Operation->getNumOperands() >= 2) {
9368 Value *LHS = Operation->getOperand(0);
9369 Value *RHS = Operation->getOperand(1);
9370 if (LHS != PN && RHS != PN)
9371 continue;
9372
9373 Inst = Operation;
9374 Init = PN->getIncomingValue(!I);
9375 OtherOp = (LHS == PN) ? RHS : LHS;
9376 return true;
9377 }
9378 }
9379 return false;
9380}
9381
9382template <typename InstTy>
9383static bool matchThreeInputRecurrence(const PHINode *PN, InstTy *&Inst,
9384 Value *&Init, Value *&OtherOp0,
9385 Value *&OtherOp1) {
9386 if (PN->getNumIncomingValues() != 2)
9387 return false;
9388
9389 for (unsigned I = 0; I != 2; ++I) {
9390 if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
9391 Operation && Operation->getNumOperands() >= 3) {
9392 Value *Op0 = Operation->getOperand(0);
9393 Value *Op1 = Operation->getOperand(1);
9394 Value *Op2 = Operation->getOperand(2);
9395
9396 if (Op0 != PN && Op1 != PN && Op2 != PN)
9397 continue;
9398
9399 Inst = Operation;
9400 Init = PN->getIncomingValue(!I);
9401 if (Op0 == PN) {
9402 OtherOp0 = Op1;
9403 OtherOp1 = Op2;
9404 } else if (Op1 == PN) {
9405 OtherOp0 = Op0;
9406 OtherOp1 = Op2;
9407 } else {
9408 OtherOp0 = Op0;
9409 OtherOp1 = Op1;
9410 }
9411 return true;
9412 }
9413 }
9414 return false;
9415}
9417 Value *&Start, Value *&Step) {
9418 // We try to match a recurrence of the form:
9419 // %iv = [Start, %entry], [%iv.next, %backedge]
9420 // %iv.next = binop %iv, Step
9421 // Or:
9422 // %iv = [Start, %entry], [%iv.next, %backedge]
9423 // %iv.next = binop Step, %iv
9424 return matchTwoInputRecurrence(P, BO, Start, Step);
9425}
9426
9428 Value *&Start, Value *&Step) {
9429 BinaryOperator *BO = nullptr;
9430 P = dyn_cast<PHINode>(I->getOperand(0));
9431 if (!P)
9432 P = dyn_cast<PHINode>(I->getOperand(1));
9433 return P && matchSimpleRecurrence(P, BO, Start, Step) && BO == I;
9434}
9435
9437 PHINode *&P, Value *&Init,
9438 Value *&OtherOp) {
9439 // Binary intrinsics only supported for now.
9440 if (I->arg_size() != 2 || I->getType() != I->getArgOperand(0)->getType() ||
9441 I->getType() != I->getArgOperand(1)->getType())
9442 return false;
9443
9444 IntrinsicInst *II = nullptr;
9445 P = dyn_cast<PHINode>(I->getArgOperand(0));
9446 if (!P)
9447 P = dyn_cast<PHINode>(I->getArgOperand(1));
9448
9449 return P && matchTwoInputRecurrence(P, II, Init, OtherOp) && II == I;
9450}
9451
9453 PHINode *&P, Value *&Init,
9454 Value *&OtherOp0,
9455 Value *&OtherOp1) {
9456 if (I->arg_size() != 3 || I->getType() != I->getArgOperand(0)->getType() ||
9457 I->getType() != I->getArgOperand(1)->getType() ||
9458 I->getType() != I->getArgOperand(2)->getType())
9459 return false;
9460 IntrinsicInst *II = nullptr;
9461 P = dyn_cast<PHINode>(I->getArgOperand(0));
9462 if (!P) {
9463 P = dyn_cast<PHINode>(I->getArgOperand(1));
9464 if (!P)
9465 P = dyn_cast<PHINode>(I->getArgOperand(2));
9466 }
9467 return P && matchThreeInputRecurrence(P, II, Init, OtherOp0, OtherOp1) &&
9468 II == I;
9469}
9470
9471/// Return true if "icmp Pred LHS RHS" is always true.
9473 const Value *RHS) {
9474 if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS)
9475 return true;
9476
9477 switch (Pred) {
9478 default:
9479 return false;
9480
9481 case CmpInst::ICMP_SLE: {
9482 const APInt *C;
9483
9484 // LHS s<= LHS +_{nsw} C if C >= 0
9485 // LHS s<= LHS | C if C >= 0
9486 if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C))) ||
9488 return !C->isNegative();
9489
9490 // LHS s<= smax(LHS, V) for any V
9492 return true;
9493
9494 // smin(RHS, V) s<= RHS for any V
9496 return true;
9497
9498 // Match A to (X +_{nsw} CA) and B to (X +_{nsw} CB)
9499 const Value *X;
9500 const APInt *CLHS, *CRHS;
9501 if (match(LHS, m_NSWAddLike(m_Value(X), m_APInt(CLHS))) &&
9503 return CLHS->sle(*CRHS);
9504
9505 return false;
9506 }
9507
9508 case CmpInst::ICMP_ULE: {
9509 // LHS u<= LHS +_{nuw} V for any V
9510 if (match(RHS, m_c_Add(m_Specific(LHS), m_Value())) &&
9512 return true;
9513
9514 // LHS u<= LHS | V for any V
9515 if (match(RHS, m_c_Or(m_Specific(LHS), m_Value())))
9516 return true;
9517
9518 // LHS u<= umax(LHS, V) for any V
9520 return true;
9521
9522 // RHS >> V u<= RHS for any V
9523 if (match(LHS, m_LShr(m_Specific(RHS), m_Value())))
9524 return true;
9525
9526 // RHS u/ C_ugt_1 u<= RHS
9527 const APInt *C;
9528 if (match(LHS, m_UDiv(m_Specific(RHS), m_APInt(C))) && C->ugt(1))
9529 return true;
9530
9531 // RHS & V u<= RHS for any V
9533 return true;
9534
9535 // umin(RHS, V) u<= RHS for any V
9537 return true;
9538
9539 // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
9540 const Value *X;
9541 const APInt *CLHS, *CRHS;
9542 if (match(LHS, m_NUWAddLike(m_Value(X), m_APInt(CLHS))) &&
9544 return CLHS->ule(*CRHS);
9545
9546 return false;
9547 }
9548 }
9549}
9550
9551/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
9552/// ALHS ARHS" is true. Otherwise, return std::nullopt.
9553static std::optional<bool>
9555 const Value *ARHS, const Value *BLHS, const Value *BRHS) {
9556 switch (Pred) {
9557 default:
9558 return std::nullopt;
9559
9560 case CmpInst::ICMP_SLT:
9561 case CmpInst::ICMP_SLE:
9562 if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS) &&
9564 return true;
9565 return std::nullopt;
9566
9567 case CmpInst::ICMP_SGT:
9568 case CmpInst::ICMP_SGE:
9569 if (isTruePredicate(CmpInst::ICMP_SLE, ALHS, BLHS) &&
9571 return true;
9572 return std::nullopt;
9573
9574 case CmpInst::ICMP_ULT:
9575 case CmpInst::ICMP_ULE:
9576 if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS) &&
9578 return true;
9579 return std::nullopt;
9580
9581 case CmpInst::ICMP_UGT:
9582 case CmpInst::ICMP_UGE:
9583 if (isTruePredicate(CmpInst::ICMP_ULE, ALHS, BLHS) &&
9585 return true;
9586 return std::nullopt;
9587 }
9588}
9589
9590/// Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
9591/// Return false if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is false.
9592/// Otherwise, return std::nullopt if we can't infer anything.
9593static std::optional<bool>
9595 CmpPredicate RPred, const ConstantRange &RCR) {
9596 auto CRImpliesPred = [&](ConstantRange CR,
9597 CmpInst::Predicate Pred) -> std::optional<bool> {
9598 // If all true values for lhs and true for rhs, lhs implies rhs
9599 if (CR.icmp(Pred, RCR))
9600 return true;
9601
9602 // If there is no overlap, lhs implies not rhs
9603 if (CR.icmp(CmpInst::getInversePredicate(Pred), RCR))
9604 return false;
9605
9606 return std::nullopt;
9607 };
9608 if (auto Res = CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9609 RPred))
9610 return Res;
9611 if (LPred.hasSameSign() ^ RPred.hasSameSign()) {
9613 : LPred.dropSameSign();
9615 : RPred.dropSameSign();
9616 return CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9617 RPred);
9618 }
9619 return std::nullopt;
9620}
9621
9622/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
9623/// is true. Return false if LHS implies RHS is false. Otherwise, return
9624/// std::nullopt if we can't infer anything.
9625static std::optional<bool>
9626isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1,
9627 CmpPredicate RPred, const Value *R0, const Value *R1,
9628 const DataLayout &DL, bool LHSIsTrue) {
9629 // The rest of the logic assumes the LHS condition is true. If that's not the
9630 // case, invert the predicate to make it so.
9631 if (!LHSIsTrue)
9632 LPred = ICmpInst::getInverseCmpPredicate(LPred);
9633
9634 // We can have non-canonical operands, so try to normalize any common operand
9635 // to L0/R0.
9636 if (L0 == R1) {
9637 std::swap(R0, R1);
9638 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9639 }
9640 if (R0 == L1) {
9641 std::swap(L0, L1);
9642 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9643 }
9644 if (L1 == R1) {
9645 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
9646 if (L0 != R0 || match(L0, m_ImmConstant())) {
9647 std::swap(L0, L1);
9648 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9649 std::swap(R0, R1);
9650 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9651 }
9652 }
9653
9654 // See if we can infer anything if operand-0 matches and we have at least one
9655 // constant.
9656 const APInt *Unused;
9657 if (L0 == R0 && (match(L1, m_APInt(Unused)) || match(R1, m_APInt(Unused)))) {
9658 // Potential TODO: We could also further use the constant range of L0/R0 to
9659 // further constraint the constant ranges. At the moment this leads to
9660 // several regressions related to not transforming `multi_use(A + C0) eq/ne
9661 // C1` (see discussion: D58633).
9662 SimplifyQuery SQ(DL);
9667
9668 // Even if L1/R1 are not both constant, we can still sometimes deduce
9669 // relationship from a single constant. For example X u> Y implies X != 0.
9670 if (auto R = isImpliedCondCommonOperandWithCR(LPred, LCR, RPred, RCR))
9671 return R;
9672 // If both L1/R1 were exact constant ranges and we didn't get anything
9673 // here, we won't be able to deduce this.
9674 if (match(L1, m_APInt(Unused)) && match(R1, m_APInt(Unused)))
9675 return std::nullopt;
9676 }
9677
9678 // Can we infer anything when the two compares have matching operands?
9679 if (L0 == R0 && L1 == R1)
9680 return ICmpInst::isImpliedByMatchingCmp(LPred, RPred);
9681
9682 // It only really makes sense in the context of signed comparison for "X - Y
9683 // must be positive if X >= Y and no overflow".
9684 // Take SGT as an example: L0:x > L1:y and C >= 0
9685 // ==> R0:(x -nsw y) < R1:(-C) is false
9686 CmpInst::Predicate SignedLPred = LPred.getPreferredSignedPredicate();
9687 if ((SignedLPred == ICmpInst::ICMP_SGT ||
9688 SignedLPred == ICmpInst::ICMP_SGE) &&
9689 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9690 if (match(R1, m_NonPositive()) &&
9691 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == false)
9692 return false;
9693 }
9694
9695 // Take SLT as an example: L0:x < L1:y and C <= 0
9696 // ==> R0:(x -nsw y) < R1:(-C) is true
9697 if ((SignedLPred == ICmpInst::ICMP_SLT ||
9698 SignedLPred == ICmpInst::ICMP_SLE) &&
9699 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9700 if (match(R1, m_NonNegative()) &&
9701 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == true)
9702 return true;
9703 }
9704
9705 // a - b == NonZero -> a != b
9706 // ptrtoint(a) - ptrtoint(b) == NonZero -> a != b
9707 const APInt *L1C;
9708 Value *A, *B;
9709 if (LPred == ICmpInst::ICMP_EQ && ICmpInst::isEquality(RPred) &&
9710 match(L1, m_APInt(L1C)) && !L1C->isZero() &&
9711 match(L0, m_Sub(m_Value(A), m_Value(B))) &&
9712 ((A == R0 && B == R1) || (A == R1 && B == R0) ||
9717 return RPred.dropSameSign() == ICmpInst::ICMP_NE;
9718 }
9719
9720 // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1
9721 if (L0 == R0 &&
9722 (LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) &&
9723 (RPred == ICmpInst::ICMP_ULT || RPred == ICmpInst::ICMP_UGE) &&
9724 match(L0, m_c_Add(m_Specific(L1), m_Specific(R1))))
9725 return CmpPredicate::getMatching(LPred, RPred).has_value();
9726
9727 if (auto P = CmpPredicate::getMatching(LPred, RPred))
9728 return isImpliedCondOperands(*P, L0, L1, R0, R1);
9729
9730 return std::nullopt;
9731}
9732
9733/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
9734/// is true. Return false if LHS implies RHS is false. Otherwise, return
9735/// std::nullopt if we can't infer anything.
9736static std::optional<bool>
9738 FCmpInst::Predicate RPred, const Value *R0, const Value *R1,
9739 const DataLayout &DL, bool LHSIsTrue) {
9740 // The rest of the logic assumes the LHS condition is true. If that's not the
9741 // case, invert the predicate to make it so.
9742 if (!LHSIsTrue)
9743 LPred = FCmpInst::getInversePredicate(LPred);
9744
9745 // We can have non-canonical operands, so try to normalize any common operand
9746 // to L0/R0.
9747 if (L0 == R1) {
9748 std::swap(R0, R1);
9749 RPred = FCmpInst::getSwappedPredicate(RPred);
9750 }
9751 if (R0 == L1) {
9752 std::swap(L0, L1);
9753 LPred = FCmpInst::getSwappedPredicate(LPred);
9754 }
9755 if (L1 == R1) {
9756 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
9757 if (L0 != R0 || match(L0, m_ImmConstant())) {
9758 std::swap(L0, L1);
9759 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9760 std::swap(R0, R1);
9761 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9762 }
9763 }
9764
9765 // Can we infer anything when the two compares have matching operands?
9766 if (L0 == R0 && L1 == R1) {
9767 if ((LPred & RPred) == LPred)
9768 return true;
9769 if ((LPred & ~RPred) == LPred)
9770 return false;
9771 }
9772
9773 // See if we can infer anything if operand-0 matches and we have at least one
9774 // constant.
9775 const APFloat *L1C, *R1C;
9776 if (L0 == R0 && match(L1, m_APFloat(L1C)) && match(R1, m_APFloat(R1C))) {
9777 if (std::optional<ConstantFPRange> DomCR =
9779 if (std::optional<ConstantFPRange> ImpliedCR =
9781 if (ImpliedCR->contains(*DomCR))
9782 return true;
9783 }
9784 if (std::optional<ConstantFPRange> ImpliedCR =
9786 FCmpInst::getInversePredicate(RPred), *R1C)) {
9787 if (ImpliedCR->contains(*DomCR))
9788 return false;
9789 }
9790 }
9791 }
9792
9793 return std::nullopt;
9794}
9795
9796/// Return true if LHS implies RHS is true. Return false if LHS implies RHS is
9797/// false. Otherwise, return std::nullopt if we can't infer anything. We
9798/// expect the RHS to be an icmp and the LHS to be an 'and', 'or', or a 'select'
9799/// instruction.
9800static std::optional<bool>
9802 const Value *RHSOp0, const Value *RHSOp1,
9803 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9804 // The LHS must be an 'or', 'and', or a 'select' instruction.
9805 assert((LHS->getOpcode() == Instruction::And ||
9806 LHS->getOpcode() == Instruction::Or ||
9807 LHS->getOpcode() == Instruction::Select) &&
9808 "Expected LHS to be 'and', 'or', or 'select'.");
9809
9810 assert(Depth <= MaxAnalysisRecursionDepth && "Hit recursion limit");
9811
9812 // If the result of an 'or' is false, then we know both legs of the 'or' are
9813 // false. Similarly, if the result of an 'and' is true, then we know both
9814 // legs of the 'and' are true.
9815 const Value *ALHS, *ARHS;
9816 if ((!LHSIsTrue && match(LHS, m_LogicalOr(m_Value(ALHS), m_Value(ARHS)))) ||
9817 (LHSIsTrue && match(LHS, m_LogicalAnd(m_Value(ALHS), m_Value(ARHS))))) {
9818 // FIXME: Make this non-recursion.
9819 if (std::optional<bool> Implication = isImpliedCondition(
9820 ALHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9821 return Implication;
9822 if (std::optional<bool> Implication = isImpliedCondition(
9823 ARHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9824 return Implication;
9825 return std::nullopt;
9826 }
9827 return std::nullopt;
9828}
9829
9830std::optional<bool>
9832 const Value *RHSOp0, const Value *RHSOp1,
9833 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9834 // Bail out when we hit the limit.
9836 return std::nullopt;
9837
9838 // A mismatch occurs when we compare a scalar cmp to a vector cmp, for
9839 // example.
9840 if (RHSOp0->getType()->isVectorTy() != LHS->getType()->isVectorTy())
9841 return std::nullopt;
9842
9843 assert(LHS->getType()->isIntOrIntVectorTy(1) &&
9844 "Expected integer type only!");
9845
9846 // Match not
9847 if (match(LHS, m_Not(m_Value(LHS))))
9848 LHSIsTrue = !LHSIsTrue;
9849
9850 // Both LHS and RHS are icmps.
9851 if (RHSOp0->getType()->getScalarType()->isIntOrPtrTy()) {
9852 CmpPredicate LHSPred;
9853 Value *LHSOp0, *LHSOp1;
9854 if (match(LHS, m_ICmpLike(LHSPred, m_Value(LHSOp0), m_Value(LHSOp1))))
9855 return isImpliedCondICmps(LHSPred, LHSOp0, LHSOp1, RHSPred, RHSOp0,
9856 RHSOp1, DL, LHSIsTrue);
9857 } else {
9858 assert(RHSOp0->getType()->isFPOrFPVectorTy() &&
9859 "Expected floating point type only!");
9860 if (const auto *LHSCmp = dyn_cast<FCmpInst>(LHS))
9861 return isImpliedCondFCmps(LHSCmp->getPredicate(), LHSCmp->getOperand(0),
9862 LHSCmp->getOperand(1), RHSPred, RHSOp0, RHSOp1,
9863 DL, LHSIsTrue);
9864 }
9865
9866 /// The LHS should be an 'or', 'and', or a 'select' instruction. We expect
9867 /// the RHS to be an icmp.
9868 /// FIXME: Add support for and/or/select on the RHS.
9869 if (const Instruction *LHSI = dyn_cast<Instruction>(LHS)) {
9870 if ((LHSI->getOpcode() == Instruction::And ||
9871 LHSI->getOpcode() == Instruction::Or ||
9872 LHSI->getOpcode() == Instruction::Select))
9873 return isImpliedCondAndOr(LHSI, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue,
9874 Depth);
9875 }
9876 return std::nullopt;
9877}
9878
9879std::optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS,
9880 const DataLayout &DL,
9881 bool LHSIsTrue, unsigned Depth) {
9882 // LHS ==> RHS by definition
9883 if (LHS == RHS)
9884 return LHSIsTrue;
9885
9886 // Match not
9887 bool InvertRHS = false;
9888 if (match(RHS, m_Not(m_Value(RHS)))) {
9889 if (LHS == RHS)
9890 return !LHSIsTrue;
9891 InvertRHS = true;
9892 }
9893
9894 CmpPredicate RHSPred;
9895 Value *RHSOp0, *RHSOp1;
9896 if (match(RHS, m_ICmpLike(RHSPred, m_Value(RHSOp0), m_Value(RHSOp1)))) {
9897 if (auto Implied = isImpliedCondition(LHS, RHSPred, RHSOp0, RHSOp1, DL,
9898 LHSIsTrue, Depth))
9899 return InvertRHS ? !*Implied : *Implied;
9900 return std::nullopt;
9901 }
9902 if (const FCmpInst *RHSCmp = dyn_cast<FCmpInst>(RHS)) {
9903 if (auto Implied = isImpliedCondition(
9904 LHS, RHSCmp->getPredicate(), RHSCmp->getOperand(0),
9905 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))
9906 return InvertRHS ? !*Implied : *Implied;
9907 return std::nullopt;
9908 }
9909
9911 return std::nullopt;
9912
9913 // LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS2
9914 // LHS ==> !(RHS1 && RHS2) if LHS ==> !RHS1 or LHS ==> !RHS2
9915 const Value *RHS1, *RHS2;
9916 if (match(RHS, m_LogicalOr(m_Value(RHS1), m_Value(RHS2)))) {
9917 if (std::optional<bool> Imp =
9918 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9919 if (*Imp == true)
9920 return !InvertRHS;
9921 if (std::optional<bool> Imp =
9922 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9923 if (*Imp == true)
9924 return !InvertRHS;
9925 }
9926 if (match(RHS, m_LogicalAnd(m_Value(RHS1), m_Value(RHS2)))) {
9927 if (std::optional<bool> Imp =
9928 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9929 if (*Imp == false)
9930 return InvertRHS;
9931 if (std::optional<bool> Imp =
9932 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9933 if (*Imp == false)
9934 return InvertRHS;
9935 }
9936
9937 return std::nullopt;
9938}
9939
9940// Returns a pair (Condition, ConditionIsTrue), where Condition is a branch
9941// condition dominating ContextI or nullptr, if no condition is found.
9942static std::pair<Value *, bool>
9944 if (!ContextI || !ContextI->getParent())
9945 return {nullptr, false};
9946
9947 // TODO: This is a poor/cheap way to determine dominance. Should we use a
9948 // dominator tree (eg, from a SimplifyQuery) instead?
9949 const BasicBlock *ContextBB = ContextI->getParent();
9950 const BasicBlock *PredBB = ContextBB->getSinglePredecessor();
9951 if (!PredBB)
9952 return {nullptr, false};
9953
9954 // We need a conditional branch in the predecessor.
9955 Value *PredCond;
9956 BasicBlock *TrueBB, *FalseBB;
9957 if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))
9958 return {nullptr, false};
9959
9960 // The branch should get simplified. Don't bother simplifying this condition.
9961 if (TrueBB == FalseBB)
9962 return {nullptr, false};
9963
9964 assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
9965 "Predecessor block does not point to successor?");
9966
9967 // Is this condition implied by the predecessor condition?
9968 return {PredCond, TrueBB == ContextBB};
9969}
9970
9971std::optional<bool> llvm::isImpliedByDomCondition(const Value *Cond,
9972 const Instruction *ContextI,
9973 const DataLayout &DL) {
9974 assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool");
9975 auto PredCond = getDomPredecessorCondition(ContextI);
9976 if (PredCond.first)
9977 return isImpliedCondition(PredCond.first, Cond, DL, PredCond.second);
9978 return std::nullopt;
9979}
9980
9982 const Value *LHS,
9983 const Value *RHS,
9984 const Instruction *ContextI,
9985 const DataLayout &DL) {
9986 auto PredCond = getDomPredecessorCondition(ContextI);
9987 if (PredCond.first)
9988 return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL,
9989 PredCond.second);
9990 return std::nullopt;
9991}
9992
9994 APInt &Upper, const InstrInfoQuery &IIQ,
9995 bool PreferSignedRange) {
9996 unsigned Width = Lower.getBitWidth();
9997 const APInt *C;
9998 switch (BO.getOpcode()) {
9999 case Instruction::Sub:
10000 if (match(BO.getOperand(0), m_APInt(C))) {
10001 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
10002 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
10003
10004 // If the caller expects a signed compare, then try to use a signed range.
10005 // Otherwise if both no-wraps are set, use the unsigned range because it
10006 // is never larger than the signed range. Example:
10007 // "sub nuw nsw i8 -2, x" is unsigned [0, 254] vs. signed [-128, 126].
10008 // "sub nuw nsw i8 2, x" is unsigned [0, 2] vs. signed [-125, 127].
10009 if (PreferSignedRange && HasNSW && HasNUW)
10010 HasNUW = false;
10011
10012 if (HasNUW) {
10013 // 'sub nuw c, x' produces [0, C].
10014 Upper = *C + 1;
10015 } else if (HasNSW) {
10016 if (C->isNegative()) {
10017 // 'sub nsw -C, x' produces [SINT_MIN, -C - SINT_MIN].
10019 Upper = *C - APInt::getSignedMaxValue(Width);
10020 } else {
10021 // Note that sub 0, INT_MIN is not NSW. It techically is a signed wrap
10022 // 'sub nsw C, x' produces [C - SINT_MAX, SINT_MAX].
10023 Lower = *C - APInt::getSignedMaxValue(Width);
10025 }
10026 }
10027 }
10028 break;
10029 case Instruction::Add:
10030 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
10031 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
10032 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
10033
10034 // If the caller expects a signed compare, then try to use a signed
10035 // range. Otherwise if both no-wraps are set, use the unsigned range
10036 // because it is never larger than the signed range. Example: "add nuw
10037 // nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125].
10038 if (PreferSignedRange && HasNSW && HasNUW)
10039 HasNUW = false;
10040
10041 if (HasNUW) {
10042 // 'add nuw x, C' produces [C, UINT_MAX].
10043 Lower = *C;
10044 } else if (HasNSW) {
10045 if (C->isNegative()) {
10046 // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C].
10048 Upper = APInt::getSignedMaxValue(Width) + *C + 1;
10049 } else {
10050 // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX].
10051 Lower = APInt::getSignedMinValue(Width) + *C;
10052 Upper = APInt::getSignedMaxValue(Width) + 1;
10053 }
10054 }
10055 }
10056 break;
10057
10058 case Instruction::And:
10059 if (match(BO.getOperand(1), m_APInt(C)))
10060 // 'and x, C' produces [0, C].
10061 Upper = *C + 1;
10062 // X & -X is a power of two or zero. So we can cap the value at max power of
10063 // two.
10064 if (match(BO.getOperand(0), m_Neg(m_Specific(BO.getOperand(1)))) ||
10065 match(BO.getOperand(1), m_Neg(m_Specific(BO.getOperand(0)))))
10066 Upper = APInt::getSignedMinValue(Width) + 1;
10067 break;
10068
10069 case Instruction::Or:
10070 if (match(BO.getOperand(1), m_APInt(C)))
10071 // 'or x, C' produces [C, UINT_MAX].
10072 Lower = *C;
10073 break;
10074
10075 case Instruction::AShr:
10076 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10077 // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C].
10079 Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1;
10080 } else if (match(BO.getOperand(0), m_APInt(C))) {
10081 unsigned ShiftAmount = Width - 1;
10082 if (!C->isZero() && IIQ.isExact(&BO))
10083 ShiftAmount = C->countr_zero();
10084 if (C->isNegative()) {
10085 // 'ashr C, x' produces [C, C >> (Width-1)]
10086 Lower = *C;
10087 Upper = C->ashr(ShiftAmount) + 1;
10088 } else {
10089 // 'ashr C, x' produces [C >> (Width-1), C]
10090 Lower = C->ashr(ShiftAmount);
10091 Upper = *C + 1;
10092 }
10093 }
10094 break;
10095
10096 case Instruction::LShr:
10097 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10098 // 'lshr x, C' produces [0, UINT_MAX >> C].
10099 Upper = APInt::getAllOnes(Width).lshr(*C) + 1;
10100 } else if (match(BO.getOperand(0), m_APInt(C))) {
10101 // 'lshr C, x' produces [C >> (Width-1), C].
10102 unsigned ShiftAmount = Width - 1;
10103 if (!C->isZero() && IIQ.isExact(&BO))
10104 ShiftAmount = C->countr_zero();
10105 Lower = C->lshr(ShiftAmount);
10106 Upper = *C + 1;
10107 }
10108 break;
10109
10110 case Instruction::Shl:
10111 if (match(BO.getOperand(0), m_APInt(C))) {
10112 if (IIQ.hasNoUnsignedWrap(&BO)) {
10113 // 'shl nuw C, x' produces [C, C << CLZ(C)]
10114 Lower = *C;
10115 Upper = Lower.shl(Lower.countl_zero()) + 1;
10116 } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw?
10117 if (C->isNegative()) {
10118 // 'shl nsw C, x' produces [C << CLO(C)-1, C]
10119 unsigned ShiftAmount = C->countl_one() - 1;
10120 Lower = C->shl(ShiftAmount);
10121 Upper = *C + 1;
10122 } else {
10123 // 'shl nsw C, x' produces [C, C << CLZ(C)-1]
10124 unsigned ShiftAmount = C->countl_zero() - 1;
10125 Lower = *C;
10126 Upper = C->shl(ShiftAmount) + 1;
10127 }
10128 } else {
10129 // If lowbit is set, value can never be zero.
10130 if ((*C)[0])
10131 Lower = APInt::getOneBitSet(Width, 0);
10132 // If we are shifting a constant the largest it can be is if the longest
10133 // sequence of consecutive ones is shifted to the highbits (breaking
10134 // ties for which sequence is higher). At the moment we take a liberal
10135 // upper bound on this by just popcounting the constant.
10136 // TODO: There may be a bitwise trick for it longest/highest
10137 // consecutative sequence of ones (naive method is O(Width) loop).
10138 Upper = APInt::getHighBitsSet(Width, C->popcount()) + 1;
10139 }
10140 } else if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10141 Upper = APInt::getBitsSetFrom(Width, C->getZExtValue()) + 1;
10142 }
10143 break;
10144
10145 case Instruction::SDiv:
10146 if (match(BO.getOperand(1), m_APInt(C))) {
10147 APInt IntMin = APInt::getSignedMinValue(Width);
10148 APInt IntMax = APInt::getSignedMaxValue(Width);
10149 if (C->isAllOnes()) {
10150 // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX]
10151 // where C != -1 and C != 0 and C != 1
10152 Lower = IntMin + 1;
10153 Upper = IntMax + 1;
10154 } else if (C->countl_zero() < Width - 1) {
10155 // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C]
10156 // where C != -1 and C != 0 and C != 1
10157 Lower = IntMin.sdiv(*C);
10158 Upper = IntMax.sdiv(*C);
10159 if (Lower.sgt(Upper))
10161 Upper = Upper + 1;
10162 assert(Upper != Lower && "Upper part of range has wrapped!");
10163 }
10164 } else if (match(BO.getOperand(0), m_APInt(C))) {
10165 if (C->isMinSignedValue()) {
10166 // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2].
10167 Lower = *C;
10168 Upper = Lower.lshr(1) + 1;
10169 } else {
10170 // 'sdiv C, x' produces [-|C|, |C|].
10171 Upper = C->abs() + 1;
10172 Lower = (-Upper) + 1;
10173 }
10174 }
10175 break;
10176
10177 case Instruction::UDiv:
10178 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
10179 // 'udiv x, C' produces [0, UINT_MAX / C].
10180 Upper = APInt::getMaxValue(Width).udiv(*C) + 1;
10181 } else if (match(BO.getOperand(0), m_APInt(C))) {
10182 // 'udiv C, x' produces [0, C].
10183 Upper = *C + 1;
10184 }
10185 break;
10186
10187 case Instruction::SRem:
10188 if (match(BO.getOperand(1), m_APInt(C))) {
10189 // 'srem x, C' produces (-|C|, |C|).
10190 Upper = C->abs();
10191 Lower = (-Upper) + 1;
10192 } else if (match(BO.getOperand(0), m_APInt(C))) {
10193 if (C->isNegative()) {
10194 // 'srem -|C|, x' produces [-|C|, 0].
10195 Upper = 1;
10196 Lower = *C;
10197 } else {
10198 // 'srem |C|, x' produces [0, |C|].
10199 Upper = *C + 1;
10200 }
10201 }
10202 break;
10203
10204 case Instruction::URem:
10205 if (match(BO.getOperand(1), m_APInt(C)))
10206 // 'urem x, C' produces [0, C).
10207 Upper = *C;
10208 else if (match(BO.getOperand(0), m_APInt(C)))
10209 // 'urem C, x' produces [0, C].
10210 Upper = *C + 1;
10211 break;
10212
10213 default:
10214 break;
10215 }
10216}
10217
10219 bool UseInstrInfo) {
10220 unsigned Width = II.getType()->getScalarSizeInBits();
10221 const APInt *C;
10222 switch (II.getIntrinsicID()) {
10223 case Intrinsic::ctlz:
10224 case Intrinsic::cttz: {
10225 APInt Upper(Width, Width);
10226 if (!UseInstrInfo || !match(II.getArgOperand(1), m_One()))
10227 Upper += 1;
10228 // Maximum of set/clear bits is the bit width.
10230 }
10231 case Intrinsic::ctpop:
10232 // Maximum of set/clear bits is the bit width.
10234 APInt(Width, Width) + 1);
10235 case Intrinsic::uadd_sat:
10236 // uadd.sat(x, C) produces [C, UINT_MAX].
10237 if (match(II.getOperand(0), m_APInt(C)) ||
10238 match(II.getOperand(1), m_APInt(C)))
10240 break;
10241 case Intrinsic::sadd_sat:
10242 if (match(II.getOperand(0), m_APInt(C)) ||
10243 match(II.getOperand(1), m_APInt(C))) {
10244 if (C->isNegative())
10245 // sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)].
10247 APInt::getSignedMaxValue(Width) + *C +
10248 1);
10249
10250 // sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].
10252 APInt::getSignedMaxValue(Width) + 1);
10253 }
10254 break;
10255 case Intrinsic::usub_sat:
10256 // usub.sat(C, x) produces [0, C].
10257 if (match(II.getOperand(0), m_APInt(C)))
10258 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
10259
10260 // usub.sat(x, C) produces [0, UINT_MAX - C].
10261 if (match(II.getOperand(1), m_APInt(C)))
10263 APInt::getMaxValue(Width) - *C + 1);
10264 break;
10265 case Intrinsic::ssub_sat:
10266 if (match(II.getOperand(0), m_APInt(C))) {
10267 if (C->isNegative())
10268 // ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)].
10270 *C - APInt::getSignedMinValue(Width) +
10271 1);
10272
10273 // ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].
10275 APInt::getSignedMaxValue(Width) + 1);
10276 } else if (match(II.getOperand(1), m_APInt(C))) {
10277 if (C->isNegative())
10278 // ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]:
10280 APInt::getSignedMaxValue(Width) + 1);
10281
10282 // ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].
10284 APInt::getSignedMaxValue(Width) - *C +
10285 1);
10286 }
10287 break;
10288 case Intrinsic::umin:
10289 case Intrinsic::umax:
10290 case Intrinsic::smin:
10291 case Intrinsic::smax:
10292 if (!match(II.getOperand(0), m_APInt(C)) &&
10293 !match(II.getOperand(1), m_APInt(C)))
10294 break;
10295
10296 switch (II.getIntrinsicID()) {
10297 case Intrinsic::umin:
10298 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
10299 case Intrinsic::umax:
10301 case Intrinsic::smin:
10303 *C + 1);
10304 case Intrinsic::smax:
10306 APInt::getSignedMaxValue(Width) + 1);
10307 default:
10308 llvm_unreachable("Must be min/max intrinsic");
10309 }
10310 break;
10311 case Intrinsic::abs:
10312 // If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX],
10313 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10314 if (match(II.getOperand(1), m_One()))
10316 APInt::getSignedMaxValue(Width) + 1);
10317
10319 APInt::getSignedMinValue(Width) + 1);
10320 case Intrinsic::vscale:
10321 if (!II.getParent() || !II.getFunction())
10322 break;
10323 return getVScaleRange(II.getFunction(), Width);
10324 default:
10325 break;
10326 }
10327
10328 return ConstantRange::getFull(Width);
10329}
10330
10332 const InstrInfoQuery &IIQ) {
10333 unsigned BitWidth = SI.getType()->getScalarSizeInBits();
10334 const Value *LHS = nullptr, *RHS = nullptr;
10336 if (R.Flavor == SPF_UNKNOWN)
10337 return ConstantRange::getFull(BitWidth);
10338
10339 if (R.Flavor == SelectPatternFlavor::SPF_ABS) {
10340 // If the negation part of the abs (in RHS) has the NSW flag,
10341 // then the result of abs(X) is [0..SIGNED_MAX],
10342 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10343 if (match(RHS, m_Neg(m_Specific(LHS))) &&
10347
10350 }
10351
10352 if (R.Flavor == SelectPatternFlavor::SPF_NABS) {
10353 // The result of -abs(X) is <= 0.
10355 APInt(BitWidth, 1));
10356 }
10357
10358 const APInt *C;
10359 if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C)))
10360 return ConstantRange::getFull(BitWidth);
10361
10362 switch (R.Flavor) {
10363 case SPF_UMIN:
10365 case SPF_UMAX:
10367 case SPF_SMIN:
10369 *C + 1);
10370 case SPF_SMAX:
10373 default:
10374 return ConstantRange::getFull(BitWidth);
10375 }
10376}
10377
10379 // The maximum representable value of a half is 65504. For floats the maximum
10380 // value is 3.4e38 which requires roughly 129 bits.
10381 unsigned BitWidth = I->getType()->getScalarSizeInBits();
10382 if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy())
10383 return;
10384 if (isa<FPToSIInst>(I) && BitWidth >= 17) {
10385 Lower = APInt(BitWidth, -65504, true);
10386 Upper = APInt(BitWidth, 65505);
10387 }
10388
10389 if (isa<FPToUIInst>(I) && BitWidth >= 16) {
10390 // For a fptoui the lower limit is left as 0.
10391 Upper = APInt(BitWidth, 65505);
10392 }
10393}
10394
10396 const SimplifyQuery &SQ,
10397 unsigned Depth) {
10398 assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction");
10399
10401 return ConstantRange::getFull(V->getType()->getScalarSizeInBits());
10402
10403 if (auto *C = dyn_cast<Constant>(V))
10404 return C->toConstantRange();
10405
10406 unsigned BitWidth = V->getType()->getScalarSizeInBits();
10407 ConstantRange CR = ConstantRange::getFull(BitWidth);
10408 if (auto *BO = dyn_cast<BinaryOperator>(V)) {
10409 APInt Lower = APInt(BitWidth, 0);
10410 APInt Upper = APInt(BitWidth, 0);
10411 // TODO: Return ConstantRange.
10412 setLimitsForBinOp(*BO, Lower, Upper, SQ.IIQ, ForSigned);
10414 } else if (auto *II = dyn_cast<IntrinsicInst>(V))
10416 else if (auto *SI = dyn_cast<SelectInst>(V)) {
10417 ConstantRange CRTrue =
10418 computeConstantRange(SI->getTrueValue(), ForSigned, SQ, Depth + 1);
10419 ConstantRange CRFalse =
10420 computeConstantRange(SI->getFalseValue(), ForSigned, SQ, Depth + 1);
10421 CR = CRTrue.unionWith(CRFalse);
10423 } else if (auto *TI = dyn_cast<TruncInst>(V)) {
10424 ConstantRange SrcCR =
10425 computeConstantRange(TI->getOperand(0), ForSigned, SQ, Depth + 1);
10426 CR = SrcCR.truncate(BitWidth);
10427 } else if (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {
10428 APInt Lower = APInt(BitWidth, 0);
10429 APInt Upper = APInt(BitWidth, 0);
10430 // TODO: Return ConstantRange.
10433 } else if (const auto *A = dyn_cast<Argument>(V))
10434 if (std::optional<ConstantRange> Range = A->getRange())
10435 CR = *Range;
10436
10437 if (auto *I = dyn_cast<Instruction>(V)) {
10438 if (auto *Range = SQ.IIQ.getMetadata(I, LLVMContext::MD_range))
10440
10441 Value *FrexpSrc;
10442 if (const auto *CB = dyn_cast<CallBase>(V)) {
10443 if (std::optional<ConstantRange> Range = CB->getRange())
10444 CR = CR.intersectWith(*Range);
10446 m_Value(FrexpSrc))))) {
10447 const fltSemantics &FltSem =
10448 FrexpSrc->getType()->getScalarType()->getFltSemantics();
10449 // It should be possible to implement this for any type, but this logic
10450 // only computes the range assuming standard subnormal handling.
10451 if (APFloat::isIEEELikeFP(FltSem)) {
10452 KnownFPClass KnownSrc =
10453 computeKnownFPClass(FrexpSrc, fcSubnormal, SQ, Depth + 1);
10454
10455 // Exponent result is (src == 0) ? 0 : ilogb(src) + 1, and unspecified
10456 // for inf/nan.
10457 int MinExp = APFloat::semanticsMinExponent(FltSem) + 1;
10458
10459 // Offset to find the true minimum exponent value for a denormal.
10460 if (!KnownSrc.isKnownNeverSubnormal())
10461 MinExp -= (APFloat::semanticsPrecision(FltSem) - 1);
10462
10463 int MaxExp = APFloat::semanticsMaxExponent(FltSem) + 1;
10465 APInt(BitWidth, MinExp, /*isSigned=*/true),
10466 APInt(BitWidth, MaxExp + 1, /*isSigned=*/true));
10467 }
10468 }
10469 }
10470
10471 if (SQ.CxtI && SQ.AC) {
10472 // Try to restrict the range based on information from assumptions.
10473 for (auto &AssumeVH : SQ.AC->assumptionsFor(V)) {
10474 if (!AssumeVH)
10475 continue;
10476 CallInst *I = cast<CallInst>(AssumeVH);
10477 assert(I->getParent()->getParent() == SQ.CxtI->getParent()->getParent() &&
10478 "Got assumption for the wrong function!");
10479 assert(I->getIntrinsicID() == Intrinsic::assume &&
10480 "must be an assume intrinsic");
10481
10482 if (!isValidAssumeForContext(I, SQ))
10483 continue;
10484 Value *Arg = I->getArgOperand(0);
10485 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
10486 // Currently we just use information from comparisons.
10487 if (!Cmp || Cmp->getOperand(0) != V)
10488 continue;
10489 // TODO: Set "ForSigned" parameter via Cmp->isSigned()?
10490 ConstantRange RHS =
10491 computeConstantRange(Cmp->getOperand(1), /*ForSigned=*/false,
10492 SQ.getWithInstruction(I), Depth + 1);
10493 CR = CR.intersectWith(
10494 ConstantRange::makeAllowedICmpRegion(Cmp->getCmpPredicate(), RHS));
10495 }
10496 }
10497
10498 return CR;
10499}
10500
10501static void
10503 function_ref<void(Value *)> InsertAffected) {
10504 assert(V != nullptr);
10505 if (isa<Argument>(V) || isa<GlobalValue>(V)) {
10506 InsertAffected(V);
10507 } else if (auto *I = dyn_cast<Instruction>(V)) {
10508 InsertAffected(V);
10509
10510 // Peek through unary operators to find the source of the condition.
10511 Value *Op;
10513 m_Trunc(m_Value(Op))))) {
10515 InsertAffected(Op);
10516 }
10517 }
10518}
10519
10521 Value *Cond, bool IsAssume, function_ref<void(Value *)> InsertAffected) {
10522 auto AddAffected = [&InsertAffected](Value *V) {
10523 addValueAffectedByCondition(V, InsertAffected);
10524 };
10525
10526 auto AddCmpOperands = [&AddAffected, IsAssume](Value *LHS, Value *RHS) {
10527 if (IsAssume) {
10528 AddAffected(LHS);
10529 AddAffected(RHS);
10530 } else if (match(RHS, m_Constant()))
10531 AddAffected(LHS);
10532 };
10533
10534 SmallVector<Value *, 8> Worklist;
10536 Worklist.push_back(Cond);
10537 while (!Worklist.empty()) {
10538 Value *V = Worklist.pop_back_val();
10539 if (!Visited.insert(V).second)
10540 continue;
10541
10542 CmpPredicate Pred;
10543 Value *A, *B, *X;
10544
10545 if (IsAssume) {
10546 AddAffected(V);
10547 if (match(V, m_Not(m_Value(X))))
10548 AddAffected(X);
10549 }
10550
10551 if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
10552 // assume(A && B) is split to -> assume(A); assume(B);
10553 // assume(!(A || B)) is split to -> assume(!A); assume(!B);
10554 // Finally, assume(A || B) / assume(!(A && B)) generally don't provide
10555 // enough information to be worth handling (intersection of information as
10556 // opposed to union).
10557 if (!IsAssume) {
10558 Worklist.push_back(A);
10559 Worklist.push_back(B);
10560 }
10561 } else if (match(V, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
10562 bool HasRHSC = match(B, m_ConstantInt());
10563 if (ICmpInst::isEquality(Pred)) {
10564 AddAffected(A);
10565 if (IsAssume)
10566 AddAffected(B);
10567 if (HasRHSC) {
10568 Value *Y;
10569 // (X << C) or (X >>_s C) or (X >>_u C).
10570 if (match(A, m_Shift(m_Value(X), m_ConstantInt())))
10571 AddAffected(X);
10572 // (X & C) or (X | C).
10573 else if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10574 match(A, m_Or(m_Value(X), m_Value(Y)))) {
10575 AddAffected(X);
10576 AddAffected(Y);
10577 }
10578 // X - Y
10579 else if (match(A, m_Sub(m_Value(X), m_Value(Y)))) {
10580 AddAffected(X);
10581 AddAffected(Y);
10582 }
10583 }
10584 } else {
10585 AddCmpOperands(A, B);
10586 if (HasRHSC) {
10587 // Handle (A + C1) u< C2, which is the canonical form of
10588 // A > C3 && A < C4.
10590 AddAffected(X);
10591
10592 if (ICmpInst::isUnsigned(Pred)) {
10593 Value *Y;
10594 // X & Y u> C -> X >u C && Y >u C
10595 // X | Y u< C -> X u< C && Y u< C
10596 // X nuw+ Y u< C -> X u< C && Y u< C
10597 if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10598 match(A, m_Or(m_Value(X), m_Value(Y))) ||
10599 match(A, m_NUWAdd(m_Value(X), m_Value(Y)))) {
10600 AddAffected(X);
10601 AddAffected(Y);
10602 }
10603 // X nuw- Y u> C -> X u> C
10604 if (match(A, m_NUWSub(m_Value(X), m_Value())))
10605 AddAffected(X);
10606 }
10607 }
10608
10609 // Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported
10610 // by computeKnownFPClass().
10612 if (Pred == ICmpInst::ICMP_SLT && match(B, m_Zero()))
10613 InsertAffected(X);
10614 else if (Pred == ICmpInst::ICMP_SGT && match(B, m_AllOnes()))
10615 InsertAffected(X);
10616 }
10617 }
10618
10619 if (HasRHSC && match(A, m_Ctpop(m_Value(X))))
10620 AddAffected(X);
10621 } else if (match(V, m_FCmp(Pred, m_Value(A), m_Value(B)))) {
10622 AddCmpOperands(A, B);
10623
10624 // fcmp fneg(x), y
10625 // fcmp fabs(x), y
10626 // fcmp fneg(fabs(x)), y
10627 if (match(A, m_FNeg(m_Value(A))))
10628 AddAffected(A);
10629 if (match(A, m_FAbs(m_Value(A))))
10630 AddAffected(A);
10631
10633 m_Value()))) {
10634 // Handle patterns that computeKnownFPClass() support.
10635 AddAffected(A);
10636 } else if (!IsAssume && match(V, m_Trunc(m_Value(X)))) {
10637 // Assume is checked here as X is already added above for assumes in
10638 // addValueAffectedByCondition
10639 AddAffected(X);
10640 } else if (!IsAssume && match(V, m_Not(m_Value(X)))) {
10641 // Assume is checked here to avoid issues with ephemeral values
10642 Worklist.push_back(X);
10643 }
10644 }
10645}
10646
10648 // (X >> C) or/add (X & mask(C) != 0)
10649 if (const auto *BO = dyn_cast<BinaryOperator>(V)) {
10650 if (BO->getOpcode() == Instruction::Add ||
10651 BO->getOpcode() == Instruction::Or) {
10652 const Value *X;
10653 const APInt *C1, *C2;
10654 if (match(BO, m_c_BinOp(m_LShr(m_Value(X), m_APInt(C1)),
10658 m_Zero())))) &&
10659 C2->popcount() == C1->getZExtValue())
10660 return X;
10661 }
10662 }
10663 return nullptr;
10664}
10665
10667 return const_cast<Value *>(stripNullTest(const_cast<const Value *>(V)));
10668}
10669
10672 unsigned MaxCount, bool AllowUndefOrPoison) {
10675 auto Push = [&](const Value *V) -> bool {
10676 Constant *C;
10677 if (match(const_cast<Value *>(V), m_ImmConstant(C))) {
10678 if (!AllowUndefOrPoison && !isGuaranteedNotToBeUndefOrPoison(C))
10679 return false;
10680 // Check existence first to avoid unnecessary allocations.
10681 if (Constants.contains(C))
10682 return true;
10683 if (Constants.size() == MaxCount)
10684 return false;
10685 Constants.insert(C);
10686 return true;
10687 }
10688
10689 if (auto *Inst = dyn_cast<Instruction>(V)) {
10690 if (Visited.insert(Inst).second)
10691 Worklist.push_back(Inst);
10692 return true;
10693 }
10694 return false;
10695 };
10696 if (!Push(V))
10697 return false;
10698 while (!Worklist.empty()) {
10699 const Instruction *CurInst = Worklist.pop_back_val();
10700 switch (CurInst->getOpcode()) {
10701 case Instruction::Select:
10702 if (!Push(CurInst->getOperand(1)))
10703 return false;
10704 if (!Push(CurInst->getOperand(2)))
10705 return false;
10706 break;
10707 case Instruction::PHI:
10708 for (Value *IncomingValue : cast<PHINode>(CurInst)->incoming_values()) {
10709 // Fast path for recurrence PHI.
10710 if (IncomingValue == CurInst)
10711 continue;
10712 if (!Push(IncomingValue))
10713 return false;
10714 }
10715 break;
10716 default:
10717 return false;
10718 }
10719 }
10720 return true;
10721}
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 _
static MaybeAlign getAlign(Value *Ptr)
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:2006
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1599
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:235
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition APInt.h: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:1670
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:3040
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
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.
LLVM_ABI AllocaInst * findAllocaForValue(Value *V, bool OffsetZero=false)
Returns unique alloca where the value comes from, or nullptr.
LLVM_ABI APInt getMinMaxLimit(SelectPatternFlavor SPF, unsigned BitWidth)
Return the minimum or maximum constant value for the specified integer min/max flavor and type.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI bool isOnlyUsedInZeroComparison(const Instruction *CxtI)
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
LLVM_ABI bool getConstantStringInfo(const Value *V, StringRef &Str, bool TrimAtNul=true)
This function computes the length of a null-terminated C string pointed to by V.
LLVM_ABI bool onlyUsedByLifetimeMarkersOrDroppableInsts(const Value *V)
Return true if the only users of this pointer are lifetime markers or droppable instructions.
LLVM_ABI Constant * ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset)
LLVM_ABI Value * stripNullTest(Value *V)
Returns the inner value X if the expression has the form f(X) where f(X) == 0 if and only if X == 0,...
LLVM_ABI bool getUnderlyingObjectsForCodeGen(const Value *V, SmallVectorImpl< Value * > &Objects)
This is a wrapper around getUnderlyingObjects and adds support for basic ptrtoint+arithmetic+inttoptr...
LLVM_ABI std::pair< Intrinsic::ID, bool > canConvertToMinOrMaxIntrinsic(ArrayRef< Value * > VL)
Check if the values in VL are select instructions that can be converted to a min or max (vector) intr...
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ABI bool getConstantDataArrayInfo(const Value *V, ConstantDataArraySlice &Slice, unsigned ElementSize, uint64_t Offset=0)
Returns true if the value V is a pointer into a ConstantDataArray.
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition bit.h:325
LLVM_ABI bool isGuaranteedToExecuteForEveryIteration(const Instruction *I, const Loop *L)
Return true if this function can prove that the instruction I is executed for every iteration of the ...
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h: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:441
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 isDereferenceableAndAlignedPointer(const Value *V, Type *Ty, Align Alignment, const SimplifyQuery &Q, bool IgnoreFree=false)
Returns true if V is always a dereferenceable pointer with alignment greater or equal than requested.
Definition Loads.cpp:245
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