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