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