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(Call, true))
3540 return isKnownNonZero(RP, Q, Depth);
3541 } else {
3542 if (MDNode *Ranges = Q.IIQ.getMetadata(Call, LLVMContext::MD_range))
3544 if (std::optional<ConstantRange> Range = Call->getRange()) {
3545 const APInt ZeroValue(Range->getBitWidth(), 0);
3546 if (!Range->contains(ZeroValue))
3547 return true;
3548 }
3549 if (const Value *RV = Call->getReturnedArgOperand())
3550 if (RV->getType() == I->getType() && isKnownNonZero(RV, Q, Depth))
3551 return true;
3552 }
3553
3554 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
3555 switch (II->getIntrinsicID()) {
3556 case Intrinsic::sshl_sat:
3557 case Intrinsic::ushl_sat:
3558 case Intrinsic::abs:
3559 case Intrinsic::bitreverse:
3560 case Intrinsic::bswap:
3561 case Intrinsic::ctpop:
3562 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3563 // NB: We don't do usub_sat here as in any case we can prove its
3564 // non-zero, we will fold it to `sub nuw` in InstCombine.
3565 case Intrinsic::ssub_sat:
3566 // For most types, if x != y then ssub.sat x, y != 0. But
3567 // ssub.sat.i1 0, -1 = 0, because 1 saturates to 0. This means
3568 // isNonZeroSub will do the wrong thing for ssub.sat.i1.
3569 if (BitWidth == 1)
3570 return false;
3571 return isNonZeroSub(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3572 II->getArgOperand(1), Depth);
3573 case Intrinsic::sadd_sat:
3574 return isNonZeroAdd(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3575 II->getArgOperand(1),
3576 /*NSW=*/true, /* NUW=*/false, Depth);
3577 // Vec reverse preserves zero/non-zero status from input vec.
3578 case Intrinsic::vector_reverse:
3579 return isKnownNonZero(II->getArgOperand(0), DemandedElts.reverseBits(),
3580 Q, Depth);
3581 // umin/smin/smax/smin/or of all non-zero elements is always non-zero.
3582 case Intrinsic::vector_reduce_or:
3583 case Intrinsic::vector_reduce_umax:
3584 case Intrinsic::vector_reduce_umin:
3585 case Intrinsic::vector_reduce_smax:
3586 case Intrinsic::vector_reduce_smin:
3587 return isKnownNonZero(II->getArgOperand(0), Q, Depth);
3588 case Intrinsic::umax:
3589 case Intrinsic::uadd_sat:
3590 // umax(X, (X != 0)) is non zero
3591 // X +usat (X != 0) is non zero
3592 if (matchOpWithOpEqZero(II->getArgOperand(0), II->getArgOperand(1)))
3593 return true;
3594
3595 return isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth) ||
3596 isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3597 case Intrinsic::smax: {
3598 // If either arg is strictly positive the result is non-zero. Otherwise
3599 // the result is non-zero if both ops are non-zero.
3600 auto IsNonZero = [&](Value *Op, std::optional<bool> &OpNonZero,
3601 const KnownBits &OpKnown) {
3602 if (!OpNonZero.has_value())
3603 OpNonZero = OpKnown.isNonZero() ||
3604 isKnownNonZero(Op, DemandedElts, Q, Depth);
3605 return *OpNonZero;
3606 };
3607 // Avoid re-computing isKnownNonZero.
3608 std::optional<bool> Op0NonZero, Op1NonZero;
3609 KnownBits Op1Known =
3610 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3611 if (Op1Known.isNonNegative() &&
3612 IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known))
3613 return true;
3614 KnownBits Op0Known =
3615 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3616 if (Op0Known.isNonNegative() &&
3617 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known))
3618 return true;
3619 return IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known) &&
3620 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known);
3621 }
3622 case Intrinsic::smin: {
3623 // If either arg is negative the result is non-zero. Otherwise
3624 // the result is non-zero if both ops are non-zero.
3625 KnownBits Op1Known =
3626 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3627 if (Op1Known.isNegative())
3628 return true;
3629 KnownBits Op0Known =
3630 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3631 if (Op0Known.isNegative())
3632 return true;
3633
3634 if (Op1Known.isNonZero() && Op0Known.isNonZero())
3635 return true;
3636 }
3637 [[fallthrough]];
3638 case Intrinsic::umin:
3639 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth) &&
3640 isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth);
3641 case Intrinsic::cttz:
3642 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3643 .Zero[0];
3644 case Intrinsic::ctlz:
3645 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3646 .isNonNegative();
3647 case Intrinsic::fshr:
3648 case Intrinsic::fshl:
3649 // If Op0 == Op1, this is a rotate. rotate(x, y) != 0 iff x != 0.
3650 if (II->getArgOperand(0) == II->getArgOperand(1))
3651 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3652 break;
3653 case Intrinsic::vscale:
3654 return true;
3655 case Intrinsic::experimental_get_vector_length:
3656 return isKnownNonZero(I->getOperand(0), Q, Depth);
3657 default:
3658 break;
3659 }
3660 break;
3661 }
3662
3663 return false;
3664 }
3665 }
3666
3667 KnownBits Known(BitWidth);
3668 computeKnownBits(I, DemandedElts, Known, Q, Depth);
3669 return Known.One != 0;
3670}
3671
3672/// Return true if the given value is known to be non-zero when defined. For
3673/// vectors, return true if every demanded element is known to be non-zero when
3674/// defined. For pointers, if the context instruction and dominator tree are
3675/// specified, perform context-sensitive analysis and return true if the
3676/// pointer couldn't possibly be null at the specified instruction.
3677/// Supports values with integer or pointer type and vectors of integers.
3678bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
3679 const SimplifyQuery &Q, unsigned Depth) {
3680 Type *Ty = V->getType();
3681
3682#ifndef NDEBUG
3683 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
3684
3685 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3686 assert(
3687 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
3688 "DemandedElt width should equal the fixed vector number of elements");
3689 } else {
3690 assert(DemandedElts == APInt(1, 1) &&
3691 "DemandedElt width should be 1 for scalars");
3692 }
3693#endif
3694
3695 if (auto *C = dyn_cast<Constant>(V)) {
3696 if (C->isNullValue())
3697 return false;
3698 if (isa<ConstantInt>(C))
3699 // Must be non-zero due to null test above.
3700 return true;
3701
3702 // For constant vectors, check that all elements are poison or known
3703 // non-zero to determine that the whole vector is known non-zero.
3704 if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
3705 for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
3706 if (!DemandedElts[i])
3707 continue;
3708 Constant *Elt = C->getAggregateElement(i);
3709 if (!Elt || Elt->isNullValue())
3710 return false;
3711 if (!isa<PoisonValue>(Elt) && !isa<ConstantInt>(Elt))
3712 return false;
3713 }
3714 return true;
3715 }
3716
3717 // Constant ptrauth can be null, iff the base pointer can be.
3718 if (auto *CPA = dyn_cast<ConstantPtrAuth>(V))
3719 return isKnownNonZero(CPA->getPointer(), DemandedElts, Q, Depth);
3720
3721 // A global variable in address space 0 is non null unless extern weak
3722 // or an absolute symbol reference. Other address spaces may have null as a
3723 // valid address for a global, so we can't assume anything.
3724 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
3725 if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&
3726 GV->getType()->getAddressSpace() == 0)
3727 return true;
3728 }
3729
3730 // For constant expressions, fall through to the Operator code below.
3731 if (!isa<ConstantExpr>(V))
3732 return false;
3733 }
3734
3735 if (const auto *A = dyn_cast<Argument>(V))
3736 if (std::optional<ConstantRange> Range = A->getRange()) {
3737 const APInt ZeroValue(Range->getBitWidth(), 0);
3738 if (!Range->contains(ZeroValue))
3739 return true;
3740 }
3741
3742 if (!isa<Constant>(V) && isKnownNonZeroFromAssume(V, Q))
3743 return true;
3744
3745 // Some of the tests below are recursive, so bail out if we hit the limit.
3747 return false;
3748
3749 // Check for pointer simplifications.
3750
3751 if (PointerType *PtrTy = dyn_cast<PointerType>(Ty)) {
3752 // A byval, inalloca may not be null in a non-default addres space. A
3753 // nonnull argument is assumed never 0.
3754 if (const Argument *A = dyn_cast<Argument>(V)) {
3755 if (((A->hasPassPointeeByValueCopyAttr() &&
3756 !NullPointerIsDefined(A->getParent(), PtrTy->getAddressSpace())) ||
3757 A->hasNonNullAttr()))
3758 return true;
3759 }
3760 }
3761
3762 if (const auto *I = dyn_cast<Operator>(V))
3763 if (isKnownNonZeroFromOperator(I, DemandedElts, Q, Depth))
3764 return true;
3765
3766 if (!isa<Constant>(V) &&
3768 return true;
3769
3770 if (const Value *Stripped = stripNullTest(V))
3771 return isKnownNonZero(Stripped, DemandedElts, Q, Depth);
3772
3773 return false;
3774}
3775
3777 unsigned Depth) {
3778 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
3779 APInt DemandedElts =
3780 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
3781 return ::isKnownNonZero(V, DemandedElts, Q, Depth);
3782}
3783
3784/// If the pair of operators are the same invertible function, return the
3785/// the operands of the function corresponding to each input. Otherwise,
3786/// return std::nullopt. An invertible function is one that is 1-to-1 and maps
3787/// every input value to exactly one output value. This is equivalent to
3788/// saying that Op1 and Op2 are equal exactly when the specified pair of
3789/// operands are equal, (except that Op1 and Op2 may be poison more often.)
3790static std::optional<std::pair<Value*, Value*>>
3792 const Operator *Op2) {
3793 if (Op1->getOpcode() != Op2->getOpcode())
3794 return std::nullopt;
3795
3796 auto getOperands = [&](unsigned OpNum) -> auto {
3797 return std::make_pair(Op1->getOperand(OpNum), Op2->getOperand(OpNum));
3798 };
3799
3800 switch (Op1->getOpcode()) {
3801 default:
3802 break;
3803 case Instruction::Or:
3804 if (!cast<PossiblyDisjointInst>(Op1)->isDisjoint() ||
3805 !cast<PossiblyDisjointInst>(Op2)->isDisjoint())
3806 break;
3807 [[fallthrough]];
3808 case Instruction::Xor:
3809 case Instruction::Add: {
3810 Value *Other;
3811 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(0)), m_Value(Other))))
3812 return std::make_pair(Op1->getOperand(1), Other);
3813 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(1)), m_Value(Other))))
3814 return std::make_pair(Op1->getOperand(0), Other);
3815 break;
3816 }
3817 case Instruction::Sub:
3818 if (Op1->getOperand(0) == Op2->getOperand(0))
3819 return getOperands(1);
3820 if (Op1->getOperand(1) == Op2->getOperand(1))
3821 return getOperands(0);
3822 break;
3823 case Instruction::Mul: {
3824 // invertible if A * B == (A * B) mod 2^N where A, and B are integers
3825 // and N is the bitwdith. The nsw case is non-obvious, but proven by
3826 // alive2: https://alive2.llvm.org/ce/z/Z6D5qK
3827 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3828 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3829 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3830 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3831 break;
3832
3833 // Assume operand order has been canonicalized
3834 if (Op1->getOperand(1) == Op2->getOperand(1) &&
3835 isa<ConstantInt>(Op1->getOperand(1)) &&
3836 !cast<ConstantInt>(Op1->getOperand(1))->isZero())
3837 return getOperands(0);
3838 break;
3839 }
3840 case Instruction::Shl: {
3841 // Same as multiplies, with the difference that we don't need to check
3842 // for a non-zero multiply. Shifts always multiply by non-zero.
3843 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3844 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3845 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3846 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3847 break;
3848
3849 if (Op1->getOperand(1) == Op2->getOperand(1))
3850 return getOperands(0);
3851 break;
3852 }
3853 case Instruction::AShr:
3854 case Instruction::LShr: {
3855 auto *PEO1 = cast<PossiblyExactOperator>(Op1);
3856 auto *PEO2 = cast<PossiblyExactOperator>(Op2);
3857 if (!PEO1->isExact() || !PEO2->isExact())
3858 break;
3859
3860 if (Op1->getOperand(1) == Op2->getOperand(1))
3861 return getOperands(0);
3862 break;
3863 }
3864 case Instruction::SExt:
3865 case Instruction::ZExt:
3866 if (Op1->getOperand(0)->getType() == Op2->getOperand(0)->getType())
3867 return getOperands(0);
3868 break;
3869 case Instruction::PHI: {
3870 const PHINode *PN1 = cast<PHINode>(Op1);
3871 const PHINode *PN2 = cast<PHINode>(Op2);
3872
3873 // If PN1 and PN2 are both recurrences, can we prove the entire recurrences
3874 // are a single invertible function of the start values? Note that repeated
3875 // application of an invertible function is also invertible
3876 BinaryOperator *BO1 = nullptr;
3877 Value *Start1 = nullptr, *Step1 = nullptr;
3878 BinaryOperator *BO2 = nullptr;
3879 Value *Start2 = nullptr, *Step2 = nullptr;
3880 if (PN1->getParent() != PN2->getParent() ||
3881 !matchSimpleRecurrence(PN1, BO1, Start1, Step1) ||
3882 !matchSimpleRecurrence(PN2, BO2, Start2, Step2))
3883 break;
3884
3885 auto Values = getInvertibleOperands(cast<Operator>(BO1),
3886 cast<Operator>(BO2));
3887 if (!Values)
3888 break;
3889
3890 // We have to be careful of mutually defined recurrences here. Ex:
3891 // * X_i = X_(i-1) OP Y_(i-1), and Y_i = X_(i-1) OP V
3892 // * X_i = Y_i = X_(i-1) OP Y_(i-1)
3893 // The invertibility of these is complicated, and not worth reasoning
3894 // about (yet?).
3895 if (Values->first != PN1 || Values->second != PN2)
3896 break;
3897
3898 return std::make_pair(Start1, Start2);
3899 }
3900 }
3901 return std::nullopt;
3902}
3903
3904/// Return true if V1 == (binop V2, X), where X is known non-zero.
3905/// Only handle a small subset of binops where (binop V2, X) with non-zero X
3906/// implies V2 != V1.
3907static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2,
3908 const APInt &DemandedElts,
3909 const SimplifyQuery &Q, unsigned Depth) {
3911 if (!BO)
3912 return false;
3913 switch (BO->getOpcode()) {
3914 default:
3915 break;
3916 case Instruction::Or:
3917 if (!cast<PossiblyDisjointInst>(V1)->isDisjoint())
3918 break;
3919 [[fallthrough]];
3920 case Instruction::Xor:
3921 case Instruction::Add:
3922 Value *Op = nullptr;
3923 if (V2 == BO->getOperand(0))
3924 Op = BO->getOperand(1);
3925 else if (V2 == BO->getOperand(1))
3926 Op = BO->getOperand(0);
3927 else
3928 return false;
3929 return isKnownNonZero(Op, DemandedElts, Q, Depth + 1);
3930 }
3931 return false;
3932}
3933
3934/// Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and
3935/// the multiplication is nuw or nsw.
3936static bool isNonEqualMul(const Value *V1, const Value *V2,
3937 const APInt &DemandedElts, const SimplifyQuery &Q,
3938 unsigned Depth) {
3939 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3940 const APInt *C;
3941 return match(OBO, m_Mul(m_Specific(V1), m_APInt(C))) &&
3942 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3943 !C->isZero() && !C->isOne() &&
3944 isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3945 }
3946 return false;
3947}
3948
3949/// Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and
3950/// the shift is nuw or nsw.
3951static bool isNonEqualShl(const Value *V1, const Value *V2,
3952 const APInt &DemandedElts, const SimplifyQuery &Q,
3953 unsigned Depth) {
3954 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3955 const APInt *C;
3956 return match(OBO, m_Shl(m_Specific(V1), m_APInt(C))) &&
3957 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3958 !C->isZero() && isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3959 }
3960 return false;
3961}
3962
3963static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2,
3964 const APInt &DemandedElts, const SimplifyQuery &Q,
3965 unsigned Depth) {
3966 // Check two PHIs are in same block.
3967 if (PN1->getParent() != PN2->getParent())
3968 return false;
3969
3971 bool UsedFullRecursion = false;
3972 for (const BasicBlock *IncomBB : PN1->blocks()) {
3973 if (!VisitedBBs.insert(IncomBB).second)
3974 continue; // Don't reprocess blocks that we have dealt with already.
3975 const Value *IV1 = PN1->getIncomingValueForBlock(IncomBB);
3976 const Value *IV2 = PN2->getIncomingValueForBlock(IncomBB);
3977 const APInt *C1, *C2;
3978 if (match(IV1, m_APInt(C1)) && match(IV2, m_APInt(C2)) && *C1 != *C2)
3979 continue;
3980
3981 // Only one pair of phi operands is allowed for full recursion.
3982 if (UsedFullRecursion)
3983 return false;
3984
3986 RecQ.CxtI = IncomBB->getTerminator();
3987 if (!isKnownNonEqual(IV1, IV2, DemandedElts, RecQ, Depth + 1))
3988 return false;
3989 UsedFullRecursion = true;
3990 }
3991 return true;
3992}
3993
3994static bool isNonEqualSelect(const Value *V1, const Value *V2,
3995 const APInt &DemandedElts, const SimplifyQuery &Q,
3996 unsigned Depth) {
3997 const SelectInst *SI1 = dyn_cast<SelectInst>(V1);
3998 if (!SI1)
3999 return false;
4000
4001 if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2)) {
4002 const Value *Cond1 = SI1->getCondition();
4003 const Value *Cond2 = SI2->getCondition();
4004 if (Cond1 == Cond2)
4005 return isKnownNonEqual(SI1->getTrueValue(), SI2->getTrueValue(),
4006 DemandedElts, Q, Depth + 1) &&
4007 isKnownNonEqual(SI1->getFalseValue(), SI2->getFalseValue(),
4008 DemandedElts, Q, Depth + 1);
4009 }
4010 return isKnownNonEqual(SI1->getTrueValue(), V2, DemandedElts, Q, Depth + 1) &&
4011 isKnownNonEqual(SI1->getFalseValue(), V2, DemandedElts, Q, Depth + 1);
4012}
4013
4014// Check to see if A is both a GEP and is the incoming value for a PHI in the
4015// loop, and B is either a ptr or another GEP. If the PHI has 2 incoming values,
4016// one of them being the recursive GEP A and the other a ptr at same base and at
4017// the same/higher offset than B we are only incrementing the pointer further in
4018// loop if offset of recursive GEP is greater than 0.
4020 const SimplifyQuery &Q) {
4021 if (!A->getType()->isPointerTy() || !B->getType()->isPointerTy())
4022 return false;
4023
4024 auto *GEPA = dyn_cast<GEPOperator>(A);
4025 if (!GEPA || GEPA->getNumIndices() != 1 || !isa<Constant>(GEPA->idx_begin()))
4026 return false;
4027
4028 // Handle 2 incoming PHI values with one being a recursive GEP.
4029 auto *PN = dyn_cast<PHINode>(GEPA->getPointerOperand());
4030 if (!PN || PN->getNumIncomingValues() != 2)
4031 return false;
4032
4033 // Search for the recursive GEP as an incoming operand, and record that as
4034 // Step.
4035 Value *Start = nullptr;
4036 Value *Step = const_cast<Value *>(A);
4037 if (PN->getIncomingValue(0) == Step)
4038 Start = PN->getIncomingValue(1);
4039 else if (PN->getIncomingValue(1) == Step)
4040 Start = PN->getIncomingValue(0);
4041 else
4042 return false;
4043
4044 // Other incoming node base should match the B base.
4045 // StartOffset >= OffsetB && StepOffset > 0?
4046 // StartOffset <= OffsetB && StepOffset < 0?
4047 // Is non-equal if above are true.
4048 // We use stripAndAccumulateInBoundsConstantOffsets to restrict the
4049 // optimisation to inbounds GEPs only.
4050 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(Start->getType());
4051 APInt StartOffset(IndexWidth, 0);
4052 Start = Start->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StartOffset);
4053 APInt StepOffset(IndexWidth, 0);
4054 Step = Step->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StepOffset);
4055
4056 // Check if Base Pointer of Step matches the PHI.
4057 if (Step != PN)
4058 return false;
4059 APInt OffsetB(IndexWidth, 0);
4060 B = B->stripAndAccumulateInBoundsConstantOffsets(Q.DL, OffsetB);
4061 return Start == B &&
4062 ((StartOffset.sge(OffsetB) && StepOffset.isStrictlyPositive()) ||
4063 (StartOffset.sle(OffsetB) && StepOffset.isNegative()));
4064}
4065
4066static bool isKnownNonEqualFromContext(const Value *V1, const Value *V2,
4067 const SimplifyQuery &Q, unsigned Depth) {
4068 if (!Q.CxtI)
4069 return false;
4070
4071 // Try to infer NonEqual based on information from dominating conditions.
4072 if (Q.DC && Q.DT) {
4073 auto IsKnownNonEqualFromDominatingCondition = [&](const Value *V) {
4074 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
4075 Value *Cond = BI->getCondition();
4076 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4077 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()) &&
4079 /*LHSIsTrue=*/true, Depth)
4080 .value_or(false))
4081 return true;
4082
4083 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4084 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()) &&
4086 /*LHSIsTrue=*/false, Depth)
4087 .value_or(false))
4088 return true;
4089 }
4090
4091 return false;
4092 };
4093
4094 if (IsKnownNonEqualFromDominatingCondition(V1) ||
4095 IsKnownNonEqualFromDominatingCondition(V2))
4096 return true;
4097 }
4098
4099 if (!Q.AC)
4100 return false;
4101
4102 // Try to infer NonEqual based on information from assumptions.
4103 for (auto &AssumeVH : Q.AC->assumptionsFor(V1)) {
4104 if (!AssumeVH)
4105 continue;
4106 CallInst *I = cast<CallInst>(AssumeVH);
4107
4108 assert(I->getFunction() == Q.CxtI->getFunction() &&
4109 "Got assumption for the wrong function!");
4110 assert(I->getIntrinsicID() == Intrinsic::assume &&
4111 "must be an assume intrinsic");
4112
4113 if (isImpliedCondition(I->getArgOperand(0), ICmpInst::ICMP_NE, V1, V2, Q.DL,
4114 /*LHSIsTrue=*/true, Depth)
4115 .value_or(false) &&
4117 return true;
4118 }
4119
4120 return false;
4121}
4122
4123/// Return true if it is known that V1 != V2.
4124static bool isKnownNonEqual(const Value *V1, const Value *V2,
4125 const APInt &DemandedElts, const SimplifyQuery &Q,
4126 unsigned Depth) {
4127 if (V1 == V2)
4128 return false;
4129 if (V1->getType() != V2->getType())
4130 // We can't look through casts yet.
4131 return false;
4132
4134 return false;
4135
4136 // See if we can recurse through (exactly one of) our operands. This
4137 // requires our operation be 1-to-1 and map every input value to exactly
4138 // one output value. Such an operation is invertible.
4139 auto *O1 = dyn_cast<Operator>(V1);
4140 auto *O2 = dyn_cast<Operator>(V2);
4141 if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) {
4142 if (auto Values = getInvertibleOperands(O1, O2))
4143 return isKnownNonEqual(Values->first, Values->second, DemandedElts, Q,
4144 Depth + 1);
4145
4146 if (const PHINode *PN1 = dyn_cast<PHINode>(V1)) {
4147 const PHINode *PN2 = cast<PHINode>(V2);
4148 // FIXME: This is missing a generalization to handle the case where one is
4149 // a PHI and another one isn't.
4150 if (isNonEqualPHIs(PN1, PN2, DemandedElts, Q, Depth))
4151 return true;
4152 };
4153 }
4154
4155 if (isModifyingBinopOfNonZero(V1, V2, DemandedElts, Q, Depth) ||
4156 isModifyingBinopOfNonZero(V2, V1, DemandedElts, Q, Depth))
4157 return true;
4158
4159 if (isNonEqualMul(V1, V2, DemandedElts, Q, Depth) ||
4160 isNonEqualMul(V2, V1, DemandedElts, Q, Depth))
4161 return true;
4162
4163 if (isNonEqualShl(V1, V2, DemandedElts, Q, Depth) ||
4164 isNonEqualShl(V2, V1, DemandedElts, Q, Depth))
4165 return true;
4166
4167 if (V1->getType()->isIntOrIntVectorTy()) {
4168 // Are any known bits in V1 contradictory to known bits in V2? If V1
4169 // has a known zero where V2 has a known one, they must not be equal.
4170 KnownBits Known1 = computeKnownBits(V1, DemandedElts, Q, Depth);
4171 if (!Known1.isUnknown()) {
4172 KnownBits Known2 = computeKnownBits(V2, DemandedElts, Q, Depth);
4173 if (Known1.Zero.intersects(Known2.One) ||
4174 Known2.Zero.intersects(Known1.One))
4175 return true;
4176 }
4177 }
4178
4179 if (isNonEqualSelect(V1, V2, DemandedElts, Q, Depth) ||
4180 isNonEqualSelect(V2, V1, DemandedElts, Q, Depth))
4181 return true;
4182
4183 if (isNonEqualPointersWithRecursiveGEP(V1, V2, Q) ||
4185 return true;
4186
4187 Value *A, *B;
4188 // PtrToInts are NonEqual if their Ptrs are NonEqual.
4189 // Check PtrToInt type matches the pointer size.
4190 if (match(V1, m_PtrToIntSameSize(Q.DL, m_Value(A))) &&
4192 return isKnownNonEqual(A, B, DemandedElts, Q, Depth + 1);
4193
4194 if (isKnownNonEqualFromContext(V1, V2, Q, Depth))
4195 return true;
4196
4197 return false;
4198}
4199
4200/// For vector constants, loop over the elements and find the constant with the
4201/// minimum number of sign bits. Return 0 if the value is not a vector constant
4202/// or if any element was not analyzed; otherwise, return the count for the
4203/// element with the minimum number of sign bits.
4205 const APInt &DemandedElts,
4206 unsigned TyBits) {
4207 const auto *CV = dyn_cast<Constant>(V);
4208 if (!CV || !isa<FixedVectorType>(CV->getType()))
4209 return 0;
4210
4211 unsigned MinSignBits = TyBits;
4212 unsigned NumElts = cast<FixedVectorType>(CV->getType())->getNumElements();
4213 for (unsigned i = 0; i != NumElts; ++i) {
4214 if (!DemandedElts[i])
4215 continue;
4216 // If we find a non-ConstantInt, bail out.
4217 auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));
4218 if (!Elt)
4219 return 0;
4220
4221 MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());
4222 }
4223
4224 return MinSignBits;
4225}
4226
4227static unsigned ComputeNumSignBitsImpl(const Value *V,
4228 const APInt &DemandedElts,
4229 const SimplifyQuery &Q, unsigned Depth);
4230
4231static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
4232 const SimplifyQuery &Q, unsigned Depth) {
4233 unsigned Result = ComputeNumSignBitsImpl(V, DemandedElts, Q, Depth);
4234 assert(Result > 0 && "At least one sign bit needs to be present!");
4235 return Result;
4236}
4237
4238/// Return the number of times the sign bit of the register is replicated into
4239/// the other bits. We know that at least 1 bit is always equal to the sign bit
4240/// (itself), but other cases can give us information. For example, immediately
4241/// after an "ashr X, 2", we know that the top 3 bits are all equal to each
4242/// other, so we return 3. For vectors, return the number of sign bits for the
4243/// vector element with the minimum number of known sign bits of the demanded
4244/// elements in the vector specified by DemandedElts.
4245static unsigned ComputeNumSignBitsImpl(const Value *V,
4246 const APInt &DemandedElts,
4247 const SimplifyQuery &Q, unsigned Depth) {
4248 Type *Ty = V->getType();
4249#ifndef NDEBUG
4250 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4251
4252 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
4253 assert(
4254 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
4255 "DemandedElt width should equal the fixed vector number of elements");
4256 } else {
4257 assert(DemandedElts == APInt(1, 1) &&
4258 "DemandedElt width should be 1 for scalars");
4259 }
4260#endif
4261
4262 // We return the minimum number of sign bits that are guaranteed to be present
4263 // in V, so for undef we have to conservatively return 1. We don't have the
4264 // same behavior for poison though -- that's a FIXME today.
4265
4266 Type *ScalarTy = Ty->getScalarType();
4267 unsigned TyBits = ScalarTy->isPointerTy() ?
4268 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
4269 Q.DL.getTypeSizeInBits(ScalarTy);
4270
4271 unsigned Tmp, Tmp2;
4272 unsigned FirstAnswer = 1;
4273
4274 // Note that ConstantInt is handled by the general computeKnownBits case
4275 // below.
4276
4278 return 1;
4279
4280 if (auto *U = dyn_cast<Operator>(V)) {
4281 switch (Operator::getOpcode(V)) {
4282 default: break;
4283 case Instruction::BitCast: {
4284 Value *Src = U->getOperand(0);
4285 Type *SrcTy = Src->getType();
4286
4287 // Skip if the source type is not an integer or integer vector type
4288 // This ensures we only process integer-like types
4289 if (!SrcTy->isIntOrIntVectorTy())
4290 break;
4291
4292 unsigned SrcBits = SrcTy->getScalarSizeInBits();
4293
4294 // Bitcast 'large element' scalar/vector to 'small element' vector.
4295 if ((SrcBits % TyBits) != 0)
4296 break;
4297
4298 // Only proceed if the destination type is a fixed-size vector
4299 if (isa<FixedVectorType>(Ty)) {
4300 // Fast case - sign splat can be simply split across the small elements.
4301 // This works for both vector and scalar sources
4302 Tmp = ComputeNumSignBits(Src, Q, Depth + 1);
4303 if (Tmp == SrcBits)
4304 return TyBits;
4305 }
4306 break;
4307 }
4308 case Instruction::SExt:
4309 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
4310 return ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1) +
4311 Tmp;
4312
4313 case Instruction::SDiv: {
4314 const APInt *Denominator;
4315 // sdiv X, C -> adds log(C) sign bits.
4316 if (match(U->getOperand(1), m_APInt(Denominator))) {
4317
4318 // Ignore non-positive denominator.
4319 if (!Denominator->isStrictlyPositive())
4320 break;
4321
4322 // Calculate the incoming numerator bits.
4323 unsigned NumBits =
4324 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4325
4326 // Add floor(log(C)) bits to the numerator bits.
4327 return std::min(TyBits, NumBits + Denominator->logBase2());
4328 }
4329 break;
4330 }
4331
4332 case Instruction::SRem: {
4333 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4334
4335 const APInt *Denominator;
4336 // srem X, C -> we know that the result is within [-C+1,C) when C is a
4337 // positive constant. This let us put a lower bound on the number of sign
4338 // bits.
4339 if (match(U->getOperand(1), m_APInt(Denominator))) {
4340
4341 // Ignore non-positive denominator.
4342 if (Denominator->isStrictlyPositive()) {
4343 // Calculate the leading sign bit constraints by examining the
4344 // denominator. Given that the denominator is positive, there are two
4345 // cases:
4346 //
4347 // 1. The numerator is positive. The result range is [0,C) and
4348 // [0,C) u< (1 << ceilLogBase2(C)).
4349 //
4350 // 2. The numerator is negative. Then the result range is (-C,0] and
4351 // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
4352 //
4353 // Thus a lower bound on the number of sign bits is `TyBits -
4354 // ceilLogBase2(C)`.
4355
4356 unsigned ResBits = TyBits - Denominator->ceilLogBase2();
4357 Tmp = std::max(Tmp, ResBits);
4358 }
4359 }
4360 return Tmp;
4361 }
4362
4363 case Instruction::AShr: {
4364 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4365 // ashr X, C -> adds C sign bits. Vectors too.
4366 const APInt *ShAmt;
4367 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4368 if (ShAmt->uge(TyBits))
4369 break; // Bad shift.
4370 unsigned ShAmtLimited = ShAmt->getZExtValue();
4371 Tmp += ShAmtLimited;
4372 if (Tmp > TyBits) Tmp = TyBits;
4373 }
4374 return Tmp;
4375 }
4376 case Instruction::Shl: {
4377 const APInt *ShAmt;
4378 Value *X = nullptr;
4379 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4380 // shl destroys sign bits.
4381 if (ShAmt->uge(TyBits))
4382 break; // Bad shift.
4383 // We can look through a zext (more or less treating it as a sext) if
4384 // all extended bits are shifted out.
4385 if (match(U->getOperand(0), m_ZExt(m_Value(X))) &&
4386 ShAmt->uge(TyBits - X->getType()->getScalarSizeInBits())) {
4387 Tmp = ComputeNumSignBits(X, DemandedElts, Q, Depth + 1);
4388 Tmp += TyBits - X->getType()->getScalarSizeInBits();
4389 } else
4390 Tmp =
4391 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4392 if (ShAmt->uge(Tmp))
4393 break; // Shifted all sign bits out.
4394 Tmp2 = ShAmt->getZExtValue();
4395 return Tmp - Tmp2;
4396 }
4397 break;
4398 }
4399 case Instruction::And:
4400 case Instruction::Or:
4401 case Instruction::Xor: // NOT is handled here.
4402 // Logical binary ops preserve the number of sign bits at the worst.
4403 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4404 if (Tmp != 1) {
4405 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4406 FirstAnswer = std::min(Tmp, Tmp2);
4407 // We computed what we know about the sign bits as our first
4408 // answer. Now proceed to the generic code that uses
4409 // computeKnownBits, and pick whichever answer is better.
4410 }
4411 break;
4412
4413 case Instruction::Select: {
4414 // If we have a clamp pattern, we know that the number of sign bits will
4415 // be the minimum of the clamp min/max range.
4416 const Value *X;
4417 const APInt *CLow, *CHigh;
4418 if (isSignedMinMaxClamp(U, X, CLow, CHigh))
4419 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4420
4421 Tmp = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4422 if (Tmp == 1)
4423 break;
4424 Tmp2 = ComputeNumSignBits(U->getOperand(2), DemandedElts, Q, Depth + 1);
4425 return std::min(Tmp, Tmp2);
4426 }
4427
4428 case Instruction::Add:
4429 // Add can have at most one carry bit. Thus we know that the output
4430 // is, at worst, one more bit than the inputs.
4431 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4432 if (Tmp == 1) break;
4433
4434 // Special case decrementing a value (ADD X, -1):
4435 if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
4436 if (CRHS->isAllOnesValue()) {
4437 KnownBits Known(TyBits);
4438 computeKnownBits(U->getOperand(0), DemandedElts, Known, Q, Depth + 1);
4439
4440 // If the input is known to be 0 or 1, the output is 0/-1, which is
4441 // all sign bits set.
4442 if ((Known.Zero | 1).isAllOnes())
4443 return TyBits;
4444
4445 // If we are subtracting one from a positive number, there is no carry
4446 // out of the result.
4447 if (Known.isNonNegative())
4448 return Tmp;
4449 }
4450
4451 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4452 if (Tmp2 == 1)
4453 break;
4454 return std::min(Tmp, Tmp2) - 1;
4455
4456 case Instruction::Sub:
4457 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4458 if (Tmp2 == 1)
4459 break;
4460
4461 // Handle NEG.
4462 if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
4463 if (CLHS->isNullValue()) {
4464 KnownBits Known(TyBits);
4465 computeKnownBits(U->getOperand(1), DemandedElts, Known, Q, Depth + 1);
4466 // If the input is known to be 0 or 1, the output is 0/-1, which is
4467 // all sign bits set.
4468 if ((Known.Zero | 1).isAllOnes())
4469 return TyBits;
4470
4471 // If the input is known to be positive (the sign bit is known clear),
4472 // the output of the NEG has the same number of sign bits as the
4473 // input.
4474 if (Known.isNonNegative())
4475 return Tmp2;
4476
4477 // Otherwise, we treat this like a SUB.
4478 }
4479
4480 // Sub can have at most one carry bit. Thus we know that the output
4481 // is, at worst, one more bit than the inputs.
4482 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4483 if (Tmp == 1)
4484 break;
4485 return std::min(Tmp, Tmp2) - 1;
4486
4487 case Instruction::Mul: {
4488 // The output of the Mul can be at most twice the valid bits in the
4489 // inputs.
4490 unsigned SignBitsOp0 =
4491 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4492 if (SignBitsOp0 == 1)
4493 break;
4494 unsigned SignBitsOp1 =
4495 ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4496 if (SignBitsOp1 == 1)
4497 break;
4498 unsigned OutValidBits =
4499 (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);
4500 return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
4501 }
4502
4503 case Instruction::PHI: {
4504 const PHINode *PN = cast<PHINode>(U);
4505 unsigned NumIncomingValues = PN->getNumIncomingValues();
4506 // Don't analyze large in-degree PHIs.
4507 if (NumIncomingValues > 4) break;
4508 // Unreachable blocks may have zero-operand PHI nodes.
4509 if (NumIncomingValues == 0) break;
4510
4511 // Take the minimum of all incoming values. This can't infinitely loop
4512 // because of our depth threshold.
4514 Tmp = TyBits;
4515 for (unsigned i = 0, e = NumIncomingValues; i != e; ++i) {
4516 if (Tmp == 1) return Tmp;
4517 RecQ.CxtI = PN->getIncomingBlock(i)->getTerminator();
4518 Tmp = std::min(Tmp, ComputeNumSignBits(PN->getIncomingValue(i),
4519 DemandedElts, RecQ, Depth + 1));
4520 }
4521 return Tmp;
4522 }
4523
4524 case Instruction::Trunc: {
4525 // If the input contained enough sign bits that some remain after the
4526 // truncation, then we can make use of that. Otherwise we don't know
4527 // anything.
4528 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4529 unsigned OperandTyBits = U->getOperand(0)->getType()->getScalarSizeInBits();
4530 if (Tmp > (OperandTyBits - TyBits))
4531 return Tmp - (OperandTyBits - TyBits);
4532
4533 return 1;
4534 }
4535
4536 case Instruction::ExtractElement:
4537 // Look through extract element. At the moment we keep this simple and
4538 // skip tracking the specific element. But at least we might find
4539 // information valid for all elements of the vector (for example if vector
4540 // is sign extended, shifted, etc).
4541 return ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4542
4543 case Instruction::ShuffleVector: {
4544 // Collect the minimum number of sign bits that are shared by every vector
4545 // element referenced by the shuffle.
4546 auto *Shuf = dyn_cast<ShuffleVectorInst>(U);
4547 if (!Shuf) {
4548 // FIXME: Add support for shufflevector constant expressions.
4549 return 1;
4550 }
4551 APInt DemandedLHS, DemandedRHS;
4552 // For undef elements, we don't know anything about the common state of
4553 // the shuffle result.
4554 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
4555 return 1;
4556 Tmp = std::numeric_limits<unsigned>::max();
4557 if (!!DemandedLHS) {
4558 const Value *LHS = Shuf->getOperand(0);
4559 Tmp = ComputeNumSignBits(LHS, DemandedLHS, Q, Depth + 1);
4560 }
4561 // If we don't know anything, early out and try computeKnownBits
4562 // fall-back.
4563 if (Tmp == 1)
4564 break;
4565 if (!!DemandedRHS) {
4566 const Value *RHS = Shuf->getOperand(1);
4567 Tmp2 = ComputeNumSignBits(RHS, DemandedRHS, Q, Depth + 1);
4568 Tmp = std::min(Tmp, Tmp2);
4569 }
4570 // If we don't know anything, early out and try computeKnownBits
4571 // fall-back.
4572 if (Tmp == 1)
4573 break;
4574 assert(Tmp <= TyBits && "Failed to determine minimum sign bits");
4575 return Tmp;
4576 }
4577 case Instruction::Call: {
4578 if (const auto *II = dyn_cast<IntrinsicInst>(U)) {
4579 switch (II->getIntrinsicID()) {
4580 default:
4581 break;
4582 case Intrinsic::abs:
4583 Tmp =
4584 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4585 if (Tmp == 1)
4586 break;
4587
4588 // Absolute value reduces number of sign bits by at most 1.
4589 return Tmp - 1;
4590 case Intrinsic::smin:
4591 case Intrinsic::smax: {
4592 const APInt *CLow, *CHigh;
4593 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
4594 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4595 }
4596 }
4597 }
4598 }
4599 }
4600 }
4601
4602 // Finally, if we can prove that the top bits of the result are 0's or 1's,
4603 // use this information.
4604
4605 // If we can examine all elements of a vector constant successfully, we're
4606 // done (we can't do any better than that). If not, keep trying.
4607 if (unsigned VecSignBits =
4608 computeNumSignBitsVectorConstant(V, DemandedElts, TyBits))
4609 return VecSignBits;
4610
4611 KnownBits Known(TyBits);
4612 computeKnownBits(V, DemandedElts, Known, Q, Depth);
4613
4614 // If we know that the sign bit is either zero or one, determine the number of
4615 // identical bits in the top of the input value.
4616 return std::max(FirstAnswer, Known.countMinSignBits());
4617}
4618
4620 const TargetLibraryInfo *TLI) {
4621 const Function *F = CB.getCalledFunction();
4622 if (!F)
4624
4625 if (F->isIntrinsic())
4626 return F->getIntrinsicID();
4627
4628 // We are going to infer semantics of a library function based on mapping it
4629 // to an LLVM intrinsic. Check that the library function is available from
4630 // this callbase and in this environment.
4631 LibFunc Func;
4632 if (F->hasLocalLinkage() || !TLI || !TLI->getLibFunc(CB, Func) ||
4633 !CB.onlyReadsMemory())
4635
4636 switch (Func) {
4637 default:
4638 break;
4639 case LibFunc_sin:
4640 case LibFunc_sinf:
4641 case LibFunc_sinl:
4642 return Intrinsic::sin;
4643 case LibFunc_cos:
4644 case LibFunc_cosf:
4645 case LibFunc_cosl:
4646 return Intrinsic::cos;
4647 case LibFunc_tan:
4648 case LibFunc_tanf:
4649 case LibFunc_tanl:
4650 return Intrinsic::tan;
4651 case LibFunc_asin:
4652 case LibFunc_asinf:
4653 case LibFunc_asinl:
4654 return Intrinsic::asin;
4655 case LibFunc_acos:
4656 case LibFunc_acosf:
4657 case LibFunc_acosl:
4658 return Intrinsic::acos;
4659 case LibFunc_atan:
4660 case LibFunc_atanf:
4661 case LibFunc_atanl:
4662 return Intrinsic::atan;
4663 case LibFunc_atan2:
4664 case LibFunc_atan2f:
4665 case LibFunc_atan2l:
4666 return Intrinsic::atan2;
4667 case LibFunc_sinh:
4668 case LibFunc_sinhf:
4669 case LibFunc_sinhl:
4670 return Intrinsic::sinh;
4671 case LibFunc_cosh:
4672 case LibFunc_coshf:
4673 case LibFunc_coshl:
4674 return Intrinsic::cosh;
4675 case LibFunc_tanh:
4676 case LibFunc_tanhf:
4677 case LibFunc_tanhl:
4678 return Intrinsic::tanh;
4679 case LibFunc_exp:
4680 case LibFunc_expf:
4681 case LibFunc_expl:
4682 return Intrinsic::exp;
4683 case LibFunc_exp2:
4684 case LibFunc_exp2f:
4685 case LibFunc_exp2l:
4686 return Intrinsic::exp2;
4687 case LibFunc_exp10:
4688 case LibFunc_exp10f:
4689 case LibFunc_exp10l:
4690 return Intrinsic::exp10;
4691 case LibFunc_log:
4692 case LibFunc_logf:
4693 case LibFunc_logl:
4694 return Intrinsic::log;
4695 case LibFunc_log10:
4696 case LibFunc_log10f:
4697 case LibFunc_log10l:
4698 return Intrinsic::log10;
4699 case LibFunc_log2:
4700 case LibFunc_log2f:
4701 case LibFunc_log2l:
4702 return Intrinsic::log2;
4703 case LibFunc_fabs:
4704 case LibFunc_fabsf:
4705 case LibFunc_fabsl:
4706 return Intrinsic::fabs;
4707 case LibFunc_fmin:
4708 case LibFunc_fminf:
4709 case LibFunc_fminl:
4710 return Intrinsic::minnum;
4711 case LibFunc_fmax:
4712 case LibFunc_fmaxf:
4713 case LibFunc_fmaxl:
4714 return Intrinsic::maxnum;
4715 case LibFunc_copysign:
4716 case LibFunc_copysignf:
4717 case LibFunc_copysignl:
4718 return Intrinsic::copysign;
4719 case LibFunc_floor:
4720 case LibFunc_floorf:
4721 case LibFunc_floorl:
4722 return Intrinsic::floor;
4723 case LibFunc_ceil:
4724 case LibFunc_ceilf:
4725 case LibFunc_ceill:
4726 return Intrinsic::ceil;
4727 case LibFunc_trunc:
4728 case LibFunc_truncf:
4729 case LibFunc_truncl:
4730 return Intrinsic::trunc;
4731 case LibFunc_rint:
4732 case LibFunc_rintf:
4733 case LibFunc_rintl:
4734 return Intrinsic::rint;
4735 case LibFunc_nearbyint:
4736 case LibFunc_nearbyintf:
4737 case LibFunc_nearbyintl:
4738 return Intrinsic::nearbyint;
4739 case LibFunc_round:
4740 case LibFunc_roundf:
4741 case LibFunc_roundl:
4742 return Intrinsic::round;
4743 case LibFunc_roundeven:
4744 case LibFunc_roundevenf:
4745 case LibFunc_roundevenl:
4746 return Intrinsic::roundeven;
4747 case LibFunc_pow:
4748 case LibFunc_powf:
4749 case LibFunc_powl:
4750 return Intrinsic::pow;
4751 case LibFunc_sqrt:
4752 case LibFunc_sqrtf:
4753 case LibFunc_sqrtl:
4754 return Intrinsic::sqrt;
4755 }
4756
4758}
4759
4760/// Given an exploded icmp instruction, return true if the comparison only
4761/// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if
4762/// the result of the comparison is true when the input value is signed.
4764 bool &TrueIfSigned) {
4765 switch (Pred) {
4766 case ICmpInst::ICMP_SLT: // True if LHS s< 0
4767 TrueIfSigned = true;
4768 return RHS.isZero();
4769 case ICmpInst::ICMP_SLE: // True if LHS s<= -1
4770 TrueIfSigned = true;
4771 return RHS.isAllOnes();
4772 case ICmpInst::ICMP_SGT: // True if LHS s> -1
4773 TrueIfSigned = false;
4774 return RHS.isAllOnes();
4775 case ICmpInst::ICMP_SGE: // True if LHS s>= 0
4776 TrueIfSigned = false;
4777 return RHS.isZero();
4778 case ICmpInst::ICMP_UGT:
4779 // True if LHS u> RHS and RHS == sign-bit-mask - 1
4780 TrueIfSigned = true;
4781 return RHS.isMaxSignedValue();
4782 case ICmpInst::ICMP_UGE:
4783 // True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4784 TrueIfSigned = true;
4785 return RHS.isMinSignedValue();
4786 case ICmpInst::ICMP_ULT:
4787 // True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4788 TrueIfSigned = false;
4789 return RHS.isMinSignedValue();
4790 case ICmpInst::ICMP_ULE:
4791 // True if LHS u<= RHS and RHS == sign-bit-mask - 1
4792 TrueIfSigned = false;
4793 return RHS.isMaxSignedValue();
4794 default:
4795 return false;
4796 }
4797}
4798
4800 bool CondIsTrue,
4801 const Instruction *CxtI,
4802 KnownFPClass &KnownFromContext,
4803 unsigned Depth = 0) {
4804 Value *A, *B;
4806 (CondIsTrue ? match(Cond, m_LogicalAnd(m_Value(A), m_Value(B)))
4807 : match(Cond, m_LogicalOr(m_Value(A), m_Value(B))))) {
4808 computeKnownFPClassFromCond(V, A, CondIsTrue, CxtI, KnownFromContext,
4809 Depth + 1);
4810 computeKnownFPClassFromCond(V, B, CondIsTrue, CxtI, KnownFromContext,
4811 Depth + 1);
4812 return;
4813 }
4815 computeKnownFPClassFromCond(V, A, !CondIsTrue, CxtI, KnownFromContext,
4816 Depth + 1);
4817 return;
4818 }
4819 CmpPredicate Pred;
4820 Value *LHS;
4821 uint64_t ClassVal = 0;
4822 const APFloat *CRHS;
4823 const APInt *RHS;
4824 if (match(Cond, m_FCmp(Pred, m_Value(LHS), m_APFloat(CRHS)))) {
4825 auto [CmpVal, MaskIfTrue, MaskIfFalse] = fcmpImpliesClass(
4826 Pred, *cast<Instruction>(Cond)->getParent()->getParent(), LHS, *CRHS,
4827 LHS != V);
4828 if (CmpVal == V)
4829 KnownFromContext.knownNot(~(CondIsTrue ? MaskIfTrue : MaskIfFalse));
4831 m_Specific(V), m_ConstantInt(ClassVal)))) {
4832 FPClassTest Mask = static_cast<FPClassTest>(ClassVal);
4833 KnownFromContext.knownNot(CondIsTrue ? ~Mask : Mask);
4834 } else if (match(Cond, m_ICmp(Pred, m_ElementWiseBitCast(m_Specific(V)),
4835 m_APInt(RHS)))) {
4836 bool TrueIfSigned;
4837 if (!isSignBitCheck(Pred, *RHS, TrueIfSigned))
4838 return;
4839 if (TrueIfSigned == CondIsTrue)
4840 KnownFromContext.signBitMustBeOne();
4841 else
4842 KnownFromContext.signBitMustBeZero();
4843 }
4844}
4845
4847 const SimplifyQuery &Q) {
4848 KnownFPClass KnownFromContext;
4849
4850 if (Q.CC && Q.CC->AffectedValues.contains(V))
4852 KnownFromContext);
4853
4854 if (!Q.CxtI)
4855 return KnownFromContext;
4856
4857 if (Q.DC && Q.DT) {
4858 // Handle dominating conditions.
4859 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
4860 Value *Cond = BI->getCondition();
4861
4862 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4863 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
4864 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/true, Q.CxtI,
4865 KnownFromContext);
4866
4867 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4868 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
4869 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/false, Q.CxtI,
4870 KnownFromContext);
4871 }
4872 }
4873
4874 if (!Q.AC)
4875 return KnownFromContext;
4876
4877 // Try to restrict the floating-point classes based on information from
4878 // assumptions.
4879 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
4880 if (!AssumeVH)
4881 continue;
4882 CallInst *I = cast<CallInst>(AssumeVH);
4883
4884 assert(I->getFunction() == Q.CxtI->getParent()->getParent() &&
4885 "Got assumption for the wrong function!");
4886 assert(I->getIntrinsicID() == Intrinsic::assume &&
4887 "must be an assume intrinsic");
4888
4889 if (!isValidAssumeForContext(I, Q))
4890 continue;
4891
4892 computeKnownFPClassFromCond(V, I->getArgOperand(0),
4893 /*CondIsTrue=*/true, Q.CxtI, KnownFromContext);
4894 }
4895
4896 return KnownFromContext;
4897}
4898
4900 Value *Arm, bool Invert,
4901 const SimplifyQuery &SQ,
4902 unsigned Depth) {
4903
4904 KnownFPClass KnownSrc;
4906 /*CondIsTrue=*/!Invert, SQ.CxtI, KnownSrc,
4907 Depth + 1);
4908 KnownSrc = KnownSrc.unionWith(Known);
4909 if (KnownSrc.isUnknown())
4910 return;
4911
4912 if (isGuaranteedNotToBeUndef(Arm, SQ.AC, SQ.CxtI, SQ.DT, Depth + 1))
4913 Known = KnownSrc;
4914}
4915
4916void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4917 FPClassTest InterestedClasses, KnownFPClass &Known,
4918 const SimplifyQuery &Q, unsigned Depth);
4919
4920static void computeKnownFPClass(const Value *V, KnownFPClass &Known,
4921 FPClassTest InterestedClasses,
4922 const SimplifyQuery &Q, unsigned Depth) {
4923 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
4924 APInt DemandedElts =
4925 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
4926 computeKnownFPClass(V, DemandedElts, InterestedClasses, Known, Q, Depth);
4927}
4928
4930 const APInt &DemandedElts,
4931 FPClassTest InterestedClasses,
4932 KnownFPClass &Known,
4933 const SimplifyQuery &Q,
4934 unsigned Depth) {
4935 if ((InterestedClasses &
4937 return;
4938
4939 KnownFPClass KnownSrc;
4940 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
4941 KnownSrc, Q, Depth + 1);
4942 Known = KnownFPClass::fptrunc(KnownSrc);
4943}
4944
4946 switch (IID) {
4947 case Intrinsic::minimum:
4949 case Intrinsic::maximum:
4951 case Intrinsic::minimumnum:
4953 case Intrinsic::maximumnum:
4955 case Intrinsic::minnum:
4957 case Intrinsic::maxnum:
4959 default:
4960 llvm_unreachable("not a floating-point min-max intrinsic");
4961 }
4962}
4963
4964/// \return true if this is a floating point value that is known to have a
4965/// magnitude smaller than 1. i.e., fabs(X) <= 1.0 or is nan.
4966static bool isAbsoluteValueULEOne(const Value *V) {
4967 // TODO: Handle frexp
4968 // TODO: Other rounding intrinsics?
4969
4970 // fabs(x - floor(x)) <= 1
4971 const Value *SubFloorX;
4972 if (match(V, m_FSub(m_Value(SubFloorX),
4974 return true;
4975
4978}
4979
4980void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4981 FPClassTest InterestedClasses, KnownFPClass &Known,
4982 const SimplifyQuery &Q, unsigned Depth) {
4983 assert(Known.isUnknown() && "should not be called with known information");
4984
4985 if (!DemandedElts) {
4986 // No demanded elts, better to assume we don't know anything.
4987 Known.resetAll();
4988 return;
4989 }
4990
4991 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4992
4993 if (auto *CFP = dyn_cast<ConstantFP>(V)) {
4994 Known = KnownFPClass(CFP->getValueAPF());
4995 return;
4996 }
4997
4999 Known.KnownFPClasses = fcPosZero;
5000 Known.SignBit = false;
5001 return;
5002 }
5003
5004 if (isa<PoisonValue>(V)) {
5005 Known.KnownFPClasses = fcNone;
5006 Known.SignBit = false;
5007 return;
5008 }
5009
5010 // Try to handle fixed width vector constants
5011 auto *VFVTy = dyn_cast<FixedVectorType>(V->getType());
5012 const Constant *CV = dyn_cast<Constant>(V);
5013 if (VFVTy && CV) {
5014 Known.KnownFPClasses = fcNone;
5015 bool SignBitAllZero = true;
5016 bool SignBitAllOne = true;
5017
5018 // For vectors, verify that each element is not NaN.
5019 unsigned NumElts = VFVTy->getNumElements();
5020 for (unsigned i = 0; i != NumElts; ++i) {
5021 if (!DemandedElts[i])
5022 continue;
5023
5024 Constant *Elt = CV->getAggregateElement(i);
5025 if (!Elt) {
5026 Known = KnownFPClass();
5027 return;
5028 }
5029 if (isa<PoisonValue>(Elt))
5030 continue;
5031 auto *CElt = dyn_cast<ConstantFP>(Elt);
5032 if (!CElt) {
5033 Known = KnownFPClass();
5034 return;
5035 }
5036
5037 const APFloat &C = CElt->getValueAPF();
5038 Known.KnownFPClasses |= C.classify();
5039 if (C.isNegative())
5040 SignBitAllZero = false;
5041 else
5042 SignBitAllOne = false;
5043 }
5044 if (SignBitAllOne != SignBitAllZero)
5045 Known.SignBit = SignBitAllOne;
5046 return;
5047 }
5048
5049 if (const auto *CDS = dyn_cast<ConstantDataSequential>(V)) {
5050 Known.KnownFPClasses = fcNone;
5051 for (size_t I = 0, E = CDS->getNumElements(); I != E; ++I)
5052 Known |= CDS->getElementAsAPFloat(I).classify();
5053 return;
5054 }
5055
5056 if (const auto *CA = dyn_cast<ConstantAggregate>(V)) {
5057 // TODO: Handle complex aggregates
5058 Known.KnownFPClasses = fcNone;
5059 for (const Use &Op : CA->operands()) {
5060 auto *CFP = dyn_cast<ConstantFP>(Op.get());
5061 if (!CFP) {
5062 Known = KnownFPClass();
5063 return;
5064 }
5065
5066 Known |= CFP->getValueAPF().classify();
5067 }
5068
5069 return;
5070 }
5071
5072 FPClassTest KnownNotFromFlags = fcNone;
5073 if (const auto *CB = dyn_cast<CallBase>(V))
5074 KnownNotFromFlags |= CB->getRetNoFPClass();
5075 else if (const auto *Arg = dyn_cast<Argument>(V))
5076 KnownNotFromFlags |= Arg->getNoFPClass();
5077
5078 const Operator *Op = dyn_cast<Operator>(V);
5080 if (FPOp->hasNoNaNs())
5081 KnownNotFromFlags |= fcNan;
5082 if (FPOp->hasNoInfs())
5083 KnownNotFromFlags |= fcInf;
5084 }
5085
5086 KnownFPClass AssumedClasses = computeKnownFPClassFromContext(V, Q);
5087 KnownNotFromFlags |= ~AssumedClasses.KnownFPClasses;
5088
5089 // We no longer need to find out about these bits from inputs if we can
5090 // assume this from flags/attributes.
5091 InterestedClasses &= ~KnownNotFromFlags;
5092
5093 llvm::scope_exit ClearClassesFromFlags([=, &Known] {
5094 Known.knownNot(KnownNotFromFlags);
5095 if (!Known.SignBit && AssumedClasses.SignBit) {
5096 if (*AssumedClasses.SignBit)
5097 Known.signBitMustBeOne();
5098 else
5099 Known.signBitMustBeZero();
5100 }
5101 });
5102
5103 if (!Op)
5104 return;
5105
5106 // All recursive calls that increase depth must come after this.
5108 return;
5109
5110 const unsigned Opc = Op->getOpcode();
5111 switch (Opc) {
5112 case Instruction::FNeg: {
5113 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5114 Known, Q, Depth + 1);
5115 Known.fneg();
5116 break;
5117 }
5118 case Instruction::Select: {
5119 auto ComputeForArm = [&](Value *Arm, bool Invert) {
5120 KnownFPClass Res;
5121 computeKnownFPClass(Arm, DemandedElts, InterestedClasses, Res, Q,
5122 Depth + 1);
5123 adjustKnownFPClassForSelectArm(Res, Op->getOperand(0), Arm, Invert, Q,
5124 Depth);
5125 return Res;
5126 };
5127 // Only known if known in both the LHS and RHS.
5128 Known =
5129 ComputeForArm(Op->getOperand(1), /*Invert=*/false)
5130 .intersectWith(ComputeForArm(Op->getOperand(2), /*Invert=*/true));
5131 break;
5132 }
5133 case Instruction::Load: {
5134 const MDNode *NoFPClass =
5135 cast<LoadInst>(Op)->getMetadata(LLVMContext::MD_nofpclass);
5136 if (!NoFPClass)
5137 break;
5138
5139 ConstantInt *MaskVal =
5141 Known.knownNot(static_cast<FPClassTest>(MaskVal->getZExtValue()));
5142 break;
5143 }
5144 case Instruction::Call: {
5145 const CallInst *II = cast<CallInst>(Op);
5146 const Intrinsic::ID IID = II->getIntrinsicID();
5147 switch (IID) {
5148 case Intrinsic::fabs: {
5149 if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) {
5150 // If we only care about the sign bit we don't need to inspect the
5151 // operand.
5152 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5153 InterestedClasses, Known, Q, Depth + 1);
5154 }
5155
5156 Known.fabs();
5157 break;
5158 }
5159 case Intrinsic::copysign: {
5160 KnownFPClass KnownSign;
5161
5162 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5163 Known, Q, Depth + 1);
5164 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5165 KnownSign, Q, Depth + 1);
5166 Known.copysign(KnownSign);
5167 break;
5168 }
5169 case Intrinsic::fma:
5170 case Intrinsic::fmuladd: {
5171 if ((InterestedClasses & fcNegative) == fcNone)
5172 break;
5173
5174 // FIXME: This should check isGuaranteedNotToBeUndef
5175 if (II->getArgOperand(0) == II->getArgOperand(1)) {
5176 KnownFPClass KnownSrc, KnownAddend;
5177 computeKnownFPClass(II->getArgOperand(2), DemandedElts,
5178 InterestedClasses, KnownAddend, Q, Depth + 1);
5179 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5180 InterestedClasses, KnownSrc, Q, Depth + 1);
5181
5182 const Function *F = II->getFunction();
5183 const fltSemantics &FltSem =
5184 II->getType()->getScalarType()->getFltSemantics();
5186 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5187
5188 if (KnownNotFromFlags & fcNan) {
5189 KnownSrc.knownNot(fcNan);
5190 KnownAddend.knownNot(fcNan);
5191 }
5192
5193 if (KnownNotFromFlags & fcInf) {
5194 KnownSrc.knownNot(fcInf);
5195 KnownAddend.knownNot(fcInf);
5196 }
5197
5198 Known = KnownFPClass::fma_square(KnownSrc, KnownAddend, Mode);
5199 break;
5200 }
5201
5202 KnownFPClass KnownSrc[3];
5203 for (int I = 0; I != 3; ++I) {
5204 computeKnownFPClass(II->getArgOperand(I), DemandedElts,
5205 InterestedClasses, KnownSrc[I], Q, Depth + 1);
5206 if (KnownSrc[I].isUnknown())
5207 return;
5208
5209 if (KnownNotFromFlags & fcNan)
5210 KnownSrc[I].knownNot(fcNan);
5211 if (KnownNotFromFlags & fcInf)
5212 KnownSrc[I].knownNot(fcInf);
5213 }
5214
5215 const Function *F = II->getFunction();
5216 const fltSemantics &FltSem =
5217 II->getType()->getScalarType()->getFltSemantics();
5219 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5220 Known = KnownFPClass::fma(KnownSrc[0], KnownSrc[1], KnownSrc[2], Mode);
5221 break;
5222 }
5223 case Intrinsic::sqrt:
5224 case Intrinsic::experimental_constrained_sqrt: {
5225 KnownFPClass KnownSrc;
5226 FPClassTest InterestedSrcs = InterestedClasses;
5227 if (InterestedClasses & fcNan)
5228 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5229
5230 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5231 KnownSrc, Q, Depth + 1);
5232
5234
5235 bool HasNSZ = Q.IIQ.hasNoSignedZeros(II);
5236 if (!HasNSZ) {
5237 const Function *F = II->getFunction();
5238 const fltSemantics &FltSem =
5239 II->getType()->getScalarType()->getFltSemantics();
5240 Mode = F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5241 }
5242
5243 Known = KnownFPClass::sqrt(KnownSrc, Mode);
5244 if (HasNSZ)
5245 Known.knownNot(fcNegZero);
5246
5247 break;
5248 }
5249 case Intrinsic::sin: {
5250 KnownFPClass KnownSrc;
5251 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5252 KnownSrc, Q, Depth + 1);
5253 Known = KnownFPClass::sin(KnownSrc);
5254 break;
5255 }
5256 case Intrinsic::cos: {
5257 KnownFPClass KnownSrc;
5258 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5259 KnownSrc, Q, Depth + 1);
5260 Known = KnownFPClass::cos(KnownSrc);
5261 break;
5262 }
5263 case Intrinsic::tan: {
5264 KnownFPClass KnownSrc;
5265 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5266 KnownSrc, Q, Depth + 1);
5267 Known = KnownFPClass::tan(KnownSrc);
5268 break;
5269 }
5270 case Intrinsic::sinh: {
5271 KnownFPClass KnownSrc;
5272 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5273 KnownSrc, Q, Depth + 1);
5274 Known = KnownFPClass::sinh(KnownSrc);
5275 break;
5276 }
5277 case Intrinsic::cosh: {
5278 KnownFPClass KnownSrc;
5279 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5280 KnownSrc, Q, Depth + 1);
5281 Known = KnownFPClass::cosh(KnownSrc);
5282 break;
5283 }
5284 case Intrinsic::tanh: {
5285 KnownFPClass KnownSrc;
5286 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5287 KnownSrc, Q, Depth + 1);
5288 Known = KnownFPClass::tanh(KnownSrc);
5289 break;
5290 }
5291 case Intrinsic::asin: {
5292 KnownFPClass KnownSrc;
5293 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5294 KnownSrc, Q, Depth + 1);
5295 Known = KnownFPClass::asin(KnownSrc);
5296 break;
5297 }
5298 case Intrinsic::acos: {
5299 KnownFPClass KnownSrc;
5300 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5301 KnownSrc, Q, Depth + 1);
5302 Known = KnownFPClass::acos(KnownSrc);
5303 break;
5304 }
5305 case Intrinsic::atan: {
5306 KnownFPClass KnownSrc;
5307 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5308 KnownSrc, Q, Depth + 1);
5309 Known = KnownFPClass::atan(KnownSrc);
5310 break;
5311 }
5312 case Intrinsic::atan2: {
5313 KnownFPClass KnownLHS, KnownRHS;
5314 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5315 KnownLHS, Q, Depth + 1);
5316 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5317 KnownRHS, Q, Depth + 1);
5318 Known = KnownFPClass::atan2(KnownLHS, KnownRHS);
5319 break;
5320 }
5321 case Intrinsic::maxnum:
5322 case Intrinsic::minnum:
5323 case Intrinsic::minimum:
5324 case Intrinsic::maximum:
5325 case Intrinsic::minimumnum:
5326 case Intrinsic::maximumnum: {
5327 KnownFPClass KnownLHS, KnownRHS;
5328 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5329 KnownLHS, Q, Depth + 1);
5330 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5331 KnownRHS, Q, Depth + 1);
5332
5333 const Function *F = II->getFunction();
5334
5336 F ? F->getDenormalMode(
5337 II->getType()->getScalarType()->getFltSemantics())
5339
5340 Known = KnownFPClass::minMaxLike(KnownLHS, KnownRHS, getMinMaxKind(IID),
5341 Mode);
5342 break;
5343 }
5344 case Intrinsic::canonicalize: {
5345 KnownFPClass KnownSrc;
5346 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5347 KnownSrc, Q, Depth + 1);
5348
5349 const Function *F = II->getFunction();
5350 DenormalMode DenormMode =
5351 F ? F->getDenormalMode(
5352 II->getType()->getScalarType()->getFltSemantics())
5354 Known = KnownFPClass::canonicalize(KnownSrc, DenormMode);
5355 break;
5356 }
5357 case Intrinsic::vector_reduce_fmax:
5358 case Intrinsic::vector_reduce_fmin:
5359 case Intrinsic::vector_reduce_fmaximum:
5360 case Intrinsic::vector_reduce_fminimum: {
5361 // reduce min/max will choose an element from one of the vector elements,
5362 // so we can infer and class information that is common to all elements.
5363 Known = computeKnownFPClass(II->getArgOperand(0), II->getFastMathFlags(),
5364 InterestedClasses, Q, Depth + 1);
5365 // Can only propagate sign if output is never NaN.
5366 if (!Known.isKnownNeverNaN())
5367 Known.SignBit.reset();
5368 break;
5369 }
5370 // reverse preserves all characteristics of the input vec's element.
5371 case Intrinsic::vector_reverse:
5372 Known = computeKnownFPClass(
5373 II->getArgOperand(0), DemandedElts.reverseBits(),
5374 II->getFastMathFlags(), InterestedClasses, Q, Depth + 1);
5375 break;
5376 case Intrinsic::trunc:
5377 case Intrinsic::floor:
5378 case Intrinsic::ceil:
5379 case Intrinsic::rint:
5380 case Intrinsic::nearbyint:
5381 case Intrinsic::round:
5382 case Intrinsic::roundeven: {
5383 KnownFPClass KnownSrc;
5384 FPClassTest InterestedSrcs = InterestedClasses;
5385 if (InterestedSrcs & fcPosFinite)
5386 InterestedSrcs |= fcPosFinite;
5387 if (InterestedSrcs & fcNegFinite)
5388 InterestedSrcs |= fcNegFinite;
5389 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5390 KnownSrc, Q, Depth + 1);
5391
5393 KnownSrc, IID == Intrinsic::trunc,
5394 V->getType()->getScalarType()->isMultiUnitFPType());
5395 break;
5396 }
5397 case Intrinsic::exp:
5398 case Intrinsic::exp2:
5399 case Intrinsic::exp10:
5400 case Intrinsic::amdgcn_exp2: {
5401 KnownFPClass KnownSrc;
5402 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5403 KnownSrc, Q, Depth + 1);
5404
5405 Known = KnownFPClass::exp(KnownSrc);
5406
5407 Type *EltTy = II->getType()->getScalarType();
5408 if (IID == Intrinsic::amdgcn_exp2 && EltTy->isFloatTy())
5409 Known.knownNot(fcSubnormal);
5410
5411 break;
5412 }
5413 case Intrinsic::fptrunc_round: {
5414 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,
5415 Q, Depth);
5416 break;
5417 }
5418 case Intrinsic::log:
5419 case Intrinsic::log10:
5420 case Intrinsic::log2:
5421 case Intrinsic::experimental_constrained_log:
5422 case Intrinsic::experimental_constrained_log10:
5423 case Intrinsic::experimental_constrained_log2:
5424 case Intrinsic::amdgcn_log: {
5425 Type *EltTy = II->getType()->getScalarType();
5426
5427 // log(+inf) -> +inf
5428 // log([+-]0.0) -> -inf
5429 // log(-inf) -> nan
5430 // log(-x) -> nan
5431 if ((InterestedClasses & (fcNan | fcInf)) != fcNone) {
5432 FPClassTest InterestedSrcs = InterestedClasses;
5433 if ((InterestedClasses & fcNegInf) != fcNone)
5434 InterestedSrcs |= fcZero | fcSubnormal;
5435 if ((InterestedClasses & fcNan) != fcNone)
5436 InterestedSrcs |= fcNan | fcNegative;
5437
5438 KnownFPClass KnownSrc;
5439 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5440 KnownSrc, Q, Depth + 1);
5441
5442 const Function *F = II->getFunction();
5443 DenormalMode Mode = F ? F->getDenormalMode(EltTy->getFltSemantics())
5445 Known = KnownFPClass::log(KnownSrc, Mode);
5446 }
5447
5448 break;
5449 }
5450 case Intrinsic::powi: {
5451 if ((InterestedClasses & (fcNan | fcInf | fcNegative)) == fcNone)
5452 break;
5453
5454 const Value *Exp = II->getArgOperand(1);
5455 Type *ExpTy = Exp->getType();
5456 unsigned BitWidth = ExpTy->getScalarType()->getIntegerBitWidth();
5457 KnownBits ExponentKnownBits(BitWidth);
5458 computeKnownBits(Exp, isa<VectorType>(ExpTy) ? DemandedElts : APInt(1, 1),
5459 ExponentKnownBits, Q, Depth + 1);
5460
5461 FPClassTest InterestedSrcs = fcNone;
5462 if (InterestedClasses & fcNan)
5463 InterestedSrcs |= fcNan;
5464 if (!ExponentKnownBits.isZero()) {
5465 if (InterestedClasses & fcInf)
5466 InterestedSrcs |= fcFinite | fcInf;
5467 if ((InterestedClasses & fcNegative) && !ExponentKnownBits.isEven())
5468 InterestedSrcs |= fcNegative;
5469 }
5470
5471 KnownFPClass KnownSrc;
5472 if (InterestedSrcs != fcNone)
5473 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5474 KnownSrc, Q, Depth + 1);
5475
5476 Known = KnownFPClass::powi(KnownSrc, ExponentKnownBits);
5477 break;
5478 }
5479 case Intrinsic::ldexp: {
5480 KnownFPClass KnownSrc;
5481 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5482 KnownSrc, Q, Depth + 1);
5483 // Can refine inf/zero handling based on the exponent operand.
5484 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;
5485
5486 KnownBits ExpBits;
5487 if ((KnownSrc.KnownFPClasses & ExpInfoMask) != fcNone) {
5488 const Value *ExpArg = II->getArgOperand(1);
5489 ExpBits = computeKnownBits(ExpArg, DemandedElts, Q, Depth + 1);
5490 }
5491
5492 const fltSemantics &Flt =
5493 II->getType()->getScalarType()->getFltSemantics();
5494
5495 const Function *F = II->getFunction();
5497 F ? F->getDenormalMode(Flt) : DenormalMode::getDynamic();
5498
5499 Known = KnownFPClass::ldexp(KnownSrc, ExpBits, Flt, Mode);
5500 break;
5501 }
5502 case Intrinsic::arithmetic_fence: {
5503 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5504 Known, Q, Depth + 1);
5505 break;
5506 }
5507 case Intrinsic::experimental_constrained_sitofp:
5508 case Intrinsic::experimental_constrained_uitofp:
5509 // Cannot produce nan
5510 Known.knownNot(fcNan);
5511
5512 // sitofp and uitofp turn into +0.0 for zero.
5513 Known.knownNot(fcNegZero);
5514
5515 // Integers cannot be subnormal
5516 Known.knownNot(fcSubnormal);
5517
5518 if (IID == Intrinsic::experimental_constrained_uitofp)
5519 Known.signBitMustBeZero();
5520
5521 // TODO: Copy inf handling from instructions
5522 break;
5523
5524 case Intrinsic::amdgcn_fract: {
5525 Known.knownNot(fcInf);
5526
5527 if (InterestedClasses & fcNan) {
5528 KnownFPClass KnownSrc;
5529 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5530 InterestedClasses, KnownSrc, Q, Depth + 1);
5531
5532 if (KnownSrc.isKnownNeverInfOrNaN())
5533 Known.knownNot(fcNan);
5534 else if (KnownSrc.isKnownNever(fcSNan))
5535 Known.knownNot(fcSNan);
5536 }
5537
5538 break;
5539 }
5540 case Intrinsic::amdgcn_rcp: {
5541 KnownFPClass KnownSrc;
5542 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5543 KnownSrc, Q, Depth + 1);
5544
5545 Known.propagateNaN(KnownSrc);
5546
5547 Type *EltTy = II->getType()->getScalarType();
5548
5549 // f32 denormal always flushed.
5550 if (EltTy->isFloatTy()) {
5551 Known.knownNot(fcSubnormal);
5552 KnownSrc.knownNot(fcSubnormal);
5553 }
5554
5555 if (KnownSrc.isKnownNever(fcNegative))
5556 Known.knownNot(fcNegative);
5557 if (KnownSrc.isKnownNever(fcPositive))
5558 Known.knownNot(fcPositive);
5559
5560 if (const Function *F = II->getFunction()) {
5561 DenormalMode Mode = F->getDenormalMode(EltTy->getFltSemantics());
5562 if (KnownSrc.isKnownNeverLogicalPosZero(Mode))
5563 Known.knownNot(fcPosInf);
5564 if (KnownSrc.isKnownNeverLogicalNegZero(Mode))
5565 Known.knownNot(fcNegInf);
5566 }
5567
5568 break;
5569 }
5570 case Intrinsic::amdgcn_rsq: {
5571 KnownFPClass KnownSrc;
5572 // The only negative value that can be returned is -inf for -0 inputs.
5574
5575 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5576 KnownSrc, Q, Depth + 1);
5577
5578 // Negative -> nan
5579 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
5580 Known.knownNot(fcNan);
5581 else if (KnownSrc.isKnownNever(fcSNan))
5582 Known.knownNot(fcSNan);
5583
5584 // +inf -> +0
5585 if (KnownSrc.isKnownNeverPosInfinity())
5586 Known.knownNot(fcPosZero);
5587
5588 Type *EltTy = II->getType()->getScalarType();
5589
5590 // f32 denormal always flushed.
5591 if (EltTy->isFloatTy())
5592 Known.knownNot(fcPosSubnormal);
5593
5594 if (const Function *F = II->getFunction()) {
5595 DenormalMode Mode = F->getDenormalMode(EltTy->getFltSemantics());
5596
5597 // -0 -> -inf
5598 if (KnownSrc.isKnownNeverLogicalNegZero(Mode))
5599 Known.knownNot(fcNegInf);
5600
5601 // +0 -> +inf
5602 if (KnownSrc.isKnownNeverLogicalPosZero(Mode))
5603 Known.knownNot(fcPosInf);
5604 }
5605
5606 break;
5607 }
5608 case Intrinsic::amdgcn_trig_preop: {
5609 // Always returns a value [0, 1)
5610 Known.knownNot(fcNan | fcInf | fcNegative);
5611 break;
5612 }
5613 default:
5614 break;
5615 }
5616
5617 break;
5618 }
5619 case Instruction::FAdd:
5620 case Instruction::FSub: {
5621 KnownFPClass KnownLHS, KnownRHS;
5622 bool WantNegative =
5623 Op->getOpcode() == Instruction::FAdd &&
5624 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;
5625 bool WantNaN = (InterestedClasses & fcNan) != fcNone;
5626 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;
5627
5628 if (!WantNaN && !WantNegative && !WantNegZero)
5629 break;
5630
5631 FPClassTest InterestedSrcs = InterestedClasses;
5632 if (WantNegative)
5633 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5634 if (InterestedClasses & fcNan)
5635 InterestedSrcs |= fcInf;
5636 computeKnownFPClass(Op->getOperand(1), DemandedElts, InterestedSrcs,
5637 KnownRHS, Q, Depth + 1);
5638
5639 // Special case fadd x, x, which is the canonical form of fmul x, 2.
5640 bool Self = Op->getOperand(0) == Op->getOperand(1) &&
5641 isGuaranteedNotToBeUndef(Op->getOperand(0), Q.AC, Q.CxtI, Q.DT,
5642 Depth + 1);
5643 if (Self)
5644 KnownLHS = KnownRHS;
5645
5646 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||
5647 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) ||
5648 WantNegZero || Opc == Instruction::FSub) {
5649
5650 // FIXME: Context function should always be passed in separately
5651 const Function *F = cast<Instruction>(Op)->getFunction();
5652 const fltSemantics &FltSem =
5653 Op->getType()->getScalarType()->getFltSemantics();
5655 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5656
5657 if (Self && Opc == Instruction::FAdd) {
5658 Known = KnownFPClass::fadd_self(KnownLHS, Mode);
5659 } else {
5660 // RHS is canonically cheaper to compute. Skip inspecting the LHS if
5661 // there's no point.
5662
5663 if (!Self) {
5664 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedSrcs,
5665 KnownLHS, Q, Depth + 1);
5666 }
5667
5668 Known = Opc == Instruction::FAdd
5669 ? KnownFPClass::fadd(KnownLHS, KnownRHS, Mode)
5670 : KnownFPClass::fsub(KnownLHS, KnownRHS, Mode);
5671 }
5672 }
5673
5674 break;
5675 }
5676 case Instruction::FMul: {
5677 const Function *F = cast<Instruction>(Op)->getFunction();
5679 F ? F->getDenormalMode(
5680 Op->getType()->getScalarType()->getFltSemantics())
5682
5683 Value *LHS = Op->getOperand(0);
5684 Value *RHS = Op->getOperand(1);
5685 // X * X is always non-negative or a NaN.
5686 // FIXME: Should check isGuaranteedNotToBeUndef
5687 if (LHS == RHS) {
5688 KnownFPClass KnownSrc;
5689 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownSrc, Q,
5690 Depth + 1);
5691 Known = KnownFPClass::square(KnownSrc, Mode);
5692 break;
5693 }
5694
5695 KnownFPClass KnownLHS, KnownRHS;
5696
5697 const APFloat *CRHS;
5698 if (match(RHS, m_APFloat(CRHS))) {
5699 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Q,
5700 Depth + 1);
5701 Known = KnownFPClass::fmul(KnownLHS, *CRHS, Mode);
5702 } else {
5703 computeKnownFPClass(RHS, DemandedElts, fcAllFlags, KnownRHS, Q,
5704 Depth + 1);
5705 // TODO: Improve accuracy in unfused FMA pattern. We can prove an
5706 // additional not-nan if the addend is known-not negative infinity if the
5707 // multiply is known-not infinity.
5708
5709 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Q,
5710 Depth + 1);
5711 Known = KnownFPClass::fmul(KnownLHS, KnownRHS, Mode);
5712 }
5713
5714 /// Propgate no-infs if the other source is known smaller than one, such
5715 /// that this cannot introduce overflow.
5716 if (KnownLHS.isKnownNever(fcInf) && isAbsoluteValueULEOne(RHS))
5717 Known.knownNot(fcInf);
5718 else if (KnownRHS.isKnownNever(fcInf) && isAbsoluteValueULEOne(LHS))
5719 Known.knownNot(fcInf);
5720
5721 break;
5722 }
5723 case Instruction::FDiv:
5724 case Instruction::FRem: {
5725 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
5726
5727 if (Op->getOpcode() == Instruction::FRem)
5728 Known.knownNot(fcInf);
5729
5730 if (Op->getOperand(0) == Op->getOperand(1) &&
5731 isGuaranteedNotToBeUndef(Op->getOperand(0), Q.AC, Q.CxtI, Q.DT)) {
5732 if (Op->getOpcode() == Instruction::FDiv) {
5733 // X / X is always exactly 1.0 or a NaN.
5735 } else {
5736 // X % X is always exactly [+-]0.0 or a NaN.
5737 Known.KnownFPClasses = fcNan | fcZero;
5738 }
5739
5740 if (!WantNan)
5741 break;
5742
5743 KnownFPClass KnownSrc;
5744 computeKnownFPClass(Op->getOperand(0), DemandedElts,
5745 fcNan | fcInf | fcZero | fcSubnormal, KnownSrc, Q,
5746 Depth + 1);
5747 const Function *F = cast<Instruction>(Op)->getFunction();
5748 const fltSemantics &FltSem =
5749 Op->getType()->getScalarType()->getFltSemantics();
5750
5752 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5753
5754 Known = Op->getOpcode() == Instruction::FDiv
5755 ? KnownFPClass::fdiv_self(KnownSrc, Mode)
5756 : KnownFPClass::frem_self(KnownSrc, Mode);
5757 break;
5758 }
5759
5760 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
5761 const bool WantPositive =
5762 Opc == Instruction::FRem && (InterestedClasses & fcPositive) != fcNone;
5763 if (!WantNan && !WantNegative && !WantPositive)
5764 break;
5765
5766 KnownFPClass KnownLHS, KnownRHS;
5767
5768 computeKnownFPClass(Op->getOperand(1), DemandedElts,
5769 fcNan | fcInf | fcZero | fcNegative, KnownRHS, Q,
5770 Depth + 1);
5771
5772 bool KnowSomethingUseful = KnownRHS.isKnownNeverNaN() ||
5773 KnownRHS.isKnownNever(fcNegative) ||
5774 KnownRHS.isKnownNever(fcPositive);
5775
5776 if (KnowSomethingUseful || WantPositive) {
5777 computeKnownFPClass(Op->getOperand(0), DemandedElts, fcAllFlags, KnownLHS,
5778 Q, Depth + 1);
5779 }
5780
5781 const Function *F = cast<Instruction>(Op)->getFunction();
5782 const fltSemantics &FltSem =
5783 Op->getType()->getScalarType()->getFltSemantics();
5784
5785 if (Op->getOpcode() == Instruction::FDiv) {
5787 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5788 Known = KnownFPClass::fdiv(KnownLHS, KnownRHS, Mode);
5789 } else {
5790 // Inf REM x and x REM 0 produce NaN.
5791 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5792 KnownLHS.isKnownNeverInfinity() && F &&
5793 KnownRHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))) {
5794 Known.knownNot(fcNan);
5795 }
5796
5797 // The sign for frem is the same as the first operand.
5798 if (KnownLHS.cannotBeOrderedLessThanZero())
5800 if (KnownLHS.cannotBeOrderedGreaterThanZero())
5802
5803 // See if we can be more aggressive about the sign of 0.
5804 if (KnownLHS.isKnownNever(fcNegative))
5805 Known.knownNot(fcNegative);
5806 if (KnownLHS.isKnownNever(fcPositive))
5807 Known.knownNot(fcPositive);
5808 }
5809
5810 break;
5811 }
5812 case Instruction::FPExt: {
5813 KnownFPClass KnownSrc;
5814 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5815 KnownSrc, Q, Depth + 1);
5816
5817 const fltSemantics &DstTy =
5818 Op->getType()->getScalarType()->getFltSemantics();
5819 const fltSemantics &SrcTy =
5820 Op->getOperand(0)->getType()->getScalarType()->getFltSemantics();
5821
5822 Known = KnownFPClass::fpext(KnownSrc, DstTy, SrcTy);
5823 break;
5824 }
5825 case Instruction::FPTrunc: {
5826 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known, Q,
5827 Depth);
5828 break;
5829 }
5830 case Instruction::SIToFP:
5831 case Instruction::UIToFP: {
5832 // Cannot produce nan
5833 Known.knownNot(fcNan);
5834
5835 // Integers cannot be subnormal
5836 Known.knownNot(fcSubnormal);
5837
5838 // sitofp and uitofp turn into +0.0 for zero.
5839 Known.knownNot(fcNegZero);
5840
5841 // UIToFP is always non-negative regardless of known bits.
5842 if (Op->getOpcode() == Instruction::UIToFP)
5843 Known.signBitMustBeZero();
5844
5845 // Only compute known bits if we can learn something useful from them.
5846 if (!(InterestedClasses & (fcPosZero | fcNormal | fcInf)))
5847 break;
5848
5849 KnownBits IntKnown =
5850 computeKnownBits(Op->getOperand(0), DemandedElts, Q, Depth + 1);
5851
5852 // If the integer is non-zero, the result cannot be +0.0
5853 if (IntKnown.isNonZero())
5854 Known.knownNot(fcPosZero);
5855
5856 if (Op->getOpcode() == Instruction::SIToFP) {
5857 // If the signed integer is known non-negative, the result is
5858 // non-negative. If the signed integer is known negative, the result is
5859 // negative.
5860 if (IntKnown.isNonNegative()) {
5861 Known.signBitMustBeZero();
5862 } else if (IntKnown.isNegative()) {
5863 Known.signBitMustBeOne();
5864 }
5865 }
5866
5867 // Guard kept for ilogb()
5868 if (InterestedClasses & fcInf) {
5869 // Get width of largest magnitude integer known.
5870 // This still works for a signed minimum value because the largest FP
5871 // value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx).
5872 int IntSize = IntKnown.getBitWidth();
5873 if (Op->getOpcode() == Instruction::UIToFP)
5874 IntSize -= IntKnown.countMinLeadingZeros();
5875 else if (Op->getOpcode() == Instruction::SIToFP)
5876 IntSize -= IntKnown.countMinSignBits();
5877
5878 // If the exponent of the largest finite FP value can hold the largest
5879 // integer, the result of the cast must be finite.
5880 Type *FPTy = Op->getType()->getScalarType();
5881 if (ilogb(APFloat::getLargest(FPTy->getFltSemantics())) >= IntSize)
5882 Known.knownNot(fcInf);
5883 }
5884
5885 break;
5886 }
5887 case Instruction::ExtractElement: {
5888 // Look through extract element. If the index is non-constant or
5889 // out-of-range demand all elements, otherwise just the extracted element.
5890 const Value *Vec = Op->getOperand(0);
5891
5892 APInt DemandedVecElts;
5893 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
5894 unsigned NumElts = VecTy->getNumElements();
5895 DemandedVecElts = APInt::getAllOnes(NumElts);
5896 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(1));
5897 if (CIdx && CIdx->getValue().ult(NumElts))
5898 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
5899 } else {
5900 DemandedVecElts = APInt(1, 1);
5901 }
5902
5903 return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,
5904 Q, Depth + 1);
5905 }
5906 case Instruction::InsertElement: {
5907 if (isa<ScalableVectorType>(Op->getType()))
5908 return;
5909
5910 const Value *Vec = Op->getOperand(0);
5911 const Value *Elt = Op->getOperand(1);
5912 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(2));
5913 unsigned NumElts = DemandedElts.getBitWidth();
5914 APInt DemandedVecElts = DemandedElts;
5915 bool NeedsElt = true;
5916 // If we know the index we are inserting to, clear it from Vec check.
5917 if (CIdx && CIdx->getValue().ult(NumElts)) {
5918 DemandedVecElts.clearBit(CIdx->getZExtValue());
5919 NeedsElt = DemandedElts[CIdx->getZExtValue()];
5920 }
5921
5922 // Do we demand the inserted element?
5923 if (NeedsElt) {
5924 computeKnownFPClass(Elt, Known, InterestedClasses, Q, Depth + 1);
5925 // If we don't know any bits, early out.
5926 if (Known.isUnknown())
5927 break;
5928 } else {
5929 Known.KnownFPClasses = fcNone;
5930 }
5931
5932 // Do we need anymore elements from Vec?
5933 if (!DemandedVecElts.isZero()) {
5934 KnownFPClass Known2;
5935 computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2, Q,
5936 Depth + 1);
5937 Known |= Known2;
5938 }
5939
5940 break;
5941 }
5942 case Instruction::ShuffleVector: {
5943 // Handle vector splat idiom
5944 if (Value *Splat = getSplatValue(V)) {
5945 computeKnownFPClass(Splat, Known, InterestedClasses, Q, Depth + 1);
5946 break;
5947 }
5948
5949 // For undef elements, we don't know anything about the common state of
5950 // the shuffle result.
5951 APInt DemandedLHS, DemandedRHS;
5952 auto *Shuf = dyn_cast<ShuffleVectorInst>(Op);
5953 if (!Shuf || !getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
5954 return;
5955
5956 if (!!DemandedLHS) {
5957 const Value *LHS = Shuf->getOperand(0);
5958 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known, Q,
5959 Depth + 1);
5960
5961 // If we don't know any bits, early out.
5962 if (Known.isUnknown())
5963 break;
5964 } else {
5965 Known.KnownFPClasses = fcNone;
5966 }
5967
5968 if (!!DemandedRHS) {
5969 KnownFPClass Known2;
5970 const Value *RHS = Shuf->getOperand(1);
5971 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2, Q,
5972 Depth + 1);
5973 Known |= Known2;
5974 }
5975
5976 break;
5977 }
5978 case Instruction::ExtractValue: {
5979 const ExtractValueInst *Extract = cast<ExtractValueInst>(Op);
5980 ArrayRef<unsigned> Indices = Extract->getIndices();
5981 const Value *Src = Extract->getAggregateOperand();
5982 if (isa<StructType>(Src->getType()) && Indices.size() == 1 &&
5983 Indices[0] == 0) {
5984 if (const auto *II = dyn_cast<IntrinsicInst>(Src)) {
5985 switch (II->getIntrinsicID()) {
5986 case Intrinsic::frexp: {
5987 Known.knownNot(fcSubnormal);
5988
5989 KnownFPClass KnownSrc;
5990 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5991 InterestedClasses, KnownSrc, Q, Depth + 1);
5992
5993 const Function *F = cast<Instruction>(Op)->getFunction();
5994 const fltSemantics &FltSem =
5995 Op->getType()->getScalarType()->getFltSemantics();
5996
5998 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5999 Known = KnownFPClass::frexp_mant(KnownSrc, Mode);
6000 return;
6001 }
6002 default:
6003 break;
6004 }
6005 }
6006 }
6007
6008 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Q,
6009 Depth + 1);
6010 break;
6011 }
6012 case Instruction::PHI: {
6013 const PHINode *P = cast<PHINode>(Op);
6014 // Unreachable blocks may have zero-operand PHI nodes.
6015 if (P->getNumIncomingValues() == 0)
6016 break;
6017
6018 // Otherwise take the unions of the known bit sets of the operands,
6019 // taking conservative care to avoid excessive recursion.
6020 const unsigned PhiRecursionLimit = MaxAnalysisRecursionDepth - 2;
6021
6022 if (Depth < PhiRecursionLimit) {
6023 // Skip if every incoming value references to ourself.
6024 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
6025 break;
6026
6027 bool First = true;
6028
6029 for (const Use &U : P->operands()) {
6030 Value *IncValue;
6031 Instruction *CxtI;
6032 breakSelfRecursivePHI(&U, P, IncValue, CxtI);
6033 // Skip direct self references.
6034 if (IncValue == P)
6035 continue;
6036
6037 KnownFPClass KnownSrc;
6038 // Recurse, but cap the recursion to two levels, because we don't want
6039 // to waste time spinning around in loops. We need at least depth 2 to
6040 // detect known sign bits.
6041 computeKnownFPClass(IncValue, DemandedElts, InterestedClasses, KnownSrc,
6043 PhiRecursionLimit);
6044
6045 if (First) {
6046 Known = KnownSrc;
6047 First = false;
6048 } else {
6049 Known |= KnownSrc;
6050 }
6051
6052 if (Known.KnownFPClasses == fcAllFlags)
6053 break;
6054 }
6055 }
6056
6057 // Look for the case of a for loop which has a positive
6058 // initial value and is incremented by a squared value.
6059 // This will propagate sign information out of such loops.
6060 if (P->getNumIncomingValues() != 2 || Known.cannotBeOrderedLessThanZero())
6061 break;
6062 for (unsigned I = 0; I < 2; I++) {
6063 Value *RecurValue = P->getIncomingValue(1 - I);
6065 if (!II)
6066 continue;
6067 Value *R, *L, *Init;
6068 PHINode *PN;
6070 PN == P) {
6071 switch (II->getIntrinsicID()) {
6072 case Intrinsic::fma:
6073 case Intrinsic::fmuladd: {
6074 KnownFPClass KnownStart;
6075 computeKnownFPClass(Init, DemandedElts, InterestedClasses, KnownStart,
6076 Q, Depth + 1);
6077 if (KnownStart.cannotBeOrderedLessThanZero() && L == R &&
6078 isGuaranteedNotToBeUndef(L, Q.AC, Q.CxtI, Q.DT, Depth + 1))
6080 break;
6081 }
6082 }
6083 }
6084 }
6085 break;
6086 }
6087 case Instruction::BitCast: {
6088 const Value *Src;
6089 if (!match(Op, m_ElementWiseBitCast(m_Value(Src))) ||
6090 !Src->getType()->isIntOrIntVectorTy())
6091 break;
6092
6093 const Type *Ty = Op->getType();
6094
6095 Value *CastLHS, *CastRHS;
6096
6097 // Match bitcast(umax(bitcast(a), bitcast(b)))
6098 if (match(Src, m_c_MaxOrMin(m_BitCast(m_Value(CastLHS)),
6099 m_BitCast(m_Value(CastRHS)))) &&
6100 CastLHS->getType() == Ty && CastRHS->getType() == Ty) {
6101 KnownFPClass KnownLHS, KnownRHS;
6102 computeKnownFPClass(CastRHS, DemandedElts, InterestedClasses, KnownRHS, Q,
6103 Depth + 1);
6104 if (!KnownRHS.isUnknown()) {
6105 computeKnownFPClass(CastLHS, DemandedElts, InterestedClasses, KnownLHS,
6106 Q, Depth + 1);
6107 Known = KnownLHS | KnownRHS;
6108 }
6109
6110 return;
6111 }
6112
6113 const Type *EltTy = Ty->getScalarType();
6114 KnownBits Bits(EltTy->getPrimitiveSizeInBits());
6115 computeKnownBits(Src, DemandedElts, Bits, Q, Depth + 1);
6116
6117 Known = KnownFPClass::bitcast(EltTy->getFltSemantics(), Bits);
6118 break;
6119 }
6120 default:
6121 break;
6122 }
6123}
6124
6126 const APInt &DemandedElts,
6127 FPClassTest InterestedClasses,
6128 const SimplifyQuery &SQ,
6129 unsigned Depth) {
6130 KnownFPClass KnownClasses;
6131 ::computeKnownFPClass(V, DemandedElts, InterestedClasses, KnownClasses, SQ,
6132 Depth);
6133 return KnownClasses;
6134}
6135
6137 FPClassTest InterestedClasses,
6138 const SimplifyQuery &SQ,
6139 unsigned Depth) {
6140 KnownFPClass Known;
6141 ::computeKnownFPClass(V, Known, InterestedClasses, SQ, Depth);
6142 return Known;
6143}
6144
6146 const Value *V, const DataLayout &DL, FPClassTest InterestedClasses,
6147 const TargetLibraryInfo *TLI, AssumptionCache *AC, const Instruction *CxtI,
6148 const DominatorTree *DT, bool UseInstrInfo, unsigned Depth) {
6149 return computeKnownFPClass(V, InterestedClasses,
6150 SimplifyQuery(DL, TLI, DT, AC, CxtI, UseInstrInfo),
6151 Depth);
6152}
6153
6155llvm::computeKnownFPClass(const Value *V, const APInt &DemandedElts,
6156 FastMathFlags FMF, FPClassTest InterestedClasses,
6157 const SimplifyQuery &SQ, unsigned Depth) {
6158 if (FMF.noNaNs())
6159 InterestedClasses &= ~fcNan;
6160 if (FMF.noInfs())
6161 InterestedClasses &= ~fcInf;
6162
6163 KnownFPClass Result =
6164 computeKnownFPClass(V, DemandedElts, InterestedClasses, SQ, Depth);
6165
6166 if (FMF.noNaNs())
6167 Result.KnownFPClasses &= ~fcNan;
6168 if (FMF.noInfs())
6169 Result.KnownFPClasses &= ~fcInf;
6170 return Result;
6171}
6172
6174 FPClassTest InterestedClasses,
6175 const SimplifyQuery &SQ,
6176 unsigned Depth) {
6177 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
6178 APInt DemandedElts =
6179 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
6180 return computeKnownFPClass(V, DemandedElts, FMF, InterestedClasses, SQ,
6181 Depth);
6182}
6183
6185 unsigned Depth) {
6187 return Known.isKnownNeverNegZero();
6188}
6189
6196
6198 unsigned Depth) {
6200 return Known.isKnownNeverInfinity();
6201}
6202
6203/// Return true if the floating-point value can never contain a NaN or infinity.
6205 unsigned Depth) {
6207 return Known.isKnownNeverNaN() && Known.isKnownNeverInfinity();
6208}
6209
6210/// Return true if the floating-point scalar value is not a NaN or if the
6211/// floating-point vector value has no NaN elements. Return false if a value
6212/// could ever be NaN.
6214 unsigned Depth) {
6216 return Known.isKnownNeverNaN();
6217}
6218
6219/// Return false if we can prove that the specified FP value's sign bit is 0.
6220/// Return true if we can prove that the specified FP value's sign bit is 1.
6221/// Otherwise return std::nullopt.
6222std::optional<bool> llvm::computeKnownFPSignBit(const Value *V,
6223 const SimplifyQuery &SQ,
6224 unsigned Depth) {
6226 return Known.SignBit;
6227}
6228
6230 auto *User = cast<Instruction>(U.getUser());
6231 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6232 if (FPOp->hasNoSignedZeros())
6233 return true;
6234 }
6235
6236 switch (User->getOpcode()) {
6237 case Instruction::FPToSI:
6238 case Instruction::FPToUI:
6239 return true;
6240 case Instruction::FCmp:
6241 // fcmp treats both positive and negative zero as equal.
6242 return true;
6243 case Instruction::Call:
6244 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6245 switch (II->getIntrinsicID()) {
6246 case Intrinsic::fabs:
6247 return true;
6248 case Intrinsic::copysign:
6249 return U.getOperandNo() == 0;
6250 case Intrinsic::is_fpclass:
6251 case Intrinsic::vp_is_fpclass: {
6252 auto Test =
6253 static_cast<FPClassTest>(
6254 cast<ConstantInt>(II->getArgOperand(1))->getZExtValue()) &
6257 }
6258 default:
6259 return false;
6260 }
6261 }
6262 return false;
6263 default:
6264 return false;
6265 }
6266}
6267
6269 auto *User = cast<Instruction>(U.getUser());
6270 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6271 if (FPOp->hasNoNaNs())
6272 return true;
6273 }
6274
6275 switch (User->getOpcode()) {
6276 case Instruction::FPToSI:
6277 case Instruction::FPToUI:
6278 return true;
6279 // Proper FP math operations ignore the sign bit of NaN.
6280 case Instruction::FAdd:
6281 case Instruction::FSub:
6282 case Instruction::FMul:
6283 case Instruction::FDiv:
6284 case Instruction::FRem:
6285 case Instruction::FPTrunc:
6286 case Instruction::FPExt:
6287 case Instruction::FCmp:
6288 return true;
6289 // Bitwise FP operations should preserve the sign bit of NaN.
6290 case Instruction::FNeg:
6291 case Instruction::Select:
6292 case Instruction::PHI:
6293 return false;
6294 case Instruction::Ret:
6295 return User->getFunction()->getAttributes().getRetNoFPClass() &
6297 case Instruction::Call:
6298 case Instruction::Invoke: {
6299 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6300 switch (II->getIntrinsicID()) {
6301 case Intrinsic::fabs:
6302 return true;
6303 case Intrinsic::copysign:
6304 return U.getOperandNo() == 0;
6305 // Other proper FP math intrinsics ignore the sign bit of NaN.
6306 case Intrinsic::maxnum:
6307 case Intrinsic::minnum:
6308 case Intrinsic::maximum:
6309 case Intrinsic::minimum:
6310 case Intrinsic::maximumnum:
6311 case Intrinsic::minimumnum:
6312 case Intrinsic::canonicalize:
6313 case Intrinsic::fma:
6314 case Intrinsic::fmuladd:
6315 case Intrinsic::sqrt:
6316 case Intrinsic::pow:
6317 case Intrinsic::powi:
6318 case Intrinsic::fptoui_sat:
6319 case Intrinsic::fptosi_sat:
6320 case Intrinsic::is_fpclass:
6321 case Intrinsic::vp_is_fpclass:
6322 return true;
6323 default:
6324 return false;
6325 }
6326 }
6327
6328 FPClassTest NoFPClass =
6329 cast<CallBase>(User)->getParamNoFPClass(U.getOperandNo());
6330 return NoFPClass & FPClassTest::fcNan;
6331 }
6332 default:
6333 return false;
6334 }
6335}
6336
6338 FastMathFlags FMF) {
6339 if (isa<PoisonValue>(V))
6340 return true;
6341 if (isa<UndefValue>(V))
6342 return false;
6343
6344 if (match(V, m_CheckedFp([](const APFloat &Val) { return Val.isInteger(); })))
6345 return true;
6346
6348 if (!I)
6349 return false;
6350
6351 switch (I->getOpcode()) {
6352 case Instruction::SIToFP:
6353 case Instruction::UIToFP:
6354 // TODO: Could check nofpclass(inf) on incoming argument
6355 if (FMF.noInfs())
6356 return true;
6357
6358 // Need to check int size cannot produce infinity, which computeKnownFPClass
6359 // knows how to do already.
6360 return isKnownNeverInfinity(I, SQ);
6361 case Instruction::Call: {
6362 const CallInst *CI = cast<CallInst>(I);
6363 switch (CI->getIntrinsicID()) {
6364 case Intrinsic::trunc:
6365 case Intrinsic::floor:
6366 case Intrinsic::ceil:
6367 case Intrinsic::rint:
6368 case Intrinsic::nearbyint:
6369 case Intrinsic::round:
6370 case Intrinsic::roundeven:
6371 return (FMF.noInfs() && FMF.noNaNs()) || isKnownNeverInfOrNaN(I, SQ);
6372 default:
6373 break;
6374 }
6375
6376 break;
6377 }
6378 default:
6379 break;
6380 }
6381
6382 return false;
6383}
6384
6386
6387 // All byte-wide stores are splatable, even of arbitrary variables.
6388 if (V->getType()->isIntegerTy(8))
6389 return V;
6390
6391 LLVMContext &Ctx = V->getContext();
6392
6393 // Undef don't care.
6394 auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
6395 if (isa<UndefValue>(V))
6396 return UndefInt8;
6397
6398 // Return poison for zero-sized type.
6399 if (DL.getTypeStoreSize(V->getType()).isZero())
6400 return PoisonValue::get(Type::getInt8Ty(Ctx));
6401
6403 if (!C) {
6404 // Conceptually, we could handle things like:
6405 // %a = zext i8 %X to i16
6406 // %b = shl i16 %a, 8
6407 // %c = or i16 %a, %b
6408 // but until there is an example that actually needs this, it doesn't seem
6409 // worth worrying about.
6410 return nullptr;
6411 }
6412
6413 // Handle 'null' ConstantArrayZero etc.
6414 if (C->isNullValue())
6416
6417 // Constant floating-point values can be handled as integer values if the
6418 // corresponding integer value is "byteable". An important case is 0.0.
6419 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
6420 Type *ScalarTy = CFP->getType()->getScalarType();
6421 if (ScalarTy->isHalfTy() || ScalarTy->isFloatTy() || ScalarTy->isDoubleTy())
6422 return isBytewiseValue(
6423 ConstantInt::get(Ctx, CFP->getValue().bitcastToAPInt()), DL);
6424
6425 // Don't handle long double formats, which have strange constraints.
6426 return nullptr;
6427 }
6428
6429 // We can handle constant integers that are multiple of 8 bits.
6430 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
6431 if (CI->getBitWidth() % 8 == 0) {
6432 if (!CI->getValue().isSplat(8))
6433 return nullptr;
6434 return ConstantInt::get(Ctx, CI->getValue().trunc(8));
6435 }
6436 }
6437
6438 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
6439 if (CE->getOpcode() == Instruction::IntToPtr) {
6440 if (auto *PtrTy = dyn_cast<PointerType>(CE->getType())) {
6441 unsigned BitWidth = DL.getPointerSizeInBits(PtrTy->getAddressSpace());
6443 CE->getOperand(0), Type::getIntNTy(Ctx, BitWidth), false, DL))
6444 return isBytewiseValue(Op, DL);
6445 }
6446 }
6447 }
6448
6449 auto Merge = [&](Value *LHS, Value *RHS) -> Value * {
6450 if (LHS == RHS)
6451 return LHS;
6452 if (!LHS || !RHS)
6453 return nullptr;
6454 if (LHS == UndefInt8)
6455 return RHS;
6456 if (RHS == UndefInt8)
6457 return LHS;
6458 return nullptr;
6459 };
6460
6462 Value *Val = UndefInt8;
6463 for (uint64_t I = 0, E = CA->getNumElements(); I != E; ++I)
6464 if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))
6465 return nullptr;
6466 return Val;
6467 }
6468
6470 Value *Val = UndefInt8;
6471 for (Value *Op : C->operands())
6472 if (!(Val = Merge(Val, isBytewiseValue(Op, DL))))
6473 return nullptr;
6474 return Val;
6475 }
6476
6477 // Don't try to handle the handful of other constants.
6478 return nullptr;
6479}
6480
6481// This is the recursive version of BuildSubAggregate. It takes a few different
6482// arguments. Idxs is the index within the nested struct From that we are
6483// looking at now (which is of type IndexedType). IdxSkip is the number of
6484// indices from Idxs that should be left out when inserting into the resulting
6485// struct. To is the result struct built so far, new insertvalue instructions
6486// build on that.
6487static Value *BuildSubAggregate(Value *From, Value *To, Type *IndexedType,
6489 unsigned IdxSkip,
6490 BasicBlock::iterator InsertBefore) {
6491 StructType *STy = dyn_cast<StructType>(IndexedType);
6492 if (STy) {
6493 // Save the original To argument so we can modify it
6494 Value *OrigTo = To;
6495 // General case, the type indexed by Idxs is a struct
6496 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
6497 // Process each struct element recursively
6498 Idxs.push_back(i);
6499 Value *PrevTo = To;
6500 To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
6501 InsertBefore);
6502 Idxs.pop_back();
6503 if (!To) {
6504 // Couldn't find any inserted value for this index? Cleanup
6505 while (PrevTo != OrigTo) {
6507 PrevTo = Del->getAggregateOperand();
6508 Del->eraseFromParent();
6509 }
6510 // Stop processing elements
6511 break;
6512 }
6513 }
6514 // If we successfully found a value for each of our subaggregates
6515 if (To)
6516 return To;
6517 }
6518 // Base case, the type indexed by SourceIdxs is not a struct, or not all of
6519 // the struct's elements had a value that was inserted directly. In the latter
6520 // case, perhaps we can't determine each of the subelements individually, but
6521 // we might be able to find the complete struct somewhere.
6522
6523 // Find the value that is at that particular spot
6524 Value *V = FindInsertedValue(From, Idxs);
6525
6526 if (!V)
6527 return nullptr;
6528
6529 // Insert the value in the new (sub) aggregate
6530 return InsertValueInst::Create(To, V, ArrayRef(Idxs).slice(IdxSkip), "tmp",
6531 InsertBefore);
6532}
6533
6534// This helper takes a nested struct and extracts a part of it (which is again a
6535// struct) into a new value. For example, given the struct:
6536// { a, { b, { c, d }, e } }
6537// and the indices "1, 1" this returns
6538// { c, d }.
6539//
6540// It does this by inserting an insertvalue for each element in the resulting
6541// struct, as opposed to just inserting a single struct. This will only work if
6542// each of the elements of the substruct are known (ie, inserted into From by an
6543// insertvalue instruction somewhere).
6544//
6545// All inserted insertvalue instructions are inserted before InsertBefore
6547 BasicBlock::iterator InsertBefore) {
6548 Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
6549 idx_range);
6550 Value *To = PoisonValue::get(IndexedType);
6551 SmallVector<unsigned, 10> Idxs(idx_range);
6552 unsigned IdxSkip = Idxs.size();
6553
6554 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
6555}
6556
6557/// Given an aggregate and a sequence of indices, see if the scalar value
6558/// indexed is already around as a register, for example if it was inserted
6559/// directly into the aggregate.
6560///
6561/// If InsertBefore is not null, this function will duplicate (modified)
6562/// insertvalues when a part of a nested struct is extracted.
6563Value *
6565 std::optional<BasicBlock::iterator> InsertBefore) {
6566 // Nothing to index? Just return V then (this is useful at the end of our
6567 // recursion).
6568 if (idx_range.empty())
6569 return V;
6570 // We have indices, so V should have an indexable type.
6571 assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
6572 "Not looking at a struct or array?");
6573 assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
6574 "Invalid indices for type?");
6575
6576 if (Constant *C = dyn_cast<Constant>(V)) {
6577 C = C->getAggregateElement(idx_range[0]);
6578 if (!C) return nullptr;
6579 return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
6580 }
6581
6583 // Loop the indices for the insertvalue instruction in parallel with the
6584 // requested indices
6585 const unsigned *req_idx = idx_range.begin();
6586 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
6587 i != e; ++i, ++req_idx) {
6588 if (req_idx == idx_range.end()) {
6589 // We can't handle this without inserting insertvalues
6590 if (!InsertBefore)
6591 return nullptr;
6592
6593 // The requested index identifies a part of a nested aggregate. Handle
6594 // this specially. For example,
6595 // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
6596 // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
6597 // %C = extractvalue {i32, { i32, i32 } } %B, 1
6598 // This can be changed into
6599 // %A = insertvalue {i32, i32 } undef, i32 10, 0
6600 // %C = insertvalue {i32, i32 } %A, i32 11, 1
6601 // which allows the unused 0,0 element from the nested struct to be
6602 // removed.
6603 return BuildSubAggregate(V, ArrayRef(idx_range.begin(), req_idx),
6604 *InsertBefore);
6605 }
6606
6607 // This insert value inserts something else than what we are looking for.
6608 // See if the (aggregate) value inserted into has the value we are
6609 // looking for, then.
6610 if (*req_idx != *i)
6611 return FindInsertedValue(I->getAggregateOperand(), idx_range,
6612 InsertBefore);
6613 }
6614 // If we end up here, the indices of the insertvalue match with those
6615 // requested (though possibly only partially). Now we recursively look at
6616 // the inserted value, passing any remaining indices.
6617 return FindInsertedValue(I->getInsertedValueOperand(),
6618 ArrayRef(req_idx, idx_range.end()), InsertBefore);
6619 }
6620
6622 // If we're extracting a value from an aggregate that was extracted from
6623 // something else, we can extract from that something else directly instead.
6624 // However, we will need to chain I's indices with the requested indices.
6625
6626 // Calculate the number of indices required
6627 unsigned size = I->getNumIndices() + idx_range.size();
6628 // Allocate some space to put the new indices in
6630 Idxs.reserve(size);
6631 // Add indices from the extract value instruction
6632 Idxs.append(I->idx_begin(), I->idx_end());
6633
6634 // Add requested indices
6635 Idxs.append(idx_range.begin(), idx_range.end());
6636
6637 assert(Idxs.size() == size
6638 && "Number of indices added not correct?");
6639
6640 return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
6641 }
6642 // Otherwise, we don't know (such as, extracting from a function return value
6643 // or load instruction)
6644 return nullptr;
6645}
6646
6647// If V refers to an initialized global constant, set Slice either to
6648// its initializer if the size of its elements equals ElementSize, or,
6649// for ElementSize == 8, to its representation as an array of unsiged
6650// char. Return true on success.
6651// Offset is in the unit "nr of ElementSize sized elements".
6654 unsigned ElementSize, uint64_t Offset) {
6655 assert(V && "V should not be null.");
6656 assert((ElementSize % 8) == 0 &&
6657 "ElementSize expected to be a multiple of the size of a byte.");
6658 unsigned ElementSizeInBytes = ElementSize / 8;
6659
6660 // Drill down into the pointer expression V, ignoring any intervening
6661 // casts, and determine the identity of the object it references along
6662 // with the cumulative byte offset into it.
6663 const GlobalVariable *GV =
6665 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
6666 // Fail if V is not based on constant global object.
6667 return false;
6668
6669 const DataLayout &DL = GV->getDataLayout();
6670 APInt Off(DL.getIndexTypeSizeInBits(V->getType()), 0);
6671
6672 if (GV != V->stripAndAccumulateConstantOffsets(DL, Off,
6673 /*AllowNonInbounds*/ true))
6674 // Fail if a constant offset could not be determined.
6675 return false;
6676
6677 uint64_t StartIdx = Off.getLimitedValue();
6678 if (StartIdx == UINT64_MAX)
6679 // Fail if the constant offset is excessive.
6680 return false;
6681
6682 // Off/StartIdx is in the unit of bytes. So we need to convert to number of
6683 // elements. Simply bail out if that isn't possible.
6684 if ((StartIdx % ElementSizeInBytes) != 0)
6685 return false;
6686
6687 Offset += StartIdx / ElementSizeInBytes;
6688 ConstantDataArray *Array = nullptr;
6689 ArrayType *ArrayTy = nullptr;
6690
6691 if (GV->getInitializer()->isNullValue()) {
6692 Type *GVTy = GV->getValueType();
6693 uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy).getFixedValue();
6694 uint64_t Length = SizeInBytes / ElementSizeInBytes;
6695
6696 Slice.Array = nullptr;
6697 Slice.Offset = 0;
6698 // Return an empty Slice for undersized constants to let callers
6699 // transform even undefined library calls into simpler, well-defined
6700 // expressions. This is preferable to making the calls although it
6701 // prevents sanitizers from detecting such calls.
6702 Slice.Length = Length < Offset ? 0 : Length - Offset;
6703 return true;
6704 }
6705
6706 auto *Init = const_cast<Constant *>(GV->getInitializer());
6707 if (auto *ArrayInit = dyn_cast<ConstantDataArray>(Init)) {
6708 Type *InitElTy = ArrayInit->getElementType();
6709 if (InitElTy->isIntegerTy(ElementSize)) {
6710 // If Init is an initializer for an array of the expected type
6711 // and size, use it as is.
6712 Array = ArrayInit;
6713 ArrayTy = ArrayInit->getType();
6714 }
6715 }
6716
6717 if (!Array) {
6718 if (ElementSize != 8)
6719 // TODO: Handle conversions to larger integral types.
6720 return false;
6721
6722 // Otherwise extract the portion of the initializer starting
6723 // at Offset as an array of bytes, and reset Offset.
6725 if (!Init)
6726 return false;
6727
6728 Offset = 0;
6730 ArrayTy = dyn_cast<ArrayType>(Init->getType());
6731 }
6732
6733 uint64_t NumElts = ArrayTy->getArrayNumElements();
6734 if (Offset > NumElts)
6735 return false;
6736
6737 Slice.Array = Array;
6738 Slice.Offset = Offset;
6739 Slice.Length = NumElts - Offset;
6740 return true;
6741}
6742
6743/// Extract bytes from the initializer of the constant array V, which need
6744/// not be a nul-terminated string. On success, store the bytes in Str and
6745/// return true. When TrimAtNul is set, Str will contain only the bytes up
6746/// to but not including the first nul. Return false on failure.
6748 bool TrimAtNul) {
6750 if (!getConstantDataArrayInfo(V, Slice, 8))
6751 return false;
6752
6753 if (Slice.Array == nullptr) {
6754 if (TrimAtNul) {
6755 // Return a nul-terminated string even for an empty Slice. This is
6756 // safe because all existing SimplifyLibcalls callers require string
6757 // arguments and the behavior of the functions they fold is undefined
6758 // otherwise. Folding the calls this way is preferable to making
6759 // the undefined library calls, even though it prevents sanitizers
6760 // from reporting such calls.
6761 Str = StringRef();
6762 return true;
6763 }
6764 if (Slice.Length == 1) {
6765 Str = StringRef("", 1);
6766 return true;
6767 }
6768 // We cannot instantiate a StringRef as we do not have an appropriate string
6769 // of 0s at hand.
6770 return false;
6771 }
6772
6773 // Start out with the entire array in the StringRef.
6774 Str = Slice.Array->getAsString();
6775 // Skip over 'offset' bytes.
6776 Str = Str.substr(Slice.Offset);
6777
6778 if (TrimAtNul) {
6779 // Trim off the \0 and anything after it. If the array is not nul
6780 // terminated, we just return the whole end of string. The client may know
6781 // some other way that the string is length-bound.
6782 Str = Str.substr(0, Str.find('\0'));
6783 }
6784 return true;
6785}
6786
6787// These next two are very similar to the above, but also look through PHI
6788// nodes.
6789// TODO: See if we can integrate these two together.
6790
6791/// If we can compute the length of the string pointed to by
6792/// the specified pointer, return 'len+1'. If we can't, return 0.
6795 unsigned CharSize) {
6796 // Look through noop bitcast instructions.
6797 V = V->stripPointerCasts();
6798
6799 // If this is a PHI node, there are two cases: either we have already seen it
6800 // or we haven't.
6801 if (const PHINode *PN = dyn_cast<PHINode>(V)) {
6802 if (!PHIs.insert(PN).second)
6803 return ~0ULL; // already in the set.
6804
6805 // If it was new, see if all the input strings are the same length.
6806 uint64_t LenSoFar = ~0ULL;
6807 for (Value *IncValue : PN->incoming_values()) {
6808 uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize);
6809 if (Len == 0) return 0; // Unknown length -> unknown.
6810
6811 if (Len == ~0ULL) continue;
6812
6813 if (Len != LenSoFar && LenSoFar != ~0ULL)
6814 return 0; // Disagree -> unknown.
6815 LenSoFar = Len;
6816 }
6817
6818 // Success, all agree.
6819 return LenSoFar;
6820 }
6821
6822 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
6823 if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
6824 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);
6825 if (Len1 == 0) return 0;
6826 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);
6827 if (Len2 == 0) return 0;
6828 if (Len1 == ~0ULL) return Len2;
6829 if (Len2 == ~0ULL) return Len1;
6830 if (Len1 != Len2) return 0;
6831 return Len1;
6832 }
6833
6834 // Otherwise, see if we can read the string.
6836 if (!getConstantDataArrayInfo(V, Slice, CharSize))
6837 return 0;
6838
6839 if (Slice.Array == nullptr)
6840 // Zeroinitializer (including an empty one).
6841 return 1;
6842
6843 // Search for the first nul character. Return a conservative result even
6844 // when there is no nul. This is safe since otherwise the string function
6845 // being folded such as strlen is undefined, and can be preferable to
6846 // making the undefined library call.
6847 unsigned NullIndex = 0;
6848 for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) {
6849 if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0)
6850 break;
6851 }
6852
6853 return NullIndex + 1;
6854}
6855
6856/// If we can compute the length of the string pointed to by
6857/// the specified pointer, return 'len+1'. If we can't, return 0.
6858uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) {
6859 if (!V->getType()->isPointerTy())
6860 return 0;
6861
6863 uint64_t Len = GetStringLengthH(V, PHIs, CharSize);
6864 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
6865 // an empty string as a length.
6866 return Len == ~0ULL ? 1 : Len;
6867}
6868
6869const Value *
6871 bool MustPreserveNullness) {
6872 assert(Call &&
6873 "getArgumentAliasingToReturnedPointer only works on nonnull calls");
6874 if (const Value *RV = Call->getReturnedArgOperand())
6875 return RV;
6876 // This can be used only as a aliasing property.
6878 Call, MustPreserveNullness))
6879 return Call->getArgOperand(0);
6880 return nullptr;
6881}
6882
6884 const CallBase *Call, bool MustPreserveNullness) {
6885 switch (Call->getIntrinsicID()) {
6886 case Intrinsic::launder_invariant_group:
6887 case Intrinsic::strip_invariant_group:
6888 case Intrinsic::aarch64_irg:
6889 case Intrinsic::aarch64_tagp:
6890 // The amdgcn_make_buffer_rsrc function does not alter the address of the
6891 // input pointer (and thus preserve null-ness for the purposes of escape
6892 // analysis, which is where the MustPreserveNullness flag comes in to play).
6893 // However, it will not necessarily map ptr addrspace(N) null to ptr
6894 // addrspace(8) null, aka the "null descriptor", which has "all loads return
6895 // 0, all stores are dropped" semantics. Given the context of this intrinsic
6896 // list, no one should be relying on such a strict interpretation of
6897 // MustPreserveNullness (and, at time of writing, they are not), but we
6898 // document this fact out of an abundance of caution.
6899 case Intrinsic::amdgcn_make_buffer_rsrc:
6900 return true;
6901 case Intrinsic::ptrmask:
6902 return !MustPreserveNullness;
6903 case Intrinsic::threadlocal_address:
6904 // The underlying variable changes with thread ID. The Thread ID may change
6905 // at coroutine suspend points.
6906 return !Call->getParent()->getParent()->isPresplitCoroutine();
6907 default:
6908 return false;
6909 }
6910}
6911
6912/// \p PN defines a loop-variant pointer to an object. Check if the
6913/// previous iteration of the loop was referring to the same object as \p PN.
6915 const LoopInfo *LI) {
6916 // Find the loop-defined value.
6917 Loop *L = LI->getLoopFor(PN->getParent());
6918 if (PN->getNumIncomingValues() != 2)
6919 return true;
6920
6921 // Find the value from previous iteration.
6922 auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
6923 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6924 PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
6925 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6926 return true;
6927
6928 // If a new pointer is loaded in the loop, the pointer references a different
6929 // object in every iteration. E.g.:
6930 // for (i)
6931 // int *p = a[i];
6932 // ...
6933 if (auto *Load = dyn_cast<LoadInst>(PrevValue))
6934 if (!L->isLoopInvariant(Load->getPointerOperand()))
6935 return false;
6936 return true;
6937}
6938
6939const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup) {
6940 for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
6941 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
6942 const Value *PtrOp = GEP->getPointerOperand();
6943 if (!PtrOp->getType()->isPointerTy()) // Only handle scalar pointer base.
6944 return V;
6945 V = PtrOp;
6946 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
6947 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
6948 Value *NewV = cast<Operator>(V)->getOperand(0);
6949 if (!NewV->getType()->isPointerTy())
6950 return V;
6951 V = NewV;
6952 } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
6953 if (GA->isInterposable())
6954 return V;
6955 V = GA->getAliasee();
6956 } else {
6957 if (auto *PHI = dyn_cast<PHINode>(V)) {
6958 // Look through single-arg phi nodes created by LCSSA.
6959 if (PHI->getNumIncomingValues() == 1) {
6960 V = PHI->getIncomingValue(0);
6961 continue;
6962 }
6963 } else if (auto *Call = dyn_cast<CallBase>(V)) {
6964 // CaptureTracking can know about special capturing properties of some
6965 // intrinsics like launder.invariant.group, that can't be expressed with
6966 // the attributes, but have properties like returning aliasing pointer.
6967 // Because some analysis may assume that nocaptured pointer is not
6968 // returned from some special intrinsic (because function would have to
6969 // be marked with returns attribute), it is crucial to use this function
6970 // because it should be in sync with CaptureTracking. Not using it may
6971 // cause weird miscompilations where 2 aliasing pointers are assumed to
6972 // noalias.
6973 if (auto *RP = getArgumentAliasingToReturnedPointer(Call, false)) {
6974 V = RP;
6975 continue;
6976 }
6977 }
6978
6979 return V;
6980 }
6981 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
6982 }
6983 return V;
6984}
6985
6988 const LoopInfo *LI, unsigned MaxLookup) {
6991 Worklist.push_back(V);
6992 do {
6993 const Value *P = Worklist.pop_back_val();
6994 P = getUnderlyingObject(P, MaxLookup);
6995
6996 if (!Visited.insert(P).second)
6997 continue;
6998
6999 if (auto *SI = dyn_cast<SelectInst>(P)) {
7000 Worklist.push_back(SI->getTrueValue());
7001 Worklist.push_back(SI->getFalseValue());
7002 continue;
7003 }
7004
7005 if (auto *PN = dyn_cast<PHINode>(P)) {
7006 // If this PHI changes the underlying object in every iteration of the
7007 // loop, don't look through it. Consider:
7008 // int **A;
7009 // for (i) {
7010 // Prev = Curr; // Prev = PHI (Prev_0, Curr)
7011 // Curr = A[i];
7012 // *Prev, *Curr;
7013 //
7014 // Prev is tracking Curr one iteration behind so they refer to different
7015 // underlying objects.
7016 if (!LI || !LI->isLoopHeader(PN->getParent()) ||
7018 append_range(Worklist, PN->incoming_values());
7019 else
7020 Objects.push_back(P);
7021 continue;
7022 }
7023
7024 Objects.push_back(P);
7025 } while (!Worklist.empty());
7026}
7027
7029 const unsigned MaxVisited = 8;
7030
7033 Worklist.push_back(V);
7034 const Value *Object = nullptr;
7035 // Used as fallback if we can't find a common underlying object through
7036 // recursion.
7037 bool First = true;
7038 const Value *FirstObject = getUnderlyingObject(V);
7039 do {
7040 const Value *P = Worklist.pop_back_val();
7041 P = First ? FirstObject : getUnderlyingObject(P);
7042 First = false;
7043
7044 if (!Visited.insert(P).second)
7045 continue;
7046
7047 if (Visited.size() == MaxVisited)
7048 return FirstObject;
7049
7050 if (auto *SI = dyn_cast<SelectInst>(P)) {
7051 Worklist.push_back(SI->getTrueValue());
7052 Worklist.push_back(SI->getFalseValue());
7053 continue;
7054 }
7055
7056 if (auto *PN = dyn_cast<PHINode>(P)) {
7057 append_range(Worklist, PN->incoming_values());
7058 continue;
7059 }
7060
7061 if (!Object)
7062 Object = P;
7063 else if (Object != P)
7064 return FirstObject;
7065 } while (!Worklist.empty());
7066
7067 return Object ? Object : FirstObject;
7068}
7069
7070/// This is the function that does the work of looking through basic
7071/// ptrtoint+arithmetic+inttoptr sequences.
7072static const Value *getUnderlyingObjectFromInt(const Value *V) {
7073 do {
7074 if (const Operator *U = dyn_cast<Operator>(V)) {
7075 // If we find a ptrtoint, we can transfer control back to the
7076 // regular getUnderlyingObjectFromInt.
7077 if (U->getOpcode() == Instruction::PtrToInt)
7078 return U->getOperand(0);
7079 // If we find an add of a constant, a multiplied value, or a phi, it's
7080 // likely that the other operand will lead us to the base
7081 // object. We don't have to worry about the case where the
7082 // object address is somehow being computed by the multiply,
7083 // because our callers only care when the result is an
7084 // identifiable object.
7085 if (U->getOpcode() != Instruction::Add ||
7086 (!isa<ConstantInt>(U->getOperand(1)) &&
7087 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
7088 !isa<PHINode>(U->getOperand(1))))
7089 return V;
7090 V = U->getOperand(0);
7091 } else {
7092 return V;
7093 }
7094 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
7095 } while (true);
7096}
7097
7098/// This is a wrapper around getUnderlyingObjects and adds support for basic
7099/// ptrtoint+arithmetic+inttoptr sequences.
7100/// It returns false if unidentified object is found in getUnderlyingObjects.
7102 SmallVectorImpl<Value *> &Objects) {
7104 SmallVector<const Value *, 4> Working(1, V);
7105 do {
7106 V = Working.pop_back_val();
7107
7109 getUnderlyingObjects(V, Objs);
7110
7111 for (const Value *V : Objs) {
7112 if (!Visited.insert(V).second)
7113 continue;
7114 if (Operator::getOpcode(V) == Instruction::IntToPtr) {
7115 const Value *O =
7116 getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
7117 if (O->getType()->isPointerTy()) {
7118 Working.push_back(O);
7119 continue;
7120 }
7121 }
7122 // If getUnderlyingObjects fails to find an identifiable object,
7123 // getUnderlyingObjectsForCodeGen also fails for safety.
7124 if (!isIdentifiedObject(V)) {
7125 Objects.clear();
7126 return false;
7127 }
7128 Objects.push_back(const_cast<Value *>(V));
7129 }
7130 } while (!Working.empty());
7131 return true;
7132}
7133
7135 AllocaInst *Result = nullptr;
7137 SmallVector<Value *, 4> Worklist;
7138
7139 auto AddWork = [&](Value *V) {
7140 if (Visited.insert(V).second)
7141 Worklist.push_back(V);
7142 };
7143
7144 AddWork(V);
7145 do {
7146 V = Worklist.pop_back_val();
7147 assert(Visited.count(V));
7148
7149 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
7150 if (Result && Result != AI)
7151 return nullptr;
7152 Result = AI;
7153 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
7154 AddWork(CI->getOperand(0));
7155 } else if (PHINode *PN = dyn_cast<PHINode>(V)) {
7156 for (Value *IncValue : PN->incoming_values())
7157 AddWork(IncValue);
7158 } else if (auto *SI = dyn_cast<SelectInst>(V)) {
7159 AddWork(SI->getTrueValue());
7160 AddWork(SI->getFalseValue());
7162 if (OffsetZero && !GEP->hasAllZeroIndices())
7163 return nullptr;
7164 AddWork(GEP->getPointerOperand());
7165 } else if (CallBase *CB = dyn_cast<CallBase>(V)) {
7166 Value *Returned = CB->getReturnedArgOperand();
7167 if (Returned)
7168 AddWork(Returned);
7169 else
7170 return nullptr;
7171 } else {
7172 return nullptr;
7173 }
7174 } while (!Worklist.empty());
7175
7176 return Result;
7177}
7178
7180 const Value *V, bool AllowLifetime, bool AllowDroppable) {
7181 for (const User *U : V->users()) {
7183 if (!II)
7184 return false;
7185
7186 if (AllowLifetime && II->isLifetimeStartOrEnd())
7187 continue;
7188
7189 if (AllowDroppable && II->isDroppable())
7190 continue;
7191
7192 return false;
7193 }
7194 return true;
7195}
7196
7199 V, /* AllowLifetime */ true, /* AllowDroppable */ false);
7200}
7203 V, /* AllowLifetime */ true, /* AllowDroppable */ true);
7204}
7205
7207 if (auto *II = dyn_cast<IntrinsicInst>(I))
7208 return isTriviallyVectorizable(II->getIntrinsicID());
7209 auto *Shuffle = dyn_cast<ShuffleVectorInst>(I);
7210 return (!Shuffle || Shuffle->isSelect()) &&
7212}
7213
7215 const Instruction *Inst, const Instruction *CtxI, AssumptionCache *AC,
7216 const DominatorTree *DT, const TargetLibraryInfo *TLI, bool UseVariableInfo,
7217 bool IgnoreUBImplyingAttrs) {
7218 return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI,
7219 AC, DT, TLI, UseVariableInfo,
7220 IgnoreUBImplyingAttrs);
7221}
7222
7224 unsigned Opcode, const Instruction *Inst, const Instruction *CtxI,
7225 AssumptionCache *AC, const DominatorTree *DT, const TargetLibraryInfo *TLI,
7226 bool UseVariableInfo, bool IgnoreUBImplyingAttrs) {
7227#ifndef NDEBUG
7228 if (Inst->getOpcode() != Opcode) {
7229 // Check that the operands are actually compatible with the Opcode override.
7230 auto hasEqualReturnAndLeadingOperandTypes =
7231 [](const Instruction *Inst, unsigned NumLeadingOperands) {
7232 if (Inst->getNumOperands() < NumLeadingOperands)
7233 return false;
7234 const Type *ExpectedType = Inst->getType();
7235 for (unsigned ItOp = 0; ItOp < NumLeadingOperands; ++ItOp)
7236 if (Inst->getOperand(ItOp)->getType() != ExpectedType)
7237 return false;
7238 return true;
7239 };
7241 hasEqualReturnAndLeadingOperandTypes(Inst, 2));
7242 assert(!Instruction::isUnaryOp(Opcode) ||
7243 hasEqualReturnAndLeadingOperandTypes(Inst, 1));
7244 }
7245#endif
7246
7247 switch (Opcode) {
7248 default:
7249 return true;
7250 case Instruction::UDiv:
7251 case Instruction::URem: {
7252 // x / y is undefined if y == 0.
7253 const APInt *V;
7254 if (match(Inst->getOperand(1), m_APInt(V)))
7255 return *V != 0;
7256 return false;
7257 }
7258 case Instruction::SDiv:
7259 case Instruction::SRem: {
7260 // x / y is undefined if y == 0 or x == INT_MIN and y == -1
7261 const APInt *Numerator, *Denominator;
7262 if (!match(Inst->getOperand(1), m_APInt(Denominator)))
7263 return false;
7264 // We cannot hoist this division if the denominator is 0.
7265 if (*Denominator == 0)
7266 return false;
7267 // It's safe to hoist if the denominator is not 0 or -1.
7268 if (!Denominator->isAllOnes())
7269 return true;
7270 // At this point we know that the denominator is -1. It is safe to hoist as
7271 // long we know that the numerator is not INT_MIN.
7272 if (match(Inst->getOperand(0), m_APInt(Numerator)))
7273 return !Numerator->isMinSignedValue();
7274 // The numerator *might* be MinSignedValue.
7275 return false;
7276 }
7277 case Instruction::Load: {
7278 if (!UseVariableInfo)
7279 return false;
7280
7281 const LoadInst *LI = dyn_cast<LoadInst>(Inst);
7282 if (!LI)
7283 return false;
7284 if (mustSuppressSpeculation(*LI))
7285 return false;
7286 const DataLayout &DL = LI->getDataLayout();
7288 LI->getType(), LI->getAlign(), DL,
7289 CtxI, AC, DT, TLI);
7290 }
7291 case Instruction::Call: {
7292 auto *CI = dyn_cast<const CallInst>(Inst);
7293 if (!CI)
7294 return false;
7295 const Function *Callee = CI->getCalledFunction();
7296
7297 // The called function could have undefined behavior or side-effects, even
7298 // if marked readnone nounwind.
7299 if (!Callee || !Callee->isSpeculatable())
7300 return false;
7301 // Since the operands may be changed after hoisting, undefined behavior may
7302 // be triggered by some UB-implying attributes.
7303 return IgnoreUBImplyingAttrs || !CI->hasUBImplyingAttrs();
7304 }
7305 case Instruction::VAArg:
7306 case Instruction::Alloca:
7307 case Instruction::Invoke:
7308 case Instruction::CallBr:
7309 case Instruction::PHI:
7310 case Instruction::Store:
7311 case Instruction::Ret:
7312 case Instruction::UncondBr:
7313 case Instruction::CondBr:
7314 case Instruction::IndirectBr:
7315 case Instruction::Switch:
7316 case Instruction::Unreachable:
7317 case Instruction::Fence:
7318 case Instruction::AtomicRMW:
7319 case Instruction::AtomicCmpXchg:
7320 case Instruction::LandingPad:
7321 case Instruction::Resume:
7322 case Instruction::CatchSwitch:
7323 case Instruction::CatchPad:
7324 case Instruction::CatchRet:
7325 case Instruction::CleanupPad:
7326 case Instruction::CleanupRet:
7327 return false; // Misc instructions which have effects
7328 }
7329}
7330
7332 if (I.mayReadOrWriteMemory())
7333 // Memory dependency possible
7334 return true;
7336 // Can't move above a maythrow call or infinite loop. Or if an
7337 // inalloca alloca, above a stacksave call.
7338 return true;
7340 // 1) Can't reorder two inf-loop calls, even if readonly
7341 // 2) Also can't reorder an inf-loop call below a instruction which isn't
7342 // safe to speculative execute. (Inverse of above)
7343 return true;
7344 return false;
7345}
7346
7347/// Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
7361
7362/// Combine constant ranges from computeConstantRange() and computeKnownBits().
7365 bool ForSigned,
7366 const SimplifyQuery &SQ) {
7367 ConstantRange CR1 =
7368 ConstantRange::fromKnownBits(V.getKnownBits(SQ), ForSigned);
7369 ConstantRange CR2 = computeConstantRange(V, ForSigned, SQ);
7372 return CR1.intersectWith(CR2, RangeType);
7373}
7374
7376 const Value *RHS,
7377 const SimplifyQuery &SQ,
7378 bool IsNSW) {
7379 ConstantRange LHSRange =
7380 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7381 ConstantRange RHSRange =
7382 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7383
7384 // mul nsw of two non-negative numbers is also nuw.
7385 if (IsNSW && LHSRange.isAllNonNegative() && RHSRange.isAllNonNegative())
7387
7388 return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange));
7389}
7390
7392 const Value *RHS,
7393 const SimplifyQuery &SQ) {
7394 // Multiplying n * m significant bits yields a result of n + m significant
7395 // bits. If the total number of significant bits does not exceed the
7396 // result bit width (minus 1), there is no overflow.
7397 // This means if we have enough leading sign bits in the operands
7398 // we can guarantee that the result does not overflow.
7399 // Ref: "Hacker's Delight" by Henry Warren
7400 unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
7401
7402 // Note that underestimating the number of sign bits gives a more
7403 // conservative answer.
7404 unsigned SignBits =
7405 ::ComputeNumSignBits(LHS, SQ) + ::ComputeNumSignBits(RHS, SQ);
7406
7407 // First handle the easy case: if we have enough sign bits there's
7408 // definitely no overflow.
7409 if (SignBits > BitWidth + 1)
7411
7412 // There are two ambiguous cases where there can be no overflow:
7413 // SignBits == BitWidth + 1 and
7414 // SignBits == BitWidth
7415 // The second case is difficult to check, therefore we only handle the
7416 // first case.
7417 if (SignBits == BitWidth + 1) {
7418 // It overflows only when both arguments are negative and the true
7419 // product is exactly the minimum negative number.
7420 // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
7421 // For simplicity we just check if at least one side is not negative.
7422 KnownBits LHSKnown = computeKnownBits(LHS, SQ);
7423 KnownBits RHSKnown = computeKnownBits(RHS, SQ);
7424 if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative())
7426 }
7428}
7429
7432 const WithCache<const Value *> &RHS,
7433 const SimplifyQuery &SQ) {
7434 ConstantRange LHSRange =
7435 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7436 ConstantRange RHSRange =
7437 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7438 return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange));
7439}
7440
7441static OverflowResult
7444 const AddOperator *Add, const SimplifyQuery &SQ) {
7445 if (Add && Add->hasNoSignedWrap()) {
7447 }
7448
7449 // If LHS and RHS each have at least two sign bits, the addition will look
7450 // like
7451 //
7452 // XX..... +
7453 // YY.....
7454 //
7455 // If the carry into the most significant position is 0, X and Y can't both
7456 // be 1 and therefore the carry out of the addition is also 0.
7457 //
7458 // If the carry into the most significant position is 1, X and Y can't both
7459 // be 0 and therefore the carry out of the addition is also 1.
7460 //
7461 // Since the carry into the most significant position is always equal to
7462 // the carry out of the addition, there is no signed overflow.
7463 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
7465
7466 ConstantRange LHSRange =
7467 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7468 ConstantRange RHSRange =
7469 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7470 OverflowResult OR =
7471 mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange));
7473 return OR;
7474
7475 // The remaining code needs Add to be available. Early returns if not so.
7476 if (!Add)
7478
7479 // If the sign of Add is the same as at least one of the operands, this add
7480 // CANNOT overflow. If this can be determined from the known bits of the
7481 // operands the above signedAddMayOverflow() check will have already done so.
7482 // The only other way to improve on the known bits is from an assumption, so
7483 // call computeKnownBitsFromContext() directly.
7484 bool LHSOrRHSKnownNonNegative =
7485 (LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative());
7486 bool LHSOrRHSKnownNegative =
7487 (LHSRange.isAllNegative() || RHSRange.isAllNegative());
7488 if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
7489 KnownBits AddKnown(LHSRange.getBitWidth());
7490 computeKnownBitsFromContext(Add, AddKnown, SQ);
7491 if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||
7492 (AddKnown.isNegative() && LHSOrRHSKnownNegative))
7494 }
7495
7497}
7498
7500 const Value *RHS,
7501 const SimplifyQuery &SQ) {
7502 // X - (X % ?)
7503 // The remainder of a value can't have greater magnitude than itself,
7504 // so the subtraction can't overflow.
7505
7506 // X - (X -nuw ?)
7507 // In the minimal case, this would simplify to "?", so there's no subtract
7508 // at all. But if this analysis is used to peek through casts, for example,
7509 // then determining no-overflow may allow other transforms.
7510
7511 // TODO: There are other patterns like this.
7512 // See simplifyICmpWithBinOpOnLHS() for candidates.
7513 if (match(RHS, m_URem(m_Specific(LHS), m_Value())) ||
7514 match(RHS, m_NUWSub(m_Specific(LHS), m_Value())))
7515 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7517
7518 if (auto C = isImpliedByDomCondition(CmpInst::ICMP_UGE, LHS, RHS, SQ.CxtI,
7519 SQ.DL)) {
7520 if (*C)
7523 }
7524
7525 ConstantRange LHSRange =
7526 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7527 ConstantRange RHSRange =
7528 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7529 return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange));
7530}
7531
7533 const Value *RHS,
7534 const SimplifyQuery &SQ) {
7535 // X - (X % ?)
7536 // The remainder of a value can't have greater magnitude than itself,
7537 // so the subtraction can't overflow.
7538
7539 // X - (X -nsw ?)
7540 // In the minimal case, this would simplify to "?", so there's no subtract
7541 // at all. But if this analysis is used to peek through casts, for example,
7542 // then determining no-overflow may allow other transforms.
7543 if (match(RHS, m_SRem(m_Specific(LHS), m_Value())) ||
7544 match(RHS, m_NSWSub(m_Specific(LHS), m_Value())))
7545 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7547
7548 // If LHS and RHS each have at least two sign bits, the subtraction
7549 // cannot overflow.
7550 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
7552
7553 ConstantRange LHSRange =
7554 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7555 ConstantRange RHSRange =
7556 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7557 return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange));
7558}
7559
7561 const DominatorTree &DT) {
7562 SmallVector<const CondBrInst *, 2> GuardingBranches;
7564
7565 for (const User *U : WO->users()) {
7566 if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) {
7567 assert(EVI->getNumIndices() == 1 && "Obvious from CI's type");
7568
7569 if (EVI->getIndices()[0] == 0)
7570 Results.push_back(EVI);
7571 else {
7572 assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type");
7573
7574 for (const auto *U : EVI->users())
7575 if (const auto *B = dyn_cast<CondBrInst>(U))
7576 GuardingBranches.push_back(B);
7577 }
7578 } else {
7579 // We are using the aggregate directly in a way we don't want to analyze
7580 // here (storing it to a global, say).
7581 return false;
7582 }
7583 }
7584
7585 auto AllUsesGuardedByBranch = [&](const CondBrInst *BI) {
7586 BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1));
7587
7588 // Check if all users of the add are provably no-wrap.
7589 for (const auto *Result : Results) {
7590 // If the extractvalue itself is not executed on overflow, the we don't
7591 // need to check each use separately, since domination is transitive.
7592 if (DT.dominates(NoWrapEdge, Result->getParent()))
7593 continue;
7594
7595 for (const auto &RU : Result->uses())
7596 if (!DT.dominates(NoWrapEdge, RU))
7597 return false;
7598 }
7599
7600 return true;
7601 };
7602
7603 return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
7604}
7605
7606/// Shifts return poison if shiftwidth is larger than the bitwidth.
7607static bool shiftAmountKnownInRange(const Value *ShiftAmount) {
7608 auto *C = dyn_cast<Constant>(ShiftAmount);
7609 if (!C)
7610 return false;
7611
7612 // Shifts return poison if shiftwidth is larger than the bitwidth.
7614 if (auto *FVTy = dyn_cast<FixedVectorType>(C->getType())) {
7615 unsigned NumElts = FVTy->getNumElements();
7616 for (unsigned i = 0; i < NumElts; ++i)
7617 ShiftAmounts.push_back(C->getAggregateElement(i));
7618 } else if (isa<ScalableVectorType>(C->getType()))
7619 return false; // Can't tell, just return false to be safe
7620 else
7621 ShiftAmounts.push_back(C);
7622
7623 bool Safe = llvm::all_of(ShiftAmounts, [](const Constant *C) {
7624 auto *CI = dyn_cast_or_null<ConstantInt>(C);
7625 return CI && CI->getValue().ult(C->getType()->getIntegerBitWidth());
7626 });
7627
7628 return Safe;
7629}
7630
7632 bool ConsiderFlagsAndMetadata) {
7633
7634 if (ConsiderFlagsAndMetadata && includesPoison(Kind) &&
7635 Op->hasPoisonGeneratingAnnotations())
7636 return true;
7637
7638 unsigned Opcode = Op->getOpcode();
7639
7640 // Check whether opcode is a poison/undef-generating operation
7641 switch (Opcode) {
7642 case Instruction::Shl:
7643 case Instruction::AShr:
7644 case Instruction::LShr:
7645 return includesPoison(Kind) && !shiftAmountKnownInRange(Op->getOperand(1));
7646 case Instruction::FPToSI:
7647 case Instruction::FPToUI:
7648 // fptosi/ui yields poison if the resulting value does not fit in the
7649 // destination type.
7650 return true;
7651 case Instruction::Call:
7652 if (auto *II = dyn_cast<IntrinsicInst>(Op)) {
7653 switch (II->getIntrinsicID()) {
7654 // NOTE: Use IntrNoCreateUndefOrPoison when possible.
7655 case Intrinsic::ctlz:
7656 case Intrinsic::cttz:
7657 case Intrinsic::abs:
7658 // We're not considering flags so it is safe to just return false.
7659 return false;
7660 case Intrinsic::sshl_sat:
7661 case Intrinsic::ushl_sat:
7662 if (!includesPoison(Kind) ||
7663 shiftAmountKnownInRange(II->getArgOperand(1)))
7664 return false;
7665 break;
7666 }
7667 }
7668 [[fallthrough]];
7669 case Instruction::CallBr:
7670 case Instruction::Invoke: {
7671 const auto *CB = cast<CallBase>(Op);
7672 return !CB->hasRetAttr(Attribute::NoUndef) &&
7673 !CB->hasFnAttr(Attribute::NoCreateUndefOrPoison);
7674 }
7675 case Instruction::InsertElement:
7676 case Instruction::ExtractElement: {
7677 // If index exceeds the length of the vector, it returns poison
7678 auto *VTy = cast<VectorType>(Op->getOperand(0)->getType());
7679 unsigned IdxOp = Op->getOpcode() == Instruction::InsertElement ? 2 : 1;
7680 auto *Idx = dyn_cast<ConstantInt>(Op->getOperand(IdxOp));
7681 if (includesPoison(Kind))
7682 return !Idx ||
7683 Idx->getValue().uge(VTy->getElementCount().getKnownMinValue());
7684 return false;
7685 }
7686 case Instruction::ShuffleVector: {
7688 ? cast<ConstantExpr>(Op)->getShuffleMask()
7689 : cast<ShuffleVectorInst>(Op)->getShuffleMask();
7690 return includesPoison(Kind) && is_contained(Mask, PoisonMaskElem);
7691 }
7692 case Instruction::FNeg:
7693 case Instruction::PHI:
7694 case Instruction::Select:
7695 case Instruction::ExtractValue:
7696 case Instruction::InsertValue:
7697 case Instruction::Freeze:
7698 case Instruction::ICmp:
7699 case Instruction::FCmp:
7700 case Instruction::GetElementPtr:
7701 return false;
7702 case Instruction::AddrSpaceCast:
7703 return true;
7704 default: {
7705 const auto *CE = dyn_cast<ConstantExpr>(Op);
7706 if (isa<CastInst>(Op) || (CE && CE->isCast()))
7707 return false;
7708 else if (Instruction::isBinaryOp(Opcode))
7709 return false;
7710 // Be conservative and return true.
7711 return true;
7712 }
7713 }
7714}
7715
7717 bool ConsiderFlagsAndMetadata) {
7718 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::UndefOrPoison,
7719 ConsiderFlagsAndMetadata);
7720}
7721
7722bool llvm::canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata) {
7723 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::PoisonOnly,
7724 ConsiderFlagsAndMetadata);
7725}
7726
7727static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V,
7728 unsigned Depth) {
7729 if (ValAssumedPoison == V)
7730 return true;
7731
7732 const unsigned MaxDepth = 2;
7733 if (Depth >= MaxDepth)
7734 return false;
7735
7736 if (const auto *I = dyn_cast<Instruction>(V)) {
7737 if (any_of(I->operands(), [=](const Use &Op) {
7738 return propagatesPoison(Op) &&
7739 directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1);
7740 }))
7741 return true;
7742
7743 // V = extractvalue V0, idx
7744 // V2 = extractvalue V0, idx2
7745 // V0's elements are all poison or not. (e.g., add_with_overflow)
7746 const WithOverflowInst *II;
7748 (match(ValAssumedPoison, m_ExtractValue(m_Specific(II))) ||
7749 llvm::is_contained(II->args(), ValAssumedPoison)))
7750 return true;
7751 }
7752 return false;
7753}
7754
7755static bool impliesPoison(const Value *ValAssumedPoison, const Value *V,
7756 unsigned Depth) {
7757 if (isGuaranteedNotToBePoison(ValAssumedPoison))
7758 return true;
7759
7760 if (directlyImpliesPoison(ValAssumedPoison, V, /* Depth */ 0))
7761 return true;
7762
7763 const unsigned MaxDepth = 2;
7764 if (Depth >= MaxDepth)
7765 return false;
7766
7767 const auto *I = dyn_cast<Instruction>(ValAssumedPoison);
7768 if (I && !canCreatePoison(cast<Operator>(I))) {
7769 return all_of(I->operands(), [=](const Value *Op) {
7770 return impliesPoison(Op, V, Depth + 1);
7771 });
7772 }
7773 return false;
7774}
7775
7776bool llvm::impliesPoison(const Value *ValAssumedPoison, const Value *V) {
7777 return ::impliesPoison(ValAssumedPoison, V, /* Depth */ 0);
7778}
7779
7780static bool programUndefinedIfUndefOrPoison(const Value *V, bool PoisonOnly);
7781
7783 const Value *V, AssumptionCache *AC, const Instruction *CtxI,
7784 const DominatorTree *DT, unsigned Depth, UndefPoisonKind Kind) {
7786 return false;
7787
7788 if (isa<MetadataAsValue>(V))
7789 return false;
7790
7791 if (const auto *A = dyn_cast<Argument>(V)) {
7792 if (A->hasAttribute(Attribute::NoUndef) ||
7793 A->hasAttribute(Attribute::Dereferenceable) ||
7794 A->hasAttribute(Attribute::DereferenceableOrNull))
7795 return true;
7796 }
7797
7798 if (auto *C = dyn_cast<Constant>(V)) {
7799 if (isa<PoisonValue>(C))
7800 return !includesPoison(Kind);
7801
7802 if (isa<UndefValue>(C))
7803 return !includesUndef(Kind);
7804
7807 return true;
7808
7809 if (C->getType()->isVectorTy()) {
7810 if (isa<ConstantExpr>(C)) {
7811 // Scalable vectors can use a ConstantExpr to build a splat.
7812 if (Constant *SplatC = C->getSplatValue())
7813 if (isa<ConstantInt>(SplatC) || isa<ConstantFP>(SplatC))
7814 return true;
7815 } else {
7816 if (includesUndef(Kind) && C->containsUndefElement())
7817 return false;
7818 if (includesPoison(Kind) && C->containsPoisonElement())
7819 return false;
7820 return !C->containsConstantExpression();
7821 }
7822 }
7823 }
7824
7825 // Strip cast operations from a pointer value.
7826 // Note that stripPointerCastsSameRepresentation can strip off getelementptr
7827 // inbounds with zero offset. To guarantee that the result isn't poison, the
7828 // stripped pointer is checked as it has to be pointing into an allocated
7829 // object or be null `null` to ensure `inbounds` getelement pointers with a
7830 // zero offset could not produce poison.
7831 // It can strip off addrspacecast that do not change bit representation as
7832 // well. We believe that such addrspacecast is equivalent to no-op.
7833 auto *StrippedV = V->stripPointerCastsSameRepresentation();
7834 if (isa<AllocaInst>(StrippedV) || isa<GlobalVariable>(StrippedV) ||
7835 isa<Function>(StrippedV) || isa<ConstantPointerNull>(StrippedV))
7836 return true;
7837
7838 auto OpCheck = [&](const Value *V) {
7839 return isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth + 1, Kind);
7840 };
7841
7842 if (auto *Opr = dyn_cast<Operator>(V)) {
7843 // If the value is a freeze instruction, then it can never
7844 // be undef or poison.
7845 if (isa<FreezeInst>(V))
7846 return true;
7847
7848 if (const auto *CB = dyn_cast<CallBase>(V)) {
7849 if (CB->hasRetAttr(Attribute::NoUndef) ||
7850 CB->hasRetAttr(Attribute::Dereferenceable) ||
7851 CB->hasRetAttr(Attribute::DereferenceableOrNull))
7852 return true;
7853 }
7854
7855 if (!::canCreateUndefOrPoison(Opr, Kind,
7856 /*ConsiderFlagsAndMetadata=*/true)) {
7857 if (const auto *PN = dyn_cast<PHINode>(V)) {
7858 unsigned Num = PN->getNumIncomingValues();
7859 bool IsWellDefined = true;
7860 for (unsigned i = 0; i < Num; ++i) {
7861 if (PN == PN->getIncomingValue(i))
7862 continue;
7863 auto *TI = PN->getIncomingBlock(i)->getTerminator();
7864 if (!isGuaranteedNotToBeUndefOrPoison(PN->getIncomingValue(i), AC, TI,
7865 DT, Depth + 1, Kind)) {
7866 IsWellDefined = false;
7867 break;
7868 }
7869 }
7870 if (IsWellDefined)
7871 return true;
7872 } else if (auto *Splat = isa<ShuffleVectorInst>(Opr) ? getSplatValue(Opr)
7873 : nullptr) {
7874 // For splats we only need to check the value being splatted.
7875 if (OpCheck(Splat))
7876 return true;
7877 } else if (all_of(Opr->operands(), OpCheck))
7878 return true;
7879 }
7880 }
7881
7882 if (auto *I = dyn_cast<LoadInst>(V))
7883 if (I->hasMetadata(LLVMContext::MD_noundef) ||
7884 I->hasMetadata(LLVMContext::MD_dereferenceable) ||
7885 I->hasMetadata(LLVMContext::MD_dereferenceable_or_null))
7886 return true;
7887
7889 return true;
7890
7891 // CxtI may be null or a cloned instruction.
7892 if (!CtxI || !CtxI->getParent() || !DT)
7893 return false;
7894
7895 auto *DNode = DT->getNode(CtxI->getParent());
7896 if (!DNode)
7897 // Unreachable block
7898 return false;
7899
7900 // If V is used as a branch condition before reaching CtxI, V cannot be
7901 // undef or poison.
7902 // br V, BB1, BB2
7903 // BB1:
7904 // CtxI ; V cannot be undef or poison here
7905 auto *Dominator = DNode->getIDom();
7906 // This check is purely for compile time reasons: we can skip the IDom walk
7907 // if what we are checking for includes undef and the value is not an integer.
7908 if (!includesUndef(Kind) || V->getType()->isIntegerTy())
7909 while (Dominator) {
7910 auto *TI = Dominator->getBlock()->getTerminatorOrNull();
7911
7912 Value *Cond = nullptr;
7913 if (auto BI = dyn_cast_or_null<CondBrInst>(TI)) {
7914 Cond = BI->getCondition();
7915 } else if (auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
7916 Cond = SI->getCondition();
7917 }
7918
7919 if (Cond) {
7920 if (Cond == V)
7921 return true;
7922 else if (!includesUndef(Kind) && isa<Operator>(Cond)) {
7923 // For poison, we can analyze further
7924 auto *Opr = cast<Operator>(Cond);
7925 if (any_of(Opr->operands(), [V](const Use &U) {
7926 return V == U && propagatesPoison(U);
7927 }))
7928 return true;
7929 }
7930 }
7931
7932 Dominator = Dominator->getIDom();
7933 }
7934
7935 if (AC && getKnowledgeValidInContext(V, {Attribute::NoUndef}, *AC, CtxI, DT))
7936 return true;
7937
7938 return false;
7939}
7940
7942 const Instruction *CtxI,
7943 const DominatorTree *DT,
7944 unsigned Depth) {
7945 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7947}
7948
7950 const Instruction *CtxI,
7951 const DominatorTree *DT, unsigned Depth) {
7952 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7954}
7955
7957 const Instruction *CtxI,
7958 const DominatorTree *DT, unsigned Depth) {
7959 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7961}
7962
7963/// Return true if undefined behavior would provably be executed on the path to
7964/// OnPathTo if Root produced a posion result. Note that this doesn't say
7965/// anything about whether OnPathTo is actually executed or whether Root is
7966/// actually poison. This can be used to assess whether a new use of Root can
7967/// be added at a location which is control equivalent with OnPathTo (such as
7968/// immediately before it) without introducing UB which didn't previously
7969/// exist. Note that a false result conveys no information.
7971 Instruction *OnPathTo,
7972 DominatorTree *DT) {
7973 // Basic approach is to assume Root is poison, propagate poison forward
7974 // through all users we can easily track, and then check whether any of those
7975 // users are provable UB and must execute before out exiting block might
7976 // exit.
7977
7978 // The set of all recursive users we've visited (which are assumed to all be
7979 // poison because of said visit)
7982 Worklist.push_back(Root);
7983 while (!Worklist.empty()) {
7984 const Instruction *I = Worklist.pop_back_val();
7985
7986 // If we know this must trigger UB on a path leading our target.
7987 if (mustTriggerUB(I, KnownPoison) && DT->dominates(I, OnPathTo))
7988 return true;
7989
7990 // If we can't analyze propagation through this instruction, just skip it
7991 // and transitive users. Safe as false is a conservative result.
7992 if (I != Root && !any_of(I->operands(), [&KnownPoison](const Use &U) {
7993 return KnownPoison.contains(U) && propagatesPoison(U);
7994 }))
7995 continue;
7996
7997 if (KnownPoison.insert(I).second)
7998 for (const User *User : I->users())
7999 Worklist.push_back(cast<Instruction>(User));
8000 }
8001
8002 // Might be non-UB, or might have a path we couldn't prove must execute on
8003 // way to exiting bb.
8004 return false;
8005}
8006
8008 const SimplifyQuery &SQ) {
8009 return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1),
8010 Add, SQ);
8011}
8012
8015 const WithCache<const Value *> &RHS,
8016 const SimplifyQuery &SQ) {
8017 return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, SQ);
8018}
8019
8021 // Note: An atomic operation isn't guaranteed to return in a reasonable amount
8022 // of time because it's possible for another thread to interfere with it for an
8023 // arbitrary length of time, but programs aren't allowed to rely on that.
8024
8025 // If there is no successor, then execution can't transfer to it.
8026 if (isa<ReturnInst>(I))
8027 return false;
8029 return false;
8030
8031 // Note: Do not add new checks here; instead, change Instruction::mayThrow or
8032 // Instruction::willReturn.
8033 //
8034 // FIXME: Move this check into Instruction::willReturn.
8035 if (isa<CatchPadInst>(I)) {
8036 switch (classifyEHPersonality(I->getFunction()->getPersonalityFn())) {
8037 default:
8038 // A catchpad may invoke exception object constructors and such, which
8039 // in some languages can be arbitrary code, so be conservative by default.
8040 return false;
8042 // For CoreCLR, it just involves a type test.
8043 return true;
8044 }
8045 }
8046
8047 // An instruction that returns without throwing must transfer control flow
8048 // to a successor.
8049 return !I->mayThrow() && I->willReturn();
8050}
8051
8053 // TODO: This is slightly conservative for invoke instruction since exiting
8054 // via an exception *is* normal control for them.
8055 for (const Instruction &I : *BB)
8057 return false;
8058 return true;
8059}
8060
8067
8070 assert(ScanLimit && "scan limit must be non-zero");
8071 for (const Instruction &I : Range) {
8072 if (--ScanLimit == 0)
8073 return false;
8075 return false;
8076 }
8077 return true;
8078}
8079
8081 const Loop *L) {
8082 // The loop header is guaranteed to be executed for every iteration.
8083 //
8084 // FIXME: Relax this constraint to cover all basic blocks that are
8085 // guaranteed to be executed at every iteration.
8086 if (I->getParent() != L->getHeader()) return false;
8087
8088 for (const Instruction &LI : *L->getHeader()) {
8089 if (&LI == I) return true;
8090 if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;
8091 }
8092 llvm_unreachable("Instruction not contained in its own parent basic block.");
8093}
8094
8096 switch (IID) {
8097 // TODO: Add more intrinsics.
8098 case Intrinsic::sadd_with_overflow:
8099 case Intrinsic::ssub_with_overflow:
8100 case Intrinsic::smul_with_overflow:
8101 case Intrinsic::uadd_with_overflow:
8102 case Intrinsic::usub_with_overflow:
8103 case Intrinsic::umul_with_overflow:
8104 // If an input is a vector containing a poison element, the
8105 // two output vectors (calculated results, overflow bits)'
8106 // corresponding lanes are poison.
8107 return true;
8108 case Intrinsic::ctpop:
8109 case Intrinsic::ctlz:
8110 case Intrinsic::cttz:
8111 case Intrinsic::abs:
8112 case Intrinsic::smax:
8113 case Intrinsic::smin:
8114 case Intrinsic::umax:
8115 case Intrinsic::umin:
8116 case Intrinsic::scmp:
8117 case Intrinsic::is_fpclass:
8118 case Intrinsic::ptrmask:
8119 case Intrinsic::ucmp:
8120 case Intrinsic::bitreverse:
8121 case Intrinsic::bswap:
8122 case Intrinsic::sadd_sat:
8123 case Intrinsic::ssub_sat:
8124 case Intrinsic::sshl_sat:
8125 case Intrinsic::uadd_sat:
8126 case Intrinsic::usub_sat:
8127 case Intrinsic::ushl_sat:
8128 case Intrinsic::smul_fix:
8129 case Intrinsic::smul_fix_sat:
8130 case Intrinsic::umul_fix:
8131 case Intrinsic::umul_fix_sat:
8132 case Intrinsic::pow:
8133 case Intrinsic::powi:
8134 case Intrinsic::sin:
8135 case Intrinsic::sinh:
8136 case Intrinsic::cos:
8137 case Intrinsic::cosh:
8138 case Intrinsic::sincos:
8139 case Intrinsic::sincospi:
8140 case Intrinsic::tan:
8141 case Intrinsic::tanh:
8142 case Intrinsic::asin:
8143 case Intrinsic::acos:
8144 case Intrinsic::atan:
8145 case Intrinsic::atan2:
8146 case Intrinsic::canonicalize:
8147 case Intrinsic::sqrt:
8148 case Intrinsic::exp:
8149 case Intrinsic::exp2:
8150 case Intrinsic::exp10:
8151 case Intrinsic::log:
8152 case Intrinsic::log2:
8153 case Intrinsic::log10:
8154 case Intrinsic::modf:
8155 case Intrinsic::floor:
8156 case Intrinsic::ceil:
8157 case Intrinsic::trunc:
8158 case Intrinsic::rint:
8159 case Intrinsic::nearbyint:
8160 case Intrinsic::round:
8161 case Intrinsic::roundeven:
8162 case Intrinsic::lrint:
8163 case Intrinsic::llrint:
8164 case Intrinsic::fshl:
8165 case Intrinsic::fshr:
8166 return true;
8167 default:
8168 return false;
8169 }
8170}
8171
8172bool llvm::propagatesPoison(const Use &PoisonOp) {
8173 const Operator *I = cast<Operator>(PoisonOp.getUser());
8174 switch (I->getOpcode()) {
8175 case Instruction::Freeze:
8176 case Instruction::PHI:
8177 case Instruction::Invoke:
8178 return false;
8179 case Instruction::Select:
8180 return PoisonOp.getOperandNo() == 0;
8181 case Instruction::Call:
8182 if (auto *II = dyn_cast<IntrinsicInst>(I))
8183 return intrinsicPropagatesPoison(II->getIntrinsicID());
8184 return false;
8185 case Instruction::ICmp:
8186 case Instruction::FCmp:
8187 case Instruction::GetElementPtr:
8188 return true;
8189 default:
8191 return true;
8192
8193 // Be conservative and return false.
8194 return false;
8195 }
8196}
8197
8198/// Enumerates all operands of \p I that are guaranteed to not be undef or
8199/// poison. If the callback \p Handle returns true, stop processing and return
8200/// true. Otherwise, return false.
8201template <typename CallableT>
8203 const CallableT &Handle) {
8204 switch (I->getOpcode()) {
8205 case Instruction::Store:
8206 if (Handle(cast<StoreInst>(I)->getPointerOperand()))
8207 return true;
8208 break;
8209
8210 case Instruction::Load:
8211 if (Handle(cast<LoadInst>(I)->getPointerOperand()))
8212 return true;
8213 break;
8214
8215 // Since dereferenceable attribute imply noundef, atomic operations
8216 // also implicitly have noundef pointers too
8217 case Instruction::AtomicCmpXchg:
8219 return true;
8220 break;
8221
8222 case Instruction::AtomicRMW:
8223 if (Handle(cast<AtomicRMWInst>(I)->getPointerOperand()))
8224 return true;
8225 break;
8226
8227 case Instruction::Call:
8228 case Instruction::Invoke: {
8229 const CallBase *CB = cast<CallBase>(I);
8230 if (CB->isIndirectCall() && Handle(CB->getCalledOperand()))
8231 return true;
8232 for (unsigned i = 0; i < CB->arg_size(); ++i)
8233 if ((CB->paramHasAttr(i, Attribute::NoUndef) ||
8234 CB->paramHasAttr(i, Attribute::Dereferenceable) ||
8235 CB->paramHasAttr(i, Attribute::DereferenceableOrNull)) &&
8236 Handle(CB->getArgOperand(i)))
8237 return true;
8238 break;
8239 }
8240 case Instruction::Ret:
8241 if (I->getFunction()->hasRetAttribute(Attribute::NoUndef) &&
8242 Handle(I->getOperand(0)))
8243 return true;
8244 break;
8245 case Instruction::Switch:
8246 if (Handle(cast<SwitchInst>(I)->getCondition()))
8247 return true;
8248 break;
8249 case Instruction::CondBr:
8250 if (Handle(cast<CondBrInst>(I)->getCondition()))
8251 return true;
8252 break;
8253 default:
8254 break;
8255 }
8256
8257 return false;
8258}
8259
8260/// Enumerates all operands of \p I that are guaranteed to not be poison.
8261template <typename CallableT>
8263 const CallableT &Handle) {
8264 if (handleGuaranteedWellDefinedOps(I, Handle))
8265 return true;
8266 switch (I->getOpcode()) {
8267 // Divisors of these operations are allowed to be partially undef.
8268 case Instruction::UDiv:
8269 case Instruction::SDiv:
8270 case Instruction::URem:
8271 case Instruction::SRem:
8272 return Handle(I->getOperand(1));
8273 default:
8274 return false;
8275 }
8276}
8277
8279 const SmallPtrSetImpl<const Value *> &KnownPoison) {
8281 I, [&](const Value *V) { return KnownPoison.count(V); });
8282}
8283
8285 bool PoisonOnly) {
8286 // We currently only look for uses of values within the same basic
8287 // block, as that makes it easier to guarantee that the uses will be
8288 // executed given that Inst is executed.
8289 //
8290 // FIXME: Expand this to consider uses beyond the same basic block. To do
8291 // this, look out for the distinction between post-dominance and strong
8292 // post-dominance.
8293 const BasicBlock *BB = nullptr;
8295 if (const auto *Inst = dyn_cast<Instruction>(V)) {
8296 BB = Inst->getParent();
8297 Begin = Inst->getIterator();
8298 Begin++;
8299 } else if (const auto *Arg = dyn_cast<Argument>(V)) {
8300 if (Arg->getParent()->isDeclaration())
8301 return false;
8302 BB = &Arg->getParent()->getEntryBlock();
8303 Begin = BB->begin();
8304 } else {
8305 return false;
8306 }
8307
8308 // Limit number of instructions we look at, to avoid scanning through large
8309 // blocks. The current limit is chosen arbitrarily.
8310 unsigned ScanLimit = 32;
8311 BasicBlock::const_iterator End = BB->end();
8312
8313 if (!PoisonOnly) {
8314 // Since undef does not propagate eagerly, be conservative & just check
8315 // whether a value is directly passed to an instruction that must take
8316 // well-defined operands.
8317
8318 for (const auto &I : make_range(Begin, End)) {
8319 if (--ScanLimit == 0)
8320 break;
8321
8322 if (handleGuaranteedWellDefinedOps(&I, [V](const Value *WellDefinedOp) {
8323 return WellDefinedOp == V;
8324 }))
8325 return true;
8326
8328 break;
8329 }
8330 return false;
8331 }
8332
8333 // Set of instructions that we have proved will yield poison if Inst
8334 // does.
8335 SmallPtrSet<const Value *, 16> YieldsPoison;
8337
8338 YieldsPoison.insert(V);
8339 Visited.insert(BB);
8340
8341 while (true) {
8342 for (const auto &I : make_range(Begin, End)) {
8343 if (--ScanLimit == 0)
8344 return false;
8345 if (mustTriggerUB(&I, YieldsPoison))
8346 return true;
8348 return false;
8349
8350 // If an operand is poison and propagates it, mark I as yielding poison.
8351 for (const Use &Op : I.operands()) {
8352 if (YieldsPoison.count(Op) && propagatesPoison(Op)) {
8353 YieldsPoison.insert(&I);
8354 break;
8355 }
8356 }
8357
8358 // Special handling for select, which returns poison if its operand 0 is
8359 // poison (handled in the loop above) *or* if both its true/false operands
8360 // are poison (handled here).
8361 if (I.getOpcode() == Instruction::Select &&
8362 YieldsPoison.count(I.getOperand(1)) &&
8363 YieldsPoison.count(I.getOperand(2))) {
8364 YieldsPoison.insert(&I);
8365 }
8366 }
8367
8368 BB = BB->getSingleSuccessor();
8369 if (!BB || !Visited.insert(BB).second)
8370 break;
8371
8372 Begin = BB->getFirstNonPHIIt();
8373 End = BB->end();
8374 }
8375 return false;
8376}
8377
8379 return ::programUndefinedIfUndefOrPoison(Inst, false);
8380}
8381
8383 return ::programUndefinedIfUndefOrPoison(Inst, true);
8384}
8385
8386static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) {
8387 if (FMF.noNaNs())
8388 return true;
8389
8390 if (auto *C = dyn_cast<ConstantFP>(V))
8391 return !C->isNaN();
8392
8393 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8394 if (!C->getElementType()->isFloatingPointTy())
8395 return false;
8396 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8397 if (C->getElementAsAPFloat(I).isNaN())
8398 return false;
8399 }
8400 return true;
8401 }
8402
8404 return true;
8405
8406 return false;
8407}
8408
8409static bool isKnownNonZero(const Value *V) {
8410 if (auto *C = dyn_cast<ConstantFP>(V))
8411 return !C->isZero();
8412
8413 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8414 if (!C->getElementType()->isFloatingPointTy())
8415 return false;
8416 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8417 if (C->getElementAsAPFloat(I).isZero())
8418 return false;
8419 }
8420 return true;
8421 }
8422
8423 return false;
8424}
8425
8426/// Match clamp pattern for float types without care about NaNs or signed zeros.
8427/// Given non-min/max outer cmp/select from the clamp pattern this
8428/// function recognizes if it can be substitued by a "canonical" min/max
8429/// pattern.
8431 Value *CmpLHS, Value *CmpRHS,
8432 Value *TrueVal, Value *FalseVal,
8433 Value *&LHS, Value *&RHS) {
8434 // Try to match
8435 // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2))
8436 // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2))
8437 // and return description of the outer Max/Min.
8438
8439 // First, check if select has inverse order:
8440 if (CmpRHS == FalseVal) {
8441 std::swap(TrueVal, FalseVal);
8442 Pred = CmpInst::getInversePredicate(Pred);
8443 }
8444
8445 // Assume success now. If there's no match, callers should not use these anyway.
8446 LHS = TrueVal;
8447 RHS = FalseVal;
8448
8449 const APFloat *FC1;
8450 if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite())
8451 return {SPF_UNKNOWN, SPNB_NA, false};
8452
8453 const APFloat *FC2;
8454 switch (Pred) {
8455 case CmpInst::FCMP_OLT:
8456 case CmpInst::FCMP_OLE:
8457 case CmpInst::FCMP_ULT:
8458 case CmpInst::FCMP_ULE:
8459 if (match(FalseVal, m_OrdOrUnordFMin(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8460 *FC1 < *FC2)
8461 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
8462 if (match(FalseVal, m_FMinNum(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8463 *FC1 < *FC2)
8464 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
8465 break;
8466 case CmpInst::FCMP_OGT:
8467 case CmpInst::FCMP_OGE:
8468 case CmpInst::FCMP_UGT:
8469 case CmpInst::FCMP_UGE:
8470 if (match(FalseVal, m_OrdOrUnordFMax(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8471 *FC1 > *FC2)
8472 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
8473 if (match(FalseVal, m_FMaxNum(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8474 *FC1 > *FC2)
8475 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
8476 break;
8477 default:
8478 break;
8479 }
8480
8481 return {SPF_UNKNOWN, SPNB_NA, false};
8482}
8483
8484/// Recognize variations of:
8485/// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
8487 Value *CmpLHS, Value *CmpRHS,
8488 Value *TrueVal, Value *FalseVal) {
8489 // Swap the select operands and predicate to match the patterns below.
8490 if (CmpRHS != TrueVal) {
8491 Pred = ICmpInst::getSwappedPredicate(Pred);
8492 std::swap(TrueVal, FalseVal);
8493 }
8494 const APInt *C1;
8495 if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) {
8496 const APInt *C2;
8497 // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
8498 if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8499 C1->slt(*C2) && Pred == CmpInst::ICMP_SLT)
8500 return {SPF_SMAX, SPNB_NA, false};
8501
8502 // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
8503 if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8504 C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT)
8505 return {SPF_SMIN, SPNB_NA, false};
8506
8507 // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
8508 if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8509 C1->ult(*C2) && Pred == CmpInst::ICMP_ULT)
8510 return {SPF_UMAX, SPNB_NA, false};
8511
8512 // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
8513 if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8514 C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT)
8515 return {SPF_UMIN, SPNB_NA, false};
8516 }
8517 return {SPF_UNKNOWN, SPNB_NA, false};
8518}
8519
8520/// Recognize variations of:
8521/// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c))
8523 Value *CmpLHS, Value *CmpRHS,
8524 Value *TVal, Value *FVal,
8525 unsigned Depth) {
8526 // TODO: Allow FP min/max with nnan/nsz.
8527 assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison");
8528
8529 Value *A = nullptr, *B = nullptr;
8530 SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1);
8531 if (!SelectPatternResult::isMinOrMax(L.Flavor))
8532 return {SPF_UNKNOWN, SPNB_NA, false};
8533
8534 Value *C = nullptr, *D = nullptr;
8535 SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1);
8536 if (L.Flavor != R.Flavor)
8537 return {SPF_UNKNOWN, SPNB_NA, false};
8538
8539 // We have something like: x Pred y ? min(a, b) : min(c, d).
8540 // Try to match the compare to the min/max operations of the select operands.
8541 // First, make sure we have the right compare predicate.
8542 switch (L.Flavor) {
8543 case SPF_SMIN:
8544 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) {
8545 Pred = ICmpInst::getSwappedPredicate(Pred);
8546 std::swap(CmpLHS, CmpRHS);
8547 }
8548 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
8549 break;
8550 return {SPF_UNKNOWN, SPNB_NA, false};
8551 case SPF_SMAX:
8552 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) {
8553 Pred = ICmpInst::getSwappedPredicate(Pred);
8554 std::swap(CmpLHS, CmpRHS);
8555 }
8556 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE)
8557 break;
8558 return {SPF_UNKNOWN, SPNB_NA, false};
8559 case SPF_UMIN:
8560 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
8561 Pred = ICmpInst::getSwappedPredicate(Pred);
8562 std::swap(CmpLHS, CmpRHS);
8563 }
8564 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE)
8565 break;
8566 return {SPF_UNKNOWN, SPNB_NA, false};
8567 case SPF_UMAX:
8568 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
8569 Pred = ICmpInst::getSwappedPredicate(Pred);
8570 std::swap(CmpLHS, CmpRHS);
8571 }
8572 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
8573 break;
8574 return {SPF_UNKNOWN, SPNB_NA, false};
8575 default:
8576 return {SPF_UNKNOWN, SPNB_NA, false};
8577 }
8578
8579 // If there is a common operand in the already matched min/max and the other
8580 // min/max operands match the compare operands (either directly or inverted),
8581 // then this is min/max of the same flavor.
8582
8583 // a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8584 // ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8585 if (D == B) {
8586 if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8587 match(A, m_Not(m_Specific(CmpRHS)))))
8588 return {L.Flavor, SPNB_NA, false};
8589 }
8590 // a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8591 // ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8592 if (C == B) {
8593 if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8594 match(A, m_Not(m_Specific(CmpRHS)))))
8595 return {L.Flavor, SPNB_NA, false};
8596 }
8597 // b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8598 // ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8599 if (D == A) {
8600 if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8601 match(B, m_Not(m_Specific(CmpRHS)))))
8602 return {L.Flavor, SPNB_NA, false};
8603 }
8604 // b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8605 // ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8606 if (C == A) {
8607 if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8608 match(B, m_Not(m_Specific(CmpRHS)))))
8609 return {L.Flavor, SPNB_NA, false};
8610 }
8611
8612 return {SPF_UNKNOWN, SPNB_NA, false};
8613}
8614
8615/// If the input value is the result of a 'not' op, constant integer, or vector
8616/// splat of a constant integer, return the bitwise-not source value.
8617/// TODO: This could be extended to handle non-splat vector integer constants.
8619 Value *NotV;
8620 if (match(V, m_Not(m_Value(NotV))))
8621 return NotV;
8622
8623 const APInt *C;
8624 if (match(V, m_APInt(C)))
8625 return ConstantInt::get(V->getType(), ~(*C));
8626
8627 return nullptr;
8628}
8629
8630/// Match non-obvious integer minimum and maximum sequences.
8632 Value *CmpLHS, Value *CmpRHS,
8633 Value *TrueVal, Value *FalseVal,
8634 Value *&LHS, Value *&RHS,
8635 unsigned Depth) {
8636 // Assume success. If there's no match, callers should not use these anyway.
8637 LHS = TrueVal;
8638 RHS = FalseVal;
8639
8640 SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);
8642 return SPR;
8643
8644 SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth);
8646 return SPR;
8647
8648 // Look through 'not' ops to find disguised min/max.
8649 // (X > Y) ? ~X : ~Y ==> (~X < ~Y) ? ~X : ~Y ==> MIN(~X, ~Y)
8650 // (X < Y) ? ~X : ~Y ==> (~X > ~Y) ? ~X : ~Y ==> MAX(~X, ~Y)
8651 if (CmpLHS == getNotValue(TrueVal) && CmpRHS == getNotValue(FalseVal)) {
8652 switch (Pred) {
8653 case CmpInst::ICMP_SGT: return {SPF_SMIN, SPNB_NA, false};
8654 case CmpInst::ICMP_SLT: return {SPF_SMAX, SPNB_NA, false};
8655 case CmpInst::ICMP_UGT: return {SPF_UMIN, SPNB_NA, false};
8656 case CmpInst::ICMP_ULT: return {SPF_UMAX, SPNB_NA, false};
8657 default: break;
8658 }
8659 }
8660
8661 // (X > Y) ? ~Y : ~X ==> (~X < ~Y) ? ~Y : ~X ==> MAX(~Y, ~X)
8662 // (X < Y) ? ~Y : ~X ==> (~X > ~Y) ? ~Y : ~X ==> MIN(~Y, ~X)
8663 if (CmpLHS == getNotValue(FalseVal) && CmpRHS == getNotValue(TrueVal)) {
8664 switch (Pred) {
8665 case CmpInst::ICMP_SGT: return {SPF_SMAX, SPNB_NA, false};
8666 case CmpInst::ICMP_SLT: return {SPF_SMIN, SPNB_NA, false};
8667 case CmpInst::ICMP_UGT: return {SPF_UMAX, SPNB_NA, false};
8668 case CmpInst::ICMP_ULT: return {SPF_UMIN, SPNB_NA, false};
8669 default: break;
8670 }
8671 }
8672
8673 if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT)
8674 return {SPF_UNKNOWN, SPNB_NA, false};
8675
8676 const APInt *C1;
8677 if (!match(CmpRHS, m_APInt(C1)))
8678 return {SPF_UNKNOWN, SPNB_NA, false};
8679
8680 // An unsigned min/max can be written with a signed compare.
8681 const APInt *C2;
8682 if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) ||
8683 (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) {
8684 // Is the sign bit set?
8685 // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX
8686 // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN
8687 if (Pred == CmpInst::ICMP_SLT && C1->isZero() && C2->isMaxSignedValue())
8688 return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8689
8690 // Is the sign bit clear?
8691 // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX
8692 // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN
8693 if (Pred == CmpInst::ICMP_SGT && C1->isAllOnes() && C2->isMinSignedValue())
8694 return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8695 }
8696
8697 return {SPF_UNKNOWN, SPNB_NA, false};
8698}
8699
8700bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW,
8701 bool AllowPoison) {
8702 assert(X && Y && "Invalid operand");
8703
8704 auto IsNegationOf = [&](const Value *X, const Value *Y) {
8705 if (!match(X, m_Neg(m_Specific(Y))))
8706 return false;
8707
8708 auto *BO = cast<BinaryOperator>(X);
8709 if (NeedNSW && !BO->hasNoSignedWrap())
8710 return false;
8711
8712 auto *Zero = cast<Constant>(BO->getOperand(0));
8713 if (!AllowPoison && !Zero->isNullValue())
8714 return false;
8715
8716 return true;
8717 };
8718
8719 // X = -Y or Y = -X
8720 if (IsNegationOf(X, Y) || IsNegationOf(Y, X))
8721 return true;
8722
8723 // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A)
8724 Value *A, *B;
8725 return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) &&
8726 match(Y, m_Sub(m_Specific(B), m_Specific(A))))) ||
8727 (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) &&
8729}
8730
8731bool llvm::isKnownInversion(const Value *X, const Value *Y) {
8732 // Handle X = icmp pred A, B, Y = icmp pred A, C.
8733 Value *A, *B, *C;
8734 CmpPredicate Pred1, Pred2;
8735 if (!match(X, m_ICmp(Pred1, m_Value(A), m_Value(B))) ||
8736 !match(Y, m_c_ICmp(Pred2, m_Specific(A), m_Value(C))))
8737 return false;
8738
8739 // They must both have samesign flag or not.
8740 if (Pred1.hasSameSign() != Pred2.hasSameSign())
8741 return false;
8742
8743 if (B == C)
8744 return Pred1 == ICmpInst::getInversePredicate(Pred2);
8745
8746 // Try to infer the relationship from constant ranges.
8747 const APInt *RHSC1, *RHSC2;
8748 if (!match(B, m_APInt(RHSC1)) || !match(C, m_APInt(RHSC2)))
8749 return false;
8750
8751 // Sign bits of two RHSCs should match.
8752 if (Pred1.hasSameSign() && RHSC1->isNonNegative() != RHSC2->isNonNegative())
8753 return false;
8754
8755 const auto CR1 = ConstantRange::makeExactICmpRegion(Pred1, *RHSC1);
8756 const auto CR2 = ConstantRange::makeExactICmpRegion(Pred2, *RHSC2);
8757
8758 return CR1.inverse() == CR2;
8759}
8760
8762 SelectPatternNaNBehavior NaNBehavior,
8763 bool Ordered) {
8764 switch (Pred) {
8765 default:
8766 return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.
8767 case ICmpInst::ICMP_UGT:
8768 case ICmpInst::ICMP_UGE:
8769 return {SPF_UMAX, SPNB_NA, false};
8770 case ICmpInst::ICMP_SGT:
8771 case ICmpInst::ICMP_SGE:
8772 return {SPF_SMAX, SPNB_NA, false};
8773 case ICmpInst::ICMP_ULT:
8774 case ICmpInst::ICMP_ULE:
8775 return {SPF_UMIN, SPNB_NA, false};
8776 case ICmpInst::ICMP_SLT:
8777 case ICmpInst::ICMP_SLE:
8778 return {SPF_SMIN, SPNB_NA, false};
8779 case FCmpInst::FCMP_UGT:
8780 case FCmpInst::FCMP_UGE:
8781 case FCmpInst::FCMP_OGT:
8782 case FCmpInst::FCMP_OGE:
8783 return {SPF_FMAXNUM, NaNBehavior, Ordered};
8784 case FCmpInst::FCMP_ULT:
8785 case FCmpInst::FCMP_ULE:
8786 case FCmpInst::FCMP_OLT:
8787 case FCmpInst::FCMP_OLE:
8788 return {SPF_FMINNUM, NaNBehavior, Ordered};
8789 }
8790}
8791
8792std::optional<std::pair<CmpPredicate, Constant *>>
8795 "Only for relational integer predicates.");
8796 if (isa<UndefValue>(C))
8797 return std::nullopt;
8798
8799 Type *Type = C->getType();
8800 bool IsSigned = ICmpInst::isSigned(Pred);
8801
8803 bool WillIncrement =
8804 UnsignedPred == ICmpInst::ICMP_ULE || UnsignedPred == ICmpInst::ICMP_UGT;
8805
8806 // Check if the constant operand can be safely incremented/decremented
8807 // without overflowing/underflowing.
8808 auto ConstantIsOk = [WillIncrement, IsSigned](ConstantInt *C) {
8809 return WillIncrement ? !C->isMaxValue(IsSigned) : !C->isMinValue(IsSigned);
8810 };
8811
8812 Constant *SafeReplacementConstant = nullptr;
8813 if (auto *CI = dyn_cast<ConstantInt>(C)) {
8814 // Bail out if the constant can't be safely incremented/decremented.
8815 if (!ConstantIsOk(CI))
8816 return std::nullopt;
8817 } else if (auto *FVTy = dyn_cast<FixedVectorType>(Type)) {
8818 unsigned NumElts = FVTy->getNumElements();
8819 for (unsigned i = 0; i != NumElts; ++i) {
8820 Constant *Elt = C->getAggregateElement(i);
8821 if (!Elt)
8822 return std::nullopt;
8823
8824 if (isa<UndefValue>(Elt))
8825 continue;
8826
8827 // Bail out if we can't determine if this constant is min/max or if we
8828 // know that this constant is min/max.
8829 auto *CI = dyn_cast<ConstantInt>(Elt);
8830 if (!CI || !ConstantIsOk(CI))
8831 return std::nullopt;
8832
8833 if (!SafeReplacementConstant)
8834 SafeReplacementConstant = CI;
8835 }
8836 } else if (isa<VectorType>(C->getType())) {
8837 // Handle scalable splat
8838 Value *SplatC = C->getSplatValue();
8839 auto *CI = dyn_cast_or_null<ConstantInt>(SplatC);
8840 // Bail out if the constant can't be safely incremented/decremented.
8841 if (!CI || !ConstantIsOk(CI))
8842 return std::nullopt;
8843 } else {
8844 // ConstantExpr?
8845 return std::nullopt;
8846 }
8847
8848 // It may not be safe to change a compare predicate in the presence of
8849 // undefined elements, so replace those elements with the first safe constant
8850 // that we found.
8851 // TODO: in case of poison, it is safe; let's replace undefs only.
8852 if (C->containsUndefOrPoisonElement()) {
8853 assert(SafeReplacementConstant && "Replacement constant not set");
8854 C = Constant::replaceUndefsWith(C, SafeReplacementConstant);
8855 }
8856
8858
8859 // Increment or decrement the constant.
8860 Constant *OneOrNegOne = ConstantInt::get(Type, WillIncrement ? 1 : -1, true);
8861 Constant *NewC = ConstantExpr::getAdd(C, OneOrNegOne);
8862
8863 return std::make_pair(NewPred, NewC);
8864}
8865
8867 FastMathFlags FMF,
8868 Value *CmpLHS, Value *CmpRHS,
8869 Value *TrueVal, Value *FalseVal,
8870 Value *&LHS, Value *&RHS,
8871 unsigned Depth) {
8872 bool HasMismatchedZeros = false;
8873 if (CmpInst::isFPPredicate(Pred)) {
8874 // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one
8875 // 0.0 operand, set the compare's 0.0 operands to that same value for the
8876 // purpose of identifying min/max. Disregard vector constants with undefined
8877 // elements because those can not be back-propagated for analysis.
8878 Value *OutputZeroVal = nullptr;
8879 if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) &&
8880 !cast<Constant>(TrueVal)->containsUndefOrPoisonElement())
8881 OutputZeroVal = TrueVal;
8882 else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) &&
8883 !cast<Constant>(FalseVal)->containsUndefOrPoisonElement())
8884 OutputZeroVal = FalseVal;
8885
8886 if (OutputZeroVal) {
8887 if (match(CmpLHS, m_AnyZeroFP()) && CmpLHS != OutputZeroVal) {
8888 HasMismatchedZeros = true;
8889 CmpLHS = OutputZeroVal;
8890 }
8891 if (match(CmpRHS, m_AnyZeroFP()) && CmpRHS != OutputZeroVal) {
8892 HasMismatchedZeros = true;
8893 CmpRHS = OutputZeroVal;
8894 }
8895 }
8896 }
8897
8898 LHS = CmpLHS;
8899 RHS = CmpRHS;
8900
8901 // Signed zero may return inconsistent results between implementations.
8902 // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0
8903 // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)
8904 // Therefore, we behave conservatively and only proceed if at least one of the
8905 // operands is known to not be zero or if we don't care about signed zero.
8906 switch (Pred) {
8907 default: break;
8910 if (!HasMismatchedZeros)
8911 break;
8912 [[fallthrough]];
8915 if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8916 !isKnownNonZero(CmpRHS))
8917 return {SPF_UNKNOWN, SPNB_NA, false};
8918 }
8919
8920 SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
8921 bool Ordered = false;
8922
8923 // When given one NaN and one non-NaN input:
8924 // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
8925 // - A simple C99 (a < b ? a : b) construction will return 'b' (as the
8926 // ordered comparison fails), which could be NaN or non-NaN.
8927 // so here we discover exactly what NaN behavior is required/accepted.
8928 if (CmpInst::isFPPredicate(Pred)) {
8929 bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
8930 bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
8931
8932 if (LHSSafe && RHSSafe) {
8933 // Both operands are known non-NaN.
8934 NaNBehavior = SPNB_RETURNS_ANY;
8935 Ordered = CmpInst::isOrdered(Pred);
8936 } else if (CmpInst::isOrdered(Pred)) {
8937 // An ordered comparison will return false when given a NaN, so it
8938 // returns the RHS.
8939 Ordered = true;
8940 if (LHSSafe)
8941 // LHS is non-NaN, so if RHS is NaN then NaN will be returned.
8942 NaNBehavior = SPNB_RETURNS_NAN;
8943 else if (RHSSafe)
8944 NaNBehavior = SPNB_RETURNS_OTHER;
8945 else
8946 // Completely unsafe.
8947 return {SPF_UNKNOWN, SPNB_NA, false};
8948 } else {
8949 Ordered = false;
8950 // An unordered comparison will return true when given a NaN, so it
8951 // returns the LHS.
8952 if (LHSSafe)
8953 // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
8954 NaNBehavior = SPNB_RETURNS_OTHER;
8955 else if (RHSSafe)
8956 NaNBehavior = SPNB_RETURNS_NAN;
8957 else
8958 // Completely unsafe.
8959 return {SPF_UNKNOWN, SPNB_NA, false};
8960 }
8961 }
8962
8963 if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
8964 std::swap(CmpLHS, CmpRHS);
8965 Pred = CmpInst::getSwappedPredicate(Pred);
8966 if (NaNBehavior == SPNB_RETURNS_NAN)
8967 NaNBehavior = SPNB_RETURNS_OTHER;
8968 else if (NaNBehavior == SPNB_RETURNS_OTHER)
8969 NaNBehavior = SPNB_RETURNS_NAN;
8970 Ordered = !Ordered;
8971 }
8972
8973 // ([if]cmp X, Y) ? X : Y
8974 if (TrueVal == CmpLHS && FalseVal == CmpRHS)
8975 return getSelectPattern(Pred, NaNBehavior, Ordered);
8976
8977 if (isKnownNegation(TrueVal, FalseVal)) {
8978 // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can
8979 // match against either LHS or sext(LHS).
8980 auto MaybeSExtCmpLHS =
8981 m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS)));
8982 auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes());
8983 auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One());
8984 if (match(TrueVal, MaybeSExtCmpLHS)) {
8985 // Set the return values. If the compare uses the negated value (-X >s 0),
8986 // swap the return values because the negated value is always 'RHS'.
8987 LHS = TrueVal;
8988 RHS = FalseVal;
8989 if (match(CmpLHS, m_Neg(m_Specific(FalseVal))))
8990 std::swap(LHS, RHS);
8991
8992 // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X)
8993 // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X)
8994 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8995 return {SPF_ABS, SPNB_NA, false};
8996
8997 // (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X)
8998 if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne))
8999 return {SPF_ABS, SPNB_NA, false};
9000
9001 // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X)
9002 // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X)
9003 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
9004 return {SPF_NABS, SPNB_NA, false};
9005 }
9006 else if (match(FalseVal, MaybeSExtCmpLHS)) {
9007 // Set the return values. If the compare uses the negated value (-X >s 0),
9008 // swap the return values because the negated value is always 'RHS'.
9009 LHS = FalseVal;
9010 RHS = TrueVal;
9011 if (match(CmpLHS, m_Neg(m_Specific(TrueVal))))
9012 std::swap(LHS, RHS);
9013
9014 // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X)
9015 // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X)
9016 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
9017 return {SPF_NABS, SPNB_NA, false};
9018
9019 // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X)
9020 // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X)
9021 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
9022 return {SPF_ABS, SPNB_NA, false};
9023 }
9024 }
9025
9026 if (CmpInst::isIntPredicate(Pred))
9027 return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth);
9028
9029 // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar
9030 // may return either -0.0 or 0.0, so fcmp/select pair has stricter
9031 // semantics than minNum. Be conservative in such case.
9032 if (NaNBehavior != SPNB_RETURNS_ANY ||
9033 (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
9034 !isKnownNonZero(CmpRHS)))
9035 return {SPF_UNKNOWN, SPNB_NA, false};
9036
9037 return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
9038}
9039
9041 Instruction::CastOps *CastOp) {
9042 const DataLayout &DL = CmpI->getDataLayout();
9043
9044 Constant *CastedTo = nullptr;
9045 switch (*CastOp) {
9046 case Instruction::ZExt:
9047 if (CmpI->isUnsigned())
9048 CastedTo = ConstantExpr::getTrunc(C, SrcTy);
9049 break;
9050 case Instruction::SExt:
9051 if (CmpI->isSigned())
9052 CastedTo = ConstantExpr::getTrunc(C, SrcTy, true);
9053 break;
9054 case Instruction::Trunc:
9055 Constant *CmpConst;
9056 if (match(CmpI->getOperand(1), m_Constant(CmpConst)) &&
9057 CmpConst->getType() == SrcTy) {
9058 // Here we have the following case:
9059 //
9060 // %cond = cmp iN %x, CmpConst
9061 // %tr = trunc iN %x to iK
9062 // %narrowsel = select i1 %cond, iK %t, iK C
9063 //
9064 // We can always move trunc after select operation:
9065 //
9066 // %cond = cmp iN %x, CmpConst
9067 // %widesel = select i1 %cond, iN %x, iN CmpConst
9068 // %tr = trunc iN %widesel to iK
9069 //
9070 // Note that C could be extended in any way because we don't care about
9071 // upper bits after truncation. It can't be abs pattern, because it would
9072 // look like:
9073 //
9074 // select i1 %cond, x, -x.
9075 //
9076 // So only min/max pattern could be matched. Such match requires widened C
9077 // == CmpConst. That is why set widened C = CmpConst, condition trunc
9078 // CmpConst == C is checked below.
9079 CastedTo = CmpConst;
9080 } else {
9081 unsigned ExtOp = CmpI->isSigned() ? Instruction::SExt : Instruction::ZExt;
9082 CastedTo = ConstantFoldCastOperand(ExtOp, C, SrcTy, DL);
9083 }
9084 break;
9085 case Instruction::FPTrunc:
9086 CastedTo = ConstantFoldCastOperand(Instruction::FPExt, C, SrcTy, DL);
9087 break;
9088 case Instruction::FPExt:
9089 CastedTo = ConstantFoldCastOperand(Instruction::FPTrunc, C, SrcTy, DL);
9090 break;
9091 case Instruction::FPToUI:
9092 CastedTo = ConstantFoldCastOperand(Instruction::UIToFP, C, SrcTy, DL);
9093 break;
9094 case Instruction::FPToSI:
9095 CastedTo = ConstantFoldCastOperand(Instruction::SIToFP, C, SrcTy, DL);
9096 break;
9097 case Instruction::UIToFP:
9098 CastedTo = ConstantFoldCastOperand(Instruction::FPToUI, C, SrcTy, DL);
9099 break;
9100 case Instruction::SIToFP:
9101 CastedTo = ConstantFoldCastOperand(Instruction::FPToSI, C, SrcTy, DL);
9102 break;
9103 default:
9104 break;
9105 }
9106
9107 if (!CastedTo)
9108 return nullptr;
9109
9110 // Make sure the cast doesn't lose any information.
9111 Constant *CastedBack =
9112 ConstantFoldCastOperand(*CastOp, CastedTo, C->getType(), DL);
9113 if (CastedBack && CastedBack != C)
9114 return nullptr;
9115
9116 return CastedTo;
9117}
9118
9119/// Helps to match a select pattern in case of a type mismatch.
9120///
9121/// The function processes the case when type of true and false values of a
9122/// select instruction differs from type of the cmp instruction operands because
9123/// of a cast instruction. The function checks if it is legal to move the cast
9124/// operation after "select". If yes, it returns the new second value of
9125/// "select" (with the assumption that cast is moved):
9126/// 1. As operand of cast instruction when both values of "select" are same cast
9127/// instructions.
9128/// 2. As restored constant (by applying reverse cast operation) when the first
9129/// value of the "select" is a cast operation and the second value is a
9130/// constant. It is implemented in lookThroughCastConst().
9131/// 3. As one operand is cast instruction and the other is not. The operands in
9132/// sel(cmp) are in different type integer.
9133/// NOTE: We return only the new second value because the first value could be
9134/// accessed as operand of cast instruction.
9135static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2,
9136 Instruction::CastOps *CastOp) {
9137 auto *Cast1 = dyn_cast<CastInst>(V1);
9138 if (!Cast1)
9139 return nullptr;
9140
9141 *CastOp = Cast1->getOpcode();
9142 Type *SrcTy = Cast1->getSrcTy();
9143 if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
9144 // If V1 and V2 are both the same cast from the same type, look through V1.
9145 if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
9146 return Cast2->getOperand(0);
9147 return nullptr;
9148 }
9149
9150 auto *C = dyn_cast<Constant>(V2);
9151 if (C)
9152 return lookThroughCastConst(CmpI, SrcTy, C, CastOp);
9153
9154 Value *CastedTo = nullptr;
9155 if (*CastOp == Instruction::Trunc) {
9156 if (match(CmpI->getOperand(1), m_ZExtOrSExt(m_Specific(V2)))) {
9157 // Here we have the following case:
9158 // %y_ext = sext iK %y to iN
9159 // %cond = cmp iN %x, %y_ext
9160 // %tr = trunc iN %x to iK
9161 // %narrowsel = select i1 %cond, iK %tr, iK %y
9162 //
9163 // We can always move trunc after select operation:
9164 // %y_ext = sext iK %y to iN
9165 // %cond = cmp iN %x, %y_ext
9166 // %widesel = select i1 %cond, iN %x, iN %y_ext
9167 // %tr = trunc iN %widesel to iK
9168 assert(V2->getType() == Cast1->getType() &&
9169 "V2 and Cast1 should be the same type.");
9170 CastedTo = CmpI->getOperand(1);
9171 }
9172 }
9173
9174 return CastedTo;
9175}
9177 Instruction::CastOps *CastOp,
9178 unsigned Depth) {
9180 return {SPF_UNKNOWN, SPNB_NA, false};
9181
9183 if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
9184
9185 CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
9186 if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};
9187
9188 Value *TrueVal = SI->getTrueValue();
9189 Value *FalseVal = SI->getFalseValue();
9190
9192 CmpI, TrueVal, FalseVal, LHS, RHS,
9193 isa<FPMathOperator>(SI) ? SI->getFastMathFlags() : FastMathFlags(),
9194 CastOp, Depth);
9195}
9196
9198 CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS,
9199 FastMathFlags FMF, Instruction::CastOps *CastOp, unsigned Depth) {
9200 CmpInst::Predicate Pred = CmpI->getPredicate();
9201 Value *CmpLHS = CmpI->getOperand(0);
9202 Value *CmpRHS = CmpI->getOperand(1);
9203 if (isa<FPMathOperator>(CmpI) && CmpI->hasNoNaNs())
9204 FMF.setNoNaNs();
9205
9206 // Bail out early.
9207 if (CmpI->isEquality())
9208 return {SPF_UNKNOWN, SPNB_NA, false};
9209
9210 // Deal with type mismatches.
9211 if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
9212 if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) {
9213 // If this is a potential fmin/fmax with a cast to integer, then ignore
9214 // -0.0 because there is no corresponding integer value.
9215 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9216 FMF.setNoSignedZeros();
9217 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9218 cast<CastInst>(TrueVal)->getOperand(0), C,
9219 LHS, RHS, Depth);
9220 }
9221 if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) {
9222 // If this is a potential fmin/fmax with a cast to integer, then ignore
9223 // -0.0 because there is no corresponding integer value.
9224 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9225 FMF.setNoSignedZeros();
9226 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9227 C, cast<CastInst>(FalseVal)->getOperand(0),
9228 LHS, RHS, Depth);
9229 }
9230 }
9231 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
9232 LHS, RHS, Depth);
9233}
9234
9236 if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT;
9237 if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT;
9238 if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT;
9239 if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT;
9240 if (SPF == SPF_FMINNUM)
9241 return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT;
9242 if (SPF == SPF_FMAXNUM)
9243 return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT;
9244 llvm_unreachable("unhandled!");
9245}
9246
9248 switch (SPF) {
9250 return Intrinsic::umin;
9252 return Intrinsic::umax;
9254 return Intrinsic::smin;
9256 return Intrinsic::smax;
9257 default:
9258 llvm_unreachable("Unexpected SPF");
9259 }
9260}
9261
9263 if (SPF == SPF_SMIN) return SPF_SMAX;
9264 if (SPF == SPF_UMIN) return SPF_UMAX;
9265 if (SPF == SPF_SMAX) return SPF_SMIN;
9266 if (SPF == SPF_UMAX) return SPF_UMIN;
9267 llvm_unreachable("unhandled!");
9268}
9269
9271 switch (MinMaxID) {
9272 case Intrinsic::smax: return Intrinsic::smin;
9273 case Intrinsic::smin: return Intrinsic::smax;
9274 case Intrinsic::umax: return Intrinsic::umin;
9275 case Intrinsic::umin: return Intrinsic::umax;
9276 // Please note that next four intrinsics may produce the same result for
9277 // original and inverted case even if X != Y due to NaN is handled specially.
9278 case Intrinsic::maximum: return Intrinsic::minimum;
9279 case Intrinsic::minimum: return Intrinsic::maximum;
9280 case Intrinsic::maxnum: return Intrinsic::minnum;
9281 case Intrinsic::minnum: return Intrinsic::maxnum;
9282 case Intrinsic::maximumnum:
9283 return Intrinsic::minimumnum;
9284 case Intrinsic::minimumnum:
9285 return Intrinsic::maximumnum;
9286 default: llvm_unreachable("Unexpected intrinsic");
9287 }
9288}
9289
9291 switch (SPF) {
9294 case SPF_UMAX: return APInt::getMaxValue(BitWidth);
9295 case SPF_UMIN: return APInt::getMinValue(BitWidth);
9296 default: llvm_unreachable("Unexpected flavor");
9297 }
9298}
9299
9300std::pair<Intrinsic::ID, bool>
9302 // Check if VL contains select instructions that can be folded into a min/max
9303 // vector intrinsic and return the intrinsic if it is possible.
9304 // TODO: Support floating point min/max.
9305 bool AllCmpSingleUse = true;
9306 SelectPatternResult SelectPattern;
9307 SelectPattern.Flavor = SPF_UNKNOWN;
9308 if (all_of(VL, [&SelectPattern, &AllCmpSingleUse](Value *I) {
9309 Value *LHS, *RHS;
9310 auto CurrentPattern = matchSelectPattern(I, LHS, RHS);
9311 if (!SelectPatternResult::isMinOrMax(CurrentPattern.Flavor))
9312 return false;
9313 if (SelectPattern.Flavor != SPF_UNKNOWN &&
9314 SelectPattern.Flavor != CurrentPattern.Flavor)
9315 return false;
9316 SelectPattern = CurrentPattern;
9317 AllCmpSingleUse &=
9319 return true;
9320 })) {
9321 switch (SelectPattern.Flavor) {
9322 case SPF_SMIN:
9323 return {Intrinsic::smin, AllCmpSingleUse};
9324 case SPF_UMIN:
9325 return {Intrinsic::umin, AllCmpSingleUse};
9326 case SPF_SMAX:
9327 return {Intrinsic::smax, AllCmpSingleUse};
9328 case SPF_UMAX:
9329 return {Intrinsic::umax, AllCmpSingleUse};
9330 case SPF_FMAXNUM:
9331 return {Intrinsic::maxnum, AllCmpSingleUse};
9332 case SPF_FMINNUM:
9333 return {Intrinsic::minnum, AllCmpSingleUse};
9334 default:
9335 llvm_unreachable("unexpected select pattern flavor");
9336 }
9337 }
9338 return {Intrinsic::not_intrinsic, false};
9339}
9340
9341template <typename InstTy>
9342static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst,
9343 Value *&Init, Value *&OtherOp) {
9344 // Handle the case of a simple two-predecessor recurrence PHI.
9345 // There's a lot more that could theoretically be done here, but
9346 // this is sufficient to catch some interesting cases.
9347 // TODO: Expand list -- gep, uadd.sat etc.
9348 if (PN->getNumIncomingValues() != 2)
9349 return false;
9350
9351 for (unsigned I = 0; I != 2; ++I) {
9352 if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
9353 Operation && Operation->getNumOperands() >= 2) {
9354 Value *LHS = Operation->getOperand(0);
9355 Value *RHS = Operation->getOperand(1);
9356 if (LHS != PN && RHS != PN)
9357 continue;
9358
9359 Inst = Operation;
9360 Init = PN->getIncomingValue(!I);
9361 OtherOp = (LHS == PN) ? RHS : LHS;
9362 return true;
9363 }
9364 }
9365 return false;
9366}
9367
9368template <typename InstTy>
9369static bool matchThreeInputRecurrence(const PHINode *PN, InstTy *&Inst,
9370 Value *&Init, Value *&OtherOp0,
9371 Value *&OtherOp1) {
9372 if (PN->getNumIncomingValues() != 2)
9373 return false;
9374
9375 for (unsigned I = 0; I != 2; ++I) {
9376 if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
9377 Operation && Operation->getNumOperands() >= 3) {
9378 Value *Op0 = Operation->getOperand(0);
9379 Value *Op1 = Operation->getOperand(1);
9380 Value *Op2 = Operation->getOperand(2);
9381
9382 if (Op0 != PN && Op1 != PN && Op2 != PN)
9383 continue;
9384
9385 Inst = Operation;
9386 Init = PN->getIncomingValue(!I);
9387 if (Op0 == PN) {
9388 OtherOp0 = Op1;
9389 OtherOp1 = Op2;
9390 } else if (Op1 == PN) {
9391 OtherOp0 = Op0;
9392 OtherOp1 = Op2;
9393 } else {
9394 OtherOp0 = Op0;
9395 OtherOp1 = Op1;
9396 }
9397 return true;
9398 }
9399 }
9400 return false;
9401}
9403 Value *&Start, Value *&Step) {
9404 // We try to match a recurrence of the form:
9405 // %iv = [Start, %entry], [%iv.next, %backedge]
9406 // %iv.next = binop %iv, Step
9407 // Or:
9408 // %iv = [Start, %entry], [%iv.next, %backedge]
9409 // %iv.next = binop Step, %iv
9410 return matchTwoInputRecurrence(P, BO, Start, Step);
9411}
9412
9414 Value *&Start, Value *&Step) {
9415 BinaryOperator *BO = nullptr;
9416 P = dyn_cast<PHINode>(I->getOperand(0));
9417 if (!P)
9418 P = dyn_cast<PHINode>(I->getOperand(1));
9419 return P && matchSimpleRecurrence(P, BO, Start, Step) && BO == I;
9420}
9421
9423 PHINode *&P, Value *&Init,
9424 Value *&OtherOp) {
9425 // Binary intrinsics only supported for now.
9426 if (I->arg_size() != 2 || I->getType() != I->getArgOperand(0)->getType() ||
9427 I->getType() != I->getArgOperand(1)->getType())
9428 return false;
9429
9430 IntrinsicInst *II = nullptr;
9431 P = dyn_cast<PHINode>(I->getArgOperand(0));
9432 if (!P)
9433 P = dyn_cast<PHINode>(I->getArgOperand(1));
9434
9435 return P && matchTwoInputRecurrence(P, II, Init, OtherOp) && II == I;
9436}
9437
9439 PHINode *&P, Value *&Init,
9440 Value *&OtherOp0,
9441 Value *&OtherOp1) {
9442 if (I->arg_size() != 3 || I->getType() != I->getArgOperand(0)->getType() ||
9443 I->getType() != I->getArgOperand(1)->getType() ||
9444 I->getType() != I->getArgOperand(2)->getType())
9445 return false;
9446 IntrinsicInst *II = nullptr;
9447 P = dyn_cast<PHINode>(I->getArgOperand(0));
9448 if (!P) {
9449 P = dyn_cast<PHINode>(I->getArgOperand(1));
9450 if (!P)
9451 P = dyn_cast<PHINode>(I->getArgOperand(2));
9452 }
9453 return P && matchThreeInputRecurrence(P, II, Init, OtherOp0, OtherOp1) &&
9454 II == I;
9455}
9456
9457/// Return true if "icmp Pred LHS RHS" is always true.
9459 const Value *RHS) {
9460 if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS)
9461 return true;
9462
9463 switch (Pred) {
9464 default:
9465 return false;
9466
9467 case CmpInst::ICMP_SLE: {
9468 const APInt *C;
9469
9470 // LHS s<= LHS +_{nsw} C if C >= 0
9471 // LHS s<= LHS | C if C >= 0
9472 if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C))) ||
9474 return !C->isNegative();
9475
9476 // LHS s<= smax(LHS, V) for any V
9478 return true;
9479
9480 // smin(RHS, V) s<= RHS for any V
9482 return true;
9483
9484 // Match A to (X +_{nsw} CA) and B to (X +_{nsw} CB)
9485 const Value *X;
9486 const APInt *CLHS, *CRHS;
9487 if (match(LHS, m_NSWAddLike(m_Value(X), m_APInt(CLHS))) &&
9489 return CLHS->sle(*CRHS);
9490
9491 return false;
9492 }
9493
9494 case CmpInst::ICMP_ULE: {
9495 // LHS u<= LHS +_{nuw} V for any V
9496 if (match(RHS, m_c_Add(m_Specific(LHS), m_Value())) &&
9498 return true;
9499
9500 // LHS u<= LHS | V for any V
9501 if (match(RHS, m_c_Or(m_Specific(LHS), m_Value())))
9502 return true;
9503
9504 // LHS u<= umax(LHS, V) for any V
9506 return true;
9507
9508 // RHS >> V u<= RHS for any V
9509 if (match(LHS, m_LShr(m_Specific(RHS), m_Value())))
9510 return true;
9511
9512 // RHS u/ C_ugt_1 u<= RHS
9513 const APInt *C;
9514 if (match(LHS, m_UDiv(m_Specific(RHS), m_APInt(C))) && C->ugt(1))
9515 return true;
9516
9517 // RHS & V u<= RHS for any V
9519 return true;
9520
9521 // umin(RHS, V) u<= RHS for any V
9523 return true;
9524
9525 // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
9526 const Value *X;
9527 const APInt *CLHS, *CRHS;
9528 if (match(LHS, m_NUWAddLike(m_Value(X), m_APInt(CLHS))) &&
9530 return CLHS->ule(*CRHS);
9531
9532 return false;
9533 }
9534 }
9535}
9536
9537/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
9538/// ALHS ARHS" is true. Otherwise, return std::nullopt.
9539static std::optional<bool>
9541 const Value *ARHS, const Value *BLHS, const Value *BRHS) {
9542 switch (Pred) {
9543 default:
9544 return std::nullopt;
9545
9546 case CmpInst::ICMP_SLT:
9547 case CmpInst::ICMP_SLE:
9548 if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS) &&
9550 return true;
9551 return std::nullopt;
9552
9553 case CmpInst::ICMP_SGT:
9554 case CmpInst::ICMP_SGE:
9555 if (isTruePredicate(CmpInst::ICMP_SLE, ALHS, BLHS) &&
9557 return true;
9558 return std::nullopt;
9559
9560 case CmpInst::ICMP_ULT:
9561 case CmpInst::ICMP_ULE:
9562 if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS) &&
9564 return true;
9565 return std::nullopt;
9566
9567 case CmpInst::ICMP_UGT:
9568 case CmpInst::ICMP_UGE:
9569 if (isTruePredicate(CmpInst::ICMP_ULE, ALHS, BLHS) &&
9571 return true;
9572 return std::nullopt;
9573 }
9574}
9575
9576/// Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
9577/// Return false if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is false.
9578/// Otherwise, return std::nullopt if we can't infer anything.
9579static std::optional<bool>
9581 CmpPredicate RPred, const ConstantRange &RCR) {
9582 auto CRImpliesPred = [&](ConstantRange CR,
9583 CmpInst::Predicate Pred) -> std::optional<bool> {
9584 // If all true values for lhs and true for rhs, lhs implies rhs
9585 if (CR.icmp(Pred, RCR))
9586 return true;
9587
9588 // If there is no overlap, lhs implies not rhs
9589 if (CR.icmp(CmpInst::getInversePredicate(Pred), RCR))
9590 return false;
9591
9592 return std::nullopt;
9593 };
9594 if (auto Res = CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9595 RPred))
9596 return Res;
9597 if (LPred.hasSameSign() ^ RPred.hasSameSign()) {
9599 : LPred.dropSameSign();
9601 : RPred.dropSameSign();
9602 return CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9603 RPred);
9604 }
9605 return std::nullopt;
9606}
9607
9608/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
9609/// is true. Return false if LHS implies RHS is false. Otherwise, return
9610/// std::nullopt if we can't infer anything.
9611static std::optional<bool>
9612isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1,
9613 CmpPredicate RPred, const Value *R0, const Value *R1,
9614 const DataLayout &DL, bool LHSIsTrue) {
9615 // The rest of the logic assumes the LHS condition is true. If that's not the
9616 // case, invert the predicate to make it so.
9617 if (!LHSIsTrue)
9618 LPred = ICmpInst::getInverseCmpPredicate(LPred);
9619
9620 // We can have non-canonical operands, so try to normalize any common operand
9621 // to L0/R0.
9622 if (L0 == R1) {
9623 std::swap(R0, R1);
9624 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9625 }
9626 if (R0 == L1) {
9627 std::swap(L0, L1);
9628 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9629 }
9630 if (L1 == R1) {
9631 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
9632 if (L0 != R0 || match(L0, m_ImmConstant())) {
9633 std::swap(L0, L1);
9634 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9635 std::swap(R0, R1);
9636 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9637 }
9638 }
9639
9640 // See if we can infer anything if operand-0 matches and we have at least one
9641 // constant.
9642 const APInt *Unused;
9643 if (L0 == R0 && (match(L1, m_APInt(Unused)) || match(R1, m_APInt(Unused)))) {
9644 // Potential TODO: We could also further use the constant range of L0/R0 to
9645 // further constraint the constant ranges. At the moment this leads to
9646 // several regressions related to not transforming `multi_use(A + C0) eq/ne
9647 // C1` (see discussion: D58633).
9648 SimplifyQuery SQ(DL);
9653
9654 // Even if L1/R1 are not both constant, we can still sometimes deduce
9655 // relationship from a single constant. For example X u> Y implies X != 0.
9656 if (auto R = isImpliedCondCommonOperandWithCR(LPred, LCR, RPred, RCR))
9657 return R;
9658 // If both L1/R1 were exact constant ranges and we didn't get anything
9659 // here, we won't be able to deduce this.
9660 if (match(L1, m_APInt(Unused)) && match(R1, m_APInt(Unused)))
9661 return std::nullopt;
9662 }
9663
9664 // Can we infer anything when the two compares have matching operands?
9665 if (L0 == R0 && L1 == R1)
9666 return ICmpInst::isImpliedByMatchingCmp(LPred, RPred);
9667
9668 // It only really makes sense in the context of signed comparison for "X - Y
9669 // must be positive if X >= Y and no overflow".
9670 // Take SGT as an example: L0:x > L1:y and C >= 0
9671 // ==> R0:(x -nsw y) < R1:(-C) is false
9672 CmpInst::Predicate SignedLPred = LPred.getPreferredSignedPredicate();
9673 if ((SignedLPred == ICmpInst::ICMP_SGT ||
9674 SignedLPred == ICmpInst::ICMP_SGE) &&
9675 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9676 if (match(R1, m_NonPositive()) &&
9677 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == false)
9678 return false;
9679 }
9680
9681 // Take SLT as an example: L0:x < L1:y and C <= 0
9682 // ==> R0:(x -nsw y) < R1:(-C) is true
9683 if ((SignedLPred == ICmpInst::ICMP_SLT ||
9684 SignedLPred == ICmpInst::ICMP_SLE) &&
9685 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9686 if (match(R1, m_NonNegative()) &&
9687 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == true)
9688 return true;
9689 }
9690
9691 // a - b == NonZero -> a != b
9692 // ptrtoint(a) - ptrtoint(b) == NonZero -> a != b
9693 const APInt *L1C;
9694 Value *A, *B;
9695 if (LPred == ICmpInst::ICMP_EQ && ICmpInst::isEquality(RPred) &&
9696 match(L1, m_APInt(L1C)) && !L1C->isZero() &&
9697 match(L0, m_Sub(m_Value(A), m_Value(B))) &&
9698 ((A == R0 && B == R1) || (A == R1 && B == R0) ||
9703 return RPred.dropSameSign() == ICmpInst::ICMP_NE;
9704 }
9705
9706 // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1
9707 if (L0 == R0 &&
9708 (LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) &&
9709 (RPred == ICmpInst::ICMP_ULT || RPred == ICmpInst::ICMP_UGE) &&
9710 match(L0, m_c_Add(m_Specific(L1), m_Specific(R1))))
9711 return CmpPredicate::getMatching(LPred, RPred).has_value();
9712
9713 if (auto P = CmpPredicate::getMatching(LPred, RPred))
9714 return isImpliedCondOperands(*P, L0, L1, R0, R1);
9715
9716 return std::nullopt;
9717}
9718
9719/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
9720/// is true. Return false if LHS implies RHS is false. Otherwise, return
9721/// std::nullopt if we can't infer anything.
9722static std::optional<bool>
9724 FCmpInst::Predicate RPred, const Value *R0, const Value *R1,
9725 const DataLayout &DL, bool LHSIsTrue) {
9726 // The rest of the logic assumes the LHS condition is true. If that's not the
9727 // case, invert the predicate to make it so.
9728 if (!LHSIsTrue)
9729 LPred = FCmpInst::getInversePredicate(LPred);
9730
9731 // We can have non-canonical operands, so try to normalize any common operand
9732 // to L0/R0.
9733 if (L0 == R1) {
9734 std::swap(R0, R1);
9735 RPred = FCmpInst::getSwappedPredicate(RPred);
9736 }
9737 if (R0 == L1) {
9738 std::swap(L0, L1);
9739 LPred = FCmpInst::getSwappedPredicate(LPred);
9740 }
9741 if (L1 == R1) {
9742 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
9743 if (L0 != R0 || match(L0, m_ImmConstant())) {
9744 std::swap(L0, L1);
9745 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9746 std::swap(R0, R1);
9747 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9748 }
9749 }
9750
9751 // Can we infer anything when the two compares have matching operands?
9752 if (L0 == R0 && L1 == R1) {
9753 if ((LPred & RPred) == LPred)
9754 return true;
9755 if ((LPred & ~RPred) == LPred)
9756 return false;
9757 }
9758
9759 // See if we can infer anything if operand-0 matches and we have at least one
9760 // constant.
9761 const APFloat *L1C, *R1C;
9762 if (L0 == R0 && match(L1, m_APFloat(L1C)) && match(R1, m_APFloat(R1C))) {
9763 if (std::optional<ConstantFPRange> DomCR =
9765 if (std::optional<ConstantFPRange> ImpliedCR =
9767 if (ImpliedCR->contains(*DomCR))
9768 return true;
9769 }
9770 if (std::optional<ConstantFPRange> ImpliedCR =
9772 FCmpInst::getInversePredicate(RPred), *R1C)) {
9773 if (ImpliedCR->contains(*DomCR))
9774 return false;
9775 }
9776 }
9777 }
9778
9779 return std::nullopt;
9780}
9781
9782/// Return true if LHS implies RHS is true. Return false if LHS implies RHS is
9783/// false. Otherwise, return std::nullopt if we can't infer anything. We
9784/// expect the RHS to be an icmp and the LHS to be an 'and', 'or', or a 'select'
9785/// instruction.
9786static std::optional<bool>
9788 const Value *RHSOp0, const Value *RHSOp1,
9789 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9790 // The LHS must be an 'or', 'and', or a 'select' instruction.
9791 assert((LHS->getOpcode() == Instruction::And ||
9792 LHS->getOpcode() == Instruction::Or ||
9793 LHS->getOpcode() == Instruction::Select) &&
9794 "Expected LHS to be 'and', 'or', or 'select'.");
9795
9796 assert(Depth <= MaxAnalysisRecursionDepth && "Hit recursion limit");
9797
9798 // If the result of an 'or' is false, then we know both legs of the 'or' are
9799 // false. Similarly, if the result of an 'and' is true, then we know both
9800 // legs of the 'and' are true.
9801 const Value *ALHS, *ARHS;
9802 if ((!LHSIsTrue && match(LHS, m_LogicalOr(m_Value(ALHS), m_Value(ARHS)))) ||
9803 (LHSIsTrue && match(LHS, m_LogicalAnd(m_Value(ALHS), m_Value(ARHS))))) {
9804 // FIXME: Make this non-recursion.
9805 if (std::optional<bool> Implication = isImpliedCondition(
9806 ALHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9807 return Implication;
9808 if (std::optional<bool> Implication = isImpliedCondition(
9809 ARHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9810 return Implication;
9811 return std::nullopt;
9812 }
9813 return std::nullopt;
9814}
9815
9816std::optional<bool>
9818 const Value *RHSOp0, const Value *RHSOp1,
9819 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9820 // Bail out when we hit the limit.
9822 return std::nullopt;
9823
9824 // A mismatch occurs when we compare a scalar cmp to a vector cmp, for
9825 // example.
9826 if (RHSOp0->getType()->isVectorTy() != LHS->getType()->isVectorTy())
9827 return std::nullopt;
9828
9829 assert(LHS->getType()->isIntOrIntVectorTy(1) &&
9830 "Expected integer type only!");
9831
9832 // Match not
9833 if (match(LHS, m_Not(m_Value(LHS))))
9834 LHSIsTrue = !LHSIsTrue;
9835
9836 // Both LHS and RHS are icmps.
9837 if (RHSOp0->getType()->getScalarType()->isIntOrPtrTy()) {
9838 CmpPredicate LHSPred;
9839 Value *LHSOp0, *LHSOp1;
9840 if (match(LHS, m_ICmpLike(LHSPred, m_Value(LHSOp0), m_Value(LHSOp1))))
9841 return isImpliedCondICmps(LHSPred, LHSOp0, LHSOp1, RHSPred, RHSOp0,
9842 RHSOp1, DL, LHSIsTrue);
9843 } else {
9844 assert(RHSOp0->getType()->isFPOrFPVectorTy() &&
9845 "Expected floating point type only!");
9846 if (const auto *LHSCmp = dyn_cast<FCmpInst>(LHS))
9847 return isImpliedCondFCmps(LHSCmp->getPredicate(), LHSCmp->getOperand(0),
9848 LHSCmp->getOperand(1), RHSPred, RHSOp0, RHSOp1,
9849 DL, LHSIsTrue);
9850 }
9851
9852 /// The LHS should be an 'or', 'and', or a 'select' instruction. We expect
9853 /// the RHS to be an icmp.
9854 /// FIXME: Add support for and/or/select on the RHS.
9855 if (const Instruction *LHSI = dyn_cast<Instruction>(LHS)) {
9856 if ((LHSI->getOpcode() == Instruction::And ||
9857 LHSI->getOpcode() == Instruction::Or ||
9858 LHSI->getOpcode() == Instruction::Select))
9859 return isImpliedCondAndOr(LHSI, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue,
9860 Depth);
9861 }
9862 return std::nullopt;
9863}
9864
9865std::optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS,
9866 const DataLayout &DL,
9867 bool LHSIsTrue, unsigned Depth) {
9868 // LHS ==> RHS by definition
9869 if (LHS == RHS)
9870 return LHSIsTrue;
9871
9872 // Match not
9873 bool InvertRHS = false;
9874 if (match(RHS, m_Not(m_Value(RHS)))) {
9875 if (LHS == RHS)
9876 return !LHSIsTrue;
9877 InvertRHS = true;
9878 }
9879
9880 CmpPredicate RHSPred;
9881 Value *RHSOp0, *RHSOp1;
9882 if (match(RHS, m_ICmpLike(RHSPred, m_Value(RHSOp0), m_Value(RHSOp1)))) {
9883 if (auto Implied = isImpliedCondition(LHS, RHSPred, RHSOp0, RHSOp1, DL,
9884 LHSIsTrue, Depth))
9885 return InvertRHS ? !*Implied : *Implied;
9886 return std::nullopt;
9887 }
9888 if (const FCmpInst *RHSCmp = dyn_cast<FCmpInst>(RHS)) {
9889 if (auto Implied = isImpliedCondition(
9890 LHS, RHSCmp->getPredicate(), RHSCmp->getOperand(0),
9891 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))
9892 return InvertRHS ? !*Implied : *Implied;
9893 return std::nullopt;
9894 }
9895
9897 return std::nullopt;
9898
9899 // LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS2
9900 // LHS ==> !(RHS1 && RHS2) if LHS ==> !RHS1 or LHS ==> !RHS2
9901 const Value *RHS1, *RHS2;
9902 if (match(RHS, m_LogicalOr(m_Value(RHS1), m_Value(RHS2)))) {
9903 if (std::optional<bool> Imp =
9904 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9905 if (*Imp == true)
9906 return !InvertRHS;
9907 if (std::optional<bool> Imp =
9908 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9909 if (*Imp == true)
9910 return !InvertRHS;
9911 }
9912 if (match(RHS, m_LogicalAnd(m_Value(RHS1), m_Value(RHS2)))) {
9913 if (std::optional<bool> Imp =
9914 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9915 if (*Imp == false)
9916 return InvertRHS;
9917 if (std::optional<bool> Imp =
9918 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9919 if (*Imp == false)
9920 return InvertRHS;
9921 }
9922
9923 return std::nullopt;
9924}
9925
9926// Returns a pair (Condition, ConditionIsTrue), where Condition is a branch
9927// condition dominating ContextI or nullptr, if no condition is found.
9928static std::pair<Value *, bool>
9930 if (!ContextI || !ContextI->getParent())
9931 return {nullptr, false};
9932
9933 // TODO: This is a poor/cheap way to determine dominance. Should we use a
9934 // dominator tree (eg, from a SimplifyQuery) instead?
9935 const BasicBlock *ContextBB = ContextI->getParent();
9936 const BasicBlock *PredBB = ContextBB->getSinglePredecessor();
9937 if (!PredBB)
9938 return {nullptr, false};
9939
9940 // We need a conditional branch in the predecessor.
9941 Value *PredCond;
9942 BasicBlock *TrueBB, *FalseBB;
9943 if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))
9944 return {nullptr, false};
9945
9946 // The branch should get simplified. Don't bother simplifying this condition.
9947 if (TrueBB == FalseBB)
9948 return {nullptr, false};
9949
9950 assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
9951 "Predecessor block does not point to successor?");
9952
9953 // Is this condition implied by the predecessor condition?
9954 return {PredCond, TrueBB == ContextBB};
9955}
9956
9957std::optional<bool> llvm::isImpliedByDomCondition(const Value *Cond,
9958 const Instruction *ContextI,
9959 const DataLayout &DL) {
9960 assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool");
9961 auto PredCond = getDomPredecessorCondition(ContextI);
9962 if (PredCond.first)
9963 return isImpliedCondition(PredCond.first, Cond, DL, PredCond.second);
9964 return std::nullopt;
9965}
9966
9968 const Value *LHS,
9969 const Value *RHS,
9970 const Instruction *ContextI,
9971 const DataLayout &DL) {
9972 auto PredCond = getDomPredecessorCondition(ContextI);
9973 if (PredCond.first)
9974 return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL,
9975 PredCond.second);
9976 return std::nullopt;
9977}
9978
9980 APInt &Upper, const InstrInfoQuery &IIQ,
9981 bool PreferSignedRange) {
9982 unsigned Width = Lower.getBitWidth();
9983 const APInt *C;
9984 switch (BO.getOpcode()) {
9985 case Instruction::Sub:
9986 if (match(BO.getOperand(0), m_APInt(C))) {
9987 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
9988 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
9989
9990 // If the caller expects a signed compare, then try to use a signed range.
9991 // Otherwise if both no-wraps are set, use the unsigned range because it
9992 // is never larger than the signed range. Example:
9993 // "sub nuw nsw i8 -2, x" is unsigned [0, 254] vs. signed [-128, 126].
9994 // "sub nuw nsw i8 2, x" is unsigned [0, 2] vs. signed [-125, 127].
9995 if (PreferSignedRange && HasNSW && HasNUW)
9996 HasNUW = false;
9997
9998 if (HasNUW) {
9999 // 'sub nuw c, x' produces [0, C].
10000 Upper = *C + 1;
10001 } else if (HasNSW) {
10002 if (C->isNegative()) {
10003 // 'sub nsw -C, x' produces [SINT_MIN, -C - SINT_MIN].
10005 Upper = *C - APInt::getSignedMaxValue(Width);
10006 } else {
10007 // Note that sub 0, INT_MIN is not NSW. It techically is a signed wrap
10008 // 'sub nsw C, x' produces [C - SINT_MAX, SINT_MAX].
10009 Lower = *C - APInt::getSignedMaxValue(Width);
10011 }
10012 }
10013 }
10014 break;
10015 case Instruction::Add:
10016 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
10017 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
10018 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
10019
10020 // If the caller expects a signed compare, then try to use a signed
10021 // range. Otherwise if both no-wraps are set, use the unsigned range
10022 // because it is never larger than the signed range. Example: "add nuw
10023 // nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125].
10024 if (PreferSignedRange && HasNSW && HasNUW)
10025 HasNUW = false;
10026
10027 if (HasNUW) {
10028 // 'add nuw x, C' produces [C, UINT_MAX].
10029 Lower = *C;
10030 } else if (HasNSW) {
10031 if (C->isNegative()) {
10032 // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C].
10034 Upper = APInt::getSignedMaxValue(Width) + *C + 1;
10035 } else {
10036 // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX].
10037 Lower = APInt::getSignedMinValue(Width) + *C;
10038 Upper = APInt::getSignedMaxValue(Width) + 1;
10039 }
10040 }
10041 }
10042 break;
10043
10044 case Instruction::And:
10045 if (match(BO.getOperand(1), m_APInt(C)))
10046 // 'and x, C' produces [0, C].
10047 Upper = *C + 1;
10048 // X & -X is a power of two or zero. So we can cap the value at max power of
10049 // two.
10050 if (match(BO.getOperand(0), m_Neg(m_Specific(BO.getOperand(1)))) ||
10051 match(BO.getOperand(1), m_Neg(m_Specific(BO.getOperand(0)))))
10052 Upper = APInt::getSignedMinValue(Width) + 1;
10053 break;
10054
10055 case Instruction::Or:
10056 if (match(BO.getOperand(1), m_APInt(C)))
10057 // 'or x, C' produces [C, UINT_MAX].
10058 Lower = *C;
10059 break;
10060
10061 case Instruction::AShr:
10062 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10063 // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C].
10065 Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1;
10066 } else if (match(BO.getOperand(0), m_APInt(C))) {
10067 unsigned ShiftAmount = Width - 1;
10068 if (!C->isZero() && IIQ.isExact(&BO))
10069 ShiftAmount = C->countr_zero();
10070 if (C->isNegative()) {
10071 // 'ashr C, x' produces [C, C >> (Width-1)]
10072 Lower = *C;
10073 Upper = C->ashr(ShiftAmount) + 1;
10074 } else {
10075 // 'ashr C, x' produces [C >> (Width-1), C]
10076 Lower = C->ashr(ShiftAmount);
10077 Upper = *C + 1;
10078 }
10079 }
10080 break;
10081
10082 case Instruction::LShr:
10083 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10084 // 'lshr x, C' produces [0, UINT_MAX >> C].
10085 Upper = APInt::getAllOnes(Width).lshr(*C) + 1;
10086 } else if (match(BO.getOperand(0), m_APInt(C))) {
10087 // 'lshr C, x' produces [C >> (Width-1), C].
10088 unsigned ShiftAmount = Width - 1;
10089 if (!C->isZero() && IIQ.isExact(&BO))
10090 ShiftAmount = C->countr_zero();
10091 Lower = C->lshr(ShiftAmount);
10092 Upper = *C + 1;
10093 }
10094 break;
10095
10096 case Instruction::Shl:
10097 if (match(BO.getOperand(0), m_APInt(C))) {
10098 if (IIQ.hasNoUnsignedWrap(&BO)) {
10099 // 'shl nuw C, x' produces [C, C << CLZ(C)]
10100 Lower = *C;
10101 Upper = Lower.shl(Lower.countl_zero()) + 1;
10102 } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw?
10103 if (C->isNegative()) {
10104 // 'shl nsw C, x' produces [C << CLO(C)-1, C]
10105 unsigned ShiftAmount = C->countl_one() - 1;
10106 Lower = C->shl(ShiftAmount);
10107 Upper = *C + 1;
10108 } else {
10109 // 'shl nsw C, x' produces [C, C << CLZ(C)-1]
10110 unsigned ShiftAmount = C->countl_zero() - 1;
10111 Lower = *C;
10112 Upper = C->shl(ShiftAmount) + 1;
10113 }
10114 } else {
10115 // If lowbit is set, value can never be zero.
10116 if ((*C)[0])
10117 Lower = APInt::getOneBitSet(Width, 0);
10118 // If we are shifting a constant the largest it can be is if the longest
10119 // sequence of consecutive ones is shifted to the highbits (breaking
10120 // ties for which sequence is higher). At the moment we take a liberal
10121 // upper bound on this by just popcounting the constant.
10122 // TODO: There may be a bitwise trick for it longest/highest
10123 // consecutative sequence of ones (naive method is O(Width) loop).
10124 Upper = APInt::getHighBitsSet(Width, C->popcount()) + 1;
10125 }
10126 } else if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10127 Upper = APInt::getBitsSetFrom(Width, C->getZExtValue()) + 1;
10128 }
10129 break;
10130
10131 case Instruction::SDiv:
10132 if (match(BO.getOperand(1), m_APInt(C))) {
10133 APInt IntMin = APInt::getSignedMinValue(Width);
10134 APInt IntMax = APInt::getSignedMaxValue(Width);
10135 if (C->isAllOnes()) {
10136 // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX]
10137 // where C != -1 and C != 0 and C != 1
10138 Lower = IntMin + 1;
10139 Upper = IntMax + 1;
10140 } else if (C->countl_zero() < Width - 1) {
10141 // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C]
10142 // where C != -1 and C != 0 and C != 1
10143 Lower = IntMin.sdiv(*C);
10144 Upper = IntMax.sdiv(*C);
10145 if (Lower.sgt(Upper))
10147 Upper = Upper + 1;
10148 assert(Upper != Lower && "Upper part of range has wrapped!");
10149 }
10150 } else if (match(BO.getOperand(0), m_APInt(C))) {
10151 if (C->isMinSignedValue()) {
10152 // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2].
10153 Lower = *C;
10154 Upper = Lower.lshr(1) + 1;
10155 } else {
10156 // 'sdiv C, x' produces [-|C|, |C|].
10157 Upper = C->abs() + 1;
10158 Lower = (-Upper) + 1;
10159 }
10160 }
10161 break;
10162
10163 case Instruction::UDiv:
10164 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
10165 // 'udiv x, C' produces [0, UINT_MAX / C].
10166 Upper = APInt::getMaxValue(Width).udiv(*C) + 1;
10167 } else if (match(BO.getOperand(0), m_APInt(C))) {
10168 // 'udiv C, x' produces [0, C].
10169 Upper = *C + 1;
10170 }
10171 break;
10172
10173 case Instruction::SRem:
10174 if (match(BO.getOperand(1), m_APInt(C))) {
10175 // 'srem x, C' produces (-|C|, |C|).
10176 Upper = C->abs();
10177 Lower = (-Upper) + 1;
10178 } else if (match(BO.getOperand(0), m_APInt(C))) {
10179 if (C->isNegative()) {
10180 // 'srem -|C|, x' produces [-|C|, 0].
10181 Upper = 1;
10182 Lower = *C;
10183 } else {
10184 // 'srem |C|, x' produces [0, |C|].
10185 Upper = *C + 1;
10186 }
10187 }
10188 break;
10189
10190 case Instruction::URem:
10191 if (match(BO.getOperand(1), m_APInt(C)))
10192 // 'urem x, C' produces [0, C).
10193 Upper = *C;
10194 else if (match(BO.getOperand(0), m_APInt(C)))
10195 // 'urem C, x' produces [0, C].
10196 Upper = *C + 1;
10197 break;
10198
10199 default:
10200 break;
10201 }
10202}
10203
10205 bool UseInstrInfo) {
10206 unsigned Width = II.getType()->getScalarSizeInBits();
10207 const APInt *C;
10208 switch (II.getIntrinsicID()) {
10209 case Intrinsic::ctlz:
10210 case Intrinsic::cttz: {
10211 APInt Upper(Width, Width);
10212 if (!UseInstrInfo || !match(II.getArgOperand(1), m_One()))
10213 Upper += 1;
10214 // Maximum of set/clear bits is the bit width.
10216 }
10217 case Intrinsic::ctpop:
10218 // Maximum of set/clear bits is the bit width.
10220 APInt(Width, Width) + 1);
10221 case Intrinsic::uadd_sat:
10222 // uadd.sat(x, C) produces [C, UINT_MAX].
10223 if (match(II.getOperand(0), m_APInt(C)) ||
10224 match(II.getOperand(1), m_APInt(C)))
10226 break;
10227 case Intrinsic::sadd_sat:
10228 if (match(II.getOperand(0), m_APInt(C)) ||
10229 match(II.getOperand(1), m_APInt(C))) {
10230 if (C->isNegative())
10231 // sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)].
10233 APInt::getSignedMaxValue(Width) + *C +
10234 1);
10235
10236 // sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].
10238 APInt::getSignedMaxValue(Width) + 1);
10239 }
10240 break;
10241 case Intrinsic::usub_sat:
10242 // usub.sat(C, x) produces [0, C].
10243 if (match(II.getOperand(0), m_APInt(C)))
10244 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
10245
10246 // usub.sat(x, C) produces [0, UINT_MAX - C].
10247 if (match(II.getOperand(1), m_APInt(C)))
10249 APInt::getMaxValue(Width) - *C + 1);
10250 break;
10251 case Intrinsic::ssub_sat:
10252 if (match(II.getOperand(0), m_APInt(C))) {
10253 if (C->isNegative())
10254 // ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)].
10256 *C - APInt::getSignedMinValue(Width) +
10257 1);
10258
10259 // ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].
10261 APInt::getSignedMaxValue(Width) + 1);
10262 } else if (match(II.getOperand(1), m_APInt(C))) {
10263 if (C->isNegative())
10264 // ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]:
10266 APInt::getSignedMaxValue(Width) + 1);
10267
10268 // ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].
10270 APInt::getSignedMaxValue(Width) - *C +
10271 1);
10272 }
10273 break;
10274 case Intrinsic::umin:
10275 case Intrinsic::umax:
10276 case Intrinsic::smin:
10277 case Intrinsic::smax:
10278 if (!match(II.getOperand(0), m_APInt(C)) &&
10279 !match(II.getOperand(1), m_APInt(C)))
10280 break;
10281
10282 switch (II.getIntrinsicID()) {
10283 case Intrinsic::umin:
10284 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
10285 case Intrinsic::umax:
10287 case Intrinsic::smin:
10289 *C + 1);
10290 case Intrinsic::smax:
10292 APInt::getSignedMaxValue(Width) + 1);
10293 default:
10294 llvm_unreachable("Must be min/max intrinsic");
10295 }
10296 break;
10297 case Intrinsic::abs:
10298 // If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX],
10299 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10300 if (match(II.getOperand(1), m_One()))
10302 APInt::getSignedMaxValue(Width) + 1);
10303
10305 APInt::getSignedMinValue(Width) + 1);
10306 case Intrinsic::vscale:
10307 if (!II.getParent() || !II.getFunction())
10308 break;
10309 return getVScaleRange(II.getFunction(), Width);
10310 default:
10311 break;
10312 }
10313
10314 return ConstantRange::getFull(Width);
10315}
10316
10318 const InstrInfoQuery &IIQ) {
10319 unsigned BitWidth = SI.getType()->getScalarSizeInBits();
10320 const Value *LHS = nullptr, *RHS = nullptr;
10322 if (R.Flavor == SPF_UNKNOWN)
10323 return ConstantRange::getFull(BitWidth);
10324
10325 if (R.Flavor == SelectPatternFlavor::SPF_ABS) {
10326 // If the negation part of the abs (in RHS) has the NSW flag,
10327 // then the result of abs(X) is [0..SIGNED_MAX],
10328 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10329 if (match(RHS, m_Neg(m_Specific(LHS))) &&
10333
10336 }
10337
10338 if (R.Flavor == SelectPatternFlavor::SPF_NABS) {
10339 // The result of -abs(X) is <= 0.
10341 APInt(BitWidth, 1));
10342 }
10343
10344 const APInt *C;
10345 if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C)))
10346 return ConstantRange::getFull(BitWidth);
10347
10348 switch (R.Flavor) {
10349 case SPF_UMIN:
10351 case SPF_UMAX:
10353 case SPF_SMIN:
10355 *C + 1);
10356 case SPF_SMAX:
10359 default:
10360 return ConstantRange::getFull(BitWidth);
10361 }
10362}
10363
10365 // The maximum representable value of a half is 65504. For floats the maximum
10366 // value is 3.4e38 which requires roughly 129 bits.
10367 unsigned BitWidth = I->getType()->getScalarSizeInBits();
10368 if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy())
10369 return;
10370 if (isa<FPToSIInst>(I) && BitWidth >= 17) {
10371 Lower = APInt(BitWidth, -65504, true);
10372 Upper = APInt(BitWidth, 65505);
10373 }
10374
10375 if (isa<FPToUIInst>(I) && BitWidth >= 16) {
10376 // For a fptoui the lower limit is left as 0.
10377 Upper = APInt(BitWidth, 65505);
10378 }
10379}
10380
10382 const SimplifyQuery &SQ,
10383 unsigned Depth) {
10384 assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction");
10385
10387 return ConstantRange::getFull(V->getType()->getScalarSizeInBits());
10388
10389 if (auto *C = dyn_cast<Constant>(V))
10390 return C->toConstantRange();
10391
10392 unsigned BitWidth = V->getType()->getScalarSizeInBits();
10393 ConstantRange CR = ConstantRange::getFull(BitWidth);
10394 if (auto *BO = dyn_cast<BinaryOperator>(V)) {
10395 APInt Lower = APInt(BitWidth, 0);
10396 APInt Upper = APInt(BitWidth, 0);
10397 // TODO: Return ConstantRange.
10398 setLimitsForBinOp(*BO, Lower, Upper, SQ.IIQ, ForSigned);
10400 } else if (auto *II = dyn_cast<IntrinsicInst>(V))
10402 else if (auto *SI = dyn_cast<SelectInst>(V)) {
10403 ConstantRange CRTrue =
10404 computeConstantRange(SI->getTrueValue(), ForSigned, SQ, Depth + 1);
10405 ConstantRange CRFalse =
10406 computeConstantRange(SI->getFalseValue(), ForSigned, SQ, Depth + 1);
10407 CR = CRTrue.unionWith(CRFalse);
10409 } else if (auto *TI = dyn_cast<TruncInst>(V)) {
10410 ConstantRange SrcCR =
10411 computeConstantRange(TI->getOperand(0), ForSigned, SQ, Depth + 1);
10412 CR = SrcCR.truncate(BitWidth);
10413 } else if (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {
10414 APInt Lower = APInt(BitWidth, 0);
10415 APInt Upper = APInt(BitWidth, 0);
10416 // TODO: Return ConstantRange.
10419 } else if (const auto *A = dyn_cast<Argument>(V))
10420 if (std::optional<ConstantRange> Range = A->getRange())
10421 CR = *Range;
10422
10423 if (auto *I = dyn_cast<Instruction>(V)) {
10424 if (auto *Range = SQ.IIQ.getMetadata(I, LLVMContext::MD_range))
10426
10427 Value *FrexpSrc;
10428 if (const auto *CB = dyn_cast<CallBase>(V)) {
10429 if (std::optional<ConstantRange> Range = CB->getRange())
10430 CR = CR.intersectWith(*Range);
10432 m_Value(FrexpSrc))))) {
10433 const fltSemantics &FltSem =
10434 FrexpSrc->getType()->getScalarType()->getFltSemantics();
10435 // It should be possible to implement this for any type, but this logic
10436 // only computes the range assuming standard subnormal handling.
10437 if (APFloat::isIEEELikeFP(FltSem)) {
10438 KnownFPClass KnownSrc =
10439 computeKnownFPClass(FrexpSrc, fcSubnormal, SQ, Depth + 1);
10440
10441 // Exponent result is (src == 0) ? 0 : ilogb(src) + 1, and unspecified
10442 // for inf/nan.
10443 int MinExp = APFloat::semanticsMinExponent(FltSem) + 1;
10444
10445 // Offset to find the true minimum exponent value for a denormal.
10446 if (!KnownSrc.isKnownNeverSubnormal())
10447 MinExp -= (APFloat::semanticsPrecision(FltSem) - 1);
10448
10449 int MaxExp = APFloat::semanticsMaxExponent(FltSem) + 1;
10451 APInt(BitWidth, MinExp, /*isSigned=*/true),
10452 APInt(BitWidth, MaxExp + 1, /*isSigned=*/true));
10453 }
10454 }
10455 }
10456
10457 if (SQ.CxtI && SQ.AC) {
10458 // Try to restrict the range based on information from assumptions.
10459 for (auto &AssumeVH : SQ.AC->assumptionsFor(V)) {
10460 if (!AssumeVH)
10461 continue;
10462 CallInst *I = cast<CallInst>(AssumeVH);
10463 assert(I->getParent()->getParent() == SQ.CxtI->getParent()->getParent() &&
10464 "Got assumption for the wrong function!");
10465 assert(I->getIntrinsicID() == Intrinsic::assume &&
10466 "must be an assume intrinsic");
10467
10468 if (!isValidAssumeForContext(I, SQ))
10469 continue;
10470 Value *Arg = I->getArgOperand(0);
10471 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
10472 // Currently we just use information from comparisons.
10473 if (!Cmp || Cmp->getOperand(0) != V)
10474 continue;
10475 // TODO: Set "ForSigned" parameter via Cmp->isSigned()?
10476 ConstantRange RHS =
10477 computeConstantRange(Cmp->getOperand(1), /*ForSigned=*/false,
10478 SQ.getWithInstruction(I), Depth + 1);
10479 CR = CR.intersectWith(
10480 ConstantRange::makeAllowedICmpRegion(Cmp->getCmpPredicate(), RHS));
10481 }
10482 }
10483
10484 return CR;
10485}
10486
10487static void
10489 function_ref<void(Value *)> InsertAffected) {
10490 assert(V != nullptr);
10491 if (isa<Argument>(V) || isa<GlobalValue>(V)) {
10492 InsertAffected(V);
10493 } else if (auto *I = dyn_cast<Instruction>(V)) {
10494 InsertAffected(V);
10495
10496 // Peek through unary operators to find the source of the condition.
10497 Value *Op;
10499 m_Trunc(m_Value(Op))))) {
10501 InsertAffected(Op);
10502 }
10503 }
10504}
10505
10507 Value *Cond, bool IsAssume, function_ref<void(Value *)> InsertAffected) {
10508 auto AddAffected = [&InsertAffected](Value *V) {
10509 addValueAffectedByCondition(V, InsertAffected);
10510 };
10511
10512 auto AddCmpOperands = [&AddAffected, IsAssume](Value *LHS, Value *RHS) {
10513 if (IsAssume) {
10514 AddAffected(LHS);
10515 AddAffected(RHS);
10516 } else if (match(RHS, m_Constant()))
10517 AddAffected(LHS);
10518 };
10519
10520 SmallVector<Value *, 8> Worklist;
10522 Worklist.push_back(Cond);
10523 while (!Worklist.empty()) {
10524 Value *V = Worklist.pop_back_val();
10525 if (!Visited.insert(V).second)
10526 continue;
10527
10528 CmpPredicate Pred;
10529 Value *A, *B, *X;
10530
10531 if (IsAssume) {
10532 AddAffected(V);
10533 if (match(V, m_Not(m_Value(X))))
10534 AddAffected(X);
10535 }
10536
10537 if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
10538 // assume(A && B) is split to -> assume(A); assume(B);
10539 // assume(!(A || B)) is split to -> assume(!A); assume(!B);
10540 // Finally, assume(A || B) / assume(!(A && B)) generally don't provide
10541 // enough information to be worth handling (intersection of information as
10542 // opposed to union).
10543 if (!IsAssume) {
10544 Worklist.push_back(A);
10545 Worklist.push_back(B);
10546 }
10547 } else if (match(V, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
10548 bool HasRHSC = match(B, m_ConstantInt());
10549 if (ICmpInst::isEquality(Pred)) {
10550 AddAffected(A);
10551 if (IsAssume)
10552 AddAffected(B);
10553 if (HasRHSC) {
10554 Value *Y;
10555 // (X << C) or (X >>_s C) or (X >>_u C).
10556 if (match(A, m_Shift(m_Value(X), m_ConstantInt())))
10557 AddAffected(X);
10558 // (X & C) or (X | C).
10559 else if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10560 match(A, m_Or(m_Value(X), m_Value(Y)))) {
10561 AddAffected(X);
10562 AddAffected(Y);
10563 }
10564 // X - Y
10565 else if (match(A, m_Sub(m_Value(X), m_Value(Y)))) {
10566 AddAffected(X);
10567 AddAffected(Y);
10568 }
10569 }
10570 } else {
10571 AddCmpOperands(A, B);
10572 if (HasRHSC) {
10573 // Handle (A + C1) u< C2, which is the canonical form of
10574 // A > C3 && A < C4.
10576 AddAffected(X);
10577
10578 if (ICmpInst::isUnsigned(Pred)) {
10579 Value *Y;
10580 // X & Y u> C -> X >u C && Y >u C
10581 // X | Y u< C -> X u< C && Y u< C
10582 // X nuw+ Y u< C -> X u< C && Y u< C
10583 if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10584 match(A, m_Or(m_Value(X), m_Value(Y))) ||
10585 match(A, m_NUWAdd(m_Value(X), m_Value(Y)))) {
10586 AddAffected(X);
10587 AddAffected(Y);
10588 }
10589 // X nuw- Y u> C -> X u> C
10590 if (match(A, m_NUWSub(m_Value(X), m_Value())))
10591 AddAffected(X);
10592 }
10593 }
10594
10595 // Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported
10596 // by computeKnownFPClass().
10598 if (Pred == ICmpInst::ICMP_SLT && match(B, m_Zero()))
10599 InsertAffected(X);
10600 else if (Pred == ICmpInst::ICMP_SGT && match(B, m_AllOnes()))
10601 InsertAffected(X);
10602 }
10603 }
10604
10605 if (HasRHSC && match(A, m_Ctpop(m_Value(X))))
10606 AddAffected(X);
10607 } else if (match(V, m_FCmp(Pred, m_Value(A), m_Value(B)))) {
10608 AddCmpOperands(A, B);
10609
10610 // fcmp fneg(x), y
10611 // fcmp fabs(x), y
10612 // fcmp fneg(fabs(x)), y
10613 if (match(A, m_FNeg(m_Value(A))))
10614 AddAffected(A);
10615 if (match(A, m_FAbs(m_Value(A))))
10616 AddAffected(A);
10617
10619 m_Value()))) {
10620 // Handle patterns that computeKnownFPClass() support.
10621 AddAffected(A);
10622 } else if (!IsAssume && match(V, m_Trunc(m_Value(X)))) {
10623 // Assume is checked here as X is already added above for assumes in
10624 // addValueAffectedByCondition
10625 AddAffected(X);
10626 } else if (!IsAssume && match(V, m_Not(m_Value(X)))) {
10627 // Assume is checked here to avoid issues with ephemeral values
10628 Worklist.push_back(X);
10629 }
10630 }
10631}
10632
10634 // (X >> C) or/add (X & mask(C) != 0)
10635 if (const auto *BO = dyn_cast<BinaryOperator>(V)) {
10636 if (BO->getOpcode() == Instruction::Add ||
10637 BO->getOpcode() == Instruction::Or) {
10638 const Value *X;
10639 const APInt *C1, *C2;
10640 if (match(BO, m_c_BinOp(m_LShr(m_Value(X), m_APInt(C1)),
10644 m_Zero())))) &&
10645 C2->popcount() == C1->getZExtValue())
10646 return X;
10647 }
10648 }
10649 return nullptr;
10650}
10651
10653 return const_cast<Value *>(stripNullTest(const_cast<const Value *>(V)));
10654}
10655
10658 unsigned MaxCount, bool AllowUndefOrPoison) {
10661 auto Push = [&](const Value *V) -> bool {
10662 Constant *C;
10663 if (match(const_cast<Value *>(V), m_ImmConstant(C))) {
10664 if (!AllowUndefOrPoison && !isGuaranteedNotToBeUndefOrPoison(C))
10665 return false;
10666 // Check existence first to avoid unnecessary allocations.
10667 if (Constants.contains(C))
10668 return true;
10669 if (Constants.size() == MaxCount)
10670 return false;
10671 Constants.insert(C);
10672 return true;
10673 }
10674
10675 if (auto *Inst = dyn_cast<Instruction>(V)) {
10676 if (Visited.insert(Inst).second)
10677 Worklist.push_back(Inst);
10678 return true;
10679 }
10680 return false;
10681 };
10682 if (!Push(V))
10683 return false;
10684 while (!Worklist.empty()) {
10685 const Instruction *CurInst = Worklist.pop_back_val();
10686 switch (CurInst->getOpcode()) {
10687 case Instruction::Select:
10688 if (!Push(CurInst->getOperand(1)))
10689 return false;
10690 if (!Push(CurInst->getOperand(2)))
10691 return false;
10692 break;
10693 case Instruction::PHI:
10694 for (Value *IncomingValue : cast<PHINode>(CurInst)->incoming_values()) {
10695 // Fast path for recurrence PHI.
10696 if (IncomingValue == CurInst)
10697 continue;
10698 if (!Push(IncomingValue))
10699 return false;
10700 }
10701 break;
10702 default:
10703 return false;
10704 }
10705 }
10706 return true;
10707}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Register Bank Select
Rewrite undef for PHI
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis Results
This file contains the simple types necessary to represent the attributes associated with functions a...
static const Function * getParent(const Value *V)
#define X(NUM, ENUM, NAME)
Definition ELF.h:851
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Utilities for dealing with flags related to floating point properties and mode controls.
static Value * getCondition(Instruction *I)
Hexagon Common GEP
Module.h This file contains the declarations for the Module class.
static bool hasNoUnsignedWrap(BinaryOperator &I)
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
This file contains the declarations for metadata subclasses.
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
#define P(N)
PowerPC Reduce CR logical Operation
R600 Clause Merge
const SmallVectorImpl< MachineOperand > & Cond
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
std::pair< BasicBlock *, BasicBlock * > Edge
This file contains some templates that are useful if you are working with the STL at all.
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
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:222
static LLVM_ABI ExponentType semanticsMaxExponent(const fltSemantics &)
Definition APFloat.cpp:218
static LLVM_ABI unsigned int semanticsPrecision(const fltSemantics &)
Definition APFloat.cpp:214
static LLVM_ABI bool isIEEELikeFP(const fltSemantics &)
Definition APFloat.cpp:255
bool isFinite() const
Definition APFloat.h: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:2022
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1615
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:1686
void clearAllBits()
Set every bit to 0.
Definition APInt.h:1419
LLVM_ABI APInt reverseBits() const
Definition APInt.cpp:789
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:1083
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:39
iterator end() const
Definition ArrayRef.h:129
size_t size() const
Get the array size.
Definition ArrayRef.h:140
iterator begin() const
Definition ArrayRef.h:128
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:135
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:184
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:55
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:590
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:3060
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 const Value * getArgumentAliasingToReturnedPointer(const CallBase *Call, bool MustPreserveNullness)
This function returns call pointer argument that is considered the same by aliasing rules.
LLVM_ABI bool isAssumeLikeIntrinsic(const Instruction *I)
Return true if it is an intrinsic that cannot be speculated but also cannot trap.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h: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:229
LLVM_ABI bool onlyUsedByLifetimeMarkersOrDroppableInsts(const Value *V)
Return true if the only users of this pointer are lifetime markers or droppable instructions.
LLVM_ABI Constant * ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset)
LLVM_ABI Value * stripNullTest(Value *V)
Returns the inner value X if the expression has the form f(X) where f(X) == 0 if and only if X == 0,...
LLVM_ABI bool getUnderlyingObjectsForCodeGen(const Value *V, SmallVectorImpl< Value * > &Objects)
This is a wrapper around getUnderlyingObjects and adds support for basic ptrtoint+arithmetic+inttoptr...
LLVM_ABI std::pair< Intrinsic::ID, bool > canConvertToMinOrMaxIntrinsic(ArrayRef< Value * > VL)
Check if the values in VL are select instructions that can be converted to a min or max (vector) intr...
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ABI bool getConstantDataArrayInfo(const Value *V, ConstantDataArraySlice &Slice, unsigned ElementSize, uint64_t Offset=0)
Returns true if the value V is a pointer into a ConstantDataArray.
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition bit.h:325
LLVM_ABI bool isGuaranteedToExecuteForEveryIteration(const Instruction *I, const Loop *L)
Return true if this function can prove that the instruction I is executed for every iteration of the ...
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2207
LLVM_ABI bool mustSuppressSpeculation(const LoadInst &LI)
Return true if speculation of the given load must be suppressed to avoid ordering or interfering with...
Definition Loads.cpp:431
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition MathExtras.h:284
gep_type_iterator gep_type_end(const User *GEP)
int ilogb(const APFloat &Arg)
Returns the exponent of the internal representation of the APFloat.
Definition APFloat.h: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 isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(const CallBase *Call, bool MustPreserveNullness)
{launder,strip}.invariant.group returns pointer that aliases its argument, and it only captures point...
LLVM_ABI bool impliesPoison(const Value *ValAssumedPoison, const Value *V)
Return true if V is poison given that ValAssumedPoison is already poison.
LLVM_ABI void getHorizDemandedEltsForFirstOperand(unsigned VectorBitWidth, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS)
Compute the demanded elements mask of horizontal binary operations.
LLVM_ABI SelectPatternResult getSelectPattern(CmpInst::Predicate Pred, SelectPatternNaNBehavior NaNBehavior=SPNB_NA, bool Ordered=false)
Determine the pattern for predicate X Pred Y ? X : Y.
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI bool programUndefinedIfPoison(const Instruction *Inst)
LLVM_ABI SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
LLVM_ABI bool matchSimpleBinaryIntrinsicRecurrence(const IntrinsicInst *I, PHINode *&P, Value *&Init, Value *&OtherOp)
Attempt to match a simple value-accumulating recurrence of the form: llvm.intrinsic....
LLVM_ABI bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
LLVM_ABI bool cannotBeNegativeZero(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if we can prove that the specified FP value is never equal to -0.0.
LLVM_ABI bool programUndefinedIfUndefOrPoison(const Instruction *Inst)
Return true if this function can prove that if Inst is executed and yields a poison value or undef bi...
LLVM_ABI void adjustKnownFPClassForSelectArm(KnownFPClass &Known, Value *Cond, Value *Arm, bool Invert, const SimplifyQuery &Q, unsigned Depth=0)
Adjust Known for the given select Arm to include information from the select Cond.
generic_gep_type_iterator<> gep_type_iterator
LLVM_ABI bool collectPossibleValues(const Value *V, SmallPtrSetImpl< const Constant * > &Constants, unsigned MaxCount, bool AllowUndefOrPoison=true)
Enumerates all possible immediate values of V and inserts them into the set Constants.
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
LLVM_ABI uint64_t GetStringLength(const Value *V, unsigned CharSize=8)
If we can compute the length of the string pointed to by the specified pointer, return 'len+1'.
LLVM_ABI OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
LLVM_ABI ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
LLVM_ABI Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
LLVM_ABI bool canCreateUndefOrPoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
canCreateUndefOrPoison returns true if Op can create undef or poison from non-undef & non-poison oper...
LLVM_ABI bool matchSimpleTernaryIntrinsicRecurrence(const IntrinsicInst *I, PHINode *&P, Value *&Init, Value *&OtherOp0, Value *&OtherOp1)
Attempt to match a simple value-accumulating recurrence of the form: llvm.intrinsic....
LLVM_ABI EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
LLVM_ABI bool isKnownInversion(const Value *X, const Value *Y)
Return true iff:
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI bool intrinsicPropagatesPoison(Intrinsic::ID IID)
Return whether this intrinsic propagates poison for all operands.
LLVM_ABI bool isNotCrossLaneOperation(const Instruction *I)
Return true if the instruction doesn't potentially cross vector lanes.
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