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