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