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