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"
80#include <algorithm>
81#include <cassert>
82#include <cstdint>
83#include <optional>
84#include <utility>
85
86using namespace llvm;
87using namespace llvm::PatternMatch;
88
89// Controls the number of uses of the value searched for possible
90// dominating comparisons.
91static cl::opt<unsigned> DomConditionsMaxUses("dom-conditions-max-uses",
92 cl::Hidden, cl::init(20));
93
94/// Maximum number of instructions to check between assume and context
95/// instruction.
96static constexpr unsigned MaxInstrsToCheckForFree = 16;
97
98/// Returns the bitwidth of the given scalar or pointer type. For vector types,
99/// returns the element type's bitwidth.
100static unsigned getBitWidth(Type *Ty, const DataLayout &DL) {
101 if (unsigned BitWidth = Ty->getScalarSizeInBits())
102 return BitWidth;
103
104 return DL.getPointerTypeSizeInBits(Ty);
105}
106
107// Given the provided Value and, potentially, a context instruction, return
108// the preferred context instruction (if any).
109static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI) {
110 // If we've been provided with a context instruction, then use that (provided
111 // it has been inserted).
112 if (CxtI && CxtI->getParent())
113 return CxtI;
114
115 // If the value is really an already-inserted instruction, then use that.
116 CxtI = dyn_cast<Instruction>(V);
117 if (CxtI && CxtI->getParent())
118 return CxtI;
119
120 return nullptr;
121}
122
124 const APInt &DemandedElts,
125 APInt &DemandedLHS, APInt &DemandedRHS) {
126 if (isa<ScalableVectorType>(Shuf->getType())) {
127 assert(DemandedElts == APInt(1,1));
128 DemandedLHS = DemandedRHS = DemandedElts;
129 return true;
130 }
131
132 int NumElts =
133 cast<FixedVectorType>(Shuf->getOperand(0)->getType())->getNumElements();
134 return llvm::getShuffleDemandedElts(NumElts, Shuf->getShuffleMask(),
135 DemandedElts, DemandedLHS, DemandedRHS);
136}
137
138static void computeKnownBits(const Value *V, const APInt &DemandedElts,
139 KnownBits &Known, const SimplifyQuery &Q,
140 unsigned Depth);
141
143 const SimplifyQuery &Q, unsigned Depth) {
144 // Since the number of lanes in a scalable vector is unknown at compile time,
145 // we track one bit which is implicitly broadcast to all lanes. This means
146 // that all lanes in a scalable vector are considered demanded.
147 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
148 APInt DemandedElts =
149 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
150 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);
151}
152
154 const DataLayout &DL, AssumptionCache *AC,
155 const Instruction *CxtI, const DominatorTree *DT,
156 bool UseInstrInfo, unsigned Depth) {
157 computeKnownBits(V, Known,
158 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo),
159 Depth);
160}
161
163 AssumptionCache *AC, const Instruction *CxtI,
164 const DominatorTree *DT, bool UseInstrInfo,
165 unsigned Depth) {
166 return computeKnownBits(
167 V, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
168}
169
170KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
171 const DataLayout &DL, AssumptionCache *AC,
172 const Instruction *CxtI,
173 const DominatorTree *DT, bool UseInstrInfo,
174 unsigned Depth) {
175 return computeKnownBits(
176 V, DemandedElts,
177 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
178}
179
181 const SimplifyQuery &SQ) {
182 // Look for an inverted mask: (X & ~M) op (Y & M).
183 {
184 Value *M;
185 if (match(LHS, m_c_And(m_Not(m_Value(M)), m_Value())) &&
187 isGuaranteedNotToBeUndef(M, SQ.AC, SQ.CxtI, SQ.DT))
188 return true;
189 }
190
191 // X op (Y & ~X)
194 return true;
195
196 // X op ((X & Y) ^ Y) -- this is the canonical form of the previous pattern
197 // for constant Y.
198 Value *Y;
199 if (match(RHS,
201 isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT) &&
202 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
203 return true;
204
205 // Peek through extends to find a 'not' of the other side:
206 // (ext Y) op ext(~Y)
207 if (match(LHS, m_ZExtOrSExt(m_Value(Y))) &&
209 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
210 return true;
211
212 // Look for: (A & B) op ~(A | B)
213 {
214 Value *A, *B;
215 if (match(LHS, m_And(m_Value(A), m_Value(B))) &&
217 isGuaranteedNotToBeUndef(A, SQ.AC, SQ.CxtI, SQ.DT) &&
218 isGuaranteedNotToBeUndef(B, SQ.AC, SQ.CxtI, SQ.DT))
219 return true;
220 }
221
222 // Look for: (X << V) op (Y >> (BitWidth - V))
223 // or (X >> V) op (Y << (BitWidth - V))
224 {
225 const Value *V;
226 const APInt *R;
227 if (((match(RHS, m_Shl(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&
228 match(LHS, m_LShr(m_Value(), m_Specific(V)))) ||
229 (match(RHS, m_LShr(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&
230 match(LHS, m_Shl(m_Value(), m_Specific(V))))) &&
231 R->uge(LHS->getType()->getScalarSizeInBits()))
232 return true;
233 }
234
235 return false;
236}
237
239 const WithCache<const Value *> &RHSCache,
240 const SimplifyQuery &SQ) {
241 const Value *LHS = LHSCache.getValue();
242 const Value *RHS = RHSCache.getValue();
243
244 assert(LHS->getType() == RHS->getType() &&
245 "LHS and RHS should have the same type");
246 assert(LHS->getType()->isIntOrIntVectorTy() &&
247 "LHS and RHS should be integers");
248
249 if (haveNoCommonBitsSetSpecialCases(LHS, RHS, SQ) ||
251 return true;
252
254 RHSCache.getKnownBits(SQ));
255}
256
258 return !I->user_empty() &&
259 all_of(I->users(), match_fn(m_ICmp(m_Value(), m_Zero())));
260}
261
263 return !I->user_empty() && all_of(I->users(), [](const User *U) {
264 CmpPredicate P;
265 return match(U, m_ICmp(P, m_Value(), m_Zero())) && ICmpInst::isEquality(P);
266 });
267}
268
270 bool OrZero, AssumptionCache *AC,
271 const Instruction *CxtI,
272 const DominatorTree *DT, bool UseInstrInfo,
273 unsigned Depth) {
274 return ::isKnownToBeAPowerOfTwo(
275 V, OrZero, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo),
276 Depth);
277}
278
279static bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
280 const SimplifyQuery &Q, unsigned Depth);
281
283 unsigned Depth) {
284 return computeKnownBits(V, SQ, Depth).isNonNegative();
285}
286
288 unsigned Depth) {
289 if (auto *CI = dyn_cast<ConstantInt>(V))
290 return CI->getValue().isStrictlyPositive();
291
292 // If `isKnownNonNegative` ever becomes more sophisticated, make sure to keep
293 // this updated.
294 KnownBits Known = computeKnownBits(V, SQ, Depth);
295 return Known.isNonNegative() &&
296 (Known.isNonZero() || isKnownNonZero(V, SQ, Depth));
297}
298
300 unsigned Depth) {
301 return computeKnownBits(V, SQ, Depth).isNegative();
302}
303
304static bool isKnownNonEqual(const Value *V1, const Value *V2,
305 const APInt &DemandedElts, const SimplifyQuery &Q,
306 unsigned Depth);
307
308bool llvm::isKnownNonEqual(const Value *V1, const Value *V2,
309 const SimplifyQuery &Q, unsigned Depth) {
310 // We don't support looking through casts.
311 if (V1 == V2 || V1->getType() != V2->getType())
312 return false;
313 auto *FVTy = dyn_cast<FixedVectorType>(V1->getType());
314 APInt DemandedElts =
315 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
316 return ::isKnownNonEqual(V1, V2, DemandedElts, Q, Depth);
317}
318
319bool llvm::MaskedValueIsZero(const Value *V, const APInt &Mask,
320 const SimplifyQuery &SQ, unsigned Depth) {
321 KnownBits Known(Mask.getBitWidth());
322 computeKnownBits(V, Known, SQ, Depth);
323 return Mask.isSubsetOf(Known.Zero);
324}
325
326static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
327 const SimplifyQuery &Q, unsigned Depth);
328
329static unsigned ComputeNumSignBits(const Value *V, const SimplifyQuery &Q,
330 unsigned Depth = 0) {
331 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
332 APInt DemandedElts =
333 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
334 return ComputeNumSignBits(V, DemandedElts, Q, Depth);
335}
336
337unsigned llvm::ComputeNumSignBits(const Value *V, const DataLayout &DL,
338 AssumptionCache *AC, const Instruction *CxtI,
339 const DominatorTree *DT, bool UseInstrInfo,
340 unsigned Depth) {
341 return ::ComputeNumSignBits(
342 V, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
343}
344
346 AssumptionCache *AC,
347 const Instruction *CxtI,
348 const DominatorTree *DT,
349 unsigned Depth) {
350 unsigned SignBits = ComputeNumSignBits(V, DL, AC, CxtI, DT, Depth);
351 return V->getType()->getScalarSizeInBits() - SignBits + 1;
352}
353
354/// Try to detect the lerp pattern: a * (b - c) + c * d
355/// where a >= 0, b >= 0, c >= 0, d >= 0, and b >= c.
356///
357/// In that particular case, we can use the following chain of reasoning:
358///
359/// a * (b - c) + c * d <= a' * (b - c) + a' * c = a' * b where a' = max(a, d)
360///
361/// Since that is true for arbitrary a, b, c and d within our constraints, we
362/// can conclude that:
363///
364/// max(a * (b - c) + c * d) <= max(max(a), max(d)) * max(b) = U
365///
366/// Considering that any result of the lerp would be less or equal to U, it
367/// would have at least the number of leading 0s as in U.
368///
369/// While being quite a specific situation, it is fairly common in computer
370/// graphics in the shape of alpha blending.
371///
372/// Modifies given KnownOut in-place with the inferred information.
373static void computeKnownBitsFromLerpPattern(const Value *Op0, const Value *Op1,
374 const APInt &DemandedElts,
375 KnownBits &KnownOut,
376 const SimplifyQuery &Q,
377 unsigned Depth) {
378
379 Type *Ty = Op0->getType();
380 const unsigned BitWidth = Ty->getScalarSizeInBits();
381
382 // Only handle scalar types for now
383 if (Ty->isVectorTy())
384 return;
385
386 // Try to match: a * (b - c) + c * d.
387 // When a == 1 => A == nullptr, the same applies to d/D as well.
388 const Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
389 const Instruction *SubBC = nullptr;
390
391 const auto MatchSubBC = [&]() {
392 // (b - c) can have two forms that interest us:
393 //
394 // 1. sub nuw %b, %c
395 // 2. xor %c, %b
396 //
397 // For the first case, nuw flag guarantees our requirement b >= c.
398 //
399 // The second case might happen when the analysis can infer that b is a mask
400 // for c and we can transform sub operation into xor (that is usually true
401 // for constant b's). Even though xor is symmetrical, canonicalization
402 // ensures that the constant will be the RHS. We have additional checks
403 // later on to ensure that this xor operation is equivalent to subtraction.
405 m_Xor(m_Value(C), m_Value(B))));
406 };
407
408 const auto MatchASubBC = [&]() {
409 // Cases:
410 // - a * (b - c)
411 // - (b - c) * a
412 // - (b - c) <- a implicitly equals 1
413 return m_CombineOr(m_c_Mul(m_Value(A), MatchSubBC()), MatchSubBC());
414 };
415
416 const auto MatchCD = [&]() {
417 // Cases:
418 // - d * c
419 // - c * d
420 // - c <- d implicitly equals 1
422 };
423
424 const auto Match = [&](const Value *LHS, const Value *RHS) {
425 // We do use m_Specific(C) in MatchCD, so we have to make sure that
426 // it's bound to anything and match(LHS, MatchASubBC()) absolutely
427 // has to evaluate first and return true.
428 //
429 // If Match returns true, it is guaranteed that B != nullptr, C != nullptr.
430 return match(LHS, MatchASubBC()) && match(RHS, MatchCD());
431 };
432
433 if (!Match(Op0, Op1) && !Match(Op1, Op0))
434 return;
435
436 const auto ComputeKnownBitsOrOne = [&](const Value *V) {
437 // For some of the values we use the convention of leaving
438 // it nullptr to signify an implicit constant 1.
439 return V ? computeKnownBits(V, DemandedElts, Q, Depth + 1)
441 };
442
443 // Check that all operands are non-negative
444 const KnownBits KnownA = ComputeKnownBitsOrOne(A);
445 if (!KnownA.isNonNegative())
446 return;
447
448 const KnownBits KnownD = ComputeKnownBitsOrOne(D);
449 if (!KnownD.isNonNegative())
450 return;
451
452 const KnownBits KnownB = computeKnownBits(B, DemandedElts, Q, Depth + 1);
453 if (!KnownB.isNonNegative())
454 return;
455
456 const KnownBits KnownC = computeKnownBits(C, DemandedElts, Q, Depth + 1);
457 if (!KnownC.isNonNegative())
458 return;
459
460 // If we matched subtraction as xor, we need to actually check that xor
461 // is semantically equivalent to subtraction.
462 //
463 // For that to be true, b has to be a mask for c or that b's known
464 // ones cover all known and possible ones of c.
465 if (SubBC->getOpcode() == Instruction::Xor &&
466 !KnownC.getMaxValue().isSubsetOf(KnownB.getMinValue()))
467 return;
468
469 const APInt MaxA = KnownA.getMaxValue();
470 const APInt MaxD = KnownD.getMaxValue();
471 const APInt MaxAD = APIntOps::umax(MaxA, MaxD);
472 const APInt MaxB = KnownB.getMaxValue();
473
474 // We can't infer leading zeros info if the upper-bound estimate wraps.
475 bool Overflow;
476 const APInt UpperBound = MaxAD.umul_ov(MaxB, Overflow);
477
478 if (Overflow)
479 return;
480
481 // If we know that x <= y and both are positive than x has at least the same
482 // number of leading zeros as y.
483 const unsigned MinimumNumberOfLeadingZeros = UpperBound.countl_zero();
484 KnownOut.Zero.setHighBits(MinimumNumberOfLeadingZeros);
485}
486
487static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1,
488 bool NSW, bool NUW,
489 const APInt &DemandedElts,
490 KnownBits &KnownOut, KnownBits &Known2,
491 const SimplifyQuery &Q, unsigned Depth) {
492 computeKnownBits(Op1, DemandedElts, KnownOut, Q, Depth + 1);
493
494 // If one operand is unknown and we have no nowrap information,
495 // the result will be unknown independently of the second operand.
496 if (KnownOut.isUnknown() && !NSW && !NUW)
497 return;
498
499 computeKnownBits(Op0, DemandedElts, Known2, Q, Depth + 1);
500 KnownOut = KnownBits::computeForAddSub(Add, NSW, NUW, Known2, KnownOut);
501
502 if (!Add && NSW && !KnownOut.isNonNegative() &&
504 .value_or(false) ||
505 match(Op1, m_c_SMin(m_Specific(Op0), m_Value()))))
506 KnownOut.makeNonNegative();
507
508 if (Add)
509 // Try to match lerp pattern and combine results
510 computeKnownBitsFromLerpPattern(Op0, Op1, DemandedElts, KnownOut, Q, Depth);
511}
512
513static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,
514 bool NUW, const APInt &DemandedElts,
515 KnownBits &Known, KnownBits &Known2,
516 const SimplifyQuery &Q, unsigned Depth) {
517 computeKnownBits(Op1, DemandedElts, Known, Q, Depth + 1);
518 computeKnownBits(Op0, DemandedElts, Known2, Q, Depth + 1);
519
520 bool isKnownNegative = false;
521 bool isKnownNonNegative = false;
522 // If the multiplication is known not to overflow, compute the sign bit.
523 if (NSW) {
524 if (Op0 == Op1) {
525 // The product of a number with itself is non-negative.
526 isKnownNonNegative = true;
527 } else {
528 bool isKnownNonNegativeOp1 = Known.isNonNegative();
529 bool isKnownNonNegativeOp0 = Known2.isNonNegative();
530 bool isKnownNegativeOp1 = Known.isNegative();
531 bool isKnownNegativeOp0 = Known2.isNegative();
532 // The product of two numbers with the same sign is non-negative.
533 isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) ||
534 (isKnownNonNegativeOp1 && isKnownNonNegativeOp0);
535 if (!isKnownNonNegative && NUW) {
536 // mul nuw nsw with a factor > 1 is non-negative.
538 isKnownNonNegative = KnownBits::sgt(Known, One).value_or(false) ||
539 KnownBits::sgt(Known2, One).value_or(false);
540 }
541
542 // The product of a negative number and a non-negative number is either
543 // negative or zero.
546 (isKnownNegativeOp1 && isKnownNonNegativeOp0 &&
547 Known2.isNonZero()) ||
548 (isKnownNegativeOp0 && isKnownNonNegativeOp1 && Known.isNonZero());
549 }
550 }
551
552 bool SelfMultiply = Op0 == Op1;
553 if (SelfMultiply)
554 SelfMultiply &=
555 isGuaranteedNotToBeUndef(Op0, Q.AC, Q.CxtI, Q.DT, Depth + 1);
556 Known = KnownBits::mul(Known, Known2, SelfMultiply);
557
558 if (SelfMultiply) {
559 unsigned SignBits = ComputeNumSignBits(Op0, DemandedElts, Q, Depth + 1);
560 unsigned TyBits = Op0->getType()->getScalarSizeInBits();
561 unsigned OutValidBits = 2 * (TyBits - SignBits + 1);
562
563 if (OutValidBits < TyBits) {
564 APInt KnownZeroMask =
565 APInt::getHighBitsSet(TyBits, TyBits - OutValidBits + 1);
566 Known.Zero |= KnownZeroMask;
567 }
568 }
569
570 // Only make use of no-wrap flags if we failed to compute the sign bit
571 // directly. This matters if the multiplication always overflows, in
572 // which case we prefer to follow the result of the direct computation,
573 // though as the program is invoking undefined behaviour we can choose
574 // whatever we like here.
575 if (isKnownNonNegative && !Known.isNegative())
576 Known.makeNonNegative();
577 else if (isKnownNegative && !Known.isNonNegative())
578 Known.makeNegative();
579}
580
582 KnownBits &Known) {
583 unsigned BitWidth = Known.getBitWidth();
584 unsigned NumRanges = Ranges.getNumOperands() / 2;
585 assert(NumRanges >= 1);
586
587 Known.setAllConflict();
588
589 for (unsigned i = 0; i < NumRanges; ++i) {
591 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
593 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
594 ConstantRange Range(Lower->getValue(), Upper->getValue());
595 // BitWidth must equal the Ranges BitWidth for the correct number of high
596 // bits to be set.
597 assert(BitWidth == Range.getBitWidth() &&
598 "Known bit width must match range bit width!");
599
600 // The first CommonPrefixBits of all values in Range are equal.
601 unsigned CommonPrefixBits =
602 (Range.getUnsignedMax() ^ Range.getUnsignedMin()).countl_zero();
603 APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits);
604 APInt UnsignedMax = Range.getUnsignedMax().zextOrTrunc(BitWidth);
605 Known.One &= UnsignedMax & Mask;
606 Known.Zero &= ~UnsignedMax & Mask;
607 }
608}
609
610static bool isEphemeralValueOf(const Instruction *I, const Value *E) {
611 // The instruction defining an assumption's condition itself is always
612 // considered ephemeral to that assumption (even if it has other
613 // non-ephemeral users). See r246696's test case for an example.
614 if (is_contained(I->operands(), E))
615 return true;
616
617 const auto *EI = dyn_cast<Instruction>(E);
618 if (!EI)
619 return false;
620
621 if (EI == I)
622 return true;
623
626 Visited.insert(EI);
627 WorkList.push_back(EI);
628 bool ReachesI = false;
629 while (!WorkList.empty()) {
630 const Instruction *V = WorkList.pop_back_val();
631 for (const User *U : V->users()) {
632 const auto *UI = cast<Instruction>(U);
633 if (UI == I) {
634 ReachesI = true;
635 continue;
636 }
637 if (UI->mayHaveSideEffects() || UI->isTerminator())
638 return false;
639 if (Visited.insert(UI).second)
640 WorkList.push_back(UI);
641 }
642 }
643 return ReachesI;
644}
645
646// Is this an intrinsic that cannot be speculated but also cannot trap?
648 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(I))
649 return CI->isAssumeLikeIntrinsic();
650
651 return false;
652}
653
655 const Instruction *CxtI,
656 const DominatorTree *DT,
657 bool AllowEphemerals) {
658 // There are two restrictions on the use of an assume:
659 // 1. The assume must dominate the context (or the control flow must
660 // reach the assume whenever it reaches the context).
661 // 2. The context must not be in the assume's set of ephemeral values
662 // (otherwise we will use the assume to prove that the condition
663 // feeding the assume is trivially true, thus causing the removal of
664 // the assume).
665
666 if (Inv->getParent() == CxtI->getParent()) {
667 // If Inv and CtxI are in the same block, check if the assume (Inv) is first
668 // in the BB.
669 if (Inv->comesBefore(CxtI))
670 return true;
671
672 // Don't let an assume affect itself - this would cause the problems
673 // `isEphemeralValueOf` is trying to prevent, and it would also make
674 // the loop below go out of bounds.
675 if (!AllowEphemerals && Inv == CxtI)
676 return false;
677
678 // The context comes first, but they're both in the same block.
679 // Make sure there is nothing in between that might interrupt
680 // the control flow, not even CxtI itself.
681 // We limit the scan distance between the assume and its context instruction
682 // to avoid a compile-time explosion. This limit is chosen arbitrarily, so
683 // it can be adjusted if needed (could be turned into a cl::opt).
684 auto Range = make_range(CxtI->getIterator(), Inv->getIterator());
686 return false;
687
688 return AllowEphemerals || !isEphemeralValueOf(Inv, CxtI);
689 }
690
691 // Inv and CxtI are in different blocks.
692 if (DT) {
693 if (DT->dominates(Inv, CxtI))
694 return true;
695 } else if (Inv->getParent() == CxtI->getParent()->getSinglePredecessor() ||
696 Inv->getParent()->isEntryBlock()) {
697 // We don't have a DT, but this trivially dominates.
698 return true;
699 }
700
701 return false;
702}
703
705 const Instruction *CtxI) {
706 // Helper to check if there are any calls in the range that may free memory.
707 auto hasNoFreeInRange = [](auto Range) {
708 for (const auto &[Idx, I] : enumerate(Range)) {
709 if (Idx > MaxInstrsToCheckForFree)
710 return false;
711
712 if (auto *CB = dyn_cast<CallBase>(&I)) {
713 if (!CB->hasFnAttr(Attribute::NoFree))
714 return false;
715 } else if (I.maySynchronize())
716 return false;
717 }
718 return true;
719 };
720
721 // Handle cross-block case: CtxI in a successor of Assume's block.
722 const BasicBlock *CtxBB = CtxI->getParent();
723 const BasicBlock *AssumeBB = Assume->getParent();
724 BasicBlock::const_iterator CtxIter = CtxI->getIterator();
725 if (CtxBB != AssumeBB) {
726 if (CtxBB->getSinglePredecessor() != AssumeBB)
727 return false;
728
729 if (!hasNoFreeInRange(make_range(CtxBB->begin(), CtxIter)))
730 return false;
731
732 CtxIter = AssumeBB->end();
733 } else {
734 // Same block case: check that Assume comes before CtxI.
735 if (!Assume->comesBefore(CtxI))
736 return false;
737 }
738
739 // Check if there are any calls between Assume and CtxIter that may free
740 // memory.
741 return hasNoFreeInRange(make_range(Assume->getIterator(), CtxIter));
742}
743
744// TODO: cmpExcludesZero misses many cases where `RHS` is non-constant but
745// we still have enough information about `RHS` to conclude non-zero. For
746// example Pred=EQ, RHS=isKnownNonZero. cmpExcludesZero is called in loops
747// so the extra compile time may not be worth it, but possibly a second API
748// should be created for use outside of loops.
749static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS) {
750 // v u> y implies v != 0.
751 if (Pred == ICmpInst::ICMP_UGT)
752 return true;
753
754 // Special-case v != 0 to also handle v != null.
755 if (Pred == ICmpInst::ICMP_NE)
756 return match(RHS, m_Zero());
757
758 // All other predicates - rely on generic ConstantRange handling.
759 const APInt *C;
760 auto Zero = APInt::getZero(RHS->getType()->getScalarSizeInBits());
761 if (match(RHS, m_APInt(C))) {
763 return !TrueValues.contains(Zero);
764 }
765
767 if (VC == nullptr)
768 return false;
769
770 for (unsigned ElemIdx = 0, NElem = VC->getNumElements(); ElemIdx < NElem;
771 ++ElemIdx) {
773 Pred, VC->getElementAsAPInt(ElemIdx));
774 if (TrueValues.contains(Zero))
775 return false;
776 }
777 return true;
778}
779
780static void breakSelfRecursivePHI(const Use *U, const PHINode *PHI,
781 Value *&ValOut, Instruction *&CtxIOut,
782 const PHINode **PhiOut = nullptr) {
783 ValOut = U->get();
784 if (ValOut == PHI)
785 return;
786 CtxIOut = PHI->getIncomingBlock(*U)->getTerminator();
787 if (PhiOut)
788 *PhiOut = PHI;
789 Value *V;
790 // If the Use is a select of this phi, compute analysis on other arm to break
791 // recursion.
792 // TODO: Min/Max
793 if (match(ValOut, m_Select(m_Value(), m_Specific(PHI), m_Value(V))) ||
794 match(ValOut, m_Select(m_Value(), m_Value(V), m_Specific(PHI))))
795 ValOut = V;
796
797 // Same for select, if this phi is 2-operand phi, compute analysis on other
798 // incoming value to break recursion.
799 // TODO: We could handle any number of incoming edges as long as we only have
800 // two unique values.
801 if (auto *IncPhi = dyn_cast<PHINode>(ValOut);
802 IncPhi && IncPhi->getNumIncomingValues() == 2) {
803 for (int Idx = 0; Idx < 2; ++Idx) {
804 if (IncPhi->getIncomingValue(Idx) == PHI) {
805 ValOut = IncPhi->getIncomingValue(1 - Idx);
806 if (PhiOut)
807 *PhiOut = IncPhi;
808 CtxIOut = IncPhi->getIncomingBlock(1 - Idx)->getTerminator();
809 break;
810 }
811 }
812 }
813}
814
815static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q) {
816 // Use of assumptions is context-sensitive. If we don't have a context, we
817 // cannot use them!
818 if (!Q.AC || !Q.CxtI)
819 return false;
820
821 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
822 if (!Elem.Assume)
823 continue;
824
825 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
826 assert(I->getFunction() == Q.CxtI->getFunction() &&
827 "Got assumption for the wrong function!");
828
829 if (Elem.Index != AssumptionCache::ExprResultIdx) {
830 if (!V->getType()->isPointerTy())
831 continue;
833 *I, I->bundle_op_info_begin()[Elem.Index])) {
834 if (RK.WasOn != V)
835 continue;
836 bool AssumeImpliesNonNull = [&]() {
837 if (RK.AttrKind == Attribute::NonNull)
838 return true;
839
840 if (RK.AttrKind == Attribute::Dereferenceable) {
843 return false;
844 assert(RK.IRArgValue &&
845 "Dereferenceable attribute without IR argument?");
846
847 auto *CI = dyn_cast<ConstantInt>(RK.IRArgValue);
848 return CI && !CI->isZero();
849 }
850
851 return false;
852 }();
853 if (AssumeImpliesNonNull && isValidAssumeForContext(I, Q))
854 return true;
855 }
856 continue;
857 }
858
859 // Warning: This loop can end up being somewhat performance sensitive.
860 // We're running this loop for once for each value queried resulting in a
861 // runtime of ~O(#assumes * #values).
862
863 Value *RHS;
864 CmpPredicate Pred;
865 auto m_V = m_CombineOr(m_Specific(V), m_PtrToInt(m_Specific(V)));
866 if (!match(I->getArgOperand(0), m_c_ICmp(Pred, m_V, m_Value(RHS))))
867 continue;
868
870 return true;
871 }
872
873 return false;
874}
875
877 Value *LHS, Value *RHS, KnownBits &Known,
878 const SimplifyQuery &Q) {
879 if (RHS->getType()->isPointerTy()) {
880 // Handle comparison of pointer to null explicitly, as it will not be
881 // covered by the m_APInt() logic below.
882 if (LHS == V && match(RHS, m_Zero())) {
883 switch (Pred) {
885 Known.setAllZero();
886 break;
889 Known.makeNonNegative();
890 break;
892 Known.makeNegative();
893 break;
894 default:
895 break;
896 }
897 }
898 return;
899 }
900
901 unsigned BitWidth = Known.getBitWidth();
902 auto m_V =
904
905 Value *Y;
906 const APInt *Mask, *C;
907 if (!match(RHS, m_APInt(C)))
908 return;
909
910 uint64_t ShAmt;
911 switch (Pred) {
913 // assume(V = C)
914 if (match(LHS, m_V)) {
915 Known = Known.unionWith(KnownBits::makeConstant(*C));
916 // assume(V & Mask = C)
917 } else if (match(LHS, m_c_And(m_V, m_Value(Y)))) {
918 // For one bits in Mask, we can propagate bits from C to V.
919 Known.One |= *C;
920 if (match(Y, m_APInt(Mask)))
921 Known.Zero |= ~*C & *Mask;
922 // assume(V | Mask = C)
923 } else if (match(LHS, m_c_Or(m_V, m_Value(Y)))) {
924 // For zero bits in Mask, we can propagate bits from C to V.
925 Known.Zero |= ~*C;
926 if (match(Y, m_APInt(Mask)))
927 Known.One |= *C & ~*Mask;
928 // assume(V << ShAmt = C)
929 } else if (match(LHS, m_Shl(m_V, m_ConstantInt(ShAmt))) &&
930 ShAmt < BitWidth) {
931 // For those bits in C that are known, we can propagate them to known
932 // bits in V shifted to the right by ShAmt.
934 RHSKnown >>= ShAmt;
935 Known = Known.unionWith(RHSKnown);
936 // assume(V >> ShAmt = C)
937 } else if (match(LHS, m_Shr(m_V, m_ConstantInt(ShAmt))) &&
938 ShAmt < BitWidth) {
939 // For those bits in RHS that are known, we can propagate them to known
940 // bits in V shifted to the right by C.
942 RHSKnown <<= ShAmt;
943 Known = Known.unionWith(RHSKnown);
944 }
945 break;
946 case ICmpInst::ICMP_NE: {
947 // assume (V & B != 0) where B is a power of 2
948 const APInt *BPow2;
949 if (C->isZero() && match(LHS, m_And(m_V, m_Power2(BPow2))))
950 Known.One |= *BPow2;
951 break;
952 }
953 default: {
954 const APInt *Offset = nullptr;
955 if (match(LHS, m_CombineOr(m_V, m_AddLike(m_V, m_APInt(Offset))))) {
957 if (Offset)
958 LHSRange = LHSRange.sub(*Offset);
959 Known = Known.unionWith(LHSRange.toKnownBits());
960 }
961 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
962 // X & Y u> C -> X u> C && Y u> C
963 // X nuw- Y u> C -> X u> C
964 if (match(LHS, m_c_And(m_V, m_Value())) ||
965 match(LHS, m_NUWSub(m_V, m_Value())))
966 Known.One.setHighBits(
967 (*C + (Pred == ICmpInst::ICMP_UGT)).countLeadingOnes());
968 }
969 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
970 // X | Y u< C -> X u< C && Y u< C
971 // X nuw+ Y u< C -> X u< C && Y u< C
972 if (match(LHS, m_c_Or(m_V, m_Value())) ||
973 match(LHS, m_c_NUWAdd(m_V, m_Value()))) {
974 Known.Zero.setHighBits(
975 (*C - (Pred == ICmpInst::ICMP_ULT)).countLeadingZeros());
976 }
977 }
978 } break;
979 }
980}
981
982static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp,
983 KnownBits &Known,
984 const SimplifyQuery &SQ, bool Invert) {
986 Invert ? Cmp->getInversePredicate() : Cmp->getPredicate();
987 Value *LHS = Cmp->getOperand(0);
988 Value *RHS = Cmp->getOperand(1);
989
990 // Handle icmp pred (trunc V), C
991 if (match(LHS, m_Trunc(m_Specific(V)))) {
992 KnownBits DstKnown(LHS->getType()->getScalarSizeInBits());
993 computeKnownBitsFromCmp(LHS, Pred, LHS, RHS, DstKnown, SQ);
995 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
996 else
997 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
998 return;
999 }
1000
1001 computeKnownBitsFromCmp(V, Pred, LHS, RHS, Known, SQ);
1002}
1003
1005 KnownBits &Known, const SimplifyQuery &SQ,
1006 bool Invert, unsigned Depth) {
1007 Value *A, *B;
1010 KnownBits Known2(Known.getBitWidth());
1011 KnownBits Known3(Known.getBitWidth());
1012 computeKnownBitsFromCond(V, A, Known2, SQ, Invert, Depth + 1);
1013 computeKnownBitsFromCond(V, B, Known3, SQ, Invert, Depth + 1);
1014 if (Invert ? match(Cond, m_LogicalOr(m_Value(), m_Value()))
1016 Known2 = Known2.unionWith(Known3);
1017 else
1018 Known2 = Known2.intersectWith(Known3);
1019 Known = Known.unionWith(Known2);
1020 return;
1021 }
1022
1023 if (auto *Cmp = dyn_cast<ICmpInst>(Cond)) {
1024 computeKnownBitsFromICmpCond(V, Cmp, Known, SQ, Invert);
1025 return;
1026 }
1027
1028 if (match(Cond, m_Trunc(m_Specific(V)))) {
1029 KnownBits DstKnown(1);
1030 if (Invert) {
1031 DstKnown.setAllZero();
1032 } else {
1033 DstKnown.setAllOnes();
1034 }
1036 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
1037 return;
1038 }
1039 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
1040 return;
1041 }
1042
1044 computeKnownBitsFromCond(V, A, Known, SQ, !Invert, Depth + 1);
1045}
1046
1048 const SimplifyQuery &Q, unsigned Depth) {
1049 // Handle injected condition.
1050 if (Q.CC && Q.CC->AffectedValues.contains(V))
1051 computeKnownBitsFromCond(V, Q.CC->Cond, Known, Q, Q.CC->Invert, Depth);
1052
1053 if (!Q.CxtI)
1054 return;
1055
1056 if (Q.DC && Q.DT) {
1057 // Handle dominating conditions.
1058 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
1059 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
1060 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
1061 computeKnownBitsFromCond(V, BI->getCondition(), Known, Q,
1062 /*Invert*/ false, Depth);
1063
1064 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
1065 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
1066 computeKnownBitsFromCond(V, BI->getCondition(), Known, Q,
1067 /*Invert*/ true, Depth);
1068 }
1069
1070 if (Known.hasConflict())
1071 Known.resetAll();
1072 }
1073
1074 if (!Q.AC)
1075 return;
1076
1077 unsigned BitWidth = Known.getBitWidth();
1078
1079 // Note that the patterns below need to be kept in sync with the code
1080 // in AssumptionCache::updateAffectedValues.
1081
1082 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
1083 if (!Elem.Assume)
1084 continue;
1085
1086 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
1087 assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() &&
1088 "Got assumption for the wrong function!");
1089
1090 if (Elem.Index != AssumptionCache::ExprResultIdx) {
1091 if (!V->getType()->isPointerTy())
1092 continue;
1094 *I, I->bundle_op_info_begin()[Elem.Index])) {
1095 if (RK.WasOn == V && RK.AttrKind == Attribute::Alignment &&
1096 isPowerOf2_64(RK.ArgValue) && isValidAssumeForContext(I, Q))
1097 Known.Zero.setLowBits(Log2_64(RK.ArgValue));
1098 }
1099 continue;
1100 }
1101
1102 // Warning: This loop can end up being somewhat performance sensitive.
1103 // We're running this loop for once for each value queried resulting in a
1104 // runtime of ~O(#assumes * #values).
1105
1106 Value *Arg = I->getArgOperand(0);
1107
1108 if (Arg == V && isValidAssumeForContext(I, Q)) {
1109 assert(BitWidth == 1 && "assume operand is not i1?");
1110 (void)BitWidth;
1111 Known.setAllOnes();
1112 return;
1113 }
1114 if (match(Arg, m_Not(m_Specific(V))) &&
1116 assert(BitWidth == 1 && "assume operand is not i1?");
1117 (void)BitWidth;
1118 Known.setAllZero();
1119 return;
1120 }
1121 auto *Trunc = dyn_cast<TruncInst>(Arg);
1122 if (Trunc && Trunc->getOperand(0) == V &&
1124 if (Trunc->hasNoUnsignedWrap()) {
1126 return;
1127 }
1128 Known.One.setBit(0);
1129 return;
1130 }
1131
1132 // The remaining tests are all recursive, so bail out if we hit the limit.
1134 continue;
1135
1136 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
1137 if (!Cmp)
1138 continue;
1139
1140 if (!isValidAssumeForContext(I, Q))
1141 continue;
1142
1143 computeKnownBitsFromICmpCond(V, Cmp, Known, Q, /*Invert=*/false);
1144 }
1145
1146 // Conflicting assumption: Undefined behavior will occur on this execution
1147 // path.
1148 if (Known.hasConflict())
1149 Known.resetAll();
1150}
1151
1152/// Compute known bits from a shift operator, including those with a
1153/// non-constant shift amount. Known is the output of this function. Known2 is a
1154/// pre-allocated temporary with the same bit width as Known and on return
1155/// contains the known bit of the shift value source. KF is an
1156/// operator-specific function that, given the known-bits and a shift amount,
1157/// compute the implied known-bits of the shift operator's result respectively
1158/// for that shift amount. The results from calling KF are conservatively
1159/// combined for all permitted shift amounts.
1161 const Operator *I, const APInt &DemandedElts, KnownBits &Known,
1162 KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth,
1163 function_ref<KnownBits(const KnownBits &, const KnownBits &, bool)> KF) {
1164 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1165 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1166 // To limit compile-time impact, only query isKnownNonZero() if we know at
1167 // least something about the shift amount.
1168 bool ShAmtNonZero =
1169 Known.isNonZero() ||
1170 (Known.getMaxValue().ult(Known.getBitWidth()) &&
1171 isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth + 1));
1172 Known = KF(Known2, Known, ShAmtNonZero);
1173}
1174
1175static KnownBits
1176getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts,
1177 const KnownBits &KnownLHS, const KnownBits &KnownRHS,
1178 const SimplifyQuery &Q, unsigned Depth) {
1179 unsigned BitWidth = KnownLHS.getBitWidth();
1180 KnownBits KnownOut(BitWidth);
1181 bool IsAnd = false;
1182 bool HasKnownOne = !KnownLHS.One.isZero() || !KnownRHS.One.isZero();
1183 Value *X = nullptr, *Y = nullptr;
1184
1185 switch (I->getOpcode()) {
1186 case Instruction::And:
1187 KnownOut = KnownLHS & KnownRHS;
1188 IsAnd = true;
1189 // and(x, -x) is common idioms that will clear all but lowest set
1190 // bit. If we have a single known bit in x, we can clear all bits
1191 // above it.
1192 // TODO: instcombine often reassociates independent `and` which can hide
1193 // this pattern. Try to match and(x, and(-x, y)) / and(and(x, y), -x).
1194 if (HasKnownOne && match(I, m_c_And(m_Value(X), m_Neg(m_Deferred(X))))) {
1195 // -(-x) == x so using whichever (LHS/RHS) gets us a better result.
1196 if (KnownLHS.countMaxTrailingZeros() <= KnownRHS.countMaxTrailingZeros())
1197 KnownOut = KnownLHS.blsi();
1198 else
1199 KnownOut = KnownRHS.blsi();
1200 }
1201 break;
1202 case Instruction::Or:
1203 KnownOut = KnownLHS | KnownRHS;
1204 break;
1205 case Instruction::Xor:
1206 KnownOut = KnownLHS ^ KnownRHS;
1207 // xor(x, x-1) is common idioms that will clear all but lowest set
1208 // bit. If we have a single known bit in x, we can clear all bits
1209 // above it.
1210 // TODO: xor(x, x-1) is often rewritting as xor(x, x-C) where C !=
1211 // -1 but for the purpose of demanded bits (xor(x, x-C) &
1212 // Demanded) == (xor(x, x-1) & Demanded). Extend the xor pattern
1213 // to use arbitrary C if xor(x, x-C) as the same as xor(x, x-1).
1214 if (HasKnownOne &&
1216 const KnownBits &XBits = I->getOperand(0) == X ? KnownLHS : KnownRHS;
1217 KnownOut = XBits.blsmsk();
1218 }
1219 break;
1220 default:
1221 llvm_unreachable("Invalid Op used in 'analyzeKnownBitsFromAndXorOr'");
1222 }
1223
1224 // and(x, add (x, -1)) is a common idiom that always clears the low bit;
1225 // xor/or(x, add (x, -1)) is an idiom that will always set the low bit.
1226 // here we handle the more general case of adding any odd number by
1227 // matching the form and/xor/or(x, add(x, y)) where y is odd.
1228 // TODO: This could be generalized to clearing any bit set in y where the
1229 // following bit is known to be unset in y.
1230 if (!KnownOut.Zero[0] && !KnownOut.One[0] &&
1234 KnownBits KnownY(BitWidth);
1235 computeKnownBits(Y, DemandedElts, KnownY, Q, Depth + 1);
1236 if (KnownY.countMinTrailingOnes() > 0) {
1237 if (IsAnd)
1238 KnownOut.Zero.setBit(0);
1239 else
1240 KnownOut.One.setBit(0);
1241 }
1242 }
1243 return KnownOut;
1244}
1245
1247 const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q,
1248 unsigned Depth,
1249 const function_ref<KnownBits(const KnownBits &, const KnownBits &)>
1250 KnownBitsFunc) {
1251 APInt DemandedEltsLHS, DemandedEltsRHS;
1253 DemandedElts, DemandedEltsLHS,
1254 DemandedEltsRHS);
1255
1256 const auto ComputeForSingleOpFunc =
1257 [Depth, &Q, KnownBitsFunc](const Value *Op, APInt &DemandedEltsOp) {
1258 return KnownBitsFunc(
1259 computeKnownBits(Op, DemandedEltsOp, Q, Depth + 1),
1260 computeKnownBits(Op, DemandedEltsOp << 1, Q, Depth + 1));
1261 };
1262
1263 if (DemandedEltsRHS.isZero())
1264 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS);
1265 if (DemandedEltsLHS.isZero())
1266 return ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS);
1267
1268 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS)
1269 .intersectWith(ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS));
1270}
1271
1272// Public so this can be used in `SimplifyDemandedUseBits`.
1274 const KnownBits &KnownLHS,
1275 const KnownBits &KnownRHS,
1276 const SimplifyQuery &SQ,
1277 unsigned Depth) {
1278 auto *FVTy = dyn_cast<FixedVectorType>(I->getType());
1279 APInt DemandedElts =
1280 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
1281
1282 return getKnownBitsFromAndXorOr(I, DemandedElts, KnownLHS, KnownRHS, SQ,
1283 Depth);
1284}
1285
1287 Attribute Attr = F->getFnAttribute(Attribute::VScaleRange);
1288 // Without vscale_range, we only know that vscale is non-zero.
1289 if (!Attr.isValid())
1291
1292 unsigned AttrMin = Attr.getVScaleRangeMin();
1293 // Minimum is larger than vscale width, result is always poison.
1294 if ((unsigned)llvm::bit_width(AttrMin) > BitWidth)
1295 return ConstantRange::getEmpty(BitWidth);
1296
1297 APInt Min(BitWidth, AttrMin);
1298 std::optional<unsigned> AttrMax = Attr.getVScaleRangeMax();
1299 if (!AttrMax || (unsigned)llvm::bit_width(*AttrMax) > BitWidth)
1301
1302 return ConstantRange(Min, APInt(BitWidth, *AttrMax) + 1);
1303}
1304
1306 Value *Arm, bool Invert,
1307 const SimplifyQuery &Q, unsigned Depth) {
1308 // If we have a constant arm, we are done.
1309 if (Known.isConstant())
1310 return;
1311
1312 // See what condition implies about the bits of the select arm.
1313 KnownBits CondRes(Known.getBitWidth());
1314 computeKnownBitsFromCond(Arm, Cond, CondRes, Q, Invert, Depth + 1);
1315 // If we don't get any information from the condition, no reason to
1316 // proceed.
1317 if (CondRes.isUnknown())
1318 return;
1319
1320 // We can have conflict if the condition is dead. I.e if we have
1321 // (x | 64) < 32 ? (x | 64) : y
1322 // we will have conflict at bit 6 from the condition/the `or`.
1323 // In that case just return. Its not particularly important
1324 // what we do, as this select is going to be simplified soon.
1325 CondRes = CondRes.unionWith(Known);
1326 if (CondRes.hasConflict())
1327 return;
1328
1329 // Finally make sure the information we found is valid. This is relatively
1330 // expensive so it's left for the very end.
1331 if (!isGuaranteedNotToBeUndef(Arm, Q.AC, Q.CxtI, Q.DT, Depth + 1))
1332 return;
1333
1334 // Finally, we know we get information from the condition and its valid,
1335 // so return it.
1336 Known = std::move(CondRes);
1337}
1338
1339// Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow).
1340// Returns the input and lower/upper bounds.
1341static bool isSignedMinMaxClamp(const Value *Select, const Value *&In,
1342 const APInt *&CLow, const APInt *&CHigh) {
1344 cast<Operator>(Select)->getOpcode() == Instruction::Select &&
1345 "Input should be a Select!");
1346
1347 const Value *LHS = nullptr, *RHS = nullptr;
1349 if (SPF != SPF_SMAX && SPF != SPF_SMIN)
1350 return false;
1351
1352 if (!match(RHS, m_APInt(CLow)))
1353 return false;
1354
1355 const Value *LHS2 = nullptr, *RHS2 = nullptr;
1357 if (getInverseMinMaxFlavor(SPF) != SPF2)
1358 return false;
1359
1360 if (!match(RHS2, m_APInt(CHigh)))
1361 return false;
1362
1363 if (SPF == SPF_SMIN)
1364 std::swap(CLow, CHigh);
1365
1366 In = LHS2;
1367 return CLow->sle(*CHigh);
1368}
1369
1371 const APInt *&CLow,
1372 const APInt *&CHigh) {
1373 assert((II->getIntrinsicID() == Intrinsic::smin ||
1374 II->getIntrinsicID() == Intrinsic::smax) &&
1375 "Must be smin/smax");
1376
1377 Intrinsic::ID InverseID = getInverseMinMaxIntrinsic(II->getIntrinsicID());
1378 auto *InnerII = dyn_cast<IntrinsicInst>(II->getArgOperand(0));
1379 if (!InnerII || InnerII->getIntrinsicID() != InverseID ||
1380 !match(II->getArgOperand(1), m_APInt(CLow)) ||
1381 !match(InnerII->getArgOperand(1), m_APInt(CHigh)))
1382 return false;
1383
1384 if (II->getIntrinsicID() == Intrinsic::smin)
1385 std::swap(CLow, CHigh);
1386 return CLow->sle(*CHigh);
1387}
1388
1390 KnownBits &Known) {
1391 const APInt *CLow, *CHigh;
1392 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
1393 Known = Known.unionWith(
1394 ConstantRange::getNonEmpty(*CLow, *CHigh + 1).toKnownBits());
1395}
1396
1398 const APInt &DemandedElts,
1399 KnownBits &Known,
1400 const SimplifyQuery &Q,
1401 unsigned Depth) {
1402 unsigned BitWidth = Known.getBitWidth();
1403
1404 KnownBits Known2(BitWidth);
1405 switch (I->getOpcode()) {
1406 default: break;
1407 case Instruction::Load:
1408 if (MDNode *MD =
1409 Q.IIQ.getMetadata(cast<LoadInst>(I), LLVMContext::MD_range))
1411 break;
1412 case Instruction::And:
1413 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1414 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1415
1416 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1417 break;
1418 case Instruction::Or:
1419 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1420 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1421
1422 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1423 break;
1424 case Instruction::Xor:
1425 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1426 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1427
1428 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1429 break;
1430 case Instruction::Mul: {
1433 computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, NUW,
1434 DemandedElts, Known, Known2, Q, Depth);
1435 break;
1436 }
1437 case Instruction::UDiv: {
1438 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1439 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1440 Known =
1441 KnownBits::udiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1442 break;
1443 }
1444 case Instruction::SDiv: {
1445 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1446 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1447 Known =
1448 KnownBits::sdiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1449 break;
1450 }
1451 case Instruction::Select: {
1452 auto ComputeForArm = [&](Value *Arm, bool Invert) {
1453 KnownBits Res(Known.getBitWidth());
1454 computeKnownBits(Arm, DemandedElts, Res, Q, Depth + 1);
1455 adjustKnownBitsForSelectArm(Res, I->getOperand(0), Arm, Invert, Q, Depth);
1456 return Res;
1457 };
1458 // Only known if known in both the LHS and RHS.
1459 Known =
1460 ComputeForArm(I->getOperand(1), /*Invert=*/false)
1461 .intersectWith(ComputeForArm(I->getOperand(2), /*Invert=*/true));
1462 break;
1463 }
1464 case Instruction::FPTrunc:
1465 case Instruction::FPExt:
1466 case Instruction::FPToUI:
1467 case Instruction::FPToSI:
1468 case Instruction::SIToFP:
1469 case Instruction::UIToFP:
1470 break; // Can't work with floating point.
1471 case Instruction::PtrToInt:
1472 case Instruction::PtrToAddr:
1473 case Instruction::IntToPtr:
1474 // Fall through and handle them the same as zext/trunc.
1475 [[fallthrough]];
1476 case Instruction::ZExt:
1477 case Instruction::Trunc: {
1478 Type *SrcTy = I->getOperand(0)->getType();
1479
1480 unsigned SrcBitWidth;
1481 // Note that we handle pointer operands here because of inttoptr/ptrtoint
1482 // which fall through here.
1483 Type *ScalarTy = SrcTy->getScalarType();
1484 SrcBitWidth = ScalarTy->isPointerTy() ?
1485 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
1486 Q.DL.getTypeSizeInBits(ScalarTy);
1487
1488 assert(SrcBitWidth && "SrcBitWidth can't be zero");
1489 Known = Known.anyextOrTrunc(SrcBitWidth);
1490 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1491 if (auto *Inst = dyn_cast<PossiblyNonNegInst>(I);
1492 Inst && Inst->hasNonNeg() && !Known.isNegative())
1493 Known.makeNonNegative();
1494 Known = Known.zextOrTrunc(BitWidth);
1495 break;
1496 }
1497 case Instruction::BitCast: {
1498 Type *SrcTy = I->getOperand(0)->getType();
1499 if (SrcTy->isIntOrPtrTy() &&
1500 // TODO: For now, not handling conversions like:
1501 // (bitcast i64 %x to <2 x i32>)
1502 !I->getType()->isVectorTy()) {
1503 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1504 break;
1505 }
1506
1507 const Value *V;
1508 // Handle bitcast from floating point to integer.
1509 if (match(I, m_ElementWiseBitCast(m_Value(V))) &&
1510 V->getType()->isFPOrFPVectorTy()) {
1511 Type *FPType = V->getType()->getScalarType();
1512 KnownFPClass Result =
1513 computeKnownFPClass(V, DemandedElts, fcAllFlags, Q, Depth + 1);
1514 FPClassTest FPClasses = Result.KnownFPClasses;
1515
1516 // TODO: Treat it as zero/poison if the use of I is unreachable.
1517 if (FPClasses == fcNone)
1518 break;
1519
1520 if (Result.isKnownNever(fcNormal | fcSubnormal | fcNan)) {
1521 Known.setAllConflict();
1522
1523 if (FPClasses & fcInf)
1525 APFloat::getInf(FPType->getFltSemantics()).bitcastToAPInt()));
1526
1527 if (FPClasses & fcZero)
1529 APInt::getZero(FPType->getScalarSizeInBits())));
1530
1531 Known.Zero.clearSignBit();
1532 Known.One.clearSignBit();
1533 }
1534
1535 if (Result.SignBit) {
1536 if (*Result.SignBit)
1537 Known.makeNegative();
1538 else
1539 Known.makeNonNegative();
1540 }
1541
1542 break;
1543 }
1544
1545 // Handle cast from vector integer type to scalar or vector integer.
1546 auto *SrcVecTy = dyn_cast<FixedVectorType>(SrcTy);
1547 if (!SrcVecTy || !SrcVecTy->getElementType()->isIntegerTy() ||
1548 !I->getType()->isIntOrIntVectorTy() ||
1549 isa<ScalableVectorType>(I->getType()))
1550 break;
1551
1552 unsigned NumElts = DemandedElts.getBitWidth();
1553 bool IsLE = Q.DL.isLittleEndian();
1554 // Look through a cast from narrow vector elements to wider type.
1555 // Examples: v4i32 -> v2i64, v3i8 -> v24
1556 unsigned SubBitWidth = SrcVecTy->getScalarSizeInBits();
1557 if (BitWidth % SubBitWidth == 0) {
1558 // Known bits are automatically intersected across demanded elements of a
1559 // vector. So for example, if a bit is computed as known zero, it must be
1560 // zero across all demanded elements of the vector.
1561 //
1562 // For this bitcast, each demanded element of the output is sub-divided
1563 // across a set of smaller vector elements in the source vector. To get
1564 // the known bits for an entire element of the output, compute the known
1565 // bits for each sub-element sequentially. This is done by shifting the
1566 // one-set-bit demanded elements parameter across the sub-elements for
1567 // consecutive calls to computeKnownBits. We are using the demanded
1568 // elements parameter as a mask operator.
1569 //
1570 // The known bits of each sub-element are then inserted into place
1571 // (dependent on endian) to form the full result of known bits.
1572 unsigned SubScale = BitWidth / SubBitWidth;
1573 APInt SubDemandedElts = APInt::getZero(NumElts * SubScale);
1574 for (unsigned i = 0; i != NumElts; ++i) {
1575 if (DemandedElts[i])
1576 SubDemandedElts.setBit(i * SubScale);
1577 }
1578
1579 KnownBits KnownSrc(SubBitWidth);
1580 for (unsigned i = 0; i != SubScale; ++i) {
1581 computeKnownBits(I->getOperand(0), SubDemandedElts.shl(i), KnownSrc, Q,
1582 Depth + 1);
1583 unsigned ShiftElt = IsLE ? i : SubScale - 1 - i;
1584 Known.insertBits(KnownSrc, ShiftElt * SubBitWidth);
1585 }
1586 }
1587 // Look through a cast from wider vector elements to narrow type.
1588 // Examples: v2i64 -> v4i32
1589 if (SubBitWidth % BitWidth == 0) {
1590 unsigned SubScale = SubBitWidth / BitWidth;
1591 KnownBits KnownSrc(SubBitWidth);
1592 APInt SubDemandedElts =
1593 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
1594 computeKnownBits(I->getOperand(0), SubDemandedElts, KnownSrc, Q,
1595 Depth + 1);
1596
1597 Known.setAllConflict();
1598 for (unsigned i = 0; i != NumElts; ++i) {
1599 if (DemandedElts[i]) {
1600 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
1601 unsigned Offset = (Shifts % SubScale) * BitWidth;
1602 Known = Known.intersectWith(KnownSrc.extractBits(BitWidth, Offset));
1603 if (Known.isUnknown())
1604 break;
1605 }
1606 }
1607 }
1608 break;
1609 }
1610 case Instruction::SExt: {
1611 // Compute the bits in the result that are not present in the input.
1612 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
1613
1614 Known = Known.trunc(SrcBitWidth);
1615 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1616 // If the sign bit of the input is known set or clear, then we know the
1617 // top bits of the result.
1618 Known = Known.sext(BitWidth);
1619 break;
1620 }
1621 case Instruction::Shl: {
1624 auto KF = [NUW, NSW](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1625 bool ShAmtNonZero) {
1626 return KnownBits::shl(KnownVal, KnownAmt, NUW, NSW, ShAmtNonZero);
1627 };
1628 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1629 KF);
1630 // Trailing zeros of a right-shifted constant never decrease.
1631 const APInt *C;
1632 if (match(I->getOperand(0), m_APInt(C)))
1633 Known.Zero.setLowBits(C->countr_zero());
1634 break;
1635 }
1636 case Instruction::LShr: {
1637 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1638 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1639 bool ShAmtNonZero) {
1640 return KnownBits::lshr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1641 };
1642 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1643 KF);
1644 // Leading zeros of a left-shifted constant never decrease.
1645 const APInt *C;
1646 if (match(I->getOperand(0), m_APInt(C)))
1647 Known.Zero.setHighBits(C->countl_zero());
1648 break;
1649 }
1650 case Instruction::AShr: {
1651 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1652 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1653 bool ShAmtNonZero) {
1654 return KnownBits::ashr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1655 };
1656 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1657 KF);
1658 break;
1659 }
1660 case Instruction::Sub: {
1663 computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW, NUW,
1664 DemandedElts, Known, Known2, Q, Depth);
1665 break;
1666 }
1667 case Instruction::Add: {
1670 computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW, NUW,
1671 DemandedElts, Known, Known2, Q, Depth);
1672 break;
1673 }
1674 case Instruction::SRem:
1675 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1676 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1677 Known = KnownBits::srem(Known, Known2);
1678 break;
1679
1680 case Instruction::URem:
1681 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1682 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1683 Known = KnownBits::urem(Known, Known2);
1684 break;
1685 case Instruction::Alloca:
1687 break;
1688 case Instruction::GetElementPtr: {
1689 // Analyze all of the subscripts of this getelementptr instruction
1690 // to determine if we can prove known low zero bits.
1691 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1692 // Accumulate the constant indices in a separate variable
1693 // to minimize the number of calls to computeForAddSub.
1694 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(I->getType());
1695 APInt AccConstIndices(IndexWidth, 0);
1696
1697 auto AddIndexToKnown = [&](KnownBits IndexBits) {
1698 if (IndexWidth == BitWidth) {
1699 // Note that inbounds does *not* guarantee nsw for the addition, as only
1700 // the offset is signed, while the base address is unsigned.
1701 Known = KnownBits::add(Known, IndexBits);
1702 } else {
1703 // If the index width is smaller than the pointer width, only add the
1704 // value to the low bits.
1705 assert(IndexWidth < BitWidth &&
1706 "Index width can't be larger than pointer width");
1707 Known.insertBits(KnownBits::add(Known.trunc(IndexWidth), IndexBits), 0);
1708 }
1709 };
1710
1712 for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
1713 // TrailZ can only become smaller, short-circuit if we hit zero.
1714 if (Known.isUnknown())
1715 break;
1716
1717 Value *Index = I->getOperand(i);
1718
1719 // Handle case when index is zero.
1720 Constant *CIndex = dyn_cast<Constant>(Index);
1721 if (CIndex && CIndex->isNullValue())
1722 continue;
1723
1724 if (StructType *STy = GTI.getStructTypeOrNull()) {
1725 // Handle struct member offset arithmetic.
1726
1727 assert(CIndex &&
1728 "Access to structure field must be known at compile time");
1729
1730 if (CIndex->getType()->isVectorTy())
1731 Index = CIndex->getSplatValue();
1732
1733 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
1734 const StructLayout *SL = Q.DL.getStructLayout(STy);
1735 uint64_t Offset = SL->getElementOffset(Idx);
1736 AccConstIndices += Offset;
1737 continue;
1738 }
1739
1740 // Handle array index arithmetic.
1741 Type *IndexedTy = GTI.getIndexedType();
1742 if (!IndexedTy->isSized()) {
1743 Known.resetAll();
1744 break;
1745 }
1746
1747 TypeSize Stride = GTI.getSequentialElementStride(Q.DL);
1748 uint64_t StrideInBytes = Stride.getKnownMinValue();
1749 if (!Stride.isScalable()) {
1750 // Fast path for constant offset.
1751 if (auto *CI = dyn_cast<ConstantInt>(Index)) {
1752 AccConstIndices +=
1753 CI->getValue().sextOrTrunc(IndexWidth) * StrideInBytes;
1754 continue;
1755 }
1756 }
1757
1758 KnownBits IndexBits =
1759 computeKnownBits(Index, Q, Depth + 1).sextOrTrunc(IndexWidth);
1760 KnownBits ScalingFactor(IndexWidth);
1761 // Multiply by current sizeof type.
1762 // &A[i] == A + i * sizeof(*A[i]).
1763 if (Stride.isScalable()) {
1764 // For scalable types the only thing we know about sizeof is
1765 // that this is a multiple of the minimum size.
1766 ScalingFactor.Zero.setLowBits(llvm::countr_zero(StrideInBytes));
1767 } else {
1768 ScalingFactor =
1769 KnownBits::makeConstant(APInt(IndexWidth, StrideInBytes));
1770 }
1771 AddIndexToKnown(KnownBits::mul(IndexBits, ScalingFactor));
1772 }
1773 if (!Known.isUnknown() && !AccConstIndices.isZero())
1774 AddIndexToKnown(KnownBits::makeConstant(AccConstIndices));
1775 break;
1776 }
1777 case Instruction::PHI: {
1778 const PHINode *P = cast<PHINode>(I);
1779 BinaryOperator *BO = nullptr;
1780 Value *R = nullptr, *L = nullptr;
1781 if (matchSimpleRecurrence(P, BO, R, L)) {
1782 // Handle the case of a simple two-predecessor recurrence PHI.
1783 // There's a lot more that could theoretically be done here, but
1784 // this is sufficient to catch some interesting cases.
1785 unsigned Opcode = BO->getOpcode();
1786
1787 switch (Opcode) {
1788 // If this is a shift recurrence, we know the bits being shifted in. We
1789 // can combine that with information about the start value of the
1790 // recurrence to conclude facts about the result. If this is a udiv
1791 // recurrence, we know that the result can never exceed either the
1792 // numerator or the start value, whichever is greater.
1793 case Instruction::LShr:
1794 case Instruction::AShr:
1795 case Instruction::Shl:
1796 case Instruction::UDiv:
1797 if (BO->getOperand(0) != I)
1798 break;
1799 [[fallthrough]];
1800
1801 // For a urem recurrence, the result can never exceed the start value. The
1802 // phi could either be the numerator or the denominator.
1803 case Instruction::URem: {
1804 // We have matched a recurrence of the form:
1805 // %iv = [R, %entry], [%iv.next, %backedge]
1806 // %iv.next = shift_op %iv, L
1807
1808 // Recurse with the phi context to avoid concern about whether facts
1809 // inferred hold at original context instruction. TODO: It may be
1810 // correct to use the original context. IF warranted, explore and
1811 // add sufficient tests to cover.
1813 RecQ.CxtI = P;
1814 computeKnownBits(R, DemandedElts, Known2, RecQ, Depth + 1);
1815 switch (Opcode) {
1816 case Instruction::Shl:
1817 // A shl recurrence will only increase the tailing zeros
1818 Known.Zero.setLowBits(Known2.countMinTrailingZeros());
1819 break;
1820 case Instruction::LShr:
1821 case Instruction::UDiv:
1822 case Instruction::URem:
1823 // lshr, udiv, and urem recurrences will preserve the leading zeros of
1824 // the start value.
1825 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1826 break;
1827 case Instruction::AShr:
1828 // An ashr recurrence will extend the initial sign bit
1829 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1830 Known.One.setHighBits(Known2.countMinLeadingOnes());
1831 break;
1832 }
1833 break;
1834 }
1835
1836 // Check for operations that have the property that if
1837 // both their operands have low zero bits, the result
1838 // will have low zero bits.
1839 case Instruction::Add:
1840 case Instruction::Sub:
1841 case Instruction::And:
1842 case Instruction::Or:
1843 case Instruction::Mul: {
1844 // Change the context instruction to the "edge" that flows into the
1845 // phi. This is important because that is where the value is actually
1846 // "evaluated" even though it is used later somewhere else. (see also
1847 // D69571).
1849
1850 unsigned OpNum = P->getOperand(0) == R ? 0 : 1;
1851 Instruction *RInst = P->getIncomingBlock(OpNum)->getTerminator();
1852 Instruction *LInst = P->getIncomingBlock(1 - OpNum)->getTerminator();
1853
1854 // Ok, we have a PHI of the form L op= R. Check for low
1855 // zero bits.
1856 RecQ.CxtI = RInst;
1857 computeKnownBits(R, DemandedElts, Known2, RecQ, Depth + 1);
1858
1859 // We need to take the minimum number of known bits
1860 KnownBits Known3(BitWidth);
1861 RecQ.CxtI = LInst;
1862 computeKnownBits(L, DemandedElts, Known3, RecQ, Depth + 1);
1863
1864 Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(),
1865 Known3.countMinTrailingZeros()));
1866
1867 auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(BO);
1868 if (!OverflowOp || !Q.IIQ.hasNoSignedWrap(OverflowOp))
1869 break;
1870
1871 switch (Opcode) {
1872 // If initial value of recurrence is nonnegative, and we are adding
1873 // a nonnegative number with nsw, the result can only be nonnegative
1874 // or poison value regardless of the number of times we execute the
1875 // add in phi recurrence. If initial value is negative and we are
1876 // adding a negative number with nsw, the result can only be
1877 // negative or poison value. Similar arguments apply to sub and mul.
1878 //
1879 // (add non-negative, non-negative) --> non-negative
1880 // (add negative, negative) --> negative
1881 case Instruction::Add: {
1882 if (Known2.isNonNegative() && Known3.isNonNegative())
1883 Known.makeNonNegative();
1884 else if (Known2.isNegative() && Known3.isNegative())
1885 Known.makeNegative();
1886 break;
1887 }
1888
1889 // (sub nsw non-negative, negative) --> non-negative
1890 // (sub nsw negative, non-negative) --> negative
1891 case Instruction::Sub: {
1892 if (BO->getOperand(0) != I)
1893 break;
1894 if (Known2.isNonNegative() && Known3.isNegative())
1895 Known.makeNonNegative();
1896 else if (Known2.isNegative() && Known3.isNonNegative())
1897 Known.makeNegative();
1898 break;
1899 }
1900
1901 // (mul nsw non-negative, non-negative) --> non-negative
1902 case Instruction::Mul:
1903 if (Known2.isNonNegative() && Known3.isNonNegative())
1904 Known.makeNonNegative();
1905 break;
1906
1907 default:
1908 break;
1909 }
1910 break;
1911 }
1912
1913 default:
1914 break;
1915 }
1916 }
1917
1918 // Unreachable blocks may have zero-operand PHI nodes.
1919 if (P->getNumIncomingValues() == 0)
1920 break;
1921
1922 // Otherwise take the unions of the known bit sets of the operands,
1923 // taking conservative care to avoid excessive recursion.
1924 if (Depth < MaxAnalysisRecursionDepth - 1 && Known.isUnknown()) {
1925 // Skip if every incoming value references to ourself.
1926 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
1927 break;
1928
1929 Known.setAllConflict();
1930 for (const Use &U : P->operands()) {
1931 Value *IncValue;
1932 const PHINode *CxtPhi;
1933 Instruction *CxtI;
1934 breakSelfRecursivePHI(&U, P, IncValue, CxtI, &CxtPhi);
1935 // Skip direct self references.
1936 if (IncValue == P)
1937 continue;
1938
1939 // Change the context instruction to the "edge" that flows into the
1940 // phi. This is important because that is where the value is actually
1941 // "evaluated" even though it is used later somewhere else. (see also
1942 // D69571).
1944
1945 Known2 = KnownBits(BitWidth);
1946
1947 // Recurse, but cap the recursion to one level, because we don't
1948 // want to waste time spinning around in loops.
1949 // TODO: See if we can base recursion limiter on number of incoming phi
1950 // edges so we don't overly clamp analysis.
1951 computeKnownBits(IncValue, DemandedElts, Known2, RecQ,
1953
1954 // See if we can further use a conditional branch into the phi
1955 // to help us determine the range of the value.
1956 if (!Known2.isConstant()) {
1957 CmpPredicate Pred;
1958 const APInt *RHSC;
1959 BasicBlock *TrueSucc, *FalseSucc;
1960 // TODO: Use RHS Value and compute range from its known bits.
1961 if (match(RecQ.CxtI,
1962 m_Br(m_c_ICmp(Pred, m_Specific(IncValue), m_APInt(RHSC)),
1963 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
1964 // Check for cases of duplicate successors.
1965 if ((TrueSucc == CxtPhi->getParent()) !=
1966 (FalseSucc == CxtPhi->getParent())) {
1967 // If we're using the false successor, invert the predicate.
1968 if (FalseSucc == CxtPhi->getParent())
1969 Pred = CmpInst::getInversePredicate(Pred);
1970 // Get the knownbits implied by the incoming phi condition.
1971 auto CR = ConstantRange::makeExactICmpRegion(Pred, *RHSC);
1972 KnownBits KnownUnion = Known2.unionWith(CR.toKnownBits());
1973 // We can have conflicts here if we are analyzing deadcode (its
1974 // impossible for us reach this BB based the icmp).
1975 if (KnownUnion.hasConflict()) {
1976 // No reason to continue analyzing in a known dead region, so
1977 // just resetAll and break. This will cause us to also exit the
1978 // outer loop.
1979 Known.resetAll();
1980 break;
1981 }
1982 Known2 = KnownUnion;
1983 }
1984 }
1985 }
1986
1987 Known = Known.intersectWith(Known2);
1988 // If all bits have been ruled out, there's no need to check
1989 // more operands.
1990 if (Known.isUnknown())
1991 break;
1992 }
1993 }
1994 break;
1995 }
1996 case Instruction::Call:
1997 case Instruction::Invoke: {
1998 // If range metadata is attached to this call, set known bits from that,
1999 // and then intersect with known bits based on other properties of the
2000 // function.
2001 if (MDNode *MD =
2002 Q.IIQ.getMetadata(cast<Instruction>(I), LLVMContext::MD_range))
2004
2005 const auto *CB = cast<CallBase>(I);
2006
2007 if (std::optional<ConstantRange> Range = CB->getRange())
2008 Known = Known.unionWith(Range->toKnownBits());
2009
2010 if (const Value *RV = CB->getReturnedArgOperand()) {
2011 if (RV->getType() == I->getType()) {
2012 computeKnownBits(RV, Known2, Q, Depth + 1);
2013 Known = Known.unionWith(Known2);
2014 // If the function doesn't return properly for all input values
2015 // (e.g. unreachable exits) then there might be conflicts between the
2016 // argument value and the range metadata. Simply discard the known bits
2017 // in case of conflicts.
2018 if (Known.hasConflict())
2019 Known.resetAll();
2020 }
2021 }
2022 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
2023 switch (II->getIntrinsicID()) {
2024 default:
2025 break;
2026 case Intrinsic::abs: {
2027 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2028 bool IntMinIsPoison = match(II->getArgOperand(1), m_One());
2029 Known = Known.unionWith(Known2.abs(IntMinIsPoison));
2030 break;
2031 }
2032 case Intrinsic::bitreverse:
2033 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2034 Known = Known.unionWith(Known2.reverseBits());
2035 break;
2036 case Intrinsic::bswap:
2037 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2038 Known = Known.unionWith(Known2.byteSwap());
2039 break;
2040 case Intrinsic::ctlz: {
2041 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2042 // If we have a known 1, its position is our upper bound.
2043 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
2044 // If this call is poison for 0 input, the result will be less than 2^n.
2045 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
2046 PossibleLZ = std::min(PossibleLZ, BitWidth - 1);
2047 unsigned LowBits = llvm::bit_width(PossibleLZ);
2048 Known.Zero.setBitsFrom(LowBits);
2049 break;
2050 }
2051 case Intrinsic::cttz: {
2052 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2053 // If we have a known 1, its position is our upper bound.
2054 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
2055 // If this call is poison for 0 input, the result will be less than 2^n.
2056 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
2057 PossibleTZ = std::min(PossibleTZ, BitWidth - 1);
2058 unsigned LowBits = llvm::bit_width(PossibleTZ);
2059 Known.Zero.setBitsFrom(LowBits);
2060 break;
2061 }
2062 case Intrinsic::ctpop: {
2063 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2064 // We can bound the space the count needs. Also, bits known to be zero
2065 // can't contribute to the population.
2066 unsigned BitsPossiblySet = Known2.countMaxPopulation();
2067 unsigned LowBits = llvm::bit_width(BitsPossiblySet);
2068 Known.Zero.setBitsFrom(LowBits);
2069 // TODO: we could bound KnownOne using the lower bound on the number
2070 // of bits which might be set provided by popcnt KnownOne2.
2071 break;
2072 }
2073 case Intrinsic::fshr:
2074 case Intrinsic::fshl: {
2075 const APInt *SA;
2076 if (!match(I->getOperand(2), m_APInt(SA)))
2077 break;
2078
2079 KnownBits Known3(BitWidth);
2080 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2081 computeKnownBits(I->getOperand(1), DemandedElts, Known3, Q, Depth + 1);
2082 Known = II->getIntrinsicID() == Intrinsic::fshl
2083 ? KnownBits::fshl(Known2, Known3, *SA)
2084 : KnownBits::fshr(Known2, Known3, *SA);
2085 break;
2086 }
2087 case Intrinsic::clmul:
2088 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2089 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2090 Known = KnownBits::clmul(Known, Known2);
2091 break;
2092 case Intrinsic::uadd_sat:
2093 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2094 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2095 Known = KnownBits::uadd_sat(Known, Known2);
2096 break;
2097 case Intrinsic::usub_sat:
2098 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2099 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2100 Known = KnownBits::usub_sat(Known, Known2);
2101 break;
2102 case Intrinsic::sadd_sat:
2103 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2104 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2105 Known = KnownBits::sadd_sat(Known, Known2);
2106 break;
2107 case Intrinsic::ssub_sat:
2108 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2109 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2110 Known = KnownBits::ssub_sat(Known, Known2);
2111 break;
2112 // Vec reverse preserves bits from input vec.
2113 case Intrinsic::vector_reverse:
2114 computeKnownBits(I->getOperand(0), DemandedElts.reverseBits(), Known, Q,
2115 Depth + 1);
2116 break;
2117 // for min/max/and/or reduce, any bit common to each element in the
2118 // input vec is set in the output.
2119 case Intrinsic::vector_reduce_and:
2120 case Intrinsic::vector_reduce_or:
2121 case Intrinsic::vector_reduce_umax:
2122 case Intrinsic::vector_reduce_umin:
2123 case Intrinsic::vector_reduce_smax:
2124 case Intrinsic::vector_reduce_smin:
2125 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2126 break;
2127 case Intrinsic::vector_reduce_xor: {
2128 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2129 // The zeros common to all vecs are zero in the output.
2130 // If the number of elements is odd, then the common ones remain. If the
2131 // number of elements is even, then the common ones becomes zeros.
2132 auto *VecTy = cast<VectorType>(I->getOperand(0)->getType());
2133 // Even, so the ones become zeros.
2134 bool EvenCnt = VecTy->getElementCount().isKnownEven();
2135 if (EvenCnt)
2136 Known.Zero |= Known.One;
2137 // Maybe even element count so need to clear ones.
2138 if (VecTy->isScalableTy() || EvenCnt)
2139 Known.One.clearAllBits();
2140 break;
2141 }
2142 case Intrinsic::vector_reduce_add: {
2143 auto *VecTy = dyn_cast<FixedVectorType>(I->getOperand(0)->getType());
2144 if (!VecTy)
2145 break;
2146 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2147 Known = Known.reduceAdd(VecTy->getNumElements());
2148 break;
2149 }
2150 case Intrinsic::umin:
2151 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2152 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2153 Known = KnownBits::umin(Known, Known2);
2154 break;
2155 case Intrinsic::umax:
2156 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2157 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2158 Known = KnownBits::umax(Known, Known2);
2159 break;
2160 case Intrinsic::smin:
2161 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2162 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2163 Known = KnownBits::smin(Known, Known2);
2165 break;
2166 case Intrinsic::smax:
2167 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2168 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2169 Known = KnownBits::smax(Known, Known2);
2171 break;
2172 case Intrinsic::ptrmask: {
2173 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2174
2175 const Value *Mask = I->getOperand(1);
2176 Known2 = KnownBits(Mask->getType()->getScalarSizeInBits());
2177 computeKnownBits(Mask, DemandedElts, Known2, Q, Depth + 1);
2178 // TODO: 1-extend would be more precise.
2179 Known &= Known2.anyextOrTrunc(BitWidth);
2180 break;
2181 }
2182 case Intrinsic::x86_sse2_pmulh_w:
2183 case Intrinsic::x86_avx2_pmulh_w:
2184 case Intrinsic::x86_avx512_pmulh_w_512:
2185 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2186 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2187 Known = KnownBits::mulhs(Known, Known2);
2188 break;
2189 case Intrinsic::x86_sse2_pmulhu_w:
2190 case Intrinsic::x86_avx2_pmulhu_w:
2191 case Intrinsic::x86_avx512_pmulhu_w_512:
2192 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2193 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2194 Known = KnownBits::mulhu(Known, Known2);
2195 break;
2196 case Intrinsic::x86_sse42_crc32_64_64:
2197 Known.Zero.setBitsFrom(32);
2198 break;
2199 case Intrinsic::x86_ssse3_phadd_d_128:
2200 case Intrinsic::x86_ssse3_phadd_w_128:
2201 case Intrinsic::x86_avx2_phadd_d:
2202 case Intrinsic::x86_avx2_phadd_w: {
2204 I, DemandedElts, Q, Depth,
2205 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2206 return KnownBits::add(KnownLHS, KnownRHS);
2207 });
2208 break;
2209 }
2210 case Intrinsic::x86_ssse3_phadd_sw_128:
2211 case Intrinsic::x86_avx2_phadd_sw: {
2213 I, DemandedElts, Q, Depth, KnownBits::sadd_sat);
2214 break;
2215 }
2216 case Intrinsic::x86_ssse3_phsub_d_128:
2217 case Intrinsic::x86_ssse3_phsub_w_128:
2218 case Intrinsic::x86_avx2_phsub_d:
2219 case Intrinsic::x86_avx2_phsub_w: {
2221 I, DemandedElts, Q, Depth,
2222 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2223 return KnownBits::sub(KnownLHS, KnownRHS);
2224 });
2225 break;
2226 }
2227 case Intrinsic::x86_ssse3_phsub_sw_128:
2228 case Intrinsic::x86_avx2_phsub_sw: {
2230 I, DemandedElts, Q, Depth, KnownBits::ssub_sat);
2231 break;
2232 }
2233 case Intrinsic::riscv_vsetvli:
2234 case Intrinsic::riscv_vsetvlimax: {
2235 bool HasAVL = II->getIntrinsicID() == Intrinsic::riscv_vsetvli;
2236 const ConstantRange Range = getVScaleRange(II->getFunction(), BitWidth);
2238 cast<ConstantInt>(II->getArgOperand(HasAVL))->getZExtValue());
2239 RISCVVType::VLMUL VLMUL = static_cast<RISCVVType::VLMUL>(
2240 cast<ConstantInt>(II->getArgOperand(1 + HasAVL))->getZExtValue());
2241 uint64_t MaxVLEN =
2242 Range.getUnsignedMax().getZExtValue() * RISCV::RVVBitsPerBlock;
2243 uint64_t MaxVL = MaxVLEN / RISCVVType::getSEWLMULRatio(SEW, VLMUL);
2244
2245 // Result of vsetvli must be not larger than AVL.
2246 if (HasAVL)
2247 if (auto *CI = dyn_cast<ConstantInt>(II->getArgOperand(0)))
2248 MaxVL = std::min(MaxVL, CI->getZExtValue());
2249
2250 unsigned KnownZeroFirstBit = Log2_32(MaxVL) + 1;
2251 if (BitWidth > KnownZeroFirstBit)
2252 Known.Zero.setBitsFrom(KnownZeroFirstBit);
2253 break;
2254 }
2255 case Intrinsic::amdgcn_mbcnt_hi:
2256 case Intrinsic::amdgcn_mbcnt_lo: {
2257 // Wave64 mbcnt_lo returns at most 32 + src1. Otherwise these return at
2258 // most 31 + src1.
2259 Known.Zero.setBitsFrom(
2260 II->getIntrinsicID() == Intrinsic::amdgcn_mbcnt_lo ? 6 : 5);
2261 computeKnownBits(I->getOperand(1), Known2, Q, Depth + 1);
2262 Known = KnownBits::add(Known, Known2);
2263 break;
2264 }
2265 case Intrinsic::vscale: {
2266 if (!II->getParent() || !II->getFunction())
2267 break;
2268
2269 Known = getVScaleRange(II->getFunction(), BitWidth).toKnownBits();
2270 break;
2271 }
2272 }
2273 }
2274 break;
2275 }
2276 case Instruction::ShuffleVector: {
2277 if (auto *Splat = getSplatValue(I)) {
2278 computeKnownBits(Splat, Known, Q, Depth + 1);
2279 break;
2280 }
2281
2282 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
2283 // FIXME: Do we need to handle ConstantExpr involving shufflevectors?
2284 if (!Shuf) {
2285 Known.resetAll();
2286 return;
2287 }
2288 // For undef elements, we don't know anything about the common state of
2289 // the shuffle result.
2290 APInt DemandedLHS, DemandedRHS;
2291 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) {
2292 Known.resetAll();
2293 return;
2294 }
2295 Known.setAllConflict();
2296 if (!!DemandedLHS) {
2297 const Value *LHS = Shuf->getOperand(0);
2298 computeKnownBits(LHS, DemandedLHS, Known, Q, Depth + 1);
2299 // If we don't know any bits, early out.
2300 if (Known.isUnknown())
2301 break;
2302 }
2303 if (!!DemandedRHS) {
2304 const Value *RHS = Shuf->getOperand(1);
2305 computeKnownBits(RHS, DemandedRHS, Known2, Q, Depth + 1);
2306 Known = Known.intersectWith(Known2);
2307 }
2308 break;
2309 }
2310 case Instruction::InsertElement: {
2311 if (isa<ScalableVectorType>(I->getType())) {
2312 Known.resetAll();
2313 return;
2314 }
2315 const Value *Vec = I->getOperand(0);
2316 const Value *Elt = I->getOperand(1);
2317 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
2318 unsigned NumElts = DemandedElts.getBitWidth();
2319 APInt DemandedVecElts = DemandedElts;
2320 bool NeedsElt = true;
2321 // If we know the index we are inserting too, clear it from Vec check.
2322 if (CIdx && CIdx->getValue().ult(NumElts)) {
2323 DemandedVecElts.clearBit(CIdx->getZExtValue());
2324 NeedsElt = DemandedElts[CIdx->getZExtValue()];
2325 }
2326
2327 Known.setAllConflict();
2328 if (NeedsElt) {
2329 computeKnownBits(Elt, Known, Q, Depth + 1);
2330 // If we don't know any bits, early out.
2331 if (Known.isUnknown())
2332 break;
2333 }
2334
2335 if (!DemandedVecElts.isZero()) {
2336 computeKnownBits(Vec, DemandedVecElts, Known2, Q, Depth + 1);
2337 Known = Known.intersectWith(Known2);
2338 }
2339 break;
2340 }
2341 case Instruction::ExtractElement: {
2342 // Look through extract element. If the index is non-constant or
2343 // out-of-range demand all elements, otherwise just the extracted element.
2344 const Value *Vec = I->getOperand(0);
2345 const Value *Idx = I->getOperand(1);
2346 auto *CIdx = dyn_cast<ConstantInt>(Idx);
2347 if (isa<ScalableVectorType>(Vec->getType())) {
2348 // FIXME: there's probably *something* we can do with scalable vectors
2349 Known.resetAll();
2350 break;
2351 }
2352 unsigned NumElts = cast<FixedVectorType>(Vec->getType())->getNumElements();
2353 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
2354 if (CIdx && CIdx->getValue().ult(NumElts))
2355 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
2356 computeKnownBits(Vec, DemandedVecElts, Known, Q, Depth + 1);
2357 break;
2358 }
2359 case Instruction::ExtractValue:
2360 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
2362 if (EVI->getNumIndices() != 1) break;
2363 if (EVI->getIndices()[0] == 0) {
2364 switch (II->getIntrinsicID()) {
2365 default: break;
2366 case Intrinsic::uadd_with_overflow:
2367 case Intrinsic::sadd_with_overflow:
2369 true, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2370 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2371 break;
2372 case Intrinsic::usub_with_overflow:
2373 case Intrinsic::ssub_with_overflow:
2375 false, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2376 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2377 break;
2378 case Intrinsic::umul_with_overflow:
2379 case Intrinsic::smul_with_overflow:
2380 computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false,
2381 false, DemandedElts, Known, Known2, Q, Depth);
2382 break;
2383 }
2384 }
2385 }
2386 break;
2387 case Instruction::Freeze:
2388 if (isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
2389 Depth + 1))
2390 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2391 break;
2392 }
2393}
2394
2395/// Determine which bits of V are known to be either zero or one and return
2396/// them.
2397KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
2398 const SimplifyQuery &Q, unsigned Depth) {
2399 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2400 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);
2401 return Known;
2402}
2403
2404/// Determine which bits of V are known to be either zero or one and return
2405/// them.
2407 unsigned Depth) {
2408 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2409 computeKnownBits(V, Known, Q, Depth);
2410 return Known;
2411}
2412
2413/// Determine which bits of V are known to be either zero or one and return
2414/// them in the Known bit set.
2415///
2416/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that
2417/// we cannot optimize based on the assumption that it is zero without changing
2418/// it to be an explicit zero. If we don't change it to zero, other code could
2419/// optimized based on the contradictory assumption that it is non-zero.
2420/// Because instcombine aggressively folds operations with undef args anyway,
2421/// this won't lose us code quality.
2422///
2423/// This function is defined on values with integer type, values with pointer
2424/// type, and vectors of integers. In the case
2425/// where V is a vector, known zero, and known one values are the
2426/// same width as the vector element, and the bit is set only if it is true
2427/// for all of the demanded elements in the vector specified by DemandedElts.
2428void computeKnownBits(const Value *V, const APInt &DemandedElts,
2429 KnownBits &Known, const SimplifyQuery &Q,
2430 unsigned Depth) {
2431 if (!DemandedElts) {
2432 // No demanded elts, better to assume we don't know anything.
2433 Known.resetAll();
2434 return;
2435 }
2436
2437 assert(V && "No Value?");
2438 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2439
2440#ifndef NDEBUG
2441 Type *Ty = V->getType();
2442 unsigned BitWidth = Known.getBitWidth();
2443
2444 assert((Ty->isIntOrIntVectorTy(BitWidth) || Ty->isPtrOrPtrVectorTy()) &&
2445 "Not integer or pointer type!");
2446
2447 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
2448 assert(
2449 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
2450 "DemandedElt width should equal the fixed vector number of elements");
2451 } else {
2452 assert(DemandedElts == APInt(1, 1) &&
2453 "DemandedElt width should be 1 for scalars or scalable vectors");
2454 }
2455
2456 Type *ScalarTy = Ty->getScalarType();
2457 if (ScalarTy->isPointerTy()) {
2458 assert(BitWidth == Q.DL.getPointerTypeSizeInBits(ScalarTy) &&
2459 "V and Known should have same BitWidth");
2460 } else {
2461 assert(BitWidth == Q.DL.getTypeSizeInBits(ScalarTy) &&
2462 "V and Known should have same BitWidth");
2463 }
2464#endif
2465
2466 const APInt *C;
2467 if (match(V, m_APInt(C))) {
2468 // We know all of the bits for a scalar constant or a splat vector constant!
2469 Known = KnownBits::makeConstant(*C);
2470 return;
2471 }
2472 // Null and aggregate-zero are all-zeros.
2474 Known.setAllZero();
2475 return;
2476 }
2477 // Handle a constant vector by taking the intersection of the known bits of
2478 // each element.
2480 assert(!isa<ScalableVectorType>(V->getType()));
2481 // We know that CDV must be a vector of integers. Take the intersection of
2482 // each element.
2483 Known.setAllConflict();
2484 for (unsigned i = 0, e = CDV->getNumElements(); i != e; ++i) {
2485 if (!DemandedElts[i])
2486 continue;
2487 APInt Elt = CDV->getElementAsAPInt(i);
2488 Known.Zero &= ~Elt;
2489 Known.One &= Elt;
2490 }
2491 if (Known.hasConflict())
2492 Known.resetAll();
2493 return;
2494 }
2495
2496 if (const auto *CV = dyn_cast<ConstantVector>(V)) {
2497 assert(!isa<ScalableVectorType>(V->getType()));
2498 // We know that CV must be a vector of integers. Take the intersection of
2499 // each element.
2500 Known.setAllConflict();
2501 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
2502 if (!DemandedElts[i])
2503 continue;
2504 Constant *Element = CV->getAggregateElement(i);
2505 if (isa<PoisonValue>(Element))
2506 continue;
2507 auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
2508 if (!ElementCI) {
2509 Known.resetAll();
2510 return;
2511 }
2512 const APInt &Elt = ElementCI->getValue();
2513 Known.Zero &= ~Elt;
2514 Known.One &= Elt;
2515 }
2516 if (Known.hasConflict())
2517 Known.resetAll();
2518 return;
2519 }
2520
2521 // Start out not knowing anything.
2522 Known.resetAll();
2523
2524 // We can't imply anything about undefs.
2525 if (isa<UndefValue>(V))
2526 return;
2527
2528 // There's no point in looking through other users of ConstantData for
2529 // assumptions. Confirm that we've handled them all.
2530 assert(!isa<ConstantData>(V) && "Unhandled constant data!");
2531
2532 if (const auto *A = dyn_cast<Argument>(V))
2533 if (std::optional<ConstantRange> Range = A->getRange())
2534 Known = Range->toKnownBits();
2535
2536 // All recursive calls that increase depth must come after this.
2538 return;
2539
2540 // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
2541 // the bits of its aliasee.
2542 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
2543 if (!GA->isInterposable())
2544 computeKnownBits(GA->getAliasee(), Known, Q, Depth + 1);
2545 return;
2546 }
2547
2548 if (const Operator *I = dyn_cast<Operator>(V))
2549 computeKnownBitsFromOperator(I, DemandedElts, Known, Q, Depth);
2550 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2551 if (std::optional<ConstantRange> CR = GV->getAbsoluteSymbolRange())
2552 Known = CR->toKnownBits();
2553 }
2554
2555 // Aligned pointers have trailing zeros - refine Known.Zero set
2556 if (isa<PointerType>(V->getType())) {
2557 Align Alignment = V->getPointerAlignment(Q.DL);
2558 Known.Zero.setLowBits(Log2(Alignment));
2559 }
2560
2561 // computeKnownBitsFromContext strictly refines Known.
2562 // Therefore, we run them after computeKnownBitsFromOperator.
2563
2564 // Check whether we can determine known bits from context such as assumes.
2565 computeKnownBitsFromContext(V, Known, Q, Depth);
2566}
2567
2568/// Try to detect a recurrence that the value of the induction variable is
2569/// always a power of two (or zero).
2570static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero,
2571 SimplifyQuery &Q, unsigned Depth) {
2572 BinaryOperator *BO = nullptr;
2573 Value *Start = nullptr, *Step = nullptr;
2574 if (!matchSimpleRecurrence(PN, BO, Start, Step))
2575 return false;
2576
2577 // Initial value must be a power of two.
2578 for (const Use &U : PN->operands()) {
2579 if (U.get() == Start) {
2580 // Initial value comes from a different BB, need to adjust context
2581 // instruction for analysis.
2582 Q.CxtI = PN->getIncomingBlock(U)->getTerminator();
2583 if (!isKnownToBeAPowerOfTwo(Start, OrZero, Q, Depth))
2584 return false;
2585 }
2586 }
2587
2588 // Except for Mul, the induction variable must be on the left side of the
2589 // increment expression, otherwise its value can be arbitrary.
2590 if (BO->getOpcode() != Instruction::Mul && BO->getOperand(1) != Step)
2591 return false;
2592
2593 Q.CxtI = BO->getParent()->getTerminator();
2594 switch (BO->getOpcode()) {
2595 case Instruction::Mul:
2596 // Power of two is closed under multiplication.
2597 return (OrZero || Q.IIQ.hasNoUnsignedWrap(BO) ||
2598 Q.IIQ.hasNoSignedWrap(BO)) &&
2599 isKnownToBeAPowerOfTwo(Step, OrZero, Q, Depth);
2600 case Instruction::SDiv:
2601 // Start value must not be signmask for signed division, so simply being a
2602 // power of two is not sufficient, and it has to be a constant.
2603 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2604 return false;
2605 [[fallthrough]];
2606 case Instruction::UDiv:
2607 // Divisor must be a power of two.
2608 // If OrZero is false, cannot guarantee induction variable is non-zero after
2609 // division, same for Shr, unless it is exact division.
2610 return (OrZero || Q.IIQ.isExact(BO)) &&
2611 isKnownToBeAPowerOfTwo(Step, false, Q, Depth);
2612 case Instruction::Shl:
2613 return OrZero || Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO);
2614 case Instruction::AShr:
2615 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2616 return false;
2617 [[fallthrough]];
2618 case Instruction::LShr:
2619 return OrZero || Q.IIQ.isExact(BO);
2620 default:
2621 return false;
2622 }
2623}
2624
2625/// Return true if we can infer that \p V is known to be a power of 2 from
2626/// dominating condition \p Cond (e.g., ctpop(V) == 1).
2627static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero,
2628 const Value *Cond,
2629 bool CondIsTrue) {
2630 CmpPredicate Pred;
2631 const APInt *RHSC;
2632 if (!match(Cond, m_ICmp(Pred, m_Ctpop(m_Specific(V)), m_APInt(RHSC))))
2633 return false;
2634 if (!CondIsTrue)
2635 Pred = ICmpInst::getInversePredicate(Pred);
2636 // ctpop(V) u< 2
2637 if (OrZero && Pred == ICmpInst::ICMP_ULT && *RHSC == 2)
2638 return true;
2639 // ctpop(V) == 1
2640 return Pred == ICmpInst::ICMP_EQ && *RHSC == 1;
2641}
2642
2643/// Return true if the given value is known to have exactly one
2644/// bit set when defined. For vectors return true if every element is known to
2645/// be a power of two when defined. Supports values with integer or pointer
2646/// types and vectors of integers.
2647bool llvm::isKnownToBeAPowerOfTwo(const Value *V, bool OrZero,
2648 const SimplifyQuery &Q, unsigned Depth) {
2649 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2650
2651 if (isa<Constant>(V))
2652 return OrZero ? match(V, m_Power2OrZero()) : match(V, m_Power2());
2653
2654 // i1 is by definition a power of 2 or zero.
2655 if (OrZero && V->getType()->getScalarSizeInBits() == 1)
2656 return true;
2657
2658 // Try to infer from assumptions.
2659 if (Q.AC && Q.CxtI) {
2660 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
2661 if (!AssumeVH)
2662 continue;
2663 CallInst *I = cast<CallInst>(AssumeVH);
2664 if (isImpliedToBeAPowerOfTwoFromCond(V, OrZero, I->getArgOperand(0),
2665 /*CondIsTrue=*/true) &&
2667 return true;
2668 }
2669 }
2670
2671 // Handle dominating conditions.
2672 if (Q.DC && Q.CxtI && Q.DT) {
2673 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
2674 Value *Cond = BI->getCondition();
2675
2676 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
2678 /*CondIsTrue=*/true) &&
2679 Q.DT->dominates(Edge0, Q.CxtI->getParent()))
2680 return true;
2681
2682 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
2684 /*CondIsTrue=*/false) &&
2685 Q.DT->dominates(Edge1, Q.CxtI->getParent()))
2686 return true;
2687 }
2688 }
2689
2690 auto *I = dyn_cast<Instruction>(V);
2691 if (!I)
2692 return false;
2693
2694 if (Q.CxtI && match(V, m_VScale())) {
2695 const Function *F = Q.CxtI->getFunction();
2696 // The vscale_range indicates vscale is a power-of-two.
2697 return F->hasFnAttribute(Attribute::VScaleRange);
2698 }
2699
2700 // 1 << X is clearly a power of two if the one is not shifted off the end. If
2701 // it is shifted off the end then the result is undefined.
2702 if (match(I, m_Shl(m_One(), m_Value())))
2703 return true;
2704
2705 // (signmask) >>l X is clearly a power of two if the one is not shifted off
2706 // the bottom. If it is shifted off the bottom then the result is undefined.
2707 if (match(I, m_LShr(m_SignMask(), m_Value())))
2708 return true;
2709
2710 // The remaining tests are all recursive, so bail out if we hit the limit.
2712 return false;
2713
2714 switch (I->getOpcode()) {
2715 case Instruction::ZExt:
2716 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2717 case Instruction::Trunc:
2718 return OrZero && isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2719 case Instruction::Shl:
2720 if (OrZero || Q.IIQ.hasNoUnsignedWrap(I) || Q.IIQ.hasNoSignedWrap(I))
2721 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2722 return false;
2723 case Instruction::LShr:
2724 if (OrZero || Q.IIQ.isExact(cast<BinaryOperator>(I)))
2725 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2726 return false;
2727 case Instruction::UDiv:
2729 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2730 return false;
2731 case Instruction::Mul:
2732 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
2733 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth) &&
2734 (OrZero || isKnownNonZero(I, Q, Depth));
2735 case Instruction::And:
2736 // A power of two and'd with anything is a power of two or zero.
2737 if (OrZero &&
2738 (isKnownToBeAPowerOfTwo(I->getOperand(1), /*OrZero*/ true, Q, Depth) ||
2739 isKnownToBeAPowerOfTwo(I->getOperand(0), /*OrZero*/ true, Q, Depth)))
2740 return true;
2741 // X & (-X) is always a power of two or zero.
2742 if (match(I->getOperand(0), m_Neg(m_Specific(I->getOperand(1)))) ||
2743 match(I->getOperand(1), m_Neg(m_Specific(I->getOperand(0)))))
2744 return OrZero || isKnownNonZero(I->getOperand(0), Q, Depth);
2745 return false;
2746 case Instruction::Add: {
2747 // Adding a power-of-two or zero to the same power-of-two or zero yields
2748 // either the original power-of-two, a larger power-of-two or zero.
2750 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) ||
2751 Q.IIQ.hasNoSignedWrap(VOBO)) {
2752 if (match(I->getOperand(0),
2753 m_c_And(m_Specific(I->getOperand(1)), m_Value())) &&
2754 isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth))
2755 return true;
2756 if (match(I->getOperand(1),
2757 m_c_And(m_Specific(I->getOperand(0)), m_Value())) &&
2758 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth))
2759 return true;
2760
2761 unsigned BitWidth = V->getType()->getScalarSizeInBits();
2762 KnownBits LHSBits(BitWidth);
2763 computeKnownBits(I->getOperand(0), LHSBits, Q, Depth);
2764
2765 KnownBits RHSBits(BitWidth);
2766 computeKnownBits(I->getOperand(1), RHSBits, Q, Depth);
2767 // If i8 V is a power of two or zero:
2768 // ZeroBits: 1 1 1 0 1 1 1 1
2769 // ~ZeroBits: 0 0 0 1 0 0 0 0
2770 if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2())
2771 // If OrZero isn't set, we cannot give back a zero result.
2772 // Make sure either the LHS or RHS has a bit set.
2773 if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue())
2774 return true;
2775 }
2776
2777 // LShr(UINT_MAX, Y) + 1 is a power of two (if add is nuw) or zero.
2778 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO))
2779 if (match(I, m_Add(m_LShr(m_AllOnes(), m_Value()), m_One())))
2780 return true;
2781 return false;
2782 }
2783 case Instruction::Select:
2784 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
2785 isKnownToBeAPowerOfTwo(I->getOperand(2), OrZero, Q, Depth);
2786 case Instruction::PHI: {
2787 // A PHI node is power of two if all incoming values are power of two, or if
2788 // it is an induction variable where in each step its value is a power of
2789 // two.
2790 auto *PN = cast<PHINode>(I);
2792
2793 // Check if it is an induction variable and always power of two.
2794 if (isPowerOfTwoRecurrence(PN, OrZero, RecQ, Depth))
2795 return true;
2796
2797 // Recursively check all incoming values. Limit recursion to 2 levels, so
2798 // that search complexity is limited to number of operands^2.
2799 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
2800 return llvm::all_of(PN->operands(), [&](const Use &U) {
2801 // Value is power of 2 if it is coming from PHI node itself by induction.
2802 if (U.get() == PN)
2803 return true;
2804
2805 // Change the context instruction to the incoming block where it is
2806 // evaluated.
2807 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
2808 return isKnownToBeAPowerOfTwo(U.get(), OrZero, RecQ, NewDepth);
2809 });
2810 }
2811 case Instruction::Invoke:
2812 case Instruction::Call: {
2813 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
2814 switch (II->getIntrinsicID()) {
2815 case Intrinsic::umax:
2816 case Intrinsic::smax:
2817 case Intrinsic::umin:
2818 case Intrinsic::smin:
2819 return isKnownToBeAPowerOfTwo(II->getArgOperand(1), OrZero, Q, Depth) &&
2820 isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2821 // bswap/bitreverse just move around bits, but don't change any 1s/0s
2822 // thus dont change pow2/non-pow2 status.
2823 case Intrinsic::bitreverse:
2824 case Intrinsic::bswap:
2825 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2826 case Intrinsic::fshr:
2827 case Intrinsic::fshl:
2828 // If Op0 == Op1, this is a rotate. is_pow2(rotate(x, y)) == is_pow2(x)
2829 if (II->getArgOperand(0) == II->getArgOperand(1))
2830 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2831 break;
2832 default:
2833 break;
2834 }
2835 }
2836 return false;
2837 }
2838 default:
2839 return false;
2840 }
2841}
2842
2843/// Test whether a GEP's result is known to be non-null.
2844///
2845/// Uses properties inherent in a GEP to try to determine whether it is known
2846/// to be non-null.
2847///
2848/// Currently this routine does not support vector GEPs.
2849static bool isGEPKnownNonNull(const GEPOperator *GEP, const SimplifyQuery &Q,
2850 unsigned Depth) {
2851 const Function *F = nullptr;
2852 if (const Instruction *I = dyn_cast<Instruction>(GEP))
2853 F = I->getFunction();
2854
2855 // If the gep is nuw or inbounds with invalid null pointer, then the GEP
2856 // may be null iff the base pointer is null and the offset is zero.
2857 if (!GEP->hasNoUnsignedWrap() &&
2858 !(GEP->isInBounds() &&
2859 !NullPointerIsDefined(F, GEP->getPointerAddressSpace())))
2860 return false;
2861
2862 // FIXME: Support vector-GEPs.
2863 assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP");
2864
2865 // If the base pointer is non-null, we cannot walk to a null address with an
2866 // inbounds GEP in address space zero.
2867 if (isKnownNonZero(GEP->getPointerOperand(), Q, Depth))
2868 return true;
2869
2870 // Walk the GEP operands and see if any operand introduces a non-zero offset.
2871 // If so, then the GEP cannot produce a null pointer, as doing so would
2872 // inherently violate the inbounds contract within address space zero.
2874 GTI != GTE; ++GTI) {
2875 // Struct types are easy -- they must always be indexed by a constant.
2876 if (StructType *STy = GTI.getStructTypeOrNull()) {
2877 ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
2878 unsigned ElementIdx = OpC->getZExtValue();
2879 const StructLayout *SL = Q.DL.getStructLayout(STy);
2880 uint64_t ElementOffset = SL->getElementOffset(ElementIdx);
2881 if (ElementOffset > 0)
2882 return true;
2883 continue;
2884 }
2885
2886 // If we have a zero-sized type, the index doesn't matter. Keep looping.
2887 if (GTI.getSequentialElementStride(Q.DL).isZero())
2888 continue;
2889
2890 // Fast path the constant operand case both for efficiency and so we don't
2891 // increment Depth when just zipping down an all-constant GEP.
2892 if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
2893 if (!OpC->isZero())
2894 return true;
2895 continue;
2896 }
2897
2898 // We post-increment Depth here because while isKnownNonZero increments it
2899 // as well, when we pop back up that increment won't persist. We don't want
2900 // to recurse 10k times just because we have 10k GEP operands. We don't
2901 // bail completely out because we want to handle constant GEPs regardless
2902 // of depth.
2904 continue;
2905
2906 if (isKnownNonZero(GTI.getOperand(), Q, Depth))
2907 return true;
2908 }
2909
2910 return false;
2911}
2912
2914 const Instruction *CtxI,
2915 const DominatorTree *DT) {
2916 assert(!isa<Constant>(V) && "Called for constant?");
2917
2918 if (!CtxI || !DT)
2919 return false;
2920
2921 unsigned NumUsesExplored = 0;
2922 for (auto &U : V->uses()) {
2923 // Avoid massive lists
2924 if (NumUsesExplored >= DomConditionsMaxUses)
2925 break;
2926 NumUsesExplored++;
2927
2928 const Instruction *UI = cast<Instruction>(U.getUser());
2929 // If the value is used as an argument to a call or invoke, then argument
2930 // attributes may provide an answer about null-ness.
2931 if (V->getType()->isPointerTy()) {
2932 if (const auto *CB = dyn_cast<CallBase>(UI)) {
2933 if (CB->isArgOperand(&U) &&
2934 CB->paramHasNonNullAttr(CB->getArgOperandNo(&U),
2935 /*AllowUndefOrPoison=*/false) &&
2936 DT->dominates(CB, CtxI))
2937 return true;
2938 }
2939 }
2940
2941 // If the value is used as a load/store, then the pointer must be non null.
2942 if (V == getLoadStorePointerOperand(UI)) {
2945 DT->dominates(UI, CtxI))
2946 return true;
2947 }
2948
2949 if ((match(UI, m_IDiv(m_Value(), m_Specific(V))) ||
2950 match(UI, m_IRem(m_Value(), m_Specific(V)))) &&
2951 isValidAssumeForContext(UI, CtxI, DT))
2952 return true;
2953
2954 // Consider only compare instructions uniquely controlling a branch
2955 Value *RHS;
2956 CmpPredicate Pred;
2957 if (!match(UI, m_c_ICmp(Pred, m_Specific(V), m_Value(RHS))))
2958 continue;
2959
2960 bool NonNullIfTrue;
2961 if (cmpExcludesZero(Pred, RHS))
2962 NonNullIfTrue = true;
2964 NonNullIfTrue = false;
2965 else
2966 continue;
2967
2970 for (const auto *CmpU : UI->users()) {
2971 assert(WorkList.empty() && "Should be!");
2972 if (Visited.insert(CmpU).second)
2973 WorkList.push_back(CmpU);
2974
2975 while (!WorkList.empty()) {
2976 auto *Curr = WorkList.pop_back_val();
2977
2978 // If a user is an AND, add all its users to the work list. We only
2979 // propagate "pred != null" condition through AND because it is only
2980 // correct to assume that all conditions of AND are met in true branch.
2981 // TODO: Support similar logic of OR and EQ predicate?
2982 if (NonNullIfTrue)
2983 if (match(Curr, m_LogicalAnd(m_Value(), m_Value()))) {
2984 for (const auto *CurrU : Curr->users())
2985 if (Visited.insert(CurrU).second)
2986 WorkList.push_back(CurrU);
2987 continue;
2988 }
2989
2990 if (const CondBrInst *BI = dyn_cast<CondBrInst>(Curr)) {
2991 BasicBlock *NonNullSuccessor =
2992 BI->getSuccessor(NonNullIfTrue ? 0 : 1);
2993 BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor);
2994 if (DT->dominates(Edge, CtxI->getParent()))
2995 return true;
2996 } else if (NonNullIfTrue && isGuard(Curr) &&
2997 DT->dominates(cast<Instruction>(Curr), CtxI)) {
2998 return true;
2999 }
3000 }
3001 }
3002 }
3003
3004 return false;
3005}
3006
3007/// Does the 'Range' metadata (which must be a valid MD_range operand list)
3008/// ensure that the value it's attached to is never Value? 'RangeType' is
3009/// is the type of the value described by the range.
3010static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) {
3011 const unsigned NumRanges = Ranges->getNumOperands() / 2;
3012 assert(NumRanges >= 1);
3013 for (unsigned i = 0; i < NumRanges; ++i) {
3015 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
3017 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
3018 ConstantRange Range(Lower->getValue(), Upper->getValue());
3019 if (Range.contains(Value))
3020 return false;
3021 }
3022 return true;
3023}
3024
3025/// Try to detect a recurrence that monotonically increases/decreases from a
3026/// non-zero starting value. These are common as induction variables.
3027static bool isNonZeroRecurrence(const PHINode *PN) {
3028 BinaryOperator *BO = nullptr;
3029 Value *Start = nullptr, *Step = nullptr;
3030 const APInt *StartC, *StepC;
3031 if (!matchSimpleRecurrence(PN, BO, Start, Step) ||
3032 !match(Start, m_APInt(StartC)) || StartC->isZero())
3033 return false;
3034
3035 switch (BO->getOpcode()) {
3036 case Instruction::Add:
3037 // Starting from non-zero and stepping away from zero can never wrap back
3038 // to zero.
3039 return BO->hasNoUnsignedWrap() ||
3040 (BO->hasNoSignedWrap() && match(Step, m_APInt(StepC)) &&
3041 StartC->isNegative() == StepC->isNegative());
3042 case Instruction::Mul:
3043 return (BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap()) &&
3044 match(Step, m_APInt(StepC)) && !StepC->isZero();
3045 case Instruction::Shl:
3046 return BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap();
3047 case Instruction::AShr:
3048 case Instruction::LShr:
3049 return BO->isExact();
3050 default:
3051 return false;
3052 }
3053}
3054
3055static bool matchOpWithOpEqZero(Value *Op0, Value *Op1) {
3057 m_Specific(Op1), m_Zero()))) ||
3059 m_Specific(Op0), m_Zero())));
3060}
3061
3062static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q,
3063 unsigned BitWidth, Value *X, Value *Y, bool NSW,
3064 bool NUW, unsigned Depth) {
3065 // (X + (X != 0)) is non zero
3066 if (matchOpWithOpEqZero(X, Y))
3067 return true;
3068
3069 if (NUW)
3070 return isKnownNonZero(Y, DemandedElts, Q, Depth) ||
3071 isKnownNonZero(X, DemandedElts, Q, Depth);
3072
3073 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
3074 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
3075
3076 // If X and Y are both non-negative (as signed values) then their sum is not
3077 // zero unless both X and Y are zero.
3078 if (XKnown.isNonNegative() && YKnown.isNonNegative())
3079 if (isKnownNonZero(Y, DemandedElts, Q, Depth) ||
3080 isKnownNonZero(X, DemandedElts, Q, Depth))
3081 return true;
3082
3083 // If X and Y are both negative (as signed values) then their sum is not
3084 // zero unless both X and Y equal INT_MIN.
3085 if (XKnown.isNegative() && YKnown.isNegative()) {
3087 // The sign bit of X is set. If some other bit is set then X is not equal
3088 // to INT_MIN.
3089 if (XKnown.One.intersects(Mask))
3090 return true;
3091 // The sign bit of Y is set. If some other bit is set then Y is not equal
3092 // to INT_MIN.
3093 if (YKnown.One.intersects(Mask))
3094 return true;
3095 }
3096
3097 // The sum of a non-negative number and a power of two is not zero.
3098 if (XKnown.isNonNegative() &&
3099 isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Q, Depth))
3100 return true;
3101 if (YKnown.isNonNegative() &&
3102 isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Q, Depth))
3103 return true;
3104
3105 return KnownBits::add(XKnown, YKnown, NSW, NUW).isNonZero();
3106}
3107
3108static bool isNonZeroSub(const APInt &DemandedElts, const SimplifyQuery &Q,
3109 unsigned BitWidth, Value *X, Value *Y,
3110 unsigned Depth) {
3111 // (X - (X != 0)) is non zero
3112 // ((X != 0) - X) is non zero
3113 if (matchOpWithOpEqZero(X, Y))
3114 return true;
3115
3116 // TODO: Move this case into isKnownNonEqual().
3117 if (auto *C = dyn_cast<Constant>(X))
3118 if (C->isNullValue() && isKnownNonZero(Y, DemandedElts, Q, Depth))
3119 return true;
3120
3121 return ::isKnownNonEqual(X, Y, DemandedElts, Q, Depth);
3122}
3123
3124static bool isNonZeroMul(const APInt &DemandedElts, const SimplifyQuery &Q,
3125 unsigned BitWidth, Value *X, Value *Y, bool NSW,
3126 bool NUW, unsigned Depth) {
3127 // If X and Y are non-zero then so is X * Y as long as the multiplication
3128 // does not overflow.
3129 if (NSW || NUW)
3130 return isKnownNonZero(X, DemandedElts, Q, Depth) &&
3131 isKnownNonZero(Y, DemandedElts, Q, Depth);
3132
3133 // If either X or Y is odd, then if the other is non-zero the result can't
3134 // be zero.
3135 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
3136 if (XKnown.One[0])
3137 return isKnownNonZero(Y, DemandedElts, Q, Depth);
3138
3139 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
3140 if (YKnown.One[0])
3141 return XKnown.isNonZero() || isKnownNonZero(X, DemandedElts, Q, Depth);
3142
3143 // If there exists any subset of X (sX) and subset of Y (sY) s.t sX * sY is
3144 // non-zero, then X * Y is non-zero. We can find sX and sY by just taking
3145 // the lowest known One of X and Y. If they are non-zero, the result
3146 // must be non-zero. We can check if LSB(X) * LSB(Y) != 0 by doing
3147 // X.CountLeadingZeros + Y.CountLeadingZeros < BitWidth.
3148 return (XKnown.countMaxTrailingZeros() + YKnown.countMaxTrailingZeros()) <
3149 BitWidth;
3150}
3151
3152static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts,
3153 const SimplifyQuery &Q, const KnownBits &KnownVal,
3154 unsigned Depth) {
3155 auto ShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
3156 switch (I->getOpcode()) {
3157 case Instruction::Shl:
3158 return Lhs.shl(Rhs);
3159 case Instruction::LShr:
3160 return Lhs.lshr(Rhs);
3161 case Instruction::AShr:
3162 return Lhs.ashr(Rhs);
3163 default:
3164 llvm_unreachable("Unknown Shift Opcode");
3165 }
3166 };
3167
3168 auto InvShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
3169 switch (I->getOpcode()) {
3170 case Instruction::Shl:
3171 return Lhs.lshr(Rhs);
3172 case Instruction::LShr:
3173 case Instruction::AShr:
3174 return Lhs.shl(Rhs);
3175 default:
3176 llvm_unreachable("Unknown Shift Opcode");
3177 }
3178 };
3179
3180 if (KnownVal.isUnknown())
3181 return false;
3182
3183 KnownBits KnownCnt =
3184 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
3185 APInt MaxShift = KnownCnt.getMaxValue();
3186 unsigned NumBits = KnownVal.getBitWidth();
3187 if (MaxShift.uge(NumBits))
3188 return false;
3189
3190 if (!ShiftOp(KnownVal.One, MaxShift).isZero())
3191 return true;
3192
3193 // If all of the bits shifted out are known to be zero, and Val is known
3194 // non-zero then at least one non-zero bit must remain.
3195 if (InvShiftOp(KnownVal.Zero, NumBits - MaxShift)
3196 .eq(InvShiftOp(APInt::getAllOnes(NumBits), NumBits - MaxShift)) &&
3197 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth))
3198 return true;
3199
3200 return false;
3201}
3202
3204 const APInt &DemandedElts,
3205 const SimplifyQuery &Q, unsigned Depth) {
3206 unsigned BitWidth = getBitWidth(I->getType()->getScalarType(), Q.DL);
3207 switch (I->getOpcode()) {
3208 case Instruction::Alloca:
3209 // Alloca never returns null, malloc might.
3210 return I->getType()->getPointerAddressSpace() == 0;
3211 case Instruction::GetElementPtr:
3212 if (I->getType()->isPointerTy())
3214 break;
3215 case Instruction::BitCast: {
3216 // We need to be a bit careful here. We can only peek through the bitcast
3217 // if the scalar size of elements in the operand are smaller than and a
3218 // multiple of the size they are casting too. Take three cases:
3219 //
3220 // 1) Unsafe:
3221 // bitcast <2 x i16> %NonZero to <4 x i8>
3222 //
3223 // %NonZero can have 2 non-zero i16 elements, but isKnownNonZero on a
3224 // <4 x i8> requires that all 4 i8 elements be non-zero which isn't
3225 // guranteed (imagine just sign bit set in the 2 i16 elements).
3226 //
3227 // 2) Unsafe:
3228 // bitcast <4 x i3> %NonZero to <3 x i4>
3229 //
3230 // Even though the scalar size of the src (`i3`) is smaller than the
3231 // scalar size of the dst `i4`, because `i3` is not a multiple of `i4`
3232 // its possible for the `3 x i4` elements to be zero because there are
3233 // some elements in the destination that don't contain any full src
3234 // element.
3235 //
3236 // 3) Safe:
3237 // bitcast <4 x i8> %NonZero to <2 x i16>
3238 //
3239 // This is always safe as non-zero in the 4 i8 elements implies
3240 // non-zero in the combination of any two adjacent ones. Since i8 is a
3241 // multiple of i16, each i16 is guranteed to have 2 full i8 elements.
3242 // This all implies the 2 i16 elements are non-zero.
3243 Type *FromTy = I->getOperand(0)->getType();
3244 if ((FromTy->isIntOrIntVectorTy() || FromTy->isPtrOrPtrVectorTy()) &&
3245 (BitWidth % getBitWidth(FromTy->getScalarType(), Q.DL)) == 0)
3246 return isKnownNonZero(I->getOperand(0), Q, Depth);
3247 } break;
3248 case Instruction::IntToPtr:
3249 // Note that we have to take special care to avoid looking through
3250 // truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well
3251 // as casts that can alter the value, e.g., AddrSpaceCasts.
3252 if (!isa<ScalableVectorType>(I->getType()) &&
3253 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
3254 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
3255 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3256 break;
3257 case Instruction::PtrToAddr:
3258 // isKnownNonZero() for pointers refers to the address bits being non-zero,
3259 // so we can directly forward.
3260 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3261 case Instruction::PtrToInt:
3262 // For inttoptr, make sure the result size is >= the address size. If the
3263 // address is non-zero, any larger value is also non-zero.
3264 if (Q.DL.getAddressSizeInBits(I->getOperand(0)->getType()) <=
3265 I->getType()->getScalarSizeInBits())
3266 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3267 break;
3268 case Instruction::Trunc:
3269 // nuw/nsw trunc preserves zero/non-zero status of input.
3270 if (auto *TI = dyn_cast<TruncInst>(I))
3271 if (TI->hasNoSignedWrap() || TI->hasNoUnsignedWrap())
3272 return isKnownNonZero(TI->getOperand(0), DemandedElts, Q, Depth);
3273 break;
3274
3275 // Iff x - y != 0, then x ^ y != 0
3276 // Therefore we can do the same exact checks
3277 case Instruction::Xor:
3278 case Instruction::Sub:
3279 return isNonZeroSub(DemandedElts, Q, BitWidth, I->getOperand(0),
3280 I->getOperand(1), Depth);
3281 case Instruction::Or:
3282 // (X | (X != 0)) is non zero
3283 if (matchOpWithOpEqZero(I->getOperand(0), I->getOperand(1)))
3284 return true;
3285 // X | Y != 0 if X != Y.
3286 if (isKnownNonEqual(I->getOperand(0), I->getOperand(1), DemandedElts, Q,
3287 Depth))
3288 return true;
3289 // X | Y != 0 if X != 0 or Y != 0.
3290 return isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth) ||
3291 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3292 case Instruction::SExt:
3293 case Instruction::ZExt:
3294 // ext X != 0 if X != 0.
3295 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3296
3297 case Instruction::Shl: {
3298 // shl nsw/nuw can't remove any non-zero bits.
3300 if (Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO))
3301 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3302
3303 // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
3304 // if the lowest bit is shifted off the end.
3305 KnownBits Known(BitWidth);
3306 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth);
3307 if (Known.One[0])
3308 return true;
3309
3310 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3311 }
3312 case Instruction::LShr:
3313 case Instruction::AShr: {
3314 // shr exact can only shift out zero bits.
3316 if (BO->isExact())
3317 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3318
3319 // shr X, Y != 0 if X is negative. Note that the value of the shift is not
3320 // defined if the sign bit is shifted off the end.
3321 KnownBits Known =
3322 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3323 if (Known.isNegative())
3324 return true;
3325
3326 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3327 }
3328 case Instruction::UDiv:
3329 case Instruction::SDiv: {
3330 // X / Y
3331 // div exact can only produce a zero if the dividend is zero.
3332 if (cast<PossiblyExactOperator>(I)->isExact())
3333 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3334
3335 KnownBits XKnown =
3336 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3337 // If X is fully unknown we won't be able to figure anything out so don't
3338 // both computing knownbits for Y.
3339 if (XKnown.isUnknown())
3340 return false;
3341
3342 KnownBits YKnown =
3343 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
3344 if (I->getOpcode() == Instruction::SDiv) {
3345 // For signed division need to compare abs value of the operands.
3346 XKnown = XKnown.abs(/*IntMinIsPoison*/ false);
3347 YKnown = YKnown.abs(/*IntMinIsPoison*/ false);
3348 }
3349 // If X u>= Y then div is non zero (0/0 is UB).
3350 std::optional<bool> XUgeY = KnownBits::uge(XKnown, YKnown);
3351 // If X is total unknown or X u< Y we won't be able to prove non-zero
3352 // with compute known bits so just return early.
3353 return XUgeY && *XUgeY;
3354 }
3355 case Instruction::Add: {
3356 // X + Y.
3357
3358 // If Add has nuw wrap flag, then if either X or Y is non-zero the result is
3359 // non-zero.
3361 return isNonZeroAdd(DemandedElts, Q, BitWidth, I->getOperand(0),
3362 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3363 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3364 }
3365 case Instruction::Mul: {
3367 return isNonZeroMul(DemandedElts, Q, BitWidth, I->getOperand(0),
3368 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3369 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3370 }
3371 case Instruction::Select: {
3372 // (C ? X : Y) != 0 if X != 0 and Y != 0.
3373
3374 // First check if the arm is non-zero using `isKnownNonZero`. If that fails,
3375 // then see if the select condition implies the arm is non-zero. For example
3376 // (X != 0 ? X : Y), we know the true arm is non-zero as the `X` "return" is
3377 // dominated by `X != 0`.
3378 auto SelectArmIsNonZero = [&](bool IsTrueArm) {
3379 Value *Op;
3380 Op = IsTrueArm ? I->getOperand(1) : I->getOperand(2);
3381 // Op is trivially non-zero.
3382 if (isKnownNonZero(Op, DemandedElts, Q, Depth))
3383 return true;
3384
3385 // The condition of the select dominates the true/false arm. Check if the
3386 // condition implies that a given arm is non-zero.
3387 Value *X;
3388 CmpPredicate Pred;
3389 if (!match(I->getOperand(0), m_c_ICmp(Pred, m_Specific(Op), m_Value(X))))
3390 return false;
3391
3392 if (!IsTrueArm)
3393 Pred = ICmpInst::getInversePredicate(Pred);
3394
3395 return cmpExcludesZero(Pred, X);
3396 };
3397
3398 if (SelectArmIsNonZero(/* IsTrueArm */ true) &&
3399 SelectArmIsNonZero(/* IsTrueArm */ false))
3400 return true;
3401 break;
3402 }
3403 case Instruction::PHI: {
3404 auto *PN = cast<PHINode>(I);
3406 return true;
3407
3408 // Check if all incoming values are non-zero using recursion.
3410 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
3411 return llvm::all_of(PN->operands(), [&](const Use &U) {
3412 if (U.get() == PN)
3413 return true;
3414 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
3415 // Check if the branch on the phi excludes zero.
3416 CmpPredicate Pred;
3417 Value *X;
3418 BasicBlock *TrueSucc, *FalseSucc;
3419 if (match(RecQ.CxtI,
3420 m_Br(m_c_ICmp(Pred, m_Specific(U.get()), m_Value(X)),
3421 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
3422 // Check for cases of duplicate successors.
3423 if ((TrueSucc == PN->getParent()) != (FalseSucc == PN->getParent())) {
3424 // If we're using the false successor, invert the predicate.
3425 if (FalseSucc == PN->getParent())
3426 Pred = CmpInst::getInversePredicate(Pred);
3427 if (cmpExcludesZero(Pred, X))
3428 return true;
3429 }
3430 }
3431 // Finally recurse on the edge and check it directly.
3432 return isKnownNonZero(U.get(), DemandedElts, RecQ, NewDepth);
3433 });
3434 }
3435 case Instruction::InsertElement: {
3436 if (isa<ScalableVectorType>(I->getType()))
3437 break;
3438
3439 const Value *Vec = I->getOperand(0);
3440 const Value *Elt = I->getOperand(1);
3441 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
3442
3443 unsigned NumElts = DemandedElts.getBitWidth();
3444 APInt DemandedVecElts = DemandedElts;
3445 bool SkipElt = false;
3446 // If we know the index we are inserting too, clear it from Vec check.
3447 if (CIdx && CIdx->getValue().ult(NumElts)) {
3448 DemandedVecElts.clearBit(CIdx->getZExtValue());
3449 SkipElt = !DemandedElts[CIdx->getZExtValue()];
3450 }
3451
3452 // Result is zero if Elt is non-zero and rest of the demanded elts in Vec
3453 // are non-zero.
3454 return (SkipElt || isKnownNonZero(Elt, Q, Depth)) &&
3455 (DemandedVecElts.isZero() ||
3456 isKnownNonZero(Vec, DemandedVecElts, Q, Depth));
3457 }
3458 case Instruction::ExtractElement:
3459 if (const auto *EEI = dyn_cast<ExtractElementInst>(I)) {
3460 const Value *Vec = EEI->getVectorOperand();
3461 const Value *Idx = EEI->getIndexOperand();
3462 auto *CIdx = dyn_cast<ConstantInt>(Idx);
3463 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
3464 unsigned NumElts = VecTy->getNumElements();
3465 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
3466 if (CIdx && CIdx->getValue().ult(NumElts))
3467 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
3468 return isKnownNonZero(Vec, DemandedVecElts, Q, Depth);
3469 }
3470 }
3471 break;
3472 case Instruction::ShuffleVector: {
3473 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
3474 if (!Shuf)
3475 break;
3476 APInt DemandedLHS, DemandedRHS;
3477 // For undef elements, we don't know anything about the common state of
3478 // the shuffle result.
3479 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
3480 break;
3481 // If demanded elements for both vecs are non-zero, the shuffle is non-zero.
3482 return (DemandedRHS.isZero() ||
3483 isKnownNonZero(Shuf->getOperand(1), DemandedRHS, Q, Depth)) &&
3484 (DemandedLHS.isZero() ||
3485 isKnownNonZero(Shuf->getOperand(0), DemandedLHS, Q, Depth));
3486 }
3487 case Instruction::Freeze:
3488 return isKnownNonZero(I->getOperand(0), Q, Depth) &&
3489 isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
3490 Depth);
3491 case Instruction::Load: {
3492 auto *LI = cast<LoadInst>(I);
3493 // A Load tagged with nonnull or dereferenceable with null pointer undefined
3494 // is never null.
3495 if (auto *PtrT = dyn_cast<PointerType>(I->getType())) {
3496 if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull) ||
3497 (Q.IIQ.getMetadata(LI, LLVMContext::MD_dereferenceable) &&
3498 !NullPointerIsDefined(LI->getFunction(), PtrT->getAddressSpace())))
3499 return true;
3500 } else if (MDNode *Ranges = Q.IIQ.getMetadata(LI, LLVMContext::MD_range)) {
3502 }
3503
3504 // No need to fall through to computeKnownBits as range metadata is already
3505 // handled in isKnownNonZero.
3506 return false;
3507 }
3508 case Instruction::ExtractValue: {
3509 const WithOverflowInst *WO;
3511 switch (WO->getBinaryOp()) {
3512 default:
3513 break;
3514 case Instruction::Add:
3515 return isNonZeroAdd(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3516 WO->getArgOperand(1),
3517 /*NSW=*/false,
3518 /*NUW=*/false, Depth);
3519 case Instruction::Sub:
3520 return isNonZeroSub(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3521 WO->getArgOperand(1), Depth);
3522 case Instruction::Mul:
3523 return isNonZeroMul(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3524 WO->getArgOperand(1),
3525 /*NSW=*/false, /*NUW=*/false, Depth);
3526 break;
3527 }
3528 }
3529 break;
3530 }
3531 case Instruction::Call:
3532 case Instruction::Invoke: {
3533 const auto *Call = cast<CallBase>(I);
3534 if (I->getType()->isPointerTy()) {
3535 if (Call->isReturnNonNull())
3536 return true;
3537 if (const auto *RP = getArgumentAliasingToReturnedPointer(
3538 Call, /*MustPreserveOffset=*/true))
3539 return isKnownNonZero(RP, Q, Depth);
3540 } else {
3541 if (MDNode *Ranges = Q.IIQ.getMetadata(Call, LLVMContext::MD_range))
3543 if (std::optional<ConstantRange> Range = Call->getRange()) {
3544 const APInt ZeroValue(Range->getBitWidth(), 0);
3545 if (!Range->contains(ZeroValue))
3546 return true;
3547 }
3548 if (const Value *RV = Call->getReturnedArgOperand())
3549 if (RV->getType() == I->getType() && isKnownNonZero(RV, Q, Depth))
3550 return true;
3551 }
3552
3553 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
3554 switch (II->getIntrinsicID()) {
3555 case Intrinsic::sshl_sat:
3556 case Intrinsic::ushl_sat:
3557 case Intrinsic::abs:
3558 case Intrinsic::bitreverse:
3559 case Intrinsic::bswap:
3560 case Intrinsic::ctpop:
3561 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3562 // NB: We don't do usub_sat here as in any case we can prove its
3563 // non-zero, we will fold it to `sub nuw` in InstCombine.
3564 case Intrinsic::ssub_sat:
3565 // For most types, if x != y then ssub.sat x, y != 0. But
3566 // ssub.sat.i1 0, -1 = 0, because 1 saturates to 0. This means
3567 // isNonZeroSub will do the wrong thing for ssub.sat.i1.
3568 if (BitWidth == 1)
3569 return false;
3570 return isNonZeroSub(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3571 II->getArgOperand(1), Depth);
3572 case Intrinsic::sadd_sat:
3573 return isNonZeroAdd(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3574 II->getArgOperand(1),
3575 /*NSW=*/true, /* NUW=*/false, Depth);
3576 // Vec reverse preserves zero/non-zero status from input vec.
3577 case Intrinsic::vector_reverse:
3578 return isKnownNonZero(II->getArgOperand(0), DemandedElts.reverseBits(),
3579 Q, Depth);
3580 // umin/smin/smax/smin/or of all non-zero elements is always non-zero.
3581 case Intrinsic::vector_reduce_or:
3582 case Intrinsic::vector_reduce_umax:
3583 case Intrinsic::vector_reduce_umin:
3584 case Intrinsic::vector_reduce_smax:
3585 case Intrinsic::vector_reduce_smin:
3586 return isKnownNonZero(II->getArgOperand(0), Q, Depth);
3587 case Intrinsic::umax:
3588 case Intrinsic::uadd_sat:
3589 // umax(X, (X != 0)) is non zero
3590 // X +usat (X != 0) is non zero
3591 if (matchOpWithOpEqZero(II->getArgOperand(0), II->getArgOperand(1)))
3592 return true;
3593
3594 return isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth) ||
3595 isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3596 case Intrinsic::smax: {
3597 // If either arg is strictly positive the result is non-zero. Otherwise
3598 // the result is non-zero if both ops are non-zero.
3599 auto IsNonZero = [&](Value *Op, std::optional<bool> &OpNonZero,
3600 const KnownBits &OpKnown) {
3601 if (!OpNonZero.has_value())
3602 OpNonZero = OpKnown.isNonZero() ||
3603 isKnownNonZero(Op, DemandedElts, Q, Depth);
3604 return *OpNonZero;
3605 };
3606 // Avoid re-computing isKnownNonZero.
3607 std::optional<bool> Op0NonZero, Op1NonZero;
3608 KnownBits Op1Known =
3609 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3610 if (Op1Known.isNonNegative() &&
3611 IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known))
3612 return true;
3613 KnownBits Op0Known =
3614 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3615 if (Op0Known.isNonNegative() &&
3616 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known))
3617 return true;
3618 return IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known) &&
3619 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known);
3620 }
3621 case Intrinsic::smin: {
3622 // If either arg is negative the result is non-zero. Otherwise
3623 // the result is non-zero if both ops are non-zero.
3624 KnownBits Op1Known =
3625 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3626 if (Op1Known.isNegative())
3627 return true;
3628 KnownBits Op0Known =
3629 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3630 if (Op0Known.isNegative())
3631 return true;
3632
3633 if (Op1Known.isNonZero() && Op0Known.isNonZero())
3634 return true;
3635 }
3636 [[fallthrough]];
3637 case Intrinsic::umin:
3638 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth) &&
3639 isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth);
3640 case Intrinsic::cttz:
3641 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3642 .Zero[0];
3643 case Intrinsic::ctlz:
3644 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3645 .isNonNegative();
3646 case Intrinsic::fshr:
3647 case Intrinsic::fshl:
3648 // If Op0 == Op1, this is a rotate. rotate(x, y) != 0 iff x != 0.
3649 if (II->getArgOperand(0) == II->getArgOperand(1))
3650 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3651 break;
3652 case Intrinsic::vscale:
3653 return true;
3654 case Intrinsic::experimental_get_vector_length:
3655 return isKnownNonZero(I->getOperand(0), Q, Depth);
3656 default:
3657 break;
3658 }
3659 break;
3660 }
3661
3662 return false;
3663 }
3664 }
3665
3666 KnownBits Known(BitWidth);
3667 computeKnownBits(I, DemandedElts, Known, Q, Depth);
3668 return Known.One != 0;
3669}
3670
3671/// Return true if the given value is known to be non-zero when defined. For
3672/// vectors, return true if every demanded element is known to be non-zero when
3673/// defined. For pointers, if the context instruction and dominator tree are
3674/// specified, perform context-sensitive analysis and return true if the
3675/// pointer couldn't possibly be null at the specified instruction.
3676/// Supports values with integer or pointer type and vectors of integers.
3677bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
3678 const SimplifyQuery &Q, unsigned Depth) {
3679 Type *Ty = V->getType();
3680
3681#ifndef NDEBUG
3682 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
3683
3684 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3685 assert(
3686 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
3687 "DemandedElt width should equal the fixed vector number of elements");
3688 } else {
3689 assert(DemandedElts == APInt(1, 1) &&
3690 "DemandedElt width should be 1 for scalars");
3691 }
3692#endif
3693
3694 if (auto *C = dyn_cast<Constant>(V)) {
3695 if (C->isNullValue())
3696 return false;
3697 if (isa<ConstantInt>(C))
3698 // Must be non-zero due to null test above.
3699 return true;
3700
3701 // For constant vectors, check that all elements are poison or known
3702 // non-zero to determine that the whole vector is known non-zero.
3703 if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
3704 for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
3705 if (!DemandedElts[i])
3706 continue;
3707 Constant *Elt = C->getAggregateElement(i);
3708 if (!Elt || Elt->isNullValue())
3709 return false;
3710 if (!isa<PoisonValue>(Elt) && !isa<ConstantInt>(Elt))
3711 return false;
3712 }
3713 return true;
3714 }
3715
3716 // Constant ptrauth can be null, iff the base pointer can be.
3717 if (auto *CPA = dyn_cast<ConstantPtrAuth>(V))
3718 return isKnownNonZero(CPA->getPointer(), DemandedElts, Q, Depth);
3719
3720 // A global variable in address space 0 is non null unless extern weak
3721 // or an absolute symbol reference. Other address spaces may have null as a
3722 // valid address for a global, so we can't assume anything.
3723 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
3724 if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&
3725 GV->getType()->getAddressSpace() == 0)
3726 return true;
3727 }
3728
3729 // For constant expressions, fall through to the Operator code below.
3730 if (!isa<ConstantExpr>(V))
3731 return false;
3732 }
3733
3734 if (const auto *A = dyn_cast<Argument>(V))
3735 if (std::optional<ConstantRange> Range = A->getRange()) {
3736 const APInt ZeroValue(Range->getBitWidth(), 0);
3737 if (!Range->contains(ZeroValue))
3738 return true;
3739 }
3740
3741 if (!isa<Constant>(V) && isKnownNonZeroFromAssume(V, Q))
3742 return true;
3743
3744 // Some of the tests below are recursive, so bail out if we hit the limit.
3746 return false;
3747
3748 // Check for pointer simplifications.
3749
3750 if (PointerType *PtrTy = dyn_cast<PointerType>(Ty)) {
3751 // A byval, inalloca may not be null in a non-default addres space. A
3752 // nonnull argument is assumed never 0.
3753 if (const Argument *A = dyn_cast<Argument>(V)) {
3754 if (((A->hasPassPointeeByValueCopyAttr() &&
3755 !NullPointerIsDefined(A->getParent(), PtrTy->getAddressSpace())) ||
3756 A->hasNonNullAttr()))
3757 return true;
3758 }
3759 }
3760
3761 if (const auto *I = dyn_cast<Operator>(V))
3762 if (isKnownNonZeroFromOperator(I, DemandedElts, Q, Depth))
3763 return true;
3764
3765 if (!isa<Constant>(V) &&
3767 return true;
3768
3769 if (const Value *Stripped = stripNullTest(V))
3770 return isKnownNonZero(Stripped, DemandedElts, Q, Depth);
3771
3772 return false;
3773}
3774
3776 unsigned Depth) {
3777 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
3778 APInt DemandedElts =
3779 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
3780 return ::isKnownNonZero(V, DemandedElts, Q, Depth);
3781}
3782
3783/// If the pair of operators are the same invertible function, return the
3784/// the operands of the function corresponding to each input. Otherwise,
3785/// return std::nullopt. An invertible function is one that is 1-to-1 and maps
3786/// every input value to exactly one output value. This is equivalent to
3787/// saying that Op1 and Op2 are equal exactly when the specified pair of
3788/// operands are equal, (except that Op1 and Op2 may be poison more often.)
3789static std::optional<std::pair<Value*, Value*>>
3791 const Operator *Op2) {
3792 if (Op1->getOpcode() != Op2->getOpcode())
3793 return std::nullopt;
3794
3795 auto getOperands = [&](unsigned OpNum) -> auto {
3796 return std::make_pair(Op1->getOperand(OpNum), Op2->getOperand(OpNum));
3797 };
3798
3799 switch (Op1->getOpcode()) {
3800 default:
3801 break;
3802 case Instruction::Or:
3803 if (!cast<PossiblyDisjointInst>(Op1)->isDisjoint() ||
3804 !cast<PossiblyDisjointInst>(Op2)->isDisjoint())
3805 break;
3806 [[fallthrough]];
3807 case Instruction::Xor:
3808 case Instruction::Add: {
3809 Value *Other;
3810 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(0)), m_Value(Other))))
3811 return std::make_pair(Op1->getOperand(1), Other);
3812 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(1)), m_Value(Other))))
3813 return std::make_pair(Op1->getOperand(0), Other);
3814 break;
3815 }
3816 case Instruction::Sub:
3817 if (Op1->getOperand(0) == Op2->getOperand(0))
3818 return getOperands(1);
3819 if (Op1->getOperand(1) == Op2->getOperand(1))
3820 return getOperands(0);
3821 break;
3822 case Instruction::Mul: {
3823 // invertible if A * B == (A * B) mod 2^N where A, and B are integers
3824 // and N is the bitwdith. The nsw case is non-obvious, but proven by
3825 // alive2: https://alive2.llvm.org/ce/z/Z6D5qK
3826 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3827 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3828 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3829 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3830 break;
3831
3832 // Assume operand order has been canonicalized
3833 if (Op1->getOperand(1) == Op2->getOperand(1) &&
3834 isa<ConstantInt>(Op1->getOperand(1)) &&
3835 !cast<ConstantInt>(Op1->getOperand(1))->isZero())
3836 return getOperands(0);
3837 break;
3838 }
3839 case Instruction::Shl: {
3840 // Same as multiplies, with the difference that we don't need to check
3841 // for a non-zero multiply. Shifts always multiply by non-zero.
3842 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3843 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3844 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3845 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3846 break;
3847
3848 if (Op1->getOperand(1) == Op2->getOperand(1))
3849 return getOperands(0);
3850 break;
3851 }
3852 case Instruction::AShr:
3853 case Instruction::LShr: {
3854 auto *PEO1 = cast<PossiblyExactOperator>(Op1);
3855 auto *PEO2 = cast<PossiblyExactOperator>(Op2);
3856 if (!PEO1->isExact() || !PEO2->isExact())
3857 break;
3858
3859 if (Op1->getOperand(1) == Op2->getOperand(1))
3860 return getOperands(0);
3861 break;
3862 }
3863 case Instruction::SExt:
3864 case Instruction::ZExt:
3865 if (Op1->getOperand(0)->getType() == Op2->getOperand(0)->getType())
3866 return getOperands(0);
3867 break;
3868 case Instruction::PHI: {
3869 const PHINode *PN1 = cast<PHINode>(Op1);
3870 const PHINode *PN2 = cast<PHINode>(Op2);
3871
3872 // If PN1 and PN2 are both recurrences, can we prove the entire recurrences
3873 // are a single invertible function of the start values? Note that repeated
3874 // application of an invertible function is also invertible
3875 BinaryOperator *BO1 = nullptr;
3876 Value *Start1 = nullptr, *Step1 = nullptr;
3877 BinaryOperator *BO2 = nullptr;
3878 Value *Start2 = nullptr, *Step2 = nullptr;
3879 if (PN1->getParent() != PN2->getParent() ||
3880 !matchSimpleRecurrence(PN1, BO1, Start1, Step1) ||
3881 !matchSimpleRecurrence(PN2, BO2, Start2, Step2))
3882 break;
3883
3884 auto Values = getInvertibleOperands(cast<Operator>(BO1),
3885 cast<Operator>(BO2));
3886 if (!Values)
3887 break;
3888
3889 // We have to be careful of mutually defined recurrences here. Ex:
3890 // * X_i = X_(i-1) OP Y_(i-1), and Y_i = X_(i-1) OP V
3891 // * X_i = Y_i = X_(i-1) OP Y_(i-1)
3892 // The invertibility of these is complicated, and not worth reasoning
3893 // about (yet?).
3894 if (Values->first != PN1 || Values->second != PN2)
3895 break;
3896
3897 return std::make_pair(Start1, Start2);
3898 }
3899 }
3900 return std::nullopt;
3901}
3902
3903/// Return true if V1 == (binop V2, X), where X is known non-zero.
3904/// Only handle a small subset of binops where (binop V2, X) with non-zero X
3905/// implies V2 != V1.
3906static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2,
3907 const APInt &DemandedElts,
3908 const SimplifyQuery &Q, unsigned Depth) {
3910 if (!BO)
3911 return false;
3912 switch (BO->getOpcode()) {
3913 default:
3914 break;
3915 case Instruction::Or:
3916 if (!cast<PossiblyDisjointInst>(V1)->isDisjoint())
3917 break;
3918 [[fallthrough]];
3919 case Instruction::Xor:
3920 case Instruction::Add:
3921 Value *Op = nullptr;
3922 if (V2 == BO->getOperand(0))
3923 Op = BO->getOperand(1);
3924 else if (V2 == BO->getOperand(1))
3925 Op = BO->getOperand(0);
3926 else
3927 return false;
3928 return isKnownNonZero(Op, DemandedElts, Q, Depth + 1);
3929 }
3930 return false;
3931}
3932
3933/// Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and
3934/// the multiplication is nuw or nsw.
3935static bool isNonEqualMul(const Value *V1, const Value *V2,
3936 const APInt &DemandedElts, const SimplifyQuery &Q,
3937 unsigned Depth) {
3938 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3939 const APInt *C;
3940 return match(OBO, m_Mul(m_Specific(V1), m_APInt(C))) &&
3941 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3942 !C->isZero() && !C->isOne() &&
3943 isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3944 }
3945 return false;
3946}
3947
3948/// Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and
3949/// the shift is nuw or nsw.
3950static bool isNonEqualShl(const Value *V1, const Value *V2,
3951 const APInt &DemandedElts, const SimplifyQuery &Q,
3952 unsigned Depth) {
3953 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3954 const APInt *C;
3955 return match(OBO, m_Shl(m_Specific(V1), m_APInt(C))) &&
3956 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3957 !C->isZero() && isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3958 }
3959 return false;
3960}
3961
3962static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2,
3963 const APInt &DemandedElts, const SimplifyQuery &Q,
3964 unsigned Depth) {
3965 // Check two PHIs are in same block.
3966 if (PN1->getParent() != PN2->getParent())
3967 return false;
3968
3970 bool UsedFullRecursion = false;
3971 for (const BasicBlock *IncomBB : PN1->blocks()) {
3972 if (!VisitedBBs.insert(IncomBB).second)
3973 continue; // Don't reprocess blocks that we have dealt with already.
3974 const Value *IV1 = PN1->getIncomingValueForBlock(IncomBB);
3975 const Value *IV2 = PN2->getIncomingValueForBlock(IncomBB);
3976 const APInt *C1, *C2;
3977 if (match(IV1, m_APInt(C1)) && match(IV2, m_APInt(C2)) && *C1 != *C2)
3978 continue;
3979
3980 // Only one pair of phi operands is allowed for full recursion.
3981 if (UsedFullRecursion)
3982 return false;
3983
3985 RecQ.CxtI = IncomBB->getTerminator();
3986 if (!isKnownNonEqual(IV1, IV2, DemandedElts, RecQ, Depth + 1))
3987 return false;
3988 UsedFullRecursion = true;
3989 }
3990 return true;
3991}
3992
3993static bool isNonEqualSelect(const Value *V1, const Value *V2,
3994 const APInt &DemandedElts, const SimplifyQuery &Q,
3995 unsigned Depth) {
3996 const SelectInst *SI1 = dyn_cast<SelectInst>(V1);
3997 if (!SI1)
3998 return false;
3999
4000 if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2)) {
4001 const Value *Cond1 = SI1->getCondition();
4002 const Value *Cond2 = SI2->getCondition();
4003 if (Cond1 == Cond2)
4004 return isKnownNonEqual(SI1->getTrueValue(), SI2->getTrueValue(),
4005 DemandedElts, Q, Depth + 1) &&
4006 isKnownNonEqual(SI1->getFalseValue(), SI2->getFalseValue(),
4007 DemandedElts, Q, Depth + 1);
4008 }
4009 return isKnownNonEqual(SI1->getTrueValue(), V2, DemandedElts, Q, Depth + 1) &&
4010 isKnownNonEqual(SI1->getFalseValue(), V2, DemandedElts, Q, Depth + 1);
4011}
4012
4013// Check to see if A is both a GEP and is the incoming value for a PHI in the
4014// loop, and B is either a ptr or another GEP. If the PHI has 2 incoming values,
4015// one of them being the recursive GEP A and the other a ptr at same base and at
4016// the same/higher offset than B we are only incrementing the pointer further in
4017// loop if offset of recursive GEP is greater than 0.
4019 const SimplifyQuery &Q) {
4020 if (!A->getType()->isPointerTy() || !B->getType()->isPointerTy())
4021 return false;
4022
4023 auto *GEPA = dyn_cast<GEPOperator>(A);
4024 if (!GEPA || GEPA->getNumIndices() != 1 || !isa<Constant>(GEPA->idx_begin()))
4025 return false;
4026
4027 // Handle 2 incoming PHI values with one being a recursive GEP.
4028 auto *PN = dyn_cast<PHINode>(GEPA->getPointerOperand());
4029 if (!PN || PN->getNumIncomingValues() != 2)
4030 return false;
4031
4032 // Search for the recursive GEP as an incoming operand, and record that as
4033 // Step.
4034 Value *Start = nullptr;
4035 Value *Step = const_cast<Value *>(A);
4036 if (PN->getIncomingValue(0) == Step)
4037 Start = PN->getIncomingValue(1);
4038 else if (PN->getIncomingValue(1) == Step)
4039 Start = PN->getIncomingValue(0);
4040 else
4041 return false;
4042
4043 // Other incoming node base should match the B base.
4044 // StartOffset >= OffsetB && StepOffset > 0?
4045 // StartOffset <= OffsetB && StepOffset < 0?
4046 // Is non-equal if above are true.
4047 // We use stripAndAccumulateInBoundsConstantOffsets to restrict the
4048 // optimisation to inbounds GEPs only.
4049 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(Start->getType());
4050 APInt StartOffset(IndexWidth, 0);
4051 Start = Start->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StartOffset);
4052 APInt StepOffset(IndexWidth, 0);
4053 Step = Step->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StepOffset);
4054
4055 // Check if Base Pointer of Step matches the PHI.
4056 if (Step != PN)
4057 return false;
4058 APInt OffsetB(IndexWidth, 0);
4059 B = B->stripAndAccumulateInBoundsConstantOffsets(Q.DL, OffsetB);
4060 return Start == B &&
4061 ((StartOffset.sge(OffsetB) && StepOffset.isStrictlyPositive()) ||
4062 (StartOffset.sle(OffsetB) && StepOffset.isNegative()));
4063}
4064
4065static bool isKnownNonEqualFromContext(const Value *V1, const Value *V2,
4066 const SimplifyQuery &Q, unsigned Depth) {
4067 if (!Q.CxtI)
4068 return false;
4069
4070 // Try to infer NonEqual based on information from dominating conditions.
4071 if (Q.DC && Q.DT) {
4072 auto IsKnownNonEqualFromDominatingCondition = [&](const Value *V) {
4073 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
4074 Value *Cond = BI->getCondition();
4075 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4076 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()) &&
4078 /*LHSIsTrue=*/true, Depth)
4079 .value_or(false))
4080 return true;
4081
4082 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4083 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()) &&
4085 /*LHSIsTrue=*/false, Depth)
4086 .value_or(false))
4087 return true;
4088 }
4089
4090 return false;
4091 };
4092
4093 if (IsKnownNonEqualFromDominatingCondition(V1) ||
4094 IsKnownNonEqualFromDominatingCondition(V2))
4095 return true;
4096 }
4097
4098 if (!Q.AC)
4099 return false;
4100
4101 // Try to infer NonEqual based on information from assumptions.
4102 for (auto &AssumeVH : Q.AC->assumptionsFor(V1)) {
4103 if (!AssumeVH)
4104 continue;
4105 CallInst *I = cast<CallInst>(AssumeVH);
4106
4107 assert(I->getFunction() == Q.CxtI->getFunction() &&
4108 "Got assumption for the wrong function!");
4109 assert(I->getIntrinsicID() == Intrinsic::assume &&
4110 "must be an assume intrinsic");
4111
4112 if (isImpliedCondition(I->getArgOperand(0), ICmpInst::ICMP_NE, V1, V2, Q.DL,
4113 /*LHSIsTrue=*/true, Depth)
4114 .value_or(false) &&
4116 return true;
4117 }
4118
4119 return false;
4120}
4121
4122/// Return true if it is known that V1 != V2.
4123static bool isKnownNonEqual(const Value *V1, const Value *V2,
4124 const APInt &DemandedElts, const SimplifyQuery &Q,
4125 unsigned Depth) {
4126 if (V1 == V2)
4127 return false;
4128 if (V1->getType() != V2->getType())
4129 // We can't look through casts yet.
4130 return false;
4131
4133 return false;
4134
4135 // See if we can recurse through (exactly one of) our operands. This
4136 // requires our operation be 1-to-1 and map every input value to exactly
4137 // one output value. Such an operation is invertible.
4138 auto *O1 = dyn_cast<Operator>(V1);
4139 auto *O2 = dyn_cast<Operator>(V2);
4140 if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) {
4141 if (auto Values = getInvertibleOperands(O1, O2))
4142 return isKnownNonEqual(Values->first, Values->second, DemandedElts, Q,
4143 Depth + 1);
4144
4145 if (const PHINode *PN1 = dyn_cast<PHINode>(V1)) {
4146 const PHINode *PN2 = cast<PHINode>(V2);
4147 // FIXME: This is missing a generalization to handle the case where one is
4148 // a PHI and another one isn't.
4149 if (isNonEqualPHIs(PN1, PN2, DemandedElts, Q, Depth))
4150 return true;
4151 };
4152 }
4153
4154 if (isModifyingBinopOfNonZero(V1, V2, DemandedElts, Q, Depth) ||
4155 isModifyingBinopOfNonZero(V2, V1, DemandedElts, Q, Depth))
4156 return true;
4157
4158 if (isNonEqualMul(V1, V2, DemandedElts, Q, Depth) ||
4159 isNonEqualMul(V2, V1, DemandedElts, Q, Depth))
4160 return true;
4161
4162 if (isNonEqualShl(V1, V2, DemandedElts, Q, Depth) ||
4163 isNonEqualShl(V2, V1, DemandedElts, Q, Depth))
4164 return true;
4165
4166 if (V1->getType()->isIntOrIntVectorTy()) {
4167 // Are any known bits in V1 contradictory to known bits in V2? If V1
4168 // has a known zero where V2 has a known one, they must not be equal.
4169 KnownBits Known1 = computeKnownBits(V1, DemandedElts, Q, Depth);
4170 if (!Known1.isUnknown()) {
4171 KnownBits Known2 = computeKnownBits(V2, DemandedElts, Q, Depth);
4172 if (Known1.Zero.intersects(Known2.One) ||
4173 Known2.Zero.intersects(Known1.One))
4174 return true;
4175 }
4176 }
4177
4178 if (isNonEqualSelect(V1, V2, DemandedElts, Q, Depth) ||
4179 isNonEqualSelect(V2, V1, DemandedElts, Q, Depth))
4180 return true;
4181
4182 if (isNonEqualPointersWithRecursiveGEP(V1, V2, Q) ||
4184 return true;
4185
4186 Value *A, *B;
4187 // PtrToInts are NonEqual if their Ptrs are NonEqual.
4188 // Check PtrToInt type matches the pointer size.
4189 if (match(V1, m_PtrToIntSameSize(Q.DL, m_Value(A))) &&
4191 return isKnownNonEqual(A, B, DemandedElts, Q, Depth + 1);
4192
4193 if (isKnownNonEqualFromContext(V1, V2, Q, Depth))
4194 return true;
4195
4196 return false;
4197}
4198
4199/// For vector constants, loop over the elements and find the constant with the
4200/// minimum number of sign bits. Return 0 if the value is not a vector constant
4201/// or if any element was not analyzed; otherwise, return the count for the
4202/// element with the minimum number of sign bits.
4204 const APInt &DemandedElts,
4205 unsigned TyBits) {
4206 const auto *CV = dyn_cast<Constant>(V);
4207 if (!CV || !isa<FixedVectorType>(CV->getType()))
4208 return 0;
4209
4210 unsigned MinSignBits = TyBits;
4211 unsigned NumElts = cast<FixedVectorType>(CV->getType())->getNumElements();
4212 for (unsigned i = 0; i != NumElts; ++i) {
4213 if (!DemandedElts[i])
4214 continue;
4215 // If we find a non-ConstantInt, bail out.
4216 auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));
4217 if (!Elt)
4218 return 0;
4219
4220 MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());
4221 }
4222
4223 return MinSignBits;
4224}
4225
4226static unsigned ComputeNumSignBitsImpl(const Value *V,
4227 const APInt &DemandedElts,
4228 const SimplifyQuery &Q, unsigned Depth);
4229
4230static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
4231 const SimplifyQuery &Q, unsigned Depth) {
4232 unsigned Result = ComputeNumSignBitsImpl(V, DemandedElts, Q, Depth);
4233 assert(Result > 0 && "At least one sign bit needs to be present!");
4234 return Result;
4235}
4236
4237/// Return the number of times the sign bit of the register is replicated into
4238/// the other bits. We know that at least 1 bit is always equal to the sign bit
4239/// (itself), but other cases can give us information. For example, immediately
4240/// after an "ashr X, 2", we know that the top 3 bits are all equal to each
4241/// other, so we return 3. For vectors, return the number of sign bits for the
4242/// vector element with the minimum number of known sign bits of the demanded
4243/// elements in the vector specified by DemandedElts.
4244static unsigned ComputeNumSignBitsImpl(const Value *V,
4245 const APInt &DemandedElts,
4246 const SimplifyQuery &Q, unsigned Depth) {
4247 Type *Ty = V->getType();
4248#ifndef NDEBUG
4249 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4250
4251 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
4252 assert(
4253 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
4254 "DemandedElt width should equal the fixed vector number of elements");
4255 } else {
4256 assert(DemandedElts == APInt(1, 1) &&
4257 "DemandedElt width should be 1 for scalars");
4258 }
4259#endif
4260
4261 // We return the minimum number of sign bits that are guaranteed to be present
4262 // in V, so for undef we have to conservatively return 1. We don't have the
4263 // same behavior for poison though -- that's a FIXME today.
4264
4265 Type *ScalarTy = Ty->getScalarType();
4266 unsigned TyBits = ScalarTy->isPointerTy() ?
4267 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
4268 Q.DL.getTypeSizeInBits(ScalarTy);
4269
4270 unsigned Tmp, Tmp2;
4271 unsigned FirstAnswer = 1;
4272
4273 // Note that ConstantInt is handled by the general computeKnownBits case
4274 // below.
4275
4277 return 1;
4278
4279 if (auto *U = dyn_cast<Operator>(V)) {
4280 switch (Operator::getOpcode(V)) {
4281 default: break;
4282 case Instruction::BitCast: {
4283 Value *Src = U->getOperand(0);
4284 Type *SrcTy = Src->getType();
4285
4286 // Skip if the source type is not an integer or integer vector type
4287 // This ensures we only process integer-like types
4288 if (!SrcTy->isIntOrIntVectorTy())
4289 break;
4290
4291 unsigned SrcBits = SrcTy->getScalarSizeInBits();
4292
4293 // Bitcast 'large element' scalar/vector to 'small element' vector.
4294 if ((SrcBits % TyBits) != 0)
4295 break;
4296
4297 // Only proceed if the destination type is a fixed-size vector
4298 if (isa<FixedVectorType>(Ty)) {
4299 // Fast case - sign splat can be simply split across the small elements.
4300 // This works for both vector and scalar sources
4301 Tmp = ComputeNumSignBits(Src, Q, Depth + 1);
4302 if (Tmp == SrcBits)
4303 return TyBits;
4304 }
4305 break;
4306 }
4307 case Instruction::SExt:
4308 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
4309 return ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1) +
4310 Tmp;
4311
4312 case Instruction::SDiv: {
4313 const APInt *Denominator;
4314 // sdiv X, C -> adds log(C) sign bits.
4315 if (match(U->getOperand(1), m_APInt(Denominator))) {
4316
4317 // Ignore non-positive denominator.
4318 if (!Denominator->isStrictlyPositive())
4319 break;
4320
4321 // Calculate the incoming numerator bits.
4322 unsigned NumBits =
4323 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4324
4325 // Add floor(log(C)) bits to the numerator bits.
4326 return std::min(TyBits, NumBits + Denominator->logBase2());
4327 }
4328 break;
4329 }
4330
4331 case Instruction::SRem: {
4332 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4333
4334 const APInt *Denominator;
4335 // srem X, C -> we know that the result is within [-C+1,C) when C is a
4336 // positive constant. This let us put a lower bound on the number of sign
4337 // bits.
4338 if (match(U->getOperand(1), m_APInt(Denominator))) {
4339
4340 // Ignore non-positive denominator.
4341 if (Denominator->isStrictlyPositive()) {
4342 // Calculate the leading sign bit constraints by examining the
4343 // denominator. Given that the denominator is positive, there are two
4344 // cases:
4345 //
4346 // 1. The numerator is positive. The result range is [0,C) and
4347 // [0,C) u< (1 << ceilLogBase2(C)).
4348 //
4349 // 2. The numerator is negative. Then the result range is (-C,0] and
4350 // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
4351 //
4352 // Thus a lower bound on the number of sign bits is `TyBits -
4353 // ceilLogBase2(C)`.
4354
4355 unsigned ResBits = TyBits - Denominator->ceilLogBase2();
4356 Tmp = std::max(Tmp, ResBits);
4357 }
4358 }
4359 return Tmp;
4360 }
4361
4362 case Instruction::AShr: {
4363 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4364 // ashr X, C -> adds C sign bits. Vectors too.
4365 const APInt *ShAmt;
4366 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4367 if (ShAmt->uge(TyBits))
4368 break; // Bad shift.
4369 unsigned ShAmtLimited = ShAmt->getZExtValue();
4370 Tmp += ShAmtLimited;
4371 if (Tmp > TyBits) Tmp = TyBits;
4372 }
4373 return Tmp;
4374 }
4375 case Instruction::Shl: {
4376 const APInt *ShAmt;
4377 Value *X = nullptr;
4378 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4379 // shl destroys sign bits.
4380 if (ShAmt->uge(TyBits))
4381 break; // Bad shift.
4382 // We can look through a zext (more or less treating it as a sext) if
4383 // all extended bits are shifted out.
4384 if (match(U->getOperand(0), m_ZExt(m_Value(X))) &&
4385 ShAmt->uge(TyBits - X->getType()->getScalarSizeInBits())) {
4386 Tmp = ComputeNumSignBits(X, DemandedElts, Q, Depth + 1);
4387 Tmp += TyBits - X->getType()->getScalarSizeInBits();
4388 } else
4389 Tmp =
4390 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4391 if (ShAmt->uge(Tmp))
4392 break; // Shifted all sign bits out.
4393 Tmp2 = ShAmt->getZExtValue();
4394 return Tmp - Tmp2;
4395 }
4396 break;
4397 }
4398 case Instruction::And:
4399 case Instruction::Or:
4400 case Instruction::Xor: // NOT is handled here.
4401 // Logical binary ops preserve the number of sign bits at the worst.
4402 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4403 if (Tmp != 1) {
4404 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4405 FirstAnswer = std::min(Tmp, Tmp2);
4406 // We computed what we know about the sign bits as our first
4407 // answer. Now proceed to the generic code that uses
4408 // computeKnownBits, and pick whichever answer is better.
4409 }
4410 break;
4411
4412 case Instruction::Select: {
4413 // If we have a clamp pattern, we know that the number of sign bits will
4414 // be the minimum of the clamp min/max range.
4415 const Value *X;
4416 const APInt *CLow, *CHigh;
4417 if (isSignedMinMaxClamp(U, X, CLow, CHigh))
4418 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4419
4420 Tmp = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4421 if (Tmp == 1)
4422 break;
4423 Tmp2 = ComputeNumSignBits(U->getOperand(2), DemandedElts, Q, Depth + 1);
4424 return std::min(Tmp, Tmp2);
4425 }
4426
4427 case Instruction::Add:
4428 // Add can have at most one carry bit. Thus we know that the output
4429 // is, at worst, one more bit than the inputs.
4430 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4431 if (Tmp == 1) break;
4432
4433 // Special case decrementing a value (ADD X, -1):
4434 if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
4435 if (CRHS->isAllOnesValue()) {
4436 KnownBits Known(TyBits);
4437 computeKnownBits(U->getOperand(0), DemandedElts, Known, Q, Depth + 1);
4438
4439 // If the input is known to be 0 or 1, the output is 0/-1, which is
4440 // all sign bits set.
4441 if ((Known.Zero | 1).isAllOnes())
4442 return TyBits;
4443
4444 // If we are subtracting one from a positive number, there is no carry
4445 // out of the result.
4446 if (Known.isNonNegative())
4447 return Tmp;
4448 }
4449
4450 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4451 if (Tmp2 == 1)
4452 break;
4453 return std::min(Tmp, Tmp2) - 1;
4454
4455 case Instruction::Sub:
4456 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4457 if (Tmp2 == 1)
4458 break;
4459
4460 // Handle NEG.
4461 if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
4462 if (CLHS->isNullValue()) {
4463 KnownBits Known(TyBits);
4464 computeKnownBits(U->getOperand(1), DemandedElts, Known, Q, Depth + 1);
4465 // If the input is known to be 0 or 1, the output is 0/-1, which is
4466 // all sign bits set.
4467 if ((Known.Zero | 1).isAllOnes())
4468 return TyBits;
4469
4470 // If the input is known to be positive (the sign bit is known clear),
4471 // the output of the NEG has the same number of sign bits as the
4472 // input.
4473 if (Known.isNonNegative())
4474 return Tmp2;
4475
4476 // Otherwise, we treat this like a SUB.
4477 }
4478
4479 // Sub can have at most one carry bit. Thus we know that the output
4480 // is, at worst, one more bit than the inputs.
4481 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4482 if (Tmp == 1)
4483 break;
4484 return std::min(Tmp, Tmp2) - 1;
4485
4486 case Instruction::Mul: {
4487 // The output of the Mul can be at most twice the valid bits in the
4488 // inputs.
4489 unsigned SignBitsOp0 =
4490 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4491 if (SignBitsOp0 == 1)
4492 break;
4493 unsigned SignBitsOp1 =
4494 ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4495 if (SignBitsOp1 == 1)
4496 break;
4497 unsigned OutValidBits =
4498 (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);
4499 return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
4500 }
4501
4502 case Instruction::PHI: {
4503 const PHINode *PN = cast<PHINode>(U);
4504 unsigned NumIncomingValues = PN->getNumIncomingValues();
4505 // Don't analyze large in-degree PHIs.
4506 if (NumIncomingValues > 4) break;
4507 // Unreachable blocks may have zero-operand PHI nodes.
4508 if (NumIncomingValues == 0) break;
4509
4510 // Take the minimum of all incoming values. This can't infinitely loop
4511 // because of our depth threshold.
4513 Tmp = TyBits;
4514 for (unsigned i = 0, e = NumIncomingValues; i != e; ++i) {
4515 if (Tmp == 1) return Tmp;
4516 RecQ.CxtI = PN->getIncomingBlock(i)->getTerminator();
4517 Tmp = std::min(Tmp, ComputeNumSignBits(PN->getIncomingValue(i),
4518 DemandedElts, RecQ, Depth + 1));
4519 }
4520 return Tmp;
4521 }
4522
4523 case Instruction::Trunc: {
4524 // If the input contained enough sign bits that some remain after the
4525 // truncation, then we can make use of that. Otherwise we don't know
4526 // anything.
4527 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4528 unsigned OperandTyBits = U->getOperand(0)->getType()->getScalarSizeInBits();
4529 if (Tmp > (OperandTyBits - TyBits))
4530 return Tmp - (OperandTyBits - TyBits);
4531
4532 return 1;
4533 }
4534
4535 case Instruction::ExtractElement:
4536 // Look through extract element. At the moment we keep this simple and
4537 // skip tracking the specific element. But at least we might find
4538 // information valid for all elements of the vector (for example if vector
4539 // is sign extended, shifted, etc).
4540 return ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4541
4542 case Instruction::ShuffleVector: {
4543 // Collect the minimum number of sign bits that are shared by every vector
4544 // element referenced by the shuffle.
4545 auto *Shuf = dyn_cast<ShuffleVectorInst>(U);
4546 if (!Shuf) {
4547 // FIXME: Add support for shufflevector constant expressions.
4548 return 1;
4549 }
4550 APInt DemandedLHS, DemandedRHS;
4551 // For undef elements, we don't know anything about the common state of
4552 // the shuffle result.
4553 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
4554 return 1;
4555 Tmp = std::numeric_limits<unsigned>::max();
4556 if (!!DemandedLHS) {
4557 const Value *LHS = Shuf->getOperand(0);
4558 Tmp = ComputeNumSignBits(LHS, DemandedLHS, Q, Depth + 1);
4559 }
4560 // If we don't know anything, early out and try computeKnownBits
4561 // fall-back.
4562 if (Tmp == 1)
4563 break;
4564 if (!!DemandedRHS) {
4565 const Value *RHS = Shuf->getOperand(1);
4566 Tmp2 = ComputeNumSignBits(RHS, DemandedRHS, Q, Depth + 1);
4567 Tmp = std::min(Tmp, Tmp2);
4568 }
4569 // If we don't know anything, early out and try computeKnownBits
4570 // fall-back.
4571 if (Tmp == 1)
4572 break;
4573 assert(Tmp <= TyBits && "Failed to determine minimum sign bits");
4574 return Tmp;
4575 }
4576 case Instruction::Call: {
4577 if (const auto *II = dyn_cast<IntrinsicInst>(U)) {
4578 switch (II->getIntrinsicID()) {
4579 default:
4580 break;
4581 case Intrinsic::abs:
4582 Tmp =
4583 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4584 if (Tmp == 1)
4585 break;
4586
4587 // Absolute value reduces number of sign bits by at most 1.
4588 return Tmp - 1;
4589 case Intrinsic::smin:
4590 case Intrinsic::smax: {
4591 const APInt *CLow, *CHigh;
4592 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
4593 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4594 }
4595 }
4596 }
4597 }
4598 }
4599 }
4600
4601 // Finally, if we can prove that the top bits of the result are 0's or 1's,
4602 // use this information.
4603
4604 // If we can examine all elements of a vector constant successfully, we're
4605 // done (we can't do any better than that). If not, keep trying.
4606 if (unsigned VecSignBits =
4607 computeNumSignBitsVectorConstant(V, DemandedElts, TyBits))
4608 return VecSignBits;
4609
4610 KnownBits Known(TyBits);
4611 computeKnownBits(V, DemandedElts, Known, Q, Depth);
4612
4613 // If we know that the sign bit is either zero or one, determine the number of
4614 // identical bits in the top of the input value.
4615 return std::max(FirstAnswer, Known.countMinSignBits());
4616}
4617
4619 const TargetLibraryInfo *TLI) {
4620 const Function *F = CB.getCalledFunction();
4621 if (!F)
4623
4624 if (F->isIntrinsic())
4625 return F->getIntrinsicID();
4626
4627 // We are going to infer semantics of a library function based on mapping it
4628 // to an LLVM intrinsic. Check that the library function is available from
4629 // this callbase and in this environment.
4630 LibFunc Func;
4631 if (F->hasLocalLinkage() || !TLI || !TLI->getLibFunc(CB, Func) ||
4632 !CB.onlyReadsMemory())
4634
4635 switch (Func) {
4636 default:
4637 break;
4638 case LibFunc_sin:
4639 case LibFunc_sinf:
4640 case LibFunc_sinl:
4641 return Intrinsic::sin;
4642 case LibFunc_cos:
4643 case LibFunc_cosf:
4644 case LibFunc_cosl:
4645 return Intrinsic::cos;
4646 case LibFunc_tan:
4647 case LibFunc_tanf:
4648 case LibFunc_tanl:
4649 return Intrinsic::tan;
4650 case LibFunc_asin:
4651 case LibFunc_asinf:
4652 case LibFunc_asinl:
4653 return Intrinsic::asin;
4654 case LibFunc_acos:
4655 case LibFunc_acosf:
4656 case LibFunc_acosl:
4657 return Intrinsic::acos;
4658 case LibFunc_atan:
4659 case LibFunc_atanf:
4660 case LibFunc_atanl:
4661 return Intrinsic::atan;
4662 case LibFunc_atan2:
4663 case LibFunc_atan2f:
4664 case LibFunc_atan2l:
4665 return Intrinsic::atan2;
4666 case LibFunc_sinh:
4667 case LibFunc_sinhf:
4668 case LibFunc_sinhl:
4669 return Intrinsic::sinh;
4670 case LibFunc_cosh:
4671 case LibFunc_coshf:
4672 case LibFunc_coshl:
4673 return Intrinsic::cosh;
4674 case LibFunc_tanh:
4675 case LibFunc_tanhf:
4676 case LibFunc_tanhl:
4677 return Intrinsic::tanh;
4678 case LibFunc_exp:
4679 case LibFunc_expf:
4680 case LibFunc_expl:
4681 return Intrinsic::exp;
4682 case LibFunc_exp2:
4683 case LibFunc_exp2f:
4684 case LibFunc_exp2l:
4685 return Intrinsic::exp2;
4686 case LibFunc_exp10:
4687 case LibFunc_exp10f:
4688 case LibFunc_exp10l:
4689 return Intrinsic::exp10;
4690 case LibFunc_log:
4691 case LibFunc_logf:
4692 case LibFunc_logl:
4693 return Intrinsic::log;
4694 case LibFunc_log10:
4695 case LibFunc_log10f:
4696 case LibFunc_log10l:
4697 return Intrinsic::log10;
4698 case LibFunc_log2:
4699 case LibFunc_log2f:
4700 case LibFunc_log2l:
4701 return Intrinsic::log2;
4702 case LibFunc_fabs:
4703 case LibFunc_fabsf:
4704 case LibFunc_fabsl:
4705 return Intrinsic::fabs;
4706 case LibFunc_fmin:
4707 case LibFunc_fminf:
4708 case LibFunc_fminl:
4709 return Intrinsic::minnum;
4710 case LibFunc_fmax:
4711 case LibFunc_fmaxf:
4712 case LibFunc_fmaxl:
4713 return Intrinsic::maxnum;
4714 case LibFunc_copysign:
4715 case LibFunc_copysignf:
4716 case LibFunc_copysignl:
4717 return Intrinsic::copysign;
4718 case LibFunc_floor:
4719 case LibFunc_floorf:
4720 case LibFunc_floorl:
4721 return Intrinsic::floor;
4722 case LibFunc_ceil:
4723 case LibFunc_ceilf:
4724 case LibFunc_ceill:
4725 return Intrinsic::ceil;
4726 case LibFunc_trunc:
4727 case LibFunc_truncf:
4728 case LibFunc_truncl:
4729 return Intrinsic::trunc;
4730 case LibFunc_rint:
4731 case LibFunc_rintf:
4732 case LibFunc_rintl:
4733 return Intrinsic::rint;
4734 case LibFunc_nearbyint:
4735 case LibFunc_nearbyintf:
4736 case LibFunc_nearbyintl:
4737 return Intrinsic::nearbyint;
4738 case LibFunc_round:
4739 case LibFunc_roundf:
4740 case LibFunc_roundl:
4741 return Intrinsic::round;
4742 case LibFunc_roundeven:
4743 case LibFunc_roundevenf:
4744 case LibFunc_roundevenl:
4745 return Intrinsic::roundeven;
4746 case LibFunc_pow:
4747 case LibFunc_powf:
4748 case LibFunc_powl:
4749 return Intrinsic::pow;
4750 case LibFunc_sqrt:
4751 case LibFunc_sqrtf:
4752 case LibFunc_sqrtl:
4753 return Intrinsic::sqrt;
4754 }
4755
4757}
4758
4759/// Given an exploded icmp instruction, return true if the comparison only
4760/// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if
4761/// the result of the comparison is true when the input value is signed.
4763 bool &TrueIfSigned) {
4764 switch (Pred) {
4765 case ICmpInst::ICMP_SLT: // True if LHS s< 0
4766 TrueIfSigned = true;
4767 return RHS.isZero();
4768 case ICmpInst::ICMP_SLE: // True if LHS s<= -1
4769 TrueIfSigned = true;
4770 return RHS.isAllOnes();
4771 case ICmpInst::ICMP_SGT: // True if LHS s> -1
4772 TrueIfSigned = false;
4773 return RHS.isAllOnes();
4774 case ICmpInst::ICMP_SGE: // True if LHS s>= 0
4775 TrueIfSigned = false;
4776 return RHS.isZero();
4777 case ICmpInst::ICMP_UGT:
4778 // True if LHS u> RHS and RHS == sign-bit-mask - 1
4779 TrueIfSigned = true;
4780 return RHS.isMaxSignedValue();
4781 case ICmpInst::ICMP_UGE:
4782 // True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4783 TrueIfSigned = true;
4784 return RHS.isMinSignedValue();
4785 case ICmpInst::ICMP_ULT:
4786 // True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4787 TrueIfSigned = false;
4788 return RHS.isMinSignedValue();
4789 case ICmpInst::ICMP_ULE:
4790 // True if LHS u<= RHS and RHS == sign-bit-mask - 1
4791 TrueIfSigned = false;
4792 return RHS.isMaxSignedValue();
4793 default:
4794 return false;
4795 }
4796}
4797
4799 bool CondIsTrue,
4800 const Instruction *CxtI,
4801 KnownFPClass &KnownFromContext,
4802 unsigned Depth = 0) {
4803 Value *A, *B;
4805 (CondIsTrue ? match(Cond, m_LogicalAnd(m_Value(A), m_Value(B)))
4806 : match(Cond, m_LogicalOr(m_Value(A), m_Value(B))))) {
4807 computeKnownFPClassFromCond(V, A, CondIsTrue, CxtI, KnownFromContext,
4808 Depth + 1);
4809 computeKnownFPClassFromCond(V, B, CondIsTrue, CxtI, KnownFromContext,
4810 Depth + 1);
4811 return;
4812 }
4814 computeKnownFPClassFromCond(V, A, !CondIsTrue, CxtI, KnownFromContext,
4815 Depth + 1);
4816 return;
4817 }
4818 CmpPredicate Pred;
4819 Value *LHS;
4820 uint64_t ClassVal = 0;
4821 const APFloat *CRHS;
4822 const APInt *RHS;
4823 if (match(Cond, m_FCmp(Pred, m_Value(LHS), m_APFloat(CRHS)))) {
4824 auto [CmpVal, MaskIfTrue, MaskIfFalse] = fcmpImpliesClass(
4825 Pred, *cast<Instruction>(Cond)->getParent()->getParent(), LHS, *CRHS,
4826 LHS != V);
4827 if (CmpVal == V)
4828 KnownFromContext.knownNot(~(CondIsTrue ? MaskIfTrue : MaskIfFalse));
4830 m_Specific(V), m_ConstantInt(ClassVal)))) {
4831 FPClassTest Mask = static_cast<FPClassTest>(ClassVal);
4832 KnownFromContext.knownNot(CondIsTrue ? ~Mask : Mask);
4833 } else if (match(Cond, m_ICmp(Pred, m_ElementWiseBitCast(m_Specific(V)),
4834 m_APInt(RHS)))) {
4835 bool TrueIfSigned;
4836 if (!isSignBitCheck(Pred, *RHS, TrueIfSigned))
4837 return;
4838 if (TrueIfSigned == CondIsTrue)
4839 KnownFromContext.signBitMustBeOne();
4840 else
4841 KnownFromContext.signBitMustBeZero();
4842 }
4843}
4844
4846 const SimplifyQuery &Q) {
4847 KnownFPClass KnownFromContext;
4848
4849 if (Q.CC && Q.CC->AffectedValues.contains(V))
4851 KnownFromContext);
4852
4853 if (!Q.CxtI)
4854 return KnownFromContext;
4855
4856 if (Q.DC && Q.DT) {
4857 // Handle dominating conditions.
4858 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
4859 Value *Cond = BI->getCondition();
4860
4861 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4862 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
4863 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/true, Q.CxtI,
4864 KnownFromContext);
4865
4866 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4867 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
4868 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/false, Q.CxtI,
4869 KnownFromContext);
4870 }
4871 }
4872
4873 if (!Q.AC)
4874 return KnownFromContext;
4875
4876 // Try to restrict the floating-point classes based on information from
4877 // assumptions.
4878 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
4879 if (!AssumeVH)
4880 continue;
4881 CallInst *I = cast<CallInst>(AssumeVH);
4882
4883 assert(I->getFunction() == Q.CxtI->getParent()->getParent() &&
4884 "Got assumption for the wrong function!");
4885 assert(I->getIntrinsicID() == Intrinsic::assume &&
4886 "must be an assume intrinsic");
4887
4888 if (!isValidAssumeForContext(I, Q))
4889 continue;
4890
4891 computeKnownFPClassFromCond(V, I->getArgOperand(0),
4892 /*CondIsTrue=*/true, Q.CxtI, KnownFromContext);
4893 }
4894
4895 return KnownFromContext;
4896}
4897
4899 Value *Arm, bool Invert,
4900 const SimplifyQuery &SQ,
4901 unsigned Depth) {
4902
4903 KnownFPClass KnownSrc;
4905 /*CondIsTrue=*/!Invert, SQ.CxtI, KnownSrc,
4906 Depth + 1);
4907 KnownSrc = KnownSrc.unionWith(Known);
4908 if (KnownSrc.isUnknown())
4909 return;
4910
4911 if (isGuaranteedNotToBeUndef(Arm, SQ.AC, SQ.CxtI, SQ.DT, Depth + 1))
4912 Known = KnownSrc;
4913}
4914
4915void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4916 FPClassTest InterestedClasses, KnownFPClass &Known,
4917 const SimplifyQuery &Q, unsigned Depth);
4918
4919static void computeKnownFPClass(const Value *V, KnownFPClass &Known,
4920 FPClassTest InterestedClasses,
4921 const SimplifyQuery &Q, unsigned Depth) {
4922 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
4923 APInt DemandedElts =
4924 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
4925 computeKnownFPClass(V, DemandedElts, InterestedClasses, Known, Q, Depth);
4926}
4927
4929 const APInt &DemandedElts,
4930 FPClassTest InterestedClasses,
4931 KnownFPClass &Known,
4932 const SimplifyQuery &Q,
4933 unsigned Depth) {
4934 if ((InterestedClasses &
4936 return;
4937
4938 KnownFPClass KnownSrc;
4939 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
4940 KnownSrc, Q, Depth + 1);
4941 Known = KnownFPClass::fptrunc(KnownSrc);
4942}
4943
4945 switch (IID) {
4946 case Intrinsic::minimum:
4948 case Intrinsic::maximum:
4950 case Intrinsic::minimumnum:
4952 case Intrinsic::maximumnum:
4954 case Intrinsic::minnum:
4956 case Intrinsic::maxnum:
4958 default:
4959 llvm_unreachable("not a floating-point min-max intrinsic");
4960 }
4961}
4962
4963/// \return true if this is a floating point value that is known to have a
4964/// magnitude smaller than 1. i.e., fabs(X) <= 1.0 or is nan.
4965static bool isAbsoluteValueULEOne(const Value *V) {
4966 // TODO: Handle frexp
4967 // TODO: Other rounding intrinsics?
4968
4969 // fabs(x - floor(x)) <= 1
4970 const Value *SubFloorX;
4971 if (match(V, m_FSub(m_Value(SubFloorX),
4973 return true;
4974
4977}
4978
4979void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4980 FPClassTest InterestedClasses, KnownFPClass &Known,
4981 const SimplifyQuery &Q, unsigned Depth) {
4982 assert(Known.isUnknown() && "should not be called with known information");
4983
4984 if (!DemandedElts) {
4985 // No demanded elts, better to assume we don't know anything.
4986 Known.resetAll();
4987 return;
4988 }
4989
4990 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4991
4992 if (auto *CFP = dyn_cast<ConstantFP>(V)) {
4993 Known = KnownFPClass(CFP->getValueAPF());
4994 return;
4995 }
4996
4998 Known.KnownFPClasses = fcPosZero;
4999 Known.SignBit = false;
5000 return;
5001 }
5002
5003 if (isa<PoisonValue>(V)) {
5004 Known.KnownFPClasses = fcNone;
5005 Known.SignBit = false;
5006 return;
5007 }
5008
5009 // Try to handle fixed width vector constants
5010 auto *VFVTy = dyn_cast<FixedVectorType>(V->getType());
5011 const Constant *CV = dyn_cast<Constant>(V);
5012 if (VFVTy && CV) {
5013 Known.KnownFPClasses = fcNone;
5014 bool SignBitAllZero = true;
5015 bool SignBitAllOne = true;
5016
5017 // For vectors, verify that each element is not NaN.
5018 unsigned NumElts = VFVTy->getNumElements();
5019 for (unsigned i = 0; i != NumElts; ++i) {
5020 if (!DemandedElts[i])
5021 continue;
5022
5023 Constant *Elt = CV->getAggregateElement(i);
5024 if (!Elt) {
5025 Known = KnownFPClass();
5026 return;
5027 }
5028 if (isa<PoisonValue>(Elt))
5029 continue;
5030 auto *CElt = dyn_cast<ConstantFP>(Elt);
5031 if (!CElt) {
5032 Known = KnownFPClass();
5033 return;
5034 }
5035
5036 const APFloat &C = CElt->getValueAPF();
5037 Known.KnownFPClasses |= C.classify();
5038 if (C.isNegative())
5039 SignBitAllZero = false;
5040 else
5041 SignBitAllOne = false;
5042 }
5043 if (SignBitAllOne != SignBitAllZero)
5044 Known.SignBit = SignBitAllOne;
5045 return;
5046 }
5047
5048 if (const auto *CDS = dyn_cast<ConstantDataSequential>(V)) {
5049 Known.KnownFPClasses = fcNone;
5050 for (size_t I = 0, E = CDS->getNumElements(); I != E; ++I)
5051 Known |= CDS->getElementAsAPFloat(I).classify();
5052 return;
5053 }
5054
5055 if (const auto *CA = dyn_cast<ConstantAggregate>(V)) {
5056 // TODO: Handle complex aggregates
5057 Known.KnownFPClasses = fcNone;
5058 for (const Use &Op : CA->operands()) {
5059 auto *CFP = dyn_cast<ConstantFP>(Op.get());
5060 if (!CFP) {
5061 Known = KnownFPClass();
5062 return;
5063 }
5064
5065 Known |= CFP->getValueAPF().classify();
5066 }
5067
5068 return;
5069 }
5070
5071 FPClassTest KnownNotFromFlags = fcNone;
5072 if (const auto *CB = dyn_cast<CallBase>(V))
5073 KnownNotFromFlags |= CB->getRetNoFPClass();
5074 else if (const auto *Arg = dyn_cast<Argument>(V))
5075 KnownNotFromFlags |= Arg->getNoFPClass();
5076
5077 const Operator *Op = dyn_cast<Operator>(V);
5079 if (FPOp->hasNoNaNs())
5080 KnownNotFromFlags |= fcNan;
5081 if (FPOp->hasNoInfs())
5082 KnownNotFromFlags |= fcInf;
5083 }
5084
5085 KnownFPClass AssumedClasses = computeKnownFPClassFromContext(V, Q);
5086 KnownNotFromFlags |= ~AssumedClasses.KnownFPClasses;
5087
5088 // We no longer need to find out about these bits from inputs if we can
5089 // assume this from flags/attributes.
5090 InterestedClasses &= ~KnownNotFromFlags;
5091
5092 llvm::scope_exit ClearClassesFromFlags([=, &Known] {
5093 Known.knownNot(KnownNotFromFlags);
5094 if (!Known.SignBit && AssumedClasses.SignBit) {
5095 if (*AssumedClasses.SignBit)
5096 Known.signBitMustBeOne();
5097 else
5098 Known.signBitMustBeZero();
5099 }
5100 });
5101
5102 if (!Op)
5103 return;
5104
5105 // All recursive calls that increase depth must come after this.
5107 return;
5108
5109 const unsigned Opc = Op->getOpcode();
5110 switch (Opc) {
5111 case Instruction::FNeg: {
5112 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5113 Known, Q, Depth + 1);
5114 Known.fneg();
5115 break;
5116 }
5117 case Instruction::Select: {
5118 auto ComputeForArm = [&](Value *Arm, bool Invert) {
5119 KnownFPClass Res;
5120 computeKnownFPClass(Arm, DemandedElts, InterestedClasses, Res, Q,
5121 Depth + 1);
5122 adjustKnownFPClassForSelectArm(Res, Op->getOperand(0), Arm, Invert, Q,
5123 Depth);
5124 return Res;
5125 };
5126 // Only known if known in both the LHS and RHS.
5127 Known =
5128 ComputeForArm(Op->getOperand(1), /*Invert=*/false)
5129 .intersectWith(ComputeForArm(Op->getOperand(2), /*Invert=*/true));
5130 break;
5131 }
5132 case Instruction::Load: {
5133 const MDNode *NoFPClass =
5134 cast<LoadInst>(Op)->getMetadata(LLVMContext::MD_nofpclass);
5135 if (!NoFPClass)
5136 break;
5137
5138 ConstantInt *MaskVal =
5140 Known.knownNot(static_cast<FPClassTest>(MaskVal->getZExtValue()));
5141 break;
5142 }
5143 case Instruction::Call: {
5144 const CallInst *II = cast<CallInst>(Op);
5145 const Intrinsic::ID IID = II->getIntrinsicID();
5146 switch (IID) {
5147 case Intrinsic::fabs: {
5148 if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) {
5149 // If we only care about the sign bit we don't need to inspect the
5150 // operand.
5151 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5152 InterestedClasses, Known, Q, Depth + 1);
5153 }
5154
5155 Known.fabs();
5156 break;
5157 }
5158 case Intrinsic::copysign: {
5159 KnownFPClass KnownSign;
5160
5161 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5162 Known, Q, Depth + 1);
5163 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5164 KnownSign, Q, Depth + 1);
5165 Known.copysign(KnownSign);
5166 break;
5167 }
5168 case Intrinsic::fma:
5169 case Intrinsic::fmuladd: {
5170 if ((InterestedClasses & fcNegative) == fcNone)
5171 break;
5172
5173 // FIXME: This should check isGuaranteedNotToBeUndef
5174 if (II->getArgOperand(0) == II->getArgOperand(1)) {
5175 KnownFPClass KnownSrc, KnownAddend;
5176 computeKnownFPClass(II->getArgOperand(2), DemandedElts,
5177 InterestedClasses, KnownAddend, Q, Depth + 1);
5178 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5179 InterestedClasses, KnownSrc, Q, Depth + 1);
5180
5181 const Function *F = II->getFunction();
5182 const fltSemantics &FltSem =
5183 II->getType()->getScalarType()->getFltSemantics();
5185 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5186
5187 if (KnownNotFromFlags & fcNan) {
5188 KnownSrc.knownNot(fcNan);
5189 KnownAddend.knownNot(fcNan);
5190 }
5191
5192 if (KnownNotFromFlags & fcInf) {
5193 KnownSrc.knownNot(fcInf);
5194 KnownAddend.knownNot(fcInf);
5195 }
5196
5197 Known = KnownFPClass::fma_square(KnownSrc, KnownAddend, Mode);
5198 break;
5199 }
5200
5201 KnownFPClass KnownSrc[3];
5202 for (int I = 0; I != 3; ++I) {
5203 computeKnownFPClass(II->getArgOperand(I), DemandedElts,
5204 InterestedClasses, KnownSrc[I], Q, Depth + 1);
5205 if (KnownSrc[I].isUnknown())
5206 return;
5207
5208 if (KnownNotFromFlags & fcNan)
5209 KnownSrc[I].knownNot(fcNan);
5210 if (KnownNotFromFlags & fcInf)
5211 KnownSrc[I].knownNot(fcInf);
5212 }
5213
5214 const Function *F = II->getFunction();
5215 const fltSemantics &FltSem =
5216 II->getType()->getScalarType()->getFltSemantics();
5218 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5219 Known = KnownFPClass::fma(KnownSrc[0], KnownSrc[1], KnownSrc[2], Mode);
5220 break;
5221 }
5222 case Intrinsic::sqrt:
5223 case Intrinsic::experimental_constrained_sqrt: {
5224 KnownFPClass KnownSrc;
5225 FPClassTest InterestedSrcs = InterestedClasses;
5226 if (InterestedClasses & fcNan)
5227 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5228
5229 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5230 KnownSrc, Q, Depth + 1);
5231
5233
5234 bool HasNSZ = Q.IIQ.hasNoSignedZeros(II);
5235 if (!HasNSZ) {
5236 const Function *F = II->getFunction();
5237 const fltSemantics &FltSem =
5238 II->getType()->getScalarType()->getFltSemantics();
5239 Mode = F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5240 }
5241
5242 Known = KnownFPClass::sqrt(KnownSrc, Mode);
5243 if (HasNSZ)
5244 Known.knownNot(fcNegZero);
5245
5246 break;
5247 }
5248 case Intrinsic::sin: {
5249 KnownFPClass KnownSrc;
5250 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5251 KnownSrc, Q, Depth + 1);
5252 Known = KnownFPClass::sin(KnownSrc);
5253 break;
5254 }
5255 case Intrinsic::cos: {
5256 KnownFPClass KnownSrc;
5257 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5258 KnownSrc, Q, Depth + 1);
5259 Known = KnownFPClass::cos(KnownSrc);
5260 break;
5261 }
5262 case Intrinsic::tan: {
5263 KnownFPClass KnownSrc;
5264 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5265 KnownSrc, Q, Depth + 1);
5266 Known = KnownFPClass::tan(KnownSrc);
5267 break;
5268 }
5269 case Intrinsic::sinh: {
5270 KnownFPClass KnownSrc;
5271 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5272 KnownSrc, Q, Depth + 1);
5273 Known = KnownFPClass::sinh(KnownSrc);
5274 break;
5275 }
5276 case Intrinsic::cosh: {
5277 KnownFPClass KnownSrc;
5278 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5279 KnownSrc, Q, Depth + 1);
5280 Known = KnownFPClass::cosh(KnownSrc);
5281 break;
5282 }
5283 case Intrinsic::tanh: {
5284 KnownFPClass KnownSrc;
5285 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5286 KnownSrc, Q, Depth + 1);
5287 Known = KnownFPClass::tanh(KnownSrc);
5288 break;
5289 }
5290 case Intrinsic::asin: {
5291 KnownFPClass KnownSrc;
5292 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5293 KnownSrc, Q, Depth + 1);
5294 Known = KnownFPClass::asin(KnownSrc);
5295 break;
5296 }
5297 case Intrinsic::acos: {
5298 KnownFPClass KnownSrc;
5299 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5300 KnownSrc, Q, Depth + 1);
5301 Known = KnownFPClass::acos(KnownSrc);
5302 break;
5303 }
5304 case Intrinsic::atan: {
5305 KnownFPClass KnownSrc;
5306 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5307 KnownSrc, Q, Depth + 1);
5308 Known = KnownFPClass::atan(KnownSrc);
5309 break;
5310 }
5311 case Intrinsic::atan2: {
5312 KnownFPClass KnownLHS, KnownRHS;
5313 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5314 KnownLHS, Q, Depth + 1);
5315 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5316 KnownRHS, Q, Depth + 1);
5317 Known = KnownFPClass::atan2(KnownLHS, KnownRHS);
5318 break;
5319 }
5320 case Intrinsic::maxnum:
5321 case Intrinsic::minnum:
5322 case Intrinsic::minimum:
5323 case Intrinsic::maximum:
5324 case Intrinsic::minimumnum:
5325 case Intrinsic::maximumnum: {
5326 KnownFPClass KnownLHS, KnownRHS;
5327 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5328 KnownLHS, Q, Depth + 1);
5329 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5330 KnownRHS, Q, Depth + 1);
5331
5332 const Function *F = II->getFunction();
5333
5335 F ? F->getDenormalMode(
5336 II->getType()->getScalarType()->getFltSemantics())
5338
5339 Known = KnownFPClass::minMaxLike(KnownLHS, KnownRHS, getMinMaxKind(IID),
5340 Mode);
5341 break;
5342 }
5343 case Intrinsic::canonicalize: {
5344 KnownFPClass KnownSrc;
5345 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5346 KnownSrc, Q, Depth + 1);
5347
5348 const Function *F = II->getFunction();
5349 DenormalMode DenormMode =
5350 F ? F->getDenormalMode(
5351 II->getType()->getScalarType()->getFltSemantics())
5353 Known = KnownFPClass::canonicalize(KnownSrc, DenormMode);
5354 break;
5355 }
5356 case Intrinsic::vector_reduce_fmax:
5357 case Intrinsic::vector_reduce_fmin:
5358 case Intrinsic::vector_reduce_fmaximum:
5359 case Intrinsic::vector_reduce_fminimum: {
5360 // reduce min/max will choose an element from one of the vector elements,
5361 // so we can infer and class information that is common to all elements.
5362 Known = computeKnownFPClass(II->getArgOperand(0), II->getFastMathFlags(),
5363 InterestedClasses, Q, Depth + 1);
5364 // Can only propagate sign if output is never NaN.
5365 if (!Known.isKnownNeverNaN())
5366 Known.SignBit.reset();
5367 break;
5368 }
5369 // reverse preserves all characteristics of the input vec's element.
5370 case Intrinsic::vector_reverse:
5371 Known = computeKnownFPClass(
5372 II->getArgOperand(0), DemandedElts.reverseBits(),
5373 II->getFastMathFlags(), InterestedClasses, Q, Depth + 1);
5374 break;
5375 case Intrinsic::trunc:
5376 case Intrinsic::floor:
5377 case Intrinsic::ceil:
5378 case Intrinsic::rint:
5379 case Intrinsic::nearbyint:
5380 case Intrinsic::round:
5381 case Intrinsic::roundeven: {
5382 KnownFPClass KnownSrc;
5383 FPClassTest InterestedSrcs = InterestedClasses;
5384 if (InterestedSrcs & fcPosFinite)
5385 InterestedSrcs |= fcPosFinite;
5386 if (InterestedSrcs & fcNegFinite)
5387 InterestedSrcs |= fcNegFinite;
5388 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5389 KnownSrc, Q, Depth + 1);
5390
5392 KnownSrc, IID == Intrinsic::trunc,
5393 V->getType()->getScalarType()->isMultiUnitFPType());
5394 break;
5395 }
5396 case Intrinsic::exp:
5397 case Intrinsic::exp2:
5398 case Intrinsic::exp10:
5399 case Intrinsic::amdgcn_exp2: {
5400 KnownFPClass KnownSrc;
5401 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5402 KnownSrc, Q, Depth + 1);
5403
5404 Known = KnownFPClass::exp(KnownSrc);
5405
5406 Type *EltTy = II->getType()->getScalarType();
5407 if (IID == Intrinsic::amdgcn_exp2 && EltTy->isFloatTy())
5408 Known.knownNot(fcSubnormal);
5409
5410 break;
5411 }
5412 case Intrinsic::fptrunc_round: {
5413 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,
5414 Q, Depth);
5415 break;
5416 }
5417 case Intrinsic::log:
5418 case Intrinsic::log10:
5419 case Intrinsic::log2:
5420 case Intrinsic::experimental_constrained_log:
5421 case Intrinsic::experimental_constrained_log10:
5422 case Intrinsic::experimental_constrained_log2:
5423 case Intrinsic::amdgcn_log: {
5424 Type *EltTy = II->getType()->getScalarType();
5425
5426 // log(+inf) -> +inf
5427 // log([+-]0.0) -> -inf
5428 // log(-inf) -> nan
5429 // log(-x) -> nan
5430 if ((InterestedClasses & (fcNan | fcInf)) != fcNone) {
5431 FPClassTest InterestedSrcs = InterestedClasses;
5432 if ((InterestedClasses & fcNegInf) != fcNone)
5433 InterestedSrcs |= fcZero | fcSubnormal;
5434 if ((InterestedClasses & fcNan) != fcNone)
5435 InterestedSrcs |= fcNan | fcNegative;
5436
5437 KnownFPClass KnownSrc;
5438 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5439 KnownSrc, Q, Depth + 1);
5440
5441 const Function *F = II->getFunction();
5442 DenormalMode Mode = F ? F->getDenormalMode(EltTy->getFltSemantics())
5444 Known = KnownFPClass::log(KnownSrc, Mode);
5445 }
5446
5447 break;
5448 }
5449 case Intrinsic::powi: {
5450 if ((InterestedClasses & (fcNan | fcInf | fcNegative)) == fcNone)
5451 break;
5452
5453 const Value *Exp = II->getArgOperand(1);
5454 Type *ExpTy = Exp->getType();
5455 unsigned BitWidth = ExpTy->getScalarType()->getIntegerBitWidth();
5456 KnownBits ExponentKnownBits(BitWidth);
5457 computeKnownBits(Exp, isa<VectorType>(ExpTy) ? DemandedElts : APInt(1, 1),
5458 ExponentKnownBits, Q, Depth + 1);
5459
5460 FPClassTest InterestedSrcs = fcNone;
5461 if (InterestedClasses & fcNan)
5462 InterestedSrcs |= fcNan;
5463 if (!ExponentKnownBits.isZero()) {
5464 if (InterestedClasses & fcInf)
5465 InterestedSrcs |= fcFinite | fcInf;
5466 if ((InterestedClasses & fcNegative) && !ExponentKnownBits.isEven())
5467 InterestedSrcs |= fcNegative;
5468 }
5469
5470 KnownFPClass KnownSrc;
5471 if (InterestedSrcs != fcNone)
5472 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5473 KnownSrc, Q, Depth + 1);
5474
5475 Known = KnownFPClass::powi(KnownSrc, ExponentKnownBits);
5476 break;
5477 }
5478 case Intrinsic::ldexp: {
5479 KnownFPClass KnownSrc;
5480 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5481 KnownSrc, Q, Depth + 1);
5482 // Can refine inf/zero handling based on the exponent operand.
5483 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;
5484
5485 KnownBits ExpBits;
5486 if ((KnownSrc.KnownFPClasses & ExpInfoMask) != fcNone) {
5487 const Value *ExpArg = II->getArgOperand(1);
5488 ExpBits = computeKnownBits(ExpArg, DemandedElts, Q, Depth + 1);
5489 }
5490
5491 const fltSemantics &Flt =
5492 II->getType()->getScalarType()->getFltSemantics();
5493
5494 const Function *F = II->getFunction();
5496 F ? F->getDenormalMode(Flt) : DenormalMode::getDynamic();
5497
5498 Known = KnownFPClass::ldexp(KnownSrc, ExpBits, Flt, Mode);
5499 break;
5500 }
5501 case Intrinsic::arithmetic_fence: {
5502 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5503 Known, Q, Depth + 1);
5504 break;
5505 }
5506 case Intrinsic::experimental_constrained_sitofp:
5507 case Intrinsic::experimental_constrained_uitofp:
5508 // Cannot produce nan
5509 Known.knownNot(fcNan);
5510
5511 // sitofp and uitofp turn into +0.0 for zero.
5512 Known.knownNot(fcNegZero);
5513
5514 // Integers cannot be subnormal
5515 Known.knownNot(fcSubnormal);
5516
5517 if (IID == Intrinsic::experimental_constrained_uitofp)
5518 Known.signBitMustBeZero();
5519
5520 // TODO: Copy inf handling from instructions
5521 break;
5522
5523 case Intrinsic::amdgcn_fract: {
5524 Known.knownNot(fcInf);
5525
5526 if (InterestedClasses & fcNan) {
5527 KnownFPClass KnownSrc;
5528 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5529 InterestedClasses, KnownSrc, Q, Depth + 1);
5530
5531 if (KnownSrc.isKnownNeverInfOrNaN())
5532 Known.knownNot(fcNan);
5533 else if (KnownSrc.isKnownNever(fcSNan))
5534 Known.knownNot(fcSNan);
5535 }
5536
5537 break;
5538 }
5539 case Intrinsic::amdgcn_rcp: {
5540 KnownFPClass KnownSrc;
5541 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5542 KnownSrc, Q, Depth + 1);
5543
5544 Known.propagateNaN(KnownSrc);
5545
5546 Type *EltTy = II->getType()->getScalarType();
5547
5548 // f32 denormal always flushed.
5549 if (EltTy->isFloatTy()) {
5550 Known.knownNot(fcSubnormal);
5551 KnownSrc.knownNot(fcSubnormal);
5552 }
5553
5554 if (KnownSrc.isKnownNever(fcNegative))
5555 Known.knownNot(fcNegative);
5556 if (KnownSrc.isKnownNever(fcPositive))
5557 Known.knownNot(fcPositive);
5558
5559 if (const Function *F = II->getFunction()) {
5560 DenormalMode Mode = F->getDenormalMode(EltTy->getFltSemantics());
5561 if (KnownSrc.isKnownNeverLogicalPosZero(Mode))
5562 Known.knownNot(fcPosInf);
5563 if (KnownSrc.isKnownNeverLogicalNegZero(Mode))
5564 Known.knownNot(fcNegInf);
5565 }
5566
5567 break;
5568 }
5569 case Intrinsic::amdgcn_rsq: {
5570 KnownFPClass KnownSrc;
5571 // The only negative value that can be returned is -inf for -0 inputs.
5573
5574 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5575 KnownSrc, Q, Depth + 1);
5576
5577 // Negative -> nan
5578 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
5579 Known.knownNot(fcNan);
5580 else if (KnownSrc.isKnownNever(fcSNan))
5581 Known.knownNot(fcSNan);
5582
5583 // +inf -> +0
5584 if (KnownSrc.isKnownNeverPosInfinity())
5585 Known.knownNot(fcPosZero);
5586
5587 Type *EltTy = II->getType()->getScalarType();
5588
5589 // f32 denormal always flushed.
5590 if (EltTy->isFloatTy())
5591 Known.knownNot(fcPosSubnormal);
5592
5593 if (const Function *F = II->getFunction()) {
5594 DenormalMode Mode = F->getDenormalMode(EltTy->getFltSemantics());
5595
5596 // -0 -> -inf
5597 if (KnownSrc.isKnownNeverLogicalNegZero(Mode))
5598 Known.knownNot(fcNegInf);
5599
5600 // +0 -> +inf
5601 if (KnownSrc.isKnownNeverLogicalPosZero(Mode))
5602 Known.knownNot(fcPosInf);
5603 }
5604
5605 break;
5606 }
5607 case Intrinsic::amdgcn_trig_preop: {
5608 // Always returns a value [0, 1)
5609 Known.knownNot(fcNan | fcInf | fcNegative);
5610 break;
5611 }
5612 default:
5613 break;
5614 }
5615
5616 break;
5617 }
5618 case Instruction::FAdd:
5619 case Instruction::FSub: {
5620 KnownFPClass KnownLHS, KnownRHS;
5621 bool WantNegative =
5622 Op->getOpcode() == Instruction::FAdd &&
5623 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;
5624 bool WantNaN = (InterestedClasses & fcNan) != fcNone;
5625 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;
5626
5627 if (!WantNaN && !WantNegative && !WantNegZero)
5628 break;
5629
5630 FPClassTest InterestedSrcs = InterestedClasses;
5631 if (WantNegative)
5632 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5633 if (InterestedClasses & fcNan)
5634 InterestedSrcs |= fcInf;
5635 computeKnownFPClass(Op->getOperand(1), DemandedElts, InterestedSrcs,
5636 KnownRHS, Q, Depth + 1);
5637
5638 // Special case fadd x, x, which is the canonical form of fmul x, 2.
5639 bool Self = Op->getOperand(0) == Op->getOperand(1) &&
5640 isGuaranteedNotToBeUndef(Op->getOperand(0), Q.AC, Q.CxtI, Q.DT,
5641 Depth + 1);
5642 if (Self)
5643 KnownLHS = KnownRHS;
5644
5645 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||
5646 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) ||
5647 WantNegZero || Opc == Instruction::FSub) {
5648
5649 // FIXME: Context function should always be passed in separately
5650 const Function *F = cast<Instruction>(Op)->getFunction();
5651 const fltSemantics &FltSem =
5652 Op->getType()->getScalarType()->getFltSemantics();
5654 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5655
5656 if (Self && Opc == Instruction::FAdd) {
5657 Known = KnownFPClass::fadd_self(KnownLHS, Mode);
5658 } else {
5659 // RHS is canonically cheaper to compute. Skip inspecting the LHS if
5660 // there's no point.
5661
5662 if (!Self) {
5663 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedSrcs,
5664 KnownLHS, Q, Depth + 1);
5665 }
5666
5667 Known = Opc == Instruction::FAdd
5668 ? KnownFPClass::fadd(KnownLHS, KnownRHS, Mode)
5669 : KnownFPClass::fsub(KnownLHS, KnownRHS, Mode);
5670 }
5671 }
5672
5673 break;
5674 }
5675 case Instruction::FMul: {
5676 const Function *F = cast<Instruction>(Op)->getFunction();
5678 F ? F->getDenormalMode(
5679 Op->getType()->getScalarType()->getFltSemantics())
5681
5682 Value *LHS = Op->getOperand(0);
5683 Value *RHS = Op->getOperand(1);
5684 // X * X is always non-negative or a NaN.
5685 // FIXME: Should check isGuaranteedNotToBeUndef
5686 if (LHS == RHS) {
5687 KnownFPClass KnownSrc;
5688 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownSrc, Q,
5689 Depth + 1);
5690 Known = KnownFPClass::square(KnownSrc, Mode);
5691 break;
5692 }
5693
5694 KnownFPClass KnownLHS, KnownRHS;
5695
5696 const APFloat *CRHS;
5697 if (match(RHS, m_APFloat(CRHS))) {
5698 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Q,
5699 Depth + 1);
5700 Known = KnownFPClass::fmul(KnownLHS, *CRHS, Mode);
5701 } else {
5702 computeKnownFPClass(RHS, DemandedElts, fcAllFlags, KnownRHS, Q,
5703 Depth + 1);
5704 // TODO: Improve accuracy in unfused FMA pattern. We can prove an
5705 // additional not-nan if the addend is known-not negative infinity if the
5706 // multiply is known-not infinity.
5707
5708 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Q,
5709 Depth + 1);
5710 Known = KnownFPClass::fmul(KnownLHS, KnownRHS, Mode);
5711 }
5712
5713 /// Propgate no-infs if the other source is known smaller than one, such
5714 /// that this cannot introduce overflow.
5715 if (KnownLHS.isKnownNever(fcInf) && isAbsoluteValueULEOne(RHS))
5716 Known.knownNot(fcInf);
5717 else if (KnownRHS.isKnownNever(fcInf) && isAbsoluteValueULEOne(LHS))
5718 Known.knownNot(fcInf);
5719
5720 break;
5721 }
5722 case Instruction::FDiv:
5723 case Instruction::FRem: {
5724 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
5725
5726 if (Op->getOpcode() == Instruction::FRem)
5727 Known.knownNot(fcInf);
5728
5729 if (Op->getOperand(0) == Op->getOperand(1) &&
5730 isGuaranteedNotToBeUndef(Op->getOperand(0), Q.AC, Q.CxtI, Q.DT)) {
5731 if (Op->getOpcode() == Instruction::FDiv) {
5732 // X / X is always exactly 1.0 or a NaN.
5734 } else {
5735 // X % X is always exactly [+-]0.0 or a NaN.
5736 Known.KnownFPClasses = fcNan | fcZero;
5737 }
5738
5739 if (!WantNan)
5740 break;
5741
5742 KnownFPClass KnownSrc;
5743 computeKnownFPClass(Op->getOperand(0), DemandedElts,
5744 fcNan | fcInf | fcZero | fcSubnormal, KnownSrc, Q,
5745 Depth + 1);
5746 const Function *F = cast<Instruction>(Op)->getFunction();
5747 const fltSemantics &FltSem =
5748 Op->getType()->getScalarType()->getFltSemantics();
5749
5751 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5752
5753 Known = Op->getOpcode() == Instruction::FDiv
5754 ? KnownFPClass::fdiv_self(KnownSrc, Mode)
5755 : KnownFPClass::frem_self(KnownSrc, Mode);
5756 break;
5757 }
5758
5759 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
5760 const bool WantPositive =
5761 Opc == Instruction::FRem && (InterestedClasses & fcPositive) != fcNone;
5762 if (!WantNan && !WantNegative && !WantPositive)
5763 break;
5764
5765 KnownFPClass KnownLHS, KnownRHS;
5766
5767 computeKnownFPClass(Op->getOperand(1), DemandedElts,
5768 fcNan | fcInf | fcZero | fcNegative, KnownRHS, Q,
5769 Depth + 1);
5770
5771 bool KnowSomethingUseful = KnownRHS.isKnownNeverNaN() ||
5772 KnownRHS.isKnownNever(fcNegative) ||
5773 KnownRHS.isKnownNever(fcPositive);
5774
5775 if (KnowSomethingUseful || WantPositive) {
5776 computeKnownFPClass(Op->getOperand(0), DemandedElts, fcAllFlags, KnownLHS,
5777 Q, Depth + 1);
5778 }
5779
5780 const Function *F = cast<Instruction>(Op)->getFunction();
5781 const fltSemantics &FltSem =
5782 Op->getType()->getScalarType()->getFltSemantics();
5783
5784 if (Op->getOpcode() == Instruction::FDiv) {
5786 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5787 Known = KnownFPClass::fdiv(KnownLHS, KnownRHS, Mode);
5788 } else {
5789 // Inf REM x and x REM 0 produce NaN.
5790 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5791 KnownLHS.isKnownNeverInfinity() && F &&
5792 KnownRHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))) {
5793 Known.knownNot(fcNan);
5794 }
5795
5796 // The sign for frem is the same as the first operand.
5797 if (KnownLHS.cannotBeOrderedLessThanZero())
5799 if (KnownLHS.cannotBeOrderedGreaterThanZero())
5801
5802 // See if we can be more aggressive about the sign of 0.
5803 if (KnownLHS.isKnownNever(fcNegative))
5804 Known.knownNot(fcNegative);
5805 if (KnownLHS.isKnownNever(fcPositive))
5806 Known.knownNot(fcPositive);
5807 }
5808
5809 break;
5810 }
5811 case Instruction::FPExt: {
5812 KnownFPClass KnownSrc;
5813 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5814 KnownSrc, Q, Depth + 1);
5815
5816 const fltSemantics &DstTy =
5817 Op->getType()->getScalarType()->getFltSemantics();
5818 const fltSemantics &SrcTy =
5819 Op->getOperand(0)->getType()->getScalarType()->getFltSemantics();
5820
5821 Known = KnownFPClass::fpext(KnownSrc, DstTy, SrcTy);
5822 break;
5823 }
5824 case Instruction::FPTrunc: {
5825 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known, Q,
5826 Depth);
5827 break;
5828 }
5829 case Instruction::SIToFP:
5830 case Instruction::UIToFP: {
5831 // Cannot produce nan
5832 Known.knownNot(fcNan);
5833
5834 // Integers cannot be subnormal
5835 Known.knownNot(fcSubnormal);
5836
5837 // sitofp and uitofp turn into +0.0 for zero.
5838 Known.knownNot(fcNegZero);
5839
5840 // UIToFP is always non-negative regardless of known bits.
5841 if (Op->getOpcode() == Instruction::UIToFP)
5842 Known.signBitMustBeZero();
5843
5844 // Only compute known bits if we can learn something useful from them.
5845 if (!(InterestedClasses & (fcPosZero | fcNormal | fcInf)))
5846 break;
5847
5848 KnownBits IntKnown =
5849 computeKnownBits(Op->getOperand(0), DemandedElts, Q, Depth + 1);
5850
5851 // If the integer is non-zero, the result cannot be +0.0
5852 if (IntKnown.isNonZero())
5853 Known.knownNot(fcPosZero);
5854
5855 if (Op->getOpcode() == Instruction::SIToFP) {
5856 // If the signed integer is known non-negative, the result is
5857 // non-negative. If the signed integer is known negative, the result is
5858 // negative.
5859 if (IntKnown.isNonNegative()) {
5860 Known.signBitMustBeZero();
5861 } else if (IntKnown.isNegative()) {
5862 Known.signBitMustBeOne();
5863 }
5864 }
5865
5866 // Guard kept for ilogb()
5867 if (InterestedClasses & fcInf) {
5868 // Get width of largest magnitude integer known.
5869 // This still works for a signed minimum value because the largest FP
5870 // value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx).
5871 int IntSize = IntKnown.getBitWidth();
5872 if (Op->getOpcode() == Instruction::UIToFP)
5873 IntSize -= IntKnown.countMinLeadingZeros();
5874 else if (Op->getOpcode() == Instruction::SIToFP)
5875 IntSize -= IntKnown.countMinSignBits();
5876
5877 // If the exponent of the largest finite FP value can hold the largest
5878 // integer, the result of the cast must be finite.
5879 Type *FPTy = Op->getType()->getScalarType();
5880 if (ilogb(APFloat::getLargest(FPTy->getFltSemantics())) >= IntSize)
5881 Known.knownNot(fcInf);
5882 }
5883
5884 break;
5885 }
5886 case Instruction::ExtractElement: {
5887 // Look through extract element. If the index is non-constant or
5888 // out-of-range demand all elements, otherwise just the extracted element.
5889 const Value *Vec = Op->getOperand(0);
5890
5891 APInt DemandedVecElts;
5892 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
5893 unsigned NumElts = VecTy->getNumElements();
5894 DemandedVecElts = APInt::getAllOnes(NumElts);
5895 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(1));
5896 if (CIdx && CIdx->getValue().ult(NumElts))
5897 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
5898 } else {
5899 DemandedVecElts = APInt(1, 1);
5900 }
5901
5902 return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,
5903 Q, Depth + 1);
5904 }
5905 case Instruction::InsertElement: {
5906 if (isa<ScalableVectorType>(Op->getType()))
5907 return;
5908
5909 const Value *Vec = Op->getOperand(0);
5910 const Value *Elt = Op->getOperand(1);
5911 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(2));
5912 unsigned NumElts = DemandedElts.getBitWidth();
5913 APInt DemandedVecElts = DemandedElts;
5914 bool NeedsElt = true;
5915 // If we know the index we are inserting to, clear it from Vec check.
5916 if (CIdx && CIdx->getValue().ult(NumElts)) {
5917 DemandedVecElts.clearBit(CIdx->getZExtValue());
5918 NeedsElt = DemandedElts[CIdx->getZExtValue()];
5919 }
5920
5921 // Do we demand the inserted element?
5922 if (NeedsElt) {
5923 computeKnownFPClass(Elt, Known, InterestedClasses, Q, Depth + 1);
5924 // If we don't know any bits, early out.
5925 if (Known.isUnknown())
5926 break;
5927 } else {
5928 Known.KnownFPClasses = fcNone;
5929 }
5930
5931 // Do we need anymore elements from Vec?
5932 if (!DemandedVecElts.isZero()) {
5933 KnownFPClass Known2;
5934 computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2, Q,
5935 Depth + 1);
5936 Known |= Known2;
5937 }
5938
5939 break;
5940 }
5941 case Instruction::ShuffleVector: {
5942 // Handle vector splat idiom
5943 if (Value *Splat = getSplatValue(V)) {
5944 computeKnownFPClass(Splat, Known, InterestedClasses, Q, Depth + 1);
5945 break;
5946 }
5947
5948 // For undef elements, we don't know anything about the common state of
5949 // the shuffle result.
5950 APInt DemandedLHS, DemandedRHS;
5951 auto *Shuf = dyn_cast<ShuffleVectorInst>(Op);
5952 if (!Shuf || !getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
5953 return;
5954
5955 if (!!DemandedLHS) {
5956 const Value *LHS = Shuf->getOperand(0);
5957 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known, Q,
5958 Depth + 1);
5959
5960 // If we don't know any bits, early out.
5961 if (Known.isUnknown())
5962 break;
5963 } else {
5964 Known.KnownFPClasses = fcNone;
5965 }
5966
5967 if (!!DemandedRHS) {
5968 KnownFPClass Known2;
5969 const Value *RHS = Shuf->getOperand(1);
5970 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2, Q,
5971 Depth + 1);
5972 Known |= Known2;
5973 }
5974
5975 break;
5976 }
5977 case Instruction::ExtractValue: {
5978 const ExtractValueInst *Extract = cast<ExtractValueInst>(Op);
5979 ArrayRef<unsigned> Indices = Extract->getIndices();
5980 const Value *Src = Extract->getAggregateOperand();
5981 if (isa<StructType>(Src->getType()) && Indices.size() == 1 &&
5982 Indices[0] == 0) {
5983 if (const auto *II = dyn_cast<IntrinsicInst>(Src)) {
5984 switch (II->getIntrinsicID()) {
5985 case Intrinsic::frexp: {
5986 Known.knownNot(fcSubnormal);
5987
5988 KnownFPClass KnownSrc;
5989 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5990 InterestedClasses, KnownSrc, Q, Depth + 1);
5991
5992 const Function *F = cast<Instruction>(Op)->getFunction();
5993 const fltSemantics &FltSem =
5994 Op->getType()->getScalarType()->getFltSemantics();
5995
5997 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5998 Known = KnownFPClass::frexp_mant(KnownSrc, Mode);
5999 return;
6000 }
6001 default:
6002 break;
6003 }
6004 }
6005 }
6006
6007 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Q,
6008 Depth + 1);
6009 break;
6010 }
6011 case Instruction::PHI: {
6012 const PHINode *P = cast<PHINode>(Op);
6013 // Unreachable blocks may have zero-operand PHI nodes.
6014 if (P->getNumIncomingValues() == 0)
6015 break;
6016
6017 // Otherwise take the unions of the known bit sets of the operands,
6018 // taking conservative care to avoid excessive recursion.
6019 const unsigned PhiRecursionLimit = MaxAnalysisRecursionDepth - 2;
6020
6021 if (Depth < PhiRecursionLimit) {
6022 // Skip if every incoming value references to ourself.
6023 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
6024 break;
6025
6026 bool First = true;
6027
6028 for (const Use &U : P->operands()) {
6029 Value *IncValue;
6030 Instruction *CxtI;
6031 breakSelfRecursivePHI(&U, P, IncValue, CxtI);
6032 // Skip direct self references.
6033 if (IncValue == P)
6034 continue;
6035
6036 KnownFPClass KnownSrc;
6037 // Recurse, but cap the recursion to two levels, because we don't want
6038 // to waste time spinning around in loops. We need at least depth 2 to
6039 // detect known sign bits.
6040 computeKnownFPClass(IncValue, DemandedElts, InterestedClasses, KnownSrc,
6042 PhiRecursionLimit);
6043
6044 if (First) {
6045 Known = KnownSrc;
6046 First = false;
6047 } else {
6048 Known |= KnownSrc;
6049 }
6050
6051 if (Known.KnownFPClasses == fcAllFlags)
6052 break;
6053 }
6054 }
6055
6056 // Look for the case of a for loop which has a positive
6057 // initial value and is incremented by a squared value.
6058 // This will propagate sign information out of such loops.
6059 if (P->getNumIncomingValues() != 2 || Known.cannotBeOrderedLessThanZero())
6060 break;
6061 for (unsigned I = 0; I < 2; I++) {
6062 Value *RecurValue = P->getIncomingValue(1 - I);
6064 if (!II)
6065 continue;
6066 Value *R, *L, *Init;
6067 PHINode *PN;
6069 PN == P) {
6070 switch (II->getIntrinsicID()) {
6071 case Intrinsic::fma:
6072 case Intrinsic::fmuladd: {
6073 KnownFPClass KnownStart;
6074 computeKnownFPClass(Init, DemandedElts, InterestedClasses, KnownStart,
6075 Q, Depth + 1);
6076 if (KnownStart.cannotBeOrderedLessThanZero() && L == R &&
6077 isGuaranteedNotToBeUndef(L, Q.AC, Q.CxtI, Q.DT, Depth + 1))
6079 break;
6080 }
6081 }
6082 }
6083 }
6084 break;
6085 }
6086 case Instruction::BitCast: {
6087 const Value *Src;
6088 if (!match(Op, m_ElementWiseBitCast(m_Value(Src))) ||
6089 !Src->getType()->isIntOrIntVectorTy())
6090 break;
6091
6092 const Type *Ty = Op->getType();
6093
6094 Value *CastLHS, *CastRHS;
6095
6096 // Match bitcast(umax(bitcast(a), bitcast(b)))
6097 if (match(Src, m_c_MaxOrMin(m_BitCast(m_Value(CastLHS)),
6098 m_BitCast(m_Value(CastRHS)))) &&
6099 CastLHS->getType() == Ty && CastRHS->getType() == Ty) {
6100 KnownFPClass KnownLHS, KnownRHS;
6101 computeKnownFPClass(CastRHS, DemandedElts, InterestedClasses, KnownRHS, Q,
6102 Depth + 1);
6103 if (!KnownRHS.isUnknown()) {
6104 computeKnownFPClass(CastLHS, DemandedElts, InterestedClasses, KnownLHS,
6105 Q, Depth + 1);
6106 Known = KnownLHS | KnownRHS;
6107 }
6108
6109 return;
6110 }
6111
6112 const Type *EltTy = Ty->getScalarType();
6113 KnownBits Bits(EltTy->getPrimitiveSizeInBits());
6114 computeKnownBits(Src, DemandedElts, Bits, Q, Depth + 1);
6115
6116 Known = KnownFPClass::bitcast(EltTy->getFltSemantics(), Bits);
6117 break;
6118 }
6119 default:
6120 break;
6121 }
6122}
6123
6125 const APInt &DemandedElts,
6126 FPClassTest InterestedClasses,
6127 const SimplifyQuery &SQ,
6128 unsigned Depth) {
6129 KnownFPClass KnownClasses;
6130 ::computeKnownFPClass(V, DemandedElts, InterestedClasses, KnownClasses, SQ,
6131 Depth);
6132 return KnownClasses;
6133}
6134
6136 FPClassTest InterestedClasses,
6137 const SimplifyQuery &SQ,
6138 unsigned Depth) {
6139 KnownFPClass Known;
6140 ::computeKnownFPClass(V, Known, InterestedClasses, SQ, Depth);
6141 return Known;
6142}
6143
6145 const Value *V, const DataLayout &DL, FPClassTest InterestedClasses,
6146 const TargetLibraryInfo *TLI, AssumptionCache *AC, const Instruction *CxtI,
6147 const DominatorTree *DT, bool UseInstrInfo, unsigned Depth) {
6148 return computeKnownFPClass(V, InterestedClasses,
6149 SimplifyQuery(DL, TLI, DT, AC, CxtI, UseInstrInfo),
6150 Depth);
6151}
6152
6154llvm::computeKnownFPClass(const Value *V, const APInt &DemandedElts,
6155 FastMathFlags FMF, FPClassTest InterestedClasses,
6156 const SimplifyQuery &SQ, unsigned Depth) {
6157 if (FMF.noNaNs())
6158 InterestedClasses &= ~fcNan;
6159 if (FMF.noInfs())
6160 InterestedClasses &= ~fcInf;
6161
6162 KnownFPClass Result =
6163 computeKnownFPClass(V, DemandedElts, InterestedClasses, SQ, Depth);
6164
6165 if (FMF.noNaNs())
6166 Result.KnownFPClasses &= ~fcNan;
6167 if (FMF.noInfs())
6168 Result.KnownFPClasses &= ~fcInf;
6169 return Result;
6170}
6171
6173 FPClassTest InterestedClasses,
6174 const SimplifyQuery &SQ,
6175 unsigned Depth) {
6176 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
6177 APInt DemandedElts =
6178 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
6179 return computeKnownFPClass(V, DemandedElts, FMF, InterestedClasses, SQ,
6180 Depth);
6181}
6182
6184 unsigned Depth) {
6186 return Known.isKnownNeverNegZero();
6187}
6188
6195
6197 unsigned Depth) {
6199 return Known.isKnownNeverInfinity();
6200}
6201
6202/// Return true if the floating-point value can never contain a NaN or infinity.
6204 unsigned Depth) {
6206 return Known.isKnownNeverNaN() && Known.isKnownNeverInfinity();
6207}
6208
6209/// Return true if the floating-point scalar value is not a NaN or if the
6210/// floating-point vector value has no NaN elements. Return false if a value
6211/// could ever be NaN.
6213 unsigned Depth) {
6215 return Known.isKnownNeverNaN();
6216}
6217
6218/// Return false if we can prove that the specified FP value's sign bit is 0.
6219/// Return true if we can prove that the specified FP value's sign bit is 1.
6220/// Otherwise return std::nullopt.
6221std::optional<bool> llvm::computeKnownFPSignBit(const Value *V,
6222 const SimplifyQuery &SQ,
6223 unsigned Depth) {
6225 return Known.SignBit;
6226}
6227
6229 auto *User = cast<Instruction>(U.getUser());
6230 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6231 if (FPOp->hasNoSignedZeros())
6232 return true;
6233 }
6234
6235 switch (User->getOpcode()) {
6236 case Instruction::FPToSI:
6237 case Instruction::FPToUI:
6238 return true;
6239 case Instruction::FCmp:
6240 // fcmp treats both positive and negative zero as equal.
6241 return true;
6242 case Instruction::Call:
6243 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6244 switch (II->getIntrinsicID()) {
6245 case Intrinsic::fabs:
6246 return true;
6247 case Intrinsic::copysign:
6248 return U.getOperandNo() == 0;
6249 case Intrinsic::is_fpclass:
6250 case Intrinsic::vp_is_fpclass: {
6251 auto Test =
6252 static_cast<FPClassTest>(
6253 cast<ConstantInt>(II->getArgOperand(1))->getZExtValue()) &
6256 }
6257 default:
6258 return false;
6259 }
6260 }
6261 return false;
6262 default:
6263 return false;
6264 }
6265}
6266
6268 auto *User = cast<Instruction>(U.getUser());
6269 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6270 if (FPOp->hasNoNaNs())
6271 return true;
6272 }
6273
6274 switch (User->getOpcode()) {
6275 case Instruction::FPToSI:
6276 case Instruction::FPToUI:
6277 return true;
6278 // Proper FP math operations ignore the sign bit of NaN.
6279 case Instruction::FAdd:
6280 case Instruction::FSub:
6281 case Instruction::FMul:
6282 case Instruction::FDiv:
6283 case Instruction::FRem:
6284 case Instruction::FPTrunc:
6285 case Instruction::FPExt:
6286 case Instruction::FCmp:
6287 return true;
6288 // Bitwise FP operations should preserve the sign bit of NaN.
6289 case Instruction::FNeg:
6290 case Instruction::Select:
6291 case Instruction::PHI:
6292 return false;
6293 case Instruction::Ret:
6294 return User->getFunction()->getAttributes().getRetNoFPClass() &
6296 case Instruction::Call:
6297 case Instruction::Invoke: {
6298 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6299 switch (II->getIntrinsicID()) {
6300 case Intrinsic::fabs:
6301 return true;
6302 case Intrinsic::copysign:
6303 return U.getOperandNo() == 0;
6304 // Other proper FP math intrinsics ignore the sign bit of NaN.
6305 case Intrinsic::maxnum:
6306 case Intrinsic::minnum:
6307 case Intrinsic::maximum:
6308 case Intrinsic::minimum:
6309 case Intrinsic::maximumnum:
6310 case Intrinsic::minimumnum:
6311 case Intrinsic::canonicalize:
6312 case Intrinsic::fma:
6313 case Intrinsic::fmuladd:
6314 case Intrinsic::sqrt:
6315 case Intrinsic::pow:
6316 case Intrinsic::powi:
6317 case Intrinsic::fptoui_sat:
6318 case Intrinsic::fptosi_sat:
6319 case Intrinsic::is_fpclass:
6320 case Intrinsic::vp_is_fpclass:
6321 return true;
6322 default:
6323 return false;
6324 }
6325 }
6326
6327 FPClassTest NoFPClass =
6328 cast<CallBase>(User)->getParamNoFPClass(U.getOperandNo());
6329 return NoFPClass & FPClassTest::fcNan;
6330 }
6331 default:
6332 return false;
6333 }
6334}
6335
6337 FastMathFlags FMF) {
6338 if (isa<PoisonValue>(V))
6339 return true;
6340 if (isa<UndefValue>(V))
6341 return false;
6342
6343 if (match(V, m_CheckedFp([](const APFloat &Val) { return Val.isInteger(); })))
6344 return true;
6345
6347 if (!I)
6348 return false;
6349
6350 switch (I->getOpcode()) {
6351 case Instruction::SIToFP:
6352 case Instruction::UIToFP:
6353 // TODO: Could check nofpclass(inf) on incoming argument
6354 if (FMF.noInfs())
6355 return true;
6356
6357 // Need to check int size cannot produce infinity, which computeKnownFPClass
6358 // knows how to do already.
6359 return isKnownNeverInfinity(I, SQ);
6360 case Instruction::Call: {
6361 const CallInst *CI = cast<CallInst>(I);
6362 switch (CI->getIntrinsicID()) {
6363 case Intrinsic::trunc:
6364 case Intrinsic::floor:
6365 case Intrinsic::ceil:
6366 case Intrinsic::rint:
6367 case Intrinsic::nearbyint:
6368 case Intrinsic::round:
6369 case Intrinsic::roundeven:
6370 return (FMF.noInfs() && FMF.noNaNs()) || isKnownNeverInfOrNaN(I, SQ);
6371 default:
6372 break;
6373 }
6374
6375 break;
6376 }
6377 default:
6378 break;
6379 }
6380
6381 return false;
6382}
6383
6385
6386 // All byte-wide stores are splatable, even of arbitrary variables.
6387 if (V->getType()->isIntegerTy(8))
6388 return V;
6389
6390 LLVMContext &Ctx = V->getContext();
6391
6392 // Undef don't care.
6393 auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
6394 if (isa<UndefValue>(V))
6395 return UndefInt8;
6396
6397 // Return poison for zero-sized type.
6398 if (DL.getTypeStoreSize(V->getType()).isZero())
6399 return PoisonValue::get(Type::getInt8Ty(Ctx));
6400
6402 if (!C) {
6403 // Conceptually, we could handle things like:
6404 // %a = zext i8 %X to i16
6405 // %b = shl i16 %a, 8
6406 // %c = or i16 %a, %b
6407 // but until there is an example that actually needs this, it doesn't seem
6408 // worth worrying about.
6409 return nullptr;
6410 }
6411
6412 // Handle 'null' ConstantArrayZero etc.
6413 if (C->isNullValue())
6415
6416 // Constant floating-point values can be handled as integer values if the
6417 // corresponding integer value is "byteable". An important case is 0.0.
6418 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
6419 Type *ScalarTy = CFP->getType()->getScalarType();
6420 if (ScalarTy->isHalfTy() || ScalarTy->isFloatTy() || ScalarTy->isDoubleTy())
6421 return isBytewiseValue(
6422 ConstantInt::get(Ctx, CFP->getValue().bitcastToAPInt()), DL);
6423
6424 // Don't handle long double formats, which have strange constraints.
6425 return nullptr;
6426 }
6427
6428 // We can handle constant integers that are multiple of 8 bits.
6429 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
6430 if (CI->getBitWidth() % 8 == 0) {
6431 if (!CI->getValue().isSplat(8))
6432 return nullptr;
6433 return ConstantInt::get(Ctx, CI->getValue().trunc(8));
6434 }
6435 }
6436
6437 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
6438 if (CE->getOpcode() == Instruction::IntToPtr) {
6439 if (auto *PtrTy = dyn_cast<PointerType>(CE->getType())) {
6440 unsigned BitWidth = DL.getPointerSizeInBits(PtrTy->getAddressSpace());
6442 CE->getOperand(0), Type::getIntNTy(Ctx, BitWidth), false, DL))
6443 return isBytewiseValue(Op, DL);
6444 }
6445 }
6446 }
6447
6448 auto Merge = [&](Value *LHS, Value *RHS) -> Value * {
6449 if (LHS == RHS)
6450 return LHS;
6451 if (!LHS || !RHS)
6452 return nullptr;
6453 if (LHS == UndefInt8)
6454 return RHS;
6455 if (RHS == UndefInt8)
6456 return LHS;
6457 return nullptr;
6458 };
6459
6461 Value *Val = UndefInt8;
6462 for (uint64_t I = 0, E = CA->getNumElements(); I != E; ++I)
6463 if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))
6464 return nullptr;
6465 return Val;
6466 }
6467
6469 Value *Val = UndefInt8;
6470 for (Value *Op : C->operands())
6471 if (!(Val = Merge(Val, isBytewiseValue(Op, DL))))
6472 return nullptr;
6473 return Val;
6474 }
6475
6476 // Don't try to handle the handful of other constants.
6477 return nullptr;
6478}
6479
6480// This is the recursive version of BuildSubAggregate. It takes a few different
6481// arguments. Idxs is the index within the nested struct From that we are
6482// looking at now (which is of type IndexedType). IdxSkip is the number of
6483// indices from Idxs that should be left out when inserting into the resulting
6484// struct. To is the result struct built so far, new insertvalue instructions
6485// build on that.
6486static Value *BuildSubAggregate(Value *From, Value *To, Type *IndexedType,
6488 unsigned IdxSkip,
6489 BasicBlock::iterator InsertBefore) {
6490 StructType *STy = dyn_cast<StructType>(IndexedType);
6491 if (STy) {
6492 // Save the original To argument so we can modify it
6493 Value *OrigTo = To;
6494 // General case, the type indexed by Idxs is a struct
6495 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
6496 // Process each struct element recursively
6497 Idxs.push_back(i);
6498 Value *PrevTo = To;
6499 To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
6500 InsertBefore);
6501 Idxs.pop_back();
6502 if (!To) {
6503 // Couldn't find any inserted value for this index? Cleanup
6504 while (PrevTo != OrigTo) {
6506 PrevTo = Del->getAggregateOperand();
6507 Del->eraseFromParent();
6508 }
6509 // Stop processing elements
6510 break;
6511 }
6512 }
6513 // If we successfully found a value for each of our subaggregates
6514 if (To)
6515 return To;
6516 }
6517 // Base case, the type indexed by SourceIdxs is not a struct, or not all of
6518 // the struct's elements had a value that was inserted directly. In the latter
6519 // case, perhaps we can't determine each of the subelements individually, but
6520 // we might be able to find the complete struct somewhere.
6521
6522 // Find the value that is at that particular spot
6523 Value *V = FindInsertedValue(From, Idxs);
6524
6525 if (!V)
6526 return nullptr;
6527
6528 // Insert the value in the new (sub) aggregate
6529 return InsertValueInst::Create(To, V, ArrayRef(Idxs).slice(IdxSkip), "tmp",
6530 InsertBefore);
6531}
6532
6533// This helper takes a nested struct and extracts a part of it (which is again a
6534// struct) into a new value. For example, given the struct:
6535// { a, { b, { c, d }, e } }
6536// and the indices "1, 1" this returns
6537// { c, d }.
6538//
6539// It does this by inserting an insertvalue for each element in the resulting
6540// struct, as opposed to just inserting a single struct. This will only work if
6541// each of the elements of the substruct are known (ie, inserted into From by an
6542// insertvalue instruction somewhere).
6543//
6544// All inserted insertvalue instructions are inserted before InsertBefore
6546 BasicBlock::iterator InsertBefore) {
6547 Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
6548 idx_range);
6549 Value *To = PoisonValue::get(IndexedType);
6550 SmallVector<unsigned, 10> Idxs(idx_range);
6551 unsigned IdxSkip = Idxs.size();
6552
6553 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
6554}
6555
6556/// Given an aggregate and a sequence of indices, see if the scalar value
6557/// indexed is already around as a register, for example if it was inserted
6558/// directly into the aggregate.
6559///
6560/// If InsertBefore is not null, this function will duplicate (modified)
6561/// insertvalues when a part of a nested struct is extracted.
6562Value *
6564 std::optional<BasicBlock::iterator> InsertBefore) {
6565 // Nothing to index? Just return V then (this is useful at the end of our
6566 // recursion).
6567 if (idx_range.empty())
6568 return V;
6569 // We have indices, so V should have an indexable type.
6570 assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
6571 "Not looking at a struct or array?");
6572 assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
6573 "Invalid indices for type?");
6574
6575 if (Constant *C = dyn_cast<Constant>(V)) {
6576 C = C->getAggregateElement(idx_range[0]);
6577 if (!C) return nullptr;
6578 return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
6579 }
6580
6582 // Loop the indices for the insertvalue instruction in parallel with the
6583 // requested indices
6584 const unsigned *req_idx = idx_range.begin();
6585 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
6586 i != e; ++i, ++req_idx) {
6587 if (req_idx == idx_range.end()) {
6588 // We can't handle this without inserting insertvalues
6589 if (!InsertBefore)
6590 return nullptr;
6591
6592 // The requested index identifies a part of a nested aggregate. Handle
6593 // this specially. For example,
6594 // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
6595 // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
6596 // %C = extractvalue {i32, { i32, i32 } } %B, 1
6597 // This can be changed into
6598 // %A = insertvalue {i32, i32 } undef, i32 10, 0
6599 // %C = insertvalue {i32, i32 } %A, i32 11, 1
6600 // which allows the unused 0,0 element from the nested struct to be
6601 // removed.
6602 return BuildSubAggregate(V, ArrayRef(idx_range.begin(), req_idx),
6603 *InsertBefore);
6604 }
6605
6606 // This insert value inserts something else than what we are looking for.
6607 // See if the (aggregate) value inserted into has the value we are
6608 // looking for, then.
6609 if (*req_idx != *i)
6610 return FindInsertedValue(I->getAggregateOperand(), idx_range,
6611 InsertBefore);
6612 }
6613 // If we end up here, the indices of the insertvalue match with those
6614 // requested (though possibly only partially). Now we recursively look at
6615 // the inserted value, passing any remaining indices.
6616 return FindInsertedValue(I->getInsertedValueOperand(),
6617 ArrayRef(req_idx, idx_range.end()), InsertBefore);
6618 }
6619
6621 // If we're extracting a value from an aggregate that was extracted from
6622 // something else, we can extract from that something else directly instead.
6623 // However, we will need to chain I's indices with the requested indices.
6624
6625 // Calculate the number of indices required
6626 unsigned size = I->getNumIndices() + idx_range.size();
6627 // Allocate some space to put the new indices in
6629 Idxs.reserve(size);
6630 // Add indices from the extract value instruction
6631 Idxs.append(I->idx_begin(), I->idx_end());
6632
6633 // Add requested indices
6634 Idxs.append(idx_range.begin(), idx_range.end());
6635
6636 assert(Idxs.size() == size
6637 && "Number of indices added not correct?");
6638
6639 return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
6640 }
6641 // Otherwise, we don't know (such as, extracting from a function return value
6642 // or load instruction)
6643 return nullptr;
6644}
6645
6646// If V refers to an initialized global constant, set Slice either to
6647// its initializer if the size of its elements equals ElementSize, or,
6648// for ElementSize == 8, to its representation as an array of unsiged
6649// char. Return true on success.
6650// Offset is in the unit "nr of ElementSize sized elements".
6653 unsigned ElementSize, uint64_t Offset) {
6654 assert(V && "V should not be null.");
6655 assert((ElementSize % 8) == 0 &&
6656 "ElementSize expected to be a multiple of the size of a byte.");
6657 unsigned ElementSizeInBytes = ElementSize / 8;
6658
6659 // Drill down into the pointer expression V, ignoring any intervening
6660 // casts, and determine the identity of the object it references along
6661 // with the cumulative byte offset into it.
6662 const GlobalVariable *GV =
6664 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
6665 // Fail if V is not based on constant global object.
6666 return false;
6667
6668 const DataLayout &DL = GV->getDataLayout();
6669 APInt Off(DL.getIndexTypeSizeInBits(V->getType()), 0);
6670
6671 if (GV != V->stripAndAccumulateConstantOffsets(DL, Off,
6672 /*AllowNonInbounds*/ true))
6673 // Fail if a constant offset could not be determined.
6674 return false;
6675
6676 uint64_t StartIdx = Off.getLimitedValue();
6677 if (StartIdx == UINT64_MAX)
6678 // Fail if the constant offset is excessive.
6679 return false;
6680
6681 // Off/StartIdx is in the unit of bytes. So we need to convert to number of
6682 // elements. Simply bail out if that isn't possible.
6683 if ((StartIdx % ElementSizeInBytes) != 0)
6684 return false;
6685
6686 Offset += StartIdx / ElementSizeInBytes;
6687 ConstantDataArray *Array = nullptr;
6688 ArrayType *ArrayTy = nullptr;
6689
6690 if (GV->getInitializer()->isNullValue()) {
6691 Type *GVTy = GV->getValueType();
6692 uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy).getFixedValue();
6693 uint64_t Length = SizeInBytes / ElementSizeInBytes;
6694
6695 Slice.Array = nullptr;
6696 Slice.Offset = 0;
6697 // Return an empty Slice for undersized constants to let callers
6698 // transform even undefined library calls into simpler, well-defined
6699 // expressions. This is preferable to making the calls although it
6700 // prevents sanitizers from detecting such calls.
6701 Slice.Length = Length < Offset ? 0 : Length - Offset;
6702 return true;
6703 }
6704
6705 auto *Init = const_cast<Constant *>(GV->getInitializer());
6706 if (auto *ArrayInit = dyn_cast<ConstantDataArray>(Init)) {
6707 Type *InitElTy = ArrayInit->getElementType();
6708 if (InitElTy->isIntegerTy(ElementSize)) {
6709 // If Init is an initializer for an array of the expected type
6710 // and size, use it as is.
6711 Array = ArrayInit;
6712 ArrayTy = ArrayInit->getType();
6713 }
6714 }
6715
6716 if (!Array) {
6717 if (ElementSize != 8)
6718 // TODO: Handle conversions to larger integral types.
6719 return false;
6720
6721 // Otherwise extract the portion of the initializer starting
6722 // at Offset as an array of bytes, and reset Offset.
6724 if (!Init)
6725 return false;
6726
6727 Offset = 0;
6729 ArrayTy = dyn_cast<ArrayType>(Init->getType());
6730 }
6731
6732 uint64_t NumElts = ArrayTy->getArrayNumElements();
6733 if (Offset > NumElts)
6734 return false;
6735
6736 Slice.Array = Array;
6737 Slice.Offset = Offset;
6738 Slice.Length = NumElts - Offset;
6739 return true;
6740}
6741
6742/// Extract bytes from the initializer of the constant array V, which need
6743/// not be a nul-terminated string. On success, store the bytes in Str and
6744/// return true. When TrimAtNul is set, Str will contain only the bytes up
6745/// to but not including the first nul. Return false on failure.
6747 bool TrimAtNul) {
6749 if (!getConstantDataArrayInfo(V, Slice, 8))
6750 return false;
6751
6752 if (Slice.Array == nullptr) {
6753 if (TrimAtNul) {
6754 // Return a nul-terminated string even for an empty Slice. This is
6755 // safe because all existing SimplifyLibcalls callers require string
6756 // arguments and the behavior of the functions they fold is undefined
6757 // otherwise. Folding the calls this way is preferable to making
6758 // the undefined library calls, even though it prevents sanitizers
6759 // from reporting such calls.
6760 Str = StringRef();
6761 return true;
6762 }
6763 if (Slice.Length == 1) {
6764 Str = StringRef("", 1);
6765 return true;
6766 }
6767 // We cannot instantiate a StringRef as we do not have an appropriate string
6768 // of 0s at hand.
6769 return false;
6770 }
6771
6772 // Start out with the entire array in the StringRef.
6773 Str = Slice.Array->getAsString();
6774 // Skip over 'offset' bytes.
6775 Str = Str.substr(Slice.Offset);
6776
6777 if (TrimAtNul) {
6778 // Trim off the \0 and anything after it. If the array is not nul
6779 // terminated, we just return the whole end of string. The client may know
6780 // some other way that the string is length-bound.
6781 Str = Str.substr(0, Str.find('\0'));
6782 }
6783 return true;
6784}
6785
6786// These next two are very similar to the above, but also look through PHI
6787// nodes.
6788// TODO: See if we can integrate these two together.
6789
6790/// If we can compute the length of the string pointed to by
6791/// the specified pointer, return 'len+1'. If we can't, return 0.
6794 unsigned CharSize) {
6795 // Look through noop bitcast instructions.
6796 V = V->stripPointerCasts();
6797
6798 // If this is a PHI node, there are two cases: either we have already seen it
6799 // or we haven't.
6800 if (const PHINode *PN = dyn_cast<PHINode>(V)) {
6801 if (!PHIs.insert(PN).second)
6802 return ~0ULL; // already in the set.
6803
6804 // If it was new, see if all the input strings are the same length.
6805 uint64_t LenSoFar = ~0ULL;
6806 for (Value *IncValue : PN->incoming_values()) {
6807 uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize);
6808 if (Len == 0) return 0; // Unknown length -> unknown.
6809
6810 if (Len == ~0ULL) continue;
6811
6812 if (Len != LenSoFar && LenSoFar != ~0ULL)
6813 return 0; // Disagree -> unknown.
6814 LenSoFar = Len;
6815 }
6816
6817 // Success, all agree.
6818 return LenSoFar;
6819 }
6820
6821 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
6822 if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
6823 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);
6824 if (Len1 == 0) return 0;
6825 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);
6826 if (Len2 == 0) return 0;
6827 if (Len1 == ~0ULL) return Len2;
6828 if (Len2 == ~0ULL) return Len1;
6829 if (Len1 != Len2) return 0;
6830 return Len1;
6831 }
6832
6833 // Otherwise, see if we can read the string.
6835 if (!getConstantDataArrayInfo(V, Slice, CharSize))
6836 return 0;
6837
6838 if (Slice.Array == nullptr)
6839 // Zeroinitializer (including an empty one).
6840 return 1;
6841
6842 // Search for the first nul character. Return a conservative result even
6843 // when there is no nul. This is safe since otherwise the string function
6844 // being folded such as strlen is undefined, and can be preferable to
6845 // making the undefined library call.
6846 unsigned NullIndex = 0;
6847 for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) {
6848 if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0)
6849 break;
6850 }
6851
6852 return NullIndex + 1;
6853}
6854
6855/// If we can compute the length of the string pointed to by
6856/// the specified pointer, return 'len+1'. If we can't, return 0.
6857uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) {
6858 if (!V->getType()->isPointerTy())
6859 return 0;
6860
6862 uint64_t Len = GetStringLengthH(V, PHIs, CharSize);
6863 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
6864 // an empty string as a length.
6865 return Len == ~0ULL ? 1 : Len;
6866}
6867
6868const Value *
6870 bool MustPreserveOffset) {
6871 assert(Call &&
6872 "getArgumentAliasingToReturnedPointer only works on nonnull calls");
6873 if (const Value *RV = Call->getReturnedArgOperand())
6874 return RV;
6875 // This can be used only as a aliasing property.
6877 Call, MustPreserveOffset))
6878 return Call->getArgOperand(0);
6879 return nullptr;
6880}
6881
6883 const CallBase *Call, bool MustPreserveOffset) {
6884 switch (Call->getIntrinsicID()) {
6885 case Intrinsic::launder_invariant_group:
6886 case Intrinsic::strip_invariant_group:
6887 case Intrinsic::aarch64_irg:
6888 case Intrinsic::aarch64_tagp:
6889 // The amdgcn_make_buffer_rsrc function does not alter the address of the
6890 // input pointer (and thus preserves the byte offset, which is the property
6891 // the MustPreserveOffset flag selects). However, it will not necessarily
6892 // map ptr addrspace(N) null to ptr addrspace(8) null, aka the "null
6893 // descriptor", which has "all loads return 0, all stores are dropped"
6894 // semantics. Given the context of this intrinsic list, no one should be
6895 // relying on such a strict bit-exact null mapping (and, at time of
6896 // writing, they are not), but we document this fact out of an abundance
6897 // of caution.
6898 case Intrinsic::amdgcn_make_buffer_rsrc:
6899 return true;
6900 case Intrinsic::ptrmask:
6901 return !MustPreserveOffset;
6902 case Intrinsic::threadlocal_address:
6903 // The underlying variable changes with thread ID. The Thread ID may change
6904 // at coroutine suspend points.
6905 return !Call->getParent()->getParent()->isPresplitCoroutine();
6906 default:
6907 return false;
6908 }
6909}
6910
6911/// \p PN defines a loop-variant pointer to an object. Check if the
6912/// previous iteration of the loop was referring to the same object as \p PN.
6914 const LoopInfo *LI) {
6915 // Find the loop-defined value.
6916 Loop *L = LI->getLoopFor(PN->getParent());
6917 if (PN->getNumIncomingValues() != 2)
6918 return true;
6919
6920 // Find the value from previous iteration.
6921 auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
6922 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6923 PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
6924 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6925 return true;
6926
6927 // If a new pointer is loaded in the loop, the pointer references a different
6928 // object in every iteration. E.g.:
6929 // for (i)
6930 // int *p = a[i];
6931 // ...
6932 if (auto *Load = dyn_cast<LoadInst>(PrevValue))
6933 if (!L->isLoopInvariant(Load->getPointerOperand()))
6934 return false;
6935 return true;
6936}
6937
6938const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup) {
6939 for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
6940 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
6941 const Value *PtrOp = GEP->getPointerOperand();
6942 if (!PtrOp->getType()->isPointerTy()) // Only handle scalar pointer base.
6943 return V;
6944 V = PtrOp;
6945 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
6946 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
6947 Value *NewV = cast<Operator>(V)->getOperand(0);
6948 if (!NewV->getType()->isPointerTy())
6949 return V;
6950 V = NewV;
6951 } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
6952 if (GA->isInterposable())
6953 return V;
6954 V = GA->getAliasee();
6955 } else {
6956 if (auto *PHI = dyn_cast<PHINode>(V)) {
6957 // Look through single-arg phi nodes created by LCSSA.
6958 if (PHI->getNumIncomingValues() == 1) {
6959 V = PHI->getIncomingValue(0);
6960 continue;
6961 }
6962 } else if (auto *Call = dyn_cast<CallBase>(V)) {
6963 // CaptureTracking can know about special capturing properties of some
6964 // intrinsics like launder.invariant.group, that can't be expressed with
6965 // the attributes, but have properties like returning aliasing pointer.
6966 // Because some analysis may assume that nocaptured pointer is not
6967 // returned from some special intrinsic (because function would have to
6968 // be marked with returns attribute), it is crucial to use this function
6969 // because it should be in sync with CaptureTracking. Not using it may
6970 // cause weird miscompilations where 2 aliasing pointers are assumed to
6971 // noalias.
6973 Call, /*MustPreserveOffset=*/false)) {
6974 V = RP;
6975 continue;
6976 }
6977 }
6978
6979 return V;
6980 }
6981 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
6982 }
6983 return V;
6984}
6985
6988 const LoopInfo *LI, unsigned MaxLookup) {
6991 Worklist.push_back(V);
6992 do {
6993 const Value *P = Worklist.pop_back_val();
6994 P = getUnderlyingObject(P, MaxLookup);
6995
6996 if (!Visited.insert(P).second)
6997 continue;
6998
6999 if (auto *SI = dyn_cast<SelectInst>(P)) {
7000 Worklist.push_back(SI->getTrueValue());
7001 Worklist.push_back(SI->getFalseValue());
7002 continue;
7003 }
7004
7005 if (auto *PN = dyn_cast<PHINode>(P)) {
7006 // If this PHI changes the underlying object in every iteration of the
7007 // loop, don't look through it. Consider:
7008 // int **A;
7009 // for (i) {
7010 // Prev = Curr; // Prev = PHI (Prev_0, Curr)
7011 // Curr = A[i];
7012 // *Prev, *Curr;
7013 //
7014 // Prev is tracking Curr one iteration behind so they refer to different
7015 // underlying objects.
7016 if (!LI || !LI->isLoopHeader(PN->getParent()) ||
7018 append_range(Worklist, PN->incoming_values());
7019 else
7020 Objects.push_back(P);
7021 continue;
7022 }
7023
7024 Objects.push_back(P);
7025 } while (!Worklist.empty());
7026}
7027
7029 const unsigned MaxVisited = 8;
7030
7033 Worklist.push_back(V);
7034 const Value *Object = nullptr;
7035 // Used as fallback if we can't find a common underlying object through
7036 // recursion.
7037 bool First = true;
7038 const Value *FirstObject = getUnderlyingObject(V);
7039 do {
7040 const Value *P = Worklist.pop_back_val();
7041 P = First ? FirstObject : getUnderlyingObject(P);
7042 First = false;
7043
7044 if (!Visited.insert(P).second)
7045 continue;
7046
7047 if (Visited.size() == MaxVisited)
7048 return FirstObject;
7049
7050 if (auto *SI = dyn_cast<SelectInst>(P)) {
7051 Worklist.push_back(SI->getTrueValue());
7052 Worklist.push_back(SI->getFalseValue());
7053 continue;
7054 }
7055
7056 if (auto *PN = dyn_cast<PHINode>(P)) {
7057 append_range(Worklist, PN->incoming_values());
7058 continue;
7059 }
7060
7061 if (!Object)
7062 Object = P;
7063 else if (Object != P)
7064 return FirstObject;
7065 } while (!Worklist.empty());
7066
7067 return Object ? Object : FirstObject;
7068}
7069
7070/// This is the function that does the work of looking through basic
7071/// ptrtoint+arithmetic+inttoptr sequences.
7072static const Value *getUnderlyingObjectFromInt(const Value *V) {
7073 do {
7074 if (const Operator *U = dyn_cast<Operator>(V)) {
7075 // If we find a ptrtoint, we can transfer control back to the
7076 // regular getUnderlyingObjectFromInt.
7077 if (U->getOpcode() == Instruction::PtrToInt)
7078 return U->getOperand(0);
7079 // If we find an add of a constant, a multiplied value, or a phi, it's
7080 // likely that the other operand will lead us to the base
7081 // object. We don't have to worry about the case where the
7082 // object address is somehow being computed by the multiply,
7083 // because our callers only care when the result is an
7084 // identifiable object.
7085 if (U->getOpcode() != Instruction::Add ||
7086 (!isa<ConstantInt>(U->getOperand(1)) &&
7087 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
7088 !isa<PHINode>(U->getOperand(1))))
7089 return V;
7090 V = U->getOperand(0);
7091 } else {
7092 return V;
7093 }
7094 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
7095 } while (true);
7096}
7097
7098/// This is a wrapper around getUnderlyingObjects and adds support for basic
7099/// ptrtoint+arithmetic+inttoptr sequences.
7100/// It returns false if unidentified object is found in getUnderlyingObjects.
7102 SmallVectorImpl<Value *> &Objects) {
7104 SmallVector<const Value *, 4> Working(1, V);
7105 do {
7106 V = Working.pop_back_val();
7107
7109 getUnderlyingObjects(V, Objs);
7110
7111 for (const Value *V : Objs) {
7112 if (!Visited.insert(V).second)
7113 continue;
7114 if (Operator::getOpcode(V) == Instruction::IntToPtr) {
7115 const Value *O =
7116 getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
7117 if (O->getType()->isPointerTy()) {
7118 Working.push_back(O);
7119 continue;
7120 }
7121 }
7122 // If getUnderlyingObjects fails to find an identifiable object,
7123 // getUnderlyingObjectsForCodeGen also fails for safety.
7124 if (!isIdentifiedObject(V)) {
7125 Objects.clear();
7126 return false;
7127 }
7128 Objects.push_back(const_cast<Value *>(V));
7129 }
7130 } while (!Working.empty());
7131 return true;
7132}
7133
7135 AllocaInst *Result = nullptr;
7137 SmallVector<Value *, 4> Worklist;
7138
7139 auto AddWork = [&](Value *V) {
7140 if (Visited.insert(V).second)
7141 Worklist.push_back(V);
7142 };
7143
7144 AddWork(V);
7145 do {
7146 V = Worklist.pop_back_val();
7147 assert(Visited.count(V));
7148
7149 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
7150 if (Result && Result != AI)
7151 return nullptr;
7152 Result = AI;
7153 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
7154 AddWork(CI->getOperand(0));
7155 } else if (PHINode *PN = dyn_cast<PHINode>(V)) {
7156 for (Value *IncValue : PN->incoming_values())
7157 AddWork(IncValue);
7158 } else if (auto *SI = dyn_cast<SelectInst>(V)) {
7159 AddWork(SI->getTrueValue());
7160 AddWork(SI->getFalseValue());
7162 if (OffsetZero && !GEP->hasAllZeroIndices())
7163 return nullptr;
7164 AddWork(GEP->getPointerOperand());
7165 } else if (CallBase *CB = dyn_cast<CallBase>(V)) {
7166 Value *Returned = CB->getReturnedArgOperand();
7167 if (Returned)
7168 AddWork(Returned);
7169 else
7170 return nullptr;
7171 } else {
7172 return nullptr;
7173 }
7174 } while (!Worklist.empty());
7175
7176 return Result;
7177}
7178
7180 const Value *V, bool AllowLifetime, bool AllowDroppable) {
7181 for (const User *U : V->users()) {
7183 if (!II)
7184 return false;
7185
7186 if (AllowLifetime && II->isLifetimeStartOrEnd())
7187 continue;
7188
7189 if (AllowDroppable && II->isDroppable())
7190 continue;
7191
7192 return false;
7193 }
7194 return true;
7195}
7196
7199 V, /* AllowLifetime */ true, /* AllowDroppable */ false);
7200}
7203 V, /* AllowLifetime */ true, /* AllowDroppable */ true);
7204}
7205
7207 if (auto *II = dyn_cast<IntrinsicInst>(I))
7208 return isTriviallyVectorizable(II->getIntrinsicID());
7209 auto *Shuffle = dyn_cast<ShuffleVectorInst>(I);
7210 return (!Shuffle || Shuffle->isSelect()) &&
7212}
7213
7215 const Instruction *Inst, const Instruction *CtxI, AssumptionCache *AC,
7216 const DominatorTree *DT, const TargetLibraryInfo *TLI, bool UseVariableInfo,
7217 bool IgnoreUBImplyingAttrs) {
7218 return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI,
7219 AC, DT, TLI, UseVariableInfo,
7220 IgnoreUBImplyingAttrs);
7221}
7222
7224 unsigned Opcode, const Instruction *Inst, const Instruction *CtxI,
7225 AssumptionCache *AC, const DominatorTree *DT, const TargetLibraryInfo *TLI,
7226 bool UseVariableInfo, bool IgnoreUBImplyingAttrs) {
7227#ifndef NDEBUG
7228 if (Inst->getOpcode() != Opcode) {
7229 // Check that the operands are actually compatible with the Opcode override.
7230 auto hasEqualReturnAndLeadingOperandTypes =
7231 [](const Instruction *Inst, unsigned NumLeadingOperands) {
7232 if (Inst->getNumOperands() < NumLeadingOperands)
7233 return false;
7234 const Type *ExpectedType = Inst->getType();
7235 for (unsigned ItOp = 0; ItOp < NumLeadingOperands; ++ItOp)
7236 if (Inst->getOperand(ItOp)->getType() != ExpectedType)
7237 return false;
7238 return true;
7239 };
7241 hasEqualReturnAndLeadingOperandTypes(Inst, 2));
7242 assert(!Instruction::isUnaryOp(Opcode) ||
7243 hasEqualReturnAndLeadingOperandTypes(Inst, 1));
7244 }
7245#endif
7246
7247 switch (Opcode) {
7248 default:
7249 return true;
7250 case Instruction::UDiv:
7251 case Instruction::URem: {
7252 // x / y is undefined if y == 0.
7253 const APInt *V;
7254 if (match(Inst->getOperand(1), m_APInt(V)))
7255 return *V != 0;
7256 return false;
7257 }
7258 case Instruction::SDiv:
7259 case Instruction::SRem: {
7260 // x / y is undefined if y == 0 or x == INT_MIN and y == -1
7261 const APInt *Numerator, *Denominator;
7262 if (!match(Inst->getOperand(1), m_APInt(Denominator)))
7263 return false;
7264 // We cannot hoist this division if the denominator is 0.
7265 if (*Denominator == 0)
7266 return false;
7267 // It's safe to hoist if the denominator is not 0 or -1.
7268 if (!Denominator->isAllOnes())
7269 return true;
7270 // At this point we know that the denominator is -1. It is safe to hoist as
7271 // long we know that the numerator is not INT_MIN.
7272 if (match(Inst->getOperand(0), m_APInt(Numerator)))
7273 return !Numerator->isMinSignedValue();
7274 // The numerator *might* be MinSignedValue.
7275 return false;
7276 }
7277 case Instruction::Load: {
7278 if (!UseVariableInfo)
7279 return false;
7280
7281 const LoadInst *LI = dyn_cast<LoadInst>(Inst);
7282 if (!LI)
7283 return false;
7284 if (mustSuppressSpeculation(*LI))
7285 return false;
7286 const DataLayout &DL = LI->getDataLayout();
7288 LI->getType(), LI->getAlign(), DL,
7289 CtxI, AC, DT, TLI);
7290 }
7291 case Instruction::Call: {
7292 auto *CI = dyn_cast<const CallInst>(Inst);
7293 if (!CI)
7294 return false;
7295 const Function *Callee = CI->getCalledFunction();
7296
7297 // The called function could have undefined behavior or side-effects, even
7298 // if marked readnone nounwind.
7299 if (!Callee || !Callee->isSpeculatable())
7300 return false;
7301 // Since the operands may be changed after hoisting, undefined behavior may
7302 // be triggered by some UB-implying attributes.
7303 return IgnoreUBImplyingAttrs || !CI->hasUBImplyingAttrs();
7304 }
7305 case Instruction::VAArg:
7306 case Instruction::Alloca:
7307 case Instruction::Invoke:
7308 case Instruction::CallBr:
7309 case Instruction::PHI:
7310 case Instruction::Store:
7311 case Instruction::Ret:
7312 case Instruction::UncondBr:
7313 case Instruction::CondBr:
7314 case Instruction::IndirectBr:
7315 case Instruction::Switch:
7316 case Instruction::Unreachable:
7317 case Instruction::Fence:
7318 case Instruction::AtomicRMW:
7319 case Instruction::AtomicCmpXchg:
7320 case Instruction::LandingPad:
7321 case Instruction::Resume:
7322 case Instruction::CatchSwitch:
7323 case Instruction::CatchPad:
7324 case Instruction::CatchRet:
7325 case Instruction::CleanupPad:
7326 case Instruction::CleanupRet:
7327 return false; // Misc instructions which have effects
7328 }
7329}
7330
7332 if (I.mayReadOrWriteMemory())
7333 // Memory dependency possible
7334 return true;
7336 // Can't move above a maythrow call or infinite loop. Or if an
7337 // inalloca alloca, above a stacksave call.
7338 return true;
7340 // 1) Can't reorder two inf-loop calls, even if readonly
7341 // 2) Also can't reorder an inf-loop call below a instruction which isn't
7342 // safe to speculative execute. (Inverse of above)
7343 return true;
7344 return false;
7345}
7346
7347/// Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
7361
7362/// Combine constant ranges from computeConstantRange() and computeKnownBits().
7365 bool ForSigned,
7366 const SimplifyQuery &SQ) {
7367 ConstantRange CR1 =
7368 ConstantRange::fromKnownBits(V.getKnownBits(SQ), ForSigned);
7369 ConstantRange CR2 = computeConstantRange(V, ForSigned, SQ);
7372 return CR1.intersectWith(CR2, RangeType);
7373}
7374
7376 const Value *RHS,
7377 const SimplifyQuery &SQ,
7378 bool IsNSW) {
7379 ConstantRange LHSRange =
7380 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7381 ConstantRange RHSRange =
7382 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7383
7384 // mul nsw of two non-negative numbers is also nuw.
7385 if (IsNSW && LHSRange.isAllNonNegative() && RHSRange.isAllNonNegative())
7387
7388 return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange));
7389}
7390
7392 const Value *RHS,
7393 const SimplifyQuery &SQ) {
7394 // Multiplying n * m significant bits yields a result of n + m significant
7395 // bits. If the total number of significant bits does not exceed the
7396 // result bit width (minus 1), there is no overflow.
7397 // This means if we have enough leading sign bits in the operands
7398 // we can guarantee that the result does not overflow.
7399 // Ref: "Hacker's Delight" by Henry Warren
7400 unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
7401
7402 // Note that underestimating the number of sign bits gives a more
7403 // conservative answer.
7404 unsigned SignBits =
7405 ::ComputeNumSignBits(LHS, SQ) + ::ComputeNumSignBits(RHS, SQ);
7406
7407 // First handle the easy case: if we have enough sign bits there's
7408 // definitely no overflow.
7409 if (SignBits > BitWidth + 1)
7411
7412 // There are two ambiguous cases where there can be no overflow:
7413 // SignBits == BitWidth + 1 and
7414 // SignBits == BitWidth
7415 // The second case is difficult to check, therefore we only handle the
7416 // first case.
7417 if (SignBits == BitWidth + 1) {
7418 // It overflows only when both arguments are negative and the true
7419 // product is exactly the minimum negative number.
7420 // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
7421 // For simplicity we just check if at least one side is not negative.
7422 KnownBits LHSKnown = computeKnownBits(LHS, SQ);
7423 KnownBits RHSKnown = computeKnownBits(RHS, SQ);
7424 if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative())
7426 }
7428}
7429
7432 const WithCache<const Value *> &RHS,
7433 const SimplifyQuery &SQ) {
7434 ConstantRange LHSRange =
7435 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7436 ConstantRange RHSRange =
7437 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7438 return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange));
7439}
7440
7441static OverflowResult
7444 const AddOperator *Add, const SimplifyQuery &SQ) {
7445 if (Add && Add->hasNoSignedWrap()) {
7447 }
7448
7449 // If LHS and RHS each have at least two sign bits, the addition will look
7450 // like
7451 //
7452 // XX..... +
7453 // YY.....
7454 //
7455 // If the carry into the most significant position is 0, X and Y can't both
7456 // be 1 and therefore the carry out of the addition is also 0.
7457 //
7458 // If the carry into the most significant position is 1, X and Y can't both
7459 // be 0 and therefore the carry out of the addition is also 1.
7460 //
7461 // Since the carry into the most significant position is always equal to
7462 // the carry out of the addition, there is no signed overflow.
7463 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
7465
7466 ConstantRange LHSRange =
7467 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7468 ConstantRange RHSRange =
7469 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7470 OverflowResult OR =
7471 mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange));
7473 return OR;
7474
7475 // The remaining code needs Add to be available. Early returns if not so.
7476 if (!Add)
7478
7479 // If the sign of Add is the same as at least one of the operands, this add
7480 // CANNOT overflow. If this can be determined from the known bits of the
7481 // operands the above signedAddMayOverflow() check will have already done so.
7482 // The only other way to improve on the known bits is from an assumption, so
7483 // call computeKnownBitsFromContext() directly.
7484 bool LHSOrRHSKnownNonNegative =
7485 (LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative());
7486 bool LHSOrRHSKnownNegative =
7487 (LHSRange.isAllNegative() || RHSRange.isAllNegative());
7488 if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
7489 KnownBits AddKnown(LHSRange.getBitWidth());
7490 computeKnownBitsFromContext(Add, AddKnown, SQ);
7491 if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||
7492 (AddKnown.isNegative() && LHSOrRHSKnownNegative))
7494 }
7495
7497}
7498
7500 const Value *RHS,
7501 const SimplifyQuery &SQ) {
7502 // X - (X % ?)
7503 // The remainder of a value can't have greater magnitude than itself,
7504 // so the subtraction can't overflow.
7505
7506 // X - (X -nuw ?)
7507 // In the minimal case, this would simplify to "?", so there's no subtract
7508 // at all. But if this analysis is used to peek through casts, for example,
7509 // then determining no-overflow may allow other transforms.
7510
7511 // TODO: There are other patterns like this.
7512 // See simplifyICmpWithBinOpOnLHS() for candidates.
7513 if (match(RHS, m_URem(m_Specific(LHS), m_Value())) ||
7514 match(RHS, m_NUWSub(m_Specific(LHS), m_Value())))
7515 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7517
7518 if (auto C = isImpliedByDomCondition(CmpInst::ICMP_UGE, LHS, RHS, SQ.CxtI,
7519 SQ.DL)) {
7520 if (*C)
7523 }
7524
7525 ConstantRange LHSRange =
7526 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7527 ConstantRange RHSRange =
7528 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7529 return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange));
7530}
7531
7533 const Value *RHS,
7534 const SimplifyQuery &SQ) {
7535 // X - (X % ?)
7536 // The remainder of a value can't have greater magnitude than itself,
7537 // so the subtraction can't overflow.
7538
7539 // X - (X -nsw ?)
7540 // In the minimal case, this would simplify to "?", so there's no subtract
7541 // at all. But if this analysis is used to peek through casts, for example,
7542 // then determining no-overflow may allow other transforms.
7543 if (match(RHS, m_SRem(m_Specific(LHS), m_Value())) ||
7544 match(RHS, m_NSWSub(m_Specific(LHS), m_Value())))
7545 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7547
7548 // If LHS and RHS each have at least two sign bits, the subtraction
7549 // cannot overflow.
7550 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
7552
7553 ConstantRange LHSRange =
7554 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7555 ConstantRange RHSRange =
7556 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7557 return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange));
7558}
7559
7561 const DominatorTree &DT) {
7562 SmallVector<const CondBrInst *, 2> GuardingBranches;
7564
7565 for (const User *U : WO->users()) {
7566 if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) {
7567 assert(EVI->getNumIndices() == 1 && "Obvious from CI's type");
7568
7569 if (EVI->getIndices()[0] == 0)
7570 Results.push_back(EVI);
7571 else {
7572 assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type");
7573
7574 for (const auto *U : EVI->users())
7575 if (const auto *B = dyn_cast<CondBrInst>(U))
7576 GuardingBranches.push_back(B);
7577 }
7578 } else {
7579 // We are using the aggregate directly in a way we don't want to analyze
7580 // here (storing it to a global, say).
7581 return false;
7582 }
7583 }
7584
7585 auto AllUsesGuardedByBranch = [&](const CondBrInst *BI) {
7586 BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1));
7587
7588 // Check if all users of the add are provably no-wrap.
7589 for (const auto *Result : Results) {
7590 // If the extractvalue itself is not executed on overflow, the we don't
7591 // need to check each use separately, since domination is transitive.
7592 if (DT.dominates(NoWrapEdge, Result->getParent()))
7593 continue;
7594
7595 for (const auto &RU : Result->uses())
7596 if (!DT.dominates(NoWrapEdge, RU))
7597 return false;
7598 }
7599
7600 return true;
7601 };
7602
7603 return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
7604}
7605
7606/// Shifts return poison if shiftwidth is larger than the bitwidth.
7607static bool shiftAmountKnownInRange(const Value *ShiftAmount) {
7608 auto *C = dyn_cast<Constant>(ShiftAmount);
7609 if (!C)
7610 return false;
7611
7612 // Shifts return poison if shiftwidth is larger than the bitwidth.
7614 if (auto *FVTy = dyn_cast<FixedVectorType>(C->getType())) {
7615 unsigned NumElts = FVTy->getNumElements();
7616 for (unsigned i = 0; i < NumElts; ++i)
7617 ShiftAmounts.push_back(C->getAggregateElement(i));
7618 } else if (isa<ScalableVectorType>(C->getType()))
7619 return false; // Can't tell, just return false to be safe
7620 else
7621 ShiftAmounts.push_back(C);
7622
7623 bool Safe = llvm::all_of(ShiftAmounts, [](const Constant *C) {
7624 auto *CI = dyn_cast_or_null<ConstantInt>(C);
7625 return CI && CI->getValue().ult(C->getType()->getIntegerBitWidth());
7626 });
7627
7628 return Safe;
7629}
7630
7632 bool ConsiderFlagsAndMetadata) {
7633
7634 if (ConsiderFlagsAndMetadata && includesPoison(Kind) &&
7635 Op->hasPoisonGeneratingAnnotations())
7636 return true;
7637
7638 unsigned Opcode = Op->getOpcode();
7639
7640 // Check whether opcode is a poison/undef-generating operation
7641 switch (Opcode) {
7642 case Instruction::Shl:
7643 case Instruction::AShr:
7644 case Instruction::LShr:
7645 return includesPoison(Kind) && !shiftAmountKnownInRange(Op->getOperand(1));
7646 case Instruction::FPToSI:
7647 case Instruction::FPToUI:
7648 // fptosi/ui yields poison if the resulting value does not fit in the
7649 // destination type.
7650 return true;
7651 case Instruction::Call:
7652 if (auto *II = dyn_cast<IntrinsicInst>(Op)) {
7653 switch (II->getIntrinsicID()) {
7654 // NOTE: Use IntrNoCreateUndefOrPoison when possible.
7655 case Intrinsic::ctlz:
7656 case Intrinsic::cttz:
7657 case Intrinsic::abs:
7658 // We're not considering flags so it is safe to just return false.
7659 return false;
7660 case Intrinsic::sshl_sat:
7661 case Intrinsic::ushl_sat:
7662 if (!includesPoison(Kind) ||
7663 shiftAmountKnownInRange(II->getArgOperand(1)))
7664 return false;
7665 break;
7666 }
7667 }
7668 [[fallthrough]];
7669 case Instruction::CallBr:
7670 case Instruction::Invoke: {
7671 const auto *CB = cast<CallBase>(Op);
7672 return !CB->hasRetAttr(Attribute::NoUndef) &&
7673 !CB->hasFnAttr(Attribute::NoCreateUndefOrPoison);
7674 }
7675 case Instruction::InsertElement:
7676 case Instruction::ExtractElement: {
7677 // If index exceeds the length of the vector, it returns poison
7678 auto *VTy = cast<VectorType>(Op->getOperand(0)->getType());
7679 unsigned IdxOp = Op->getOpcode() == Instruction::InsertElement ? 2 : 1;
7680 auto *Idx = dyn_cast<ConstantInt>(Op->getOperand(IdxOp));
7681 if (includesPoison(Kind))
7682 return !Idx ||
7683 Idx->getValue().uge(VTy->getElementCount().getKnownMinValue());
7684 return false;
7685 }
7686 case Instruction::ShuffleVector: {
7688 ? cast<ConstantExpr>(Op)->getShuffleMask()
7689 : cast<ShuffleVectorInst>(Op)->getShuffleMask();
7690 return includesPoison(Kind) && is_contained(Mask, PoisonMaskElem);
7691 }
7692 case Instruction::FNeg:
7693 case Instruction::PHI:
7694 case Instruction::Select:
7695 case Instruction::ExtractValue:
7696 case Instruction::InsertValue:
7697 case Instruction::Freeze:
7698 case Instruction::ICmp:
7699 case Instruction::FCmp:
7700 case Instruction::GetElementPtr:
7701 return false;
7702 case Instruction::AddrSpaceCast:
7703 return true;
7704 default: {
7705 const auto *CE = dyn_cast<ConstantExpr>(Op);
7706 if (isa<CastInst>(Op) || (CE && CE->isCast()))
7707 return false;
7708 else if (Instruction::isBinaryOp(Opcode))
7709 return false;
7710 // Be conservative and return true.
7711 return true;
7712 }
7713 }
7714}
7715
7717 bool ConsiderFlagsAndMetadata) {
7718 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::UndefOrPoison,
7719 ConsiderFlagsAndMetadata);
7720}
7721
7722bool llvm::canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata) {
7723 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::PoisonOnly,
7724 ConsiderFlagsAndMetadata);
7725}
7726
7727static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V,
7728 unsigned Depth) {
7729 if (ValAssumedPoison == V)
7730 return true;
7731
7732 const unsigned MaxDepth = 2;
7733 if (Depth >= MaxDepth)
7734 return false;
7735
7736 if (const auto *I = dyn_cast<Instruction>(V)) {
7737 if (any_of(I->operands(), [=](const Use &Op) {
7738 return propagatesPoison(Op) &&
7739 directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1);
7740 }))
7741 return true;
7742
7743 // V = extractvalue V0, idx
7744 // V2 = extractvalue V0, idx2
7745 // V0's elements are all poison or not. (e.g., add_with_overflow)
7746 const WithOverflowInst *II;
7748 (match(ValAssumedPoison, m_ExtractValue(m_Specific(II))) ||
7749 llvm::is_contained(II->args(), ValAssumedPoison)))
7750 return true;
7751 }
7752 return false;
7753}
7754
7755static bool impliesPoison(const Value *ValAssumedPoison, const Value *V,
7756 unsigned Depth) {
7757 if (isGuaranteedNotToBePoison(ValAssumedPoison))
7758 return true;
7759
7760 if (directlyImpliesPoison(ValAssumedPoison, V, /* Depth */ 0))
7761 return true;
7762
7763 const unsigned MaxDepth = 2;
7764 if (Depth >= MaxDepth)
7765 return false;
7766
7767 const auto *I = dyn_cast<Instruction>(ValAssumedPoison);
7768 if (I && !canCreatePoison(cast<Operator>(I))) {
7769 return all_of(I->operands(), [=](const Value *Op) {
7770 return impliesPoison(Op, V, Depth + 1);
7771 });
7772 }
7773 return false;
7774}
7775
7776bool llvm::impliesPoison(const Value *ValAssumedPoison, const Value *V) {
7777 return ::impliesPoison(ValAssumedPoison, V, /* Depth */ 0);
7778}
7779
7780static bool programUndefinedIfUndefOrPoison(const Value *V, bool PoisonOnly);
7781
7783 const Value *V, AssumptionCache *AC, const Instruction *CtxI,
7784 const DominatorTree *DT, unsigned Depth, UndefPoisonKind Kind) {
7786 return false;
7787
7788 if (isa<MetadataAsValue>(V))
7789 return false;
7790
7791 if (const auto *A = dyn_cast<Argument>(V)) {
7792 if (A->hasAttribute(Attribute::NoUndef) ||
7793 A->hasAttribute(Attribute::Dereferenceable) ||
7794 A->hasAttribute(Attribute::DereferenceableOrNull))
7795 return true;
7796 }
7797
7798 if (auto *C = dyn_cast<Constant>(V)) {
7799 if (isa<PoisonValue>(C))
7800 return !includesPoison(Kind);
7801
7802 if (isa<UndefValue>(C))
7803 return !includesUndef(Kind);
7804
7807 return true;
7808
7809 if (C->getType()->isVectorTy()) {
7810 if (isa<ConstantExpr>(C)) {
7811 // Scalable vectors can use a ConstantExpr to build a splat.
7812 if (Constant *SplatC = C->getSplatValue())
7813 if (isa<ConstantInt>(SplatC) || isa<ConstantFP>(SplatC))
7814 return true;
7815 } else {
7816 if (includesUndef(Kind) && C->containsUndefElement())
7817 return false;
7818 if (includesPoison(Kind) && C->containsPoisonElement())
7819 return false;
7820 return !C->containsConstantExpression();
7821 }
7822 }
7823 }
7824
7825 // Strip cast operations from a pointer value.
7826 // Note that stripPointerCastsSameRepresentation can strip off getelementptr
7827 // inbounds with zero offset. To guarantee that the result isn't poison, the
7828 // stripped pointer is checked as it has to be pointing into an allocated
7829 // object or be null `null` to ensure `inbounds` getelement pointers with a
7830 // zero offset could not produce poison.
7831 // It can strip off addrspacecast that do not change bit representation as
7832 // well. We believe that such addrspacecast is equivalent to no-op.
7833 auto *StrippedV = V->stripPointerCastsSameRepresentation();
7834 if (isa<AllocaInst>(StrippedV) || isa<GlobalVariable>(StrippedV) ||
7835 isa<Function>(StrippedV) || isa<ConstantPointerNull>(StrippedV))
7836 return true;
7837
7838 auto OpCheck = [&](const Value *V) {
7839 return isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth + 1, Kind);
7840 };
7841
7842 if (auto *Opr = dyn_cast<Operator>(V)) {
7843 // If the value is a freeze instruction, then it can never
7844 // be undef or poison.
7845 if (isa<FreezeInst>(V))
7846 return true;
7847
7848 if (const auto *CB = dyn_cast<CallBase>(V)) {
7849 if (CB->hasRetAttr(Attribute::NoUndef) ||
7850 CB->hasRetAttr(Attribute::Dereferenceable) ||
7851 CB->hasRetAttr(Attribute::DereferenceableOrNull))
7852 return true;
7853 }
7854
7855 if (!::canCreateUndefOrPoison(Opr, Kind,
7856 /*ConsiderFlagsAndMetadata=*/true)) {
7857 if (const auto *PN = dyn_cast<PHINode>(V)) {
7858 unsigned Num = PN->getNumIncomingValues();
7859 bool IsWellDefined = true;
7860 for (unsigned i = 0; i < Num; ++i) {
7861 if (PN == PN->getIncomingValue(i))
7862 continue;
7863 auto *TI = PN->getIncomingBlock(i)->getTerminator();
7864 if (!isGuaranteedNotToBeUndefOrPoison(PN->getIncomingValue(i), AC, TI,
7865 DT, Depth + 1, Kind)) {
7866 IsWellDefined = false;
7867 break;
7868 }
7869 }
7870 if (IsWellDefined)
7871 return true;
7872 } else if (auto *Splat = isa<ShuffleVectorInst>(Opr) ? getSplatValue(Opr)
7873 : nullptr) {
7874 // For splats we only need to check the value being splatted.
7875 if (OpCheck(Splat))
7876 return true;
7877 } else if (all_of(Opr->operands(), OpCheck))
7878 return true;
7879 }
7880 }
7881
7882 if (auto *I = dyn_cast<LoadInst>(V))
7883 if (I->hasMetadata(LLVMContext::MD_noundef) ||
7884 I->hasMetadata(LLVMContext::MD_dereferenceable) ||
7885 I->hasMetadata(LLVMContext::MD_dereferenceable_or_null))
7886 return true;
7887
7889 return true;
7890
7891 // CxtI may be null or a cloned instruction.
7892 if (!CtxI || !CtxI->getParent() || !DT)
7893 return false;
7894
7895 auto *DNode = DT->getNode(CtxI->getParent());
7896 if (!DNode)
7897 // Unreachable block
7898 return false;
7899
7900 // If V is used as a branch condition before reaching CtxI, V cannot be
7901 // undef or poison.
7902 // br V, BB1, BB2
7903 // BB1:
7904 // CtxI ; V cannot be undef or poison here
7905 auto *Dominator = DNode->getIDom();
7906 // This check is purely for compile time reasons: we can skip the IDom walk
7907 // if what we are checking for includes undef and the value is not an integer.
7908 if (!includesUndef(Kind) || V->getType()->isIntegerTy())
7909 while (Dominator) {
7910 auto *TI = Dominator->getBlock()->getTerminatorOrNull();
7911
7912 Value *Cond = nullptr;
7913 if (auto BI = dyn_cast_or_null<CondBrInst>(TI)) {
7914 Cond = BI->getCondition();
7915 } else if (auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
7916 Cond = SI->getCondition();
7917 }
7918
7919 if (Cond) {
7920 if (Cond == V)
7921 return true;
7922 else if (!includesUndef(Kind) && isa<Operator>(Cond)) {
7923 // For poison, we can analyze further
7924 auto *Opr = cast<Operator>(Cond);
7925 if (any_of(Opr->operands(), [V](const Use &U) {
7926 return V == U && propagatesPoison(U);
7927 }))
7928 return true;
7929 }
7930 }
7931
7932 Dominator = Dominator->getIDom();
7933 }
7934
7935 if (AC && getKnowledgeValidInContext(V, {Attribute::NoUndef}, *AC, CtxI, DT))
7936 return true;
7937
7938 return false;
7939}
7940
7942 const Instruction *CtxI,
7943 const DominatorTree *DT,
7944 unsigned Depth) {
7945 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7947}
7948
7950 const Instruction *CtxI,
7951 const DominatorTree *DT, unsigned Depth) {
7952 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7954}
7955
7957 const Instruction *CtxI,
7958 const DominatorTree *DT, unsigned Depth) {
7959 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7961}
7962
7963/// Return true if undefined behavior would provably be executed on the path to
7964/// OnPathTo if Root produced a posion result. Note that this doesn't say
7965/// anything about whether OnPathTo is actually executed or whether Root is
7966/// actually poison. This can be used to assess whether a new use of Root can
7967/// be added at a location which is control equivalent with OnPathTo (such as
7968/// immediately before it) without introducing UB which didn't previously
7969/// exist. Note that a false result conveys no information.
7971 Instruction *OnPathTo,
7972 DominatorTree *DT) {
7973 // Basic approach is to assume Root is poison, propagate poison forward
7974 // through all users we can easily track, and then check whether any of those
7975 // users are provable UB and must execute before out exiting block might
7976 // exit.
7977
7978 // The set of all recursive users we've visited (which are assumed to all be
7979 // poison because of said visit)
7982 Worklist.push_back(Root);
7983 while (!Worklist.empty()) {
7984 const Instruction *I = Worklist.pop_back_val();
7985
7986 // If we know this must trigger UB on a path leading our target.
7987 if (mustTriggerUB(I, KnownPoison) && DT->dominates(I, OnPathTo))
7988 return true;
7989
7990 // If we can't analyze propagation through this instruction, just skip it
7991 // and transitive users. Safe as false is a conservative result.
7992 if (I != Root && !any_of(I->operands(), [&KnownPoison](const Use &U) {
7993 return KnownPoison.contains(U) && propagatesPoison(U);
7994 }))
7995 continue;
7996
7997 if (KnownPoison.insert(I).second)
7998 for (const User *User : I->users())
7999 Worklist.push_back(cast<Instruction>(User));
8000 }
8001
8002 // Might be non-UB, or might have a path we couldn't prove must execute on
8003 // way to exiting bb.
8004 return false;
8005}
8006
8008 const SimplifyQuery &SQ) {
8009 return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1),
8010 Add, SQ);
8011}
8012
8015 const WithCache<const Value *> &RHS,
8016 const SimplifyQuery &SQ) {
8017 return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, SQ);
8018}
8019
8021 // Note: An atomic operation isn't guaranteed to return in a reasonable amount
8022 // of time because it's possible for another thread to interfere with it for an
8023 // arbitrary length of time, but programs aren't allowed to rely on that.
8024
8025 // If there is no successor, then execution can't transfer to it.
8026 if (isa<ReturnInst>(I))
8027 return false;
8029 return false;
8030
8031 // Note: Do not add new checks here; instead, change Instruction::mayThrow or
8032 // Instruction::willReturn.
8033 //
8034 // FIXME: Move this check into Instruction::willReturn.
8035 if (isa<CatchPadInst>(I)) {
8036 switch (classifyEHPersonality(I->getFunction()->getPersonalityFn())) {
8037 default:
8038 // A catchpad may invoke exception object constructors and such, which
8039 // in some languages can be arbitrary code, so be conservative by default.
8040 return false;
8042 // For CoreCLR, it just involves a type test.
8043 return true;
8044 }
8045 }
8046
8047 // An instruction that returns without throwing must transfer control flow
8048 // to a successor.
8049 return !I->mayThrow() && I->willReturn();
8050}
8051
8053 // TODO: This is slightly conservative for invoke instruction since exiting
8054 // via an exception *is* normal control for them.
8055 for (const Instruction &I : *BB)
8057 return false;
8058 return true;
8059}
8060
8067
8070 assert(ScanLimit && "scan limit must be non-zero");
8071 for (const Instruction &I : Range) {
8072 if (--ScanLimit == 0)
8073 return false;
8075 return false;
8076 }
8077 return true;
8078}
8079
8081 const Loop *L) {
8082 // The loop header is guaranteed to be executed for every iteration.
8083 //
8084 // FIXME: Relax this constraint to cover all basic blocks that are
8085 // guaranteed to be executed at every iteration.
8086 if (I->getParent() != L->getHeader()) return false;
8087
8088 for (const Instruction &LI : *L->getHeader()) {
8089 if (&LI == I) return true;
8090 if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;
8091 }
8092 llvm_unreachable("Instruction not contained in its own parent basic block.");
8093}
8094
8096 switch (IID) {
8097 // TODO: Add more intrinsics.
8098 case Intrinsic::sadd_with_overflow:
8099 case Intrinsic::ssub_with_overflow:
8100 case Intrinsic::smul_with_overflow:
8101 case Intrinsic::uadd_with_overflow:
8102 case Intrinsic::usub_with_overflow:
8103 case Intrinsic::umul_with_overflow:
8104 // If an input is a vector containing a poison element, the
8105 // two output vectors (calculated results, overflow bits)'
8106 // corresponding lanes are poison.
8107 return true;
8108 case Intrinsic::ctpop:
8109 case Intrinsic::ctlz:
8110 case Intrinsic::cttz:
8111 case Intrinsic::abs:
8112 case Intrinsic::smax:
8113 case Intrinsic::smin:
8114 case Intrinsic::umax:
8115 case Intrinsic::umin:
8116 case Intrinsic::scmp:
8117 case Intrinsic::is_fpclass:
8118 case Intrinsic::ptrmask:
8119 case Intrinsic::ucmp:
8120 case Intrinsic::bitreverse:
8121 case Intrinsic::bswap:
8122 case Intrinsic::sadd_sat:
8123 case Intrinsic::ssub_sat:
8124 case Intrinsic::sshl_sat:
8125 case Intrinsic::uadd_sat:
8126 case Intrinsic::usub_sat:
8127 case Intrinsic::ushl_sat:
8128 case Intrinsic::smul_fix:
8129 case Intrinsic::smul_fix_sat:
8130 case Intrinsic::umul_fix:
8131 case Intrinsic::umul_fix_sat:
8132 case Intrinsic::pow:
8133 case Intrinsic::powi:
8134 case Intrinsic::sin:
8135 case Intrinsic::sinh:
8136 case Intrinsic::cos:
8137 case Intrinsic::cosh:
8138 case Intrinsic::sincos:
8139 case Intrinsic::sincospi:
8140 case Intrinsic::tan:
8141 case Intrinsic::tanh:
8142 case Intrinsic::asin:
8143 case Intrinsic::acos:
8144 case Intrinsic::atan:
8145 case Intrinsic::atan2:
8146 case Intrinsic::canonicalize:
8147 case Intrinsic::sqrt:
8148 case Intrinsic::exp:
8149 case Intrinsic::exp2:
8150 case Intrinsic::exp10:
8151 case Intrinsic::log:
8152 case Intrinsic::log2:
8153 case Intrinsic::log10:
8154 case Intrinsic::modf:
8155 case Intrinsic::floor:
8156 case Intrinsic::ceil:
8157 case Intrinsic::trunc:
8158 case Intrinsic::rint:
8159 case Intrinsic::nearbyint:
8160 case Intrinsic::round:
8161 case Intrinsic::roundeven:
8162 case Intrinsic::lrint:
8163 case Intrinsic::llrint:
8164 case Intrinsic::fshl:
8165 case Intrinsic::fshr:
8166 return true;
8167 default:
8168 return false;
8169 }
8170}
8171
8172bool llvm::propagatesPoison(const Use &PoisonOp) {
8173 const Operator *I = cast<Operator>(PoisonOp.getUser());
8174 switch (I->getOpcode()) {
8175 case Instruction::Freeze:
8176 case Instruction::PHI:
8177 case Instruction::Invoke:
8178 return false;
8179 case Instruction::Select:
8180 return PoisonOp.getOperandNo() == 0;
8181 case Instruction::Call:
8182 if (auto *II = dyn_cast<IntrinsicInst>(I))
8183 return intrinsicPropagatesPoison(II->getIntrinsicID());
8184 return false;
8185 case Instruction::ICmp:
8186 case Instruction::FCmp:
8187 case Instruction::GetElementPtr:
8188 return true;
8189 default:
8191 return true;
8192
8193 // Be conservative and return false.
8194 return false;
8195 }
8196}
8197
8198/// Enumerates all operands of \p I that are guaranteed to not be undef or
8199/// poison. If the callback \p Handle returns true, stop processing and return
8200/// true. Otherwise, return false.
8201template <typename CallableT>
8203 const CallableT &Handle) {
8204 switch (I->getOpcode()) {
8205 case Instruction::Store:
8206 if (Handle(cast<StoreInst>(I)->getPointerOperand()))
8207 return true;
8208 break;
8209
8210 case Instruction::Load:
8211 if (Handle(cast<LoadInst>(I)->getPointerOperand()))
8212 return true;
8213 break;
8214
8215 // Since dereferenceable attribute imply noundef, atomic operations
8216 // also implicitly have noundef pointers too
8217 case Instruction::AtomicCmpXchg:
8219 return true;
8220 break;
8221
8222 case Instruction::AtomicRMW:
8223 if (Handle(cast<AtomicRMWInst>(I)->getPointerOperand()))
8224 return true;
8225 break;
8226
8227 case Instruction::Call:
8228 case Instruction::Invoke: {
8229 const CallBase *CB = cast<CallBase>(I);
8230 if (CB->isIndirectCall() && Handle(CB->getCalledOperand()))
8231 return true;
8232 for (unsigned i = 0; i < CB->arg_size(); ++i)
8233 if ((CB->paramHasAttr(i, Attribute::NoUndef) ||
8234 CB->paramHasAttr(i, Attribute::Dereferenceable) ||
8235 CB->paramHasAttr(i, Attribute::DereferenceableOrNull)) &&
8236 Handle(CB->getArgOperand(i)))
8237 return true;
8238 break;
8239 }
8240 case Instruction::Ret:
8241 if (I->getFunction()->hasRetAttribute(Attribute::NoUndef) &&
8242 Handle(I->getOperand(0)))
8243 return true;
8244 break;
8245 case Instruction::Switch:
8246 if (Handle(cast<SwitchInst>(I)->getCondition()))
8247 return true;
8248 break;
8249 case Instruction::CondBr:
8250 if (Handle(cast<CondBrInst>(I)->getCondition()))
8251 return true;
8252 break;
8253 default:
8254 break;
8255 }
8256
8257 return false;
8258}
8259
8260/// Enumerates all operands of \p I that are guaranteed to not be poison.
8261template <typename CallableT>
8263 const CallableT &Handle) {
8264 if (handleGuaranteedWellDefinedOps(I, Handle))
8265 return true;
8266 switch (I->getOpcode()) {
8267 // Divisors of these operations are allowed to be partially undef.
8268 case Instruction::UDiv:
8269 case Instruction::SDiv:
8270 case Instruction::URem:
8271 case Instruction::SRem:
8272 return Handle(I->getOperand(1));
8273 default:
8274 return false;
8275 }
8276}
8277
8279 const SmallPtrSetImpl<const Value *> &KnownPoison) {
8281 I, [&](const Value *V) { return KnownPoison.count(V); });
8282}
8283
8285 bool PoisonOnly) {
8286 // We currently only look for uses of values within the same basic
8287 // block, as that makes it easier to guarantee that the uses will be
8288 // executed given that Inst is executed.
8289 //
8290 // FIXME: Expand this to consider uses beyond the same basic block. To do
8291 // this, look out for the distinction between post-dominance and strong
8292 // post-dominance.
8293 const BasicBlock *BB = nullptr;
8295 if (const auto *Inst = dyn_cast<Instruction>(V)) {
8296 BB = Inst->getParent();
8297 Begin = Inst->getIterator();
8298 Begin++;
8299 } else if (const auto *Arg = dyn_cast<Argument>(V)) {
8300 if (Arg->getParent()->isDeclaration())
8301 return false;
8302 BB = &Arg->getParent()->getEntryBlock();
8303 Begin = BB->begin();
8304 } else {
8305 return false;
8306 }
8307
8308 // Limit number of instructions we look at, to avoid scanning through large
8309 // blocks. The current limit is chosen arbitrarily.
8310 unsigned ScanLimit = 32;
8311 BasicBlock::const_iterator End = BB->end();
8312
8313 if (!PoisonOnly) {
8314 // Since undef does not propagate eagerly, be conservative & just check
8315 // whether a value is directly passed to an instruction that must take
8316 // well-defined operands.
8317
8318 for (const auto &I : make_range(Begin, End)) {
8319 if (--ScanLimit == 0)
8320 break;
8321
8322 if (handleGuaranteedWellDefinedOps(&I, [V](const Value *WellDefinedOp) {
8323 return WellDefinedOp == V;
8324 }))
8325 return true;
8326
8328 break;
8329 }
8330 return false;
8331 }
8332
8333 // Set of instructions that we have proved will yield poison if Inst
8334 // does.
8335 SmallPtrSet<const Value *, 16> YieldsPoison;
8337
8338 YieldsPoison.insert(V);
8339 Visited.insert(BB);
8340
8341 while (true) {
8342 for (const auto &I : make_range(Begin, End)) {
8343 if (--ScanLimit == 0)
8344 return false;
8345 if (mustTriggerUB(&I, YieldsPoison))
8346 return true;
8348 return false;
8349
8350 // If an operand is poison and propagates it, mark I as yielding poison.
8351 for (const Use &Op : I.operands()) {
8352 if (YieldsPoison.count(Op) && propagatesPoison(Op)) {
8353 YieldsPoison.insert(&I);
8354 break;
8355 }
8356 }
8357
8358 // Special handling for select, which returns poison if its operand 0 is
8359 // poison (handled in the loop above) *or* if both its true/false operands
8360 // are poison (handled here).
8361 if (I.getOpcode() == Instruction::Select &&
8362 YieldsPoison.count(I.getOperand(1)) &&
8363 YieldsPoison.count(I.getOperand(2))) {
8364 YieldsPoison.insert(&I);
8365 }
8366 }
8367
8368 BB = BB->getSingleSuccessor();
8369 if (!BB || !Visited.insert(BB).second)
8370 break;
8371
8372 Begin = BB->getFirstNonPHIIt();
8373 End = BB->end();
8374 }
8375 return false;
8376}
8377
8379 return ::programUndefinedIfUndefOrPoison(Inst, false);
8380}
8381
8383 return ::programUndefinedIfUndefOrPoison(Inst, true);
8384}
8385
8386static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) {
8387 if (FMF.noNaNs())
8388 return true;
8389
8390 if (auto *C = dyn_cast<ConstantFP>(V))
8391 return !C->isNaN();
8392
8393 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8394 if (!C->getElementType()->isFloatingPointTy())
8395 return false;
8396 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8397 if (C->getElementAsAPFloat(I).isNaN())
8398 return false;
8399 }
8400 return true;
8401 }
8402
8404 return true;
8405
8406 return false;
8407}
8408
8409static bool isKnownNonZero(const Value *V) {
8410 if (auto *C = dyn_cast<ConstantFP>(V))
8411 return !C->isZero();
8412
8413 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8414 if (!C->getElementType()->isFloatingPointTy())
8415 return false;
8416 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8417 if (C->getElementAsAPFloat(I).isZero())
8418 return false;
8419 }
8420 return true;
8421 }
8422
8423 return false;
8424}
8425
8426/// Match clamp pattern for float types without care about NaNs or signed zeros.
8427/// Given non-min/max outer cmp/select from the clamp pattern this
8428/// function recognizes if it can be substitued by a "canonical" min/max
8429/// pattern.
8431 Value *CmpLHS, Value *CmpRHS,
8432 Value *TrueVal, Value *FalseVal,
8433 Value *&LHS, Value *&RHS) {
8434 // Try to match
8435 // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2))
8436 // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2))
8437 // and return description of the outer Max/Min.
8438
8439 // First, check if select has inverse order:
8440 if (CmpRHS == FalseVal) {
8441 std::swap(TrueVal, FalseVal);
8442 Pred = CmpInst::getInversePredicate(Pred);
8443 }
8444
8445 // Assume success now. If there's no match, callers should not use these anyway.
8446 LHS = TrueVal;
8447 RHS = FalseVal;
8448
8449 const APFloat *FC1;
8450 if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite())
8451 return {SPF_UNKNOWN, SPNB_NA, false};
8452
8453 const APFloat *FC2;
8454 switch (Pred) {
8455 case CmpInst::FCMP_OLT:
8456 case CmpInst::FCMP_OLE:
8457 case CmpInst::FCMP_ULT:
8458 case CmpInst::FCMP_ULE:
8459 if (match(FalseVal, m_OrdOrUnordFMin(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8460 *FC1 < *FC2)
8461 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
8462 if (match(FalseVal, m_FMinNum(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8463 *FC1 < *FC2)
8464 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
8465 break;
8466 case CmpInst::FCMP_OGT:
8467 case CmpInst::FCMP_OGE:
8468 case CmpInst::FCMP_UGT:
8469 case CmpInst::FCMP_UGE:
8470 if (match(FalseVal, m_OrdOrUnordFMax(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8471 *FC1 > *FC2)
8472 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
8473 if (match(FalseVal, m_FMaxNum(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8474 *FC1 > *FC2)
8475 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
8476 break;
8477 default:
8478 break;
8479 }
8480
8481 return {SPF_UNKNOWN, SPNB_NA, false};
8482}
8483
8484/// Recognize variations of:
8485/// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
8487 Value *CmpLHS, Value *CmpRHS,
8488 Value *TrueVal, Value *FalseVal) {
8489 // Swap the select operands and predicate to match the patterns below.
8490 if (CmpRHS != TrueVal) {
8491 Pred = ICmpInst::getSwappedPredicate(Pred);
8492 std::swap(TrueVal, FalseVal);
8493 }
8494 const APInt *C1;
8495 if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) {
8496 const APInt *C2;
8497 // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
8498 if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8499 C1->slt(*C2) && Pred == CmpInst::ICMP_SLT)
8500 return {SPF_SMAX, SPNB_NA, false};
8501
8502 // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
8503 if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8504 C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT)
8505 return {SPF_SMIN, SPNB_NA, false};
8506
8507 // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
8508 if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8509 C1->ult(*C2) && Pred == CmpInst::ICMP_ULT)
8510 return {SPF_UMAX, SPNB_NA, false};
8511
8512 // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
8513 if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8514 C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT)
8515 return {SPF_UMIN, SPNB_NA, false};
8516 }
8517 return {SPF_UNKNOWN, SPNB_NA, false};
8518}
8519
8520/// Recognize variations of:
8521/// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c))
8523 Value *CmpLHS, Value *CmpRHS,
8524 Value *TVal, Value *FVal,
8525 unsigned Depth) {
8526 // TODO: Allow FP min/max with nnan/nsz.
8527 assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison");
8528
8529 Value *A = nullptr, *B = nullptr;
8530 SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1);
8531 if (!SelectPatternResult::isMinOrMax(L.Flavor))
8532 return {SPF_UNKNOWN, SPNB_NA, false};
8533
8534 Value *C = nullptr, *D = nullptr;
8535 SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1);
8536 if (L.Flavor != R.Flavor)
8537 return {SPF_UNKNOWN, SPNB_NA, false};
8538
8539 // We have something like: x Pred y ? min(a, b) : min(c, d).
8540 // Try to match the compare to the min/max operations of the select operands.
8541 // First, make sure we have the right compare predicate.
8542 switch (L.Flavor) {
8543 case SPF_SMIN:
8544 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) {
8545 Pred = ICmpInst::getSwappedPredicate(Pred);
8546 std::swap(CmpLHS, CmpRHS);
8547 }
8548 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
8549 break;
8550 return {SPF_UNKNOWN, SPNB_NA, false};
8551 case SPF_SMAX:
8552 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) {
8553 Pred = ICmpInst::getSwappedPredicate(Pred);
8554 std::swap(CmpLHS, CmpRHS);
8555 }
8556 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE)
8557 break;
8558 return {SPF_UNKNOWN, SPNB_NA, false};
8559 case SPF_UMIN:
8560 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
8561 Pred = ICmpInst::getSwappedPredicate(Pred);
8562 std::swap(CmpLHS, CmpRHS);
8563 }
8564 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE)
8565 break;
8566 return {SPF_UNKNOWN, SPNB_NA, false};
8567 case SPF_UMAX:
8568 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
8569 Pred = ICmpInst::getSwappedPredicate(Pred);
8570 std::swap(CmpLHS, CmpRHS);
8571 }
8572 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
8573 break;
8574 return {SPF_UNKNOWN, SPNB_NA, false};
8575 default:
8576 return {SPF_UNKNOWN, SPNB_NA, false};
8577 }
8578
8579 // If there is a common operand in the already matched min/max and the other
8580 // min/max operands match the compare operands (either directly or inverted),
8581 // then this is min/max of the same flavor.
8582
8583 // a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8584 // ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8585 if (D == B) {
8586 if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8587 match(A, m_Not(m_Specific(CmpRHS)))))
8588 return {L.Flavor, SPNB_NA, false};
8589 }
8590 // a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8591 // ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8592 if (C == B) {
8593 if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8594 match(A, m_Not(m_Specific(CmpRHS)))))
8595 return {L.Flavor, SPNB_NA, false};
8596 }
8597 // b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8598 // ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8599 if (D == A) {
8600 if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8601 match(B, m_Not(m_Specific(CmpRHS)))))
8602 return {L.Flavor, SPNB_NA, false};
8603 }
8604 // b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8605 // ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8606 if (C == A) {
8607 if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8608 match(B, m_Not(m_Specific(CmpRHS)))))
8609 return {L.Flavor, SPNB_NA, false};
8610 }
8611
8612 return {SPF_UNKNOWN, SPNB_NA, false};
8613}
8614
8615/// If the input value is the result of a 'not' op, constant integer, or vector
8616/// splat of a constant integer, return the bitwise-not source value.
8617/// TODO: This could be extended to handle non-splat vector integer constants.
8619 Value *NotV;
8620 if (match(V, m_Not(m_Value(NotV))))
8621 return NotV;
8622
8623 const APInt *C;
8624 if (match(V, m_APInt(C)))
8625 return ConstantInt::get(V->getType(), ~(*C));
8626
8627 return nullptr;
8628}
8629
8630/// Match non-obvious integer minimum and maximum sequences.
8632 Value *CmpLHS, Value *CmpRHS,
8633 Value *TrueVal, Value *FalseVal,
8634 Value *&LHS, Value *&RHS,
8635 unsigned Depth) {
8636 // Assume success. If there's no match, callers should not use these anyway.
8637 LHS = TrueVal;
8638 RHS = FalseVal;
8639
8640 SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);
8642 return SPR;
8643
8644 SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth);
8646 return SPR;
8647
8648 // Look through 'not' ops to find disguised min/max.
8649 // (X > Y) ? ~X : ~Y ==> (~X < ~Y) ? ~X : ~Y ==> MIN(~X, ~Y)
8650 // (X < Y) ? ~X : ~Y ==> (~X > ~Y) ? ~X : ~Y ==> MAX(~X, ~Y)
8651 if (CmpLHS == getNotValue(TrueVal) && CmpRHS == getNotValue(FalseVal)) {
8652 switch (Pred) {
8653 case CmpInst::ICMP_SGT: return {SPF_SMIN, SPNB_NA, false};
8654 case CmpInst::ICMP_SLT: return {SPF_SMAX, SPNB_NA, false};
8655 case CmpInst::ICMP_UGT: return {SPF_UMIN, SPNB_NA, false};
8656 case CmpInst::ICMP_ULT: return {SPF_UMAX, SPNB_NA, false};
8657 default: break;
8658 }
8659 }
8660
8661 // (X > Y) ? ~Y : ~X ==> (~X < ~Y) ? ~Y : ~X ==> MAX(~Y, ~X)
8662 // (X < Y) ? ~Y : ~X ==> (~X > ~Y) ? ~Y : ~X ==> MIN(~Y, ~X)
8663 if (CmpLHS == getNotValue(FalseVal) && CmpRHS == getNotValue(TrueVal)) {
8664 switch (Pred) {
8665 case CmpInst::ICMP_SGT: return {SPF_SMAX, SPNB_NA, false};
8666 case CmpInst::ICMP_SLT: return {SPF_SMIN, SPNB_NA, false};
8667 case CmpInst::ICMP_UGT: return {SPF_UMAX, SPNB_NA, false};
8668 case CmpInst::ICMP_ULT: return {SPF_UMIN, SPNB_NA, false};
8669 default: break;
8670 }
8671 }
8672
8673 if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT)
8674 return {SPF_UNKNOWN, SPNB_NA, false};
8675
8676 const APInt *C1;
8677 if (!match(CmpRHS, m_APInt(C1)))
8678 return {SPF_UNKNOWN, SPNB_NA, false};
8679
8680 // An unsigned min/max can be written with a signed compare.
8681 const APInt *C2;
8682 if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) ||
8683 (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) {
8684 // Is the sign bit set?
8685 // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX
8686 // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN
8687 if (Pred == CmpInst::ICMP_SLT && C1->isZero() && C2->isMaxSignedValue())
8688 return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8689
8690 // Is the sign bit clear?
8691 // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX
8692 // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN
8693 if (Pred == CmpInst::ICMP_SGT && C1->isAllOnes() && C2->isMinSignedValue())
8694 return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8695 }
8696
8697 return {SPF_UNKNOWN, SPNB_NA, false};
8698}
8699
8700bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW,
8701 bool AllowPoison) {
8702 assert(X && Y && "Invalid operand");
8703
8704 auto IsNegationOf = [&](const Value *X, const Value *Y) {
8705 if (!match(X, m_Neg(m_Specific(Y))))
8706 return false;
8707
8708 auto *BO = cast<BinaryOperator>(X);
8709 if (NeedNSW && !BO->hasNoSignedWrap())
8710 return false;
8711
8712 auto *Zero = cast<Constant>(BO->getOperand(0));
8713 if (!AllowPoison && !Zero->isNullValue())
8714 return false;
8715
8716 return true;
8717 };
8718
8719 // X = -Y or Y = -X
8720 if (IsNegationOf(X, Y) || IsNegationOf(Y, X))
8721 return true;
8722
8723 // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A)
8724 Value *A, *B;
8725 return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) &&
8726 match(Y, m_Sub(m_Specific(B), m_Specific(A))))) ||
8727 (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) &&
8729}
8730
8731bool llvm::isKnownInversion(const Value *X, const Value *Y) {
8732 // Handle X = icmp pred A, B, Y = icmp pred A, C.
8733 Value *A, *B, *C;
8734 CmpPredicate Pred1, Pred2;
8735 if (!match(X, m_ICmp(Pred1, m_Value(A), m_Value(B))) ||
8736 !match(Y, m_c_ICmp(Pred2, m_Specific(A), m_Value(C))))
8737 return false;
8738
8739 // They must both have samesign flag or not.
8740 if (Pred1.hasSameSign() != Pred2.hasSameSign())
8741 return false;
8742
8743 if (B == C)
8744 return Pred1 == ICmpInst::getInversePredicate(Pred2);
8745
8746 // Try to infer the relationship from constant ranges.
8747 const APInt *RHSC1, *RHSC2;
8748 if (!match(B, m_APInt(RHSC1)) || !match(C, m_APInt(RHSC2)))
8749 return false;
8750
8751 // Sign bits of two RHSCs should match.
8752 if (Pred1.hasSameSign() && RHSC1->isNonNegative() != RHSC2->isNonNegative())
8753 return false;
8754
8755 const auto CR1 = ConstantRange::makeExactICmpRegion(Pred1, *RHSC1);
8756 const auto CR2 = ConstantRange::makeExactICmpRegion(Pred2, *RHSC2);
8757
8758 return CR1.inverse() == CR2;
8759}
8760
8762 SelectPatternNaNBehavior NaNBehavior,
8763 bool Ordered) {
8764 switch (Pred) {
8765 default:
8766 return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.
8767 case ICmpInst::ICMP_UGT:
8768 case ICmpInst::ICMP_UGE:
8769 return {SPF_UMAX, SPNB_NA, false};
8770 case ICmpInst::ICMP_SGT:
8771 case ICmpInst::ICMP_SGE:
8772 return {SPF_SMAX, SPNB_NA, false};
8773 case ICmpInst::ICMP_ULT:
8774 case ICmpInst::ICMP_ULE:
8775 return {SPF_UMIN, SPNB_NA, false};
8776 case ICmpInst::ICMP_SLT:
8777 case ICmpInst::ICMP_SLE:
8778 return {SPF_SMIN, SPNB_NA, false};
8779 case FCmpInst::FCMP_UGT:
8780 case FCmpInst::FCMP_UGE:
8781 case FCmpInst::FCMP_OGT:
8782 case FCmpInst::FCMP_OGE:
8783 return {SPF_FMAXNUM, NaNBehavior, Ordered};
8784 case FCmpInst::FCMP_ULT:
8785 case FCmpInst::FCMP_ULE:
8786 case FCmpInst::FCMP_OLT:
8787 case FCmpInst::FCMP_OLE:
8788 return {SPF_FMINNUM, NaNBehavior, Ordered};
8789 }
8790}
8791
8792std::optional<std::pair<CmpPredicate, Constant *>>
8795 "Only for relational integer predicates.");
8796 if (isa<UndefValue>(C))
8797 return std::nullopt;
8798
8799 Type *Type = C->getType();
8800 bool IsSigned = ICmpInst::isSigned(Pred);
8801
8803 bool WillIncrement =
8804 UnsignedPred == ICmpInst::ICMP_ULE || UnsignedPred == ICmpInst::ICMP_UGT;
8805
8806 // Check if the constant operand can be safely incremented/decremented
8807 // without overflowing/underflowing.
8808 auto ConstantIsOk = [WillIncrement, IsSigned](ConstantInt *C) {
8809 return WillIncrement ? !C->isMaxValue(IsSigned) : !C->isMinValue(IsSigned);
8810 };
8811
8812 Constant *SafeReplacementConstant = nullptr;
8813 if (auto *CI = dyn_cast<ConstantInt>(C)) {
8814 // Bail out if the constant can't be safely incremented/decremented.
8815 if (!ConstantIsOk(CI))
8816 return std::nullopt;
8817 } else if (auto *FVTy = dyn_cast<FixedVectorType>(Type)) {
8818 unsigned NumElts = FVTy->getNumElements();
8819 for (unsigned i = 0; i != NumElts; ++i) {
8820 Constant *Elt = C->getAggregateElement(i);
8821 if (!Elt)
8822 return std::nullopt;
8823
8824 if (isa<UndefValue>(Elt))
8825 continue;
8826
8827 // Bail out if we can't determine if this constant is min/max or if we
8828 // know that this constant is min/max.
8829 auto *CI = dyn_cast<ConstantInt>(Elt);
8830 if (!CI || !ConstantIsOk(CI))
8831 return std::nullopt;
8832
8833 if (!SafeReplacementConstant)
8834 SafeReplacementConstant = CI;
8835 }
8836 } else if (isa<VectorType>(C->getType())) {
8837 // Handle scalable splat
8838 Value *SplatC = C->getSplatValue();
8839 auto *CI = dyn_cast_or_null<ConstantInt>(SplatC);
8840 // Bail out if the constant can't be safely incremented/decremented.
8841 if (!CI || !ConstantIsOk(CI))
8842 return std::nullopt;
8843 } else {
8844 // ConstantExpr?
8845 return std::nullopt;
8846 }
8847
8848 // It may not be safe to change a compare predicate in the presence of
8849 // undefined elements, so replace those elements with the first safe constant
8850 // that we found.
8851 // TODO: in case of poison, it is safe; let's replace undefs only.
8852 if (C->containsUndefOrPoisonElement()) {
8853 assert(SafeReplacementConstant && "Replacement constant not set");
8854 C = Constant::replaceUndefsWith(C, SafeReplacementConstant);
8855 }
8856
8858
8859 // Increment or decrement the constant.
8860 Constant *OneOrNegOne = ConstantInt::get(Type, WillIncrement ? 1 : -1, true);
8861 Constant *NewC = ConstantExpr::getAdd(C, OneOrNegOne);
8862
8863 return std::make_pair(NewPred, NewC);
8864}
8865
8867 FastMathFlags FMF,
8868 Value *CmpLHS, Value *CmpRHS,
8869 Value *TrueVal, Value *FalseVal,
8870 Value *&LHS, Value *&RHS,
8871 unsigned Depth) {
8872 bool HasMismatchedZeros = false;
8873 if (CmpInst::isFPPredicate(Pred)) {
8874 // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one
8875 // 0.0 operand, set the compare's 0.0 operands to that same value for the
8876 // purpose of identifying min/max. Disregard vector constants with undefined
8877 // elements because those can not be back-propagated for analysis.
8878 Value *OutputZeroVal = nullptr;
8879 if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) &&
8880 !cast<Constant>(TrueVal)->containsUndefOrPoisonElement())
8881 OutputZeroVal = TrueVal;
8882 else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) &&
8883 !cast<Constant>(FalseVal)->containsUndefOrPoisonElement())
8884 OutputZeroVal = FalseVal;
8885
8886 if (OutputZeroVal) {
8887 if (match(CmpLHS, m_AnyZeroFP()) && CmpLHS != OutputZeroVal) {
8888 HasMismatchedZeros = true;
8889 CmpLHS = OutputZeroVal;
8890 }
8891 if (match(CmpRHS, m_AnyZeroFP()) && CmpRHS != OutputZeroVal) {
8892 HasMismatchedZeros = true;
8893 CmpRHS = OutputZeroVal;
8894 }
8895 }
8896 }
8897
8898 LHS = CmpLHS;
8899 RHS = CmpRHS;
8900
8901 // Signed zero may return inconsistent results between implementations.
8902 // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0
8903 // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)
8904 // Therefore, we behave conservatively and only proceed if at least one of the
8905 // operands is known to not be zero or if we don't care about signed zero.
8906 switch (Pred) {
8907 default: break;
8910 if (!HasMismatchedZeros)
8911 break;
8912 [[fallthrough]];
8915 if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8916 !isKnownNonZero(CmpRHS))
8917 return {SPF_UNKNOWN, SPNB_NA, false};
8918 }
8919
8920 SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
8921 bool Ordered = false;
8922
8923 // When given one NaN and one non-NaN input:
8924 // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
8925 // - A simple C99 (a < b ? a : b) construction will return 'b' (as the
8926 // ordered comparison fails), which could be NaN or non-NaN.
8927 // so here we discover exactly what NaN behavior is required/accepted.
8928 if (CmpInst::isFPPredicate(Pred)) {
8929 bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
8930 bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
8931
8932 if (LHSSafe && RHSSafe) {
8933 // Both operands are known non-NaN.
8934 NaNBehavior = SPNB_RETURNS_ANY;
8935 Ordered = CmpInst::isOrdered(Pred);
8936 } else if (CmpInst::isOrdered(Pred)) {
8937 // An ordered comparison will return false when given a NaN, so it
8938 // returns the RHS.
8939 Ordered = true;
8940 if (LHSSafe)
8941 // LHS is non-NaN, so if RHS is NaN then NaN will be returned.
8942 NaNBehavior = SPNB_RETURNS_NAN;
8943 else if (RHSSafe)
8944 NaNBehavior = SPNB_RETURNS_OTHER;
8945 else
8946 // Completely unsafe.
8947 return {SPF_UNKNOWN, SPNB_NA, false};
8948 } else {
8949 Ordered = false;
8950 // An unordered comparison will return true when given a NaN, so it
8951 // returns the LHS.
8952 if (LHSSafe)
8953 // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
8954 NaNBehavior = SPNB_RETURNS_OTHER;
8955 else if (RHSSafe)
8956 NaNBehavior = SPNB_RETURNS_NAN;
8957 else
8958 // Completely unsafe.
8959 return {SPF_UNKNOWN, SPNB_NA, false};
8960 }
8961 }
8962
8963 if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
8964 std::swap(CmpLHS, CmpRHS);
8965 Pred = CmpInst::getSwappedPredicate(Pred);
8966 if (NaNBehavior == SPNB_RETURNS_NAN)
8967 NaNBehavior = SPNB_RETURNS_OTHER;
8968 else if (NaNBehavior == SPNB_RETURNS_OTHER)
8969 NaNBehavior = SPNB_RETURNS_NAN;
8970 Ordered = !Ordered;
8971 }
8972
8973 // ([if]cmp X, Y) ? X : Y
8974 if (TrueVal == CmpLHS && FalseVal == CmpRHS)
8975 return getSelectPattern(Pred, NaNBehavior, Ordered);
8976
8977 if (isKnownNegation(TrueVal, FalseVal)) {
8978 // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can
8979 // match against either LHS or sext(LHS).
8980 auto MaybeSExtCmpLHS =
8981 m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS)));
8982 auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes());
8983 auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One());
8984 if (match(TrueVal, MaybeSExtCmpLHS)) {
8985 // Set the return values. If the compare uses the negated value (-X >s 0),
8986 // swap the return values because the negated value is always 'RHS'.
8987 LHS = TrueVal;
8988 RHS = FalseVal;
8989 if (match(CmpLHS, m_Neg(m_Specific(FalseVal))))
8990 std::swap(LHS, RHS);
8991
8992 // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X)
8993 // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X)
8994 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8995 return {SPF_ABS, SPNB_NA, false};
8996
8997 // (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X)
8998 if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne))
8999 return {SPF_ABS, SPNB_NA, false};
9000
9001 // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X)
9002 // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X)
9003 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
9004 return {SPF_NABS, SPNB_NA, false};
9005 }
9006 else if (match(FalseVal, MaybeSExtCmpLHS)) {
9007 // Set the return values. If the compare uses the negated value (-X >s 0),
9008 // swap the return values because the negated value is always 'RHS'.
9009 LHS = FalseVal;
9010 RHS = TrueVal;
9011 if (match(CmpLHS, m_Neg(m_Specific(TrueVal))))
9012 std::swap(LHS, RHS);
9013
9014 // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X)
9015 // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X)
9016 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
9017 return {SPF_NABS, SPNB_NA, false};
9018
9019 // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X)
9020 // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X)
9021 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
9022 return {SPF_ABS, SPNB_NA, false};
9023 }
9024 }
9025
9026 if (CmpInst::isIntPredicate(Pred))
9027 return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth);
9028
9029 // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar
9030 // may return either -0.0 or 0.0, so fcmp/select pair has stricter
9031 // semantics than minNum. Be conservative in such case.
9032 if (NaNBehavior != SPNB_RETURNS_ANY ||
9033 (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
9034 !isKnownNonZero(CmpRHS)))
9035 return {SPF_UNKNOWN, SPNB_NA, false};
9036
9037 return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
9038}
9039
9041 Instruction::CastOps *CastOp) {
9042 const DataLayout &DL = CmpI->getDataLayout();
9043
9044 Constant *CastedTo = nullptr;
9045 switch (*CastOp) {
9046 case Instruction::ZExt:
9047 if (CmpI->isUnsigned())
9048 CastedTo = ConstantExpr::getTrunc(C, SrcTy);
9049 break;
9050 case Instruction::SExt:
9051 if (CmpI->isSigned())
9052 CastedTo = ConstantExpr::getTrunc(C, SrcTy, true);
9053 break;
9054 case Instruction::Trunc:
9055 Constant *CmpConst;
9056 if (match(CmpI->getOperand(1), m_Constant(CmpConst)) &&
9057 CmpConst->getType() == SrcTy) {
9058 // Here we have the following case:
9059 //
9060 // %cond = cmp iN %x, CmpConst
9061 // %tr = trunc iN %x to iK
9062 // %narrowsel = select i1 %cond, iK %t, iK C
9063 //
9064 // We can always move trunc after select operation:
9065 //
9066 // %cond = cmp iN %x, CmpConst
9067 // %widesel = select i1 %cond, iN %x, iN CmpConst
9068 // %tr = trunc iN %widesel to iK
9069 //
9070 // Note that C could be extended in any way because we don't care about
9071 // upper bits after truncation. It can't be abs pattern, because it would
9072 // look like:
9073 //
9074 // select i1 %cond, x, -x.
9075 //
9076 // So only min/max pattern could be matched. Such match requires widened C
9077 // == CmpConst. That is why set widened C = CmpConst, condition trunc
9078 // CmpConst == C is checked below.
9079 CastedTo = CmpConst;
9080 } else {
9081 unsigned ExtOp = CmpI->isSigned() ? Instruction::SExt : Instruction::ZExt;
9082 CastedTo = ConstantFoldCastOperand(ExtOp, C, SrcTy, DL);
9083 }
9084 break;
9085 case Instruction::FPTrunc:
9086 CastedTo = ConstantFoldCastOperand(Instruction::FPExt, C, SrcTy, DL);
9087 break;
9088 case Instruction::FPExt:
9089 CastedTo = ConstantFoldCastOperand(Instruction::FPTrunc, C, SrcTy, DL);
9090 break;
9091 case Instruction::FPToUI:
9092 CastedTo = ConstantFoldCastOperand(Instruction::UIToFP, C, SrcTy, DL);
9093 break;
9094 case Instruction::FPToSI:
9095 CastedTo = ConstantFoldCastOperand(Instruction::SIToFP, C, SrcTy, DL);
9096 break;
9097 case Instruction::UIToFP:
9098 CastedTo = ConstantFoldCastOperand(Instruction::FPToUI, C, SrcTy, DL);
9099 break;
9100 case Instruction::SIToFP:
9101 CastedTo = ConstantFoldCastOperand(Instruction::FPToSI, C, SrcTy, DL);
9102 break;
9103 default:
9104 break;
9105 }
9106
9107 if (!CastedTo)
9108 return nullptr;
9109
9110 // Make sure the cast doesn't lose any information.
9111 Constant *CastedBack =
9112 ConstantFoldCastOperand(*CastOp, CastedTo, C->getType(), DL);
9113 if (CastedBack && CastedBack != C)
9114 return nullptr;
9115
9116 return CastedTo;
9117}
9118
9119/// Helps to match a select pattern in case of a type mismatch.
9120///
9121/// The function processes the case when type of true and false values of a
9122/// select instruction differs from type of the cmp instruction operands because
9123/// of a cast instruction. The function checks if it is legal to move the cast
9124/// operation after "select". If yes, it returns the new second value of
9125/// "select" (with the assumption that cast is moved):
9126/// 1. As operand of cast instruction when both values of "select" are same cast
9127/// instructions.
9128/// 2. As restored constant (by applying reverse cast operation) when the first
9129/// value of the "select" is a cast operation and the second value is a
9130/// constant. It is implemented in lookThroughCastConst().
9131/// 3. As one operand is cast instruction and the other is not. The operands in
9132/// sel(cmp) are in different type integer.
9133/// NOTE: We return only the new second value because the first value could be
9134/// accessed as operand of cast instruction.
9135static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2,
9136 Instruction::CastOps *CastOp) {
9137 auto *Cast1 = dyn_cast<CastInst>(V1);
9138 if (!Cast1)
9139 return nullptr;
9140
9141 *CastOp = Cast1->getOpcode();
9142 Type *SrcTy = Cast1->getSrcTy();
9143 if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
9144 // If V1 and V2 are both the same cast from the same type, look through V1.
9145 if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
9146 return Cast2->getOperand(0);
9147 return nullptr;
9148 }
9149
9150 auto *C = dyn_cast<Constant>(V2);
9151 if (C)
9152 return lookThroughCastConst(CmpI, SrcTy, C, CastOp);
9153
9154 Value *CastedTo = nullptr;
9155 if (*CastOp == Instruction::Trunc) {
9156 if (match(CmpI->getOperand(1), m_ZExtOrSExt(m_Specific(V2)))) {
9157 // Here we have the following case:
9158 // %y_ext = sext iK %y to iN
9159 // %cond = cmp iN %x, %y_ext
9160 // %tr = trunc iN %x to iK
9161 // %narrowsel = select i1 %cond, iK %tr, iK %y
9162 //
9163 // We can always move trunc after select operation:
9164 // %y_ext = sext iK %y to iN
9165 // %cond = cmp iN %x, %y_ext
9166 // %widesel = select i1 %cond, iN %x, iN %y_ext
9167 // %tr = trunc iN %widesel to iK
9168 assert(V2->getType() == Cast1->getType() &&
9169 "V2 and Cast1 should be the same type.");
9170 CastedTo = CmpI->getOperand(1);
9171 }
9172 }
9173
9174 return CastedTo;
9175}
9177 Instruction::CastOps *CastOp,
9178 unsigned Depth) {
9180 return {SPF_UNKNOWN, SPNB_NA, false};
9181
9183 if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
9184
9185 CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
9186 if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};
9187
9188 Value *TrueVal = SI->getTrueValue();
9189 Value *FalseVal = SI->getFalseValue();
9190
9191 return llvm::matchDecomposedSelectPattern(CmpI, TrueVal, FalseVal, LHS, RHS,
9192 SI->getFastMathFlagsOrNone(),
9193 CastOp, Depth);
9194}
9195
9197 CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS,
9198 FastMathFlags FMF, Instruction::CastOps *CastOp, unsigned Depth) {
9199 CmpInst::Predicate Pred = CmpI->getPredicate();
9200 Value *CmpLHS = CmpI->getOperand(0);
9201 Value *CmpRHS = CmpI->getOperand(1);
9202 if (isa<FPMathOperator>(CmpI) && CmpI->hasNoNaNs())
9203 FMF.setNoNaNs();
9204
9205 // Bail out early.
9206 if (CmpI->isEquality())
9207 return {SPF_UNKNOWN, SPNB_NA, false};
9208
9209 // Deal with type mismatches.
9210 if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
9211 if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) {
9212 // If this is a potential fmin/fmax with a cast to integer, then ignore
9213 // -0.0 because there is no corresponding integer value.
9214 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9215 FMF.setNoSignedZeros();
9216 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9217 cast<CastInst>(TrueVal)->getOperand(0), C,
9218 LHS, RHS, Depth);
9219 }
9220 if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) {
9221 // If this is a potential fmin/fmax with a cast to integer, then ignore
9222 // -0.0 because there is no corresponding integer value.
9223 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9224 FMF.setNoSignedZeros();
9225 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9226 C, cast<CastInst>(FalseVal)->getOperand(0),
9227 LHS, RHS, Depth);
9228 }
9229 }
9230 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
9231 LHS, RHS, Depth);
9232}
9233
9235 if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT;
9236 if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT;
9237 if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT;
9238 if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT;
9239 if (SPF == SPF_FMINNUM)
9240 return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT;
9241 if (SPF == SPF_FMAXNUM)
9242 return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT;
9243 llvm_unreachable("unhandled!");
9244}
9245
9247 switch (SPF) {
9249 return Intrinsic::umin;
9251 return Intrinsic::umax;
9253 return Intrinsic::smin;
9255 return Intrinsic::smax;
9256 default:
9257 llvm_unreachable("Unexpected SPF");
9258 }
9259}
9260
9262 if (SPF == SPF_SMIN) return SPF_SMAX;
9263 if (SPF == SPF_UMIN) return SPF_UMAX;
9264 if (SPF == SPF_SMAX) return SPF_SMIN;
9265 if (SPF == SPF_UMAX) return SPF_UMIN;
9266 llvm_unreachable("unhandled!");
9267}
9268
9270 switch (MinMaxID) {
9271 case Intrinsic::smax: return Intrinsic::smin;
9272 case Intrinsic::smin: return Intrinsic::smax;
9273 case Intrinsic::umax: return Intrinsic::umin;
9274 case Intrinsic::umin: return Intrinsic::umax;
9275 // Please note that next four intrinsics may produce the same result for
9276 // original and inverted case even if X != Y due to NaN is handled specially.
9277 case Intrinsic::maximum: return Intrinsic::minimum;
9278 case Intrinsic::minimum: return Intrinsic::maximum;
9279 case Intrinsic::maxnum: return Intrinsic::minnum;
9280 case Intrinsic::minnum: return Intrinsic::maxnum;
9281 case Intrinsic::maximumnum:
9282 return Intrinsic::minimumnum;
9283 case Intrinsic::minimumnum:
9284 return Intrinsic::maximumnum;
9285 default: llvm_unreachable("Unexpected intrinsic");
9286 }
9287}
9288
9290 switch (SPF) {
9293 case SPF_UMAX: return APInt::getMaxValue(BitWidth);
9294 case SPF_UMIN: return APInt::getMinValue(BitWidth);
9295 default: llvm_unreachable("Unexpected flavor");
9296 }
9297}
9298
9299std::pair<Intrinsic::ID, bool>
9301 // Check if VL contains select instructions that can be folded into a min/max
9302 // vector intrinsic and return the intrinsic if it is possible.
9303 // TODO: Support floating point min/max.
9304 bool AllCmpSingleUse = true;
9305 SelectPatternResult SelectPattern;
9306 SelectPattern.Flavor = SPF_UNKNOWN;
9307 if (all_of(VL, [&SelectPattern, &AllCmpSingleUse](Value *I) {
9308 Value *LHS, *RHS;
9309 auto CurrentPattern = matchSelectPattern(I, LHS, RHS);
9310 if (!SelectPatternResult::isMinOrMax(CurrentPattern.Flavor))
9311 return false;
9312 if (SelectPattern.Flavor != SPF_UNKNOWN &&
9313 SelectPattern.Flavor != CurrentPattern.Flavor)
9314 return false;
9315 SelectPattern = CurrentPattern;
9316 AllCmpSingleUse &=
9318 return true;
9319 })) {
9320 switch (SelectPattern.Flavor) {
9321 case SPF_SMIN:
9322 return {Intrinsic::smin, AllCmpSingleUse};
9323 case SPF_UMIN:
9324 return {Intrinsic::umin, AllCmpSingleUse};
9325 case SPF_SMAX:
9326 return {Intrinsic::smax, AllCmpSingleUse};
9327 case SPF_UMAX:
9328 return {Intrinsic::umax, AllCmpSingleUse};
9329 case SPF_FMAXNUM:
9330 return {Intrinsic::maxnum, AllCmpSingleUse};
9331 case SPF_FMINNUM:
9332 return {Intrinsic::minnum, AllCmpSingleUse};
9333 default:
9334 llvm_unreachable("unexpected select pattern flavor");
9335 }
9336 }
9337 return {Intrinsic::not_intrinsic, false};
9338}
9339
9340template <typename InstTy>
9341static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst,
9342 Value *&Init, Value *&OtherOp) {
9343 // Handle the case of a simple two-predecessor recurrence PHI.
9344 // There's a lot more that could theoretically be done here, but
9345 // this is sufficient to catch some interesting cases.
9346 // TODO: Expand list -- gep, uadd.sat etc.
9347 if (PN->getNumIncomingValues() != 2)
9348 return false;
9349
9350 for (unsigned I = 0; I != 2; ++I) {
9351 if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
9352 Operation && Operation->getNumOperands() >= 2) {
9353 Value *LHS = Operation->getOperand(0);
9354 Value *RHS = Operation->getOperand(1);
9355 if (LHS != PN && RHS != PN)
9356 continue;
9357
9358 Inst = Operation;
9359 Init = PN->getIncomingValue(!I);
9360 OtherOp = (LHS == PN) ? RHS : LHS;
9361 return true;
9362 }
9363 }
9364 return false;
9365}
9366
9367template <typename InstTy>
9368static bool matchThreeInputRecurrence(const PHINode *PN, InstTy *&Inst,
9369 Value *&Init, Value *&OtherOp0,
9370 Value *&OtherOp1) {
9371 if (PN->getNumIncomingValues() != 2)
9372 return false;
9373
9374 for (unsigned I = 0; I != 2; ++I) {
9375 if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
9376 Operation && Operation->getNumOperands() >= 3) {
9377 Value *Op0 = Operation->getOperand(0);
9378 Value *Op1 = Operation->getOperand(1);
9379 Value *Op2 = Operation->getOperand(2);
9380
9381 if (Op0 != PN && Op1 != PN && Op2 != PN)
9382 continue;
9383
9384 Inst = Operation;
9385 Init = PN->getIncomingValue(!I);
9386 if (Op0 == PN) {
9387 OtherOp0 = Op1;
9388 OtherOp1 = Op2;
9389 } else if (Op1 == PN) {
9390 OtherOp0 = Op0;
9391 OtherOp1 = Op2;
9392 } else {
9393 OtherOp0 = Op0;
9394 OtherOp1 = Op1;
9395 }
9396 return true;
9397 }
9398 }
9399 return false;
9400}
9402 Value *&Start, Value *&Step) {
9403 // We try to match a recurrence of the form:
9404 // %iv = [Start, %entry], [%iv.next, %backedge]
9405 // %iv.next = binop %iv, Step
9406 // Or:
9407 // %iv = [Start, %entry], [%iv.next, %backedge]
9408 // %iv.next = binop Step, %iv
9409 return matchTwoInputRecurrence(P, BO, Start, Step);
9410}
9411
9413 Value *&Start, Value *&Step) {
9414 BinaryOperator *BO = nullptr;
9415 P = dyn_cast<PHINode>(I->getOperand(0));
9416 if (!P)
9417 P = dyn_cast<PHINode>(I->getOperand(1));
9418 return P && matchSimpleRecurrence(P, BO, Start, Step) && BO == I;
9419}
9420
9422 PHINode *&P, Value *&Init,
9423 Value *&OtherOp) {
9424 // Binary intrinsics only supported for now.
9425 if (I->arg_size() != 2 || I->getType() != I->getArgOperand(0)->getType() ||
9426 I->getType() != I->getArgOperand(1)->getType())
9427 return false;
9428
9429 IntrinsicInst *II = nullptr;
9430 P = dyn_cast<PHINode>(I->getArgOperand(0));
9431 if (!P)
9432 P = dyn_cast<PHINode>(I->getArgOperand(1));
9433
9434 return P && matchTwoInputRecurrence(P, II, Init, OtherOp) && II == I;
9435}
9436
9438 PHINode *&P, Value *&Init,
9439 Value *&OtherOp0,
9440 Value *&OtherOp1) {
9441 if (I->arg_size() != 3 || I->getType() != I->getArgOperand(0)->getType() ||
9442 I->getType() != I->getArgOperand(1)->getType() ||
9443 I->getType() != I->getArgOperand(2)->getType())
9444 return false;
9445 IntrinsicInst *II = nullptr;
9446 P = dyn_cast<PHINode>(I->getArgOperand(0));
9447 if (!P) {
9448 P = dyn_cast<PHINode>(I->getArgOperand(1));
9449 if (!P)
9450 P = dyn_cast<PHINode>(I->getArgOperand(2));
9451 }
9452 return P && matchThreeInputRecurrence(P, II, Init, OtherOp0, OtherOp1) &&
9453 II == I;
9454}
9455
9456/// Return true if "icmp Pred LHS RHS" is always true.
9458 const Value *RHS) {
9459 if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS)
9460 return true;
9461
9462 switch (Pred) {
9463 default:
9464 return false;
9465
9466 case CmpInst::ICMP_SLE: {
9467 const APInt *C;
9468
9469 // LHS s<= LHS +_{nsw} C if C >= 0
9470 // LHS s<= LHS | C if C >= 0
9471 if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C))) ||
9473 return !C->isNegative();
9474
9475 // LHS s<= smax(LHS, V) for any V
9477 return true;
9478
9479 // smin(RHS, V) s<= RHS for any V
9481 return true;
9482
9483 // Match A to (X +_{nsw} CA) and B to (X +_{nsw} CB)
9484 const Value *X;
9485 const APInt *CLHS, *CRHS;
9486 if (match(LHS, m_NSWAddLike(m_Value(X), m_APInt(CLHS))) &&
9488 return CLHS->sle(*CRHS);
9489
9490 return false;
9491 }
9492
9493 case CmpInst::ICMP_ULE: {
9494 // LHS u<= LHS +_{nuw} V for any V
9495 if (match(RHS, m_c_Add(m_Specific(LHS), m_Value())) &&
9497 return true;
9498
9499 // LHS u<= LHS | V for any V
9500 if (match(RHS, m_c_Or(m_Specific(LHS), m_Value())))
9501 return true;
9502
9503 // LHS u<= umax(LHS, V) for any V
9505 return true;
9506
9507 // RHS >> V u<= RHS for any V
9508 if (match(LHS, m_LShr(m_Specific(RHS), m_Value())))
9509 return true;
9510
9511 // RHS u/ C_ugt_1 u<= RHS
9512 const APInt *C;
9513 if (match(LHS, m_UDiv(m_Specific(RHS), m_APInt(C))) && C->ugt(1))
9514 return true;
9515
9516 // RHS & V u<= RHS for any V
9518 return true;
9519
9520 // umin(RHS, V) u<= RHS for any V
9522 return true;
9523
9524 // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
9525 const Value *X;
9526 const APInt *CLHS, *CRHS;
9527 if (match(LHS, m_NUWAddLike(m_Value(X), m_APInt(CLHS))) &&
9529 return CLHS->ule(*CRHS);
9530
9531 return false;
9532 }
9533 }
9534}
9535
9536/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
9537/// ALHS ARHS" is true. Otherwise, return std::nullopt.
9538static std::optional<bool>
9540 const Value *ARHS, const Value *BLHS, const Value *BRHS) {
9541 switch (Pred) {
9542 default:
9543 return std::nullopt;
9544
9545 case CmpInst::ICMP_SLT:
9546 case CmpInst::ICMP_SLE:
9547 if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS) &&
9549 return true;
9550 return std::nullopt;
9551
9552 case CmpInst::ICMP_SGT:
9553 case CmpInst::ICMP_SGE:
9554 if (isTruePredicate(CmpInst::ICMP_SLE, ALHS, BLHS) &&
9556 return true;
9557 return std::nullopt;
9558
9559 case CmpInst::ICMP_ULT:
9560 case CmpInst::ICMP_ULE:
9561 if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS) &&
9563 return true;
9564 return std::nullopt;
9565
9566 case CmpInst::ICMP_UGT:
9567 case CmpInst::ICMP_UGE:
9568 if (isTruePredicate(CmpInst::ICMP_ULE, ALHS, BLHS) &&
9570 return true;
9571 return std::nullopt;
9572 }
9573}
9574
9575/// Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
9576/// Return false if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is false.
9577/// Otherwise, return std::nullopt if we can't infer anything.
9578static std::optional<bool>
9580 CmpPredicate RPred, const ConstantRange &RCR) {
9581 auto CRImpliesPred = [&](ConstantRange CR,
9582 CmpInst::Predicate Pred) -> std::optional<bool> {
9583 // If all true values for lhs and true for rhs, lhs implies rhs
9584 if (CR.icmp(Pred, RCR))
9585 return true;
9586
9587 // If there is no overlap, lhs implies not rhs
9588 if (CR.icmp(CmpInst::getInversePredicate(Pred), RCR))
9589 return false;
9590
9591 return std::nullopt;
9592 };
9593 if (auto Res = CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9594 RPred))
9595 return Res;
9596 if (LPred.hasSameSign() ^ RPred.hasSameSign()) {
9598 : LPred.dropSameSign();
9600 : RPred.dropSameSign();
9601 return CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9602 RPred);
9603 }
9604 return std::nullopt;
9605}
9606
9607/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
9608/// is true. Return false if LHS implies RHS is false. Otherwise, return
9609/// std::nullopt if we can't infer anything.
9610static std::optional<bool>
9611isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1,
9612 CmpPredicate RPred, const Value *R0, const Value *R1,
9613 const DataLayout &DL, bool LHSIsTrue) {
9614 // The rest of the logic assumes the LHS condition is true. If that's not the
9615 // case, invert the predicate to make it so.
9616 if (!LHSIsTrue)
9617 LPred = ICmpInst::getInverseCmpPredicate(LPred);
9618
9619 // We can have non-canonical operands, so try to normalize any common operand
9620 // to L0/R0.
9621 if (L0 == R1) {
9622 std::swap(R0, R1);
9623 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9624 }
9625 if (R0 == L1) {
9626 std::swap(L0, L1);
9627 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9628 }
9629 if (L1 == R1) {
9630 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
9631 if (L0 != R0 || match(L0, m_ImmConstant())) {
9632 std::swap(L0, L1);
9633 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9634 std::swap(R0, R1);
9635 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9636 }
9637 }
9638
9639 // See if we can infer anything if operand-0 matches and we have at least one
9640 // constant.
9641 const APInt *Unused;
9642 if (L0 == R0 && (match(L1, m_APInt(Unused)) || match(R1, m_APInt(Unused)))) {
9643 // Potential TODO: We could also further use the constant range of L0/R0 to
9644 // further constraint the constant ranges. At the moment this leads to
9645 // several regressions related to not transforming `multi_use(A + C0) eq/ne
9646 // C1` (see discussion: D58633).
9647 SimplifyQuery SQ(DL);
9652
9653 // Even if L1/R1 are not both constant, we can still sometimes deduce
9654 // relationship from a single constant. For example X u> Y implies X != 0.
9655 if (auto R = isImpliedCondCommonOperandWithCR(LPred, LCR, RPred, RCR))
9656 return R;
9657 // If both L1/R1 were exact constant ranges and we didn't get anything
9658 // here, we won't be able to deduce this.
9659 if (match(L1, m_APInt(Unused)) && match(R1, m_APInt(Unused)))
9660 return std::nullopt;
9661 }
9662
9663 // Can we infer anything when the two compares have matching operands?
9664 if (L0 == R0 && L1 == R1)
9665 return ICmpInst::isImpliedByMatchingCmp(LPred, RPred);
9666
9667 // It only really makes sense in the context of signed comparison for "X - Y
9668 // must be positive if X >= Y and no overflow".
9669 // Take SGT as an example: L0:x > L1:y and C >= 0
9670 // ==> R0:(x -nsw y) < R1:(-C) is false
9671 CmpInst::Predicate SignedLPred = LPred.getPreferredSignedPredicate();
9672 if ((SignedLPred == ICmpInst::ICMP_SGT ||
9673 SignedLPred == ICmpInst::ICMP_SGE) &&
9674 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9675 if (match(R1, m_NonPositive()) &&
9676 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == false)
9677 return false;
9678 }
9679
9680 // Take SLT as an example: L0:x < L1:y and C <= 0
9681 // ==> R0:(x -nsw y) < R1:(-C) is true
9682 if ((SignedLPred == ICmpInst::ICMP_SLT ||
9683 SignedLPred == ICmpInst::ICMP_SLE) &&
9684 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9685 if (match(R1, m_NonNegative()) &&
9686 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == true)
9687 return true;
9688 }
9689
9690 // a - b == NonZero -> a != b
9691 // ptrtoint(a) - ptrtoint(b) == NonZero -> a != b
9692 const APInt *L1C;
9693 Value *A, *B;
9694 if (LPred == ICmpInst::ICMP_EQ && ICmpInst::isEquality(RPred) &&
9695 match(L1, m_APInt(L1C)) && !L1C->isZero() &&
9696 match(L0, m_Sub(m_Value(A), m_Value(B))) &&
9697 ((A == R0 && B == R1) || (A == R1 && B == R0) ||
9702 return RPred.dropSameSign() == ICmpInst::ICMP_NE;
9703 }
9704
9705 // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1
9706 if (L0 == R0 &&
9707 (LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) &&
9708 (RPred == ICmpInst::ICMP_ULT || RPred == ICmpInst::ICMP_UGE) &&
9709 match(L0, m_c_Add(m_Specific(L1), m_Specific(R1))))
9710 return CmpPredicate::getMatching(LPred, RPred).has_value();
9711
9712 if (auto P = CmpPredicate::getMatching(LPred, RPred))
9713 return isImpliedCondOperands(*P, L0, L1, R0, R1);
9714
9715 return std::nullopt;
9716}
9717
9718/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
9719/// is true. Return false if LHS implies RHS is false. Otherwise, return
9720/// std::nullopt if we can't infer anything.
9721static std::optional<bool>
9723 FCmpInst::Predicate RPred, const Value *R0, const Value *R1,
9724 const DataLayout &DL, bool LHSIsTrue) {
9725 // The rest of the logic assumes the LHS condition is true. If that's not the
9726 // case, invert the predicate to make it so.
9727 if (!LHSIsTrue)
9728 LPred = FCmpInst::getInversePredicate(LPred);
9729
9730 // We can have non-canonical operands, so try to normalize any common operand
9731 // to L0/R0.
9732 if (L0 == R1) {
9733 std::swap(R0, R1);
9734 RPred = FCmpInst::getSwappedPredicate(RPred);
9735 }
9736 if (R0 == L1) {
9737 std::swap(L0, L1);
9738 LPred = FCmpInst::getSwappedPredicate(LPred);
9739 }
9740 if (L1 == R1) {
9741 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
9742 if (L0 != R0 || match(L0, m_ImmConstant())) {
9743 std::swap(L0, L1);
9744 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9745 std::swap(R0, R1);
9746 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9747 }
9748 }
9749
9750 // Can we infer anything when the two compares have matching operands?
9751 if (L0 == R0 && L1 == R1) {
9752 if ((LPred & RPred) == LPred)
9753 return true;
9754 if ((LPred & ~RPred) == LPred)
9755 return false;
9756 }
9757
9758 // See if we can infer anything if operand-0 matches and we have at least one
9759 // constant.
9760 const APFloat *L1C, *R1C;
9761 if (L0 == R0 && match(L1, m_APFloat(L1C)) && match(R1, m_APFloat(R1C))) {
9762 if (std::optional<ConstantFPRange> DomCR =
9764 if (std::optional<ConstantFPRange> ImpliedCR =
9766 if (ImpliedCR->contains(*DomCR))
9767 return true;
9768 }
9769 if (std::optional<ConstantFPRange> ImpliedCR =
9771 FCmpInst::getInversePredicate(RPred), *R1C)) {
9772 if (ImpliedCR->contains(*DomCR))
9773 return false;
9774 }
9775 }
9776 }
9777
9778 return std::nullopt;
9779}
9780
9781/// Return true if LHS implies RHS is true. Return false if LHS implies RHS is
9782/// false. Otherwise, return std::nullopt if we can't infer anything. We
9783/// expect the RHS to be an icmp and the LHS to be an 'and', 'or', or a 'select'
9784/// instruction.
9785static std::optional<bool>
9787 const Value *RHSOp0, const Value *RHSOp1,
9788 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9789 // The LHS must be an 'or', 'and', or a 'select' instruction.
9790 assert((LHS->getOpcode() == Instruction::And ||
9791 LHS->getOpcode() == Instruction::Or ||
9792 LHS->getOpcode() == Instruction::Select) &&
9793 "Expected LHS to be 'and', 'or', or 'select'.");
9794
9795 assert(Depth <= MaxAnalysisRecursionDepth && "Hit recursion limit");
9796
9797 // If the result of an 'or' is false, then we know both legs of the 'or' are
9798 // false. Similarly, if the result of an 'and' is true, then we know both
9799 // legs of the 'and' are true.
9800 const Value *ALHS, *ARHS;
9801 if ((!LHSIsTrue && match(LHS, m_LogicalOr(m_Value(ALHS), m_Value(ARHS)))) ||
9802 (LHSIsTrue && match(LHS, m_LogicalAnd(m_Value(ALHS), m_Value(ARHS))))) {
9803 // FIXME: Make this non-recursion.
9804 if (std::optional<bool> Implication = isImpliedCondition(
9805 ALHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9806 return Implication;
9807 if (std::optional<bool> Implication = isImpliedCondition(
9808 ARHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9809 return Implication;
9810 return std::nullopt;
9811 }
9812 return std::nullopt;
9813}
9814
9815std::optional<bool>
9817 const Value *RHSOp0, const Value *RHSOp1,
9818 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9819 // Bail out when we hit the limit.
9821 return std::nullopt;
9822
9823 // A mismatch occurs when we compare a scalar cmp to a vector cmp, for
9824 // example.
9825 if (RHSOp0->getType()->isVectorTy() != LHS->getType()->isVectorTy())
9826 return std::nullopt;
9827
9828 assert(LHS->getType()->isIntOrIntVectorTy(1) &&
9829 "Expected integer type only!");
9830
9831 // Match not
9832 if (match(LHS, m_Not(m_Value(LHS))))
9833 LHSIsTrue = !LHSIsTrue;
9834
9835 // Both LHS and RHS are icmps.
9836 if (RHSOp0->getType()->getScalarType()->isIntOrPtrTy()) {
9837 CmpPredicate LHSPred;
9838 Value *LHSOp0, *LHSOp1;
9839 if (match(LHS, m_ICmpLike(LHSPred, m_Value(LHSOp0), m_Value(LHSOp1))))
9840 return isImpliedCondICmps(LHSPred, LHSOp0, LHSOp1, RHSPred, RHSOp0,
9841 RHSOp1, DL, LHSIsTrue);
9842 } else {
9843 assert(RHSOp0->getType()->isFPOrFPVectorTy() &&
9844 "Expected floating point type only!");
9845 if (const auto *LHSCmp = dyn_cast<FCmpInst>(LHS))
9846 return isImpliedCondFCmps(LHSCmp->getPredicate(), LHSCmp->getOperand(0),
9847 LHSCmp->getOperand(1), RHSPred, RHSOp0, RHSOp1,
9848 DL, LHSIsTrue);
9849 }
9850
9851 /// The LHS should be an 'or', 'and', or a 'select' instruction. We expect
9852 /// the RHS to be an icmp.
9853 /// FIXME: Add support for and/or/select on the RHS.
9854 if (const Instruction *LHSI = dyn_cast<Instruction>(LHS)) {
9855 if ((LHSI->getOpcode() == Instruction::And ||
9856 LHSI->getOpcode() == Instruction::Or ||
9857 LHSI->getOpcode() == Instruction::Select))
9858 return isImpliedCondAndOr(LHSI, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue,
9859 Depth);
9860 }
9861 return std::nullopt;
9862}
9863
9864std::optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS,
9865 const DataLayout &DL,
9866 bool LHSIsTrue, unsigned Depth) {
9867 // LHS ==> RHS by definition
9868 if (LHS == RHS)
9869 return LHSIsTrue;
9870
9871 // Match not
9872 bool InvertRHS = false;
9873 if (match(RHS, m_Not(m_Value(RHS)))) {
9874 if (LHS == RHS)
9875 return !LHSIsTrue;
9876 InvertRHS = true;
9877 }
9878
9879 CmpPredicate RHSPred;
9880 Value *RHSOp0, *RHSOp1;
9881 if (match(RHS, m_ICmpLike(RHSPred, m_Value(RHSOp0), m_Value(RHSOp1)))) {
9882 if (auto Implied = isImpliedCondition(LHS, RHSPred, RHSOp0, RHSOp1, DL,
9883 LHSIsTrue, Depth))
9884 return InvertRHS ? !*Implied : *Implied;
9885 return std::nullopt;
9886 }
9887 if (const FCmpInst *RHSCmp = dyn_cast<FCmpInst>(RHS)) {
9888 if (auto Implied = isImpliedCondition(
9889 LHS, RHSCmp->getPredicate(), RHSCmp->getOperand(0),
9890 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))
9891 return InvertRHS ? !*Implied : *Implied;
9892 return std::nullopt;
9893 }
9894
9896 return std::nullopt;
9897
9898 // LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS2
9899 // LHS ==> !(RHS1 && RHS2) if LHS ==> !RHS1 or LHS ==> !RHS2
9900 const Value *RHS1, *RHS2;
9901 if (match(RHS, m_LogicalOr(m_Value(RHS1), m_Value(RHS2)))) {
9902 if (std::optional<bool> Imp =
9903 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9904 if (*Imp == true)
9905 return !InvertRHS;
9906 if (std::optional<bool> Imp =
9907 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9908 if (*Imp == true)
9909 return !InvertRHS;
9910 }
9911 if (match(RHS, m_LogicalAnd(m_Value(RHS1), m_Value(RHS2)))) {
9912 if (std::optional<bool> Imp =
9913 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9914 if (*Imp == false)
9915 return InvertRHS;
9916 if (std::optional<bool> Imp =
9917 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9918 if (*Imp == false)
9919 return InvertRHS;
9920 }
9921
9922 return std::nullopt;
9923}
9924
9925// Returns a pair (Condition, ConditionIsTrue), where Condition is a branch
9926// condition dominating ContextI or nullptr, if no condition is found.
9927static std::pair<Value *, bool>
9929 if (!ContextI || !ContextI->getParent())
9930 return {nullptr, false};
9931
9932 // TODO: This is a poor/cheap way to determine dominance. Should we use a
9933 // dominator tree (eg, from a SimplifyQuery) instead?
9934 const BasicBlock *ContextBB = ContextI->getParent();
9935 const BasicBlock *PredBB = ContextBB->getSinglePredecessor();
9936 if (!PredBB)
9937 return {nullptr, false};
9938
9939 // We need a conditional branch in the predecessor.
9940 Value *PredCond;
9941 BasicBlock *TrueBB, *FalseBB;
9942 if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))
9943 return {nullptr, false};
9944
9945 // The branch should get simplified. Don't bother simplifying this condition.
9946 if (TrueBB == FalseBB)
9947 return {nullptr, false};
9948
9949 assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
9950 "Predecessor block does not point to successor?");
9951
9952 // Is this condition implied by the predecessor condition?
9953 return {PredCond, TrueBB == ContextBB};
9954}
9955
9956std::optional<bool> llvm::isImpliedByDomCondition(const Value *Cond,
9957 const Instruction *ContextI,
9958 const DataLayout &DL) {
9959 assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool");
9960 auto PredCond = getDomPredecessorCondition(ContextI);
9961 if (PredCond.first)
9962 return isImpliedCondition(PredCond.first, Cond, DL, PredCond.second);
9963 return std::nullopt;
9964}
9965
9967 const Value *LHS,
9968 const Value *RHS,
9969 const Instruction *ContextI,
9970 const DataLayout &DL) {
9971 auto PredCond = getDomPredecessorCondition(ContextI);
9972 if (PredCond.first)
9973 return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL,
9974 PredCond.second);
9975 return std::nullopt;
9976}
9977
9979 APInt &Upper, const InstrInfoQuery &IIQ,
9980 bool PreferSignedRange) {
9981 unsigned Width = Lower.getBitWidth();
9982 const APInt *C;
9983 switch (BO.getOpcode()) {
9984 case Instruction::Sub:
9985 if (match(BO.getOperand(0), m_APInt(C))) {
9986 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
9987 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
9988
9989 // If the caller expects a signed compare, then try to use a signed range.
9990 // Otherwise if both no-wraps are set, use the unsigned range because it
9991 // is never larger than the signed range. Example:
9992 // "sub nuw nsw i8 -2, x" is unsigned [0, 254] vs. signed [-128, 126].
9993 // "sub nuw nsw i8 2, x" is unsigned [0, 2] vs. signed [-125, 127].
9994 if (PreferSignedRange && HasNSW && HasNUW)
9995 HasNUW = false;
9996
9997 if (HasNUW) {
9998 // 'sub nuw c, x' produces [0, C].
9999 Upper = *C + 1;
10000 } else if (HasNSW) {
10001 if (C->isNegative()) {
10002 // 'sub nsw -C, x' produces [SINT_MIN, -C - SINT_MIN].
10004 Upper = *C - APInt::getSignedMaxValue(Width);
10005 } else {
10006 // Note that sub 0, INT_MIN is not NSW. It techically is a signed wrap
10007 // 'sub nsw C, x' produces [C - SINT_MAX, SINT_MAX].
10008 Lower = *C - APInt::getSignedMaxValue(Width);
10010 }
10011 }
10012 }
10013 break;
10014 case Instruction::Add:
10015 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
10016 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
10017 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
10018
10019 // If the caller expects a signed compare, then try to use a signed
10020 // range. Otherwise if both no-wraps are set, use the unsigned range
10021 // because it is never larger than the signed range. Example: "add nuw
10022 // nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125].
10023 if (PreferSignedRange && HasNSW && HasNUW)
10024 HasNUW = false;
10025
10026 if (HasNUW) {
10027 // 'add nuw x, C' produces [C, UINT_MAX].
10028 Lower = *C;
10029 } else if (HasNSW) {
10030 if (C->isNegative()) {
10031 // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C].
10033 Upper = APInt::getSignedMaxValue(Width) + *C + 1;
10034 } else {
10035 // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX].
10036 Lower = APInt::getSignedMinValue(Width) + *C;
10037 Upper = APInt::getSignedMaxValue(Width) + 1;
10038 }
10039 }
10040 }
10041 break;
10042
10043 case Instruction::And:
10044 if (match(BO.getOperand(1), m_APInt(C)))
10045 // 'and x, C' produces [0, C].
10046 Upper = *C + 1;
10047 // X & -X is a power of two or zero. So we can cap the value at max power of
10048 // two.
10049 if (match(BO.getOperand(0), m_Neg(m_Specific(BO.getOperand(1)))) ||
10050 match(BO.getOperand(1), m_Neg(m_Specific(BO.getOperand(0)))))
10051 Upper = APInt::getSignedMinValue(Width) + 1;
10052 break;
10053
10054 case Instruction::Or:
10055 if (match(BO.getOperand(1), m_APInt(C)))
10056 // 'or x, C' produces [C, UINT_MAX].
10057 Lower = *C;
10058 break;
10059
10060 case Instruction::AShr:
10061 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10062 // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C].
10064 Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1;
10065 } else if (match(BO.getOperand(0), m_APInt(C))) {
10066 unsigned ShiftAmount = Width - 1;
10067 if (!C->isZero() && IIQ.isExact(&BO))
10068 ShiftAmount = C->countr_zero();
10069 if (C->isNegative()) {
10070 // 'ashr C, x' produces [C, C >> (Width-1)]
10071 Lower = *C;
10072 Upper = C->ashr(ShiftAmount) + 1;
10073 } else {
10074 // 'ashr C, x' produces [C >> (Width-1), C]
10075 Lower = C->ashr(ShiftAmount);
10076 Upper = *C + 1;
10077 }
10078 }
10079 break;
10080
10081 case Instruction::LShr:
10082 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10083 // 'lshr x, C' produces [0, UINT_MAX >> C].
10084 Upper = APInt::getAllOnes(Width).lshr(*C) + 1;
10085 } else if (match(BO.getOperand(0), m_APInt(C))) {
10086 // 'lshr C, x' produces [C >> (Width-1), C].
10087 unsigned ShiftAmount = Width - 1;
10088 if (!C->isZero() && IIQ.isExact(&BO))
10089 ShiftAmount = C->countr_zero();
10090 Lower = C->lshr(ShiftAmount);
10091 Upper = *C + 1;
10092 }
10093 break;
10094
10095 case Instruction::Shl:
10096 if (match(BO.getOperand(0), m_APInt(C))) {
10097 if (IIQ.hasNoUnsignedWrap(&BO)) {
10098 // 'shl nuw C, x' produces [C, C << CLZ(C)]
10099 Lower = *C;
10100 Upper = Lower.shl(Lower.countl_zero()) + 1;
10101 } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw?
10102 if (C->isNegative()) {
10103 // 'shl nsw C, x' produces [C << CLO(C)-1, C]
10104 unsigned ShiftAmount = C->countl_one() - 1;
10105 Lower = C->shl(ShiftAmount);
10106 Upper = *C + 1;
10107 } else {
10108 // 'shl nsw C, x' produces [C, C << CLZ(C)-1]
10109 unsigned ShiftAmount = C->countl_zero() - 1;
10110 Lower = *C;
10111 Upper = C->shl(ShiftAmount) + 1;
10112 }
10113 } else {
10114 // If lowbit is set, value can never be zero.
10115 if ((*C)[0])
10116 Lower = APInt::getOneBitSet(Width, 0);
10117 // If we are shifting a constant the largest it can be is if the longest
10118 // sequence of consecutive ones is shifted to the highbits (breaking
10119 // ties for which sequence is higher). At the moment we take a liberal
10120 // upper bound on this by just popcounting the constant.
10121 // TODO: There may be a bitwise trick for it longest/highest
10122 // consecutative sequence of ones (naive method is O(Width) loop).
10123 Upper = APInt::getHighBitsSet(Width, C->popcount()) + 1;
10124 }
10125 } else if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10126 Upper = APInt::getBitsSetFrom(Width, C->getZExtValue()) + 1;
10127 }
10128 break;
10129
10130 case Instruction::SDiv:
10131 if (match(BO.getOperand(1), m_APInt(C))) {
10132 APInt IntMin = APInt::getSignedMinValue(Width);
10133 APInt IntMax = APInt::getSignedMaxValue(Width);
10134 if (C->isAllOnes()) {
10135 // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX]
10136 // where C != -1 and C != 0 and C != 1
10137 Lower = IntMin + 1;
10138 Upper = IntMax + 1;
10139 } else if (C->countl_zero() < Width - 1) {
10140 // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C]
10141 // where C != -1 and C != 0 and C != 1
10142 Lower = IntMin.sdiv(*C);
10143 Upper = IntMax.sdiv(*C);
10144 if (Lower.sgt(Upper))
10146 Upper = Upper + 1;
10147 assert(Upper != Lower && "Upper part of range has wrapped!");
10148 }
10149 } else if (match(BO.getOperand(0), m_APInt(C))) {
10150 if (C->isMinSignedValue()) {
10151 // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2].
10152 Lower = *C;
10153 Upper = Lower.lshr(1) + 1;
10154 } else {
10155 // 'sdiv C, x' produces [-|C|, |C|].
10156 Upper = C->abs() + 1;
10157 Lower = (-Upper) + 1;
10158 }
10159 }
10160 break;
10161
10162 case Instruction::UDiv:
10163 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
10164 // 'udiv x, C' produces [0, UINT_MAX / C].
10165 Upper = APInt::getMaxValue(Width).udiv(*C) + 1;
10166 } else if (match(BO.getOperand(0), m_APInt(C))) {
10167 // 'udiv C, x' produces [0, C].
10168 Upper = *C + 1;
10169 }
10170 break;
10171
10172 case Instruction::SRem:
10173 if (match(BO.getOperand(1), m_APInt(C))) {
10174 // 'srem x, C' produces (-|C|, |C|).
10175 Upper = C->abs();
10176 Lower = (-Upper) + 1;
10177 } else if (match(BO.getOperand(0), m_APInt(C))) {
10178 if (C->isNegative()) {
10179 // 'srem -|C|, x' produces [-|C|, 0].
10180 Upper = 1;
10181 Lower = *C;
10182 } else {
10183 // 'srem |C|, x' produces [0, |C|].
10184 Upper = *C + 1;
10185 }
10186 }
10187 break;
10188
10189 case Instruction::URem:
10190 if (match(BO.getOperand(1), m_APInt(C)))
10191 // 'urem x, C' produces [0, C).
10192 Upper = *C;
10193 else if (match(BO.getOperand(0), m_APInt(C)))
10194 // 'urem C, x' produces [0, C].
10195 Upper = *C + 1;
10196 break;
10197
10198 default:
10199 break;
10200 }
10201}
10202
10204 bool UseInstrInfo) {
10205 unsigned Width = II.getType()->getScalarSizeInBits();
10206 const APInt *C;
10207 switch (II.getIntrinsicID()) {
10208 case Intrinsic::ctlz:
10209 case Intrinsic::cttz: {
10210 APInt Upper(Width, Width);
10211 if (!UseInstrInfo || !match(II.getArgOperand(1), m_One()))
10212 Upper += 1;
10213 // Maximum of set/clear bits is the bit width.
10215 }
10216 case Intrinsic::ctpop:
10217 // Maximum of set/clear bits is the bit width.
10219 APInt(Width, Width) + 1);
10220 case Intrinsic::uadd_sat:
10221 // uadd.sat(x, C) produces [C, UINT_MAX].
10222 if (match(II.getOperand(0), m_APInt(C)) ||
10223 match(II.getOperand(1), m_APInt(C)))
10225 break;
10226 case Intrinsic::sadd_sat:
10227 if (match(II.getOperand(0), m_APInt(C)) ||
10228 match(II.getOperand(1), m_APInt(C))) {
10229 if (C->isNegative())
10230 // sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)].
10232 APInt::getSignedMaxValue(Width) + *C +
10233 1);
10234
10235 // sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].
10237 APInt::getSignedMaxValue(Width) + 1);
10238 }
10239 break;
10240 case Intrinsic::usub_sat:
10241 // usub.sat(C, x) produces [0, C].
10242 if (match(II.getOperand(0), m_APInt(C)))
10243 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
10244
10245 // usub.sat(x, C) produces [0, UINT_MAX - C].
10246 if (match(II.getOperand(1), m_APInt(C)))
10248 APInt::getMaxValue(Width) - *C + 1);
10249 break;
10250 case Intrinsic::ssub_sat:
10251 if (match(II.getOperand(0), m_APInt(C))) {
10252 if (C->isNegative())
10253 // ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)].
10255 *C - APInt::getSignedMinValue(Width) +
10256 1);
10257
10258 // ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].
10260 APInt::getSignedMaxValue(Width) + 1);
10261 } else if (match(II.getOperand(1), m_APInt(C))) {
10262 if (C->isNegative())
10263 // ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]:
10265 APInt::getSignedMaxValue(Width) + 1);
10266
10267 // ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].
10269 APInt::getSignedMaxValue(Width) - *C +
10270 1);
10271 }
10272 break;
10273 case Intrinsic::umin:
10274 case Intrinsic::umax:
10275 case Intrinsic::smin:
10276 case Intrinsic::smax:
10277 if (!match(II.getOperand(0), m_APInt(C)) &&
10278 !match(II.getOperand(1), m_APInt(C)))
10279 break;
10280
10281 switch (II.getIntrinsicID()) {
10282 case Intrinsic::umin:
10283 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
10284 case Intrinsic::umax:
10286 case Intrinsic::smin:
10288 *C + 1);
10289 case Intrinsic::smax:
10291 APInt::getSignedMaxValue(Width) + 1);
10292 default:
10293 llvm_unreachable("Must be min/max intrinsic");
10294 }
10295 break;
10296 case Intrinsic::abs:
10297 // If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX],
10298 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10299 if (match(II.getOperand(1), m_One()))
10301 APInt::getSignedMaxValue(Width) + 1);
10302
10304 APInt::getSignedMinValue(Width) + 1);
10305 case Intrinsic::vscale:
10306 if (!II.getParent() || !II.getFunction())
10307 break;
10308 return getVScaleRange(II.getFunction(), Width);
10309 default:
10310 break;
10311 }
10312
10313 return ConstantRange::getFull(Width);
10314}
10315
10317 const InstrInfoQuery &IIQ) {
10318 unsigned BitWidth = SI.getType()->getScalarSizeInBits();
10319 const Value *LHS = nullptr, *RHS = nullptr;
10321 if (R.Flavor == SPF_UNKNOWN)
10322 return ConstantRange::getFull(BitWidth);
10323
10324 if (R.Flavor == SelectPatternFlavor::SPF_ABS) {
10325 // If the negation part of the abs (in RHS) has the NSW flag,
10326 // then the result of abs(X) is [0..SIGNED_MAX],
10327 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10328 if (match(RHS, m_Neg(m_Specific(LHS))) &&
10332
10335 }
10336
10337 if (R.Flavor == SelectPatternFlavor::SPF_NABS) {
10338 // The result of -abs(X) is <= 0.
10340 APInt(BitWidth, 1));
10341 }
10342
10343 const APInt *C;
10344 if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C)))
10345 return ConstantRange::getFull(BitWidth);
10346
10347 switch (R.Flavor) {
10348 case SPF_UMIN:
10350 case SPF_UMAX:
10352 case SPF_SMIN:
10354 *C + 1);
10355 case SPF_SMAX:
10358 default:
10359 return ConstantRange::getFull(BitWidth);
10360 }
10361}
10362
10364 // The maximum representable value of a half is 65504. For floats the maximum
10365 // value is 3.4e38 which requires roughly 129 bits.
10366 unsigned BitWidth = I->getType()->getScalarSizeInBits();
10367 if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy())
10368 return;
10369 if (isa<FPToSIInst>(I) && BitWidth >= 17) {
10370 Lower = APInt(BitWidth, -65504, true);
10371 Upper = APInt(BitWidth, 65505);
10372 }
10373
10374 if (isa<FPToUIInst>(I) && BitWidth >= 16) {
10375 // For a fptoui the lower limit is left as 0.
10376 Upper = APInt(BitWidth, 65505);
10377 }
10378}
10379
10381 const SimplifyQuery &SQ,
10382 unsigned Depth) {
10383 assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction");
10384
10386 return ConstantRange::getFull(V->getType()->getScalarSizeInBits());
10387
10388 if (auto *C = dyn_cast<Constant>(V))
10389 return C->toConstantRange();
10390
10391 unsigned BitWidth = V->getType()->getScalarSizeInBits();
10392 ConstantRange CR = ConstantRange::getFull(BitWidth);
10393 if (auto *BO = dyn_cast<BinaryOperator>(V)) {
10394 APInt Lower = APInt(BitWidth, 0);
10395 APInt Upper = APInt(BitWidth, 0);
10396 // TODO: Return ConstantRange.
10397 setLimitsForBinOp(*BO, Lower, Upper, SQ.IIQ, ForSigned);
10399 } else if (auto *II = dyn_cast<IntrinsicInst>(V))
10401 else if (auto *SI = dyn_cast<SelectInst>(V)) {
10402 ConstantRange CRTrue =
10403 computeConstantRange(SI->getTrueValue(), ForSigned, SQ, Depth + 1);
10404 ConstantRange CRFalse =
10405 computeConstantRange(SI->getFalseValue(), ForSigned, SQ, Depth + 1);
10406 CR = CRTrue.unionWith(CRFalse);
10408 } else if (auto *TI = dyn_cast<TruncInst>(V)) {
10409 ConstantRange SrcCR =
10410 computeConstantRange(TI->getOperand(0), ForSigned, SQ, Depth + 1);
10411 CR = SrcCR.truncate(BitWidth);
10412 } else if (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {
10413 APInt Lower = APInt(BitWidth, 0);
10414 APInt Upper = APInt(BitWidth, 0);
10415 // TODO: Return ConstantRange.
10418 } else if (const auto *A = dyn_cast<Argument>(V))
10419 if (std::optional<ConstantRange> Range = A->getRange())
10420 CR = *Range;
10421
10422 if (auto *I = dyn_cast<Instruction>(V)) {
10423 if (auto *Range = SQ.IIQ.getMetadata(I, LLVMContext::MD_range))
10425
10426 Value *FrexpSrc;
10427 if (const auto *CB = dyn_cast<CallBase>(V)) {
10428 if (std::optional<ConstantRange> Range = CB->getRange())
10429 CR = CR.intersectWith(*Range);
10431 m_Value(FrexpSrc))))) {
10432 const fltSemantics &FltSem =
10433 FrexpSrc->getType()->getScalarType()->getFltSemantics();
10434 // It should be possible to implement this for any type, but this logic
10435 // only computes the range assuming standard subnormal handling.
10436 if (APFloat::isIEEELikeFP(FltSem)) {
10437 KnownFPClass KnownSrc =
10438 computeKnownFPClass(FrexpSrc, fcSubnormal, SQ, Depth + 1);
10439
10440 // Exponent result is (src == 0) ? 0 : ilogb(src) + 1, and unspecified
10441 // for inf/nan.
10442 int MinExp = APFloat::semanticsMinExponent(FltSem) + 1;
10443
10444 // Offset to find the true minimum exponent value for a denormal.
10445 if (!KnownSrc.isKnownNeverSubnormal())
10446 MinExp -= (APFloat::semanticsPrecision(FltSem) - 1);
10447
10448 int MaxExp = APFloat::semanticsMaxExponent(FltSem) + 1;
10450 APInt(BitWidth, MinExp, /*isSigned=*/true),
10451 APInt(BitWidth, MaxExp + 1, /*isSigned=*/true));
10452 }
10453 }
10454 }
10455
10456 if (SQ.CxtI && SQ.AC) {
10457 // Try to restrict the range based on information from assumptions.
10458 for (auto &AssumeVH : SQ.AC->assumptionsFor(V)) {
10459 if (!AssumeVH)
10460 continue;
10461 CallInst *I = cast<CallInst>(AssumeVH);
10462 assert(I->getParent()->getParent() == SQ.CxtI->getParent()->getParent() &&
10463 "Got assumption for the wrong function!");
10464 assert(I->getIntrinsicID() == Intrinsic::assume &&
10465 "must be an assume intrinsic");
10466
10467 if (!isValidAssumeForContext(I, SQ))
10468 continue;
10469 Value *Arg = I->getArgOperand(0);
10470 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
10471 // Currently we just use information from comparisons.
10472 if (!Cmp || Cmp->getOperand(0) != V)
10473 continue;
10474 // TODO: Set "ForSigned" parameter via Cmp->isSigned()?
10475 ConstantRange RHS =
10476 computeConstantRange(Cmp->getOperand(1), /*ForSigned=*/false,
10477 SQ.getWithInstruction(I), Depth + 1);
10478 CR = CR.intersectWith(
10479 ConstantRange::makeAllowedICmpRegion(Cmp->getCmpPredicate(), RHS));
10480 }
10481 }
10482
10483 return CR;
10484}
10485
10486static void
10488 function_ref<void(Value *)> InsertAffected) {
10489 assert(V != nullptr);
10490 if (isa<Argument>(V) || isa<GlobalValue>(V)) {
10491 InsertAffected(V);
10492 } else if (auto *I = dyn_cast<Instruction>(V)) {
10493 InsertAffected(V);
10494
10495 // Peek through unary operators to find the source of the condition.
10496 Value *Op;
10498 m_Trunc(m_Value(Op))))) {
10500 InsertAffected(Op);
10501 }
10502 }
10503}
10504
10506 Value *Cond, bool IsAssume, function_ref<void(Value *)> InsertAffected) {
10507 auto AddAffected = [&InsertAffected](Value *V) {
10508 addValueAffectedByCondition(V, InsertAffected);
10509 };
10510
10511 auto AddCmpOperands = [&AddAffected, IsAssume](Value *LHS, Value *RHS) {
10512 if (IsAssume) {
10513 AddAffected(LHS);
10514 AddAffected(RHS);
10515 } else if (match(RHS, m_Constant()))
10516 AddAffected(LHS);
10517 };
10518
10519 SmallVector<Value *, 8> Worklist;
10521 Worklist.push_back(Cond);
10522 while (!Worklist.empty()) {
10523 Value *V = Worklist.pop_back_val();
10524 if (!Visited.insert(V).second)
10525 continue;
10526
10527 CmpPredicate Pred;
10528 Value *A, *B, *X;
10529
10530 if (IsAssume) {
10531 AddAffected(V);
10532 if (match(V, m_Not(m_Value(X))))
10533 AddAffected(X);
10534 }
10535
10536 if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
10537 // assume(A && B) is split to -> assume(A); assume(B);
10538 // assume(!(A || B)) is split to -> assume(!A); assume(!B);
10539 // Finally, assume(A || B) / assume(!(A && B)) generally don't provide
10540 // enough information to be worth handling (intersection of information as
10541 // opposed to union).
10542 if (!IsAssume) {
10543 Worklist.push_back(A);
10544 Worklist.push_back(B);
10545 }
10546 } else if (match(V, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
10547 bool HasRHSC = match(B, m_ConstantInt());
10548 if (ICmpInst::isEquality(Pred)) {
10549 AddAffected(A);
10550 if (IsAssume)
10551 AddAffected(B);
10552 if (HasRHSC) {
10553 Value *Y;
10554 // (X << C) or (X >>_s C) or (X >>_u C).
10555 if (match(A, m_Shift(m_Value(X), m_ConstantInt())))
10556 AddAffected(X);
10557 // (X & C) or (X | C).
10558 else if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10559 match(A, m_Or(m_Value(X), m_Value(Y)))) {
10560 AddAffected(X);
10561 AddAffected(Y);
10562 }
10563 // X - Y
10564 else if (match(A, m_Sub(m_Value(X), m_Value(Y)))) {
10565 AddAffected(X);
10566 AddAffected(Y);
10567 }
10568 }
10569 } else {
10570 AddCmpOperands(A, B);
10571 if (HasRHSC) {
10572 // Handle (A + C1) u< C2, which is the canonical form of
10573 // A > C3 && A < C4.
10575 AddAffected(X);
10576
10577 if (ICmpInst::isUnsigned(Pred)) {
10578 Value *Y;
10579 // X & Y u> C -> X >u C && Y >u C
10580 // X | Y u< C -> X u< C && Y u< C
10581 // X nuw+ Y u< C -> X u< C && Y u< C
10582 if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10583 match(A, m_Or(m_Value(X), m_Value(Y))) ||
10584 match(A, m_NUWAdd(m_Value(X), m_Value(Y)))) {
10585 AddAffected(X);
10586 AddAffected(Y);
10587 }
10588 // X nuw- Y u> C -> X u> C
10589 if (match(A, m_NUWSub(m_Value(X), m_Value())))
10590 AddAffected(X);
10591 }
10592 }
10593
10594 // Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported
10595 // by computeKnownFPClass().
10597 if (Pred == ICmpInst::ICMP_SLT && match(B, m_Zero()))
10598 InsertAffected(X);
10599 else if (Pred == ICmpInst::ICMP_SGT && match(B, m_AllOnes()))
10600 InsertAffected(X);
10601 }
10602 }
10603
10604 if (HasRHSC && match(A, m_Ctpop(m_Value(X))))
10605 AddAffected(X);
10606 } else if (match(V, m_FCmp(Pred, m_Value(A), m_Value(B)))) {
10607 AddCmpOperands(A, B);
10608
10609 // fcmp fneg(x), y
10610 // fcmp fabs(x), y
10611 // fcmp fneg(fabs(x)), y
10612 if (match(A, m_FNeg(m_Value(A))))
10613 AddAffected(A);
10614 if (match(A, m_FAbs(m_Value(A))))
10615 AddAffected(A);
10616
10618 m_Value()))) {
10619 // Handle patterns that computeKnownFPClass() support.
10620 AddAffected(A);
10621 } else if (!IsAssume && match(V, m_Trunc(m_Value(X)))) {
10622 // Assume is checked here as X is already added above for assumes in
10623 // addValueAffectedByCondition
10624 AddAffected(X);
10625 } else if (!IsAssume && match(V, m_Not(m_Value(X)))) {
10626 // Assume is checked here to avoid issues with ephemeral values
10627 Worklist.push_back(X);
10628 }
10629 }
10630}
10631
10633 // (X >> C) or/add (X & mask(C) != 0)
10634 if (const auto *BO = dyn_cast<BinaryOperator>(V)) {
10635 if (BO->getOpcode() == Instruction::Add ||
10636 BO->getOpcode() == Instruction::Or) {
10637 const Value *X;
10638 const APInt *C1, *C2;
10639 if (match(BO, m_c_BinOp(m_LShr(m_Value(X), m_APInt(C1)),
10643 m_Zero())))) &&
10644 C2->popcount() == C1->getZExtValue())
10645 return X;
10646 }
10647 }
10648 return nullptr;
10649}
10650
10652 return const_cast<Value *>(stripNullTest(const_cast<const Value *>(V)));
10653}
10654
10657 unsigned MaxCount, bool AllowUndefOrPoison) {
10660 auto Push = [&](const Value *V) -> bool {
10661 Constant *C;
10662 if (match(const_cast<Value *>(V), m_ImmConstant(C))) {
10663 if (!AllowUndefOrPoison && !isGuaranteedNotToBeUndefOrPoison(C))
10664 return false;
10665 // Check existence first to avoid unnecessary allocations.
10666 if (Constants.contains(C))
10667 return true;
10668 if (Constants.size() == MaxCount)
10669 return false;
10670 Constants.insert(C);
10671 return true;
10672 }
10673
10674 if (auto *Inst = dyn_cast<Instruction>(V)) {
10675 if (Visited.insert(Inst).second)
10676 Worklist.push_back(Inst);
10677 return true;
10678 }
10679 return false;
10680 };
10681 if (!Push(V))
10682 return false;
10683 while (!Worklist.empty()) {
10684 const Instruction *CurInst = Worklist.pop_back_val();
10685 switch (CurInst->getOpcode()) {
10686 case Instruction::Select:
10687 if (!Push(CurInst->getOperand(1)))
10688 return false;
10689 if (!Push(CurInst->getOperand(2)))
10690 return false;
10691 break;
10692 case Instruction::PHI:
10693 for (Value *IncomingValue : cast<PHINode>(CurInst)->incoming_values()) {
10694 // Fast path for recurrence PHI.
10695 if (IncomingValue == CurInst)
10696 continue;
10697 if (!Push(IncomingValue))
10698 return false;
10699 }
10700 break;
10701 default:
10702 return false;
10703 }
10704 }
10705 return true;
10706}
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:853
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)
static Value * getOpcode(Value &V, Type &Ty, InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB)
#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")
This file contains the UndefPoisonKind enum and helper functions.
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.
static bool isKnownNonEqualFromContext(const Value *V1, const Value *V2, const SimplifyQuery &Q, unsigned Depth)
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 std::optional< bool > isImpliedCondCommonOperandWithCR(CmpPredicate LPred, const ConstantRange &LCR, CmpPredicate RPred, const ConstantRange &RCR)
Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
static ConstantRange getRangeForSelectPattern(const SelectInst &SI, const InstrInfoQuery &IIQ)
static void computeKnownBitsFromOperator(const Operator *I, const APInt &DemandedElts, KnownBits &Known, const SimplifyQuery &Q, unsigned Depth)
static uint64_t GetStringLengthH(const Value *V, SmallPtrSetImpl< const PHINode * > &PHIs, unsigned CharSize)
If we can compute the length of the string pointed to by the specified pointer, return 'len+1'.
static void computeKnownBitsFromShiftOperator(const Operator *I, const APInt &DemandedElts, KnownBits &Known, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth, function_ref< KnownBits(const KnownBits &, const KnownBits &, bool)> KF)
Compute known bits from a shift operator, including those with a non-constant shift amount.
static bool onlyUsedByLifetimeMarkersOrDroppableInstsHelper(const Value *V, bool AllowLifetime, bool AllowDroppable)
static std::optional< bool > isImpliedCondAndOr(const Instruction *LHS, CmpPredicate RHSPred, const Value *RHSOp0, const Value *RHSOp1, const DataLayout &DL, bool LHSIsTrue, unsigned Depth)
Return true if LHS implies RHS is true.
static bool isSignedMinMaxClamp(const Value *Select, const Value *&In, const APInt *&CLow, const APInt *&CHigh)
static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, bool NSW, bool NUW, unsigned Depth)
static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V, unsigned Depth)
static bool isNonEqualSelect(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst, Value *&Init, Value *&OtherOp)
static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static void computeKnownBitsFromCmp(const Value *V, CmpInst::Predicate Pred, Value *LHS, Value *RHS, KnownBits &Known, const SimplifyQuery &Q)
static SelectPatternResult matchMinMaxOfMinMax(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TVal, Value *FVal, unsigned Depth)
Recognize variations of: a < c ?
static void unionWithMinMaxIntrinsicClamp(const IntrinsicInst *II, KnownBits &Known)
static void setLimitForFPToI(const Instruction *I, APInt &Lower, APInt &Upper)
static bool isSameUnderlyingObjectInLoop(const PHINode *PN, const LoopInfo *LI)
PN defines a loop-variant pointer to an object.
static bool isNonEqualPointersWithRecursiveGEP(const Value *A, const Value *B, const SimplifyQuery &Q)
static bool isSignedMinMaxIntrinsicClamp(const IntrinsicInst *II, const APInt *&CLow, const APInt *&CHigh)
static Value * lookThroughCastConst(CmpInst *CmpI, Type *SrcTy, Constant *C, Instruction::CastOps *CastOp)
static bool handleGuaranteedWellDefinedOps(const Instruction *I, const CallableT &Handle)
Enumerates all operands of I that are guaranteed to not be undef or poison.
static bool isAbsoluteValueULEOne(const Value *V)
static void computeKnownBitsFromLerpPattern(const Value *Op0, const Value *Op1, const APInt &DemandedElts, KnownBits &KnownOut, const SimplifyQuery &Q, unsigned Depth)
Try to detect the lerp pattern: a * (b - c) + c * d where a >= 0, b >= 0, c >= 0, d >= 0,...
static KnownFPClass computeKnownFPClassFromContext(const Value *V, const SimplifyQuery &Q)
static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1, bool NSW, bool NUW, const APInt &DemandedElts, KnownBits &KnownOut, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth)
static Value * getNotValue(Value *V)
If the input value is the result of a 'not' op, constant integer, or vector splat of a constant integ...
static constexpr KnownFPClass::MinMaxKind getMinMaxKind(Intrinsic::ID IID)
static unsigned ComputeNumSignBitsImpl(const Value *V, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return the number of times the sign bit of the register is replicated into the other bits.
static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp, KnownBits &Known, const SimplifyQuery &SQ, bool Invert)
static bool isKnownNonZeroFromOperator(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static bool matchOpWithOpEqZero(Value *Op0, Value *Op1)
static bool isNonZeroRecurrence(const PHINode *PN)
Try to detect a recurrence that monotonically increases/decreases from a non-zero starting value.
static SelectPatternResult matchClamp(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal)
Recognize variations of: CLAMP(v,l,h) ==> ((v) < (l) ?
static bool shiftAmountKnownInRange(const Value *ShiftAmount)
Shifts return poison if shiftwidth is larger than the bitwidth.
static bool isEphemeralValueOf(const Instruction *I, const Value *E)
static SelectPatternResult matchMinMax(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, unsigned Depth)
Match non-obvious integer minimum and maximum sequences.
static KnownBits computeKnownBitsForHorizontalOperation(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth, const function_ref< KnownBits(const KnownBits &, const KnownBits &)> KnownBitsFunc)
static bool handleGuaranteedNonPoisonOps(const Instruction *I, const CallableT &Handle)
Enumerates all operands of I that are guaranteed to not be poison.
static std::optional< std::pair< Value *, Value * > > getInvertibleOperands(const Operator *Op1, const Operator *Op2)
If the pair of operators are the same invertible function, return the the operands of the function co...
static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS)
static void computeKnownBitsFromCond(const Value *V, Value *Cond, KnownBits &Known, const SimplifyQuery &SQ, bool Invert, unsigned Depth)
static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q)
static std::optional< bool > isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS, const Value *ARHS, const Value *BLHS, const Value *BRHS)
Return true if "icmp Pred BLHS BRHS" is true whenever "icmp PredALHS ARHS" is true.
static const Instruction * safeCxtI(const Value *V, const Instruction *CxtI)
static bool isNonEqualMul(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and the multiplication is nuw o...
static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero, const Value *Cond, bool CondIsTrue)
Return true if we can infer that V is known to be a power of 2 from dominating condition Cond (e....
static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW, bool NUW, const APInt &DemandedElts, KnownBits &Known, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth)
static bool matchThreeInputRecurrence(const PHINode *PN, InstTy *&Inst, Value *&Init, Value *&OtherOp0, Value *&OtherOp1)
static bool isKnownNonNaN(const Value *V, FastMathFlags FMF)
static ConstantRange getRangeForIntrinsic(const IntrinsicInst &II, bool UseInstrInfo)
static void computeKnownFPClassForFPTrunc(const Operator *Op, const APInt &DemandedElts, FPClassTest InterestedClasses, KnownFPClass &Known, const SimplifyQuery &Q, unsigned Depth)
static Value * BuildSubAggregate(Value *From, Value *To, Type *IndexedType, SmallVectorImpl< unsigned > &Idxs, unsigned IdxSkip, BasicBlock::iterator InsertBefore)
Value * RHS
Value * LHS
static LLVM_ABI ExponentType semanticsMinExponent(const fltSemantics &)
Definition APFloat.cpp:235
static LLVM_ABI ExponentType semanticsMaxExponent(const fltSemantics &)
Definition APFloat.cpp:231
static LLVM_ABI unsigned int semanticsPrecision(const fltSemantics &)
Definition APFloat.cpp:227
static LLVM_ABI bool isIEEELikeFP(const fltSemantics &)
Definition APFloat.cpp:268
bool isFinite() const
Definition APFloat.h:1543
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1197
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1157
bool isInteger() const
Definition APFloat.h:1555
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:2023
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1616
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:235
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition APInt.h:1429
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition APInt.h:424
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1563
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition APInt.h:1414
unsigned popcount() const
Count the number of bits set.
Definition APInt.h:1693
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1408
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition APInt.h:207
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition APInt.h:1353
unsigned ceilLogBase2() const
Definition APInt.h:1787
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition APInt.h:1208
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition APInt.h:372
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1189
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:381
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1511
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1118
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:210
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition APInt.h:217
bool isNegative() const
Determine sign of this APInt.
Definition APInt.h:330
bool intersects(const APInt &RHS) const
This operation tests if there are any pairs of corresponding bits between this APInt and RHS that are...
Definition APInt.h:1256
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1687
void clearAllBits()
Set every bit to 0.
Definition APInt.h:1419
LLVM_ABI APInt reverseBits() const
Definition APInt.cpp:790
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition APInt.h:1173
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1651
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition APInt.h:1621
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:220
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1084
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition APInt.h:357
unsigned logBase2() const
Definition APInt.h:1784
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:834
bool getBoolValue() const
Convert APInt to a boolean value.
Definition APInt.h:472
bool isMaxSignedValue() const
Determine if this is the largest signed value.
Definition APInt.h:406
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:335
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition APInt.h:1157
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:880
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition APInt.h:1264
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition APInt.h:1137
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition APInt.h:297
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:201
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition APInt.h:1411
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1244
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:287
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:240
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:858
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1228
void clearSignBit()
Set the sign bit to 0.
Definition APInt.h:1472
an instruction to allocate memory on the stack
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator end() const
Definition ArrayRef.h:130
size_t size() const
Get the array size.
Definition ArrayRef.h:141
iterator begin() const
Definition ArrayRef.h:129
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
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:185
Class to represent array types.
This represents the llvm.assume intrinsic.
A cache of @llvm.assume calls within a function.
MutableArrayRef< ResultElem > assumptionsFor(const Value *V)
Access the list of assumptions which affect this value.
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:105
LLVM_ABI std::optional< unsigned > getVScaleRangeMax() const
Returns the maximum value for the vscale_range attribute or std::nullopt when unknown.
LLVM_ABI unsigned getVScaleRangeMin() const
Returns the minimum value for the vscale_range attribute.
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition Attributes.h:261
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator end()
Definition BasicBlock.h:474
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:461
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
InstListType::const_iterator const_iterator
Definition BasicBlock.h:171
LLVM_ABI const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
LLVM_ABI const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Definition BasicBlock.h:237
LLVM_ABI Instruction::BinaryOps getBinaryOp() const
Returns the binary operation underlying the intrinsic.
BinaryOps getOpcode() const
Definition InstrTypes.h:409
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:512
This class is the base class for the comparison instructions.
Definition InstrTypes.h:728
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:740
@ ICMP_SLT
signed less than
Definition InstrTypes.h:769
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:770
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:746
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition InstrTypes.h:755
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:744
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:745
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:764
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:763
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:767
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition InstrTypes.h:754
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:765
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition InstrTypes.h:752
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:747
@ ICMP_NE
not equal
Definition InstrTypes.h:762
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:768
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:766
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition InstrTypes.h:753
bool isSigned() const
Definition InstrTypes.h:993
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:890
bool isTrueWhenEqual() const
This is just a convenience.
static bool isFPPredicate(Predicate P)
Definition InstrTypes.h:833
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition InstrTypes.h:852
Predicate getPredicate() const
Return the predicate for this instruction.
Definition InstrTypes.h:828
Predicate getFlippedStrictnessPredicate() const
For predicate of kind "is X or equal to 0" returns the predicate "is X".
Definition InstrTypes.h:956
static bool isIntPredicate(Predicate P)
Definition InstrTypes.h:839
static LLVM_ABI bool isOrdered(Predicate predicate)
Determine if the predicate is an ordered operation.
bool isUnsigned() const
Definition InstrTypes.h:999
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
static LLVM_ABI std::optional< CmpPredicate > getMatching(CmpPredicate A, CmpPredicate B)
Compares two CmpPredicates taking samesign into account and returns the canonicalized CmpPredicate if...
LLVM_ABI CmpInst::Predicate getPreferredSignedPredicate() const
Attempts to return a signed CmpInst::Predicate from the CmpPredicate.
CmpInst::Predicate dropSameSign() const
Drops samesign information.
bool hasSameSign() const
Query samesign information, for optimizations.
Conditional Branch instruction.
An array constant whose element type is a simple 1/2/4/8-byte integer, bytes or float/double,...
Definition Constants.h:859
ConstantDataSequential - A vector or array constant whose element type is a simple 1/2/4/8-byte integ...
Definition Constants.h:749
StringRef getAsString() const
If this array is isString(), then this method returns the array as a StringRef.
Definition Constants.h:825
A vector constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition Constants.h:945
static LLVM_ABI Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static LLVM_ABI Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static LLVM_ABI std::optional< ConstantFPRange > makeExactFCmpRegion(FCmpInst::Predicate Pred, const APFloat &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:420
This is the shared class of boolean and integer constants.
Definition Constants.h:87
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition Constants.h:168
This class represents a range of values.
PreferredRangeType
If represented precisely, the result of some range operations may consist of multiple disjoint ranges...
static LLVM_ABI ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
LLVM_ABI OverflowResult unsignedSubMayOverflow(const ConstantRange &Other) const
Return whether unsigned sub of the two ranges always/never overflows.
LLVM_ABI bool isAllNegative() const
Return true if all values in this range are negative.
LLVM_ABI OverflowResult unsignedAddMayOverflow(const ConstantRange &Other) const
Return whether unsigned add of the two ranges always/never overflows.
LLVM_ABI KnownBits toKnownBits() const
Return known bits for values in this range.
LLVM_ABI bool icmp(CmpInst::Predicate Pred, const ConstantRange &Other) const
Does the predicate Pred hold between ranges this and Other?
LLVM_ABI OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const
Return whether unsigned mul of the two ranges always/never overflows.
LLVM_ABI ConstantRange truncate(uint32_t BitWidth, unsigned NoWrapKind=0) const
Return a new range in the specified integer type, which must be strictly smaller than the current typ...
LLVM_ABI bool isAllNonNegative() const
Return true if all values in this range are non-negative.
static LLVM_ABI ConstantRange makeAllowedICmpRegion(CmpInst::Predicate Pred, const ConstantRange &Other)
Produce the smallest range such that all values that may satisfy the given predicate with any value c...
LLVM_ABI ConstantRange unionWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the union of this range with another range.
static LLVM_ABI ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred, const APInt &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
LLVM_ABI bool contains(const APInt &Val) const
Return true if the specified value is in the set.
LLVM_ABI OverflowResult signedAddMayOverflow(const ConstantRange &Other) const
Return whether signed add of the two ranges always/never overflows.
LLVM_ABI ConstantRange intersectWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the intersection of this range with another range.
OverflowResult
Represents whether an operation on the given constant range is known to always or never overflow.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
static ConstantRange getNonEmpty(APInt Lower, APInt Upper)
Create non-empty constant range with the given bounds.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
LLVM_ABI OverflowResult signedSubMayOverflow(const ConstantRange &Other) const
Return whether signed sub of the two ranges always/never overflows.
LLVM_ABI ConstantRange sub(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a subtraction of a value in this r...
This is an important base class in LLVM.
Definition Constant.h:43
static LLVM_ABI Constant * replaceUndefsWith(Constant *C, Constant *Replacement)
Try to replace undefined constant C or undefined elements in C with Replacement.
LLVM_ABI Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition Constant.h:64
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...
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
bool isLittleEndian() const
Layout endianness...
Definition DataLayout.h:217
unsigned getAddressSizeInBits(unsigned AS) const
The size in bits of an address in for the given AS.
Definition DataLayout.h:518
LLVM_ABI const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
LLVM_ABI unsigned getIndexTypeSizeInBits(Type *Ty) const
The size in bits of the index used in GEP calculation for this type.
LLVM_ABI unsigned getPointerTypeSizeInBits(Type *) const
The pointer representation size in bits for this type.
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
Definition DataLayout.h:791
ArrayRef< CondBrInst * > conditionsFor(const Value *V) const
Access the list of branches which affect this value.
DomTreeNodeBase * getIDom() const
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:155
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:202
Convenience struct for specifying and reasoning about fast-math flags.
Definition FMF.h:23
bool noSignedZeros() const
Definition FMF.h:67
bool noInfs() const
Definition FMF.h:66
void setNoSignedZeros(bool B=true)
Definition FMF.h:84
void setNoNaNs(bool B=true)
Definition FMF.h:78
bool noNaNs() const
Definition FMF.h:65
const BasicBlock & getEntryBlock() const
Definition Function.h:809
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:196
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:1075
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1439
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:156
bool isExact() const
Test whether this division is known to be exact, with zero remainder.
Definition Operator.h:175
This class represents the LLVM 'select' instruction.
const Value * getFalseValue() const
const Value * getCondition() const
const Value * getTrueValue() const
This instruction constructs a fixed permutation of two input vectors.
VectorType * getType() const
Overload to return most specific vector type.
static LLVM_ABI void getShuffleMask(const Constant *Mask, SmallVectorImpl< int > &Result)
Convert the input shuffle mask operand to a vector of integers.
size_type size() const
Definition SmallPtrSet.h:99
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void reserve(size_type N)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:591
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition DataLayout.h:743
TypeSize getElementOffset(unsigned Idx) const
Definition DataLayout.h:774
Class to represent struct types.
unsigned getNumElements() const
Random access to the elements.
Type * getElementType(unsigned N) const
Provides information about what library functions are available for the current target.
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM_ABI unsigned getIntegerBitWidth() const
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:288
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition Type.h:263
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:282
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
Definition Type.h:155
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
LLVM_ABI uint64_t getArrayNumElements() const
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:307
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:368
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:326
bool isHalfTy() const
Return true if this is 'half', a 16-bit IEEE fp type.
Definition Type.h:144
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:232
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
Definition Type.h:158
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
Definition Type.h:285
bool isIntOrPtrTy() const
Return true if this is an integer type or a pointer type.
Definition Type.h:270
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:257
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition Type.cpp:313
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
Definition Type.h:227
LLVM_ABI const fltSemantics & getFltSemantics() const
Definition Type.cpp: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:36
User * getUser() const
Returns the User that contains this Use.
Definition Use.h:61
op_range operands()
Definition User.h:267
Value * getOperand(unsigned i) const
Definition User.h:207
unsigned getNumOperands() const
Definition User.h:229
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
const Value * stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, APInt &Offset) const
This is a wrapper around stripAndAccumulateConstantOffsets with the in-bounds requirement set to fals...
Definition Value.h:727
iterator_range< user_iterator > users()
Definition Value.h:426
LLVM_ABI const Value * stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, bool AllowInvariantGroup=false, function_ref< bool(Value &Value, APInt &Offset)> ExternalAnalysis=nullptr, bool LookThroughIntToPtr=false) const
Accumulate the constant offset this value has compared to a base pointer.
const KnownBits & getKnownBits(const SimplifyQuery &Q) const
Definition WithCache.h:59
PointerType getValue() const
Definition WithCache.h:57
Represents an op.with.overflow intrinsic.
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:168
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:165
An efficient, type-erasing, non-owning reference to a callable.
TypeSize getSequentialElementStride(const DataLayout &DL) const
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:123
A range adaptor for a pair of iterators.
CallInst * Call
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
#define UINT64_MAX
Definition DataTypes.h:77
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI APInt ScaleBitMask(const APInt &A, unsigned NewBitWidth, bool MatchAllBits=false)
Splat/Merge neighboring bits to widen/narrow the bitmask represented by.
Definition APInt.cpp:3061
const APInt & umax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be unsigned.
Definition APInt.h:2292
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
SpecificConstantMatch m_ZeroInt()
Convenience matchers for specific integer values.
BinaryOp_match< SpecificConstantMatch, SrcTy, TargetOpcode::G_SUB > m_Neg(const SrcTy &&Src)
Matches a register negated by a G_SUB.
BinaryOp_match< SrcTy, SpecificConstantMatch, TargetOpcode::G_XOR, true > m_Not(const SrcTy &&Src)
Matches a register not-ed by a G_XOR.
OneUse_match< SubPat > m_OneUse(const SubPat &SP)
match_combine_or< Ty... > m_CombineOr(const Ty &...Ps)
Combine pattern matchers matching any of Ps patterns.
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
cst_pred_ty< is_lowbit_mask > m_LowBitMask()
Match an integer or vector with only the low bit(s) set.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
PtrToIntSameSize_match< OpTy > m_PtrToIntSameSize(const DataLayout &DL, const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, FCmpInst > m_FCmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
cst_pred_ty< is_sign_mask > m_SignMask()
Match an integer or vector with only the sign bit(s) set.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWAdd(const LHS &L, const RHS &R)
auto m_PtrToIntOrAddr(const OpTy &Op)
Matches PtrToInt or PtrToAddr.
BinaryOp_match< LHS, RHS, Instruction::FSub > m_FSub(const LHS &L, const RHS &R)
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
BinaryOp_match< LHS, RHS, Instruction::URem > m_URem(const LHS &L, const RHS &R)
auto m_LogicalOp()
Matches either L && R or L || R where L and R are arbitrary values.
ap_match< APInt > m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
BinaryOp_match< LHS, RHS, Instruction::And, true > m_c_And(const LHS &L, const RHS &R)
Matches an And with LHS and RHS in either order.
cst_pred_ty< is_power2_or_zero > m_Power2OrZero()
Match an integer or vector of 0 or power-of-2 values.
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWSub(const LHS &L, const RHS &R)
bool match(Val *V, const Pattern &P)
BinOpPred_match< LHS, RHS, is_idiv_op > m_IDiv(const LHS &L, const RHS &R)
Matches integer division operations.
match_bind< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
match_deferred< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMaxNum(const Opnd0 &Op0, const Opnd1 &Op1)
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
ap_match< APFloat > m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
CmpClass_match< LHS, RHS, ICmpInst, true > m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
auto match_fn(const Pattern &P)
A match functor that can be used as a UnaryPredicate in functional algorithms like all_of.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true > m_c_NUWAdd(const LHS &L, const RHS &R)
cst_pred_ty< is_nonnegative > m_NonNegative()
Match an integer or vector of non-negative values.
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
IntrinsicID_match m_Intrinsic()
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
IntrinsicID_match m_VScale()
Matches a call to llvm.vscale().
match_combine_or< MaxMin_match< FCmpInst, LHS, RHS, ofmin_pred_ty >, MaxMin_match< FCmpInst, LHS, RHS, ufmin_pred_ty > > m_OrdOrUnordFMin(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point minimum function.
auto m_BasicBlock()
Match an arbitrary basic block value and ignore it.
ExtractValue_match< Ind, Val_t > m_ExtractValue(const Val_t &V)
Match a single index ExtractValue instruction.
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > m_SMin(const LHS &L, const RHS &R)
ICmpLike_match< LHS, RHS > m_ICmpLike(CmpPredicate &Pred, const LHS &L, const RHS &R)
auto m_Value()
Match an arbitrary value and ignore it.
BinaryOp_match< LHS, RHS, Instruction::Xor, true > m_c_Xor(const LHS &L, const RHS &R)
Matches an Xor with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
auto m_Constant()
Match an arbitrary Constant and ignore it.
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty, true > m_c_SMin(const LHS &L, const RHS &R)
Matches an SMin with LHS and RHS in either order.
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty, true > m_c_UMax(const LHS &L, const RHS &R)
Matches a UMax with LHS and RHS in either order.
match_bind< WithOverflowInst > m_WithOverflowInst(WithOverflowInst *&I)
Match a with overflow intrinsic, capturing it if we match.
SpecificCmpClass_match< LHS, RHS, ICmpInst > m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
BinaryOp_match< LHS, RHS, Instruction::UDiv > m_UDiv(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty > m_UMax(const LHS &L, const RHS &R)
match_immconstant_ty m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty, true > m_c_UMin(const LHS &L, const RHS &R)
Matches a UMin with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Add, true > m_c_Add(const LHS &L, const RHS &R)
Matches a Add with LHS and RHS in either order.
match_combine_or< BinaryOp_match< LHS, RHS, Instruction::Add >, DisjointOr_match< LHS, RHS > > m_AddLike(const LHS &L, const RHS &R)
Match either "add" or "or disjoint".
match_combine_or< MaxMin_match< FCmpInst, LHS, RHS, ofmax_pred_ty >, MaxMin_match< FCmpInst, LHS, RHS, ufmax_pred_ty > > m_OrdOrUnordFMax(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point maximum function.
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty, true > m_c_SMax(const LHS &L, const RHS &R)
Matches an SMax with LHS and RHS in either order.
CastOperator_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
auto m_c_MaxOrMin(const LHS &L, const RHS &R)
cstfp_pred_ty< custom_checkfn< APFloat > > m_CheckedFp(function_ref< bool(const APFloat &)> CheckFn)
Match a float or vector where CheckFn(ele) for each element is true.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWSub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty > m_SMax(const LHS &L, const RHS &R)
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap >, DisjointOr_match< LHS, RHS > > m_NSWAddLike(const LHS &L, const RHS &R)
Match either "add nsw" or "or disjoint".
m_Intrinsic_Ty< Opnd0 >::Ty m_Ctpop(const Opnd0 &Op0)
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap > m_NSWAdd(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, SExtInst > > m_ZExtOrSExt(const OpTy &Op)
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match 'fneg X' as 'fsub -0.0, X'.
BinOpPred_match< LHS, RHS, is_shift_op > m_Shift(const LHS &L, const RHS &R)
Matches shift operations.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
BinOpPred_match< LHS, RHS, is_irem_op > m_IRem(const LHS &L, const RHS &R)
Matches integer remainder operations.
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
brc_match< Cond_t, match_bind< BasicBlock >, match_bind< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_FMinNum(const Opnd0 &Op0, const Opnd1 &Op1)
cst_pred_ty< is_nonpositive > m_NonPositive()
Match an integer or vector of non-positive values.
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap >, DisjointOr_match< LHS, RHS > > m_NUWAddLike(const LHS &L, const RHS &R)
Match either "add nuw" or "or disjoint".
ElementWiseBitCast_match< OpTy > m_ElementWiseBitCast(const OpTy &Op)
m_Intrinsic_Ty< Opnd0 >::Ty m_FAbs(const Opnd0 &Op0)
BinaryOp_match< LHS, RHS, Instruction::Mul, true > m_c_Mul(const LHS &L, const RHS &R)
Matches a Mul with LHS and RHS in either order.
CastOperator_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > m_UMin(const LHS &L, const RHS &R)
auto m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
static unsigned decodeVSEW(unsigned VSEW)
LLVM_ABI unsigned getSEWLMULRatio(unsigned SEW, VLMUL VLMul)
static constexpr unsigned RVVBitsPerBlock
initializer< Ty > init(const Ty &Val)
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:668
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI bool haveNoCommonBitsSet(const WithCache< const Value * > &LHSCache, const WithCache< const Value * > &RHSCache, const SimplifyQuery &SQ)
Return true if LHS and RHS have no common bits set.
LLVM_ABI bool mustExecuteUBIfPoisonOnPathTo(Instruction *Root, Instruction *OnPathTo, DominatorTree *DT)
Return true if undefined behavior would provable be executed on the path to OnPathTo if Root produced...
LLVM_ABI Intrinsic::ID getInverseMinMaxIntrinsic(Intrinsic::ID MinMaxID)
LLVM_ABI bool willNotFreeBetween(const Instruction *Assume, const Instruction *CtxI)
Returns true, if no instruction between Assume and CtxI may free (including through synchronization).
@ Offset
Definition DWP.cpp:558
@ Length
Definition DWP.cpp:558
@ NeverOverflows
Never overflows.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
LLVM_ABI KnownFPClass computeKnownFPClass(const Value *V, const APInt &DemandedElts, FPClassTest InterestedClasses, const SimplifyQuery &SQ, unsigned Depth=0)
Determine which floating-point classes are valid for V, and return them in KnownFPClass bit sets.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1738
MaybeAlign getAlign(const CallInst &I, unsigned Index)
LLVM_ABI bool isValidAssumeForContext(const Instruction *I, const Instruction *CxtI, const DominatorTree *DT=nullptr, bool AllowEphemerals=false)
Return true if it is valid to use the assumptions provided by an assume intrinsic,...
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1668
LLVM_ABI bool canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
LLVM_ABI bool mustTriggerUB(const Instruction *I, const SmallPtrSetImpl< const Value * > &KnownPoison)
Return true if the given instruction must trigger undefined behavior when I is executed with any oper...
LLVM_ABI bool isKnownNeverInfinity(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point scalar value is not an infinity or if the floating-point vector val...
LLVM_ABI void computeKnownBitsFromContext(const Value *V, KnownBits &Known, const SimplifyQuery &Q, unsigned Depth=0)
Merge bits known from context-dependent facts into Known.
LLVM_ABI bool isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI)
LLVM_ABI bool isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS, bool &TrueIfSigned)
Given an exploded icmp instruction, return true if the comparison only checks the sign bit.
LLVM_ABI bool isAssumeLikeIntrinsic(const Instruction *I)
Return true if it is an intrinsic that cannot be speculated but also cannot trap.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2553
LLVM_ABI AllocaInst * findAllocaForValue(Value *V, bool OffsetZero=false)
Returns unique alloca where the value comes from, or nullptr.
LLVM_ABI APInt getMinMaxLimit(SelectPatternFlavor SPF, unsigned BitWidth)
Return the minimum or maximum constant value for the specified integer min/max flavor and type.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI bool isOnlyUsedInZeroComparison(const Instruction *CxtI)
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
LLVM_ABI bool getConstantStringInfo(const Value *V, StringRef &Str, bool TrimAtNul=true)
This function computes the length of a null-terminated C string pointed to by V.
LLVM_ABI bool isDereferenceableAndAlignedPointer(const Value *V, Type *Ty, Align Alignment, const DataLayout &DL, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Returns true if V is always a dereferenceable pointer with alignment greater or equal than requested.
Definition Loads.cpp:230
LLVM_ABI bool onlyUsedByLifetimeMarkersOrDroppableInsts(const Value *V)
Return true if the only users of this pointer are lifetime markers or droppable instructions.
LLVM_ABI Constant * ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset)
LLVM_ABI Value * stripNullTest(Value *V)
Returns the inner value X if the expression has the form f(X) where f(X) == 0 if and only if X == 0,...
LLVM_ABI bool getUnderlyingObjectsForCodeGen(const Value *V, SmallVectorImpl< Value * > &Objects)
This is a wrapper around getUnderlyingObjects and adds support for basic ptrtoint+arithmetic+inttoptr...
LLVM_ABI std::pair< Intrinsic::ID, bool > canConvertToMinOrMaxIntrinsic(ArrayRef< Value * > VL)
Check if the values in VL are select instructions that can be converted to a min or max (vector) intr...
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ABI bool getConstantDataArrayInfo(const Value *V, ConstantDataArraySlice &Slice, unsigned ElementSize, uint64_t Offset=0)
Returns true if the value V is a pointer into a ConstantDataArray.
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition bit.h:325
LLVM_ABI bool isGuaranteedToExecuteForEveryIteration(const Instruction *I, const Loop *L)
Return true if this function can prove that the instruction I is executed for every iteration of the ...
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2207
LLVM_ABI bool isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(const CallBase *Call, bool MustPreserveOffset)
{launder,strip}.invariant.group returns pointer that aliases its argument, and it only captures point...
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:432
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)
LLVM_ABI const Value * getArgumentAliasingToReturnedPointer(const CallBase *Call, bool MustPreserveOffset)
This function returns call pointer argument that is considered the same by aliasing rules.
int ilogb(const APFloat &Arg)
Returns the exponent of the internal representation of the APFloat.
Definition APFloat.h:1631
LLVM_ABI bool isSafeToSpeculativelyExecute(const Instruction *I, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true, bool IgnoreUBImplyingAttrs=true)
Return true if the instruction does not have any effects besides calculating the result and does not ...
LLVM_ABI Value * getSplatValue(const Value *V)
Get splat value if the input is a splat vector or return nullptr.
LLVM_ABI CmpInst::Predicate getMinMaxPred(SelectPatternFlavor SPF, bool Ordered=false)
Return the canonical comparison predicate for the specified minimum/maximum flavor.
bool isa_and_nonnull(const Y &Val)
Definition Casting.h:676
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:337
LLVM_ABI bool canIgnoreSignBitOfZero(const Use &U)
Return true if the sign bit of the FP value can be ignored by the user when the value is zero.
LLVM_ABI bool isGuaranteedNotToBeUndef(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be undef, but may be poison.
LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
std::tuple< Value *, FPClassTest, FPClassTest > fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS, FPClassTest RHSClass, bool LookThroughSrc=true)
const Value * getPointerOperand(const Value *V)
A helper function that returns the pointer operand of a load, store or GEP instruction.
LLVM_ABI bool MaskedValueIsZero(const Value *V, const APInt &Mask, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if 'V & Mask' is known to be zero.
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:204
LLVM_ABI bool isOverflowIntrinsicNoWrap(const WithOverflowInst *WO, const DominatorTree &DT)
Returns true if the arithmetic part of the WO 's result is used only along the paths control dependen...
LLVM_ABI RetainedKnowledge getKnowledgeFromBundle(AssumeInst &Assume, const CallBase::BundleOpInfo &BOI)
This extracts the Knowledge from an element of an operand bundle.
LLVM_ABI bool matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO, Value *&Start, Value *&Step)
Attempt to match a simple first order recurrence cycle of the form: iv = phi Ty [Start,...
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1745
LLVM_ABI OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ, bool IsNSW=false)
LLVM_ABI bool getShuffleDemandedElts(int SrcWidth, ArrayRef< int > Mask, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS, bool AllowUndefElts=false)
Transform a shuffle mask's output demanded element mask into demanded element masks for the 2 operand...
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:331
LLVM_ABI 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 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.
bool includesPoison(UndefPoisonKind Kind)
Returns true if Kind includes the Poison bit.
Definition UndefPoison.h:27
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...
bool includesUndef(UndefPoisonKind Kind)
Returns true if Kind includes the Undef bit.
Definition UndefPoison.h:33
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)
UndefPoisonKind
Enumeration to track whether we are interested in Undef, Poison, or both.
Definition UndefPoison.h:20
LLVM_ABI Value * isBytewiseValue(Value *V, const DataLayout &DL)
If the specified value can be set by repeating the same byte in memory, return the i8 value that it i...
LLVM_ABI std::optional< std::pair< CmpPredicate, Constant * > > getFlippedStrictnessPredicateAndConstant(CmpPredicate Pred, Constant *C)
Convert an integer comparison with a constant RHS into an equivalent form with the strictness flipped...
LLVM_ABI unsigned ComputeMaxSignificantBits(const Value *Op, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Get the upper bound on bit size for this Value Op as a signed integer.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1946
LLVM_ABI bool isKnownIntegral(const Value *V, const SimplifyQuery &SQ, FastMathFlags FMF)
Return true if the floating-point value V is known to be an integer value.
LLVM_ABI OverflowResult computeOverflowForUnsignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:197
LLVM_ABI bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Return true if the given value is known to have exactly one bit set when defined.
LLVM_ABI std::optional< bool > isImpliedByDomCondition(const Value *Cond, const Instruction *ContextI, const DataLayout &DL)
Return the boolean condition value in the context of the given instruction if it is known based on do...
LLVM_ABI bool isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be poison, but may be undef.
LLVM_ABI void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, KnownBits &Known)
Compute known bits from the range metadata.
LLVM_ABI Value * FindInsertedValue(Value *V, ArrayRef< unsigned > idx_range, std::optional< BasicBlock::iterator > InsertBefore=std::nullopt)
Given an aggregate and an sequence of indices, see if the scalar value indexed is already around as a...
LLVM_ABI bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW=false, bool AllowPoison=true)
Return true if the two given values are negation.
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
LLVM_ABI bool isKnownPositive(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be positive (i.e.
LLVM_ABI Constant * ConstantFoldIntegerCast(Constant *C, Type *DestTy, bool IsSigned, const DataLayout &DL)
Constant fold a zext, sext or trunc, depending on IsSigned and whether the DestTy is wider or narrowe...
LLVM_ABI bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the give value is known to be non-negative.
LLVM_ABI bool cannotBeOrderedLessThanZero(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if we can prove that the specified FP value is either NaN or never less than -0....
LLVM_ABI void getUnderlyingObjects(const Value *V, SmallVectorImpl< const Value * > &Objects, const LoopInfo *LI=nullptr, unsigned MaxLookup=MaxLookupSearchDepth)
This method is similar to getUnderlyingObject except that it can look through phi and select instruct...
LLVM_ABI bool mayHaveNonDefUseDependency(const Instruction &I)
Returns true if the result or effects of the given instructions I depend values not reachable through...
LLVM_ABI bool isTriviallyVectorizable(Intrinsic::ID ID)
Identify if the intrinsic is trivially vectorizable.
LLVM_ABI bool isIdentifiedObject(const Value *V)
Return true if this pointer refers to a distinct and identifiable object.
LLVM_ABI std::optional< bool > isImpliedCondition(const Value *LHS, const Value *RHS, const DataLayout &DL, bool LHSIsTrue=true, unsigned Depth=0)
Return true if RHS is known to be implied true by LHS.
LLVM_ABI std::optional< bool > computeKnownFPSignBit(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return false if we can prove that the specified FP value's sign bit is 0.
LLVM_ABI bool canIgnoreSignBitOfNaN(const Use &U)
Return true if the sign bit of the FP value can be ignored by the user when the value is NaN.
LLVM_ABI ConstantRange computeConstantRange(const Value *V, bool ForSigned, const SimplifyQuery &SQ, unsigned Depth=0)
Determine the possible constant range of an integer or vector of integer value.
LLVM_ABI void findValuesAffectedByCondition(Value *Cond, bool IsAssume, function_ref< void(Value *)> InsertAffected)
Call InsertAffected on all Values whose known bits / value may be affected by the condition Cond.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:876
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
SmallPtrSet< Value *, 4 > AffectedValues
Represents offset+length into a ConstantDataArray.
const ConstantDataArray * Array
ConstantDataArray pointer.
Represent subnormal handling kind for floating point instruction inputs and outputs.
static constexpr DenormalMode getDynamic()
InstrInfoQuery provides an interface to query additional information for instructions like metadata o...
bool isExact(const BinaryOperator *Op) const
MDNode * getMetadata(const Instruction *I, unsigned KindID) const
bool hasNoSignedZeros(const InstT *Op) const
bool hasNoSignedWrap(const InstT *Op) const
bool hasNoUnsignedWrap(const InstT *Op) const
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition KnownBits.h:315
static LLVM_ABI KnownBits sadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)
KnownBits anyextOrTrunc(unsigned BitWidth) const
Return known bits for an "any" extension or truncation of the value we're tracking.
Definition KnownBits.h:190
static LLVM_ABI KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition KnownBits.h:269
static LLVM_ABI KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition KnownBits.h:106
bool isZero() const
Returns true if value is all zero.
Definition KnownBits.h:78
LLVM_ABI KnownBits blsi() const
Compute known bits for X & -X, which has only the lowest bit set of X set.
void makeNonNegative()
Make this value non-negative.
Definition KnownBits.h:125
static LLVM_ABI KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
unsigned countMinLeadingOnes() const
Returns the minimum number of leading one bits.
Definition KnownBits.h:265
LLVM_ABI KnownBits reduceAdd(unsigned NumElts) const
Compute known bits for horizontal add for a vector with NumElts elements, where each element has the ...
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition KnownBits.h:256
static LLVM_ABI KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
static LLVM_ABI KnownBits ssub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.ssub.sat(LHS, RHS)
static LLVM_ABI KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
bool isUnknown() const
Returns true if we don't know any bits.
Definition KnownBits.h:64
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition KnownBits.h:288
LLVM_ABI KnownBits blsmsk() const
Compute known bits for X ^ (X - 1), which has all bits up to and including the lowest set bit of X se...
void makeNegative()
Make this value negative.
Definition KnownBits.h:120
void setAllConflict()
Make all bits known to be both zero and one.
Definition KnownBits.h:97
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition KnownBits.h:165
KnownBits byteSwap() const
Definition KnownBits.h:553
bool hasConflict() const
Returns true if there is conflicting information.
Definition KnownBits.h:51
static LLVM_ABI KnownBits fshl(const KnownBits &LHS, const KnownBits &RHS, const APInt &Amt)
Compute known bits for fshl(LHS, RHS, Amt).
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition KnownBits.h:303
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition KnownBits.h:84
KnownBits reverseBits() const
Definition KnownBits.h:557
unsigned getBitWidth() const
Get the bit width of this value.
Definition KnownBits.h:44
static LLVM_ABI KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition KnownBits.h:176
bool isConstant() const
Returns true if we know the value of all bits.
Definition KnownBits.h:54
void resetAll()
Resets the known state of all bits.
Definition KnownBits.h:72
static KnownBits add(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false, bool SelfAdd=false)
Compute knownbits resulting from addition of LHS and RHS.
Definition KnownBits.h:361
KnownBits unionWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for either this or RHS or both.
Definition KnownBits.h:335
static LLVM_ABI KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition KnownBits.h:109
bool isEven() const
Return if the value is known even (the low bit is 0).
Definition KnownBits.h:162
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition KnownBits.h:239
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition KnownBits.h:325
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition KnownBits.h:184
unsigned countMinTrailingOnes() const
Returns the minimum number of trailing one bits.
Definition KnownBits.h:259
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition KnownBits.h:200
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition KnownBits.h:262
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:146
static LLVM_ABI KnownBits fshr(const KnownBits &LHS, const KnownBits &RHS, const APInt &Amt)
Compute known bits for fshr(LHS, RHS, Amt).
static LLVM_ABI KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
static LLVM_ABI KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
static LLVM_ABI KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
static LLVM_ABI KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
APInt getMinValue() const
Return the minimal unsigned value possible given these KnownBits.
Definition KnownBits.h:130
static LLVM_ABI KnownBits computeForAddSub(bool Add, bool NSW, bool NUW, const KnownBits &LHS, const KnownBits &RHS)
Compute known bits resulting from adding LHS and RHS.
Definition KnownBits.cpp:61
static LLVM_ABI KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition KnownBits.h:340
bool isNegative() const
Returns true if this value is known to be negative.
Definition KnownBits.h:103
static KnownBits sub(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from subtraction of LHS and RHS.
Definition KnownBits.h:376
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition KnownBits.h:294
void setAllOnes()
Make all bits known to be one and discard any previous information.
Definition KnownBits.h:90
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition KnownBits.h:233
static LLVM_ABI KnownBits uadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.uadd.sat(LHS, RHS)
static LLVM_ABI KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
KnownBits anyext(unsigned BitWidth) const
Return known bits for an "any" extension of the value we're tracking, where we don't know anything ab...
Definition KnownBits.h:171
static LLVM_ABI KnownBits clmul(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for clmul(LHS, RHS).
LLVM_ABI KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
static LLVM_ABI std::optional< bool > sgt(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SGT result.
static LLVM_ABI std::optional< bool > uge(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_UGE result.
static LLVM_ABI KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
static LLVM_ABI KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
KnownBits sextOrTrunc(unsigned BitWidth) const
Return known bits for a sign extension or truncation of the value we're tracking.
Definition KnownBits.h:210
bool isKnownNeverInfOrNaN() const
Return true if it's known this can never be an infinity or nan.
FPClassTest KnownFPClasses
Floating-point classes the value could be one of.
bool isKnownNeverInfinity() const
Return true if it's known this can never be an infinity.
bool cannotBeOrderedGreaterThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never greater tha...
static LLVM_ABI KnownFPClass sin(const KnownFPClass &Src)
Report known values for sin.
static LLVM_ABI KnownFPClass fdiv_self(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fdiv x, x.
static constexpr FPClassTest OrderedGreaterThanZeroMask
static constexpr FPClassTest OrderedLessThanZeroMask
void knownNot(FPClassTest RuleOut)
static LLVM_ABI KnownFPClass fmul(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fmul.
static LLVM_ABI KnownFPClass fadd_self(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fadd x, x.
void copysign(const KnownFPClass &Sign)
static KnownFPClass square(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
static LLVM_ABI KnownFPClass fsub(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fsub.
bool isKnownNeverSubnormal() const
Return true if it's known this can never be a subnormal.
KnownFPClass unionWith(const KnownFPClass &RHS) const
static LLVM_ABI KnownFPClass canonicalize(const KnownFPClass &Src, DenormalMode DenormMode=DenormalMode::getDynamic())
Apply the canonicalize intrinsic to this value.
LLVM_ABI bool isKnownNeverLogicalZero(DenormalMode Mode) const
Return true if it's known this can never be interpreted as a zero.
static LLVM_ABI KnownFPClass log(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for log/log2/log10.
static LLVM_ABI KnownFPClass atan(const KnownFPClass &Src)
Report known values for atan.
static LLVM_ABI KnownFPClass atan2(const KnownFPClass &LHS, const KnownFPClass &RHS)
Report known values for atan2.
static LLVM_ABI KnownFPClass fdiv(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fdiv.
static LLVM_ABI KnownFPClass roundToIntegral(const KnownFPClass &Src, bool IsTrunc, bool IsMultiUnitFPType)
Propagate known class for rounding intrinsics (trunc, floor, ceil, rint, nearbyint,...
static LLVM_ABI KnownFPClass cos(const KnownFPClass &Src)
Report known values for cos.
static LLVM_ABI KnownFPClass ldexp(const KnownFPClass &Src, const KnownBits &N, const fltSemantics &Flt, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for ldexp.
static LLVM_ABI KnownFPClass cosh(const KnownFPClass &Src)
Report known values for cosh.
static LLVM_ABI KnownFPClass minMaxLike(const KnownFPClass &LHS, const KnownFPClass &RHS, MinMaxKind Kind, DenormalMode DenormMode=DenormalMode::getDynamic())
bool isUnknown() const
KnownFPClass intersectWith(const KnownFPClass &RHS) const
static LLVM_ABI KnownFPClass exp(const KnownFPClass &Src)
Report known values for exp, exp2 and exp10.
static LLVM_ABI KnownFPClass frexp_mant(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for mantissa component of frexp.
std::optional< bool > SignBit
std::nullopt if the sign bit is unknown, true if the sign bit is definitely set or false if the sign ...
static LLVM_ABI KnownFPClass asin(const KnownFPClass &Src)
Report known values for asin.
bool isKnownNeverNaN() const
Return true if it's known this can never be a nan.
bool isKnownNever(FPClassTest Mask) const
Return true if it's known this can never be one of the mask entries.
static LLVM_ABI KnownFPClass fpext(const KnownFPClass &KnownSrc, const fltSemantics &DstTy, const fltSemantics &SrcTy)
Propagate known class for fpext.
bool isKnownNeverNegZero() const
Return true if it's known this can never be a negative zero.
static LLVM_ABI KnownFPClass fma(const KnownFPClass &LHS, const KnownFPClass &RHS, const KnownFPClass &Addend, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fma.
static LLVM_ABI KnownFPClass tan(const KnownFPClass &Src)
Report known values for tan.
void propagateNaN(const KnownFPClass &Src, bool PreserveSign=false)
static LLVM_ABI KnownFPClass fptrunc(const KnownFPClass &KnownSrc)
Propagate known class for fptrunc.
bool cannotBeOrderedLessThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never less than -...
void signBitMustBeOne()
Assume the sign bit is one.
void signBitMustBeZero()
Assume the sign bit is zero.
static LLVM_ABI KnownFPClass sqrt(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for sqrt.
LLVM_ABI bool isKnownNeverLogicalPosZero(DenormalMode Mode) const
Return true if it's known this can never be interpreted as a positive zero.
bool isKnownNeverPosInfinity() const
Return true if it's known this can never be +infinity.
static LLVM_ABI KnownFPClass fadd(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fadd.
LLVM_ABI bool isKnownNeverLogicalNegZero(DenormalMode Mode) const
Return true if it's known this can never be interpreted as a negative zero.
static LLVM_ABI KnownFPClass bitcast(const fltSemantics &FltSemantics, const KnownBits &Bits)
Report known values for a bitcast into a float with provided semantics.
static LLVM_ABI KnownFPClass fma_square(const KnownFPClass &Squared, const KnownFPClass &Addend, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fma squared, squared, addend.
static LLVM_ABI KnownFPClass acos(const KnownFPClass &Src)
Report known values for acos.
static LLVM_ABI KnownFPClass frem_self(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for frem.
static LLVM_ABI KnownFPClass powi(const KnownFPClass &Src, const KnownBits &N)
Propagate known class for powi.
static LLVM_ABI KnownFPClass sinh(const KnownFPClass &Src)
Report known values for sinh.
static LLVM_ABI KnownFPClass tanh(const KnownFPClass &Src)
Report known values for tanh.
Represent one information held inside an operand bundle of an llvm.assume.
SelectPatternFlavor Flavor
static bool isMinOrMax(SelectPatternFlavor SPF)
When implementing this min/max pattern as fcmp; select, does the fcmp have to be ordered?
const DataLayout & DL
SimplifyQuery getWithoutCondContext() const
const Instruction * CxtI
const DominatorTree * DT
SimplifyQuery getWithInstruction(const Instruction *I) const
AssumptionCache * AC
const DomConditionCache * DC
const InstrInfoQuery IIQ
const CondContext * CC