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