LLVM 23.0.0git
ValueTracking.cpp
Go to the documentation of this file.
1//===- ValueTracking.cpp - Walk computations to compute properties --------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains routines that help analyze properties that chains of
10// computations have.
11//
12//===----------------------------------------------------------------------===//
13
15#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/ScopeExit.h"
23#include "llvm/ADT/StringRef.h"
33#include "llvm/Analysis/Loads.h"
38#include "llvm/IR/Argument.h"
39#include "llvm/IR/Attributes.h"
40#include "llvm/IR/BasicBlock.h"
41#include "llvm/IR/Constant.h"
44#include "llvm/IR/Constants.h"
47#include "llvm/IR/Dominators.h"
49#include "llvm/IR/Function.h"
51#include "llvm/IR/GlobalAlias.h"
52#include "llvm/IR/GlobalValue.h"
54#include "llvm/IR/InstrTypes.h"
55#include "llvm/IR/Instruction.h"
58#include "llvm/IR/Intrinsics.h"
59#include "llvm/IR/IntrinsicsAArch64.h"
60#include "llvm/IR/IntrinsicsAMDGPU.h"
61#include "llvm/IR/IntrinsicsRISCV.h"
62#include "llvm/IR/IntrinsicsX86.h"
63#include "llvm/IR/LLVMContext.h"
64#include "llvm/IR/Metadata.h"
65#include "llvm/IR/Module.h"
66#include "llvm/IR/Operator.h"
68#include "llvm/IR/Type.h"
69#include "llvm/IR/User.h"
70#include "llvm/IR/Value.h"
79#include <algorithm>
80#include <cassert>
81#include <cstdint>
82#include <optional>
83#include <utility>
84
85using namespace llvm;
86using namespace llvm::PatternMatch;
87
88// Controls the number of uses of the value searched for possible
89// dominating comparisons.
90static cl::opt<unsigned> DomConditionsMaxUses("dom-conditions-max-uses",
91 cl::Hidden, cl::init(20));
92
93/// Maximum number of instructions to check between assume and context
94/// instruction.
95static constexpr unsigned MaxInstrsToCheckForFree = 16;
96
97/// Returns the bitwidth of the given scalar or pointer type. For vector types,
98/// returns the element type's bitwidth.
99static unsigned getBitWidth(Type *Ty, const DataLayout &DL) {
100 if (unsigned BitWidth = Ty->getScalarSizeInBits())
101 return BitWidth;
102
103 return DL.getPointerTypeSizeInBits(Ty);
104}
105
106// Given the provided Value and, potentially, a context instruction, return
107// the preferred context instruction (if any).
108static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI) {
109 // If we've been provided with a context instruction, then use that (provided
110 // it has been inserted).
111 if (CxtI && CxtI->getParent())
112 return CxtI;
113
114 // If the value is really an already-inserted instruction, then use that.
115 CxtI = dyn_cast<Instruction>(V);
116 if (CxtI && CxtI->getParent())
117 return CxtI;
118
119 return nullptr;
120}
121
123 const APInt &DemandedElts,
124 APInt &DemandedLHS, APInt &DemandedRHS) {
125 if (isa<ScalableVectorType>(Shuf->getType())) {
126 assert(DemandedElts == APInt(1,1));
127 DemandedLHS = DemandedRHS = DemandedElts;
128 return true;
129 }
130
131 int NumElts =
132 cast<FixedVectorType>(Shuf->getOperand(0)->getType())->getNumElements();
133 return llvm::getShuffleDemandedElts(NumElts, Shuf->getShuffleMask(),
134 DemandedElts, DemandedLHS, DemandedRHS);
135}
136
137static void computeKnownBits(const Value *V, const APInt &DemandedElts,
138 KnownBits &Known, const SimplifyQuery &Q,
139 unsigned Depth);
140
142 const SimplifyQuery &Q, unsigned Depth) {
143 // Since the number of lanes in a scalable vector is unknown at compile time,
144 // we track one bit which is implicitly broadcast to all lanes. This means
145 // that all lanes in a scalable vector are considered demanded.
146 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
147 APInt DemandedElts =
148 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
149 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);
150}
151
153 const DataLayout &DL, AssumptionCache *AC,
154 const Instruction *CxtI, const DominatorTree *DT,
155 bool UseInstrInfo, unsigned Depth) {
156 computeKnownBits(V, Known,
157 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo),
158 Depth);
159}
160
162 AssumptionCache *AC, const Instruction *CxtI,
163 const DominatorTree *DT, bool UseInstrInfo,
164 unsigned Depth) {
165 return computeKnownBits(
166 V, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
167}
168
169KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
170 const DataLayout &DL, AssumptionCache *AC,
171 const Instruction *CxtI,
172 const DominatorTree *DT, bool UseInstrInfo,
173 unsigned Depth) {
174 return computeKnownBits(
175 V, DemandedElts,
176 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
177}
178
180 const SimplifyQuery &SQ) {
181 // Look for an inverted mask: (X & ~M) op (Y & M).
182 {
183 Value *M;
184 if (match(LHS, m_c_And(m_Not(m_Value(M)), m_Value())) &&
186 isGuaranteedNotToBeUndef(M, SQ.AC, SQ.CxtI, SQ.DT))
187 return true;
188 }
189
190 // X op (Y & ~X)
193 return true;
194
195 // X op ((X & Y) ^ Y) -- this is the canonical form of the previous pattern
196 // for constant Y.
197 Value *Y;
198 if (match(RHS,
200 isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT) &&
201 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
202 return true;
203
204 // Peek through extends to find a 'not' of the other side:
205 // (ext Y) op ext(~Y)
206 if (match(LHS, m_ZExtOrSExt(m_Value(Y))) &&
208 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
209 return true;
210
211 // Look for: (A & B) op ~(A | B)
212 {
213 Value *A, *B;
214 if (match(LHS, m_And(m_Value(A), m_Value(B))) &&
216 isGuaranteedNotToBeUndef(A, SQ.AC, SQ.CxtI, SQ.DT) &&
217 isGuaranteedNotToBeUndef(B, SQ.AC, SQ.CxtI, SQ.DT))
218 return true;
219 }
220
221 // Look for: (X << V) op (Y >> (BitWidth - V))
222 // or (X >> V) op (Y << (BitWidth - V))
223 {
224 const Value *V;
225 const APInt *R;
226 if (((match(RHS, m_Shl(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&
227 match(LHS, m_LShr(m_Value(), m_Specific(V)))) ||
228 (match(RHS, m_LShr(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&
229 match(LHS, m_Shl(m_Value(), m_Specific(V))))) &&
230 R->uge(LHS->getType()->getScalarSizeInBits()))
231 return true;
232 }
233
234 return false;
235}
236
238 const WithCache<const Value *> &RHSCache,
239 const SimplifyQuery &SQ) {
240 const Value *LHS = LHSCache.getValue();
241 const Value *RHS = RHSCache.getValue();
242
243 assert(LHS->getType() == RHS->getType() &&
244 "LHS and RHS should have the same type");
245 assert(LHS->getType()->isIntOrIntVectorTy() &&
246 "LHS and RHS should be integers");
247
248 if (haveNoCommonBitsSetSpecialCases(LHS, RHS, SQ) ||
250 return true;
251
253 RHSCache.getKnownBits(SQ));
254}
255
257 return !I->user_empty() &&
258 all_of(I->users(), match_fn(m_ICmp(m_Value(), m_Zero())));
259}
260
262 return !I->user_empty() && all_of(I->users(), [](const User *U) {
263 CmpPredicate P;
264 return match(U, m_ICmp(P, m_Value(), m_Zero())) && ICmpInst::isEquality(P);
265 });
266}
267
269 bool OrZero, AssumptionCache *AC,
270 const Instruction *CxtI,
271 const DominatorTree *DT, bool UseInstrInfo,
272 unsigned Depth) {
273 return ::isKnownToBeAPowerOfTwo(
274 V, OrZero, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo),
275 Depth);
276}
277
278static bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
279 const SimplifyQuery &Q, unsigned Depth);
280
282 unsigned Depth) {
283 return computeKnownBits(V, SQ, Depth).isNonNegative();
284}
285
287 unsigned Depth) {
288 if (auto *CI = dyn_cast<ConstantInt>(V))
289 return CI->getValue().isStrictlyPositive();
290
291 // If `isKnownNonNegative` ever becomes more sophisticated, make sure to keep
292 // this updated.
293 KnownBits Known = computeKnownBits(V, SQ, Depth);
294 return Known.isNonNegative() &&
295 (Known.isNonZero() || isKnownNonZero(V, SQ, Depth));
296}
297
299 unsigned Depth) {
300 return computeKnownBits(V, SQ, Depth).isNegative();
301}
302
303static bool isKnownNonEqual(const Value *V1, const Value *V2,
304 const APInt &DemandedElts, const SimplifyQuery &Q,
305 unsigned Depth);
306
307bool llvm::isKnownNonEqual(const Value *V1, const Value *V2,
308 const SimplifyQuery &Q, unsigned Depth) {
309 // We don't support looking through casts.
310 if (V1 == V2 || V1->getType() != V2->getType())
311 return false;
312 auto *FVTy = dyn_cast<FixedVectorType>(V1->getType());
313 APInt DemandedElts =
314 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
315 return ::isKnownNonEqual(V1, V2, DemandedElts, Q, Depth);
316}
317
318bool llvm::MaskedValueIsZero(const Value *V, const APInt &Mask,
319 const SimplifyQuery &SQ, unsigned Depth) {
320 KnownBits Known(Mask.getBitWidth());
321 computeKnownBits(V, Known, SQ, Depth);
322 return Mask.isSubsetOf(Known.Zero);
323}
324
325static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
326 const SimplifyQuery &Q, unsigned Depth);
327
328static unsigned ComputeNumSignBits(const Value *V, const SimplifyQuery &Q,
329 unsigned Depth = 0) {
330 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
331 APInt DemandedElts =
332 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
333 return ComputeNumSignBits(V, DemandedElts, Q, Depth);
334}
335
336unsigned llvm::ComputeNumSignBits(const Value *V, const DataLayout &DL,
337 AssumptionCache *AC, const Instruction *CxtI,
338 const DominatorTree *DT, bool UseInstrInfo,
339 unsigned Depth) {
340 return ::ComputeNumSignBits(
341 V, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
342}
343
345 AssumptionCache *AC,
346 const Instruction *CxtI,
347 const DominatorTree *DT,
348 unsigned Depth) {
349 unsigned SignBits = ComputeNumSignBits(V, DL, AC, CxtI, DT, Depth);
350 return V->getType()->getScalarSizeInBits() - SignBits + 1;
351}
352
353/// Try to detect the lerp pattern: a * (b - c) + c * d
354/// where a >= 0, b >= 0, c >= 0, d >= 0, and b >= c.
355///
356/// In that particular case, we can use the following chain of reasoning:
357///
358/// a * (b - c) + c * d <= a' * (b - c) + a' * c = a' * b where a' = max(a, d)
359///
360/// Since that is true for arbitrary a, b, c and d within our constraints, we
361/// can conclude that:
362///
363/// max(a * (b - c) + c * d) <= max(max(a), max(d)) * max(b) = U
364///
365/// Considering that any result of the lerp would be less or equal to U, it
366/// would have at least the number of leading 0s as in U.
367///
368/// While being quite a specific situation, it is fairly common in computer
369/// graphics in the shape of alpha blending.
370///
371/// Modifies given KnownOut in-place with the inferred information.
372static void computeKnownBitsFromLerpPattern(const Value *Op0, const Value *Op1,
373 const APInt &DemandedElts,
374 KnownBits &KnownOut,
375 const SimplifyQuery &Q,
376 unsigned Depth) {
377
378 Type *Ty = Op0->getType();
379 const unsigned BitWidth = Ty->getScalarSizeInBits();
380
381 // Only handle scalar types for now
382 if (Ty->isVectorTy())
383 return;
384
385 // Try to match: a * (b - c) + c * d.
386 // When a == 1 => A == nullptr, the same applies to d/D as well.
387 const Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
388 const Instruction *SubBC = nullptr;
389
390 const auto MatchSubBC = [&]() {
391 // (b - c) can have two forms that interest us:
392 //
393 // 1. sub nuw %b, %c
394 // 2. xor %c, %b
395 //
396 // For the first case, nuw flag guarantees our requirement b >= c.
397 //
398 // The second case might happen when the analysis can infer that b is a mask
399 // for c and we can transform sub operation into xor (that is usually true
400 // for constant b's). Even though xor is symmetrical, canonicalization
401 // ensures that the constant will be the RHS. We have additional checks
402 // later on to ensure that this xor operation is equivalent to subtraction.
404 m_Xor(m_Value(C), m_Value(B))));
405 };
406
407 const auto MatchASubBC = [&]() {
408 // Cases:
409 // - a * (b - c)
410 // - (b - c) * a
411 // - (b - c) <- a implicitly equals 1
412 return m_CombineOr(m_c_Mul(m_Value(A), MatchSubBC()), MatchSubBC());
413 };
414
415 const auto MatchCD = [&]() {
416 // Cases:
417 // - d * c
418 // - c * d
419 // - c <- d implicitly equals 1
421 };
422
423 const auto Match = [&](const Value *LHS, const Value *RHS) {
424 // We do use m_Specific(C) in MatchCD, so we have to make sure that
425 // it's bound to anything and match(LHS, MatchASubBC()) absolutely
426 // has to evaluate first and return true.
427 //
428 // If Match returns true, it is guaranteed that B != nullptr, C != nullptr.
429 return match(LHS, MatchASubBC()) && match(RHS, MatchCD());
430 };
431
432 if (!Match(Op0, Op1) && !Match(Op1, Op0))
433 return;
434
435 const auto ComputeKnownBitsOrOne = [&](const Value *V) {
436 // For some of the values we use the convention of leaving
437 // it nullptr to signify an implicit constant 1.
438 return V ? computeKnownBits(V, DemandedElts, Q, Depth + 1)
440 };
441
442 // Check that all operands are non-negative
443 const KnownBits KnownA = ComputeKnownBitsOrOne(A);
444 if (!KnownA.isNonNegative())
445 return;
446
447 const KnownBits KnownD = ComputeKnownBitsOrOne(D);
448 if (!KnownD.isNonNegative())
449 return;
450
451 const KnownBits KnownB = computeKnownBits(B, DemandedElts, Q, Depth + 1);
452 if (!KnownB.isNonNegative())
453 return;
454
455 const KnownBits KnownC = computeKnownBits(C, DemandedElts, Q, Depth + 1);
456 if (!KnownC.isNonNegative())
457 return;
458
459 // If we matched subtraction as xor, we need to actually check that xor
460 // is semantically equivalent to subtraction.
461 //
462 // For that to be true, b has to be a mask for c or that b's known
463 // ones cover all known and possible ones of c.
464 if (SubBC->getOpcode() == Instruction::Xor &&
465 !KnownC.getMaxValue().isSubsetOf(KnownB.getMinValue()))
466 return;
467
468 const APInt MaxA = KnownA.getMaxValue();
469 const APInt MaxD = KnownD.getMaxValue();
470 const APInt MaxAD = APIntOps::umax(MaxA, MaxD);
471 const APInt MaxB = KnownB.getMaxValue();
472
473 // We can't infer leading zeros info if the upper-bound estimate wraps.
474 bool Overflow;
475 const APInt UpperBound = MaxAD.umul_ov(MaxB, Overflow);
476
477 if (Overflow)
478 return;
479
480 // If we know that x <= y and both are positive than x has at least the same
481 // number of leading zeros as y.
482 const unsigned MinimumNumberOfLeadingZeros = UpperBound.countl_zero();
483 KnownOut.Zero.setHighBits(MinimumNumberOfLeadingZeros);
484}
485
486static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1,
487 bool NSW, bool NUW,
488 const APInt &DemandedElts,
489 KnownBits &KnownOut, KnownBits &Known2,
490 const SimplifyQuery &Q, unsigned Depth) {
491 computeKnownBits(Op1, DemandedElts, KnownOut, Q, Depth + 1);
492
493 // If one operand is unknown and we have no nowrap information,
494 // the result will be unknown independently of the second operand.
495 if (KnownOut.isUnknown() && !NSW && !NUW)
496 return;
497
498 computeKnownBits(Op0, DemandedElts, Known2, Q, Depth + 1);
499 KnownOut = KnownBits::computeForAddSub(Add, NSW, NUW, Known2, KnownOut);
500
501 if (!Add && NSW && !KnownOut.isNonNegative() &&
503 .value_or(false) ||
504 match(Op1, m_c_SMin(m_Specific(Op0), m_Value()))))
505 KnownOut.makeNonNegative();
506
507 if (Add)
508 // Try to match lerp pattern and combine results
509 computeKnownBitsFromLerpPattern(Op0, Op1, DemandedElts, KnownOut, Q, Depth);
510}
511
512static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,
513 bool NUW, const APInt &DemandedElts,
514 KnownBits &Known, KnownBits &Known2,
515 const SimplifyQuery &Q, unsigned Depth) {
516 computeKnownBits(Op1, DemandedElts, Known, Q, Depth + 1);
517 computeKnownBits(Op0, DemandedElts, Known2, Q, Depth + 1);
518
519 bool isKnownNegative = false;
520 bool isKnownNonNegative = false;
521 // If the multiplication is known not to overflow, compute the sign bit.
522 if (NSW) {
523 if (Op0 == Op1) {
524 // The product of a number with itself is non-negative.
525 isKnownNonNegative = true;
526 } else {
527 bool isKnownNonNegativeOp1 = Known.isNonNegative();
528 bool isKnownNonNegativeOp0 = Known2.isNonNegative();
529 bool isKnownNegativeOp1 = Known.isNegative();
530 bool isKnownNegativeOp0 = Known2.isNegative();
531 // The product of two numbers with the same sign is non-negative.
532 isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) ||
533 (isKnownNonNegativeOp1 && isKnownNonNegativeOp0);
534 if (!isKnownNonNegative && NUW) {
535 // mul nuw nsw with a factor > 1 is non-negative.
537 isKnownNonNegative = KnownBits::sgt(Known, One).value_or(false) ||
538 KnownBits::sgt(Known2, One).value_or(false);
539 }
540
541 // The product of a negative number and a non-negative number is either
542 // negative or zero.
545 (isKnownNegativeOp1 && isKnownNonNegativeOp0 &&
546 Known2.isNonZero()) ||
547 (isKnownNegativeOp0 && isKnownNonNegativeOp1 && Known.isNonZero());
548 }
549 }
550
551 bool SelfMultiply = Op0 == Op1;
552 if (SelfMultiply)
553 SelfMultiply &=
554 isGuaranteedNotToBeUndef(Op0, Q.AC, Q.CxtI, Q.DT, Depth + 1);
555 Known = KnownBits::mul(Known, Known2, SelfMultiply);
556
557 if (SelfMultiply) {
558 unsigned SignBits = ComputeNumSignBits(Op0, DemandedElts, Q, Depth + 1);
559 unsigned TyBits = Op0->getType()->getScalarSizeInBits();
560 unsigned OutValidBits = 2 * (TyBits - SignBits + 1);
561
562 if (OutValidBits < TyBits) {
563 APInt KnownZeroMask =
564 APInt::getHighBitsSet(TyBits, TyBits - OutValidBits + 1);
565 Known.Zero |= KnownZeroMask;
566 }
567 }
568
569 // Only make use of no-wrap flags if we failed to compute the sign bit
570 // directly. This matters if the multiplication always overflows, in
571 // which case we prefer to follow the result of the direct computation,
572 // though as the program is invoking undefined behaviour we can choose
573 // whatever we like here.
574 if (isKnownNonNegative && !Known.isNegative())
575 Known.makeNonNegative();
576 else if (isKnownNegative && !Known.isNonNegative())
577 Known.makeNegative();
578}
579
581 KnownBits &Known) {
582 unsigned BitWidth = Known.getBitWidth();
583 unsigned NumRanges = Ranges.getNumOperands() / 2;
584 assert(NumRanges >= 1);
585
586 Known.setAllConflict();
587
588 for (unsigned i = 0; i < NumRanges; ++i) {
590 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
592 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
593 ConstantRange Range(Lower->getValue(), Upper->getValue());
594 // BitWidth must equal the Ranges BitWidth for the correct number of high
595 // bits to be set.
596 assert(BitWidth == Range.getBitWidth() &&
597 "Known bit width must match range bit width!");
598
599 // The first CommonPrefixBits of all values in Range are equal.
600 unsigned CommonPrefixBits =
601 (Range.getUnsignedMax() ^ Range.getUnsignedMin()).countl_zero();
602 APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits);
603 APInt UnsignedMax = Range.getUnsignedMax().zextOrTrunc(BitWidth);
604 Known.One &= UnsignedMax & Mask;
605 Known.Zero &= ~UnsignedMax & Mask;
606 }
607}
608
609static bool isEphemeralValueOf(const Instruction *I, const Value *E) {
613
614 // The instruction defining an assumption's condition itself is always
615 // considered ephemeral to that assumption (even if it has other
616 // non-ephemeral users). See r246696's test case for an example.
617 if (is_contained(I->operands(), E))
618 return true;
619
620 while (!WorkSet.empty()) {
621 const Instruction *V = WorkSet.pop_back_val();
622 if (!Visited.insert(V).second)
623 continue;
624
625 // If all uses of this value are ephemeral, then so is this value.
626 if (all_of(V->users(), [&](const User *U) {
627 return EphValues.count(cast<Instruction>(U));
628 })) {
629 if (V == E)
630 return true;
631
632 if (V == I || (!V->mayHaveSideEffects() && !V->isTerminator())) {
633 EphValues.insert(V);
634
635 for (const Use &U : V->operands()) {
636 if (const auto *I = dyn_cast<Instruction>(U.get()))
637 WorkSet.push_back(I);
638 }
639 }
640 }
641 }
642
643 return false;
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.CxtI, Q.DT))
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
871 if (cmpExcludesZero(Pred, RHS) && isValidAssumeForContext(I, Q.CxtI, Q.DT))
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 // Allow AllowEphemerals in isValidAssumeForContext, as the CxtI might
1098 // be the producer of the pointer in the bundle. At the moment, align
1099 // assumptions aren't optimized away.
1100 if (RK.WasOn == V && RK.AttrKind == Attribute::Alignment &&
1101 isPowerOf2_64(RK.ArgValue) &&
1102 isValidAssumeForContext(I, Q.CxtI, Q.DT, /*AllowEphemerals*/ true))
1103 Known.Zero.setLowBits(Log2_64(RK.ArgValue));
1104 }
1105 continue;
1106 }
1107
1108 // Warning: This loop can end up being somewhat performance sensitive.
1109 // We're running this loop for once for each value queried resulting in a
1110 // runtime of ~O(#assumes * #values).
1111
1112 Value *Arg = I->getArgOperand(0);
1113
1114 if (Arg == V && isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
1115 assert(BitWidth == 1 && "assume operand is not i1?");
1116 (void)BitWidth;
1117 Known.setAllOnes();
1118 return;
1119 }
1120 if (match(Arg, m_Not(m_Specific(V))) &&
1122 assert(BitWidth == 1 && "assume operand is not i1?");
1123 (void)BitWidth;
1124 Known.setAllZero();
1125 return;
1126 }
1127 auto *Trunc = dyn_cast<TruncInst>(Arg);
1128 if (Trunc && Trunc->getOperand(0) == V &&
1130 if (Trunc->hasNoUnsignedWrap()) {
1132 return;
1133 }
1134 Known.One.setBit(0);
1135 return;
1136 }
1137
1138 // The remaining tests are all recursive, so bail out if we hit the limit.
1140 continue;
1141
1142 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
1143 if (!Cmp)
1144 continue;
1145
1146 if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
1147 continue;
1148
1149 computeKnownBitsFromICmpCond(V, Cmp, Known, Q, /*Invert=*/false);
1150 }
1151
1152 // Conflicting assumption: Undefined behavior will occur on this execution
1153 // path.
1154 if (Known.hasConflict())
1155 Known.resetAll();
1156}
1157
1158/// Compute known bits from a shift operator, including those with a
1159/// non-constant shift amount. Known is the output of this function. Known2 is a
1160/// pre-allocated temporary with the same bit width as Known and on return
1161/// contains the known bit of the shift value source. KF is an
1162/// operator-specific function that, given the known-bits and a shift amount,
1163/// compute the implied known-bits of the shift operator's result respectively
1164/// for that shift amount. The results from calling KF are conservatively
1165/// combined for all permitted shift amounts.
1167 const Operator *I, const APInt &DemandedElts, KnownBits &Known,
1168 KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth,
1169 function_ref<KnownBits(const KnownBits &, const KnownBits &, bool)> KF) {
1170 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1171 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1172 // To limit compile-time impact, only query isKnownNonZero() if we know at
1173 // least something about the shift amount.
1174 bool ShAmtNonZero =
1175 Known.isNonZero() ||
1176 (Known.getMaxValue().ult(Known.getBitWidth()) &&
1177 isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth + 1));
1178 Known = KF(Known2, Known, ShAmtNonZero);
1179}
1180
1181static KnownBits
1182getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts,
1183 const KnownBits &KnownLHS, const KnownBits &KnownRHS,
1184 const SimplifyQuery &Q, unsigned Depth) {
1185 unsigned BitWidth = KnownLHS.getBitWidth();
1186 KnownBits KnownOut(BitWidth);
1187 bool IsAnd = false;
1188 bool HasKnownOne = !KnownLHS.One.isZero() || !KnownRHS.One.isZero();
1189 Value *X = nullptr, *Y = nullptr;
1190
1191 switch (I->getOpcode()) {
1192 case Instruction::And:
1193 KnownOut = KnownLHS & KnownRHS;
1194 IsAnd = true;
1195 // and(x, -x) is common idioms that will clear all but lowest set
1196 // bit. If we have a single known bit in x, we can clear all bits
1197 // above it.
1198 // TODO: instcombine often reassociates independent `and` which can hide
1199 // this pattern. Try to match and(x, and(-x, y)) / and(and(x, y), -x).
1200 if (HasKnownOne && match(I, m_c_And(m_Value(X), m_Neg(m_Deferred(X))))) {
1201 // -(-x) == x so using whichever (LHS/RHS) gets us a better result.
1202 if (KnownLHS.countMaxTrailingZeros() <= KnownRHS.countMaxTrailingZeros())
1203 KnownOut = KnownLHS.blsi();
1204 else
1205 KnownOut = KnownRHS.blsi();
1206 }
1207 break;
1208 case Instruction::Or:
1209 KnownOut = KnownLHS | KnownRHS;
1210 break;
1211 case Instruction::Xor:
1212 KnownOut = KnownLHS ^ KnownRHS;
1213 // xor(x, x-1) is common idioms that will clear all but lowest set
1214 // bit. If we have a single known bit in x, we can clear all bits
1215 // above it.
1216 // TODO: xor(x, x-1) is often rewritting as xor(x, x-C) where C !=
1217 // -1 but for the purpose of demanded bits (xor(x, x-C) &
1218 // Demanded) == (xor(x, x-1) & Demanded). Extend the xor pattern
1219 // to use arbitrary C if xor(x, x-C) as the same as xor(x, x-1).
1220 if (HasKnownOne &&
1222 const KnownBits &XBits = I->getOperand(0) == X ? KnownLHS : KnownRHS;
1223 KnownOut = XBits.blsmsk();
1224 }
1225 break;
1226 default:
1227 llvm_unreachable("Invalid Op used in 'analyzeKnownBitsFromAndXorOr'");
1228 }
1229
1230 // and(x, add (x, -1)) is a common idiom that always clears the low bit;
1231 // xor/or(x, add (x, -1)) is an idiom that will always set the low bit.
1232 // here we handle the more general case of adding any odd number by
1233 // matching the form and/xor/or(x, add(x, y)) where y is odd.
1234 // TODO: This could be generalized to clearing any bit set in y where the
1235 // following bit is known to be unset in y.
1236 if (!KnownOut.Zero[0] && !KnownOut.One[0] &&
1240 KnownBits KnownY(BitWidth);
1241 computeKnownBits(Y, DemandedElts, KnownY, Q, Depth + 1);
1242 if (KnownY.countMinTrailingOnes() > 0) {
1243 if (IsAnd)
1244 KnownOut.Zero.setBit(0);
1245 else
1246 KnownOut.One.setBit(0);
1247 }
1248 }
1249 return KnownOut;
1250}
1251
1253 const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q,
1254 unsigned Depth,
1255 const function_ref<KnownBits(const KnownBits &, const KnownBits &)>
1256 KnownBitsFunc) {
1257 APInt DemandedEltsLHS, DemandedEltsRHS;
1259 DemandedElts, DemandedEltsLHS,
1260 DemandedEltsRHS);
1261
1262 const auto ComputeForSingleOpFunc =
1263 [Depth, &Q, KnownBitsFunc](const Value *Op, APInt &DemandedEltsOp) {
1264 return KnownBitsFunc(
1265 computeKnownBits(Op, DemandedEltsOp, Q, Depth + 1),
1266 computeKnownBits(Op, DemandedEltsOp << 1, Q, Depth + 1));
1267 };
1268
1269 if (DemandedEltsRHS.isZero())
1270 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS);
1271 if (DemandedEltsLHS.isZero())
1272 return ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS);
1273
1274 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS)
1275 .intersectWith(ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS));
1276}
1277
1278// Public so this can be used in `SimplifyDemandedUseBits`.
1280 const KnownBits &KnownLHS,
1281 const KnownBits &KnownRHS,
1282 const SimplifyQuery &SQ,
1283 unsigned Depth) {
1284 auto *FVTy = dyn_cast<FixedVectorType>(I->getType());
1285 APInt DemandedElts =
1286 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
1287
1288 return getKnownBitsFromAndXorOr(I, DemandedElts, KnownLHS, KnownRHS, SQ,
1289 Depth);
1290}
1291
1293 Attribute Attr = F->getFnAttribute(Attribute::VScaleRange);
1294 // Without vscale_range, we only know that vscale is non-zero.
1295 if (!Attr.isValid())
1297
1298 unsigned AttrMin = Attr.getVScaleRangeMin();
1299 // Minimum is larger than vscale width, result is always poison.
1300 if ((unsigned)llvm::bit_width(AttrMin) > BitWidth)
1301 return ConstantRange::getEmpty(BitWidth);
1302
1303 APInt Min(BitWidth, AttrMin);
1304 std::optional<unsigned> AttrMax = Attr.getVScaleRangeMax();
1305 if (!AttrMax || (unsigned)llvm::bit_width(*AttrMax) > BitWidth)
1307
1308 return ConstantRange(Min, APInt(BitWidth, *AttrMax) + 1);
1309}
1310
1312 Value *Arm, bool Invert,
1313 const SimplifyQuery &Q, unsigned Depth) {
1314 // If we have a constant arm, we are done.
1315 if (Known.isConstant())
1316 return;
1317
1318 // See what condition implies about the bits of the select arm.
1319 KnownBits CondRes(Known.getBitWidth());
1320 computeKnownBitsFromCond(Arm, Cond, CondRes, Q, Invert, Depth + 1);
1321 // If we don't get any information from the condition, no reason to
1322 // proceed.
1323 if (CondRes.isUnknown())
1324 return;
1325
1326 // We can have conflict if the condition is dead. I.e if we have
1327 // (x | 64) < 32 ? (x | 64) : y
1328 // we will have conflict at bit 6 from the condition/the `or`.
1329 // In that case just return. Its not particularly important
1330 // what we do, as this select is going to be simplified soon.
1331 CondRes = CondRes.unionWith(Known);
1332 if (CondRes.hasConflict())
1333 return;
1334
1335 // Finally make sure the information we found is valid. This is relatively
1336 // expensive so it's left for the very end.
1337 if (!isGuaranteedNotToBeUndef(Arm, Q.AC, Q.CxtI, Q.DT, Depth + 1))
1338 return;
1339
1340 // Finally, we know we get information from the condition and its valid,
1341 // so return it.
1342 Known = std::move(CondRes);
1343}
1344
1345// Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow).
1346// Returns the input and lower/upper bounds.
1347static bool isSignedMinMaxClamp(const Value *Select, const Value *&In,
1348 const APInt *&CLow, const APInt *&CHigh) {
1350 cast<Operator>(Select)->getOpcode() == Instruction::Select &&
1351 "Input should be a Select!");
1352
1353 const Value *LHS = nullptr, *RHS = nullptr;
1355 if (SPF != SPF_SMAX && SPF != SPF_SMIN)
1356 return false;
1357
1358 if (!match(RHS, m_APInt(CLow)))
1359 return false;
1360
1361 const Value *LHS2 = nullptr, *RHS2 = nullptr;
1363 if (getInverseMinMaxFlavor(SPF) != SPF2)
1364 return false;
1365
1366 if (!match(RHS2, m_APInt(CHigh)))
1367 return false;
1368
1369 if (SPF == SPF_SMIN)
1370 std::swap(CLow, CHigh);
1371
1372 In = LHS2;
1373 return CLow->sle(*CHigh);
1374}
1375
1377 const APInt *&CLow,
1378 const APInt *&CHigh) {
1379 assert((II->getIntrinsicID() == Intrinsic::smin ||
1380 II->getIntrinsicID() == Intrinsic::smax) &&
1381 "Must be smin/smax");
1382
1383 Intrinsic::ID InverseID = getInverseMinMaxIntrinsic(II->getIntrinsicID());
1384 auto *InnerII = dyn_cast<IntrinsicInst>(II->getArgOperand(0));
1385 if (!InnerII || InnerII->getIntrinsicID() != InverseID ||
1386 !match(II->getArgOperand(1), m_APInt(CLow)) ||
1387 !match(InnerII->getArgOperand(1), m_APInt(CHigh)))
1388 return false;
1389
1390 if (II->getIntrinsicID() == Intrinsic::smin)
1391 std::swap(CLow, CHigh);
1392 return CLow->sle(*CHigh);
1393}
1394
1396 KnownBits &Known) {
1397 const APInt *CLow, *CHigh;
1398 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
1399 Known = Known.unionWith(
1400 ConstantRange::getNonEmpty(*CLow, *CHigh + 1).toKnownBits());
1401}
1402
1404 const APInt &DemandedElts,
1405 KnownBits &Known,
1406 const SimplifyQuery &Q,
1407 unsigned Depth) {
1408 unsigned BitWidth = Known.getBitWidth();
1409
1410 KnownBits Known2(BitWidth);
1411 switch (I->getOpcode()) {
1412 default: break;
1413 case Instruction::Load:
1414 if (MDNode *MD =
1415 Q.IIQ.getMetadata(cast<LoadInst>(I), LLVMContext::MD_range))
1417 break;
1418 case Instruction::And:
1419 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1420 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1421
1422 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1423 break;
1424 case Instruction::Or:
1425 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1426 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1427
1428 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1429 break;
1430 case Instruction::Xor:
1431 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1432 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1433
1434 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1435 break;
1436 case Instruction::Mul: {
1439 computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, NUW,
1440 DemandedElts, Known, Known2, Q, Depth);
1441 break;
1442 }
1443 case Instruction::UDiv: {
1444 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1445 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1446 Known =
1447 KnownBits::udiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1448 break;
1449 }
1450 case Instruction::SDiv: {
1451 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1452 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1453 Known =
1454 KnownBits::sdiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1455 break;
1456 }
1457 case Instruction::Select: {
1458 auto ComputeForArm = [&](Value *Arm, bool Invert) {
1459 KnownBits Res(Known.getBitWidth());
1460 computeKnownBits(Arm, DemandedElts, Res, Q, Depth + 1);
1461 adjustKnownBitsForSelectArm(Res, I->getOperand(0), Arm, Invert, Q, Depth);
1462 return Res;
1463 };
1464 // Only known if known in both the LHS and RHS.
1465 Known =
1466 ComputeForArm(I->getOperand(1), /*Invert=*/false)
1467 .intersectWith(ComputeForArm(I->getOperand(2), /*Invert=*/true));
1468 break;
1469 }
1470 case Instruction::FPTrunc:
1471 case Instruction::FPExt:
1472 case Instruction::FPToUI:
1473 case Instruction::FPToSI:
1474 case Instruction::SIToFP:
1475 case Instruction::UIToFP:
1476 break; // Can't work with floating point.
1477 case Instruction::PtrToInt:
1478 case Instruction::PtrToAddr:
1479 case Instruction::IntToPtr:
1480 // Fall through and handle them the same as zext/trunc.
1481 [[fallthrough]];
1482 case Instruction::ZExt:
1483 case Instruction::Trunc: {
1484 Type *SrcTy = I->getOperand(0)->getType();
1485
1486 unsigned SrcBitWidth;
1487 // Note that we handle pointer operands here because of inttoptr/ptrtoint
1488 // which fall through here.
1489 Type *ScalarTy = SrcTy->getScalarType();
1490 SrcBitWidth = ScalarTy->isPointerTy() ?
1491 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
1492 Q.DL.getTypeSizeInBits(ScalarTy);
1493
1494 assert(SrcBitWidth && "SrcBitWidth can't be zero");
1495 Known = Known.anyextOrTrunc(SrcBitWidth);
1496 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1497 if (auto *Inst = dyn_cast<PossiblyNonNegInst>(I);
1498 Inst && Inst->hasNonNeg() && !Known.isNegative())
1499 Known.makeNonNegative();
1500 Known = Known.zextOrTrunc(BitWidth);
1501 break;
1502 }
1503 case Instruction::BitCast: {
1504 Type *SrcTy = I->getOperand(0)->getType();
1505 if (SrcTy->isIntOrPtrTy() &&
1506 // TODO: For now, not handling conversions like:
1507 // (bitcast i64 %x to <2 x i32>)
1508 !I->getType()->isVectorTy()) {
1509 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1510 break;
1511 }
1512
1513 const Value *V;
1514 // Handle bitcast from floating point to integer.
1515 if (match(I, m_ElementWiseBitCast(m_Value(V))) &&
1516 V->getType()->isFPOrFPVectorTy()) {
1517 Type *FPType = V->getType()->getScalarType();
1518 KnownFPClass Result =
1519 computeKnownFPClass(V, DemandedElts, fcAllFlags, Q, Depth + 1);
1520 FPClassTest FPClasses = Result.KnownFPClasses;
1521
1522 // TODO: Treat it as zero/poison if the use of I is unreachable.
1523 if (FPClasses == fcNone)
1524 break;
1525
1526 if (Result.isKnownNever(fcNormal | fcSubnormal | fcNan)) {
1527 Known.setAllConflict();
1528
1529 if (FPClasses & fcInf)
1531 APFloat::getInf(FPType->getFltSemantics()).bitcastToAPInt()));
1532
1533 if (FPClasses & fcZero)
1535 APInt::getZero(FPType->getScalarSizeInBits())));
1536
1537 Known.Zero.clearSignBit();
1538 Known.One.clearSignBit();
1539 }
1540
1541 if (Result.SignBit) {
1542 if (*Result.SignBit)
1543 Known.makeNegative();
1544 else
1545 Known.makeNonNegative();
1546 }
1547
1548 break;
1549 }
1550
1551 // Handle cast from vector integer type to scalar or vector integer.
1552 auto *SrcVecTy = dyn_cast<FixedVectorType>(SrcTy);
1553 if (!SrcVecTy || !SrcVecTy->getElementType()->isIntegerTy() ||
1554 !I->getType()->isIntOrIntVectorTy() ||
1555 isa<ScalableVectorType>(I->getType()))
1556 break;
1557
1558 unsigned NumElts = DemandedElts.getBitWidth();
1559 bool IsLE = Q.DL.isLittleEndian();
1560 // Look through a cast from narrow vector elements to wider type.
1561 // Examples: v4i32 -> v2i64, v3i8 -> v24
1562 unsigned SubBitWidth = SrcVecTy->getScalarSizeInBits();
1563 if (BitWidth % SubBitWidth == 0) {
1564 // Known bits are automatically intersected across demanded elements of a
1565 // vector. So for example, if a bit is computed as known zero, it must be
1566 // zero across all demanded elements of the vector.
1567 //
1568 // For this bitcast, each demanded element of the output is sub-divided
1569 // across a set of smaller vector elements in the source vector. To get
1570 // the known bits for an entire element of the output, compute the known
1571 // bits for each sub-element sequentially. This is done by shifting the
1572 // one-set-bit demanded elements parameter across the sub-elements for
1573 // consecutive calls to computeKnownBits. We are using the demanded
1574 // elements parameter as a mask operator.
1575 //
1576 // The known bits of each sub-element are then inserted into place
1577 // (dependent on endian) to form the full result of known bits.
1578 unsigned SubScale = BitWidth / SubBitWidth;
1579 APInt SubDemandedElts = APInt::getZero(NumElts * SubScale);
1580 for (unsigned i = 0; i != NumElts; ++i) {
1581 if (DemandedElts[i])
1582 SubDemandedElts.setBit(i * SubScale);
1583 }
1584
1585 KnownBits KnownSrc(SubBitWidth);
1586 for (unsigned i = 0; i != SubScale; ++i) {
1587 computeKnownBits(I->getOperand(0), SubDemandedElts.shl(i), KnownSrc, Q,
1588 Depth + 1);
1589 unsigned ShiftElt = IsLE ? i : SubScale - 1 - i;
1590 Known.insertBits(KnownSrc, ShiftElt * SubBitWidth);
1591 }
1592 }
1593 // Look through a cast from wider vector elements to narrow type.
1594 // Examples: v2i64 -> v4i32
1595 if (SubBitWidth % BitWidth == 0) {
1596 unsigned SubScale = SubBitWidth / BitWidth;
1597 KnownBits KnownSrc(SubBitWidth);
1598 APInt SubDemandedElts =
1599 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
1600 computeKnownBits(I->getOperand(0), SubDemandedElts, KnownSrc, Q,
1601 Depth + 1);
1602
1603 Known.setAllConflict();
1604 for (unsigned i = 0; i != NumElts; ++i) {
1605 if (DemandedElts[i]) {
1606 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
1607 unsigned Offset = (Shifts % SubScale) * BitWidth;
1608 Known = Known.intersectWith(KnownSrc.extractBits(BitWidth, Offset));
1609 if (Known.isUnknown())
1610 break;
1611 }
1612 }
1613 }
1614 break;
1615 }
1616 case Instruction::SExt: {
1617 // Compute the bits in the result that are not present in the input.
1618 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
1619
1620 Known = Known.trunc(SrcBitWidth);
1621 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1622 // If the sign bit of the input is known set or clear, then we know the
1623 // top bits of the result.
1624 Known = Known.sext(BitWidth);
1625 break;
1626 }
1627 case Instruction::Shl: {
1630 auto KF = [NUW, NSW](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1631 bool ShAmtNonZero) {
1632 return KnownBits::shl(KnownVal, KnownAmt, NUW, NSW, ShAmtNonZero);
1633 };
1634 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1635 KF);
1636 // Trailing zeros of a right-shifted constant never decrease.
1637 const APInt *C;
1638 if (match(I->getOperand(0), m_APInt(C)))
1639 Known.Zero.setLowBits(C->countr_zero());
1640 break;
1641 }
1642 case Instruction::LShr: {
1643 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1644 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1645 bool ShAmtNonZero) {
1646 return KnownBits::lshr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1647 };
1648 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1649 KF);
1650 // Leading zeros of a left-shifted constant never decrease.
1651 const APInt *C;
1652 if (match(I->getOperand(0), m_APInt(C)))
1653 Known.Zero.setHighBits(C->countl_zero());
1654 break;
1655 }
1656 case Instruction::AShr: {
1657 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1658 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1659 bool ShAmtNonZero) {
1660 return KnownBits::ashr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1661 };
1662 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1663 KF);
1664 break;
1665 }
1666 case Instruction::Sub: {
1669 computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW, NUW,
1670 DemandedElts, Known, Known2, Q, Depth);
1671 break;
1672 }
1673 case Instruction::Add: {
1676 computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW, NUW,
1677 DemandedElts, Known, Known2, Q, Depth);
1678 break;
1679 }
1680 case Instruction::SRem:
1681 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1682 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1683 Known = KnownBits::srem(Known, Known2);
1684 break;
1685
1686 case Instruction::URem:
1687 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1688 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1689 Known = KnownBits::urem(Known, Known2);
1690 break;
1691 case Instruction::Alloca:
1693 break;
1694 case Instruction::GetElementPtr: {
1695 // Analyze all of the subscripts of this getelementptr instruction
1696 // to determine if we can prove known low zero bits.
1697 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1698 // Accumulate the constant indices in a separate variable
1699 // to minimize the number of calls to computeForAddSub.
1700 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(I->getType());
1701 APInt AccConstIndices(IndexWidth, 0);
1702
1703 auto AddIndexToKnown = [&](KnownBits IndexBits) {
1704 if (IndexWidth == BitWidth) {
1705 // Note that inbounds does *not* guarantee nsw for the addition, as only
1706 // the offset is signed, while the base address is unsigned.
1707 Known = KnownBits::add(Known, IndexBits);
1708 } else {
1709 // If the index width is smaller than the pointer width, only add the
1710 // value to the low bits.
1711 assert(IndexWidth < BitWidth &&
1712 "Index width can't be larger than pointer width");
1713 Known.insertBits(KnownBits::add(Known.trunc(IndexWidth), IndexBits), 0);
1714 }
1715 };
1716
1718 for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
1719 // TrailZ can only become smaller, short-circuit if we hit zero.
1720 if (Known.isUnknown())
1721 break;
1722
1723 Value *Index = I->getOperand(i);
1724
1725 // Handle case when index is zero.
1726 Constant *CIndex = dyn_cast<Constant>(Index);
1727 if (CIndex && CIndex->isNullValue())
1728 continue;
1729
1730 if (StructType *STy = GTI.getStructTypeOrNull()) {
1731 // Handle struct member offset arithmetic.
1732
1733 assert(CIndex &&
1734 "Access to structure field must be known at compile time");
1735
1736 if (CIndex->getType()->isVectorTy())
1737 Index = CIndex->getSplatValue();
1738
1739 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
1740 const StructLayout *SL = Q.DL.getStructLayout(STy);
1741 uint64_t Offset = SL->getElementOffset(Idx);
1742 AccConstIndices += Offset;
1743 continue;
1744 }
1745
1746 // Handle array index arithmetic.
1747 Type *IndexedTy = GTI.getIndexedType();
1748 if (!IndexedTy->isSized()) {
1749 Known.resetAll();
1750 break;
1751 }
1752
1753 TypeSize Stride = GTI.getSequentialElementStride(Q.DL);
1754 uint64_t StrideInBytes = Stride.getKnownMinValue();
1755 if (!Stride.isScalable()) {
1756 // Fast path for constant offset.
1757 if (auto *CI = dyn_cast<ConstantInt>(Index)) {
1758 AccConstIndices +=
1759 CI->getValue().sextOrTrunc(IndexWidth) * StrideInBytes;
1760 continue;
1761 }
1762 }
1763
1764 KnownBits IndexBits =
1765 computeKnownBits(Index, Q, Depth + 1).sextOrTrunc(IndexWidth);
1766 KnownBits ScalingFactor(IndexWidth);
1767 // Multiply by current sizeof type.
1768 // &A[i] == A + i * sizeof(*A[i]).
1769 if (Stride.isScalable()) {
1770 // For scalable types the only thing we know about sizeof is
1771 // that this is a multiple of the minimum size.
1772 ScalingFactor.Zero.setLowBits(llvm::countr_zero(StrideInBytes));
1773 } else {
1774 ScalingFactor =
1775 KnownBits::makeConstant(APInt(IndexWidth, StrideInBytes));
1776 }
1777 AddIndexToKnown(KnownBits::mul(IndexBits, ScalingFactor));
1778 }
1779 if (!Known.isUnknown() && !AccConstIndices.isZero())
1780 AddIndexToKnown(KnownBits::makeConstant(AccConstIndices));
1781 break;
1782 }
1783 case Instruction::PHI: {
1784 const PHINode *P = cast<PHINode>(I);
1785 BinaryOperator *BO = nullptr;
1786 Value *R = nullptr, *L = nullptr;
1787 if (matchSimpleRecurrence(P, BO, R, L)) {
1788 // Handle the case of a simple two-predecessor recurrence PHI.
1789 // There's a lot more that could theoretically be done here, but
1790 // this is sufficient to catch some interesting cases.
1791 unsigned Opcode = BO->getOpcode();
1792
1793 switch (Opcode) {
1794 // If this is a shift recurrence, we know the bits being shifted in. We
1795 // can combine that with information about the start value of the
1796 // recurrence to conclude facts about the result. If this is a udiv
1797 // recurrence, we know that the result can never exceed either the
1798 // numerator or the start value, whichever is greater.
1799 case Instruction::LShr:
1800 case Instruction::AShr:
1801 case Instruction::Shl:
1802 case Instruction::UDiv:
1803 if (BO->getOperand(0) != I)
1804 break;
1805 [[fallthrough]];
1806
1807 // For a urem recurrence, the result can never exceed the start value. The
1808 // phi could either be the numerator or the denominator.
1809 case Instruction::URem: {
1810 // We have matched a recurrence of the form:
1811 // %iv = [R, %entry], [%iv.next, %backedge]
1812 // %iv.next = shift_op %iv, L
1813
1814 // Recurse with the phi context to avoid concern about whether facts
1815 // inferred hold at original context instruction. TODO: It may be
1816 // correct to use the original context. IF warranted, explore and
1817 // add sufficient tests to cover.
1819 RecQ.CxtI = P;
1820 computeKnownBits(R, DemandedElts, Known2, RecQ, Depth + 1);
1821 switch (Opcode) {
1822 case Instruction::Shl:
1823 // A shl recurrence will only increase the tailing zeros
1824 Known.Zero.setLowBits(Known2.countMinTrailingZeros());
1825 break;
1826 case Instruction::LShr:
1827 case Instruction::UDiv:
1828 case Instruction::URem:
1829 // lshr, udiv, and urem recurrences will preserve the leading zeros of
1830 // the start value.
1831 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1832 break;
1833 case Instruction::AShr:
1834 // An ashr recurrence will extend the initial sign bit
1835 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1836 Known.One.setHighBits(Known2.countMinLeadingOnes());
1837 break;
1838 }
1839 break;
1840 }
1841
1842 // Check for operations that have the property that if
1843 // both their operands have low zero bits, the result
1844 // will have low zero bits.
1845 case Instruction::Add:
1846 case Instruction::Sub:
1847 case Instruction::And:
1848 case Instruction::Or:
1849 case Instruction::Mul: {
1850 // Change the context instruction to the "edge" that flows into the
1851 // phi. This is important because that is where the value is actually
1852 // "evaluated" even though it is used later somewhere else. (see also
1853 // D69571).
1855
1856 unsigned OpNum = P->getOperand(0) == R ? 0 : 1;
1857 Instruction *RInst = P->getIncomingBlock(OpNum)->getTerminator();
1858 Instruction *LInst = P->getIncomingBlock(1 - OpNum)->getTerminator();
1859
1860 // Ok, we have a PHI of the form L op= R. Check for low
1861 // zero bits.
1862 RecQ.CxtI = RInst;
1863 computeKnownBits(R, DemandedElts, Known2, RecQ, Depth + 1);
1864
1865 // We need to take the minimum number of known bits
1866 KnownBits Known3(BitWidth);
1867 RecQ.CxtI = LInst;
1868 computeKnownBits(L, DemandedElts, Known3, RecQ, Depth + 1);
1869
1870 Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(),
1871 Known3.countMinTrailingZeros()));
1872
1873 auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(BO);
1874 if (!OverflowOp || !Q.IIQ.hasNoSignedWrap(OverflowOp))
1875 break;
1876
1877 switch (Opcode) {
1878 // If initial value of recurrence is nonnegative, and we are adding
1879 // a nonnegative number with nsw, the result can only be nonnegative
1880 // or poison value regardless of the number of times we execute the
1881 // add in phi recurrence. If initial value is negative and we are
1882 // adding a negative number with nsw, the result can only be
1883 // negative or poison value. Similar arguments apply to sub and mul.
1884 //
1885 // (add non-negative, non-negative) --> non-negative
1886 // (add negative, negative) --> negative
1887 case Instruction::Add: {
1888 if (Known2.isNonNegative() && Known3.isNonNegative())
1889 Known.makeNonNegative();
1890 else if (Known2.isNegative() && Known3.isNegative())
1891 Known.makeNegative();
1892 break;
1893 }
1894
1895 // (sub nsw non-negative, negative) --> non-negative
1896 // (sub nsw negative, non-negative) --> negative
1897 case Instruction::Sub: {
1898 if (BO->getOperand(0) != I)
1899 break;
1900 if (Known2.isNonNegative() && Known3.isNegative())
1901 Known.makeNonNegative();
1902 else if (Known2.isNegative() && Known3.isNonNegative())
1903 Known.makeNegative();
1904 break;
1905 }
1906
1907 // (mul nsw non-negative, non-negative) --> non-negative
1908 case Instruction::Mul:
1909 if (Known2.isNonNegative() && Known3.isNonNegative())
1910 Known.makeNonNegative();
1911 break;
1912
1913 default:
1914 break;
1915 }
1916 break;
1917 }
1918
1919 default:
1920 break;
1921 }
1922 }
1923
1924 // Unreachable blocks may have zero-operand PHI nodes.
1925 if (P->getNumIncomingValues() == 0)
1926 break;
1927
1928 // Otherwise take the unions of the known bit sets of the operands,
1929 // taking conservative care to avoid excessive recursion.
1930 if (Depth < MaxAnalysisRecursionDepth - 1 && Known.isUnknown()) {
1931 // Skip if every incoming value references to ourself.
1932 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
1933 break;
1934
1935 Known.setAllConflict();
1936 for (const Use &U : P->operands()) {
1937 Value *IncValue;
1938 const PHINode *CxtPhi;
1939 Instruction *CxtI;
1940 breakSelfRecursivePHI(&U, P, IncValue, CxtI, &CxtPhi);
1941 // Skip direct self references.
1942 if (IncValue == P)
1943 continue;
1944
1945 // Change the context instruction to the "edge" that flows into the
1946 // phi. This is important because that is where the value is actually
1947 // "evaluated" even though it is used later somewhere else. (see also
1948 // D69571).
1950
1951 Known2 = KnownBits(BitWidth);
1952
1953 // Recurse, but cap the recursion to one level, because we don't
1954 // want to waste time spinning around in loops.
1955 // TODO: See if we can base recursion limiter on number of incoming phi
1956 // edges so we don't overly clamp analysis.
1957 computeKnownBits(IncValue, DemandedElts, Known2, RecQ,
1959
1960 // See if we can further use a conditional branch into the phi
1961 // to help us determine the range of the value.
1962 if (!Known2.isConstant()) {
1963 CmpPredicate Pred;
1964 const APInt *RHSC;
1965 BasicBlock *TrueSucc, *FalseSucc;
1966 // TODO: Use RHS Value and compute range from its known bits.
1967 if (match(RecQ.CxtI,
1968 m_Br(m_c_ICmp(Pred, m_Specific(IncValue), m_APInt(RHSC)),
1969 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
1970 // Check for cases of duplicate successors.
1971 if ((TrueSucc == CxtPhi->getParent()) !=
1972 (FalseSucc == CxtPhi->getParent())) {
1973 // If we're using the false successor, invert the predicate.
1974 if (FalseSucc == CxtPhi->getParent())
1975 Pred = CmpInst::getInversePredicate(Pred);
1976 // Get the knownbits implied by the incoming phi condition.
1977 auto CR = ConstantRange::makeExactICmpRegion(Pred, *RHSC);
1978 KnownBits KnownUnion = Known2.unionWith(CR.toKnownBits());
1979 // We can have conflicts here if we are analyzing deadcode (its
1980 // impossible for us reach this BB based the icmp).
1981 if (KnownUnion.hasConflict()) {
1982 // No reason to continue analyzing in a known dead region, so
1983 // just resetAll and break. This will cause us to also exit the
1984 // outer loop.
1985 Known.resetAll();
1986 break;
1987 }
1988 Known2 = KnownUnion;
1989 }
1990 }
1991 }
1992
1993 Known = Known.intersectWith(Known2);
1994 // If all bits have been ruled out, there's no need to check
1995 // more operands.
1996 if (Known.isUnknown())
1997 break;
1998 }
1999 }
2000 break;
2001 }
2002 case Instruction::Call:
2003 case Instruction::Invoke: {
2004 // If range metadata is attached to this call, set known bits from that,
2005 // and then intersect with known bits based on other properties of the
2006 // function.
2007 if (MDNode *MD =
2008 Q.IIQ.getMetadata(cast<Instruction>(I), LLVMContext::MD_range))
2010
2011 const auto *CB = cast<CallBase>(I);
2012
2013 if (std::optional<ConstantRange> Range = CB->getRange())
2014 Known = Known.unionWith(Range->toKnownBits());
2015
2016 if (const Value *RV = CB->getReturnedArgOperand()) {
2017 if (RV->getType() == I->getType()) {
2018 computeKnownBits(RV, Known2, Q, Depth + 1);
2019 Known = Known.unionWith(Known2);
2020 // If the function doesn't return properly for all input values
2021 // (e.g. unreachable exits) then there might be conflicts between the
2022 // argument value and the range metadata. Simply discard the known bits
2023 // in case of conflicts.
2024 if (Known.hasConflict())
2025 Known.resetAll();
2026 }
2027 }
2028 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
2029 switch (II->getIntrinsicID()) {
2030 default:
2031 break;
2032 case Intrinsic::abs: {
2033 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2034 bool IntMinIsPoison = match(II->getArgOperand(1), m_One());
2035 Known = Known.unionWith(Known2.abs(IntMinIsPoison));
2036 break;
2037 }
2038 case Intrinsic::bitreverse:
2039 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2040 Known = Known.unionWith(Known2.reverseBits());
2041 break;
2042 case Intrinsic::bswap:
2043 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2044 Known = Known.unionWith(Known2.byteSwap());
2045 break;
2046 case Intrinsic::ctlz: {
2047 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2048 // If we have a known 1, its position is our upper bound.
2049 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
2050 // If this call is poison for 0 input, the result will be less than 2^n.
2051 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
2052 PossibleLZ = std::min(PossibleLZ, BitWidth - 1);
2053 unsigned LowBits = llvm::bit_width(PossibleLZ);
2054 Known.Zero.setBitsFrom(LowBits);
2055 break;
2056 }
2057 case Intrinsic::cttz: {
2058 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2059 // If we have a known 1, its position is our upper bound.
2060 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
2061 // If this call is poison for 0 input, the result will be less than 2^n.
2062 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
2063 PossibleTZ = std::min(PossibleTZ, BitWidth - 1);
2064 unsigned LowBits = llvm::bit_width(PossibleTZ);
2065 Known.Zero.setBitsFrom(LowBits);
2066 break;
2067 }
2068 case Intrinsic::ctpop: {
2069 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2070 // We can bound the space the count needs. Also, bits known to be zero
2071 // can't contribute to the population.
2072 unsigned BitsPossiblySet = Known2.countMaxPopulation();
2073 unsigned LowBits = llvm::bit_width(BitsPossiblySet);
2074 Known.Zero.setBitsFrom(LowBits);
2075 // TODO: we could bound KnownOne using the lower bound on the number
2076 // of bits which might be set provided by popcnt KnownOne2.
2077 break;
2078 }
2079 case Intrinsic::fshr:
2080 case Intrinsic::fshl: {
2081 const APInt *SA;
2082 if (!match(I->getOperand(2), m_APInt(SA)))
2083 break;
2084
2085 // Normalize to funnel shift left.
2086 uint64_t ShiftAmt = SA->urem(BitWidth);
2087 if (II->getIntrinsicID() == Intrinsic::fshr)
2088 ShiftAmt = BitWidth - ShiftAmt;
2089
2090 KnownBits Known3(BitWidth);
2091 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2092 computeKnownBits(I->getOperand(1), DemandedElts, Known3, Q, Depth + 1);
2093
2094 Known2 <<= ShiftAmt;
2095 Known3 >>= BitWidth - ShiftAmt;
2096 Known = Known2.unionWith(Known3);
2097 break;
2098 }
2099 case Intrinsic::clmul:
2100 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2101 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2102 Known = KnownBits::clmul(Known, Known2);
2103 break;
2104 case Intrinsic::uadd_sat:
2105 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2106 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2107 Known = KnownBits::uadd_sat(Known, Known2);
2108 break;
2109 case Intrinsic::usub_sat:
2110 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2111 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2112 Known = KnownBits::usub_sat(Known, Known2);
2113 break;
2114 case Intrinsic::sadd_sat:
2115 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2116 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2117 Known = KnownBits::sadd_sat(Known, Known2);
2118 break;
2119 case Intrinsic::ssub_sat:
2120 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2121 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2122 Known = KnownBits::ssub_sat(Known, Known2);
2123 break;
2124 // Vec reverse preserves bits from input vec.
2125 case Intrinsic::vector_reverse:
2126 computeKnownBits(I->getOperand(0), DemandedElts.reverseBits(), Known, Q,
2127 Depth + 1);
2128 break;
2129 // for min/max/and/or reduce, any bit common to each element in the
2130 // input vec is set in the output.
2131 case Intrinsic::vector_reduce_and:
2132 case Intrinsic::vector_reduce_or:
2133 case Intrinsic::vector_reduce_umax:
2134 case Intrinsic::vector_reduce_umin:
2135 case Intrinsic::vector_reduce_smax:
2136 case Intrinsic::vector_reduce_smin:
2137 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2138 break;
2139 case Intrinsic::vector_reduce_xor: {
2140 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2141 // The zeros common to all vecs are zero in the output.
2142 // If the number of elements is odd, then the common ones remain. If the
2143 // number of elements is even, then the common ones becomes zeros.
2144 auto *VecTy = cast<VectorType>(I->getOperand(0)->getType());
2145 // Even, so the ones become zeros.
2146 bool EvenCnt = VecTy->getElementCount().isKnownEven();
2147 if (EvenCnt)
2148 Known.Zero |= Known.One;
2149 // Maybe even element count so need to clear ones.
2150 if (VecTy->isScalableTy() || EvenCnt)
2151 Known.One.clearAllBits();
2152 break;
2153 }
2154 case Intrinsic::vector_reduce_add: {
2155 auto *VecTy = dyn_cast<FixedVectorType>(I->getOperand(0)->getType());
2156 if (!VecTy)
2157 break;
2158 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2159 Known = Known.reduceAdd(VecTy->getNumElements());
2160 break;
2161 }
2162 case Intrinsic::umin:
2163 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2164 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2165 Known = KnownBits::umin(Known, Known2);
2166 break;
2167 case Intrinsic::umax:
2168 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2169 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2170 Known = KnownBits::umax(Known, Known2);
2171 break;
2172 case Intrinsic::smin:
2173 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2174 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2175 Known = KnownBits::smin(Known, Known2);
2177 break;
2178 case Intrinsic::smax:
2179 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2180 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2181 Known = KnownBits::smax(Known, Known2);
2183 break;
2184 case Intrinsic::ptrmask: {
2185 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2186
2187 const Value *Mask = I->getOperand(1);
2188 Known2 = KnownBits(Mask->getType()->getScalarSizeInBits());
2189 computeKnownBits(Mask, DemandedElts, Known2, Q, Depth + 1);
2190 // TODO: 1-extend would be more precise.
2191 Known &= Known2.anyextOrTrunc(BitWidth);
2192 break;
2193 }
2194 case Intrinsic::x86_sse2_pmulh_w:
2195 case Intrinsic::x86_avx2_pmulh_w:
2196 case Intrinsic::x86_avx512_pmulh_w_512:
2197 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2198 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2199 Known = KnownBits::mulhs(Known, Known2);
2200 break;
2201 case Intrinsic::x86_sse2_pmulhu_w:
2202 case Intrinsic::x86_avx2_pmulhu_w:
2203 case Intrinsic::x86_avx512_pmulhu_w_512:
2204 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2205 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2206 Known = KnownBits::mulhu(Known, Known2);
2207 break;
2208 case Intrinsic::x86_sse42_crc32_64_64:
2209 Known.Zero.setBitsFrom(32);
2210 break;
2211 case Intrinsic::x86_ssse3_phadd_d_128:
2212 case Intrinsic::x86_ssse3_phadd_w_128:
2213 case Intrinsic::x86_avx2_phadd_d:
2214 case Intrinsic::x86_avx2_phadd_w: {
2216 I, DemandedElts, Q, Depth,
2217 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2218 return KnownBits::add(KnownLHS, KnownRHS);
2219 });
2220 break;
2221 }
2222 case Intrinsic::x86_ssse3_phadd_sw_128:
2223 case Intrinsic::x86_avx2_phadd_sw: {
2225 I, DemandedElts, Q, Depth, KnownBits::sadd_sat);
2226 break;
2227 }
2228 case Intrinsic::x86_ssse3_phsub_d_128:
2229 case Intrinsic::x86_ssse3_phsub_w_128:
2230 case Intrinsic::x86_avx2_phsub_d:
2231 case Intrinsic::x86_avx2_phsub_w: {
2233 I, DemandedElts, Q, Depth,
2234 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2235 return KnownBits::sub(KnownLHS, KnownRHS);
2236 });
2237 break;
2238 }
2239 case Intrinsic::x86_ssse3_phsub_sw_128:
2240 case Intrinsic::x86_avx2_phsub_sw: {
2242 I, DemandedElts, Q, Depth, KnownBits::ssub_sat);
2243 break;
2244 }
2245 case Intrinsic::riscv_vsetvli:
2246 case Intrinsic::riscv_vsetvlimax: {
2247 bool HasAVL = II->getIntrinsicID() == Intrinsic::riscv_vsetvli;
2248 const ConstantRange Range = getVScaleRange(II->getFunction(), BitWidth);
2250 cast<ConstantInt>(II->getArgOperand(HasAVL))->getZExtValue());
2251 RISCVVType::VLMUL VLMUL = static_cast<RISCVVType::VLMUL>(
2252 cast<ConstantInt>(II->getArgOperand(1 + HasAVL))->getZExtValue());
2253 uint64_t MaxVLEN =
2254 Range.getUnsignedMax().getZExtValue() * RISCV::RVVBitsPerBlock;
2255 uint64_t MaxVL = MaxVLEN / RISCVVType::getSEWLMULRatio(SEW, VLMUL);
2256
2257 // Result of vsetvli must be not larger than AVL.
2258 if (HasAVL)
2259 if (auto *CI = dyn_cast<ConstantInt>(II->getArgOperand(0)))
2260 MaxVL = std::min(MaxVL, CI->getZExtValue());
2261
2262 unsigned KnownZeroFirstBit = Log2_32(MaxVL) + 1;
2263 if (BitWidth > KnownZeroFirstBit)
2264 Known.Zero.setBitsFrom(KnownZeroFirstBit);
2265 break;
2266 }
2267 case Intrinsic::amdgcn_mbcnt_hi:
2268 case Intrinsic::amdgcn_mbcnt_lo: {
2269 // Wave64 mbcnt_lo returns at most 32 + src1. Otherwise these return at
2270 // most 31 + src1.
2271 Known.Zero.setBitsFrom(
2272 II->getIntrinsicID() == Intrinsic::amdgcn_mbcnt_lo ? 6 : 5);
2273 computeKnownBits(I->getOperand(1), Known2, Q, Depth + 1);
2274 Known = KnownBits::add(Known, Known2);
2275 break;
2276 }
2277 case Intrinsic::vscale: {
2278 if (!II->getParent() || !II->getFunction())
2279 break;
2280
2281 Known = getVScaleRange(II->getFunction(), BitWidth).toKnownBits();
2282 break;
2283 }
2284 }
2285 }
2286 break;
2287 }
2288 case Instruction::ShuffleVector: {
2289 if (auto *Splat = getSplatValue(I)) {
2290 computeKnownBits(Splat, Known, Q, Depth + 1);
2291 break;
2292 }
2293
2294 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
2295 // FIXME: Do we need to handle ConstantExpr involving shufflevectors?
2296 if (!Shuf) {
2297 Known.resetAll();
2298 return;
2299 }
2300 // For undef elements, we don't know anything about the common state of
2301 // the shuffle result.
2302 APInt DemandedLHS, DemandedRHS;
2303 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) {
2304 Known.resetAll();
2305 return;
2306 }
2307 Known.setAllConflict();
2308 if (!!DemandedLHS) {
2309 const Value *LHS = Shuf->getOperand(0);
2310 computeKnownBits(LHS, DemandedLHS, Known, Q, Depth + 1);
2311 // If we don't know any bits, early out.
2312 if (Known.isUnknown())
2313 break;
2314 }
2315 if (!!DemandedRHS) {
2316 const Value *RHS = Shuf->getOperand(1);
2317 computeKnownBits(RHS, DemandedRHS, Known2, Q, Depth + 1);
2318 Known = Known.intersectWith(Known2);
2319 }
2320 break;
2321 }
2322 case Instruction::InsertElement: {
2323 if (isa<ScalableVectorType>(I->getType())) {
2324 Known.resetAll();
2325 return;
2326 }
2327 const Value *Vec = I->getOperand(0);
2328 const Value *Elt = I->getOperand(1);
2329 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
2330 unsigned NumElts = DemandedElts.getBitWidth();
2331 APInt DemandedVecElts = DemandedElts;
2332 bool NeedsElt = true;
2333 // If we know the index we are inserting too, clear it from Vec check.
2334 if (CIdx && CIdx->getValue().ult(NumElts)) {
2335 DemandedVecElts.clearBit(CIdx->getZExtValue());
2336 NeedsElt = DemandedElts[CIdx->getZExtValue()];
2337 }
2338
2339 Known.setAllConflict();
2340 if (NeedsElt) {
2341 computeKnownBits(Elt, Known, Q, Depth + 1);
2342 // If we don't know any bits, early out.
2343 if (Known.isUnknown())
2344 break;
2345 }
2346
2347 if (!DemandedVecElts.isZero()) {
2348 computeKnownBits(Vec, DemandedVecElts, Known2, Q, Depth + 1);
2349 Known = Known.intersectWith(Known2);
2350 }
2351 break;
2352 }
2353 case Instruction::ExtractElement: {
2354 // Look through extract element. If the index is non-constant or
2355 // out-of-range demand all elements, otherwise just the extracted element.
2356 const Value *Vec = I->getOperand(0);
2357 const Value *Idx = I->getOperand(1);
2358 auto *CIdx = dyn_cast<ConstantInt>(Idx);
2359 if (isa<ScalableVectorType>(Vec->getType())) {
2360 // FIXME: there's probably *something* we can do with scalable vectors
2361 Known.resetAll();
2362 break;
2363 }
2364 unsigned NumElts = cast<FixedVectorType>(Vec->getType())->getNumElements();
2365 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
2366 if (CIdx && CIdx->getValue().ult(NumElts))
2367 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
2368 computeKnownBits(Vec, DemandedVecElts, Known, Q, Depth + 1);
2369 break;
2370 }
2371 case Instruction::ExtractValue:
2372 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
2374 if (EVI->getNumIndices() != 1) break;
2375 if (EVI->getIndices()[0] == 0) {
2376 switch (II->getIntrinsicID()) {
2377 default: break;
2378 case Intrinsic::uadd_with_overflow:
2379 case Intrinsic::sadd_with_overflow:
2381 true, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2382 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2383 break;
2384 case Intrinsic::usub_with_overflow:
2385 case Intrinsic::ssub_with_overflow:
2387 false, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2388 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2389 break;
2390 case Intrinsic::umul_with_overflow:
2391 case Intrinsic::smul_with_overflow:
2392 computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false,
2393 false, DemandedElts, Known, Known2, Q, Depth);
2394 break;
2395 }
2396 }
2397 }
2398 break;
2399 case Instruction::Freeze:
2400 if (isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
2401 Depth + 1))
2402 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2403 break;
2404 }
2405}
2406
2407/// Determine which bits of V are known to be either zero or one and return
2408/// them.
2409KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
2410 const SimplifyQuery &Q, unsigned Depth) {
2411 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2412 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);
2413 return Known;
2414}
2415
2416/// Determine which bits of V are known to be either zero or one and return
2417/// them.
2419 unsigned Depth) {
2420 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2421 computeKnownBits(V, Known, Q, Depth);
2422 return Known;
2423}
2424
2425/// Determine which bits of V are known to be either zero or one and return
2426/// them in the Known bit set.
2427///
2428/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that
2429/// we cannot optimize based on the assumption that it is zero without changing
2430/// it to be an explicit zero. If we don't change it to zero, other code could
2431/// optimized based on the contradictory assumption that it is non-zero.
2432/// Because instcombine aggressively folds operations with undef args anyway,
2433/// this won't lose us code quality.
2434///
2435/// This function is defined on values with integer type, values with pointer
2436/// type, and vectors of integers. In the case
2437/// where V is a vector, known zero, and known one values are the
2438/// same width as the vector element, and the bit is set only if it is true
2439/// for all of the demanded elements in the vector specified by DemandedElts.
2440void computeKnownBits(const Value *V, const APInt &DemandedElts,
2441 KnownBits &Known, const SimplifyQuery &Q,
2442 unsigned Depth) {
2443 if (!DemandedElts) {
2444 // No demanded elts, better to assume we don't know anything.
2445 Known.resetAll();
2446 return;
2447 }
2448
2449 assert(V && "No Value?");
2450 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2451
2452#ifndef NDEBUG
2453 Type *Ty = V->getType();
2454 unsigned BitWidth = Known.getBitWidth();
2455
2456 assert((Ty->isIntOrIntVectorTy(BitWidth) || Ty->isPtrOrPtrVectorTy()) &&
2457 "Not integer or pointer type!");
2458
2459 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
2460 assert(
2461 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
2462 "DemandedElt width should equal the fixed vector number of elements");
2463 } else {
2464 assert(DemandedElts == APInt(1, 1) &&
2465 "DemandedElt width should be 1 for scalars or scalable vectors");
2466 }
2467
2468 Type *ScalarTy = Ty->getScalarType();
2469 if (ScalarTy->isPointerTy()) {
2470 assert(BitWidth == Q.DL.getPointerTypeSizeInBits(ScalarTy) &&
2471 "V and Known should have same BitWidth");
2472 } else {
2473 assert(BitWidth == Q.DL.getTypeSizeInBits(ScalarTy) &&
2474 "V and Known should have same BitWidth");
2475 }
2476#endif
2477
2478 const APInt *C;
2479 if (match(V, m_APInt(C))) {
2480 // We know all of the bits for a scalar constant or a splat vector constant!
2481 Known = KnownBits::makeConstant(*C);
2482 return;
2483 }
2484 // Null and aggregate-zero are all-zeros.
2486 Known.setAllZero();
2487 return;
2488 }
2489 // Handle a constant vector by taking the intersection of the known bits of
2490 // each element.
2492 assert(!isa<ScalableVectorType>(V->getType()));
2493 // We know that CDV must be a vector of integers. Take the intersection of
2494 // each element.
2495 Known.setAllConflict();
2496 for (unsigned i = 0, e = CDV->getNumElements(); i != e; ++i) {
2497 if (!DemandedElts[i])
2498 continue;
2499 APInt Elt = CDV->getElementAsAPInt(i);
2500 Known.Zero &= ~Elt;
2501 Known.One &= Elt;
2502 }
2503 if (Known.hasConflict())
2504 Known.resetAll();
2505 return;
2506 }
2507
2508 if (const auto *CV = dyn_cast<ConstantVector>(V)) {
2509 assert(!isa<ScalableVectorType>(V->getType()));
2510 // We know that CV must be a vector of integers. Take the intersection of
2511 // each element.
2512 Known.setAllConflict();
2513 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
2514 if (!DemandedElts[i])
2515 continue;
2516 Constant *Element = CV->getAggregateElement(i);
2517 if (isa<PoisonValue>(Element))
2518 continue;
2519 auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
2520 if (!ElementCI) {
2521 Known.resetAll();
2522 return;
2523 }
2524 const APInt &Elt = ElementCI->getValue();
2525 Known.Zero &= ~Elt;
2526 Known.One &= Elt;
2527 }
2528 if (Known.hasConflict())
2529 Known.resetAll();
2530 return;
2531 }
2532
2533 // Start out not knowing anything.
2534 Known.resetAll();
2535
2536 // We can't imply anything about undefs.
2537 if (isa<UndefValue>(V))
2538 return;
2539
2540 // There's no point in looking through other users of ConstantData for
2541 // assumptions. Confirm that we've handled them all.
2542 assert(!isa<ConstantData>(V) && "Unhandled constant data!");
2543
2544 if (const auto *A = dyn_cast<Argument>(V))
2545 if (std::optional<ConstantRange> Range = A->getRange())
2546 Known = Range->toKnownBits();
2547
2548 // All recursive calls that increase depth must come after this.
2550 return;
2551
2552 // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
2553 // the bits of its aliasee.
2554 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
2555 if (!GA->isInterposable())
2556 computeKnownBits(GA->getAliasee(), Known, Q, Depth + 1);
2557 return;
2558 }
2559
2560 if (const Operator *I = dyn_cast<Operator>(V))
2561 computeKnownBitsFromOperator(I, DemandedElts, Known, Q, Depth);
2562 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2563 if (std::optional<ConstantRange> CR = GV->getAbsoluteSymbolRange())
2564 Known = CR->toKnownBits();
2565 }
2566
2567 // Aligned pointers have trailing zeros - refine Known.Zero set
2568 if (isa<PointerType>(V->getType())) {
2569 Align Alignment = V->getPointerAlignment(Q.DL);
2570 Known.Zero.setLowBits(Log2(Alignment));
2571 }
2572
2573 // computeKnownBitsFromContext strictly refines Known.
2574 // Therefore, we run them after computeKnownBitsFromOperator.
2575
2576 // Check whether we can determine known bits from context such as assumes.
2577 computeKnownBitsFromContext(V, Known, Q, Depth);
2578}
2579
2580/// Try to detect a recurrence that the value of the induction variable is
2581/// always a power of two (or zero).
2582static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero,
2583 SimplifyQuery &Q, unsigned Depth) {
2584 BinaryOperator *BO = nullptr;
2585 Value *Start = nullptr, *Step = nullptr;
2586 if (!matchSimpleRecurrence(PN, BO, Start, Step))
2587 return false;
2588
2589 // Initial value must be a power of two.
2590 for (const Use &U : PN->operands()) {
2591 if (U.get() == Start) {
2592 // Initial value comes from a different BB, need to adjust context
2593 // instruction for analysis.
2594 Q.CxtI = PN->getIncomingBlock(U)->getTerminator();
2595 if (!isKnownToBeAPowerOfTwo(Start, OrZero, Q, Depth))
2596 return false;
2597 }
2598 }
2599
2600 // Except for Mul, the induction variable must be on the left side of the
2601 // increment expression, otherwise its value can be arbitrary.
2602 if (BO->getOpcode() != Instruction::Mul && BO->getOperand(1) != Step)
2603 return false;
2604
2605 Q.CxtI = BO->getParent()->getTerminator();
2606 switch (BO->getOpcode()) {
2607 case Instruction::Mul:
2608 // Power of two is closed under multiplication.
2609 return (OrZero || Q.IIQ.hasNoUnsignedWrap(BO) ||
2610 Q.IIQ.hasNoSignedWrap(BO)) &&
2611 isKnownToBeAPowerOfTwo(Step, OrZero, Q, Depth);
2612 case Instruction::SDiv:
2613 // Start value must not be signmask for signed division, so simply being a
2614 // power of two is not sufficient, and it has to be a constant.
2615 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2616 return false;
2617 [[fallthrough]];
2618 case Instruction::UDiv:
2619 // Divisor must be a power of two.
2620 // If OrZero is false, cannot guarantee induction variable is non-zero after
2621 // division, same for Shr, unless it is exact division.
2622 return (OrZero || Q.IIQ.isExact(BO)) &&
2623 isKnownToBeAPowerOfTwo(Step, false, Q, Depth);
2624 case Instruction::Shl:
2625 return OrZero || Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO);
2626 case Instruction::AShr:
2627 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2628 return false;
2629 [[fallthrough]];
2630 case Instruction::LShr:
2631 return OrZero || Q.IIQ.isExact(BO);
2632 default:
2633 return false;
2634 }
2635}
2636
2637/// Return true if we can infer that \p V is known to be a power of 2 from
2638/// dominating condition \p Cond (e.g., ctpop(V) == 1).
2639static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero,
2640 const Value *Cond,
2641 bool CondIsTrue) {
2642 CmpPredicate Pred;
2643 const APInt *RHSC;
2645 m_APInt(RHSC))))
2646 return false;
2647 if (!CondIsTrue)
2648 Pred = ICmpInst::getInversePredicate(Pred);
2649 // ctpop(V) u< 2
2650 if (OrZero && Pred == ICmpInst::ICMP_ULT && *RHSC == 2)
2651 return true;
2652 // ctpop(V) == 1
2653 return Pred == ICmpInst::ICMP_EQ && *RHSC == 1;
2654}
2655
2656/// Return true if the given value is known to have exactly one
2657/// bit set when defined. For vectors return true if every element is known to
2658/// be a power of two when defined. Supports values with integer or pointer
2659/// types and vectors of integers.
2660bool llvm::isKnownToBeAPowerOfTwo(const Value *V, bool OrZero,
2661 const SimplifyQuery &Q, unsigned Depth) {
2662 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2663
2664 if (isa<Constant>(V))
2665 return OrZero ? match(V, m_Power2OrZero()) : match(V, m_Power2());
2666
2667 // i1 is by definition a power of 2 or zero.
2668 if (OrZero && V->getType()->getScalarSizeInBits() == 1)
2669 return true;
2670
2671 // Try to infer from assumptions.
2672 if (Q.AC && Q.CxtI) {
2673 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
2674 if (!AssumeVH)
2675 continue;
2676 CallInst *I = cast<CallInst>(AssumeVH);
2677 if (isImpliedToBeAPowerOfTwoFromCond(V, OrZero, I->getArgOperand(0),
2678 /*CondIsTrue=*/true) &&
2680 return true;
2681 }
2682 }
2683
2684 // Handle dominating conditions.
2685 if (Q.DC && Q.CxtI && Q.DT) {
2686 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
2687 Value *Cond = BI->getCondition();
2688
2689 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
2691 /*CondIsTrue=*/true) &&
2692 Q.DT->dominates(Edge0, Q.CxtI->getParent()))
2693 return true;
2694
2695 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
2697 /*CondIsTrue=*/false) &&
2698 Q.DT->dominates(Edge1, Q.CxtI->getParent()))
2699 return true;
2700 }
2701 }
2702
2703 auto *I = dyn_cast<Instruction>(V);
2704 if (!I)
2705 return false;
2706
2707 if (Q.CxtI && match(V, m_VScale())) {
2708 const Function *F = Q.CxtI->getFunction();
2709 // The vscale_range indicates vscale is a power-of-two.
2710 return F->hasFnAttribute(Attribute::VScaleRange);
2711 }
2712
2713 // 1 << X is clearly a power of two if the one is not shifted off the end. If
2714 // it is shifted off the end then the result is undefined.
2715 if (match(I, m_Shl(m_One(), m_Value())))
2716 return true;
2717
2718 // (signmask) >>l X is clearly a power of two if the one is not shifted off
2719 // the bottom. If it is shifted off the bottom then the result is undefined.
2720 if (match(I, m_LShr(m_SignMask(), m_Value())))
2721 return true;
2722
2723 // The remaining tests are all recursive, so bail out if we hit the limit.
2725 return false;
2726
2727 switch (I->getOpcode()) {
2728 case Instruction::ZExt:
2729 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2730 case Instruction::Trunc:
2731 return OrZero && isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2732 case Instruction::Shl:
2733 if (OrZero || Q.IIQ.hasNoUnsignedWrap(I) || Q.IIQ.hasNoSignedWrap(I))
2734 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2735 return false;
2736 case Instruction::LShr:
2737 if (OrZero || Q.IIQ.isExact(cast<BinaryOperator>(I)))
2738 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2739 return false;
2740 case Instruction::UDiv:
2742 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2743 return false;
2744 case Instruction::Mul:
2745 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
2746 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth) &&
2747 (OrZero || isKnownNonZero(I, Q, Depth));
2748 case Instruction::And:
2749 // A power of two and'd with anything is a power of two or zero.
2750 if (OrZero &&
2751 (isKnownToBeAPowerOfTwo(I->getOperand(1), /*OrZero*/ true, Q, Depth) ||
2752 isKnownToBeAPowerOfTwo(I->getOperand(0), /*OrZero*/ true, Q, Depth)))
2753 return true;
2754 // X & (-X) is always a power of two or zero.
2755 if (match(I->getOperand(0), m_Neg(m_Specific(I->getOperand(1)))) ||
2756 match(I->getOperand(1), m_Neg(m_Specific(I->getOperand(0)))))
2757 return OrZero || isKnownNonZero(I->getOperand(0), Q, Depth);
2758 return false;
2759 case Instruction::Add: {
2760 // Adding a power-of-two or zero to the same power-of-two or zero yields
2761 // either the original power-of-two, a larger power-of-two or zero.
2763 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) ||
2764 Q.IIQ.hasNoSignedWrap(VOBO)) {
2765 if (match(I->getOperand(0),
2766 m_c_And(m_Specific(I->getOperand(1)), m_Value())) &&
2767 isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth))
2768 return true;
2769 if (match(I->getOperand(1),
2770 m_c_And(m_Specific(I->getOperand(0)), m_Value())) &&
2771 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth))
2772 return true;
2773
2774 unsigned BitWidth = V->getType()->getScalarSizeInBits();
2775 KnownBits LHSBits(BitWidth);
2776 computeKnownBits(I->getOperand(0), LHSBits, Q, Depth);
2777
2778 KnownBits RHSBits(BitWidth);
2779 computeKnownBits(I->getOperand(1), RHSBits, Q, Depth);
2780 // If i8 V is a power of two or zero:
2781 // ZeroBits: 1 1 1 0 1 1 1 1
2782 // ~ZeroBits: 0 0 0 1 0 0 0 0
2783 if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2())
2784 // If OrZero isn't set, we cannot give back a zero result.
2785 // Make sure either the LHS or RHS has a bit set.
2786 if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue())
2787 return true;
2788 }
2789
2790 // LShr(UINT_MAX, Y) + 1 is a power of two (if add is nuw) or zero.
2791 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO))
2792 if (match(I, m_Add(m_LShr(m_AllOnes(), m_Value()), m_One())))
2793 return true;
2794 return false;
2795 }
2796 case Instruction::Select:
2797 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
2798 isKnownToBeAPowerOfTwo(I->getOperand(2), OrZero, Q, Depth);
2799 case Instruction::PHI: {
2800 // A PHI node is power of two if all incoming values are power of two, or if
2801 // it is an induction variable where in each step its value is a power of
2802 // two.
2803 auto *PN = cast<PHINode>(I);
2805
2806 // Check if it is an induction variable and always power of two.
2807 if (isPowerOfTwoRecurrence(PN, OrZero, RecQ, Depth))
2808 return true;
2809
2810 // Recursively check all incoming values. Limit recursion to 2 levels, so
2811 // that search complexity is limited to number of operands^2.
2812 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
2813 return llvm::all_of(PN->operands(), [&](const Use &U) {
2814 // Value is power of 2 if it is coming from PHI node itself by induction.
2815 if (U.get() == PN)
2816 return true;
2817
2818 // Change the context instruction to the incoming block where it is
2819 // evaluated.
2820 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
2821 return isKnownToBeAPowerOfTwo(U.get(), OrZero, RecQ, NewDepth);
2822 });
2823 }
2824 case Instruction::Invoke:
2825 case Instruction::Call: {
2826 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
2827 switch (II->getIntrinsicID()) {
2828 case Intrinsic::umax:
2829 case Intrinsic::smax:
2830 case Intrinsic::umin:
2831 case Intrinsic::smin:
2832 return isKnownToBeAPowerOfTwo(II->getArgOperand(1), OrZero, Q, Depth) &&
2833 isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2834 // bswap/bitreverse just move around bits, but don't change any 1s/0s
2835 // thus dont change pow2/non-pow2 status.
2836 case Intrinsic::bitreverse:
2837 case Intrinsic::bswap:
2838 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2839 case Intrinsic::fshr:
2840 case Intrinsic::fshl:
2841 // If Op0 == Op1, this is a rotate. is_pow2(rotate(x, y)) == is_pow2(x)
2842 if (II->getArgOperand(0) == II->getArgOperand(1))
2843 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2844 break;
2845 default:
2846 break;
2847 }
2848 }
2849 return false;
2850 }
2851 default:
2852 return false;
2853 }
2854}
2855
2856/// Test whether a GEP's result is known to be non-null.
2857///
2858/// Uses properties inherent in a GEP to try to determine whether it is known
2859/// to be non-null.
2860///
2861/// Currently this routine does not support vector GEPs.
2862static bool isGEPKnownNonNull(const GEPOperator *GEP, const SimplifyQuery &Q,
2863 unsigned Depth) {
2864 const Function *F = nullptr;
2865 if (const Instruction *I = dyn_cast<Instruction>(GEP))
2866 F = I->getFunction();
2867
2868 // If the gep is nuw or inbounds with invalid null pointer, then the GEP
2869 // may be null iff the base pointer is null and the offset is zero.
2870 if (!GEP->hasNoUnsignedWrap() &&
2871 !(GEP->isInBounds() &&
2872 !NullPointerIsDefined(F, GEP->getPointerAddressSpace())))
2873 return false;
2874
2875 // FIXME: Support vector-GEPs.
2876 assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP");
2877
2878 // If the base pointer is non-null, we cannot walk to a null address with an
2879 // inbounds GEP in address space zero.
2880 if (isKnownNonZero(GEP->getPointerOperand(), Q, Depth))
2881 return true;
2882
2883 // Walk the GEP operands and see if any operand introduces a non-zero offset.
2884 // If so, then the GEP cannot produce a null pointer, as doing so would
2885 // inherently violate the inbounds contract within address space zero.
2887 GTI != GTE; ++GTI) {
2888 // Struct types are easy -- they must always be indexed by a constant.
2889 if (StructType *STy = GTI.getStructTypeOrNull()) {
2890 ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
2891 unsigned ElementIdx = OpC->getZExtValue();
2892 const StructLayout *SL = Q.DL.getStructLayout(STy);
2893 uint64_t ElementOffset = SL->getElementOffset(ElementIdx);
2894 if (ElementOffset > 0)
2895 return true;
2896 continue;
2897 }
2898
2899 // If we have a zero-sized type, the index doesn't matter. Keep looping.
2900 if (GTI.getSequentialElementStride(Q.DL).isZero())
2901 continue;
2902
2903 // Fast path the constant operand case both for efficiency and so we don't
2904 // increment Depth when just zipping down an all-constant GEP.
2905 if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
2906 if (!OpC->isZero())
2907 return true;
2908 continue;
2909 }
2910
2911 // We post-increment Depth here because while isKnownNonZero increments it
2912 // as well, when we pop back up that increment won't persist. We don't want
2913 // to recurse 10k times just because we have 10k GEP operands. We don't
2914 // bail completely out because we want to handle constant GEPs regardless
2915 // of depth.
2917 continue;
2918
2919 if (isKnownNonZero(GTI.getOperand(), Q, Depth))
2920 return true;
2921 }
2922
2923 return false;
2924}
2925
2927 const Instruction *CtxI,
2928 const DominatorTree *DT) {
2929 assert(!isa<Constant>(V) && "Called for constant?");
2930
2931 if (!CtxI || !DT)
2932 return false;
2933
2934 unsigned NumUsesExplored = 0;
2935 for (auto &U : V->uses()) {
2936 // Avoid massive lists
2937 if (NumUsesExplored >= DomConditionsMaxUses)
2938 break;
2939 NumUsesExplored++;
2940
2941 const Instruction *UI = cast<Instruction>(U.getUser());
2942 // If the value is used as an argument to a call or invoke, then argument
2943 // attributes may provide an answer about null-ness.
2944 if (V->getType()->isPointerTy()) {
2945 if (const auto *CB = dyn_cast<CallBase>(UI)) {
2946 if (CB->isArgOperand(&U) &&
2947 CB->paramHasNonNullAttr(CB->getArgOperandNo(&U),
2948 /*AllowUndefOrPoison=*/false) &&
2949 DT->dominates(CB, CtxI))
2950 return true;
2951 }
2952 }
2953
2954 // If the value is used as a load/store, then the pointer must be non null.
2955 if (V == getLoadStorePointerOperand(UI)) {
2958 DT->dominates(UI, CtxI))
2959 return true;
2960 }
2961
2962 if ((match(UI, m_IDiv(m_Value(), m_Specific(V))) ||
2963 match(UI, m_IRem(m_Value(), m_Specific(V)))) &&
2964 isValidAssumeForContext(UI, CtxI, DT))
2965 return true;
2966
2967 // Consider only compare instructions uniquely controlling a branch
2968 Value *RHS;
2969 CmpPredicate Pred;
2970 if (!match(UI, m_c_ICmp(Pred, m_Specific(V), m_Value(RHS))))
2971 continue;
2972
2973 bool NonNullIfTrue;
2974 if (cmpExcludesZero(Pred, RHS))
2975 NonNullIfTrue = true;
2977 NonNullIfTrue = false;
2978 else
2979 continue;
2980
2983 for (const auto *CmpU : UI->users()) {
2984 assert(WorkList.empty() && "Should be!");
2985 if (Visited.insert(CmpU).second)
2986 WorkList.push_back(CmpU);
2987
2988 while (!WorkList.empty()) {
2989 auto *Curr = WorkList.pop_back_val();
2990
2991 // If a user is an AND, add all its users to the work list. We only
2992 // propagate "pred != null" condition through AND because it is only
2993 // correct to assume that all conditions of AND are met in true branch.
2994 // TODO: Support similar logic of OR and EQ predicate?
2995 if (NonNullIfTrue)
2996 if (match(Curr, m_LogicalAnd(m_Value(), m_Value()))) {
2997 for (const auto *CurrU : Curr->users())
2998 if (Visited.insert(CurrU).second)
2999 WorkList.push_back(CurrU);
3000 continue;
3001 }
3002
3003 if (const CondBrInst *BI = dyn_cast<CondBrInst>(Curr)) {
3004 BasicBlock *NonNullSuccessor =
3005 BI->getSuccessor(NonNullIfTrue ? 0 : 1);
3006 BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor);
3007 if (DT->dominates(Edge, CtxI->getParent()))
3008 return true;
3009 } else if (NonNullIfTrue && isGuard(Curr) &&
3010 DT->dominates(cast<Instruction>(Curr), CtxI)) {
3011 return true;
3012 }
3013 }
3014 }
3015 }
3016
3017 return false;
3018}
3019
3020/// Does the 'Range' metadata (which must be a valid MD_range operand list)
3021/// ensure that the value it's attached to is never Value? 'RangeType' is
3022/// is the type of the value described by the range.
3023static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) {
3024 const unsigned NumRanges = Ranges->getNumOperands() / 2;
3025 assert(NumRanges >= 1);
3026 for (unsigned i = 0; i < NumRanges; ++i) {
3028 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
3030 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
3031 ConstantRange Range(Lower->getValue(), Upper->getValue());
3032 if (Range.contains(Value))
3033 return false;
3034 }
3035 return true;
3036}
3037
3038/// Try to detect a recurrence that monotonically increases/decreases from a
3039/// non-zero starting value. These are common as induction variables.
3040static bool isNonZeroRecurrence(const PHINode *PN) {
3041 BinaryOperator *BO = nullptr;
3042 Value *Start = nullptr, *Step = nullptr;
3043 const APInt *StartC, *StepC;
3044 if (!matchSimpleRecurrence(PN, BO, Start, Step) ||
3045 !match(Start, m_APInt(StartC)) || StartC->isZero())
3046 return false;
3047
3048 switch (BO->getOpcode()) {
3049 case Instruction::Add:
3050 // Starting from non-zero and stepping away from zero can never wrap back
3051 // to zero.
3052 return BO->hasNoUnsignedWrap() ||
3053 (BO->hasNoSignedWrap() && match(Step, m_APInt(StepC)) &&
3054 StartC->isNegative() == StepC->isNegative());
3055 case Instruction::Mul:
3056 return (BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap()) &&
3057 match(Step, m_APInt(StepC)) && !StepC->isZero();
3058 case Instruction::Shl:
3059 return BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap();
3060 case Instruction::AShr:
3061 case Instruction::LShr:
3062 return BO->isExact();
3063 default:
3064 return false;
3065 }
3066}
3067
3068static bool matchOpWithOpEqZero(Value *Op0, Value *Op1) {
3070 m_Specific(Op1), m_Zero()))) ||
3072 m_Specific(Op0), m_Zero())));
3073}
3074
3075static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q,
3076 unsigned BitWidth, Value *X, Value *Y, bool NSW,
3077 bool NUW, unsigned Depth) {
3078 // (X + (X != 0)) is non zero
3079 if (matchOpWithOpEqZero(X, Y))
3080 return true;
3081
3082 if (NUW)
3083 return isKnownNonZero(Y, DemandedElts, Q, Depth) ||
3084 isKnownNonZero(X, DemandedElts, Q, Depth);
3085
3086 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
3087 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
3088
3089 // If X and Y are both non-negative (as signed values) then their sum is not
3090 // zero unless both X and Y are zero.
3091 if (XKnown.isNonNegative() && YKnown.isNonNegative())
3092 if (isKnownNonZero(Y, DemandedElts, Q, Depth) ||
3093 isKnownNonZero(X, DemandedElts, Q, Depth))
3094 return true;
3095
3096 // If X and Y are both negative (as signed values) then their sum is not
3097 // zero unless both X and Y equal INT_MIN.
3098 if (XKnown.isNegative() && YKnown.isNegative()) {
3100 // The sign bit of X is set. If some other bit is set then X is not equal
3101 // to INT_MIN.
3102 if (XKnown.One.intersects(Mask))
3103 return true;
3104 // The sign bit of Y is set. If some other bit is set then Y is not equal
3105 // to INT_MIN.
3106 if (YKnown.One.intersects(Mask))
3107 return true;
3108 }
3109
3110 // The sum of a non-negative number and a power of two is not zero.
3111 if (XKnown.isNonNegative() &&
3112 isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Q, Depth))
3113 return true;
3114 if (YKnown.isNonNegative() &&
3115 isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Q, Depth))
3116 return true;
3117
3118 return KnownBits::add(XKnown, YKnown, NSW, NUW).isNonZero();
3119}
3120
3121static bool isNonZeroSub(const APInt &DemandedElts, const SimplifyQuery &Q,
3122 unsigned BitWidth, Value *X, Value *Y,
3123 unsigned Depth) {
3124 // (X - (X != 0)) is non zero
3125 // ((X != 0) - X) is non zero
3126 if (matchOpWithOpEqZero(X, Y))
3127 return true;
3128
3129 // TODO: Move this case into isKnownNonEqual().
3130 if (auto *C = dyn_cast<Constant>(X))
3131 if (C->isNullValue() && isKnownNonZero(Y, DemandedElts, Q, Depth))
3132 return true;
3133
3134 return ::isKnownNonEqual(X, Y, DemandedElts, Q, Depth);
3135}
3136
3137static bool isNonZeroMul(const APInt &DemandedElts, const SimplifyQuery &Q,
3138 unsigned BitWidth, Value *X, Value *Y, bool NSW,
3139 bool NUW, unsigned Depth) {
3140 // If X and Y are non-zero then so is X * Y as long as the multiplication
3141 // does not overflow.
3142 if (NSW || NUW)
3143 return isKnownNonZero(X, DemandedElts, Q, Depth) &&
3144 isKnownNonZero(Y, DemandedElts, Q, Depth);
3145
3146 // If either X or Y is odd, then if the other is non-zero the result can't
3147 // be zero.
3148 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
3149 if (XKnown.One[0])
3150 return isKnownNonZero(Y, DemandedElts, Q, Depth);
3151
3152 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
3153 if (YKnown.One[0])
3154 return XKnown.isNonZero() || isKnownNonZero(X, DemandedElts, Q, Depth);
3155
3156 // If there exists any subset of X (sX) and subset of Y (sY) s.t sX * sY is
3157 // non-zero, then X * Y is non-zero. We can find sX and sY by just taking
3158 // the lowest known One of X and Y. If they are non-zero, the result
3159 // must be non-zero. We can check if LSB(X) * LSB(Y) != 0 by doing
3160 // X.CountLeadingZeros + Y.CountLeadingZeros < BitWidth.
3161 return (XKnown.countMaxTrailingZeros() + YKnown.countMaxTrailingZeros()) <
3162 BitWidth;
3163}
3164
3165static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts,
3166 const SimplifyQuery &Q, const KnownBits &KnownVal,
3167 unsigned Depth) {
3168 auto ShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
3169 switch (I->getOpcode()) {
3170 case Instruction::Shl:
3171 return Lhs.shl(Rhs);
3172 case Instruction::LShr:
3173 return Lhs.lshr(Rhs);
3174 case Instruction::AShr:
3175 return Lhs.ashr(Rhs);
3176 default:
3177 llvm_unreachable("Unknown Shift Opcode");
3178 }
3179 };
3180
3181 auto InvShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
3182 switch (I->getOpcode()) {
3183 case Instruction::Shl:
3184 return Lhs.lshr(Rhs);
3185 case Instruction::LShr:
3186 case Instruction::AShr:
3187 return Lhs.shl(Rhs);
3188 default:
3189 llvm_unreachable("Unknown Shift Opcode");
3190 }
3191 };
3192
3193 if (KnownVal.isUnknown())
3194 return false;
3195
3196 KnownBits KnownCnt =
3197 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
3198 APInt MaxShift = KnownCnt.getMaxValue();
3199 unsigned NumBits = KnownVal.getBitWidth();
3200 if (MaxShift.uge(NumBits))
3201 return false;
3202
3203 if (!ShiftOp(KnownVal.One, MaxShift).isZero())
3204 return true;
3205
3206 // If all of the bits shifted out are known to be zero, and Val is known
3207 // non-zero then at least one non-zero bit must remain.
3208 if (InvShiftOp(KnownVal.Zero, NumBits - MaxShift)
3209 .eq(InvShiftOp(APInt::getAllOnes(NumBits), NumBits - MaxShift)) &&
3210 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth))
3211 return true;
3212
3213 return false;
3214}
3215
3217 const APInt &DemandedElts,
3218 const SimplifyQuery &Q, unsigned Depth) {
3219 unsigned BitWidth = getBitWidth(I->getType()->getScalarType(), Q.DL);
3220 switch (I->getOpcode()) {
3221 case Instruction::Alloca:
3222 // Alloca never returns null, malloc might.
3223 return I->getType()->getPointerAddressSpace() == 0;
3224 case Instruction::GetElementPtr:
3225 if (I->getType()->isPointerTy())
3227 break;
3228 case Instruction::BitCast: {
3229 // We need to be a bit careful here. We can only peek through the bitcast
3230 // if the scalar size of elements in the operand are smaller than and a
3231 // multiple of the size they are casting too. Take three cases:
3232 //
3233 // 1) Unsafe:
3234 // bitcast <2 x i16> %NonZero to <4 x i8>
3235 //
3236 // %NonZero can have 2 non-zero i16 elements, but isKnownNonZero on a
3237 // <4 x i8> requires that all 4 i8 elements be non-zero which isn't
3238 // guranteed (imagine just sign bit set in the 2 i16 elements).
3239 //
3240 // 2) Unsafe:
3241 // bitcast <4 x i3> %NonZero to <3 x i4>
3242 //
3243 // Even though the scalar size of the src (`i3`) is smaller than the
3244 // scalar size of the dst `i4`, because `i3` is not a multiple of `i4`
3245 // its possible for the `3 x i4` elements to be zero because there are
3246 // some elements in the destination that don't contain any full src
3247 // element.
3248 //
3249 // 3) Safe:
3250 // bitcast <4 x i8> %NonZero to <2 x i16>
3251 //
3252 // This is always safe as non-zero in the 4 i8 elements implies
3253 // non-zero in the combination of any two adjacent ones. Since i8 is a
3254 // multiple of i16, each i16 is guranteed to have 2 full i8 elements.
3255 // This all implies the 2 i16 elements are non-zero.
3256 Type *FromTy = I->getOperand(0)->getType();
3257 if ((FromTy->isIntOrIntVectorTy() || FromTy->isPtrOrPtrVectorTy()) &&
3258 (BitWidth % getBitWidth(FromTy->getScalarType(), Q.DL)) == 0)
3259 return isKnownNonZero(I->getOperand(0), Q, Depth);
3260 } break;
3261 case Instruction::IntToPtr:
3262 // Note that we have to take special care to avoid looking through
3263 // truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well
3264 // as casts that can alter the value, e.g., AddrSpaceCasts.
3265 if (!isa<ScalableVectorType>(I->getType()) &&
3266 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
3267 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
3268 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3269 break;
3270 case Instruction::PtrToAddr:
3271 // isKnownNonZero() for pointers refers to the address bits being non-zero,
3272 // so we can directly forward.
3273 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3274 case Instruction::PtrToInt:
3275 // For inttoptr, make sure the result size is >= the address size. If the
3276 // address is non-zero, any larger value is also non-zero.
3277 if (Q.DL.getAddressSizeInBits(I->getOperand(0)->getType()) <=
3278 I->getType()->getScalarSizeInBits())
3279 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3280 break;
3281 case Instruction::Trunc:
3282 // nuw/nsw trunc preserves zero/non-zero status of input.
3283 if (auto *TI = dyn_cast<TruncInst>(I))
3284 if (TI->hasNoSignedWrap() || TI->hasNoUnsignedWrap())
3285 return isKnownNonZero(TI->getOperand(0), DemandedElts, Q, Depth);
3286 break;
3287
3288 // Iff x - y != 0, then x ^ y != 0
3289 // Therefore we can do the same exact checks
3290 case Instruction::Xor:
3291 case Instruction::Sub:
3292 return isNonZeroSub(DemandedElts, Q, BitWidth, I->getOperand(0),
3293 I->getOperand(1), Depth);
3294 case Instruction::Or:
3295 // (X | (X != 0)) is non zero
3296 if (matchOpWithOpEqZero(I->getOperand(0), I->getOperand(1)))
3297 return true;
3298 // X | Y != 0 if X != Y.
3299 if (isKnownNonEqual(I->getOperand(0), I->getOperand(1), DemandedElts, Q,
3300 Depth))
3301 return true;
3302 // X | Y != 0 if X != 0 or Y != 0.
3303 return isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth) ||
3304 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3305 case Instruction::SExt:
3306 case Instruction::ZExt:
3307 // ext X != 0 if X != 0.
3308 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3309
3310 case Instruction::Shl: {
3311 // shl nsw/nuw can't remove any non-zero bits.
3313 if (Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO))
3314 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3315
3316 // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
3317 // if the lowest bit is shifted off the end.
3318 KnownBits Known(BitWidth);
3319 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth);
3320 if (Known.One[0])
3321 return true;
3322
3323 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3324 }
3325 case Instruction::LShr:
3326 case Instruction::AShr: {
3327 // shr exact can only shift out zero bits.
3329 if (BO->isExact())
3330 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3331
3332 // shr X, Y != 0 if X is negative. Note that the value of the shift is not
3333 // defined if the sign bit is shifted off the end.
3334 KnownBits Known =
3335 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3336 if (Known.isNegative())
3337 return true;
3338
3339 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3340 }
3341 case Instruction::UDiv:
3342 case Instruction::SDiv: {
3343 // X / Y
3344 // div exact can only produce a zero if the dividend is zero.
3345 if (cast<PossiblyExactOperator>(I)->isExact())
3346 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3347
3348 KnownBits XKnown =
3349 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3350 // If X is fully unknown we won't be able to figure anything out so don't
3351 // both computing knownbits for Y.
3352 if (XKnown.isUnknown())
3353 return false;
3354
3355 KnownBits YKnown =
3356 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
3357 if (I->getOpcode() == Instruction::SDiv) {
3358 // For signed division need to compare abs value of the operands.
3359 XKnown = XKnown.abs(/*IntMinIsPoison*/ false);
3360 YKnown = YKnown.abs(/*IntMinIsPoison*/ false);
3361 }
3362 // If X u>= Y then div is non zero (0/0 is UB).
3363 std::optional<bool> XUgeY = KnownBits::uge(XKnown, YKnown);
3364 // If X is total unknown or X u< Y we won't be able to prove non-zero
3365 // with compute known bits so just return early.
3366 return XUgeY && *XUgeY;
3367 }
3368 case Instruction::Add: {
3369 // X + Y.
3370
3371 // If Add has nuw wrap flag, then if either X or Y is non-zero the result is
3372 // non-zero.
3374 return isNonZeroAdd(DemandedElts, Q, BitWidth, I->getOperand(0),
3375 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3376 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3377 }
3378 case Instruction::Mul: {
3380 return isNonZeroMul(DemandedElts, Q, BitWidth, I->getOperand(0),
3381 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3382 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3383 }
3384 case Instruction::Select: {
3385 // (C ? X : Y) != 0 if X != 0 and Y != 0.
3386
3387 // First check if the arm is non-zero using `isKnownNonZero`. If that fails,
3388 // then see if the select condition implies the arm is non-zero. For example
3389 // (X != 0 ? X : Y), we know the true arm is non-zero as the `X` "return" is
3390 // dominated by `X != 0`.
3391 auto SelectArmIsNonZero = [&](bool IsTrueArm) {
3392 Value *Op;
3393 Op = IsTrueArm ? I->getOperand(1) : I->getOperand(2);
3394 // Op is trivially non-zero.
3395 if (isKnownNonZero(Op, DemandedElts, Q, Depth))
3396 return true;
3397
3398 // The condition of the select dominates the true/false arm. Check if the
3399 // condition implies that a given arm is non-zero.
3400 Value *X;
3401 CmpPredicate Pred;
3402 if (!match(I->getOperand(0), m_c_ICmp(Pred, m_Specific(Op), m_Value(X))))
3403 return false;
3404
3405 if (!IsTrueArm)
3406 Pred = ICmpInst::getInversePredicate(Pred);
3407
3408 return cmpExcludesZero(Pred, X);
3409 };
3410
3411 if (SelectArmIsNonZero(/* IsTrueArm */ true) &&
3412 SelectArmIsNonZero(/* IsTrueArm */ false))
3413 return true;
3414 break;
3415 }
3416 case Instruction::PHI: {
3417 auto *PN = cast<PHINode>(I);
3419 return true;
3420
3421 // Check if all incoming values are non-zero using recursion.
3423 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
3424 return llvm::all_of(PN->operands(), [&](const Use &U) {
3425 if (U.get() == PN)
3426 return true;
3427 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
3428 // Check if the branch on the phi excludes zero.
3429 CmpPredicate Pred;
3430 Value *X;
3431 BasicBlock *TrueSucc, *FalseSucc;
3432 if (match(RecQ.CxtI,
3433 m_Br(m_c_ICmp(Pred, m_Specific(U.get()), m_Value(X)),
3434 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
3435 // Check for cases of duplicate successors.
3436 if ((TrueSucc == PN->getParent()) != (FalseSucc == PN->getParent())) {
3437 // If we're using the false successor, invert the predicate.
3438 if (FalseSucc == PN->getParent())
3439 Pred = CmpInst::getInversePredicate(Pred);
3440 if (cmpExcludesZero(Pred, X))
3441 return true;
3442 }
3443 }
3444 // Finally recurse on the edge and check it directly.
3445 return isKnownNonZero(U.get(), DemandedElts, RecQ, NewDepth);
3446 });
3447 }
3448 case Instruction::InsertElement: {
3449 if (isa<ScalableVectorType>(I->getType()))
3450 break;
3451
3452 const Value *Vec = I->getOperand(0);
3453 const Value *Elt = I->getOperand(1);
3454 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
3455
3456 unsigned NumElts = DemandedElts.getBitWidth();
3457 APInt DemandedVecElts = DemandedElts;
3458 bool SkipElt = false;
3459 // If we know the index we are inserting too, clear it from Vec check.
3460 if (CIdx && CIdx->getValue().ult(NumElts)) {
3461 DemandedVecElts.clearBit(CIdx->getZExtValue());
3462 SkipElt = !DemandedElts[CIdx->getZExtValue()];
3463 }
3464
3465 // Result is zero if Elt is non-zero and rest of the demanded elts in Vec
3466 // are non-zero.
3467 return (SkipElt || isKnownNonZero(Elt, Q, Depth)) &&
3468 (DemandedVecElts.isZero() ||
3469 isKnownNonZero(Vec, DemandedVecElts, Q, Depth));
3470 }
3471 case Instruction::ExtractElement:
3472 if (const auto *EEI = dyn_cast<ExtractElementInst>(I)) {
3473 const Value *Vec = EEI->getVectorOperand();
3474 const Value *Idx = EEI->getIndexOperand();
3475 auto *CIdx = dyn_cast<ConstantInt>(Idx);
3476 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
3477 unsigned NumElts = VecTy->getNumElements();
3478 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
3479 if (CIdx && CIdx->getValue().ult(NumElts))
3480 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
3481 return isKnownNonZero(Vec, DemandedVecElts, Q, Depth);
3482 }
3483 }
3484 break;
3485 case Instruction::ShuffleVector: {
3486 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
3487 if (!Shuf)
3488 break;
3489 APInt DemandedLHS, DemandedRHS;
3490 // For undef elements, we don't know anything about the common state of
3491 // the shuffle result.
3492 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
3493 break;
3494 // If demanded elements for both vecs are non-zero, the shuffle is non-zero.
3495 return (DemandedRHS.isZero() ||
3496 isKnownNonZero(Shuf->getOperand(1), DemandedRHS, Q, Depth)) &&
3497 (DemandedLHS.isZero() ||
3498 isKnownNonZero(Shuf->getOperand(0), DemandedLHS, Q, Depth));
3499 }
3500 case Instruction::Freeze:
3501 return isKnownNonZero(I->getOperand(0), Q, Depth) &&
3502 isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
3503 Depth);
3504 case Instruction::Load: {
3505 auto *LI = cast<LoadInst>(I);
3506 // A Load tagged with nonnull or dereferenceable with null pointer undefined
3507 // is never null.
3508 if (auto *PtrT = dyn_cast<PointerType>(I->getType())) {
3509 if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull) ||
3510 (Q.IIQ.getMetadata(LI, LLVMContext::MD_dereferenceable) &&
3511 !NullPointerIsDefined(LI->getFunction(), PtrT->getAddressSpace())))
3512 return true;
3513 } else if (MDNode *Ranges = Q.IIQ.getMetadata(LI, LLVMContext::MD_range)) {
3515 }
3516
3517 // No need to fall through to computeKnownBits as range metadata is already
3518 // handled in isKnownNonZero.
3519 return false;
3520 }
3521 case Instruction::ExtractValue: {
3522 const WithOverflowInst *WO;
3524 switch (WO->getBinaryOp()) {
3525 default:
3526 break;
3527 case Instruction::Add:
3528 return isNonZeroAdd(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3529 WO->getArgOperand(1),
3530 /*NSW=*/false,
3531 /*NUW=*/false, Depth);
3532 case Instruction::Sub:
3533 return isNonZeroSub(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3534 WO->getArgOperand(1), Depth);
3535 case Instruction::Mul:
3536 return isNonZeroMul(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3537 WO->getArgOperand(1),
3538 /*NSW=*/false, /*NUW=*/false, Depth);
3539 break;
3540 }
3541 }
3542 break;
3543 }
3544 case Instruction::Call:
3545 case Instruction::Invoke: {
3546 const auto *Call = cast<CallBase>(I);
3547 if (I->getType()->isPointerTy()) {
3548 if (Call->isReturnNonNull())
3549 return true;
3550 if (const auto *RP = getArgumentAliasingToReturnedPointer(Call, true))
3551 return isKnownNonZero(RP, Q, Depth);
3552 } else {
3553 if (MDNode *Ranges = Q.IIQ.getMetadata(Call, LLVMContext::MD_range))
3555 if (std::optional<ConstantRange> Range = Call->getRange()) {
3556 const APInt ZeroValue(Range->getBitWidth(), 0);
3557 if (!Range->contains(ZeroValue))
3558 return true;
3559 }
3560 if (const Value *RV = Call->getReturnedArgOperand())
3561 if (RV->getType() == I->getType() && isKnownNonZero(RV, Q, Depth))
3562 return true;
3563 }
3564
3565 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
3566 switch (II->getIntrinsicID()) {
3567 case Intrinsic::sshl_sat:
3568 case Intrinsic::ushl_sat:
3569 case Intrinsic::abs:
3570 case Intrinsic::bitreverse:
3571 case Intrinsic::bswap:
3572 case Intrinsic::ctpop:
3573 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3574 // NB: We don't do usub_sat here as in any case we can prove its
3575 // non-zero, we will fold it to `sub nuw` in InstCombine.
3576 case Intrinsic::ssub_sat:
3577 // For most types, if x != y then ssub.sat x, y != 0. But
3578 // ssub.sat.i1 0, -1 = 0, because 1 saturates to 0. This means
3579 // isNonZeroSub will do the wrong thing for ssub.sat.i1.
3580 if (BitWidth == 1)
3581 return false;
3582 return isNonZeroSub(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3583 II->getArgOperand(1), Depth);
3584 case Intrinsic::sadd_sat:
3585 return isNonZeroAdd(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3586 II->getArgOperand(1),
3587 /*NSW=*/true, /* NUW=*/false, Depth);
3588 // Vec reverse preserves zero/non-zero status from input vec.
3589 case Intrinsic::vector_reverse:
3590 return isKnownNonZero(II->getArgOperand(0), DemandedElts.reverseBits(),
3591 Q, Depth);
3592 // umin/smin/smax/smin/or of all non-zero elements is always non-zero.
3593 case Intrinsic::vector_reduce_or:
3594 case Intrinsic::vector_reduce_umax:
3595 case Intrinsic::vector_reduce_umin:
3596 case Intrinsic::vector_reduce_smax:
3597 case Intrinsic::vector_reduce_smin:
3598 return isKnownNonZero(II->getArgOperand(0), Q, Depth);
3599 case Intrinsic::umax:
3600 case Intrinsic::uadd_sat:
3601 // umax(X, (X != 0)) is non zero
3602 // X +usat (X != 0) is non zero
3603 if (matchOpWithOpEqZero(II->getArgOperand(0), II->getArgOperand(1)))
3604 return true;
3605
3606 return isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth) ||
3607 isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3608 case Intrinsic::smax: {
3609 // If either arg is strictly positive the result is non-zero. Otherwise
3610 // the result is non-zero if both ops are non-zero.
3611 auto IsNonZero = [&](Value *Op, std::optional<bool> &OpNonZero,
3612 const KnownBits &OpKnown) {
3613 if (!OpNonZero.has_value())
3614 OpNonZero = OpKnown.isNonZero() ||
3615 isKnownNonZero(Op, DemandedElts, Q, Depth);
3616 return *OpNonZero;
3617 };
3618 // Avoid re-computing isKnownNonZero.
3619 std::optional<bool> Op0NonZero, Op1NonZero;
3620 KnownBits Op1Known =
3621 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3622 if (Op1Known.isNonNegative() &&
3623 IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known))
3624 return true;
3625 KnownBits Op0Known =
3626 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3627 if (Op0Known.isNonNegative() &&
3628 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known))
3629 return true;
3630 return IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known) &&
3631 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known);
3632 }
3633 case Intrinsic::smin: {
3634 // If either arg is negative the result is non-zero. Otherwise
3635 // the result is non-zero if both ops are non-zero.
3636 KnownBits Op1Known =
3637 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3638 if (Op1Known.isNegative())
3639 return true;
3640 KnownBits Op0Known =
3641 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3642 if (Op0Known.isNegative())
3643 return true;
3644
3645 if (Op1Known.isNonZero() && Op0Known.isNonZero())
3646 return true;
3647 }
3648 [[fallthrough]];
3649 case Intrinsic::umin:
3650 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth) &&
3651 isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth);
3652 case Intrinsic::cttz:
3653 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3654 .Zero[0];
3655 case Intrinsic::ctlz:
3656 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3657 .isNonNegative();
3658 case Intrinsic::fshr:
3659 case Intrinsic::fshl:
3660 // If Op0 == Op1, this is a rotate. rotate(x, y) != 0 iff x != 0.
3661 if (II->getArgOperand(0) == II->getArgOperand(1))
3662 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3663 break;
3664 case Intrinsic::vscale:
3665 return true;
3666 case Intrinsic::experimental_get_vector_length:
3667 return isKnownNonZero(I->getOperand(0), Q, Depth);
3668 default:
3669 break;
3670 }
3671 break;
3672 }
3673
3674 return false;
3675 }
3676 }
3677
3678 KnownBits Known(BitWidth);
3679 computeKnownBits(I, DemandedElts, Known, Q, Depth);
3680 return Known.One != 0;
3681}
3682
3683/// Return true if the given value is known to be non-zero when defined. For
3684/// vectors, return true if every demanded element is known to be non-zero when
3685/// defined. For pointers, if the context instruction and dominator tree are
3686/// specified, perform context-sensitive analysis and return true if the
3687/// pointer couldn't possibly be null at the specified instruction.
3688/// Supports values with integer or pointer type and vectors of integers.
3689bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
3690 const SimplifyQuery &Q, unsigned Depth) {
3691 Type *Ty = V->getType();
3692
3693#ifndef NDEBUG
3694 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
3695
3696 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3697 assert(
3698 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
3699 "DemandedElt width should equal the fixed vector number of elements");
3700 } else {
3701 assert(DemandedElts == APInt(1, 1) &&
3702 "DemandedElt width should be 1 for scalars");
3703 }
3704#endif
3705
3706 if (auto *C = dyn_cast<Constant>(V)) {
3707 if (C->isNullValue())
3708 return false;
3709 if (isa<ConstantInt>(C))
3710 // Must be non-zero due to null test above.
3711 return true;
3712
3713 // For constant vectors, check that all elements are poison or known
3714 // non-zero to determine that the whole vector is known non-zero.
3715 if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
3716 for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
3717 if (!DemandedElts[i])
3718 continue;
3719 Constant *Elt = C->getAggregateElement(i);
3720 if (!Elt || Elt->isNullValue())
3721 return false;
3722 if (!isa<PoisonValue>(Elt) && !isa<ConstantInt>(Elt))
3723 return false;
3724 }
3725 return true;
3726 }
3727
3728 // Constant ptrauth can be null, iff the base pointer can be.
3729 if (auto *CPA = dyn_cast<ConstantPtrAuth>(V))
3730 return isKnownNonZero(CPA->getPointer(), DemandedElts, Q, Depth);
3731
3732 // A global variable in address space 0 is non null unless extern weak
3733 // or an absolute symbol reference. Other address spaces may have null as a
3734 // valid address for a global, so we can't assume anything.
3735 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
3736 if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&
3737 GV->getType()->getAddressSpace() == 0)
3738 return true;
3739 }
3740
3741 // For constant expressions, fall through to the Operator code below.
3742 if (!isa<ConstantExpr>(V))
3743 return false;
3744 }
3745
3746 if (const auto *A = dyn_cast<Argument>(V))
3747 if (std::optional<ConstantRange> Range = A->getRange()) {
3748 const APInt ZeroValue(Range->getBitWidth(), 0);
3749 if (!Range->contains(ZeroValue))
3750 return true;
3751 }
3752
3753 if (!isa<Constant>(V) && isKnownNonZeroFromAssume(V, Q))
3754 return true;
3755
3756 // Some of the tests below are recursive, so bail out if we hit the limit.
3758 return false;
3759
3760 // Check for pointer simplifications.
3761
3762 if (PointerType *PtrTy = dyn_cast<PointerType>(Ty)) {
3763 // A byval, inalloca may not be null in a non-default addres space. A
3764 // nonnull argument is assumed never 0.
3765 if (const Argument *A = dyn_cast<Argument>(V)) {
3766 if (((A->hasPassPointeeByValueCopyAttr() &&
3767 !NullPointerIsDefined(A->getParent(), PtrTy->getAddressSpace())) ||
3768 A->hasNonNullAttr()))
3769 return true;
3770 }
3771 }
3772
3773 if (const auto *I = dyn_cast<Operator>(V))
3774 if (isKnownNonZeroFromOperator(I, DemandedElts, Q, Depth))
3775 return true;
3776
3777 if (!isa<Constant>(V) &&
3779 return true;
3780
3781 if (const Value *Stripped = stripNullTest(V))
3782 return isKnownNonZero(Stripped, DemandedElts, Q, Depth);
3783
3784 return false;
3785}
3786
3788 unsigned Depth) {
3789 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
3790 APInt DemandedElts =
3791 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
3792 return ::isKnownNonZero(V, DemandedElts, Q, Depth);
3793}
3794
3795/// If the pair of operators are the same invertible function, return the
3796/// the operands of the function corresponding to each input. Otherwise,
3797/// return std::nullopt. An invertible function is one that is 1-to-1 and maps
3798/// every input value to exactly one output value. This is equivalent to
3799/// saying that Op1 and Op2 are equal exactly when the specified pair of
3800/// operands are equal, (except that Op1 and Op2 may be poison more often.)
3801static std::optional<std::pair<Value*, Value*>>
3803 const Operator *Op2) {
3804 if (Op1->getOpcode() != Op2->getOpcode())
3805 return std::nullopt;
3806
3807 auto getOperands = [&](unsigned OpNum) -> auto {
3808 return std::make_pair(Op1->getOperand(OpNum), Op2->getOperand(OpNum));
3809 };
3810
3811 switch (Op1->getOpcode()) {
3812 default:
3813 break;
3814 case Instruction::Or:
3815 if (!cast<PossiblyDisjointInst>(Op1)->isDisjoint() ||
3816 !cast<PossiblyDisjointInst>(Op2)->isDisjoint())
3817 break;
3818 [[fallthrough]];
3819 case Instruction::Xor:
3820 case Instruction::Add: {
3821 Value *Other;
3822 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(0)), m_Value(Other))))
3823 return std::make_pair(Op1->getOperand(1), Other);
3824 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(1)), m_Value(Other))))
3825 return std::make_pair(Op1->getOperand(0), Other);
3826 break;
3827 }
3828 case Instruction::Sub:
3829 if (Op1->getOperand(0) == Op2->getOperand(0))
3830 return getOperands(1);
3831 if (Op1->getOperand(1) == Op2->getOperand(1))
3832 return getOperands(0);
3833 break;
3834 case Instruction::Mul: {
3835 // invertible if A * B == (A * B) mod 2^N where A, and B are integers
3836 // and N is the bitwdith. The nsw case is non-obvious, but proven by
3837 // alive2: https://alive2.llvm.org/ce/z/Z6D5qK
3838 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3839 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3840 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3841 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3842 break;
3843
3844 // Assume operand order has been canonicalized
3845 if (Op1->getOperand(1) == Op2->getOperand(1) &&
3846 isa<ConstantInt>(Op1->getOperand(1)) &&
3847 !cast<ConstantInt>(Op1->getOperand(1))->isZero())
3848 return getOperands(0);
3849 break;
3850 }
3851 case Instruction::Shl: {
3852 // Same as multiplies, with the difference that we don't need to check
3853 // for a non-zero multiply. Shifts always multiply by non-zero.
3854 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3855 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3856 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3857 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3858 break;
3859
3860 if (Op1->getOperand(1) == Op2->getOperand(1))
3861 return getOperands(0);
3862 break;
3863 }
3864 case Instruction::AShr:
3865 case Instruction::LShr: {
3866 auto *PEO1 = cast<PossiblyExactOperator>(Op1);
3867 auto *PEO2 = cast<PossiblyExactOperator>(Op2);
3868 if (!PEO1->isExact() || !PEO2->isExact())
3869 break;
3870
3871 if (Op1->getOperand(1) == Op2->getOperand(1))
3872 return getOperands(0);
3873 break;
3874 }
3875 case Instruction::SExt:
3876 case Instruction::ZExt:
3877 if (Op1->getOperand(0)->getType() == Op2->getOperand(0)->getType())
3878 return getOperands(0);
3879 break;
3880 case Instruction::PHI: {
3881 const PHINode *PN1 = cast<PHINode>(Op1);
3882 const PHINode *PN2 = cast<PHINode>(Op2);
3883
3884 // If PN1 and PN2 are both recurrences, can we prove the entire recurrences
3885 // are a single invertible function of the start values? Note that repeated
3886 // application of an invertible function is also invertible
3887 BinaryOperator *BO1 = nullptr;
3888 Value *Start1 = nullptr, *Step1 = nullptr;
3889 BinaryOperator *BO2 = nullptr;
3890 Value *Start2 = nullptr, *Step2 = nullptr;
3891 if (PN1->getParent() != PN2->getParent() ||
3892 !matchSimpleRecurrence(PN1, BO1, Start1, Step1) ||
3893 !matchSimpleRecurrence(PN2, BO2, Start2, Step2))
3894 break;
3895
3896 auto Values = getInvertibleOperands(cast<Operator>(BO1),
3897 cast<Operator>(BO2));
3898 if (!Values)
3899 break;
3900
3901 // We have to be careful of mutually defined recurrences here. Ex:
3902 // * X_i = X_(i-1) OP Y_(i-1), and Y_i = X_(i-1) OP V
3903 // * X_i = Y_i = X_(i-1) OP Y_(i-1)
3904 // The invertibility of these is complicated, and not worth reasoning
3905 // about (yet?).
3906 if (Values->first != PN1 || Values->second != PN2)
3907 break;
3908
3909 return std::make_pair(Start1, Start2);
3910 }
3911 }
3912 return std::nullopt;
3913}
3914
3915/// Return true if V1 == (binop V2, X), where X is known non-zero.
3916/// Only handle a small subset of binops where (binop V2, X) with non-zero X
3917/// implies V2 != V1.
3918static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2,
3919 const APInt &DemandedElts,
3920 const SimplifyQuery &Q, unsigned Depth) {
3922 if (!BO)
3923 return false;
3924 switch (BO->getOpcode()) {
3925 default:
3926 break;
3927 case Instruction::Or:
3928 if (!cast<PossiblyDisjointInst>(V1)->isDisjoint())
3929 break;
3930 [[fallthrough]];
3931 case Instruction::Xor:
3932 case Instruction::Add:
3933 Value *Op = nullptr;
3934 if (V2 == BO->getOperand(0))
3935 Op = BO->getOperand(1);
3936 else if (V2 == BO->getOperand(1))
3937 Op = BO->getOperand(0);
3938 else
3939 return false;
3940 return isKnownNonZero(Op, DemandedElts, Q, Depth + 1);
3941 }
3942 return false;
3943}
3944
3945/// Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and
3946/// the multiplication is nuw or nsw.
3947static bool isNonEqualMul(const Value *V1, const Value *V2,
3948 const APInt &DemandedElts, const SimplifyQuery &Q,
3949 unsigned Depth) {
3950 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3951 const APInt *C;
3952 return match(OBO, m_Mul(m_Specific(V1), m_APInt(C))) &&
3953 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3954 !C->isZero() && !C->isOne() &&
3955 isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3956 }
3957 return false;
3958}
3959
3960/// Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and
3961/// the shift is nuw or nsw.
3962static bool isNonEqualShl(const Value *V1, const Value *V2,
3963 const APInt &DemandedElts, const SimplifyQuery &Q,
3964 unsigned Depth) {
3965 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3966 const APInt *C;
3967 return match(OBO, m_Shl(m_Specific(V1), m_APInt(C))) &&
3968 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3969 !C->isZero() && isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3970 }
3971 return false;
3972}
3973
3974static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2,
3975 const APInt &DemandedElts, const SimplifyQuery &Q,
3976 unsigned Depth) {
3977 // Check two PHIs are in same block.
3978 if (PN1->getParent() != PN2->getParent())
3979 return false;
3980
3982 bool UsedFullRecursion = false;
3983 for (const BasicBlock *IncomBB : PN1->blocks()) {
3984 if (!VisitedBBs.insert(IncomBB).second)
3985 continue; // Don't reprocess blocks that we have dealt with already.
3986 const Value *IV1 = PN1->getIncomingValueForBlock(IncomBB);
3987 const Value *IV2 = PN2->getIncomingValueForBlock(IncomBB);
3988 const APInt *C1, *C2;
3989 if (match(IV1, m_APInt(C1)) && match(IV2, m_APInt(C2)) && *C1 != *C2)
3990 continue;
3991
3992 // Only one pair of phi operands is allowed for full recursion.
3993 if (UsedFullRecursion)
3994 return false;
3995
3997 RecQ.CxtI = IncomBB->getTerminator();
3998 if (!isKnownNonEqual(IV1, IV2, DemandedElts, RecQ, Depth + 1))
3999 return false;
4000 UsedFullRecursion = true;
4001 }
4002 return true;
4003}
4004
4005static bool isNonEqualSelect(const Value *V1, const Value *V2,
4006 const APInt &DemandedElts, const SimplifyQuery &Q,
4007 unsigned Depth) {
4008 const SelectInst *SI1 = dyn_cast<SelectInst>(V1);
4009 if (!SI1)
4010 return false;
4011
4012 if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2)) {
4013 const Value *Cond1 = SI1->getCondition();
4014 const Value *Cond2 = SI2->getCondition();
4015 if (Cond1 == Cond2)
4016 return isKnownNonEqual(SI1->getTrueValue(), SI2->getTrueValue(),
4017 DemandedElts, Q, Depth + 1) &&
4018 isKnownNonEqual(SI1->getFalseValue(), SI2->getFalseValue(),
4019 DemandedElts, Q, Depth + 1);
4020 }
4021 return isKnownNonEqual(SI1->getTrueValue(), V2, DemandedElts, Q, Depth + 1) &&
4022 isKnownNonEqual(SI1->getFalseValue(), V2, DemandedElts, Q, Depth + 1);
4023}
4024
4025// Check to see if A is both a GEP and is the incoming value for a PHI in the
4026// loop, and B is either a ptr or another GEP. If the PHI has 2 incoming values,
4027// one of them being the recursive GEP A and the other a ptr at same base and at
4028// the same/higher offset than B we are only incrementing the pointer further in
4029// loop if offset of recursive GEP is greater than 0.
4031 const SimplifyQuery &Q) {
4032 if (!A->getType()->isPointerTy() || !B->getType()->isPointerTy())
4033 return false;
4034
4035 auto *GEPA = dyn_cast<GEPOperator>(A);
4036 if (!GEPA || GEPA->getNumIndices() != 1 || !isa<Constant>(GEPA->idx_begin()))
4037 return false;
4038
4039 // Handle 2 incoming PHI values with one being a recursive GEP.
4040 auto *PN = dyn_cast<PHINode>(GEPA->getPointerOperand());
4041 if (!PN || PN->getNumIncomingValues() != 2)
4042 return false;
4043
4044 // Search for the recursive GEP as an incoming operand, and record that as
4045 // Step.
4046 Value *Start = nullptr;
4047 Value *Step = const_cast<Value *>(A);
4048 if (PN->getIncomingValue(0) == Step)
4049 Start = PN->getIncomingValue(1);
4050 else if (PN->getIncomingValue(1) == Step)
4051 Start = PN->getIncomingValue(0);
4052 else
4053 return false;
4054
4055 // Other incoming node base should match the B base.
4056 // StartOffset >= OffsetB && StepOffset > 0?
4057 // StartOffset <= OffsetB && StepOffset < 0?
4058 // Is non-equal if above are true.
4059 // We use stripAndAccumulateInBoundsConstantOffsets to restrict the
4060 // optimisation to inbounds GEPs only.
4061 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(Start->getType());
4062 APInt StartOffset(IndexWidth, 0);
4063 Start = Start->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StartOffset);
4064 APInt StepOffset(IndexWidth, 0);
4065 Step = Step->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StepOffset);
4066
4067 // Check if Base Pointer of Step matches the PHI.
4068 if (Step != PN)
4069 return false;
4070 APInt OffsetB(IndexWidth, 0);
4071 B = B->stripAndAccumulateInBoundsConstantOffsets(Q.DL, OffsetB);
4072 return Start == B &&
4073 ((StartOffset.sge(OffsetB) && StepOffset.isStrictlyPositive()) ||
4074 (StartOffset.sle(OffsetB) && StepOffset.isNegative()));
4075}
4076
4077static bool isKnownNonEqualFromContext(const Value *V1, const Value *V2,
4078 const SimplifyQuery &Q, unsigned Depth) {
4079 if (!Q.CxtI)
4080 return false;
4081
4082 // Try to infer NonEqual based on information from dominating conditions.
4083 if (Q.DC && Q.DT) {
4084 auto IsKnownNonEqualFromDominatingCondition = [&](const Value *V) {
4085 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
4086 Value *Cond = BI->getCondition();
4087 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4088 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()) &&
4090 /*LHSIsTrue=*/true, Depth)
4091 .value_or(false))
4092 return true;
4093
4094 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4095 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()) &&
4097 /*LHSIsTrue=*/false, Depth)
4098 .value_or(false))
4099 return true;
4100 }
4101
4102 return false;
4103 };
4104
4105 if (IsKnownNonEqualFromDominatingCondition(V1) ||
4106 IsKnownNonEqualFromDominatingCondition(V2))
4107 return true;
4108 }
4109
4110 if (!Q.AC)
4111 return false;
4112
4113 // Try to infer NonEqual based on information from assumptions.
4114 for (auto &AssumeVH : Q.AC->assumptionsFor(V1)) {
4115 if (!AssumeVH)
4116 continue;
4117 CallInst *I = cast<CallInst>(AssumeVH);
4118
4119 assert(I->getFunction() == Q.CxtI->getFunction() &&
4120 "Got assumption for the wrong function!");
4121 assert(I->getIntrinsicID() == Intrinsic::assume &&
4122 "must be an assume intrinsic");
4123
4124 if (isImpliedCondition(I->getArgOperand(0), ICmpInst::ICMP_NE, V1, V2, Q.DL,
4125 /*LHSIsTrue=*/true, Depth)
4126 .value_or(false) &&
4128 return true;
4129 }
4130
4131 return false;
4132}
4133
4134/// Return true if it is known that V1 != V2.
4135static bool isKnownNonEqual(const Value *V1, const Value *V2,
4136 const APInt &DemandedElts, const SimplifyQuery &Q,
4137 unsigned Depth) {
4138 if (V1 == V2)
4139 return false;
4140 if (V1->getType() != V2->getType())
4141 // We can't look through casts yet.
4142 return false;
4143
4145 return false;
4146
4147 // See if we can recurse through (exactly one of) our operands. This
4148 // requires our operation be 1-to-1 and map every input value to exactly
4149 // one output value. Such an operation is invertible.
4150 auto *O1 = dyn_cast<Operator>(V1);
4151 auto *O2 = dyn_cast<Operator>(V2);
4152 if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) {
4153 if (auto Values = getInvertibleOperands(O1, O2))
4154 return isKnownNonEqual(Values->first, Values->second, DemandedElts, Q,
4155 Depth + 1);
4156
4157 if (const PHINode *PN1 = dyn_cast<PHINode>(V1)) {
4158 const PHINode *PN2 = cast<PHINode>(V2);
4159 // FIXME: This is missing a generalization to handle the case where one is
4160 // a PHI and another one isn't.
4161 if (isNonEqualPHIs(PN1, PN2, DemandedElts, Q, Depth))
4162 return true;
4163 };
4164 }
4165
4166 if (isModifyingBinopOfNonZero(V1, V2, DemandedElts, Q, Depth) ||
4167 isModifyingBinopOfNonZero(V2, V1, DemandedElts, Q, Depth))
4168 return true;
4169
4170 if (isNonEqualMul(V1, V2, DemandedElts, Q, Depth) ||
4171 isNonEqualMul(V2, V1, DemandedElts, Q, Depth))
4172 return true;
4173
4174 if (isNonEqualShl(V1, V2, DemandedElts, Q, Depth) ||
4175 isNonEqualShl(V2, V1, DemandedElts, Q, Depth))
4176 return true;
4177
4178 if (V1->getType()->isIntOrIntVectorTy()) {
4179 // Are any known bits in V1 contradictory to known bits in V2? If V1
4180 // has a known zero where V2 has a known one, they must not be equal.
4181 KnownBits Known1 = computeKnownBits(V1, DemandedElts, Q, Depth);
4182 if (!Known1.isUnknown()) {
4183 KnownBits Known2 = computeKnownBits(V2, DemandedElts, Q, Depth);
4184 if (Known1.Zero.intersects(Known2.One) ||
4185 Known2.Zero.intersects(Known1.One))
4186 return true;
4187 }
4188 }
4189
4190 if (isNonEqualSelect(V1, V2, DemandedElts, Q, Depth) ||
4191 isNonEqualSelect(V2, V1, DemandedElts, Q, Depth))
4192 return true;
4193
4194 if (isNonEqualPointersWithRecursiveGEP(V1, V2, Q) ||
4196 return true;
4197
4198 Value *A, *B;
4199 // PtrToInts are NonEqual if their Ptrs are NonEqual.
4200 // Check PtrToInt type matches the pointer size.
4201 if (match(V1, m_PtrToIntSameSize(Q.DL, m_Value(A))) &&
4203 return isKnownNonEqual(A, B, DemandedElts, Q, Depth + 1);
4204
4205 if (isKnownNonEqualFromContext(V1, V2, Q, Depth))
4206 return true;
4207
4208 return false;
4209}
4210
4211/// For vector constants, loop over the elements and find the constant with the
4212/// minimum number of sign bits. Return 0 if the value is not a vector constant
4213/// or if any element was not analyzed; otherwise, return the count for the
4214/// element with the minimum number of sign bits.
4216 const APInt &DemandedElts,
4217 unsigned TyBits) {
4218 const auto *CV = dyn_cast<Constant>(V);
4219 if (!CV || !isa<FixedVectorType>(CV->getType()))
4220 return 0;
4221
4222 unsigned MinSignBits = TyBits;
4223 unsigned NumElts = cast<FixedVectorType>(CV->getType())->getNumElements();
4224 for (unsigned i = 0; i != NumElts; ++i) {
4225 if (!DemandedElts[i])
4226 continue;
4227 // If we find a non-ConstantInt, bail out.
4228 auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));
4229 if (!Elt)
4230 return 0;
4231
4232 MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());
4233 }
4234
4235 return MinSignBits;
4236}
4237
4238static unsigned ComputeNumSignBitsImpl(const Value *V,
4239 const APInt &DemandedElts,
4240 const SimplifyQuery &Q, unsigned Depth);
4241
4242static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
4243 const SimplifyQuery &Q, unsigned Depth) {
4244 unsigned Result = ComputeNumSignBitsImpl(V, DemandedElts, Q, Depth);
4245 assert(Result > 0 && "At least one sign bit needs to be present!");
4246 return Result;
4247}
4248
4249/// Return the number of times the sign bit of the register is replicated into
4250/// the other bits. We know that at least 1 bit is always equal to the sign bit
4251/// (itself), but other cases can give us information. For example, immediately
4252/// after an "ashr X, 2", we know that the top 3 bits are all equal to each
4253/// other, so we return 3. For vectors, return the number of sign bits for the
4254/// vector element with the minimum number of known sign bits of the demanded
4255/// elements in the vector specified by DemandedElts.
4256static unsigned ComputeNumSignBitsImpl(const Value *V,
4257 const APInt &DemandedElts,
4258 const SimplifyQuery &Q, unsigned Depth) {
4259 Type *Ty = V->getType();
4260#ifndef NDEBUG
4261 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4262
4263 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
4264 assert(
4265 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
4266 "DemandedElt width should equal the fixed vector number of elements");
4267 } else {
4268 assert(DemandedElts == APInt(1, 1) &&
4269 "DemandedElt width should be 1 for scalars");
4270 }
4271#endif
4272
4273 // We return the minimum number of sign bits that are guaranteed to be present
4274 // in V, so for undef we have to conservatively return 1. We don't have the
4275 // same behavior for poison though -- that's a FIXME today.
4276
4277 Type *ScalarTy = Ty->getScalarType();
4278 unsigned TyBits = ScalarTy->isPointerTy() ?
4279 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
4280 Q.DL.getTypeSizeInBits(ScalarTy);
4281
4282 unsigned Tmp, Tmp2;
4283 unsigned FirstAnswer = 1;
4284
4285 // Note that ConstantInt is handled by the general computeKnownBits case
4286 // below.
4287
4289 return 1;
4290
4291 if (auto *U = dyn_cast<Operator>(V)) {
4292 switch (Operator::getOpcode(V)) {
4293 default: break;
4294 case Instruction::BitCast: {
4295 Value *Src = U->getOperand(0);
4296 Type *SrcTy = Src->getType();
4297
4298 // Skip if the source type is not an integer or integer vector type
4299 // This ensures we only process integer-like types
4300 if (!SrcTy->isIntOrIntVectorTy())
4301 break;
4302
4303 unsigned SrcBits = SrcTy->getScalarSizeInBits();
4304
4305 // Bitcast 'large element' scalar/vector to 'small element' vector.
4306 if ((SrcBits % TyBits) != 0)
4307 break;
4308
4309 // Only proceed if the destination type is a fixed-size vector
4310 if (isa<FixedVectorType>(Ty)) {
4311 // Fast case - sign splat can be simply split across the small elements.
4312 // This works for both vector and scalar sources
4313 Tmp = ComputeNumSignBits(Src, Q, Depth + 1);
4314 if (Tmp == SrcBits)
4315 return TyBits;
4316 }
4317 break;
4318 }
4319 case Instruction::SExt:
4320 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
4321 return ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1) +
4322 Tmp;
4323
4324 case Instruction::SDiv: {
4325 const APInt *Denominator;
4326 // sdiv X, C -> adds log(C) sign bits.
4327 if (match(U->getOperand(1), m_APInt(Denominator))) {
4328
4329 // Ignore non-positive denominator.
4330 if (!Denominator->isStrictlyPositive())
4331 break;
4332
4333 // Calculate the incoming numerator bits.
4334 unsigned NumBits =
4335 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4336
4337 // Add floor(log(C)) bits to the numerator bits.
4338 return std::min(TyBits, NumBits + Denominator->logBase2());
4339 }
4340 break;
4341 }
4342
4343 case Instruction::SRem: {
4344 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4345
4346 const APInt *Denominator;
4347 // srem X, C -> we know that the result is within [-C+1,C) when C is a
4348 // positive constant. This let us put a lower bound on the number of sign
4349 // bits.
4350 if (match(U->getOperand(1), m_APInt(Denominator))) {
4351
4352 // Ignore non-positive denominator.
4353 if (Denominator->isStrictlyPositive()) {
4354 // Calculate the leading sign bit constraints by examining the
4355 // denominator. Given that the denominator is positive, there are two
4356 // cases:
4357 //
4358 // 1. The numerator is positive. The result range is [0,C) and
4359 // [0,C) u< (1 << ceilLogBase2(C)).
4360 //
4361 // 2. The numerator is negative. Then the result range is (-C,0] and
4362 // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
4363 //
4364 // Thus a lower bound on the number of sign bits is `TyBits -
4365 // ceilLogBase2(C)`.
4366
4367 unsigned ResBits = TyBits - Denominator->ceilLogBase2();
4368 Tmp = std::max(Tmp, ResBits);
4369 }
4370 }
4371 return Tmp;
4372 }
4373
4374 case Instruction::AShr: {
4375 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4376 // ashr X, C -> adds C sign bits. Vectors too.
4377 const APInt *ShAmt;
4378 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4379 if (ShAmt->uge(TyBits))
4380 break; // Bad shift.
4381 unsigned ShAmtLimited = ShAmt->getZExtValue();
4382 Tmp += ShAmtLimited;
4383 if (Tmp > TyBits) Tmp = TyBits;
4384 }
4385 return Tmp;
4386 }
4387 case Instruction::Shl: {
4388 const APInt *ShAmt;
4389 Value *X = nullptr;
4390 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4391 // shl destroys sign bits.
4392 if (ShAmt->uge(TyBits))
4393 break; // Bad shift.
4394 // We can look through a zext (more or less treating it as a sext) if
4395 // all extended bits are shifted out.
4396 if (match(U->getOperand(0), m_ZExt(m_Value(X))) &&
4397 ShAmt->uge(TyBits - X->getType()->getScalarSizeInBits())) {
4398 Tmp = ComputeNumSignBits(X, DemandedElts, Q, Depth + 1);
4399 Tmp += TyBits - X->getType()->getScalarSizeInBits();
4400 } else
4401 Tmp =
4402 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4403 if (ShAmt->uge(Tmp))
4404 break; // Shifted all sign bits out.
4405 Tmp2 = ShAmt->getZExtValue();
4406 return Tmp - Tmp2;
4407 }
4408 break;
4409 }
4410 case Instruction::And:
4411 case Instruction::Or:
4412 case Instruction::Xor: // NOT is handled here.
4413 // Logical binary ops preserve the number of sign bits at the worst.
4414 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4415 if (Tmp != 1) {
4416 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4417 FirstAnswer = std::min(Tmp, Tmp2);
4418 // We computed what we know about the sign bits as our first
4419 // answer. Now proceed to the generic code that uses
4420 // computeKnownBits, and pick whichever answer is better.
4421 }
4422 break;
4423
4424 case Instruction::Select: {
4425 // If we have a clamp pattern, we know that the number of sign bits will
4426 // be the minimum of the clamp min/max range.
4427 const Value *X;
4428 const APInt *CLow, *CHigh;
4429 if (isSignedMinMaxClamp(U, X, CLow, CHigh))
4430 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4431
4432 Tmp = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4433 if (Tmp == 1)
4434 break;
4435 Tmp2 = ComputeNumSignBits(U->getOperand(2), DemandedElts, Q, Depth + 1);
4436 return std::min(Tmp, Tmp2);
4437 }
4438
4439 case Instruction::Add:
4440 // Add can have at most one carry bit. Thus we know that the output
4441 // is, at worst, one more bit than the inputs.
4442 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4443 if (Tmp == 1) break;
4444
4445 // Special case decrementing a value (ADD X, -1):
4446 if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
4447 if (CRHS->isAllOnesValue()) {
4448 KnownBits Known(TyBits);
4449 computeKnownBits(U->getOperand(0), DemandedElts, Known, Q, Depth + 1);
4450
4451 // If the input is known to be 0 or 1, the output is 0/-1, which is
4452 // all sign bits set.
4453 if ((Known.Zero | 1).isAllOnes())
4454 return TyBits;
4455
4456 // If we are subtracting one from a positive number, there is no carry
4457 // out of the result.
4458 if (Known.isNonNegative())
4459 return Tmp;
4460 }
4461
4462 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4463 if (Tmp2 == 1)
4464 break;
4465 return std::min(Tmp, Tmp2) - 1;
4466
4467 case Instruction::Sub:
4468 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4469 if (Tmp2 == 1)
4470 break;
4471
4472 // Handle NEG.
4473 if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
4474 if (CLHS->isNullValue()) {
4475 KnownBits Known(TyBits);
4476 computeKnownBits(U->getOperand(1), DemandedElts, Known, Q, Depth + 1);
4477 // If the input is known to be 0 or 1, the output is 0/-1, which is
4478 // all sign bits set.
4479 if ((Known.Zero | 1).isAllOnes())
4480 return TyBits;
4481
4482 // If the input is known to be positive (the sign bit is known clear),
4483 // the output of the NEG has the same number of sign bits as the
4484 // input.
4485 if (Known.isNonNegative())
4486 return Tmp2;
4487
4488 // Otherwise, we treat this like a SUB.
4489 }
4490
4491 // Sub can have at most one carry bit. Thus we know that the output
4492 // is, at worst, one more bit than the inputs.
4493 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4494 if (Tmp == 1)
4495 break;
4496 return std::min(Tmp, Tmp2) - 1;
4497
4498 case Instruction::Mul: {
4499 // The output of the Mul can be at most twice the valid bits in the
4500 // inputs.
4501 unsigned SignBitsOp0 =
4502 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4503 if (SignBitsOp0 == 1)
4504 break;
4505 unsigned SignBitsOp1 =
4506 ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4507 if (SignBitsOp1 == 1)
4508 break;
4509 unsigned OutValidBits =
4510 (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);
4511 return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
4512 }
4513
4514 case Instruction::PHI: {
4515 const PHINode *PN = cast<PHINode>(U);
4516 unsigned NumIncomingValues = PN->getNumIncomingValues();
4517 // Don't analyze large in-degree PHIs.
4518 if (NumIncomingValues > 4) break;
4519 // Unreachable blocks may have zero-operand PHI nodes.
4520 if (NumIncomingValues == 0) break;
4521
4522 // Take the minimum of all incoming values. This can't infinitely loop
4523 // because of our depth threshold.
4525 Tmp = TyBits;
4526 for (unsigned i = 0, e = NumIncomingValues; i != e; ++i) {
4527 if (Tmp == 1) return Tmp;
4528 RecQ.CxtI = PN->getIncomingBlock(i)->getTerminator();
4529 Tmp = std::min(Tmp, ComputeNumSignBits(PN->getIncomingValue(i),
4530 DemandedElts, RecQ, Depth + 1));
4531 }
4532 return Tmp;
4533 }
4534
4535 case Instruction::Trunc: {
4536 // If the input contained enough sign bits that some remain after the
4537 // truncation, then we can make use of that. Otherwise we don't know
4538 // anything.
4539 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4540 unsigned OperandTyBits = U->getOperand(0)->getType()->getScalarSizeInBits();
4541 if (Tmp > (OperandTyBits - TyBits))
4542 return Tmp - (OperandTyBits - TyBits);
4543
4544 return 1;
4545 }
4546
4547 case Instruction::ExtractElement:
4548 // Look through extract element. At the moment we keep this simple and
4549 // skip tracking the specific element. But at least we might find
4550 // information valid for all elements of the vector (for example if vector
4551 // is sign extended, shifted, etc).
4552 return ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4553
4554 case Instruction::ShuffleVector: {
4555 // Collect the minimum number of sign bits that are shared by every vector
4556 // element referenced by the shuffle.
4557 auto *Shuf = dyn_cast<ShuffleVectorInst>(U);
4558 if (!Shuf) {
4559 // FIXME: Add support for shufflevector constant expressions.
4560 return 1;
4561 }
4562 APInt DemandedLHS, DemandedRHS;
4563 // For undef elements, we don't know anything about the common state of
4564 // the shuffle result.
4565 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
4566 return 1;
4567 Tmp = std::numeric_limits<unsigned>::max();
4568 if (!!DemandedLHS) {
4569 const Value *LHS = Shuf->getOperand(0);
4570 Tmp = ComputeNumSignBits(LHS, DemandedLHS, Q, Depth + 1);
4571 }
4572 // If we don't know anything, early out and try computeKnownBits
4573 // fall-back.
4574 if (Tmp == 1)
4575 break;
4576 if (!!DemandedRHS) {
4577 const Value *RHS = Shuf->getOperand(1);
4578 Tmp2 = ComputeNumSignBits(RHS, DemandedRHS, Q, Depth + 1);
4579 Tmp = std::min(Tmp, Tmp2);
4580 }
4581 // If we don't know anything, early out and try computeKnownBits
4582 // fall-back.
4583 if (Tmp == 1)
4584 break;
4585 assert(Tmp <= TyBits && "Failed to determine minimum sign bits");
4586 return Tmp;
4587 }
4588 case Instruction::Call: {
4589 if (const auto *II = dyn_cast<IntrinsicInst>(U)) {
4590 switch (II->getIntrinsicID()) {
4591 default:
4592 break;
4593 case Intrinsic::abs:
4594 Tmp =
4595 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4596 if (Tmp == 1)
4597 break;
4598
4599 // Absolute value reduces number of sign bits by at most 1.
4600 return Tmp - 1;
4601 case Intrinsic::smin:
4602 case Intrinsic::smax: {
4603 const APInt *CLow, *CHigh;
4604 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
4605 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4606 }
4607 }
4608 }
4609 }
4610 }
4611 }
4612
4613 // Finally, if we can prove that the top bits of the result are 0's or 1's,
4614 // use this information.
4615
4616 // If we can examine all elements of a vector constant successfully, we're
4617 // done (we can't do any better than that). If not, keep trying.
4618 if (unsigned VecSignBits =
4619 computeNumSignBitsVectorConstant(V, DemandedElts, TyBits))
4620 return VecSignBits;
4621
4622 KnownBits Known(TyBits);
4623 computeKnownBits(V, DemandedElts, Known, Q, Depth);
4624
4625 // If we know that the sign bit is either zero or one, determine the number of
4626 // identical bits in the top of the input value.
4627 return std::max(FirstAnswer, Known.countMinSignBits());
4628}
4629
4631 const TargetLibraryInfo *TLI) {
4632 const Function *F = CB.getCalledFunction();
4633 if (!F)
4635
4636 if (F->isIntrinsic())
4637 return F->getIntrinsicID();
4638
4639 // We are going to infer semantics of a library function based on mapping it
4640 // to an LLVM intrinsic. Check that the library function is available from
4641 // this callbase and in this environment.
4642 LibFunc Func;
4643 if (F->hasLocalLinkage() || !TLI || !TLI->getLibFunc(CB, Func) ||
4644 !CB.onlyReadsMemory())
4646
4647 switch (Func) {
4648 default:
4649 break;
4650 case LibFunc_sin:
4651 case LibFunc_sinf:
4652 case LibFunc_sinl:
4653 return Intrinsic::sin;
4654 case LibFunc_cos:
4655 case LibFunc_cosf:
4656 case LibFunc_cosl:
4657 return Intrinsic::cos;
4658 case LibFunc_tan:
4659 case LibFunc_tanf:
4660 case LibFunc_tanl:
4661 return Intrinsic::tan;
4662 case LibFunc_asin:
4663 case LibFunc_asinf:
4664 case LibFunc_asinl:
4665 return Intrinsic::asin;
4666 case LibFunc_acos:
4667 case LibFunc_acosf:
4668 case LibFunc_acosl:
4669 return Intrinsic::acos;
4670 case LibFunc_atan:
4671 case LibFunc_atanf:
4672 case LibFunc_atanl:
4673 return Intrinsic::atan;
4674 case LibFunc_atan2:
4675 case LibFunc_atan2f:
4676 case LibFunc_atan2l:
4677 return Intrinsic::atan2;
4678 case LibFunc_sinh:
4679 case LibFunc_sinhf:
4680 case LibFunc_sinhl:
4681 return Intrinsic::sinh;
4682 case LibFunc_cosh:
4683 case LibFunc_coshf:
4684 case LibFunc_coshl:
4685 return Intrinsic::cosh;
4686 case LibFunc_tanh:
4687 case LibFunc_tanhf:
4688 case LibFunc_tanhl:
4689 return Intrinsic::tanh;
4690 case LibFunc_exp:
4691 case LibFunc_expf:
4692 case LibFunc_expl:
4693 return Intrinsic::exp;
4694 case LibFunc_exp2:
4695 case LibFunc_exp2f:
4696 case LibFunc_exp2l:
4697 return Intrinsic::exp2;
4698 case LibFunc_exp10:
4699 case LibFunc_exp10f:
4700 case LibFunc_exp10l:
4701 return Intrinsic::exp10;
4702 case LibFunc_log:
4703 case LibFunc_logf:
4704 case LibFunc_logl:
4705 return Intrinsic::log;
4706 case LibFunc_log10:
4707 case LibFunc_log10f:
4708 case LibFunc_log10l:
4709 return Intrinsic::log10;
4710 case LibFunc_log2:
4711 case LibFunc_log2f:
4712 case LibFunc_log2l:
4713 return Intrinsic::log2;
4714 case LibFunc_fabs:
4715 case LibFunc_fabsf:
4716 case LibFunc_fabsl:
4717 return Intrinsic::fabs;
4718 case LibFunc_fmin:
4719 case LibFunc_fminf:
4720 case LibFunc_fminl:
4721 return Intrinsic::minnum;
4722 case LibFunc_fmax:
4723 case LibFunc_fmaxf:
4724 case LibFunc_fmaxl:
4725 return Intrinsic::maxnum;
4726 case LibFunc_copysign:
4727 case LibFunc_copysignf:
4728 case LibFunc_copysignl:
4729 return Intrinsic::copysign;
4730 case LibFunc_floor:
4731 case LibFunc_floorf:
4732 case LibFunc_floorl:
4733 return Intrinsic::floor;
4734 case LibFunc_ceil:
4735 case LibFunc_ceilf:
4736 case LibFunc_ceill:
4737 return Intrinsic::ceil;
4738 case LibFunc_trunc:
4739 case LibFunc_truncf:
4740 case LibFunc_truncl:
4741 return Intrinsic::trunc;
4742 case LibFunc_rint:
4743 case LibFunc_rintf:
4744 case LibFunc_rintl:
4745 return Intrinsic::rint;
4746 case LibFunc_nearbyint:
4747 case LibFunc_nearbyintf:
4748 case LibFunc_nearbyintl:
4749 return Intrinsic::nearbyint;
4750 case LibFunc_round:
4751 case LibFunc_roundf:
4752 case LibFunc_roundl:
4753 return Intrinsic::round;
4754 case LibFunc_roundeven:
4755 case LibFunc_roundevenf:
4756 case LibFunc_roundevenl:
4757 return Intrinsic::roundeven;
4758 case LibFunc_pow:
4759 case LibFunc_powf:
4760 case LibFunc_powl:
4761 return Intrinsic::pow;
4762 case LibFunc_sqrt:
4763 case LibFunc_sqrtf:
4764 case LibFunc_sqrtl:
4765 return Intrinsic::sqrt;
4766 }
4767
4769}
4770
4771/// Given an exploded icmp instruction, return true if the comparison only
4772/// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if
4773/// the result of the comparison is true when the input value is signed.
4775 bool &TrueIfSigned) {
4776 switch (Pred) {
4777 case ICmpInst::ICMP_SLT: // True if LHS s< 0
4778 TrueIfSigned = true;
4779 return RHS.isZero();
4780 case ICmpInst::ICMP_SLE: // True if LHS s<= -1
4781 TrueIfSigned = true;
4782 return RHS.isAllOnes();
4783 case ICmpInst::ICMP_SGT: // True if LHS s> -1
4784 TrueIfSigned = false;
4785 return RHS.isAllOnes();
4786 case ICmpInst::ICMP_SGE: // True if LHS s>= 0
4787 TrueIfSigned = false;
4788 return RHS.isZero();
4789 case ICmpInst::ICMP_UGT:
4790 // True if LHS u> RHS and RHS == sign-bit-mask - 1
4791 TrueIfSigned = true;
4792 return RHS.isMaxSignedValue();
4793 case ICmpInst::ICMP_UGE:
4794 // True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4795 TrueIfSigned = true;
4796 return RHS.isMinSignedValue();
4797 case ICmpInst::ICMP_ULT:
4798 // True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4799 TrueIfSigned = false;
4800 return RHS.isMinSignedValue();
4801 case ICmpInst::ICMP_ULE:
4802 // True if LHS u<= RHS and RHS == sign-bit-mask - 1
4803 TrueIfSigned = false;
4804 return RHS.isMaxSignedValue();
4805 default:
4806 return false;
4807 }
4808}
4809
4811 bool CondIsTrue,
4812 const Instruction *CxtI,
4813 KnownFPClass &KnownFromContext,
4814 unsigned Depth = 0) {
4815 Value *A, *B;
4817 (CondIsTrue ? match(Cond, m_LogicalAnd(m_Value(A), m_Value(B)))
4818 : match(Cond, m_LogicalOr(m_Value(A), m_Value(B))))) {
4819 computeKnownFPClassFromCond(V, A, CondIsTrue, CxtI, KnownFromContext,
4820 Depth + 1);
4821 computeKnownFPClassFromCond(V, B, CondIsTrue, CxtI, KnownFromContext,
4822 Depth + 1);
4823 return;
4824 }
4826 computeKnownFPClassFromCond(V, A, !CondIsTrue, CxtI, KnownFromContext,
4827 Depth + 1);
4828 return;
4829 }
4830 CmpPredicate Pred;
4831 Value *LHS;
4832 uint64_t ClassVal = 0;
4833 const APFloat *CRHS;
4834 const APInt *RHS;
4835 if (match(Cond, m_FCmp(Pred, m_Value(LHS), m_APFloat(CRHS)))) {
4836 auto [CmpVal, MaskIfTrue, MaskIfFalse] = fcmpImpliesClass(
4837 Pred, *cast<Instruction>(Cond)->getParent()->getParent(), LHS, *CRHS,
4838 LHS != V);
4839 if (CmpVal == V)
4840 KnownFromContext.knownNot(~(CondIsTrue ? MaskIfTrue : MaskIfFalse));
4842 m_Specific(V), m_ConstantInt(ClassVal)))) {
4843 FPClassTest Mask = static_cast<FPClassTest>(ClassVal);
4844 KnownFromContext.knownNot(CondIsTrue ? ~Mask : Mask);
4845 } else if (match(Cond, m_ICmp(Pred, m_ElementWiseBitCast(m_Specific(V)),
4846 m_APInt(RHS)))) {
4847 bool TrueIfSigned;
4848 if (!isSignBitCheck(Pred, *RHS, TrueIfSigned))
4849 return;
4850 if (TrueIfSigned == CondIsTrue)
4851 KnownFromContext.signBitMustBeOne();
4852 else
4853 KnownFromContext.signBitMustBeZero();
4854 }
4855}
4856
4858 const SimplifyQuery &Q) {
4859 KnownFPClass KnownFromContext;
4860
4861 if (Q.CC && Q.CC->AffectedValues.contains(V))
4863 KnownFromContext);
4864
4865 if (!Q.CxtI)
4866 return KnownFromContext;
4867
4868 if (Q.DC && Q.DT) {
4869 // Handle dominating conditions.
4870 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
4871 Value *Cond = BI->getCondition();
4872
4873 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4874 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
4875 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/true, Q.CxtI,
4876 KnownFromContext);
4877
4878 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4879 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
4880 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/false, Q.CxtI,
4881 KnownFromContext);
4882 }
4883 }
4884
4885 if (!Q.AC)
4886 return KnownFromContext;
4887
4888 // Try to restrict the floating-point classes based on information from
4889 // assumptions.
4890 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
4891 if (!AssumeVH)
4892 continue;
4893 CallInst *I = cast<CallInst>(AssumeVH);
4894
4895 assert(I->getFunction() == Q.CxtI->getParent()->getParent() &&
4896 "Got assumption for the wrong function!");
4897 assert(I->getIntrinsicID() == Intrinsic::assume &&
4898 "must be an assume intrinsic");
4899
4900 if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
4901 continue;
4902
4903 computeKnownFPClassFromCond(V, I->getArgOperand(0),
4904 /*CondIsTrue=*/true, Q.CxtI, KnownFromContext);
4905 }
4906
4907 return KnownFromContext;
4908}
4909
4911 Value *Arm, bool Invert,
4912 const SimplifyQuery &SQ,
4913 unsigned Depth) {
4914
4915 KnownFPClass KnownSrc;
4917 /*CondIsTrue=*/!Invert, SQ.CxtI, KnownSrc,
4918 Depth + 1);
4919 KnownSrc = KnownSrc.unionWith(Known);
4920 if (KnownSrc.isUnknown())
4921 return;
4922
4923 if (isGuaranteedNotToBeUndef(Arm, SQ.AC, SQ.CxtI, SQ.DT, Depth + 1))
4924 Known = KnownSrc;
4925}
4926
4927void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4928 FPClassTest InterestedClasses, KnownFPClass &Known,
4929 const SimplifyQuery &Q, unsigned Depth);
4930
4931static void computeKnownFPClass(const Value *V, KnownFPClass &Known,
4932 FPClassTest InterestedClasses,
4933 const SimplifyQuery &Q, unsigned Depth) {
4934 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
4935 APInt DemandedElts =
4936 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
4937 computeKnownFPClass(V, DemandedElts, InterestedClasses, Known, Q, Depth);
4938}
4939
4941 const APInt &DemandedElts,
4942 FPClassTest InterestedClasses,
4943 KnownFPClass &Known,
4944 const SimplifyQuery &Q,
4945 unsigned Depth) {
4946 if ((InterestedClasses &
4948 return;
4949
4950 KnownFPClass KnownSrc;
4951 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
4952 KnownSrc, Q, Depth + 1);
4953 Known = KnownFPClass::fptrunc(KnownSrc);
4954}
4955
4957 switch (IID) {
4958 case Intrinsic::minimum:
4960 case Intrinsic::maximum:
4962 case Intrinsic::minimumnum:
4964 case Intrinsic::maximumnum:
4966 case Intrinsic::minnum:
4968 case Intrinsic::maxnum:
4970 default:
4971 llvm_unreachable("not a floating-point min-max intrinsic");
4972 }
4973}
4974
4975/// \return true if this is a floating point value that is known to have a
4976/// magnitude smaller than 1. i.e., fabs(X) <= 1.0
4977static bool isAbsoluteValueLessEqualOne(const Value *V) {
4978 // TODO: Handle frexp and x - floor(x)?
4980}
4981
4982void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4983 FPClassTest InterestedClasses, KnownFPClass &Known,
4984 const SimplifyQuery &Q, unsigned Depth) {
4985 assert(Known.isUnknown() && "should not be called with known information");
4986
4987 if (!DemandedElts) {
4988 // No demanded elts, better to assume we don't know anything.
4989 Known.resetAll();
4990 return;
4991 }
4992
4993 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4994
4995 if (auto *CFP = dyn_cast<ConstantFP>(V)) {
4996 Known = KnownFPClass(CFP->getValueAPF());
4997 return;
4998 }
4999
5001 Known.KnownFPClasses = fcPosZero;
5002 Known.SignBit = false;
5003 return;
5004 }
5005
5006 if (isa<PoisonValue>(V)) {
5007 Known.KnownFPClasses = fcNone;
5008 Known.SignBit = false;
5009 return;
5010 }
5011
5012 // Try to handle fixed width vector constants
5013 auto *VFVTy = dyn_cast<FixedVectorType>(V->getType());
5014 const Constant *CV = dyn_cast<Constant>(V);
5015 if (VFVTy && CV) {
5016 Known.KnownFPClasses = fcNone;
5017 bool SignBitAllZero = true;
5018 bool SignBitAllOne = true;
5019
5020 // For vectors, verify that each element is not NaN.
5021 unsigned NumElts = VFVTy->getNumElements();
5022 for (unsigned i = 0; i != NumElts; ++i) {
5023 if (!DemandedElts[i])
5024 continue;
5025
5026 Constant *Elt = CV->getAggregateElement(i);
5027 if (!Elt) {
5028 Known = KnownFPClass();
5029 return;
5030 }
5031 if (isa<PoisonValue>(Elt))
5032 continue;
5033 auto *CElt = dyn_cast<ConstantFP>(Elt);
5034 if (!CElt) {
5035 Known = KnownFPClass();
5036 return;
5037 }
5038
5039 const APFloat &C = CElt->getValueAPF();
5040 Known.KnownFPClasses |= C.classify();
5041 if (C.isNegative())
5042 SignBitAllZero = false;
5043 else
5044 SignBitAllOne = false;
5045 }
5046 if (SignBitAllOne != SignBitAllZero)
5047 Known.SignBit = SignBitAllOne;
5048 return;
5049 }
5050
5051 if (const auto *CDS = dyn_cast<ConstantDataSequential>(V)) {
5052 Known.KnownFPClasses = fcNone;
5053 for (size_t I = 0, E = CDS->getNumElements(); I != E; ++I)
5054 Known |= CDS->getElementAsAPFloat(I).classify();
5055 return;
5056 }
5057
5058 if (const auto *CA = dyn_cast<ConstantAggregate>(V)) {
5059 // TODO: Handle complex aggregates
5060 Known.KnownFPClasses = fcNone;
5061 for (const Use &Op : CA->operands()) {
5062 auto *CFP = dyn_cast<ConstantFP>(Op.get());
5063 if (!CFP) {
5064 Known = KnownFPClass();
5065 return;
5066 }
5067
5068 Known |= CFP->getValueAPF().classify();
5069 }
5070
5071 return;
5072 }
5073
5074 FPClassTest KnownNotFromFlags = fcNone;
5075 if (const auto *CB = dyn_cast<CallBase>(V))
5076 KnownNotFromFlags |= CB->getRetNoFPClass();
5077 else if (const auto *Arg = dyn_cast<Argument>(V))
5078 KnownNotFromFlags |= Arg->getNoFPClass();
5079
5080 const Operator *Op = dyn_cast<Operator>(V);
5082 if (FPOp->hasNoNaNs())
5083 KnownNotFromFlags |= fcNan;
5084 if (FPOp->hasNoInfs())
5085 KnownNotFromFlags |= fcInf;
5086 }
5087
5088 KnownFPClass AssumedClasses = computeKnownFPClassFromContext(V, Q);
5089 KnownNotFromFlags |= ~AssumedClasses.KnownFPClasses;
5090
5091 // We no longer need to find out about these bits from inputs if we can
5092 // assume this from flags/attributes.
5093 InterestedClasses &= ~KnownNotFromFlags;
5094
5095 llvm::scope_exit ClearClassesFromFlags([=, &Known] {
5096 Known.knownNot(KnownNotFromFlags);
5097 if (!Known.SignBit && AssumedClasses.SignBit) {
5098 if (*AssumedClasses.SignBit)
5099 Known.signBitMustBeOne();
5100 else
5101 Known.signBitMustBeZero();
5102 }
5103 });
5104
5105 if (!Op)
5106 return;
5107
5108 // All recursive calls that increase depth must come after this.
5110 return;
5111
5112 const unsigned Opc = Op->getOpcode();
5113 switch (Opc) {
5114 case Instruction::FNeg: {
5115 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5116 Known, Q, Depth + 1);
5117 Known.fneg();
5118 break;
5119 }
5120 case Instruction::Select: {
5121 auto ComputeForArm = [&](Value *Arm, bool Invert) {
5122 KnownFPClass Res;
5123 computeKnownFPClass(Arm, DemandedElts, InterestedClasses, Res, Q,
5124 Depth + 1);
5125 adjustKnownFPClassForSelectArm(Res, Op->getOperand(0), Arm, Invert, Q,
5126 Depth);
5127 return Res;
5128 };
5129 // Only known if known in both the LHS and RHS.
5130 Known =
5131 ComputeForArm(Op->getOperand(1), /*Invert=*/false)
5132 .intersectWith(ComputeForArm(Op->getOperand(2), /*Invert=*/true));
5133 break;
5134 }
5135 case Instruction::Load: {
5136 const MDNode *NoFPClass =
5137 cast<LoadInst>(Op)->getMetadata(LLVMContext::MD_nofpclass);
5138 if (!NoFPClass)
5139 break;
5140
5141 ConstantInt *MaskVal =
5143 Known.knownNot(static_cast<FPClassTest>(MaskVal->getZExtValue()));
5144 break;
5145 }
5146 case Instruction::Call: {
5147 const CallInst *II = cast<CallInst>(Op);
5148 const Intrinsic::ID IID = II->getIntrinsicID();
5149 switch (IID) {
5150 case Intrinsic::fabs: {
5151 if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) {
5152 // If we only care about the sign bit we don't need to inspect the
5153 // operand.
5154 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5155 InterestedClasses, Known, Q, Depth + 1);
5156 }
5157
5158 Known.fabs();
5159 break;
5160 }
5161 case Intrinsic::copysign: {
5162 KnownFPClass KnownSign;
5163
5164 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5165 Known, Q, Depth + 1);
5166 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5167 KnownSign, Q, Depth + 1);
5168 Known.copysign(KnownSign);
5169 break;
5170 }
5171 case Intrinsic::fma:
5172 case Intrinsic::fmuladd: {
5173 if ((InterestedClasses & fcNegative) == fcNone)
5174 break;
5175
5176 // FIXME: This should check isGuaranteedNotToBeUndef
5177 if (II->getArgOperand(0) == II->getArgOperand(1)) {
5178 KnownFPClass KnownSrc, KnownAddend;
5179 computeKnownFPClass(II->getArgOperand(2), DemandedElts,
5180 InterestedClasses, KnownAddend, Q, Depth + 1);
5181 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5182 InterestedClasses, KnownSrc, Q, Depth + 1);
5183
5184 const Function *F = II->getFunction();
5185 const fltSemantics &FltSem =
5186 II->getType()->getScalarType()->getFltSemantics();
5188 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5189
5190 if (KnownNotFromFlags & fcNan) {
5191 KnownSrc.knownNot(fcNan);
5192 KnownAddend.knownNot(fcNan);
5193 }
5194
5195 if (KnownNotFromFlags & fcInf) {
5196 KnownSrc.knownNot(fcInf);
5197 KnownAddend.knownNot(fcInf);
5198 }
5199
5200 Known = KnownFPClass::fma_square(KnownSrc, KnownAddend, Mode);
5201 break;
5202 }
5203
5204 KnownFPClass KnownSrc[3];
5205 for (int I = 0; I != 3; ++I) {
5206 computeKnownFPClass(II->getArgOperand(I), DemandedElts,
5207 InterestedClasses, KnownSrc[I], Q, Depth + 1);
5208 if (KnownSrc[I].isUnknown())
5209 return;
5210
5211 if (KnownNotFromFlags & fcNan)
5212 KnownSrc[I].knownNot(fcNan);
5213 if (KnownNotFromFlags & fcInf)
5214 KnownSrc[I].knownNot(fcInf);
5215 }
5216
5217 const Function *F = II->getFunction();
5218 const fltSemantics &FltSem =
5219 II->getType()->getScalarType()->getFltSemantics();
5221 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5222 Known = KnownFPClass::fma(KnownSrc[0], KnownSrc[1], KnownSrc[2], Mode);
5223 break;
5224 }
5225 case Intrinsic::sqrt:
5226 case Intrinsic::experimental_constrained_sqrt: {
5227 KnownFPClass KnownSrc;
5228 FPClassTest InterestedSrcs = InterestedClasses;
5229 if (InterestedClasses & fcNan)
5230 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5231
5232 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5233 KnownSrc, Q, Depth + 1);
5234
5236
5237 bool HasNSZ = Q.IIQ.hasNoSignedZeros(II);
5238 if (!HasNSZ) {
5239 const Function *F = II->getFunction();
5240 const fltSemantics &FltSem =
5241 II->getType()->getScalarType()->getFltSemantics();
5242 Mode = F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5243 }
5244
5245 Known = KnownFPClass::sqrt(KnownSrc, Mode);
5246 if (HasNSZ)
5247 Known.knownNot(fcNegZero);
5248
5249 break;
5250 }
5251 case Intrinsic::sin:
5252 case Intrinsic::cos: {
5253 // Return NaN on infinite inputs.
5254 KnownFPClass KnownSrc;
5255 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5256 KnownSrc, Q, Depth + 1);
5257 Known = IID == Intrinsic::sin ? KnownFPClass::sin(KnownSrc)
5258 : KnownFPClass::cos(KnownSrc);
5259 break;
5260 }
5261 case Intrinsic::maxnum:
5262 case Intrinsic::minnum:
5263 case Intrinsic::minimum:
5264 case Intrinsic::maximum:
5265 case Intrinsic::minimumnum:
5266 case Intrinsic::maximumnum: {
5267 KnownFPClass KnownLHS, KnownRHS;
5268 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5269 KnownLHS, Q, Depth + 1);
5270 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5271 KnownRHS, Q, Depth + 1);
5272
5273 const Function *F = II->getFunction();
5274
5276 F ? F->getDenormalMode(
5277 II->getType()->getScalarType()->getFltSemantics())
5279
5280 Known = KnownFPClass::minMaxLike(KnownLHS, KnownRHS, getMinMaxKind(IID),
5281 Mode);
5282 break;
5283 }
5284 case Intrinsic::canonicalize: {
5285 KnownFPClass KnownSrc;
5286 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5287 KnownSrc, Q, Depth + 1);
5288
5289 const Function *F = II->getFunction();
5290 DenormalMode DenormMode =
5291 F ? F->getDenormalMode(
5292 II->getType()->getScalarType()->getFltSemantics())
5294 Known = KnownFPClass::canonicalize(KnownSrc, DenormMode);
5295 break;
5296 }
5297 case Intrinsic::vector_reduce_fmax:
5298 case Intrinsic::vector_reduce_fmin:
5299 case Intrinsic::vector_reduce_fmaximum:
5300 case Intrinsic::vector_reduce_fminimum: {
5301 // reduce min/max will choose an element from one of the vector elements,
5302 // so we can infer and class information that is common to all elements.
5303 Known = computeKnownFPClass(II->getArgOperand(0), II->getFastMathFlags(),
5304 InterestedClasses, Q, Depth + 1);
5305 // Can only propagate sign if output is never NaN.
5306 if (!Known.isKnownNeverNaN())
5307 Known.SignBit.reset();
5308 break;
5309 }
5310 // reverse preserves all characteristics of the input vec's element.
5311 case Intrinsic::vector_reverse:
5312 Known = computeKnownFPClass(
5313 II->getArgOperand(0), DemandedElts.reverseBits(),
5314 II->getFastMathFlags(), InterestedClasses, Q, Depth + 1);
5315 break;
5316 case Intrinsic::trunc:
5317 case Intrinsic::floor:
5318 case Intrinsic::ceil:
5319 case Intrinsic::rint:
5320 case Intrinsic::nearbyint:
5321 case Intrinsic::round:
5322 case Intrinsic::roundeven: {
5323 KnownFPClass KnownSrc;
5324 FPClassTest InterestedSrcs = InterestedClasses;
5325 if (InterestedSrcs & fcPosFinite)
5326 InterestedSrcs |= fcPosFinite;
5327 if (InterestedSrcs & fcNegFinite)
5328 InterestedSrcs |= fcNegFinite;
5329 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5330 KnownSrc, Q, Depth + 1);
5331
5333 KnownSrc, IID == Intrinsic::trunc,
5334 V->getType()->getScalarType()->isMultiUnitFPType());
5335 break;
5336 }
5337 case Intrinsic::exp:
5338 case Intrinsic::exp2:
5339 case Intrinsic::exp10:
5340 case Intrinsic::amdgcn_exp2: {
5341 KnownFPClass KnownSrc;
5342 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5343 KnownSrc, Q, Depth + 1);
5344
5345 Known = KnownFPClass::exp(KnownSrc);
5346
5347 Type *EltTy = II->getType()->getScalarType();
5348 if (IID == Intrinsic::amdgcn_exp2 && EltTy->isFloatTy())
5349 Known.knownNot(fcSubnormal);
5350
5351 break;
5352 }
5353 case Intrinsic::fptrunc_round: {
5354 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,
5355 Q, Depth);
5356 break;
5357 }
5358 case Intrinsic::log:
5359 case Intrinsic::log10:
5360 case Intrinsic::log2:
5361 case Intrinsic::experimental_constrained_log:
5362 case Intrinsic::experimental_constrained_log10:
5363 case Intrinsic::experimental_constrained_log2:
5364 case Intrinsic::amdgcn_log: {
5365 Type *EltTy = II->getType()->getScalarType();
5366
5367 // log(+inf) -> +inf
5368 // log([+-]0.0) -> -inf
5369 // log(-inf) -> nan
5370 // log(-x) -> nan
5371 if ((InterestedClasses & (fcNan | fcInf)) != fcNone) {
5372 FPClassTest InterestedSrcs = InterestedClasses;
5373 if ((InterestedClasses & fcNegInf) != fcNone)
5374 InterestedSrcs |= fcZero | fcSubnormal;
5375 if ((InterestedClasses & fcNan) != fcNone)
5376 InterestedSrcs |= fcNan | fcNegative;
5377
5378 KnownFPClass KnownSrc;
5379 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5380 KnownSrc, Q, Depth + 1);
5381
5382 const Function *F = II->getFunction();
5383 DenormalMode Mode = F ? F->getDenormalMode(EltTy->getFltSemantics())
5385 Known = KnownFPClass::log(KnownSrc, Mode);
5386 }
5387
5388 break;
5389 }
5390 case Intrinsic::powi: {
5391 if ((InterestedClasses & fcNegative) == fcNone)
5392 break;
5393
5394 const Value *Exp = II->getArgOperand(1);
5395 Type *ExpTy = Exp->getType();
5396 unsigned BitWidth = ExpTy->getScalarType()->getIntegerBitWidth();
5397 KnownBits ExponentKnownBits(BitWidth);
5398 computeKnownBits(Exp, isa<VectorType>(ExpTy) ? DemandedElts : APInt(1, 1),
5399 ExponentKnownBits, Q, Depth + 1);
5400
5401 KnownFPClass KnownSrc;
5402 if (ExponentKnownBits.isZero() || !ExponentKnownBits.isEven()) {
5403 computeKnownFPClass(II->getArgOperand(0), DemandedElts, fcNegative,
5404 KnownSrc, Q, Depth + 1);
5405 }
5406
5407 Known = KnownFPClass::powi(KnownSrc, ExponentKnownBits);
5408 break;
5409 }
5410 case Intrinsic::ldexp: {
5411 KnownFPClass KnownSrc;
5412 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5413 KnownSrc, Q, Depth + 1);
5414 // Can refine inf/zero handling based on the exponent operand.
5415 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;
5416
5417 KnownBits ExpBits;
5418 if ((KnownSrc.KnownFPClasses & ExpInfoMask) != fcNone) {
5419 const Value *ExpArg = II->getArgOperand(1);
5420 ExpBits = computeKnownBits(ExpArg, DemandedElts, Q, Depth + 1);
5421 }
5422
5423 const fltSemantics &Flt =
5424 II->getType()->getScalarType()->getFltSemantics();
5425
5426 const Function *F = II->getFunction();
5428 F ? F->getDenormalMode(Flt) : DenormalMode::getDynamic();
5429
5430 Known = KnownFPClass::ldexp(KnownSrc, ExpBits, Flt, Mode);
5431 break;
5432 }
5433 case Intrinsic::arithmetic_fence: {
5434 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5435 Known, Q, Depth + 1);
5436 break;
5437 }
5438 case Intrinsic::experimental_constrained_sitofp:
5439 case Intrinsic::experimental_constrained_uitofp:
5440 // Cannot produce nan
5441 Known.knownNot(fcNan);
5442
5443 // sitofp and uitofp turn into +0.0 for zero.
5444 Known.knownNot(fcNegZero);
5445
5446 // Integers cannot be subnormal
5447 Known.knownNot(fcSubnormal);
5448
5449 if (IID == Intrinsic::experimental_constrained_uitofp)
5450 Known.signBitMustBeZero();
5451
5452 // TODO: Copy inf handling from instructions
5453 break;
5454
5455 case Intrinsic::amdgcn_fract: {
5456 Known.knownNot(fcInf);
5457
5458 if (InterestedClasses & fcNan) {
5459 KnownFPClass KnownSrc;
5460 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5461 InterestedClasses, KnownSrc, Q, Depth + 1);
5462
5463 if (KnownSrc.isKnownNeverInfOrNaN())
5464 Known.knownNot(fcNan);
5465 else if (KnownSrc.isKnownNever(fcSNan))
5466 Known.knownNot(fcSNan);
5467 }
5468
5469 break;
5470 }
5471 case Intrinsic::amdgcn_rcp: {
5472 KnownFPClass KnownSrc;
5473 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5474 KnownSrc, Q, Depth + 1);
5475
5476 Known.propagateNaN(KnownSrc);
5477
5478 Type *EltTy = II->getType()->getScalarType();
5479
5480 // f32 denormal always flushed.
5481 if (EltTy->isFloatTy()) {
5482 Known.knownNot(fcSubnormal);
5483 KnownSrc.knownNot(fcSubnormal);
5484 }
5485
5486 if (KnownSrc.isKnownNever(fcNegative))
5487 Known.knownNot(fcNegative);
5488 if (KnownSrc.isKnownNever(fcPositive))
5489 Known.knownNot(fcPositive);
5490
5491 if (const Function *F = II->getFunction()) {
5492 DenormalMode Mode = F->getDenormalMode(EltTy->getFltSemantics());
5493 if (KnownSrc.isKnownNeverLogicalPosZero(Mode))
5494 Known.knownNot(fcPosInf);
5495 if (KnownSrc.isKnownNeverLogicalNegZero(Mode))
5496 Known.knownNot(fcNegInf);
5497 }
5498
5499 break;
5500 }
5501 case Intrinsic::amdgcn_rsq: {
5502 KnownFPClass KnownSrc;
5503 // The only negative value that can be returned is -inf for -0 inputs.
5505
5506 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5507 KnownSrc, Q, Depth + 1);
5508
5509 // Negative -> nan
5510 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
5511 Known.knownNot(fcNan);
5512 else if (KnownSrc.isKnownNever(fcSNan))
5513 Known.knownNot(fcSNan);
5514
5515 // +inf -> +0
5516 if (KnownSrc.isKnownNeverPosInfinity())
5517 Known.knownNot(fcPosZero);
5518
5519 Type *EltTy = II->getType()->getScalarType();
5520
5521 // f32 denormal always flushed.
5522 if (EltTy->isFloatTy())
5523 Known.knownNot(fcPosSubnormal);
5524
5525 if (const Function *F = II->getFunction()) {
5526 DenormalMode Mode = F->getDenormalMode(EltTy->getFltSemantics());
5527
5528 // -0 -> -inf
5529 if (KnownSrc.isKnownNeverLogicalNegZero(Mode))
5530 Known.knownNot(fcNegInf);
5531
5532 // +0 -> +inf
5533 if (KnownSrc.isKnownNeverLogicalPosZero(Mode))
5534 Known.knownNot(fcPosInf);
5535 }
5536
5537 break;
5538 }
5539 case Intrinsic::amdgcn_trig_preop: {
5540 // Always returns a value [0, 1)
5541 Known.knownNot(fcNan | fcInf | fcNegative);
5542 break;
5543 }
5544 default:
5545 break;
5546 }
5547
5548 break;
5549 }
5550 case Instruction::FAdd:
5551 case Instruction::FSub: {
5552 KnownFPClass KnownLHS, KnownRHS;
5553 bool WantNegative =
5554 Op->getOpcode() == Instruction::FAdd &&
5555 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;
5556 bool WantNaN = (InterestedClasses & fcNan) != fcNone;
5557 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;
5558
5559 if (!WantNaN && !WantNegative && !WantNegZero)
5560 break;
5561
5562 FPClassTest InterestedSrcs = InterestedClasses;
5563 if (WantNegative)
5564 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5565 if (InterestedClasses & fcNan)
5566 InterestedSrcs |= fcInf;
5567 computeKnownFPClass(Op->getOperand(1), DemandedElts, InterestedSrcs,
5568 KnownRHS, Q, Depth + 1);
5569
5570 // Special case fadd x, x, which is the canonical form of fmul x, 2.
5571 bool Self = Op->getOperand(0) == Op->getOperand(1) &&
5572 isGuaranteedNotToBeUndef(Op->getOperand(0), Q.AC, Q.CxtI, Q.DT,
5573 Depth + 1);
5574 if (Self)
5575 KnownLHS = KnownRHS;
5576
5577 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||
5578 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) ||
5579 WantNegZero || Opc == Instruction::FSub) {
5580
5581 // FIXME: Context function should always be passed in separately
5582 const Function *F = cast<Instruction>(Op)->getFunction();
5583 const fltSemantics &FltSem =
5584 Op->getType()->getScalarType()->getFltSemantics();
5586 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5587
5588 if (Self && Opc == Instruction::FAdd) {
5589 Known = KnownFPClass::fadd_self(KnownLHS, Mode);
5590 } else {
5591 // RHS is canonically cheaper to compute. Skip inspecting the LHS if
5592 // there's no point.
5593
5594 if (!Self) {
5595 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedSrcs,
5596 KnownLHS, Q, Depth + 1);
5597 }
5598
5599 Known = Opc == Instruction::FAdd
5600 ? KnownFPClass::fadd(KnownLHS, KnownRHS, Mode)
5601 : KnownFPClass::fsub(KnownLHS, KnownRHS, Mode);
5602 }
5603 }
5604
5605 break;
5606 }
5607 case Instruction::FMul: {
5608 const Function *F = cast<Instruction>(Op)->getFunction();
5610 F ? F->getDenormalMode(
5611 Op->getType()->getScalarType()->getFltSemantics())
5613
5614 Value *LHS = Op->getOperand(0);
5615 Value *RHS = Op->getOperand(1);
5616 // X * X is always non-negative or a NaN.
5617 // FIXME: Should check isGuaranteedNotToBeUndef
5618 if (LHS == RHS) {
5619 KnownFPClass KnownSrc;
5620 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownSrc, Q,
5621 Depth + 1);
5622 Known = KnownFPClass::square(KnownSrc, Mode);
5623 break;
5624 }
5625
5626 KnownFPClass KnownLHS, KnownRHS;
5627
5628 const APFloat *CRHS;
5629 if (match(RHS, m_APFloat(CRHS))) {
5630 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Q,
5631 Depth + 1);
5632 Known = KnownFPClass::fmul(KnownLHS, *CRHS, Mode);
5633 } else {
5634 computeKnownFPClass(RHS, DemandedElts, fcAllFlags, KnownRHS, Q,
5635 Depth + 1);
5636 // TODO: Improve accuracy in unfused FMA pattern. We can prove an
5637 // additional not-nan if the addend is known-not negative infinity if the
5638 // multiply is known-not infinity.
5639
5640 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Q,
5641 Depth + 1);
5642 Known = KnownFPClass::fmul(KnownLHS, KnownRHS, Mode);
5643 }
5644
5645 /// Propgate no-infs if the other source is known smaller than one, such
5646 /// that this cannot introduce overflow.
5648 Known.knownNot(fcInf);
5649 else if (KnownRHS.isKnownNever(fcInf) && isAbsoluteValueLessEqualOne(LHS))
5650 Known.knownNot(fcInf);
5651
5652 break;
5653 }
5654 case Instruction::FDiv:
5655 case Instruction::FRem: {
5656 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
5657
5658 if (Op->getOpcode() == Instruction::FRem)
5659 Known.knownNot(fcInf);
5660
5661 if (Op->getOperand(0) == Op->getOperand(1) &&
5662 isGuaranteedNotToBeUndef(Op->getOperand(0), Q.AC, Q.CxtI, Q.DT)) {
5663 if (Op->getOpcode() == Instruction::FDiv) {
5664 // X / X is always exactly 1.0 or a NaN.
5666 } else {
5667 // X % X is always exactly [+-]0.0 or a NaN.
5668 Known.KnownFPClasses = fcNan | fcZero;
5669 }
5670
5671 if (!WantNan)
5672 break;
5673
5674 KnownFPClass KnownSrc;
5675 computeKnownFPClass(Op->getOperand(0), DemandedElts,
5676 fcNan | fcInf | fcZero | fcSubnormal, KnownSrc, Q,
5677 Depth + 1);
5678 const Function *F = cast<Instruction>(Op)->getFunction();
5679 const fltSemantics &FltSem =
5680 Op->getType()->getScalarType()->getFltSemantics();
5681
5683 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5684
5685 Known = Op->getOpcode() == Instruction::FDiv
5686 ? KnownFPClass::fdiv_self(KnownSrc, Mode)
5687 : KnownFPClass::frem_self(KnownSrc, Mode);
5688 break;
5689 }
5690
5691 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
5692 const bool WantPositive =
5693 Opc == Instruction::FRem && (InterestedClasses & fcPositive) != fcNone;
5694 if (!WantNan && !WantNegative && !WantPositive)
5695 break;
5696
5697 KnownFPClass KnownLHS, KnownRHS;
5698
5699 computeKnownFPClass(Op->getOperand(1), DemandedElts,
5700 fcNan | fcInf | fcZero | fcNegative, KnownRHS, Q,
5701 Depth + 1);
5702
5703 bool KnowSomethingUseful = KnownRHS.isKnownNeverNaN() ||
5704 KnownRHS.isKnownNever(fcNegative) ||
5705 KnownRHS.isKnownNever(fcPositive);
5706
5707 if (KnowSomethingUseful || WantPositive) {
5708 computeKnownFPClass(Op->getOperand(0), DemandedElts, fcAllFlags, KnownLHS,
5709 Q, Depth + 1);
5710 }
5711
5712 const Function *F = cast<Instruction>(Op)->getFunction();
5713 const fltSemantics &FltSem =
5714 Op->getType()->getScalarType()->getFltSemantics();
5715
5716 if (Op->getOpcode() == Instruction::FDiv) {
5718 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5719 Known = KnownFPClass::fdiv(KnownLHS, KnownRHS, Mode);
5720 } else {
5721 // Inf REM x and x REM 0 produce NaN.
5722 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5723 KnownLHS.isKnownNeverInfinity() && F &&
5724 KnownRHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))) {
5725 Known.knownNot(fcNan);
5726 }
5727
5728 // The sign for frem is the same as the first operand.
5729 if (KnownLHS.cannotBeOrderedLessThanZero())
5731 if (KnownLHS.cannotBeOrderedGreaterThanZero())
5733
5734 // See if we can be more aggressive about the sign of 0.
5735 if (KnownLHS.isKnownNever(fcNegative))
5736 Known.knownNot(fcNegative);
5737 if (KnownLHS.isKnownNever(fcPositive))
5738 Known.knownNot(fcPositive);
5739 }
5740
5741 break;
5742 }
5743 case Instruction::FPExt: {
5744 KnownFPClass KnownSrc;
5745 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5746 KnownSrc, Q, Depth + 1);
5747
5748 const fltSemantics &DstTy =
5749 Op->getType()->getScalarType()->getFltSemantics();
5750 const fltSemantics &SrcTy =
5751 Op->getOperand(0)->getType()->getScalarType()->getFltSemantics();
5752
5753 Known = KnownFPClass::fpext(KnownSrc, DstTy, SrcTy);
5754 break;
5755 }
5756 case Instruction::FPTrunc: {
5757 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known, Q,
5758 Depth);
5759 break;
5760 }
5761 case Instruction::SIToFP:
5762 case Instruction::UIToFP: {
5763 // Cannot produce nan
5764 Known.knownNot(fcNan);
5765
5766 // Integers cannot be subnormal
5767 Known.knownNot(fcSubnormal);
5768
5769 // sitofp and uitofp turn into +0.0 for zero.
5770 Known.knownNot(fcNegZero);
5771
5772 // UIToFP is always non-negative regardless of known bits.
5773 if (Op->getOpcode() == Instruction::UIToFP)
5774 Known.signBitMustBeZero();
5775
5776 // Only compute known bits if we can learn something useful from them.
5777 if (!(InterestedClasses & (fcPosZero | fcNormal | fcInf)))
5778 break;
5779
5780 KnownBits IntKnown =
5781 computeKnownBits(Op->getOperand(0), DemandedElts, Q, Depth + 1);
5782
5783 // If the integer is non-zero, the result cannot be +0.0
5784 if (IntKnown.isNonZero())
5785 Known.knownNot(fcPosZero);
5786
5787 if (Op->getOpcode() == Instruction::SIToFP) {
5788 // If the signed integer is known non-negative, the result is
5789 // non-negative. If the signed integer is known negative, the result is
5790 // negative.
5791 if (IntKnown.isNonNegative()) {
5792 Known.signBitMustBeZero();
5793 } else if (IntKnown.isNegative()) {
5794 Known.signBitMustBeOne();
5795 }
5796 }
5797
5798 // Guard kept for ilogb()
5799 if (InterestedClasses & fcInf) {
5800 // Get width of largest magnitude integer known.
5801 // This still works for a signed minimum value because the largest FP
5802 // value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx).
5803 int IntSize = IntKnown.getBitWidth();
5804 if (Op->getOpcode() == Instruction::UIToFP)
5805 IntSize -= IntKnown.countMinLeadingZeros();
5806 else if (Op->getOpcode() == Instruction::SIToFP)
5807 IntSize -= IntKnown.countMinSignBits();
5808
5809 // If the exponent of the largest finite FP value can hold the largest
5810 // integer, the result of the cast must be finite.
5811 Type *FPTy = Op->getType()->getScalarType();
5812 if (ilogb(APFloat::getLargest(FPTy->getFltSemantics())) >= IntSize)
5813 Known.knownNot(fcInf);
5814 }
5815
5816 break;
5817 }
5818 case Instruction::ExtractElement: {
5819 // Look through extract element. If the index is non-constant or
5820 // out-of-range demand all elements, otherwise just the extracted element.
5821 const Value *Vec = Op->getOperand(0);
5822
5823 APInt DemandedVecElts;
5824 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
5825 unsigned NumElts = VecTy->getNumElements();
5826 DemandedVecElts = APInt::getAllOnes(NumElts);
5827 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(1));
5828 if (CIdx && CIdx->getValue().ult(NumElts))
5829 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
5830 } else {
5831 DemandedVecElts = APInt(1, 1);
5832 }
5833
5834 return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,
5835 Q, Depth + 1);
5836 }
5837 case Instruction::InsertElement: {
5838 if (isa<ScalableVectorType>(Op->getType()))
5839 return;
5840
5841 const Value *Vec = Op->getOperand(0);
5842 const Value *Elt = Op->getOperand(1);
5843 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(2));
5844 unsigned NumElts = DemandedElts.getBitWidth();
5845 APInt DemandedVecElts = DemandedElts;
5846 bool NeedsElt = true;
5847 // If we know the index we are inserting to, clear it from Vec check.
5848 if (CIdx && CIdx->getValue().ult(NumElts)) {
5849 DemandedVecElts.clearBit(CIdx->getZExtValue());
5850 NeedsElt = DemandedElts[CIdx->getZExtValue()];
5851 }
5852
5853 // Do we demand the inserted element?
5854 if (NeedsElt) {
5855 computeKnownFPClass(Elt, Known, InterestedClasses, Q, Depth + 1);
5856 // If we don't know any bits, early out.
5857 if (Known.isUnknown())
5858 break;
5859 } else {
5860 Known.KnownFPClasses = fcNone;
5861 }
5862
5863 // Do we need anymore elements from Vec?
5864 if (!DemandedVecElts.isZero()) {
5865 KnownFPClass Known2;
5866 computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2, Q,
5867 Depth + 1);
5868 Known |= Known2;
5869 }
5870
5871 break;
5872 }
5873 case Instruction::ShuffleVector: {
5874 // Handle vector splat idiom
5875 if (Value *Splat = getSplatValue(V)) {
5876 computeKnownFPClass(Splat, Known, InterestedClasses, Q, Depth + 1);
5877 break;
5878 }
5879
5880 // For undef elements, we don't know anything about the common state of
5881 // the shuffle result.
5882 APInt DemandedLHS, DemandedRHS;
5883 auto *Shuf = dyn_cast<ShuffleVectorInst>(Op);
5884 if (!Shuf || !getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
5885 return;
5886
5887 if (!!DemandedLHS) {
5888 const Value *LHS = Shuf->getOperand(0);
5889 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known, Q,
5890 Depth + 1);
5891
5892 // If we don't know any bits, early out.
5893 if (Known.isUnknown())
5894 break;
5895 } else {
5896 Known.KnownFPClasses = fcNone;
5897 }
5898
5899 if (!!DemandedRHS) {
5900 KnownFPClass Known2;
5901 const Value *RHS = Shuf->getOperand(1);
5902 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2, Q,
5903 Depth + 1);
5904 Known |= Known2;
5905 }
5906
5907 break;
5908 }
5909 case Instruction::ExtractValue: {
5910 const ExtractValueInst *Extract = cast<ExtractValueInst>(Op);
5911 ArrayRef<unsigned> Indices = Extract->getIndices();
5912 const Value *Src = Extract->getAggregateOperand();
5913 if (isa<StructType>(Src->getType()) && Indices.size() == 1 &&
5914 Indices[0] == 0) {
5915 if (const auto *II = dyn_cast<IntrinsicInst>(Src)) {
5916 switch (II->getIntrinsicID()) {
5917 case Intrinsic::frexp: {
5918 Known.knownNot(fcSubnormal);
5919
5920 KnownFPClass KnownSrc;
5921 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5922 InterestedClasses, KnownSrc, Q, Depth + 1);
5923
5924 const Function *F = cast<Instruction>(Op)->getFunction();
5925 const fltSemantics &FltSem =
5926 Op->getType()->getScalarType()->getFltSemantics();
5927
5929 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5930 Known = KnownFPClass::frexp_mant(KnownSrc, Mode);
5931 return;
5932 }
5933 default:
5934 break;
5935 }
5936 }
5937 }
5938
5939 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Q,
5940 Depth + 1);
5941 break;
5942 }
5943 case Instruction::PHI: {
5944 const PHINode *P = cast<PHINode>(Op);
5945 // Unreachable blocks may have zero-operand PHI nodes.
5946 if (P->getNumIncomingValues() == 0)
5947 break;
5948
5949 // Otherwise take the unions of the known bit sets of the operands,
5950 // taking conservative care to avoid excessive recursion.
5951 const unsigned PhiRecursionLimit = MaxAnalysisRecursionDepth - 2;
5952
5953 if (Depth < PhiRecursionLimit) {
5954 // Skip if every incoming value references to ourself.
5955 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
5956 break;
5957
5958 bool First = true;
5959
5960 for (const Use &U : P->operands()) {
5961 Value *IncValue;
5962 Instruction *CxtI;
5963 breakSelfRecursivePHI(&U, P, IncValue, CxtI);
5964 // Skip direct self references.
5965 if (IncValue == P)
5966 continue;
5967
5968 KnownFPClass KnownSrc;
5969 // Recurse, but cap the recursion to two levels, because we don't want
5970 // to waste time spinning around in loops. We need at least depth 2 to
5971 // detect known sign bits.
5972 computeKnownFPClass(IncValue, DemandedElts, InterestedClasses, KnownSrc,
5974 PhiRecursionLimit);
5975
5976 if (First) {
5977 Known = KnownSrc;
5978 First = false;
5979 } else {
5980 Known |= KnownSrc;
5981 }
5982
5983 if (Known.KnownFPClasses == fcAllFlags)
5984 break;
5985 }
5986 }
5987
5988 // Look for the case of a for loop which has a positive
5989 // initial value and is incremented by a squared value.
5990 // This will propagate sign information out of such loops.
5991 if (P->getNumIncomingValues() != 2 || Known.cannotBeOrderedLessThanZero())
5992 break;
5993 for (unsigned I = 0; I < 2; I++) {
5994 Value *RecurValue = P->getIncomingValue(1 - I);
5996 if (!II)
5997 continue;
5998 Value *R, *L, *Init;
5999 PHINode *PN;
6001 PN == P) {
6002 switch (II->getIntrinsicID()) {
6003 case Intrinsic::fma:
6004 case Intrinsic::fmuladd: {
6005 KnownFPClass KnownStart;
6006 computeKnownFPClass(Init, DemandedElts, InterestedClasses, KnownStart,
6007 Q, Depth + 1);
6008 if (KnownStart.cannotBeOrderedLessThanZero() && L == R &&
6009 isGuaranteedNotToBeUndef(L, Q.AC, Q.CxtI, Q.DT, Depth + 1))
6011 break;
6012 }
6013 }
6014 }
6015 }
6016 break;
6017 }
6018 case Instruction::BitCast: {
6019 const Value *Src;
6020 if (!match(Op, m_ElementWiseBitCast(m_Value(Src))) ||
6021 !Src->getType()->isIntOrIntVectorTy())
6022 break;
6023
6024 const Type *Ty = Op->getType();
6025
6026 Value *CastLHS, *CastRHS;
6027
6028 // Match bitcast(umax(bitcast(a), bitcast(b)))
6029 if (match(Src, m_c_MaxOrMin(m_BitCast(m_Value(CastLHS)),
6030 m_BitCast(m_Value(CastRHS)))) &&
6031 CastLHS->getType() == Ty && CastRHS->getType() == Ty) {
6032 KnownFPClass KnownLHS, KnownRHS;
6033 computeKnownFPClass(CastRHS, DemandedElts, InterestedClasses, KnownRHS, Q,
6034 Depth + 1);
6035 if (!KnownRHS.isUnknown()) {
6036 computeKnownFPClass(CastLHS, DemandedElts, InterestedClasses, KnownLHS,
6037 Q, Depth + 1);
6038 Known = KnownLHS | KnownRHS;
6039 }
6040
6041 return;
6042 }
6043
6044 const Type *EltTy = Ty->getScalarType();
6045 KnownBits Bits(EltTy->getPrimitiveSizeInBits());
6046 computeKnownBits(Src, DemandedElts, Bits, Q, Depth + 1);
6047
6048 Known = KnownFPClass::bitcast(EltTy->getFltSemantics(), Bits);
6049 break;
6050 }
6051 default:
6052 break;
6053 }
6054}
6055
6057 const APInt &DemandedElts,
6058 FPClassTest InterestedClasses,
6059 const SimplifyQuery &SQ,
6060 unsigned Depth) {
6061 KnownFPClass KnownClasses;
6062 ::computeKnownFPClass(V, DemandedElts, InterestedClasses, KnownClasses, SQ,
6063 Depth);
6064 return KnownClasses;
6065}
6066
6068 FPClassTest InterestedClasses,
6069 const SimplifyQuery &SQ,
6070 unsigned Depth) {
6071 KnownFPClass Known;
6072 ::computeKnownFPClass(V, Known, InterestedClasses, SQ, Depth);
6073 return Known;
6074}
6075
6077 const Value *V, const DataLayout &DL, FPClassTest InterestedClasses,
6078 const TargetLibraryInfo *TLI, AssumptionCache *AC, const Instruction *CxtI,
6079 const DominatorTree *DT, bool UseInstrInfo, unsigned Depth) {
6080 return computeKnownFPClass(V, InterestedClasses,
6081 SimplifyQuery(DL, TLI, DT, AC, CxtI, UseInstrInfo),
6082 Depth);
6083}
6084
6086llvm::computeKnownFPClass(const Value *V, const APInt &DemandedElts,
6087 FastMathFlags FMF, FPClassTest InterestedClasses,
6088 const SimplifyQuery &SQ, unsigned Depth) {
6089 if (FMF.noNaNs())
6090 InterestedClasses &= ~fcNan;
6091 if (FMF.noInfs())
6092 InterestedClasses &= ~fcInf;
6093
6094 KnownFPClass Result =
6095 computeKnownFPClass(V, DemandedElts, InterestedClasses, SQ, Depth);
6096
6097 if (FMF.noNaNs())
6098 Result.KnownFPClasses &= ~fcNan;
6099 if (FMF.noInfs())
6100 Result.KnownFPClasses &= ~fcInf;
6101 return Result;
6102}
6103
6105 FPClassTest InterestedClasses,
6106 const SimplifyQuery &SQ,
6107 unsigned Depth) {
6108 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
6109 APInt DemandedElts =
6110 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
6111 return computeKnownFPClass(V, DemandedElts, FMF, InterestedClasses, SQ,
6112 Depth);
6113}
6114
6116 unsigned Depth) {
6118 return Known.isKnownNeverNegZero();
6119}
6120
6127
6129 unsigned Depth) {
6131 return Known.isKnownNeverInfinity();
6132}
6133
6134/// Return true if the floating-point value can never contain a NaN or infinity.
6136 unsigned Depth) {
6138 return Known.isKnownNeverNaN() && Known.isKnownNeverInfinity();
6139}
6140
6141/// Return true if the floating-point scalar value is not a NaN or if the
6142/// floating-point vector value has no NaN elements. Return false if a value
6143/// could ever be NaN.
6145 unsigned Depth) {
6147 return Known.isKnownNeverNaN();
6148}
6149
6150/// Return false if we can prove that the specified FP value's sign bit is 0.
6151/// Return true if we can prove that the specified FP value's sign bit is 1.
6152/// Otherwise return std::nullopt.
6153std::optional<bool> llvm::computeKnownFPSignBit(const Value *V,
6154 const SimplifyQuery &SQ,
6155 unsigned Depth) {
6157 return Known.SignBit;
6158}
6159
6161 auto *User = cast<Instruction>(U.getUser());
6162 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6163 if (FPOp->hasNoSignedZeros())
6164 return true;
6165 }
6166
6167 switch (User->getOpcode()) {
6168 case Instruction::FPToSI:
6169 case Instruction::FPToUI:
6170 return true;
6171 case Instruction::FCmp:
6172 // fcmp treats both positive and negative zero as equal.
6173 return true;
6174 case Instruction::Call:
6175 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6176 switch (II->getIntrinsicID()) {
6177 case Intrinsic::fabs:
6178 return true;
6179 case Intrinsic::copysign:
6180 return U.getOperandNo() == 0;
6181 case Intrinsic::is_fpclass:
6182 case Intrinsic::vp_is_fpclass: {
6183 auto Test =
6184 static_cast<FPClassTest>(
6185 cast<ConstantInt>(II->getArgOperand(1))->getZExtValue()) &
6188 }
6189 default:
6190 return false;
6191 }
6192 }
6193 return false;
6194 default:
6195 return false;
6196 }
6197}
6198
6200 auto *User = cast<Instruction>(U.getUser());
6201 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6202 if (FPOp->hasNoNaNs())
6203 return true;
6204 }
6205
6206 switch (User->getOpcode()) {
6207 case Instruction::FPToSI:
6208 case Instruction::FPToUI:
6209 return true;
6210 // Proper FP math operations ignore the sign bit of NaN.
6211 case Instruction::FAdd:
6212 case Instruction::FSub:
6213 case Instruction::FMul:
6214 case Instruction::FDiv:
6215 case Instruction::FRem:
6216 case Instruction::FPTrunc:
6217 case Instruction::FPExt:
6218 case Instruction::FCmp:
6219 return true;
6220 // Bitwise FP operations should preserve the sign bit of NaN.
6221 case Instruction::FNeg:
6222 case Instruction::Select:
6223 case Instruction::PHI:
6224 return false;
6225 case Instruction::Ret:
6226 return User->getFunction()->getAttributes().getRetNoFPClass() &
6228 case Instruction::Call:
6229 case Instruction::Invoke: {
6230 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6231 switch (II->getIntrinsicID()) {
6232 case Intrinsic::fabs:
6233 return true;
6234 case Intrinsic::copysign:
6235 return U.getOperandNo() == 0;
6236 // Other proper FP math intrinsics ignore the sign bit of NaN.
6237 case Intrinsic::maxnum:
6238 case Intrinsic::minnum:
6239 case Intrinsic::maximum:
6240 case Intrinsic::minimum:
6241 case Intrinsic::maximumnum:
6242 case Intrinsic::minimumnum:
6243 case Intrinsic::canonicalize:
6244 case Intrinsic::fma:
6245 case Intrinsic::fmuladd:
6246 case Intrinsic::sqrt:
6247 case Intrinsic::pow:
6248 case Intrinsic::powi:
6249 case Intrinsic::fptoui_sat:
6250 case Intrinsic::fptosi_sat:
6251 case Intrinsic::is_fpclass:
6252 case Intrinsic::vp_is_fpclass:
6253 return true;
6254 default:
6255 return false;
6256 }
6257 }
6258
6259 FPClassTest NoFPClass =
6260 cast<CallBase>(User)->getParamNoFPClass(U.getOperandNo());
6261 return NoFPClass & FPClassTest::fcNan;
6262 }
6263 default:
6264 return false;
6265 }
6266}
6267
6269 FastMathFlags FMF) {
6270 if (isa<PoisonValue>(V))
6271 return true;
6272 if (isa<UndefValue>(V))
6273 return false;
6274
6275 if (match(V, m_CheckedFp([](const APFloat &Val) { return Val.isInteger(); })))
6276 return true;
6277
6279 if (!I)
6280 return false;
6281
6282 switch (I->getOpcode()) {
6283 case Instruction::SIToFP:
6284 case Instruction::UIToFP:
6285 // TODO: Could check nofpclass(inf) on incoming argument
6286 if (FMF.noInfs())
6287 return true;
6288
6289 // Need to check int size cannot produce infinity, which computeKnownFPClass
6290 // knows how to do already.
6291 return isKnownNeverInfinity(I, SQ);
6292 case Instruction::Call: {
6293 const CallInst *CI = cast<CallInst>(I);
6294 switch (CI->getIntrinsicID()) {
6295 case Intrinsic::trunc:
6296 case Intrinsic::floor:
6297 case Intrinsic::ceil:
6298 case Intrinsic::rint:
6299 case Intrinsic::nearbyint:
6300 case Intrinsic::round:
6301 case Intrinsic::roundeven:
6302 return (FMF.noInfs() && FMF.noNaNs()) || isKnownNeverInfOrNaN(I, SQ);
6303 default:
6304 break;
6305 }
6306
6307 break;
6308 }
6309 default:
6310 break;
6311 }
6312
6313 return false;
6314}
6315
6317
6318 // All byte-wide stores are splatable, even of arbitrary variables.
6319 if (V->getType()->isIntegerTy(8))
6320 return V;
6321
6322 LLVMContext &Ctx = V->getContext();
6323
6324 // Undef don't care.
6325 auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
6326 if (isa<UndefValue>(V))
6327 return UndefInt8;
6328
6329 // Return poison for zero-sized type.
6330 if (DL.getTypeStoreSize(V->getType()).isZero())
6331 return PoisonValue::get(Type::getInt8Ty(Ctx));
6332
6334 if (!C) {
6335 // Conceptually, we could handle things like:
6336 // %a = zext i8 %X to i16
6337 // %b = shl i16 %a, 8
6338 // %c = or i16 %a, %b
6339 // but until there is an example that actually needs this, it doesn't seem
6340 // worth worrying about.
6341 return nullptr;
6342 }
6343
6344 // Handle 'null' ConstantArrayZero etc.
6345 if (C->isNullValue())
6347
6348 // Constant floating-point values can be handled as integer values if the
6349 // corresponding integer value is "byteable". An important case is 0.0.
6350 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
6351 Type *ScalarTy = CFP->getType()->getScalarType();
6352 if (ScalarTy->isHalfTy() || ScalarTy->isFloatTy() || ScalarTy->isDoubleTy())
6353 return isBytewiseValue(
6354 ConstantInt::get(Ctx, CFP->getValue().bitcastToAPInt()), DL);
6355
6356 // Don't handle long double formats, which have strange constraints.
6357 return nullptr;
6358 }
6359
6360 // We can handle constant integers that are multiple of 8 bits.
6361 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
6362 if (CI->getBitWidth() % 8 == 0) {
6363 if (!CI->getValue().isSplat(8))
6364 return nullptr;
6365 return ConstantInt::get(Ctx, CI->getValue().trunc(8));
6366 }
6367 }
6368
6369 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
6370 if (CE->getOpcode() == Instruction::IntToPtr) {
6371 if (auto *PtrTy = dyn_cast<PointerType>(CE->getType())) {
6372 unsigned BitWidth = DL.getPointerSizeInBits(PtrTy->getAddressSpace());
6374 CE->getOperand(0), Type::getIntNTy(Ctx, BitWidth), false, DL))
6375 return isBytewiseValue(Op, DL);
6376 }
6377 }
6378 }
6379
6380 auto Merge = [&](Value *LHS, Value *RHS) -> Value * {
6381 if (LHS == RHS)
6382 return LHS;
6383 if (!LHS || !RHS)
6384 return nullptr;
6385 if (LHS == UndefInt8)
6386 return RHS;
6387 if (RHS == UndefInt8)
6388 return LHS;
6389 return nullptr;
6390 };
6391
6393 Value *Val = UndefInt8;
6394 for (uint64_t I = 0, E = CA->getNumElements(); I != E; ++I)
6395 if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))
6396 return nullptr;
6397 return Val;
6398 }
6399
6401 Value *Val = UndefInt8;
6402 for (Value *Op : C->operands())
6403 if (!(Val = Merge(Val, isBytewiseValue(Op, DL))))
6404 return nullptr;
6405 return Val;
6406 }
6407
6408 // Don't try to handle the handful of other constants.
6409 return nullptr;
6410}
6411
6412// This is the recursive version of BuildSubAggregate. It takes a few different
6413// arguments. Idxs is the index within the nested struct From that we are
6414// looking at now (which is of type IndexedType). IdxSkip is the number of
6415// indices from Idxs that should be left out when inserting into the resulting
6416// struct. To is the result struct built so far, new insertvalue instructions
6417// build on that.
6418static Value *BuildSubAggregate(Value *From, Value *To, Type *IndexedType,
6420 unsigned IdxSkip,
6421 BasicBlock::iterator InsertBefore) {
6422 StructType *STy = dyn_cast<StructType>(IndexedType);
6423 if (STy) {
6424 // Save the original To argument so we can modify it
6425 Value *OrigTo = To;
6426 // General case, the type indexed by Idxs is a struct
6427 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
6428 // Process each struct element recursively
6429 Idxs.push_back(i);
6430 Value *PrevTo = To;
6431 To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
6432 InsertBefore);
6433 Idxs.pop_back();
6434 if (!To) {
6435 // Couldn't find any inserted value for this index? Cleanup
6436 while (PrevTo != OrigTo) {
6438 PrevTo = Del->getAggregateOperand();
6439 Del->eraseFromParent();
6440 }
6441 // Stop processing elements
6442 break;
6443 }
6444 }
6445 // If we successfully found a value for each of our subaggregates
6446 if (To)
6447 return To;
6448 }
6449 // Base case, the type indexed by SourceIdxs is not a struct, or not all of
6450 // the struct's elements had a value that was inserted directly. In the latter
6451 // case, perhaps we can't determine each of the subelements individually, but
6452 // we might be able to find the complete struct somewhere.
6453
6454 // Find the value that is at that particular spot
6455 Value *V = FindInsertedValue(From, Idxs);
6456
6457 if (!V)
6458 return nullptr;
6459
6460 // Insert the value in the new (sub) aggregate
6461 return InsertValueInst::Create(To, V, ArrayRef(Idxs).slice(IdxSkip), "tmp",
6462 InsertBefore);
6463}
6464
6465// This helper takes a nested struct and extracts a part of it (which is again a
6466// struct) into a new value. For example, given the struct:
6467// { a, { b, { c, d }, e } }
6468// and the indices "1, 1" this returns
6469// { c, d }.
6470//
6471// It does this by inserting an insertvalue for each element in the resulting
6472// struct, as opposed to just inserting a single struct. This will only work if
6473// each of the elements of the substruct are known (ie, inserted into From by an
6474// insertvalue instruction somewhere).
6475//
6476// All inserted insertvalue instructions are inserted before InsertBefore
6478 BasicBlock::iterator InsertBefore) {
6479 Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
6480 idx_range);
6481 Value *To = PoisonValue::get(IndexedType);
6482 SmallVector<unsigned, 10> Idxs(idx_range);
6483 unsigned IdxSkip = Idxs.size();
6484
6485 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
6486}
6487
6488/// Given an aggregate and a sequence of indices, see if the scalar value
6489/// indexed is already around as a register, for example if it was inserted
6490/// directly into the aggregate.
6491///
6492/// If InsertBefore is not null, this function will duplicate (modified)
6493/// insertvalues when a part of a nested struct is extracted.
6494Value *
6496 std::optional<BasicBlock::iterator> InsertBefore) {
6497 // Nothing to index? Just return V then (this is useful at the end of our
6498 // recursion).
6499 if (idx_range.empty())
6500 return V;
6501 // We have indices, so V should have an indexable type.
6502 assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
6503 "Not looking at a struct or array?");
6504 assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
6505 "Invalid indices for type?");
6506
6507 if (Constant *C = dyn_cast<Constant>(V)) {
6508 C = C->getAggregateElement(idx_range[0]);
6509 if (!C) return nullptr;
6510 return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
6511 }
6512
6514 // Loop the indices for the insertvalue instruction in parallel with the
6515 // requested indices
6516 const unsigned *req_idx = idx_range.begin();
6517 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
6518 i != e; ++i, ++req_idx) {
6519 if (req_idx == idx_range.end()) {
6520 // We can't handle this without inserting insertvalues
6521 if (!InsertBefore)
6522 return nullptr;
6523
6524 // The requested index identifies a part of a nested aggregate. Handle
6525 // this specially. For example,
6526 // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
6527 // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
6528 // %C = extractvalue {i32, { i32, i32 } } %B, 1
6529 // This can be changed into
6530 // %A = insertvalue {i32, i32 } undef, i32 10, 0
6531 // %C = insertvalue {i32, i32 } %A, i32 11, 1
6532 // which allows the unused 0,0 element from the nested struct to be
6533 // removed.
6534 return BuildSubAggregate(V, ArrayRef(idx_range.begin(), req_idx),
6535 *InsertBefore);
6536 }
6537
6538 // This insert value inserts something else than what we are looking for.
6539 // See if the (aggregate) value inserted into has the value we are
6540 // looking for, then.
6541 if (*req_idx != *i)
6542 return FindInsertedValue(I->getAggregateOperand(), idx_range,
6543 InsertBefore);
6544 }
6545 // If we end up here, the indices of the insertvalue match with those
6546 // requested (though possibly only partially). Now we recursively look at
6547 // the inserted value, passing any remaining indices.
6548 return FindInsertedValue(I->getInsertedValueOperand(),
6549 ArrayRef(req_idx, idx_range.end()), InsertBefore);
6550 }
6551
6553 // If we're extracting a value from an aggregate that was extracted from
6554 // something else, we can extract from that something else directly instead.
6555 // However, we will need to chain I's indices with the requested indices.
6556
6557 // Calculate the number of indices required
6558 unsigned size = I->getNumIndices() + idx_range.size();
6559 // Allocate some space to put the new indices in
6561 Idxs.reserve(size);
6562 // Add indices from the extract value instruction
6563 Idxs.append(I->idx_begin(), I->idx_end());
6564
6565 // Add requested indices
6566 Idxs.append(idx_range.begin(), idx_range.end());
6567
6568 assert(Idxs.size() == size
6569 && "Number of indices added not correct?");
6570
6571 return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
6572 }
6573 // Otherwise, we don't know (such as, extracting from a function return value
6574 // or load instruction)
6575 return nullptr;
6576}
6577
6578// If V refers to an initialized global constant, set Slice either to
6579// its initializer if the size of its elements equals ElementSize, or,
6580// for ElementSize == 8, to its representation as an array of unsiged
6581// char. Return true on success.
6582// Offset is in the unit "nr of ElementSize sized elements".
6585 unsigned ElementSize, uint64_t Offset) {
6586 assert(V && "V should not be null.");
6587 assert((ElementSize % 8) == 0 &&
6588 "ElementSize expected to be a multiple of the size of a byte.");
6589 unsigned ElementSizeInBytes = ElementSize / 8;
6590
6591 // Drill down into the pointer expression V, ignoring any intervening
6592 // casts, and determine the identity of the object it references along
6593 // with the cumulative byte offset into it.
6594 const GlobalVariable *GV =
6596 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
6597 // Fail if V is not based on constant global object.
6598 return false;
6599
6600 const DataLayout &DL = GV->getDataLayout();
6601 APInt Off(DL.getIndexTypeSizeInBits(V->getType()), 0);
6602
6603 if (GV != V->stripAndAccumulateConstantOffsets(DL, Off,
6604 /*AllowNonInbounds*/ true))
6605 // Fail if a constant offset could not be determined.
6606 return false;
6607
6608 uint64_t StartIdx = Off.getLimitedValue();
6609 if (StartIdx == UINT64_MAX)
6610 // Fail if the constant offset is excessive.
6611 return false;
6612
6613 // Off/StartIdx is in the unit of bytes. So we need to convert to number of
6614 // elements. Simply bail out if that isn't possible.
6615 if ((StartIdx % ElementSizeInBytes) != 0)
6616 return false;
6617
6618 Offset += StartIdx / ElementSizeInBytes;
6619 ConstantDataArray *Array = nullptr;
6620 ArrayType *ArrayTy = nullptr;
6621
6622 if (GV->getInitializer()->isNullValue()) {
6623 Type *GVTy = GV->getValueType();
6624 uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy).getFixedValue();
6625 uint64_t Length = SizeInBytes / ElementSizeInBytes;
6626
6627 Slice.Array = nullptr;
6628 Slice.Offset = 0;
6629 // Return an empty Slice for undersized constants to let callers
6630 // transform even undefined library calls into simpler, well-defined
6631 // expressions. This is preferable to making the calls although it
6632 // prevents sanitizers from detecting such calls.
6633 Slice.Length = Length < Offset ? 0 : Length - Offset;
6634 return true;
6635 }
6636
6637 auto *Init = const_cast<Constant *>(GV->getInitializer());
6638 if (auto *ArrayInit = dyn_cast<ConstantDataArray>(Init)) {
6639 Type *InitElTy = ArrayInit->getElementType();
6640 if (InitElTy->isIntegerTy(ElementSize)) {
6641 // If Init is an initializer for an array of the expected type
6642 // and size, use it as is.
6643 Array = ArrayInit;
6644 ArrayTy = ArrayInit->getType();
6645 }
6646 }
6647
6648 if (!Array) {
6649 if (ElementSize != 8)
6650 // TODO: Handle conversions to larger integral types.
6651 return false;
6652
6653 // Otherwise extract the portion of the initializer starting
6654 // at Offset as an array of bytes, and reset Offset.
6656 if (!Init)
6657 return false;
6658
6659 Offset = 0;
6661 ArrayTy = dyn_cast<ArrayType>(Init->getType());
6662 }
6663
6664 uint64_t NumElts = ArrayTy->getArrayNumElements();
6665 if (Offset > NumElts)
6666 return false;
6667
6668 Slice.Array = Array;
6669 Slice.Offset = Offset;
6670 Slice.Length = NumElts - Offset;
6671 return true;
6672}
6673
6674/// Extract bytes from the initializer of the constant array V, which need
6675/// not be a nul-terminated string. On success, store the bytes in Str and
6676/// return true. When TrimAtNul is set, Str will contain only the bytes up
6677/// to but not including the first nul. Return false on failure.
6679 bool TrimAtNul) {
6681 if (!getConstantDataArrayInfo(V, Slice, 8))
6682 return false;
6683
6684 if (Slice.Array == nullptr) {
6685 if (TrimAtNul) {
6686 // Return a nul-terminated string even for an empty Slice. This is
6687 // safe because all existing SimplifyLibcalls callers require string
6688 // arguments and the behavior of the functions they fold is undefined
6689 // otherwise. Folding the calls this way is preferable to making
6690 // the undefined library calls, even though it prevents sanitizers
6691 // from reporting such calls.
6692 Str = StringRef();
6693 return true;
6694 }
6695 if (Slice.Length == 1) {
6696 Str = StringRef("", 1);
6697 return true;
6698 }
6699 // We cannot instantiate a StringRef as we do not have an appropriate string
6700 // of 0s at hand.
6701 return false;
6702 }
6703
6704 // Start out with the entire array in the StringRef.
6705 Str = Slice.Array->getAsString();
6706 // Skip over 'offset' bytes.
6707 Str = Str.substr(Slice.Offset);
6708
6709 if (TrimAtNul) {
6710 // Trim off the \0 and anything after it. If the array is not nul
6711 // terminated, we just return the whole end of string. The client may know
6712 // some other way that the string is length-bound.
6713 Str = Str.substr(0, Str.find('\0'));
6714 }
6715 return true;
6716}
6717
6718// These next two are very similar to the above, but also look through PHI
6719// nodes.
6720// TODO: See if we can integrate these two together.
6721
6722/// If we can compute the length of the string pointed to by
6723/// the specified pointer, return 'len+1'. If we can't, return 0.
6726 unsigned CharSize) {
6727 // Look through noop bitcast instructions.
6728 V = V->stripPointerCasts();
6729
6730 // If this is a PHI node, there are two cases: either we have already seen it
6731 // or we haven't.
6732 if (const PHINode *PN = dyn_cast<PHINode>(V)) {
6733 if (!PHIs.insert(PN).second)
6734 return ~0ULL; // already in the set.
6735
6736 // If it was new, see if all the input strings are the same length.
6737 uint64_t LenSoFar = ~0ULL;
6738 for (Value *IncValue : PN->incoming_values()) {
6739 uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize);
6740 if (Len == 0) return 0; // Unknown length -> unknown.
6741
6742 if (Len == ~0ULL) continue;
6743
6744 if (Len != LenSoFar && LenSoFar != ~0ULL)
6745 return 0; // Disagree -> unknown.
6746 LenSoFar = Len;
6747 }
6748
6749 // Success, all agree.
6750 return LenSoFar;
6751 }
6752
6753 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
6754 if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
6755 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);
6756 if (Len1 == 0) return 0;
6757 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);
6758 if (Len2 == 0) return 0;
6759 if (Len1 == ~0ULL) return Len2;
6760 if (Len2 == ~0ULL) return Len1;
6761 if (Len1 != Len2) return 0;
6762 return Len1;
6763 }
6764
6765 // Otherwise, see if we can read the string.
6767 if (!getConstantDataArrayInfo(V, Slice, CharSize))
6768 return 0;
6769
6770 if (Slice.Array == nullptr)
6771 // Zeroinitializer (including an empty one).
6772 return 1;
6773
6774 // Search for the first nul character. Return a conservative result even
6775 // when there is no nul. This is safe since otherwise the string function
6776 // being folded such as strlen is undefined, and can be preferable to
6777 // making the undefined library call.
6778 unsigned NullIndex = 0;
6779 for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) {
6780 if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0)
6781 break;
6782 }
6783
6784 return NullIndex + 1;
6785}
6786
6787/// If we can compute the length of the string pointed to by
6788/// the specified pointer, return 'len+1'. If we can't, return 0.
6789uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) {
6790 if (!V->getType()->isPointerTy())
6791 return 0;
6792
6794 uint64_t Len = GetStringLengthH(V, PHIs, CharSize);
6795 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
6796 // an empty string as a length.
6797 return Len == ~0ULL ? 1 : Len;
6798}
6799
6800const Value *
6802 bool MustPreserveNullness) {
6803 assert(Call &&
6804 "getArgumentAliasingToReturnedPointer only works on nonnull calls");
6805 if (const Value *RV = Call->getReturnedArgOperand())
6806 return RV;
6807 // This can be used only as a aliasing property.
6809 Call, MustPreserveNullness))
6810 return Call->getArgOperand(0);
6811 return nullptr;
6812}
6813
6815 const CallBase *Call, bool MustPreserveNullness) {
6816 switch (Call->getIntrinsicID()) {
6817 case Intrinsic::launder_invariant_group:
6818 case Intrinsic::strip_invariant_group:
6819 case Intrinsic::aarch64_irg:
6820 case Intrinsic::aarch64_tagp:
6821 // The amdgcn_make_buffer_rsrc function does not alter the address of the
6822 // input pointer (and thus preserve null-ness for the purposes of escape
6823 // analysis, which is where the MustPreserveNullness flag comes in to play).
6824 // However, it will not necessarily map ptr addrspace(N) null to ptr
6825 // addrspace(8) null, aka the "null descriptor", which has "all loads return
6826 // 0, all stores are dropped" semantics. Given the context of this intrinsic
6827 // list, no one should be relying on such a strict interpretation of
6828 // MustPreserveNullness (and, at time of writing, they are not), but we
6829 // document this fact out of an abundance of caution.
6830 case Intrinsic::amdgcn_make_buffer_rsrc:
6831 return true;
6832 case Intrinsic::ptrmask:
6833 return !MustPreserveNullness;
6834 case Intrinsic::threadlocal_address:
6835 // The underlying variable changes with thread ID. The Thread ID may change
6836 // at coroutine suspend points.
6837 return !Call->getParent()->getParent()->isPresplitCoroutine();
6838 default:
6839 return false;
6840 }
6841}
6842
6843/// \p PN defines a loop-variant pointer to an object. Check if the
6844/// previous iteration of the loop was referring to the same object as \p PN.
6846 const LoopInfo *LI) {
6847 // Find the loop-defined value.
6848 Loop *L = LI->getLoopFor(PN->getParent());
6849 if (PN->getNumIncomingValues() != 2)
6850 return true;
6851
6852 // Find the value from previous iteration.
6853 auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
6854 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6855 PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
6856 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6857 return true;
6858
6859 // If a new pointer is loaded in the loop, the pointer references a different
6860 // object in every iteration. E.g.:
6861 // for (i)
6862 // int *p = a[i];
6863 // ...
6864 if (auto *Load = dyn_cast<LoadInst>(PrevValue))
6865 if (!L->isLoopInvariant(Load->getPointerOperand()))
6866 return false;
6867 return true;
6868}
6869
6870const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup) {
6871 for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
6872 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
6873 const Value *PtrOp = GEP->getPointerOperand();
6874 if (!PtrOp->getType()->isPointerTy()) // Only handle scalar pointer base.
6875 return V;
6876 V = PtrOp;
6877 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
6878 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
6879 Value *NewV = cast<Operator>(V)->getOperand(0);
6880 if (!NewV->getType()->isPointerTy())
6881 return V;
6882 V = NewV;
6883 } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
6884 if (GA->isInterposable())
6885 return V;
6886 V = GA->getAliasee();
6887 } else {
6888 if (auto *PHI = dyn_cast<PHINode>(V)) {
6889 // Look through single-arg phi nodes created by LCSSA.
6890 if (PHI->getNumIncomingValues() == 1) {
6891 V = PHI->getIncomingValue(0);
6892 continue;
6893 }
6894 } else if (auto *Call = dyn_cast<CallBase>(V)) {
6895 // CaptureTracking can know about special capturing properties of some
6896 // intrinsics like launder.invariant.group, that can't be expressed with
6897 // the attributes, but have properties like returning aliasing pointer.
6898 // Because some analysis may assume that nocaptured pointer is not
6899 // returned from some special intrinsic (because function would have to
6900 // be marked with returns attribute), it is crucial to use this function
6901 // because it should be in sync with CaptureTracking. Not using it may
6902 // cause weird miscompilations where 2 aliasing pointers are assumed to
6903 // noalias.
6904 if (auto *RP = getArgumentAliasingToReturnedPointer(Call, false)) {
6905 V = RP;
6906 continue;
6907 }
6908 }
6909
6910 return V;
6911 }
6912 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
6913 }
6914 return V;
6915}
6916
6919 const LoopInfo *LI, unsigned MaxLookup) {
6922 Worklist.push_back(V);
6923 do {
6924 const Value *P = Worklist.pop_back_val();
6925 P = getUnderlyingObject(P, MaxLookup);
6926
6927 if (!Visited.insert(P).second)
6928 continue;
6929
6930 if (auto *SI = dyn_cast<SelectInst>(P)) {
6931 Worklist.push_back(SI->getTrueValue());
6932 Worklist.push_back(SI->getFalseValue());
6933 continue;
6934 }
6935
6936 if (auto *PN = dyn_cast<PHINode>(P)) {
6937 // If this PHI changes the underlying object in every iteration of the
6938 // loop, don't look through it. Consider:
6939 // int **A;
6940 // for (i) {
6941 // Prev = Curr; // Prev = PHI (Prev_0, Curr)
6942 // Curr = A[i];
6943 // *Prev, *Curr;
6944 //
6945 // Prev is tracking Curr one iteration behind so they refer to different
6946 // underlying objects.
6947 if (!LI || !LI->isLoopHeader(PN->getParent()) ||
6949 append_range(Worklist, PN->incoming_values());
6950 else
6951 Objects.push_back(P);
6952 continue;
6953 }
6954
6955 Objects.push_back(P);
6956 } while (!Worklist.empty());
6957}
6958
6960 const unsigned MaxVisited = 8;
6961
6964 Worklist.push_back(V);
6965 const Value *Object = nullptr;
6966 // Used as fallback if we can't find a common underlying object through
6967 // recursion.
6968 bool First = true;
6969 const Value *FirstObject = getUnderlyingObject(V);
6970 do {
6971 const Value *P = Worklist.pop_back_val();
6972 P = First ? FirstObject : getUnderlyingObject(P);
6973 First = false;
6974
6975 if (!Visited.insert(P).second)
6976 continue;
6977
6978 if (Visited.size() == MaxVisited)
6979 return FirstObject;
6980
6981 if (auto *SI = dyn_cast<SelectInst>(P)) {
6982 Worklist.push_back(SI->getTrueValue());
6983 Worklist.push_back(SI->getFalseValue());
6984 continue;
6985 }
6986
6987 if (auto *PN = dyn_cast<PHINode>(P)) {
6988 append_range(Worklist, PN->incoming_values());
6989 continue;
6990 }
6991
6992 if (!Object)
6993 Object = P;
6994 else if (Object != P)
6995 return FirstObject;
6996 } while (!Worklist.empty());
6997
6998 return Object ? Object : FirstObject;
6999}
7000
7001/// This is the function that does the work of looking through basic
7002/// ptrtoint+arithmetic+inttoptr sequences.
7003static const Value *getUnderlyingObjectFromInt(const Value *V) {
7004 do {
7005 if (const Operator *U = dyn_cast<Operator>(V)) {
7006 // If we find a ptrtoint, we can transfer control back to the
7007 // regular getUnderlyingObjectFromInt.
7008 if (U->getOpcode() == Instruction::PtrToInt)
7009 return U->getOperand(0);
7010 // If we find an add of a constant, a multiplied value, or a phi, it's
7011 // likely that the other operand will lead us to the base
7012 // object. We don't have to worry about the case where the
7013 // object address is somehow being computed by the multiply,
7014 // because our callers only care when the result is an
7015 // identifiable object.
7016 if (U->getOpcode() != Instruction::Add ||
7017 (!isa<ConstantInt>(U->getOperand(1)) &&
7018 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
7019 !isa<PHINode>(U->getOperand(1))))
7020 return V;
7021 V = U->getOperand(0);
7022 } else {
7023 return V;
7024 }
7025 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
7026 } while (true);
7027}
7028
7029/// This is a wrapper around getUnderlyingObjects and adds support for basic
7030/// ptrtoint+arithmetic+inttoptr sequences.
7031/// It returns false if unidentified object is found in getUnderlyingObjects.
7033 SmallVectorImpl<Value *> &Objects) {
7035 SmallVector<const Value *, 4> Working(1, V);
7036 do {
7037 V = Working.pop_back_val();
7038
7040 getUnderlyingObjects(V, Objs);
7041
7042 for (const Value *V : Objs) {
7043 if (!Visited.insert(V).second)
7044 continue;
7045 if (Operator::getOpcode(V) == Instruction::IntToPtr) {
7046 const Value *O =
7047 getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
7048 if (O->getType()->isPointerTy()) {
7049 Working.push_back(O);
7050 continue;
7051 }
7052 }
7053 // If getUnderlyingObjects fails to find an identifiable object,
7054 // getUnderlyingObjectsForCodeGen also fails for safety.
7055 if (!isIdentifiedObject(V)) {
7056 Objects.clear();
7057 return false;
7058 }
7059 Objects.push_back(const_cast<Value *>(V));
7060 }
7061 } while (!Working.empty());
7062 return true;
7063}
7064
7066 AllocaInst *Result = nullptr;
7068 SmallVector<Value *, 4> Worklist;
7069
7070 auto AddWork = [&](Value *V) {
7071 if (Visited.insert(V).second)
7072 Worklist.push_back(V);
7073 };
7074
7075 AddWork(V);
7076 do {
7077 V = Worklist.pop_back_val();
7078 assert(Visited.count(V));
7079
7080 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
7081 if (Result && Result != AI)
7082 return nullptr;
7083 Result = AI;
7084 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
7085 AddWork(CI->getOperand(0));
7086 } else if (PHINode *PN = dyn_cast<PHINode>(V)) {
7087 for (Value *IncValue : PN->incoming_values())
7088 AddWork(IncValue);
7089 } else if (auto *SI = dyn_cast<SelectInst>(V)) {
7090 AddWork(SI->getTrueValue());
7091 AddWork(SI->getFalseValue());
7093 if (OffsetZero && !GEP->hasAllZeroIndices())
7094 return nullptr;
7095 AddWork(GEP->getPointerOperand());
7096 } else if (CallBase *CB = dyn_cast<CallBase>(V)) {
7097 Value *Returned = CB->getReturnedArgOperand();
7098 if (Returned)
7099 AddWork(Returned);
7100 else
7101 return nullptr;
7102 } else {
7103 return nullptr;
7104 }
7105 } while (!Worklist.empty());
7106
7107 return Result;
7108}
7109
7111 const Value *V, bool AllowLifetime, bool AllowDroppable) {
7112 for (const User *U : V->users()) {
7114 if (!II)
7115 return false;
7116
7117 if (AllowLifetime && II->isLifetimeStartOrEnd())
7118 continue;
7119
7120 if (AllowDroppable && II->isDroppable())
7121 continue;
7122
7123 return false;
7124 }
7125 return true;
7126}
7127
7130 V, /* AllowLifetime */ true, /* AllowDroppable */ false);
7131}
7134 V, /* AllowLifetime */ true, /* AllowDroppable */ true);
7135}
7136
7138 if (auto *II = dyn_cast<IntrinsicInst>(I))
7139 return isTriviallyVectorizable(II->getIntrinsicID());
7140 auto *Shuffle = dyn_cast<ShuffleVectorInst>(I);
7141 return (!Shuffle || Shuffle->isSelect()) &&
7143}
7144
7146 const Instruction *Inst, const Instruction *CtxI, AssumptionCache *AC,
7147 const DominatorTree *DT, const TargetLibraryInfo *TLI, bool UseVariableInfo,
7148 bool IgnoreUBImplyingAttrs) {
7149 return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI,
7150 AC, DT, TLI, UseVariableInfo,
7151 IgnoreUBImplyingAttrs);
7152}
7153
7155 unsigned Opcode, const Instruction *Inst, const Instruction *CtxI,
7156 AssumptionCache *AC, const DominatorTree *DT, const TargetLibraryInfo *TLI,
7157 bool UseVariableInfo, bool IgnoreUBImplyingAttrs) {
7158#ifndef NDEBUG
7159 if (Inst->getOpcode() != Opcode) {
7160 // Check that the operands are actually compatible with the Opcode override.
7161 auto hasEqualReturnAndLeadingOperandTypes =
7162 [](const Instruction *Inst, unsigned NumLeadingOperands) {
7163 if (Inst->getNumOperands() < NumLeadingOperands)
7164 return false;
7165 const Type *ExpectedType = Inst->getType();
7166 for (unsigned ItOp = 0; ItOp < NumLeadingOperands; ++ItOp)
7167 if (Inst->getOperand(ItOp)->getType() != ExpectedType)
7168 return false;
7169 return true;
7170 };
7172 hasEqualReturnAndLeadingOperandTypes(Inst, 2));
7173 assert(!Instruction::isUnaryOp(Opcode) ||
7174 hasEqualReturnAndLeadingOperandTypes(Inst, 1));
7175 }
7176#endif
7177
7178 switch (Opcode) {
7179 default:
7180 return true;
7181 case Instruction::UDiv:
7182 case Instruction::URem: {
7183 // x / y is undefined if y == 0.
7184 const APInt *V;
7185 if (match(Inst->getOperand(1), m_APInt(V)))
7186 return *V != 0;
7187 return false;
7188 }
7189 case Instruction::SDiv:
7190 case Instruction::SRem: {
7191 // x / y is undefined if y == 0 or x == INT_MIN and y == -1
7192 const APInt *Numerator, *Denominator;
7193 if (!match(Inst->getOperand(1), m_APInt(Denominator)))
7194 return false;
7195 // We cannot hoist this division if the denominator is 0.
7196 if (*Denominator == 0)
7197 return false;
7198 // It's safe to hoist if the denominator is not 0 or -1.
7199 if (!Denominator->isAllOnes())
7200 return true;
7201 // At this point we know that the denominator is -1. It is safe to hoist as
7202 // long we know that the numerator is not INT_MIN.
7203 if (match(Inst->getOperand(0), m_APInt(Numerator)))
7204 return !Numerator->isMinSignedValue();
7205 // The numerator *might* be MinSignedValue.
7206 return false;
7207 }
7208 case Instruction::Load: {
7209 if (!UseVariableInfo)
7210 return false;
7211
7212 const LoadInst *LI = dyn_cast<LoadInst>(Inst);
7213 if (!LI)
7214 return false;
7215 if (mustSuppressSpeculation(*LI))
7216 return false;
7217 const DataLayout &DL = LI->getDataLayout();
7219 LI->getType(), LI->getAlign(), DL,
7220 CtxI, AC, DT, TLI);
7221 }
7222 case Instruction::Call: {
7223 auto *CI = dyn_cast<const CallInst>(Inst);
7224 if (!CI)
7225 return false;
7226 const Function *Callee = CI->getCalledFunction();
7227
7228 // The called function could have undefined behavior or side-effects, even
7229 // if marked readnone nounwind.
7230 if (!Callee || !Callee->isSpeculatable())
7231 return false;
7232 // Since the operands may be changed after hoisting, undefined behavior may
7233 // be triggered by some UB-implying attributes.
7234 return IgnoreUBImplyingAttrs || !CI->hasUBImplyingAttrs();
7235 }
7236 case Instruction::VAArg:
7237 case Instruction::Alloca:
7238 case Instruction::Invoke:
7239 case Instruction::CallBr:
7240 case Instruction::PHI:
7241 case Instruction::Store:
7242 case Instruction::Ret:
7243 case Instruction::UncondBr:
7244 case Instruction::CondBr:
7245 case Instruction::IndirectBr:
7246 case Instruction::Switch:
7247 case Instruction::Unreachable:
7248 case Instruction::Fence:
7249 case Instruction::AtomicRMW:
7250 case Instruction::AtomicCmpXchg:
7251 case Instruction::LandingPad:
7252 case Instruction::Resume:
7253 case Instruction::CatchSwitch:
7254 case Instruction::CatchPad:
7255 case Instruction::CatchRet:
7256 case Instruction::CleanupPad:
7257 case Instruction::CleanupRet:
7258 return false; // Misc instructions which have effects
7259 }
7260}
7261
7263 if (I.mayReadOrWriteMemory())
7264 // Memory dependency possible
7265 return true;
7267 // Can't move above a maythrow call or infinite loop. Or if an
7268 // inalloca alloca, above a stacksave call.
7269 return true;
7271 // 1) Can't reorder two inf-loop calls, even if readonly
7272 // 2) Also can't reorder an inf-loop call below a instruction which isn't
7273 // safe to speculative execute. (Inverse of above)
7274 return true;
7275 return false;
7276}
7277
7278/// Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
7292
7293/// Combine constant ranges from computeConstantRange() and computeKnownBits().
7296 bool ForSigned,
7297 const SimplifyQuery &SQ) {
7298 ConstantRange CR1 =
7299 ConstantRange::fromKnownBits(V.getKnownBits(SQ), ForSigned);
7300 ConstantRange CR2 = computeConstantRange(V, ForSigned, SQ.IIQ.UseInstrInfo);
7303 return CR1.intersectWith(CR2, RangeType);
7304}
7305
7307 const Value *RHS,
7308 const SimplifyQuery &SQ,
7309 bool IsNSW) {
7310 ConstantRange LHSRange =
7311 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7312 ConstantRange RHSRange =
7313 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7314
7315 // mul nsw of two non-negative numbers is also nuw.
7316 if (IsNSW && LHSRange.isAllNonNegative() && RHSRange.isAllNonNegative())
7318
7319 return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange));
7320}
7321
7323 const Value *RHS,
7324 const SimplifyQuery &SQ) {
7325 // Multiplying n * m significant bits yields a result of n + m significant
7326 // bits. If the total number of significant bits does not exceed the
7327 // result bit width (minus 1), there is no overflow.
7328 // This means if we have enough leading sign bits in the operands
7329 // we can guarantee that the result does not overflow.
7330 // Ref: "Hacker's Delight" by Henry Warren
7331 unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
7332
7333 // Note that underestimating the number of sign bits gives a more
7334 // conservative answer.
7335 unsigned SignBits =
7336 ::ComputeNumSignBits(LHS, SQ) + ::ComputeNumSignBits(RHS, SQ);
7337
7338 // First handle the easy case: if we have enough sign bits there's
7339 // definitely no overflow.
7340 if (SignBits > BitWidth + 1)
7342
7343 // There are two ambiguous cases where there can be no overflow:
7344 // SignBits == BitWidth + 1 and
7345 // SignBits == BitWidth
7346 // The second case is difficult to check, therefore we only handle the
7347 // first case.
7348 if (SignBits == BitWidth + 1) {
7349 // It overflows only when both arguments are negative and the true
7350 // product is exactly the minimum negative number.
7351 // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
7352 // For simplicity we just check if at least one side is not negative.
7353 KnownBits LHSKnown = computeKnownBits(LHS, SQ);
7354 KnownBits RHSKnown = computeKnownBits(RHS, SQ);
7355 if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative())
7357 }
7359}
7360
7363 const WithCache<const Value *> &RHS,
7364 const SimplifyQuery &SQ) {
7365 ConstantRange LHSRange =
7366 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7367 ConstantRange RHSRange =
7368 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7369 return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange));
7370}
7371
7372static OverflowResult
7375 const AddOperator *Add, const SimplifyQuery &SQ) {
7376 if (Add && Add->hasNoSignedWrap()) {
7378 }
7379
7380 // If LHS and RHS each have at least two sign bits, the addition will look
7381 // like
7382 //
7383 // XX..... +
7384 // YY.....
7385 //
7386 // If the carry into the most significant position is 0, X and Y can't both
7387 // be 1 and therefore the carry out of the addition is also 0.
7388 //
7389 // If the carry into the most significant position is 1, X and Y can't both
7390 // be 0 and therefore the carry out of the addition is also 1.
7391 //
7392 // Since the carry into the most significant position is always equal to
7393 // the carry out of the addition, there is no signed overflow.
7394 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
7396
7397 ConstantRange LHSRange =
7398 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7399 ConstantRange RHSRange =
7400 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7401 OverflowResult OR =
7402 mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange));
7404 return OR;
7405
7406 // The remaining code needs Add to be available. Early returns if not so.
7407 if (!Add)
7409
7410 // If the sign of Add is the same as at least one of the operands, this add
7411 // CANNOT overflow. If this can be determined from the known bits of the
7412 // operands the above signedAddMayOverflow() check will have already done so.
7413 // The only other way to improve on the known bits is from an assumption, so
7414 // call computeKnownBitsFromContext() directly.
7415 bool LHSOrRHSKnownNonNegative =
7416 (LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative());
7417 bool LHSOrRHSKnownNegative =
7418 (LHSRange.isAllNegative() || RHSRange.isAllNegative());
7419 if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
7420 KnownBits AddKnown(LHSRange.getBitWidth());
7421 computeKnownBitsFromContext(Add, AddKnown, SQ);
7422 if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||
7423 (AddKnown.isNegative() && LHSOrRHSKnownNegative))
7425 }
7426
7428}
7429
7431 const Value *RHS,
7432 const SimplifyQuery &SQ) {
7433 // X - (X % ?)
7434 // The remainder of a value can't have greater magnitude than itself,
7435 // so the subtraction can't overflow.
7436
7437 // X - (X -nuw ?)
7438 // In the minimal case, this would simplify to "?", so there's no subtract
7439 // at all. But if this analysis is used to peek through casts, for example,
7440 // then determining no-overflow may allow other transforms.
7441
7442 // TODO: There are other patterns like this.
7443 // See simplifyICmpWithBinOpOnLHS() for candidates.
7444 if (match(RHS, m_URem(m_Specific(LHS), m_Value())) ||
7445 match(RHS, m_NUWSub(m_Specific(LHS), m_Value())))
7446 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7448
7449 if (auto C = isImpliedByDomCondition(CmpInst::ICMP_UGE, LHS, RHS, SQ.CxtI,
7450 SQ.DL)) {
7451 if (*C)
7454 }
7455
7456 ConstantRange LHSRange =
7457 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7458 ConstantRange RHSRange =
7459 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7460 return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange));
7461}
7462
7464 const Value *RHS,
7465 const SimplifyQuery &SQ) {
7466 // X - (X % ?)
7467 // The remainder of a value can't have greater magnitude than itself,
7468 // so the subtraction can't overflow.
7469
7470 // X - (X -nsw ?)
7471 // In the minimal case, this would simplify to "?", so there's no subtract
7472 // at all. But if this analysis is used to peek through casts, for example,
7473 // then determining no-overflow may allow other transforms.
7474 if (match(RHS, m_SRem(m_Specific(LHS), m_Value())) ||
7475 match(RHS, m_NSWSub(m_Specific(LHS), m_Value())))
7476 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7478
7479 // If LHS and RHS each have at least two sign bits, the subtraction
7480 // cannot overflow.
7481 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
7483
7484 ConstantRange LHSRange =
7485 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7486 ConstantRange RHSRange =
7487 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7488 return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange));
7489}
7490
7492 const DominatorTree &DT) {
7493 SmallVector<const CondBrInst *, 2> GuardingBranches;
7495
7496 for (const User *U : WO->users()) {
7497 if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) {
7498 assert(EVI->getNumIndices() == 1 && "Obvious from CI's type");
7499
7500 if (EVI->getIndices()[0] == 0)
7501 Results.push_back(EVI);
7502 else {
7503 assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type");
7504
7505 for (const auto *U : EVI->users())
7506 if (const auto *B = dyn_cast<CondBrInst>(U))
7507 GuardingBranches.push_back(B);
7508 }
7509 } else {
7510 // We are using the aggregate directly in a way we don't want to analyze
7511 // here (storing it to a global, say).
7512 return false;
7513 }
7514 }
7515
7516 auto AllUsesGuardedByBranch = [&](const CondBrInst *BI) {
7517 BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1));
7518
7519 // Check if all users of the add are provably no-wrap.
7520 for (const auto *Result : Results) {
7521 // If the extractvalue itself is not executed on overflow, the we don't
7522 // need to check each use separately, since domination is transitive.
7523 if (DT.dominates(NoWrapEdge, Result->getParent()))
7524 continue;
7525
7526 for (const auto &RU : Result->uses())
7527 if (!DT.dominates(NoWrapEdge, RU))
7528 return false;
7529 }
7530
7531 return true;
7532 };
7533
7534 return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
7535}
7536
7537/// Shifts return poison if shiftwidth is larger than the bitwidth.
7538static bool shiftAmountKnownInRange(const Value *ShiftAmount) {
7539 auto *C = dyn_cast<Constant>(ShiftAmount);
7540 if (!C)
7541 return false;
7542
7543 // Shifts return poison if shiftwidth is larger than the bitwidth.
7545 if (auto *FVTy = dyn_cast<FixedVectorType>(C->getType())) {
7546 unsigned NumElts = FVTy->getNumElements();
7547 for (unsigned i = 0; i < NumElts; ++i)
7548 ShiftAmounts.push_back(C->getAggregateElement(i));
7549 } else if (isa<ScalableVectorType>(C->getType()))
7550 return false; // Can't tell, just return false to be safe
7551 else
7552 ShiftAmounts.push_back(C);
7553
7554 bool Safe = llvm::all_of(ShiftAmounts, [](const Constant *C) {
7555 auto *CI = dyn_cast_or_null<ConstantInt>(C);
7556 return CI && CI->getValue().ult(C->getType()->getIntegerBitWidth());
7557 });
7558
7559 return Safe;
7560}
7561
7567
7569 return (unsigned(Kind) & unsigned(UndefPoisonKind::PoisonOnly)) != 0;
7570}
7571
7573 return (unsigned(Kind) & unsigned(UndefPoisonKind::UndefOnly)) != 0;
7574}
7575
7577 bool ConsiderFlagsAndMetadata) {
7578
7579 if (ConsiderFlagsAndMetadata && includesPoison(Kind) &&
7580 Op->hasPoisonGeneratingAnnotations())
7581 return true;
7582
7583 unsigned Opcode = Op->getOpcode();
7584
7585 // Check whether opcode is a poison/undef-generating operation
7586 switch (Opcode) {
7587 case Instruction::Shl:
7588 case Instruction::AShr:
7589 case Instruction::LShr:
7590 return includesPoison(Kind) && !shiftAmountKnownInRange(Op->getOperand(1));
7591 case Instruction::FPToSI:
7592 case Instruction::FPToUI:
7593 // fptosi/ui yields poison if the resulting value does not fit in the
7594 // destination type.
7595 return true;
7596 case Instruction::Call:
7597 if (auto *II = dyn_cast<IntrinsicInst>(Op)) {
7598 switch (II->getIntrinsicID()) {
7599 // NOTE: Use IntrNoCreateUndefOrPoison when possible.
7600 case Intrinsic::ctlz:
7601 case Intrinsic::cttz:
7602 case Intrinsic::abs:
7603 // We're not considering flags so it is safe to just return false.
7604 return false;
7605 case Intrinsic::sshl_sat:
7606 case Intrinsic::ushl_sat:
7607 if (!includesPoison(Kind) ||
7608 shiftAmountKnownInRange(II->getArgOperand(1)))
7609 return false;
7610 break;
7611 }
7612 }
7613 [[fallthrough]];
7614 case Instruction::CallBr:
7615 case Instruction::Invoke: {
7616 const auto *CB = cast<CallBase>(Op);
7617 return !CB->hasRetAttr(Attribute::NoUndef) &&
7618 !CB->hasFnAttr(Attribute::NoCreateUndefOrPoison);
7619 }
7620 case Instruction::InsertElement:
7621 case Instruction::ExtractElement: {
7622 // If index exceeds the length of the vector, it returns poison
7623 auto *VTy = cast<VectorType>(Op->getOperand(0)->getType());
7624 unsigned IdxOp = Op->getOpcode() == Instruction::InsertElement ? 2 : 1;
7625 auto *Idx = dyn_cast<ConstantInt>(Op->getOperand(IdxOp));
7626 if (includesPoison(Kind))
7627 return !Idx ||
7628 Idx->getValue().uge(VTy->getElementCount().getKnownMinValue());
7629 return false;
7630 }
7631 case Instruction::ShuffleVector: {
7633 ? cast<ConstantExpr>(Op)->getShuffleMask()
7634 : cast<ShuffleVectorInst>(Op)->getShuffleMask();
7635 return includesPoison(Kind) && is_contained(Mask, PoisonMaskElem);
7636 }
7637 case Instruction::FNeg:
7638 case Instruction::PHI:
7639 case Instruction::Select:
7640 case Instruction::ExtractValue:
7641 case Instruction::InsertValue:
7642 case Instruction::Freeze:
7643 case Instruction::ICmp:
7644 case Instruction::FCmp:
7645 case Instruction::GetElementPtr:
7646 return false;
7647 case Instruction::AddrSpaceCast:
7648 return true;
7649 default: {
7650 const auto *CE = dyn_cast<ConstantExpr>(Op);
7651 if (isa<CastInst>(Op) || (CE && CE->isCast()))
7652 return false;
7653 else if (Instruction::isBinaryOp(Opcode))
7654 return false;
7655 // Be conservative and return true.
7656 return true;
7657 }
7658 }
7659}
7660
7662 bool ConsiderFlagsAndMetadata) {
7663 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::UndefOrPoison,
7664 ConsiderFlagsAndMetadata);
7665}
7666
7667bool llvm::canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata) {
7668 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::PoisonOnly,
7669 ConsiderFlagsAndMetadata);
7670}
7671
7672static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V,
7673 unsigned Depth) {
7674 if (ValAssumedPoison == V)
7675 return true;
7676
7677 const unsigned MaxDepth = 2;
7678 if (Depth >= MaxDepth)
7679 return false;
7680
7681 if (const auto *I = dyn_cast<Instruction>(V)) {
7682 if (any_of(I->operands(), [=](const Use &Op) {
7683 return propagatesPoison(Op) &&
7684 directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1);
7685 }))
7686 return true;
7687
7688 // V = extractvalue V0, idx
7689 // V2 = extractvalue V0, idx2
7690 // V0's elements are all poison or not. (e.g., add_with_overflow)
7691 const WithOverflowInst *II;
7693 (match(ValAssumedPoison, m_ExtractValue(m_Specific(II))) ||
7694 llvm::is_contained(II->args(), ValAssumedPoison)))
7695 return true;
7696 }
7697 return false;
7698}
7699
7700static bool impliesPoison(const Value *ValAssumedPoison, const Value *V,
7701 unsigned Depth) {
7702 if (isGuaranteedNotToBePoison(ValAssumedPoison))
7703 return true;
7704
7705 if (directlyImpliesPoison(ValAssumedPoison, V, /* Depth */ 0))
7706 return true;
7707
7708 const unsigned MaxDepth = 2;
7709 if (Depth >= MaxDepth)
7710 return false;
7711
7712 const auto *I = dyn_cast<Instruction>(ValAssumedPoison);
7713 if (I && !canCreatePoison(cast<Operator>(I))) {
7714 return all_of(I->operands(), [=](const Value *Op) {
7715 return impliesPoison(Op, V, Depth + 1);
7716 });
7717 }
7718 return false;
7719}
7720
7721bool llvm::impliesPoison(const Value *ValAssumedPoison, const Value *V) {
7722 return ::impliesPoison(ValAssumedPoison, V, /* Depth */ 0);
7723}
7724
7725static bool programUndefinedIfUndefOrPoison(const Value *V, bool PoisonOnly);
7726
7728 const Value *V, AssumptionCache *AC, const Instruction *CtxI,
7729 const DominatorTree *DT, unsigned Depth, UndefPoisonKind Kind) {
7731 return false;
7732
7733 if (isa<MetadataAsValue>(V))
7734 return false;
7735
7736 if (const auto *A = dyn_cast<Argument>(V)) {
7737 if (A->hasAttribute(Attribute::NoUndef) ||
7738 A->hasAttribute(Attribute::Dereferenceable) ||
7739 A->hasAttribute(Attribute::DereferenceableOrNull))
7740 return true;
7741 }
7742
7743 if (auto *C = dyn_cast<Constant>(V)) {
7744 if (isa<PoisonValue>(C))
7745 return !includesPoison(Kind);
7746
7747 if (isa<UndefValue>(C))
7748 return !includesUndef(Kind);
7749
7752 return true;
7753
7754 if (C->getType()->isVectorTy()) {
7755 if (isa<ConstantExpr>(C)) {
7756 // Scalable vectors can use a ConstantExpr to build a splat.
7757 if (Constant *SplatC = C->getSplatValue())
7758 if (isa<ConstantInt>(SplatC) || isa<ConstantFP>(SplatC))
7759 return true;
7760 } else {
7761 if (includesUndef(Kind) && C->containsUndefElement())
7762 return false;
7763 if (includesPoison(Kind) && C->containsPoisonElement())
7764 return false;
7765 return !C->containsConstantExpression();
7766 }
7767 }
7768 }
7769
7770 // Strip cast operations from a pointer value.
7771 // Note that stripPointerCastsSameRepresentation can strip off getelementptr
7772 // inbounds with zero offset. To guarantee that the result isn't poison, the
7773 // stripped pointer is checked as it has to be pointing into an allocated
7774 // object or be null `null` to ensure `inbounds` getelement pointers with a
7775 // zero offset could not produce poison.
7776 // It can strip off addrspacecast that do not change bit representation as
7777 // well. We believe that such addrspacecast is equivalent to no-op.
7778 auto *StrippedV = V->stripPointerCastsSameRepresentation();
7779 if (isa<AllocaInst>(StrippedV) || isa<GlobalVariable>(StrippedV) ||
7780 isa<Function>(StrippedV) || isa<ConstantPointerNull>(StrippedV))
7781 return true;
7782
7783 auto OpCheck = [&](const Value *V) {
7784 return isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth + 1, Kind);
7785 };
7786
7787 if (auto *Opr = dyn_cast<Operator>(V)) {
7788 // If the value is a freeze instruction, then it can never
7789 // be undef or poison.
7790 if (isa<FreezeInst>(V))
7791 return true;
7792
7793 if (const auto *CB = dyn_cast<CallBase>(V)) {
7794 if (CB->hasRetAttr(Attribute::NoUndef) ||
7795 CB->hasRetAttr(Attribute::Dereferenceable) ||
7796 CB->hasRetAttr(Attribute::DereferenceableOrNull))
7797 return true;
7798 }
7799
7800 if (!::canCreateUndefOrPoison(Opr, Kind,
7801 /*ConsiderFlagsAndMetadata=*/true)) {
7802 if (const auto *PN = dyn_cast<PHINode>(V)) {
7803 unsigned Num = PN->getNumIncomingValues();
7804 bool IsWellDefined = true;
7805 for (unsigned i = 0; i < Num; ++i) {
7806 if (PN == PN->getIncomingValue(i))
7807 continue;
7808 auto *TI = PN->getIncomingBlock(i)->getTerminator();
7809 if (!isGuaranteedNotToBeUndefOrPoison(PN->getIncomingValue(i), AC, TI,
7810 DT, Depth + 1, Kind)) {
7811 IsWellDefined = false;
7812 break;
7813 }
7814 }
7815 if (IsWellDefined)
7816 return true;
7817 } else if (auto *Splat = isa<ShuffleVectorInst>(Opr) ? getSplatValue(Opr)
7818 : nullptr) {
7819 // For splats we only need to check the value being splatted.
7820 if (OpCheck(Splat))
7821 return true;
7822 } else if (all_of(Opr->operands(), OpCheck))
7823 return true;
7824 }
7825 }
7826
7827 if (auto *I = dyn_cast<LoadInst>(V))
7828 if (I->hasMetadata(LLVMContext::MD_noundef) ||
7829 I->hasMetadata(LLVMContext::MD_dereferenceable) ||
7830 I->hasMetadata(LLVMContext::MD_dereferenceable_or_null))
7831 return true;
7832
7834 return true;
7835
7836 // CxtI may be null or a cloned instruction.
7837 if (!CtxI || !CtxI->getParent() || !DT)
7838 return false;
7839
7840 auto *DNode = DT->getNode(CtxI->getParent());
7841 if (!DNode)
7842 // Unreachable block
7843 return false;
7844
7845 // If V is used as a branch condition before reaching CtxI, V cannot be
7846 // undef or poison.
7847 // br V, BB1, BB2
7848 // BB1:
7849 // CtxI ; V cannot be undef or poison here
7850 auto *Dominator = DNode->getIDom();
7851 // This check is purely for compile time reasons: we can skip the IDom walk
7852 // if what we are checking for includes undef and the value is not an integer.
7853 if (!includesUndef(Kind) || V->getType()->isIntegerTy())
7854 while (Dominator) {
7855 auto *TI = Dominator->getBlock()->getTerminatorOrNull();
7856
7857 Value *Cond = nullptr;
7858 if (auto BI = dyn_cast_or_null<CondBrInst>(TI)) {
7859 Cond = BI->getCondition();
7860 } else if (auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
7861 Cond = SI->getCondition();
7862 }
7863
7864 if (Cond) {
7865 if (Cond == V)
7866 return true;
7867 else if (!includesUndef(Kind) && isa<Operator>(Cond)) {
7868 // For poison, we can analyze further
7869 auto *Opr = cast<Operator>(Cond);
7870 if (any_of(Opr->operands(), [V](const Use &U) {
7871 return V == U && propagatesPoison(U);
7872 }))
7873 return true;
7874 }
7875 }
7876
7877 Dominator = Dominator->getIDom();
7878 }
7879
7880 if (AC && getKnowledgeValidInContext(V, {Attribute::NoUndef}, *AC, CtxI, DT))
7881 return true;
7882
7883 return false;
7884}
7885
7887 const Instruction *CtxI,
7888 const DominatorTree *DT,
7889 unsigned Depth) {
7890 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7892}
7893
7895 const Instruction *CtxI,
7896 const DominatorTree *DT, unsigned Depth) {
7897 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7899}
7900
7902 const Instruction *CtxI,
7903 const DominatorTree *DT, unsigned Depth) {
7904 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7906}
7907
7908/// Return true if undefined behavior would provably be executed on the path to
7909/// OnPathTo if Root produced a posion result. Note that this doesn't say
7910/// anything about whether OnPathTo is actually executed or whether Root is
7911/// actually poison. This can be used to assess whether a new use of Root can
7912/// be added at a location which is control equivalent with OnPathTo (such as
7913/// immediately before it) without introducing UB which didn't previously
7914/// exist. Note that a false result conveys no information.
7916 Instruction *OnPathTo,
7917 DominatorTree *DT) {
7918 // Basic approach is to assume Root is poison, propagate poison forward
7919 // through all users we can easily track, and then check whether any of those
7920 // users are provable UB and must execute before out exiting block might
7921 // exit.
7922
7923 // The set of all recursive users we've visited (which are assumed to all be
7924 // poison because of said visit)
7927 Worklist.push_back(Root);
7928 while (!Worklist.empty()) {
7929 const Instruction *I = Worklist.pop_back_val();
7930
7931 // If we know this must trigger UB on a path leading our target.
7932 if (mustTriggerUB(I, KnownPoison) && DT->dominates(I, OnPathTo))
7933 return true;
7934
7935 // If we can't analyze propagation through this instruction, just skip it
7936 // and transitive users. Safe as false is a conservative result.
7937 if (I != Root && !any_of(I->operands(), [&KnownPoison](const Use &U) {
7938 return KnownPoison.contains(U) && propagatesPoison(U);
7939 }))
7940 continue;
7941
7942 if (KnownPoison.insert(I).second)
7943 for (const User *User : I->users())
7944 Worklist.push_back(cast<Instruction>(User));
7945 }
7946
7947 // Might be non-UB, or might have a path we couldn't prove must execute on
7948 // way to exiting bb.
7949 return false;
7950}
7951
7953 const SimplifyQuery &SQ) {
7954 return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1),
7955 Add, SQ);
7956}
7957
7960 const WithCache<const Value *> &RHS,
7961 const SimplifyQuery &SQ) {
7962 return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, SQ);
7963}
7964
7966 // Note: An atomic operation isn't guaranteed to return in a reasonable amount
7967 // of time because it's possible for another thread to interfere with it for an
7968 // arbitrary length of time, but programs aren't allowed to rely on that.
7969
7970 // If there is no successor, then execution can't transfer to it.
7971 if (isa<ReturnInst>(I))
7972 return false;
7974 return false;
7975
7976 // Note: Do not add new checks here; instead, change Instruction::mayThrow or
7977 // Instruction::willReturn.
7978 //
7979 // FIXME: Move this check into Instruction::willReturn.
7980 if (isa<CatchPadInst>(I)) {
7981 switch (classifyEHPersonality(I->getFunction()->getPersonalityFn())) {
7982 default:
7983 // A catchpad may invoke exception object constructors and such, which
7984 // in some languages can be arbitrary code, so be conservative by default.
7985 return false;
7987 // For CoreCLR, it just involves a type test.
7988 return true;
7989 }
7990 }
7991
7992 // An instruction that returns without throwing must transfer control flow
7993 // to a successor.
7994 return !I->mayThrow() && I->willReturn();
7995}
7996
7998 // TODO: This is slightly conservative for invoke instruction since exiting
7999 // via an exception *is* normal control for them.
8000 for (const Instruction &I : *BB)
8002 return false;
8003 return true;
8004}
8005
8012
8015 assert(ScanLimit && "scan limit must be non-zero");
8016 for (const Instruction &I : Range) {
8017 if (--ScanLimit == 0)
8018 return false;
8020 return false;
8021 }
8022 return true;
8023}
8024
8026 const Loop *L) {
8027 // The loop header is guaranteed to be executed for every iteration.
8028 //
8029 // FIXME: Relax this constraint to cover all basic blocks that are
8030 // guaranteed to be executed at every iteration.
8031 if (I->getParent() != L->getHeader()) return false;
8032
8033 for (const Instruction &LI : *L->getHeader()) {
8034 if (&LI == I) return true;
8035 if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;
8036 }
8037 llvm_unreachable("Instruction not contained in its own parent basic block.");
8038}
8039
8041 switch (IID) {
8042 // TODO: Add more intrinsics.
8043 case Intrinsic::sadd_with_overflow:
8044 case Intrinsic::ssub_with_overflow:
8045 case Intrinsic::smul_with_overflow:
8046 case Intrinsic::uadd_with_overflow:
8047 case Intrinsic::usub_with_overflow:
8048 case Intrinsic::umul_with_overflow:
8049 // If an input is a vector containing a poison element, the
8050 // two output vectors (calculated results, overflow bits)'
8051 // corresponding lanes are poison.
8052 return true;
8053 case Intrinsic::ctpop:
8054 case Intrinsic::ctlz:
8055 case Intrinsic::cttz:
8056 case Intrinsic::abs:
8057 case Intrinsic::smax:
8058 case Intrinsic::smin:
8059 case Intrinsic::umax:
8060 case Intrinsic::umin:
8061 case Intrinsic::scmp:
8062 case Intrinsic::is_fpclass:
8063 case Intrinsic::ptrmask:
8064 case Intrinsic::ucmp:
8065 case Intrinsic::bitreverse:
8066 case Intrinsic::bswap:
8067 case Intrinsic::sadd_sat:
8068 case Intrinsic::ssub_sat:
8069 case Intrinsic::sshl_sat:
8070 case Intrinsic::uadd_sat:
8071 case Intrinsic::usub_sat:
8072 case Intrinsic::ushl_sat:
8073 case Intrinsic::smul_fix:
8074 case Intrinsic::smul_fix_sat:
8075 case Intrinsic::umul_fix:
8076 case Intrinsic::umul_fix_sat:
8077 case Intrinsic::pow:
8078 case Intrinsic::powi:
8079 case Intrinsic::sin:
8080 case Intrinsic::sinh:
8081 case Intrinsic::cos:
8082 case Intrinsic::cosh:
8083 case Intrinsic::sincos:
8084 case Intrinsic::sincospi:
8085 case Intrinsic::tan:
8086 case Intrinsic::tanh:
8087 case Intrinsic::asin:
8088 case Intrinsic::acos:
8089 case Intrinsic::atan:
8090 case Intrinsic::atan2:
8091 case Intrinsic::canonicalize:
8092 case Intrinsic::sqrt:
8093 case Intrinsic::exp:
8094 case Intrinsic::exp2:
8095 case Intrinsic::exp10:
8096 case Intrinsic::log:
8097 case Intrinsic::log2:
8098 case Intrinsic::log10:
8099 case Intrinsic::modf:
8100 case Intrinsic::floor:
8101 case Intrinsic::ceil:
8102 case Intrinsic::trunc:
8103 case Intrinsic::rint:
8104 case Intrinsic::nearbyint:
8105 case Intrinsic::round:
8106 case Intrinsic::roundeven:
8107 case Intrinsic::lrint:
8108 case Intrinsic::llrint:
8109 case Intrinsic::fshl:
8110 case Intrinsic::fshr:
8111 return true;
8112 default:
8113 return false;
8114 }
8115}
8116
8117bool llvm::propagatesPoison(const Use &PoisonOp) {
8118 const Operator *I = cast<Operator>(PoisonOp.getUser());
8119 switch (I->getOpcode()) {
8120 case Instruction::Freeze:
8121 case Instruction::PHI:
8122 case Instruction::Invoke:
8123 return false;
8124 case Instruction::Select:
8125 return PoisonOp.getOperandNo() == 0;
8126 case Instruction::Call:
8127 if (auto *II = dyn_cast<IntrinsicInst>(I))
8128 return intrinsicPropagatesPoison(II->getIntrinsicID());
8129 return false;
8130 case Instruction::ICmp:
8131 case Instruction::FCmp:
8132 case Instruction::GetElementPtr:
8133 return true;
8134 default:
8136 return true;
8137
8138 // Be conservative and return false.
8139 return false;
8140 }
8141}
8142
8143/// Enumerates all operands of \p I that are guaranteed to not be undef or
8144/// poison. If the callback \p Handle returns true, stop processing and return
8145/// true. Otherwise, return false.
8146template <typename CallableT>
8148 const CallableT &Handle) {
8149 switch (I->getOpcode()) {
8150 case Instruction::Store:
8151 if (Handle(cast<StoreInst>(I)->getPointerOperand()))
8152 return true;
8153 break;
8154
8155 case Instruction::Load:
8156 if (Handle(cast<LoadInst>(I)->getPointerOperand()))
8157 return true;
8158 break;
8159
8160 // Since dereferenceable attribute imply noundef, atomic operations
8161 // also implicitly have noundef pointers too
8162 case Instruction::AtomicCmpXchg:
8164 return true;
8165 break;
8166
8167 case Instruction::AtomicRMW:
8168 if (Handle(cast<AtomicRMWInst>(I)->getPointerOperand()))
8169 return true;
8170 break;
8171
8172 case Instruction::Call:
8173 case Instruction::Invoke: {
8174 const CallBase *CB = cast<CallBase>(I);
8175 if (CB->isIndirectCall() && Handle(CB->getCalledOperand()))
8176 return true;
8177 for (unsigned i = 0; i < CB->arg_size(); ++i)
8178 if ((CB->paramHasAttr(i, Attribute::NoUndef) ||
8179 CB->paramHasAttr(i, Attribute::Dereferenceable) ||
8180 CB->paramHasAttr(i, Attribute::DereferenceableOrNull)) &&
8181 Handle(CB->getArgOperand(i)))
8182 return true;
8183 break;
8184 }
8185 case Instruction::Ret:
8186 if (I->getFunction()->hasRetAttribute(Attribute::NoUndef) &&
8187 Handle(I->getOperand(0)))
8188 return true;
8189 break;
8190 case Instruction::Switch:
8191 if (Handle(cast<SwitchInst>(I)->getCondition()))
8192 return true;
8193 break;
8194 case Instruction::CondBr:
8195 if (Handle(cast<CondBrInst>(I)->getCondition()))
8196 return true;
8197 break;
8198 default:
8199 break;
8200 }
8201
8202 return false;
8203}
8204
8205/// Enumerates all operands of \p I that are guaranteed to not be poison.
8206template <typename CallableT>
8208 const CallableT &Handle) {
8209 if (handleGuaranteedWellDefinedOps(I, Handle))
8210 return true;
8211 switch (I->getOpcode()) {
8212 // Divisors of these operations are allowed to be partially undef.
8213 case Instruction::UDiv:
8214 case Instruction::SDiv:
8215 case Instruction::URem:
8216 case Instruction::SRem:
8217 return Handle(I->getOperand(1));
8218 default:
8219 return false;
8220 }
8221}
8222
8224 const SmallPtrSetImpl<const Value *> &KnownPoison) {
8226 I, [&](const Value *V) { return KnownPoison.count(V); });
8227}
8228
8230 bool PoisonOnly) {
8231 // We currently only look for uses of values within the same basic
8232 // block, as that makes it easier to guarantee that the uses will be
8233 // executed given that Inst is executed.
8234 //
8235 // FIXME: Expand this to consider uses beyond the same basic block. To do
8236 // this, look out for the distinction between post-dominance and strong
8237 // post-dominance.
8238 const BasicBlock *BB = nullptr;
8240 if (const auto *Inst = dyn_cast<Instruction>(V)) {
8241 BB = Inst->getParent();
8242 Begin = Inst->getIterator();
8243 Begin++;
8244 } else if (const auto *Arg = dyn_cast<Argument>(V)) {
8245 if (Arg->getParent()->isDeclaration())
8246 return false;
8247 BB = &Arg->getParent()->getEntryBlock();
8248 Begin = BB->begin();
8249 } else {
8250 return false;
8251 }
8252
8253 // Limit number of instructions we look at, to avoid scanning through large
8254 // blocks. The current limit is chosen arbitrarily.
8255 unsigned ScanLimit = 32;
8256 BasicBlock::const_iterator End = BB->end();
8257
8258 if (!PoisonOnly) {
8259 // Since undef does not propagate eagerly, be conservative & just check
8260 // whether a value is directly passed to an instruction that must take
8261 // well-defined operands.
8262
8263 for (const auto &I : make_range(Begin, End)) {
8264 if (--ScanLimit == 0)
8265 break;
8266
8267 if (handleGuaranteedWellDefinedOps(&I, [V](const Value *WellDefinedOp) {
8268 return WellDefinedOp == V;
8269 }))
8270 return true;
8271
8273 break;
8274 }
8275 return false;
8276 }
8277
8278 // Set of instructions that we have proved will yield poison if Inst
8279 // does.
8280 SmallPtrSet<const Value *, 16> YieldsPoison;
8282
8283 YieldsPoison.insert(V);
8284 Visited.insert(BB);
8285
8286 while (true) {
8287 for (const auto &I : make_range(Begin, End)) {
8288 if (--ScanLimit == 0)
8289 return false;
8290 if (mustTriggerUB(&I, YieldsPoison))
8291 return true;
8293 return false;
8294
8295 // If an operand is poison and propagates it, mark I as yielding poison.
8296 for (const Use &Op : I.operands()) {
8297 if (YieldsPoison.count(Op) && propagatesPoison(Op)) {
8298 YieldsPoison.insert(&I);
8299 break;
8300 }
8301 }
8302
8303 // Special handling for select, which returns poison if its operand 0 is
8304 // poison (handled in the loop above) *or* if both its true/false operands
8305 // are poison (handled here).
8306 if (I.getOpcode() == Instruction::Select &&
8307 YieldsPoison.count(I.getOperand(1)) &&
8308 YieldsPoison.count(I.getOperand(2))) {
8309 YieldsPoison.insert(&I);
8310 }
8311 }
8312
8313 BB = BB->getSingleSuccessor();
8314 if (!BB || !Visited.insert(BB).second)
8315 break;
8316
8317 Begin = BB->getFirstNonPHIIt();
8318 End = BB->end();
8319 }
8320 return false;
8321}
8322
8324 return ::programUndefinedIfUndefOrPoison(Inst, false);
8325}
8326
8328 return ::programUndefinedIfUndefOrPoison(Inst, true);
8329}
8330
8331static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) {
8332 if (FMF.noNaNs())
8333 return true;
8334
8335 if (auto *C = dyn_cast<ConstantFP>(V))
8336 return !C->isNaN();
8337
8338 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8339 if (!C->getElementType()->isFloatingPointTy())
8340 return false;
8341 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8342 if (C->getElementAsAPFloat(I).isNaN())
8343 return false;
8344 }
8345 return true;
8346 }
8347
8349 return true;
8350
8351 return false;
8352}
8353
8354static bool isKnownNonZero(const Value *V) {
8355 if (auto *C = dyn_cast<ConstantFP>(V))
8356 return !C->isZero();
8357
8358 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8359 if (!C->getElementType()->isFloatingPointTy())
8360 return false;
8361 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8362 if (C->getElementAsAPFloat(I).isZero())
8363 return false;
8364 }
8365 return true;
8366 }
8367
8368 return false;
8369}
8370
8371/// Match clamp pattern for float types without care about NaNs or signed zeros.
8372/// Given non-min/max outer cmp/select from the clamp pattern this
8373/// function recognizes if it can be substitued by a "canonical" min/max
8374/// pattern.
8376 Value *CmpLHS, Value *CmpRHS,
8377 Value *TrueVal, Value *FalseVal,
8378 Value *&LHS, Value *&RHS) {
8379 // Try to match
8380 // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2))
8381 // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2))
8382 // and return description of the outer Max/Min.
8383
8384 // First, check if select has inverse order:
8385 if (CmpRHS == FalseVal) {
8386 std::swap(TrueVal, FalseVal);
8387 Pred = CmpInst::getInversePredicate(Pred);
8388 }
8389
8390 // Assume success now. If there's no match, callers should not use these anyway.
8391 LHS = TrueVal;
8392 RHS = FalseVal;
8393
8394 const APFloat *FC1;
8395 if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite())
8396 return {SPF_UNKNOWN, SPNB_NA, false};
8397
8398 const APFloat *FC2;
8399 switch (Pred) {
8400 case CmpInst::FCMP_OLT:
8401 case CmpInst::FCMP_OLE:
8402 case CmpInst::FCMP_ULT:
8403 case CmpInst::FCMP_ULE:
8404 if (match(FalseVal, m_OrdOrUnordFMin(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8405 *FC1 < *FC2)
8406 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
8407 break;
8408 case CmpInst::FCMP_OGT:
8409 case CmpInst::FCMP_OGE:
8410 case CmpInst::FCMP_UGT:
8411 case CmpInst::FCMP_UGE:
8412 if (match(FalseVal, m_OrdOrUnordFMax(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8413 *FC1 > *FC2)
8414 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
8415 break;
8416 default:
8417 break;
8418 }
8419
8420 return {SPF_UNKNOWN, SPNB_NA, false};
8421}
8422
8423/// Recognize variations of:
8424/// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
8426 Value *CmpLHS, Value *CmpRHS,
8427 Value *TrueVal, Value *FalseVal) {
8428 // Swap the select operands and predicate to match the patterns below.
8429 if (CmpRHS != TrueVal) {
8430 Pred = ICmpInst::getSwappedPredicate(Pred);
8431 std::swap(TrueVal, FalseVal);
8432 }
8433 const APInt *C1;
8434 if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) {
8435 const APInt *C2;
8436 // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
8437 if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8438 C1->slt(*C2) && Pred == CmpInst::ICMP_SLT)
8439 return {SPF_SMAX, SPNB_NA, false};
8440
8441 // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
8442 if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8443 C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT)
8444 return {SPF_SMIN, SPNB_NA, false};
8445
8446 // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
8447 if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8448 C1->ult(*C2) && Pred == CmpInst::ICMP_ULT)
8449 return {SPF_UMAX, SPNB_NA, false};
8450
8451 // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
8452 if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8453 C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT)
8454 return {SPF_UMIN, SPNB_NA, false};
8455 }
8456 return {SPF_UNKNOWN, SPNB_NA, false};
8457}
8458
8459/// Recognize variations of:
8460/// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c))
8462 Value *CmpLHS, Value *CmpRHS,
8463 Value *TVal, Value *FVal,
8464 unsigned Depth) {
8465 // TODO: Allow FP min/max with nnan/nsz.
8466 assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison");
8467
8468 Value *A = nullptr, *B = nullptr;
8469 SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1);
8470 if (!SelectPatternResult::isMinOrMax(L.Flavor))
8471 return {SPF_UNKNOWN, SPNB_NA, false};
8472
8473 Value *C = nullptr, *D = nullptr;
8474 SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1);
8475 if (L.Flavor != R.Flavor)
8476 return {SPF_UNKNOWN, SPNB_NA, false};
8477
8478 // We have something like: x Pred y ? min(a, b) : min(c, d).
8479 // Try to match the compare to the min/max operations of the select operands.
8480 // First, make sure we have the right compare predicate.
8481 switch (L.Flavor) {
8482 case SPF_SMIN:
8483 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) {
8484 Pred = ICmpInst::getSwappedPredicate(Pred);
8485 std::swap(CmpLHS, CmpRHS);
8486 }
8487 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
8488 break;
8489 return {SPF_UNKNOWN, SPNB_NA, false};
8490 case SPF_SMAX:
8491 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) {
8492 Pred = ICmpInst::getSwappedPredicate(Pred);
8493 std::swap(CmpLHS, CmpRHS);
8494 }
8495 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE)
8496 break;
8497 return {SPF_UNKNOWN, SPNB_NA, false};
8498 case SPF_UMIN:
8499 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
8500 Pred = ICmpInst::getSwappedPredicate(Pred);
8501 std::swap(CmpLHS, CmpRHS);
8502 }
8503 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE)
8504 break;
8505 return {SPF_UNKNOWN, SPNB_NA, false};
8506 case SPF_UMAX:
8507 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
8508 Pred = ICmpInst::getSwappedPredicate(Pred);
8509 std::swap(CmpLHS, CmpRHS);
8510 }
8511 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
8512 break;
8513 return {SPF_UNKNOWN, SPNB_NA, false};
8514 default:
8515 return {SPF_UNKNOWN, SPNB_NA, false};
8516 }
8517
8518 // If there is a common operand in the already matched min/max and the other
8519 // min/max operands match the compare operands (either directly or inverted),
8520 // then this is min/max of the same flavor.
8521
8522 // a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8523 // ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8524 if (D == B) {
8525 if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8526 match(A, m_Not(m_Specific(CmpRHS)))))
8527 return {L.Flavor, SPNB_NA, false};
8528 }
8529 // a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8530 // ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8531 if (C == B) {
8532 if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8533 match(A, m_Not(m_Specific(CmpRHS)))))
8534 return {L.Flavor, SPNB_NA, false};
8535 }
8536 // b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8537 // ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8538 if (D == A) {
8539 if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8540 match(B, m_Not(m_Specific(CmpRHS)))))
8541 return {L.Flavor, SPNB_NA, false};
8542 }
8543 // b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8544 // ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8545 if (C == A) {
8546 if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8547 match(B, m_Not(m_Specific(CmpRHS)))))
8548 return {L.Flavor, SPNB_NA, false};
8549 }
8550
8551 return {SPF_UNKNOWN, SPNB_NA, false};
8552}
8553
8554/// If the input value is the result of a 'not' op, constant integer, or vector
8555/// splat of a constant integer, return the bitwise-not source value.
8556/// TODO: This could be extended to handle non-splat vector integer constants.
8558 Value *NotV;
8559 if (match(V, m_Not(m_Value(NotV))))
8560 return NotV;
8561
8562 const APInt *C;
8563 if (match(V, m_APInt(C)))
8564 return ConstantInt::get(V->getType(), ~(*C));
8565
8566 return nullptr;
8567}
8568
8569/// Match non-obvious integer minimum and maximum sequences.
8571 Value *CmpLHS, Value *CmpRHS,
8572 Value *TrueVal, Value *FalseVal,
8573 Value *&LHS, Value *&RHS,
8574 unsigned Depth) {
8575 // Assume success. If there's no match, callers should not use these anyway.
8576 LHS = TrueVal;
8577 RHS = FalseVal;
8578
8579 SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);
8581 return SPR;
8582
8583 SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth);
8585 return SPR;
8586
8587 // Look through 'not' ops to find disguised min/max.
8588 // (X > Y) ? ~X : ~Y ==> (~X < ~Y) ? ~X : ~Y ==> MIN(~X, ~Y)
8589 // (X < Y) ? ~X : ~Y ==> (~X > ~Y) ? ~X : ~Y ==> MAX(~X, ~Y)
8590 if (CmpLHS == getNotValue(TrueVal) && CmpRHS == getNotValue(FalseVal)) {
8591 switch (Pred) {
8592 case CmpInst::ICMP_SGT: return {SPF_SMIN, SPNB_NA, false};
8593 case CmpInst::ICMP_SLT: return {SPF_SMAX, SPNB_NA, false};
8594 case CmpInst::ICMP_UGT: return {SPF_UMIN, SPNB_NA, false};
8595 case CmpInst::ICMP_ULT: return {SPF_UMAX, SPNB_NA, false};
8596 default: break;
8597 }
8598 }
8599
8600 // (X > Y) ? ~Y : ~X ==> (~X < ~Y) ? ~Y : ~X ==> MAX(~Y, ~X)
8601 // (X < Y) ? ~Y : ~X ==> (~X > ~Y) ? ~Y : ~X ==> MIN(~Y, ~X)
8602 if (CmpLHS == getNotValue(FalseVal) && CmpRHS == getNotValue(TrueVal)) {
8603 switch (Pred) {
8604 case CmpInst::ICMP_SGT: return {SPF_SMAX, SPNB_NA, false};
8605 case CmpInst::ICMP_SLT: return {SPF_SMIN, SPNB_NA, false};
8606 case CmpInst::ICMP_UGT: return {SPF_UMAX, SPNB_NA, false};
8607 case CmpInst::ICMP_ULT: return {SPF_UMIN, SPNB_NA, false};
8608 default: break;
8609 }
8610 }
8611
8612 if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT)
8613 return {SPF_UNKNOWN, SPNB_NA, false};
8614
8615 const APInt *C1;
8616 if (!match(CmpRHS, m_APInt(C1)))
8617 return {SPF_UNKNOWN, SPNB_NA, false};
8618
8619 // An unsigned min/max can be written with a signed compare.
8620 const APInt *C2;
8621 if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) ||
8622 (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) {
8623 // Is the sign bit set?
8624 // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX
8625 // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN
8626 if (Pred == CmpInst::ICMP_SLT && C1->isZero() && C2->isMaxSignedValue())
8627 return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8628
8629 // Is the sign bit clear?
8630 // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX
8631 // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN
8632 if (Pred == CmpInst::ICMP_SGT && C1->isAllOnes() && C2->isMinSignedValue())
8633 return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8634 }
8635
8636 return {SPF_UNKNOWN, SPNB_NA, false};
8637}
8638
8639bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW,
8640 bool AllowPoison) {
8641 assert(X && Y && "Invalid operand");
8642
8643 auto IsNegationOf = [&](const Value *X, const Value *Y) {
8644 if (!match(X, m_Neg(m_Specific(Y))))
8645 return false;
8646
8647 auto *BO = cast<BinaryOperator>(X);
8648 if (NeedNSW && !BO->hasNoSignedWrap())
8649 return false;
8650
8651 auto *Zero = cast<Constant>(BO->getOperand(0));
8652 if (!AllowPoison && !Zero->isNullValue())
8653 return false;
8654
8655 return true;
8656 };
8657
8658 // X = -Y or Y = -X
8659 if (IsNegationOf(X, Y) || IsNegationOf(Y, X))
8660 return true;
8661
8662 // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A)
8663 Value *A, *B;
8664 return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) &&
8665 match(Y, m_Sub(m_Specific(B), m_Specific(A))))) ||
8666 (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) &&
8668}
8669
8670bool llvm::isKnownInversion(const Value *X, const Value *Y) {
8671 // Handle X = icmp pred A, B, Y = icmp pred A, C.
8672 Value *A, *B, *C;
8673 CmpPredicate Pred1, Pred2;
8674 if (!match(X, m_ICmp(Pred1, m_Value(A), m_Value(B))) ||
8675 !match(Y, m_c_ICmp(Pred2, m_Specific(A), m_Value(C))))
8676 return false;
8677
8678 // They must both have samesign flag or not.
8679 if (Pred1.hasSameSign() != Pred2.hasSameSign())
8680 return false;
8681
8682 if (B == C)
8683 return Pred1 == ICmpInst::getInversePredicate(Pred2);
8684
8685 // Try to infer the relationship from constant ranges.
8686 const APInt *RHSC1, *RHSC2;
8687 if (!match(B, m_APInt(RHSC1)) || !match(C, m_APInt(RHSC2)))
8688 return false;
8689
8690 // Sign bits of two RHSCs should match.
8691 if (Pred1.hasSameSign() && RHSC1->isNonNegative() != RHSC2->isNonNegative())
8692 return false;
8693
8694 const auto CR1 = ConstantRange::makeExactICmpRegion(Pred1, *RHSC1);
8695 const auto CR2 = ConstantRange::makeExactICmpRegion(Pred2, *RHSC2);
8696
8697 return CR1.inverse() == CR2;
8698}
8699
8701 SelectPatternNaNBehavior NaNBehavior,
8702 bool Ordered) {
8703 switch (Pred) {
8704 default:
8705 return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.
8706 case ICmpInst::ICMP_UGT:
8707 case ICmpInst::ICMP_UGE:
8708 return {SPF_UMAX, SPNB_NA, false};
8709 case ICmpInst::ICMP_SGT:
8710 case ICmpInst::ICMP_SGE:
8711 return {SPF_SMAX, SPNB_NA, false};
8712 case ICmpInst::ICMP_ULT:
8713 case ICmpInst::ICMP_ULE:
8714 return {SPF_UMIN, SPNB_NA, false};
8715 case ICmpInst::ICMP_SLT:
8716 case ICmpInst::ICMP_SLE:
8717 return {SPF_SMIN, SPNB_NA, false};
8718 case FCmpInst::FCMP_UGT:
8719 case FCmpInst::FCMP_UGE:
8720 case FCmpInst::FCMP_OGT:
8721 case FCmpInst::FCMP_OGE:
8722 return {SPF_FMAXNUM, NaNBehavior, Ordered};
8723 case FCmpInst::FCMP_ULT:
8724 case FCmpInst::FCMP_ULE:
8725 case FCmpInst::FCMP_OLT:
8726 case FCmpInst::FCMP_OLE:
8727 return {SPF_FMINNUM, NaNBehavior, Ordered};
8728 }
8729}
8730
8731std::optional<std::pair<CmpPredicate, Constant *>>
8734 "Only for relational integer predicates.");
8735 if (isa<UndefValue>(C))
8736 return std::nullopt;
8737
8738 Type *Type = C->getType();
8739 bool IsSigned = ICmpInst::isSigned(Pred);
8740
8742 bool WillIncrement =
8743 UnsignedPred == ICmpInst::ICMP_ULE || UnsignedPred == ICmpInst::ICMP_UGT;
8744
8745 // Check if the constant operand can be safely incremented/decremented
8746 // without overflowing/underflowing.
8747 auto ConstantIsOk = [WillIncrement, IsSigned](ConstantInt *C) {
8748 return WillIncrement ? !C->isMaxValue(IsSigned) : !C->isMinValue(IsSigned);
8749 };
8750
8751 Constant *SafeReplacementConstant = nullptr;
8752 if (auto *CI = dyn_cast<ConstantInt>(C)) {
8753 // Bail out if the constant can't be safely incremented/decremented.
8754 if (!ConstantIsOk(CI))
8755 return std::nullopt;
8756 } else if (auto *FVTy = dyn_cast<FixedVectorType>(Type)) {
8757 unsigned NumElts = FVTy->getNumElements();
8758 for (unsigned i = 0; i != NumElts; ++i) {
8759 Constant *Elt = C->getAggregateElement(i);
8760 if (!Elt)
8761 return std::nullopt;
8762
8763 if (isa<UndefValue>(Elt))
8764 continue;
8765
8766 // Bail out if we can't determine if this constant is min/max or if we
8767 // know that this constant is min/max.
8768 auto *CI = dyn_cast<ConstantInt>(Elt);
8769 if (!CI || !ConstantIsOk(CI))
8770 return std::nullopt;
8771
8772 if (!SafeReplacementConstant)
8773 SafeReplacementConstant = CI;
8774 }
8775 } else if (isa<VectorType>(C->getType())) {
8776 // Handle scalable splat
8777 Value *SplatC = C->getSplatValue();
8778 auto *CI = dyn_cast_or_null<ConstantInt>(SplatC);
8779 // Bail out if the constant can't be safely incremented/decremented.
8780 if (!CI || !ConstantIsOk(CI))
8781 return std::nullopt;
8782 } else {
8783 // ConstantExpr?
8784 return std::nullopt;
8785 }
8786
8787 // It may not be safe to change a compare predicate in the presence of
8788 // undefined elements, so replace those elements with the first safe constant
8789 // that we found.
8790 // TODO: in case of poison, it is safe; let's replace undefs only.
8791 if (C->containsUndefOrPoisonElement()) {
8792 assert(SafeReplacementConstant && "Replacement constant not set");
8793 C = Constant::replaceUndefsWith(C, SafeReplacementConstant);
8794 }
8795
8797
8798 // Increment or decrement the constant.
8799 Constant *OneOrNegOne = ConstantInt::get(Type, WillIncrement ? 1 : -1, true);
8800 Constant *NewC = ConstantExpr::getAdd(C, OneOrNegOne);
8801
8802 return std::make_pair(NewPred, NewC);
8803}
8804
8806 FastMathFlags FMF,
8807 Value *CmpLHS, Value *CmpRHS,
8808 Value *TrueVal, Value *FalseVal,
8809 Value *&LHS, Value *&RHS,
8810 unsigned Depth) {
8811 bool HasMismatchedZeros = false;
8812 if (CmpInst::isFPPredicate(Pred)) {
8813 // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one
8814 // 0.0 operand, set the compare's 0.0 operands to that same value for the
8815 // purpose of identifying min/max. Disregard vector constants with undefined
8816 // elements because those can not be back-propagated for analysis.
8817 Value *OutputZeroVal = nullptr;
8818 if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) &&
8819 !cast<Constant>(TrueVal)->containsUndefOrPoisonElement())
8820 OutputZeroVal = TrueVal;
8821 else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) &&
8822 !cast<Constant>(FalseVal)->containsUndefOrPoisonElement())
8823 OutputZeroVal = FalseVal;
8824
8825 if (OutputZeroVal) {
8826 if (match(CmpLHS, m_AnyZeroFP()) && CmpLHS != OutputZeroVal) {
8827 HasMismatchedZeros = true;
8828 CmpLHS = OutputZeroVal;
8829 }
8830 if (match(CmpRHS, m_AnyZeroFP()) && CmpRHS != OutputZeroVal) {
8831 HasMismatchedZeros = true;
8832 CmpRHS = OutputZeroVal;
8833 }
8834 }
8835 }
8836
8837 LHS = CmpLHS;
8838 RHS = CmpRHS;
8839
8840 // Signed zero may return inconsistent results between implementations.
8841 // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0
8842 // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)
8843 // Therefore, we behave conservatively and only proceed if at least one of the
8844 // operands is known to not be zero or if we don't care about signed zero.
8845 switch (Pred) {
8846 default: break;
8849 if (!HasMismatchedZeros)
8850 break;
8851 [[fallthrough]];
8854 if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8855 !isKnownNonZero(CmpRHS))
8856 return {SPF_UNKNOWN, SPNB_NA, false};
8857 }
8858
8859 SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
8860 bool Ordered = false;
8861
8862 // When given one NaN and one non-NaN input:
8863 // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
8864 // - A simple C99 (a < b ? a : b) construction will return 'b' (as the
8865 // ordered comparison fails), which could be NaN or non-NaN.
8866 // so here we discover exactly what NaN behavior is required/accepted.
8867 if (CmpInst::isFPPredicate(Pred)) {
8868 bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
8869 bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
8870
8871 if (LHSSafe && RHSSafe) {
8872 // Both operands are known non-NaN.
8873 NaNBehavior = SPNB_RETURNS_ANY;
8874 Ordered = CmpInst::isOrdered(Pred);
8875 } else if (CmpInst::isOrdered(Pred)) {
8876 // An ordered comparison will return false when given a NaN, so it
8877 // returns the RHS.
8878 Ordered = true;
8879 if (LHSSafe)
8880 // LHS is non-NaN, so if RHS is NaN then NaN will be returned.
8881 NaNBehavior = SPNB_RETURNS_NAN;
8882 else if (RHSSafe)
8883 NaNBehavior = SPNB_RETURNS_OTHER;
8884 else
8885 // Completely unsafe.
8886 return {SPF_UNKNOWN, SPNB_NA, false};
8887 } else {
8888 Ordered = false;
8889 // An unordered comparison will return true when given a NaN, so it
8890 // returns the LHS.
8891 if (LHSSafe)
8892 // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
8893 NaNBehavior = SPNB_RETURNS_OTHER;
8894 else if (RHSSafe)
8895 NaNBehavior = SPNB_RETURNS_NAN;
8896 else
8897 // Completely unsafe.
8898 return {SPF_UNKNOWN, SPNB_NA, false};
8899 }
8900 }
8901
8902 if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
8903 std::swap(CmpLHS, CmpRHS);
8904 Pred = CmpInst::getSwappedPredicate(Pred);
8905 if (NaNBehavior == SPNB_RETURNS_NAN)
8906 NaNBehavior = SPNB_RETURNS_OTHER;
8907 else if (NaNBehavior == SPNB_RETURNS_OTHER)
8908 NaNBehavior = SPNB_RETURNS_NAN;
8909 Ordered = !Ordered;
8910 }
8911
8912 // ([if]cmp X, Y) ? X : Y
8913 if (TrueVal == CmpLHS && FalseVal == CmpRHS)
8914 return getSelectPattern(Pred, NaNBehavior, Ordered);
8915
8916 if (isKnownNegation(TrueVal, FalseVal)) {
8917 // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can
8918 // match against either LHS or sext(LHS).
8919 auto MaybeSExtCmpLHS =
8920 m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS)));
8921 auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes());
8922 auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One());
8923 if (match(TrueVal, MaybeSExtCmpLHS)) {
8924 // Set the return values. If the compare uses the negated value (-X >s 0),
8925 // swap the return values because the negated value is always 'RHS'.
8926 LHS = TrueVal;
8927 RHS = FalseVal;
8928 if (match(CmpLHS, m_Neg(m_Specific(FalseVal))))
8929 std::swap(LHS, RHS);
8930
8931 // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X)
8932 // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X)
8933 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8934 return {SPF_ABS, SPNB_NA, false};
8935
8936 // (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X)
8937 if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne))
8938 return {SPF_ABS, SPNB_NA, false};
8939
8940 // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X)
8941 // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X)
8942 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8943 return {SPF_NABS, SPNB_NA, false};
8944 }
8945 else if (match(FalseVal, MaybeSExtCmpLHS)) {
8946 // Set the return values. If the compare uses the negated value (-X >s 0),
8947 // swap the return values because the negated value is always 'RHS'.
8948 LHS = FalseVal;
8949 RHS = TrueVal;
8950 if (match(CmpLHS, m_Neg(m_Specific(TrueVal))))
8951 std::swap(LHS, RHS);
8952
8953 // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X)
8954 // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X)
8955 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8956 return {SPF_NABS, SPNB_NA, false};
8957
8958 // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X)
8959 // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X)
8960 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8961 return {SPF_ABS, SPNB_NA, false};
8962 }
8963 }
8964
8965 if (CmpInst::isIntPredicate(Pred))
8966 return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth);
8967
8968 // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar
8969 // may return either -0.0 or 0.0, so fcmp/select pair has stricter
8970 // semantics than minNum. Be conservative in such case.
8971 if (NaNBehavior != SPNB_RETURNS_ANY ||
8972 (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8973 !isKnownNonZero(CmpRHS)))
8974 return {SPF_UNKNOWN, SPNB_NA, false};
8975
8976 return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
8977}
8978
8980 Instruction::CastOps *CastOp) {
8981 const DataLayout &DL = CmpI->getDataLayout();
8982
8983 Constant *CastedTo = nullptr;
8984 switch (*CastOp) {
8985 case Instruction::ZExt:
8986 if (CmpI->isUnsigned())
8987 CastedTo = ConstantExpr::getTrunc(C, SrcTy);
8988 break;
8989 case Instruction::SExt:
8990 if (CmpI->isSigned())
8991 CastedTo = ConstantExpr::getTrunc(C, SrcTy, true);
8992 break;
8993 case Instruction::Trunc:
8994 Constant *CmpConst;
8995 if (match(CmpI->getOperand(1), m_Constant(CmpConst)) &&
8996 CmpConst->getType() == SrcTy) {
8997 // Here we have the following case:
8998 //
8999 // %cond = cmp iN %x, CmpConst
9000 // %tr = trunc iN %x to iK
9001 // %narrowsel = select i1 %cond, iK %t, iK C
9002 //
9003 // We can always move trunc after select operation:
9004 //
9005 // %cond = cmp iN %x, CmpConst
9006 // %widesel = select i1 %cond, iN %x, iN CmpConst
9007 // %tr = trunc iN %widesel to iK
9008 //
9009 // Note that C could be extended in any way because we don't care about
9010 // upper bits after truncation. It can't be abs pattern, because it would
9011 // look like:
9012 //
9013 // select i1 %cond, x, -x.
9014 //
9015 // So only min/max pattern could be matched. Such match requires widened C
9016 // == CmpConst. That is why set widened C = CmpConst, condition trunc
9017 // CmpConst == C is checked below.
9018 CastedTo = CmpConst;
9019 } else {
9020 unsigned ExtOp = CmpI->isSigned() ? Instruction::SExt : Instruction::ZExt;
9021 CastedTo = ConstantFoldCastOperand(ExtOp, C, SrcTy, DL);
9022 }
9023 break;
9024 case Instruction::FPTrunc:
9025 CastedTo = ConstantFoldCastOperand(Instruction::FPExt, C, SrcTy, DL);
9026 break;
9027 case Instruction::FPExt:
9028 CastedTo = ConstantFoldCastOperand(Instruction::FPTrunc, C, SrcTy, DL);
9029 break;
9030 case Instruction::FPToUI:
9031 CastedTo = ConstantFoldCastOperand(Instruction::UIToFP, C, SrcTy, DL);
9032 break;
9033 case Instruction::FPToSI:
9034 CastedTo = ConstantFoldCastOperand(Instruction::SIToFP, C, SrcTy, DL);
9035 break;
9036 case Instruction::UIToFP:
9037 CastedTo = ConstantFoldCastOperand(Instruction::FPToUI, C, SrcTy, DL);
9038 break;
9039 case Instruction::SIToFP:
9040 CastedTo = ConstantFoldCastOperand(Instruction::FPToSI, C, SrcTy, DL);
9041 break;
9042 default:
9043 break;
9044 }
9045
9046 if (!CastedTo)
9047 return nullptr;
9048
9049 // Make sure the cast doesn't lose any information.
9050 Constant *CastedBack =
9051 ConstantFoldCastOperand(*CastOp, CastedTo, C->getType(), DL);
9052 if (CastedBack && CastedBack != C)
9053 return nullptr;
9054
9055 return CastedTo;
9056}
9057
9058/// Helps to match a select pattern in case of a type mismatch.
9059///
9060/// The function processes the case when type of true and false values of a
9061/// select instruction differs from type of the cmp instruction operands because
9062/// of a cast instruction. The function checks if it is legal to move the cast
9063/// operation after "select". If yes, it returns the new second value of
9064/// "select" (with the assumption that cast is moved):
9065/// 1. As operand of cast instruction when both values of "select" are same cast
9066/// instructions.
9067/// 2. As restored constant (by applying reverse cast operation) when the first
9068/// value of the "select" is a cast operation and the second value is a
9069/// constant. It is implemented in lookThroughCastConst().
9070/// 3. As one operand is cast instruction and the other is not. The operands in
9071/// sel(cmp) are in different type integer.
9072/// NOTE: We return only the new second value because the first value could be
9073/// accessed as operand of cast instruction.
9074static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2,
9075 Instruction::CastOps *CastOp) {
9076 auto *Cast1 = dyn_cast<CastInst>(V1);
9077 if (!Cast1)
9078 return nullptr;
9079
9080 *CastOp = Cast1->getOpcode();
9081 Type *SrcTy = Cast1->getSrcTy();
9082 if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
9083 // If V1 and V2 are both the same cast from the same type, look through V1.
9084 if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
9085 return Cast2->getOperand(0);
9086 return nullptr;
9087 }
9088
9089 auto *C = dyn_cast<Constant>(V2);
9090 if (C)
9091 return lookThroughCastConst(CmpI, SrcTy, C, CastOp);
9092
9093 Value *CastedTo = nullptr;
9094 if (*CastOp == Instruction::Trunc) {
9095 if (match(CmpI->getOperand(1), m_ZExtOrSExt(m_Specific(V2)))) {
9096 // Here we have the following case:
9097 // %y_ext = sext iK %y to iN
9098 // %cond = cmp iN %x, %y_ext
9099 // %tr = trunc iN %x to iK
9100 // %narrowsel = select i1 %cond, iK %tr, iK %y
9101 //
9102 // We can always move trunc after select operation:
9103 // %y_ext = sext iK %y to iN
9104 // %cond = cmp iN %x, %y_ext
9105 // %widesel = select i1 %cond, iN %x, iN %y_ext
9106 // %tr = trunc iN %widesel to iK
9107 assert(V2->getType() == Cast1->getType() &&
9108 "V2 and Cast1 should be the same type.");
9109 CastedTo = CmpI->getOperand(1);
9110 }
9111 }
9112
9113 return CastedTo;
9114}
9116 Instruction::CastOps *CastOp,
9117 unsigned Depth) {
9119 return {SPF_UNKNOWN, SPNB_NA, false};
9120
9122 if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
9123
9124 CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
9125 if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};
9126
9127 Value *TrueVal = SI->getTrueValue();
9128 Value *FalseVal = SI->getFalseValue();
9129
9131 CmpI, TrueVal, FalseVal, LHS, RHS,
9132 isa<FPMathOperator>(SI) ? SI->getFastMathFlags() : FastMathFlags(),
9133 CastOp, Depth);
9134}
9135
9137 CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS,
9138 FastMathFlags FMF, Instruction::CastOps *CastOp, unsigned Depth) {
9139 CmpInst::Predicate Pred = CmpI->getPredicate();
9140 Value *CmpLHS = CmpI->getOperand(0);
9141 Value *CmpRHS = CmpI->getOperand(1);
9142 if (isa<FPMathOperator>(CmpI) && CmpI->hasNoNaNs())
9143 FMF.setNoNaNs();
9144
9145 // Bail out early.
9146 if (CmpI->isEquality())
9147 return {SPF_UNKNOWN, SPNB_NA, false};
9148
9149 // Deal with type mismatches.
9150 if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
9151 if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) {
9152 // If this is a potential fmin/fmax with a cast to integer, then ignore
9153 // -0.0 because there is no corresponding integer value.
9154 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9155 FMF.setNoSignedZeros();
9156 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9157 cast<CastInst>(TrueVal)->getOperand(0), C,
9158 LHS, RHS, Depth);
9159 }
9160 if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) {
9161 // If this is a potential fmin/fmax with a cast to integer, then ignore
9162 // -0.0 because there is no corresponding integer value.
9163 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9164 FMF.setNoSignedZeros();
9165 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9166 C, cast<CastInst>(FalseVal)->getOperand(0),
9167 LHS, RHS, Depth);
9168 }
9169 }
9170 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
9171 LHS, RHS, Depth);
9172}
9173
9175 if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT;
9176 if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT;
9177 if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT;
9178 if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT;
9179 if (SPF == SPF_FMINNUM)
9180 return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT;
9181 if (SPF == SPF_FMAXNUM)
9182 return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT;
9183 llvm_unreachable("unhandled!");
9184}
9185
9187 switch (SPF) {
9189 return Intrinsic::umin;
9191 return Intrinsic::umax;
9193 return Intrinsic::smin;
9195 return Intrinsic::smax;
9196 default:
9197 llvm_unreachable("Unexpected SPF");
9198 }
9199}
9200
9202 if (SPF == SPF_SMIN) return SPF_SMAX;
9203 if (SPF == SPF_UMIN) return SPF_UMAX;
9204 if (SPF == SPF_SMAX) return SPF_SMIN;
9205 if (SPF == SPF_UMAX) return SPF_UMIN;
9206 llvm_unreachable("unhandled!");
9207}
9208
9210 switch (MinMaxID) {
9211 case Intrinsic::smax: return Intrinsic::smin;
9212 case Intrinsic::smin: return Intrinsic::smax;
9213 case Intrinsic::umax: return Intrinsic::umin;
9214 case Intrinsic::umin: return Intrinsic::umax;
9215 // Please note that next four intrinsics may produce the same result for
9216 // original and inverted case even if X != Y due to NaN is handled specially.
9217 case Intrinsic::maximum: return Intrinsic::minimum;
9218 case Intrinsic::minimum: return Intrinsic::maximum;
9219 case Intrinsic::maxnum: return Intrinsic::minnum;
9220 case Intrinsic::minnum: return Intrinsic::maxnum;
9221 case Intrinsic::maximumnum:
9222 return Intrinsic::minimumnum;
9223 case Intrinsic::minimumnum:
9224 return Intrinsic::maximumnum;
9225 default: llvm_unreachable("Unexpected intrinsic");
9226 }
9227}
9228
9230 switch (SPF) {
9233 case SPF_UMAX: return APInt::getMaxValue(BitWidth);
9234 case SPF_UMIN: return APInt::getMinValue(BitWidth);
9235 default: llvm_unreachable("Unexpected flavor");
9236 }
9237}
9238
9239std::pair<Intrinsic::ID, bool>
9241 // Check if VL contains select instructions that can be folded into a min/max
9242 // vector intrinsic and return the intrinsic if it is possible.
9243 // TODO: Support floating point min/max.
9244 bool AllCmpSingleUse = true;
9245 SelectPatternResult SelectPattern;
9246 SelectPattern.Flavor = SPF_UNKNOWN;
9247 if (all_of(VL, [&SelectPattern, &AllCmpSingleUse](Value *I) {
9248 Value *LHS, *RHS;
9249 auto CurrentPattern = matchSelectPattern(I, LHS, RHS);
9250 if (!SelectPatternResult::isMinOrMax(CurrentPattern.Flavor))
9251 return false;
9252 if (SelectPattern.Flavor != SPF_UNKNOWN &&
9253 SelectPattern.Flavor != CurrentPattern.Flavor)
9254 return false;
9255 SelectPattern = CurrentPattern;
9256 AllCmpSingleUse &=
9258 return true;
9259 })) {
9260 switch (SelectPattern.Flavor) {
9261 case SPF_SMIN:
9262 return {Intrinsic::smin, AllCmpSingleUse};
9263 case SPF_UMIN:
9264 return {Intrinsic::umin, AllCmpSingleUse};
9265 case SPF_SMAX:
9266 return {Intrinsic::smax, AllCmpSingleUse};
9267 case SPF_UMAX:
9268 return {Intrinsic::umax, AllCmpSingleUse};
9269 case SPF_FMAXNUM:
9270 return {Intrinsic::maxnum, AllCmpSingleUse};
9271 case SPF_FMINNUM:
9272 return {Intrinsic::minnum, AllCmpSingleUse};
9273 default:
9274 llvm_unreachable("unexpected select pattern flavor");
9275 }
9276 }
9277 return {Intrinsic::not_intrinsic, false};
9278}
9279
9280template <typename InstTy>
9281static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst,
9282 Value *&Init, Value *&OtherOp) {
9283 // Handle the case of a simple two-predecessor recurrence PHI.
9284 // There's a lot more that could theoretically be done here, but
9285 // this is sufficient to catch some interesting cases.
9286 // TODO: Expand list -- gep, uadd.sat etc.
9287 if (PN->getNumIncomingValues() != 2)
9288 return false;
9289
9290 for (unsigned I = 0; I != 2; ++I) {
9291 if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
9292 Operation && Operation->getNumOperands() >= 2) {
9293 Value *LHS = Operation->getOperand(0);
9294 Value *RHS = Operation->getOperand(1);
9295 if (LHS != PN && RHS != PN)
9296 continue;
9297
9298 Inst = Operation;
9299 Init = PN->getIncomingValue(!I);
9300 OtherOp = (LHS == PN) ? RHS : LHS;
9301 return true;
9302 }
9303 }
9304 return false;
9305}
9306
9307template <typename InstTy>
9308static bool matchThreeInputRecurrence(const PHINode *PN, InstTy *&Inst,
9309 Value *&Init, Value *&OtherOp0,
9310 Value *&OtherOp1) {
9311 if (PN->getNumIncomingValues() != 2)
9312 return false;
9313
9314 for (unsigned I = 0; I != 2; ++I) {
9315 if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
9316 Operation && Operation->getNumOperands() >= 3) {
9317 Value *Op0 = Operation->getOperand(0);
9318 Value *Op1 = Operation->getOperand(1);
9319 Value *Op2 = Operation->getOperand(2);
9320
9321 if (Op0 != PN && Op1 != PN && Op2 != PN)
9322 continue;
9323
9324 Inst = Operation;
9325 Init = PN->getIncomingValue(!I);
9326 if (Op0 == PN) {
9327 OtherOp0 = Op1;
9328 OtherOp1 = Op2;
9329 } else if (Op1 == PN) {
9330 OtherOp0 = Op0;
9331 OtherOp1 = Op2;
9332 } else {
9333 OtherOp0 = Op0;
9334 OtherOp1 = Op1;
9335 }
9336 return true;
9337 }
9338 }
9339 return false;
9340}
9342 Value *&Start, Value *&Step) {
9343 // We try to match a recurrence of the form:
9344 // %iv = [Start, %entry], [%iv.next, %backedge]
9345 // %iv.next = binop %iv, Step
9346 // Or:
9347 // %iv = [Start, %entry], [%iv.next, %backedge]
9348 // %iv.next = binop Step, %iv
9349 return matchTwoInputRecurrence(P, BO, Start, Step);
9350}
9351
9353 Value *&Start, Value *&Step) {
9354 BinaryOperator *BO = nullptr;
9355 P = dyn_cast<PHINode>(I->getOperand(0));
9356 if (!P)
9357 P = dyn_cast<PHINode>(I->getOperand(1));
9358 return P && matchSimpleRecurrence(P, BO, Start, Step) && BO == I;
9359}
9360
9362 PHINode *&P, Value *&Init,
9363 Value *&OtherOp) {
9364 // Binary intrinsics only supported for now.
9365 if (I->arg_size() != 2 || I->getType() != I->getArgOperand(0)->getType() ||
9366 I->getType() != I->getArgOperand(1)->getType())
9367 return false;
9368
9369 IntrinsicInst *II = nullptr;
9370 P = dyn_cast<PHINode>(I->getArgOperand(0));
9371 if (!P)
9372 P = dyn_cast<PHINode>(I->getArgOperand(1));
9373
9374 return P && matchTwoInputRecurrence(P, II, Init, OtherOp) && II == I;
9375}
9376
9378 PHINode *&P, Value *&Init,
9379 Value *&OtherOp0,
9380 Value *&OtherOp1) {
9381 if (I->arg_size() != 3 || I->getType() != I->getArgOperand(0)->getType() ||
9382 I->getType() != I->getArgOperand(1)->getType() ||
9383 I->getType() != I->getArgOperand(2)->getType())
9384 return false;
9385 IntrinsicInst *II = nullptr;
9386 P = dyn_cast<PHINode>(I->getArgOperand(0));
9387 if (!P) {
9388 P = dyn_cast<PHINode>(I->getArgOperand(1));
9389 if (!P)
9390 P = dyn_cast<PHINode>(I->getArgOperand(2));
9391 }
9392 return P && matchThreeInputRecurrence(P, II, Init, OtherOp0, OtherOp1) &&
9393 II == I;
9394}
9395
9396/// Return true if "icmp Pred LHS RHS" is always true.
9398 const Value *RHS) {
9399 if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS)
9400 return true;
9401
9402 switch (Pred) {
9403 default:
9404 return false;
9405
9406 case CmpInst::ICMP_SLE: {
9407 const APInt *C;
9408
9409 // LHS s<= LHS +_{nsw} C if C >= 0
9410 // LHS s<= LHS | C if C >= 0
9411 if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C))) ||
9413 return !C->isNegative();
9414
9415 // LHS s<= smax(LHS, V) for any V
9417 return true;
9418
9419 // smin(RHS, V) s<= RHS for any V
9421 return true;
9422
9423 // Match A to (X +_{nsw} CA) and B to (X +_{nsw} CB)
9424 const Value *X;
9425 const APInt *CLHS, *CRHS;
9426 if (match(LHS, m_NSWAddLike(m_Value(X), m_APInt(CLHS))) &&
9428 return CLHS->sle(*CRHS);
9429
9430 return false;
9431 }
9432
9433 case CmpInst::ICMP_ULE: {
9434 // LHS u<= LHS +_{nuw} V for any V
9435 if (match(RHS, m_c_Add(m_Specific(LHS), m_Value())) &&
9437 return true;
9438
9439 // LHS u<= LHS | V for any V
9440 if (match(RHS, m_c_Or(m_Specific(LHS), m_Value())))
9441 return true;
9442
9443 // LHS u<= umax(LHS, V) for any V
9445 return true;
9446
9447 // RHS >> V u<= RHS for any V
9448 if (match(LHS, m_LShr(m_Specific(RHS), m_Value())))
9449 return true;
9450
9451 // RHS u/ C_ugt_1 u<= RHS
9452 const APInt *C;
9453 if (match(LHS, m_UDiv(m_Specific(RHS), m_APInt(C))) && C->ugt(1))
9454 return true;
9455
9456 // RHS & V u<= RHS for any V
9458 return true;
9459
9460 // umin(RHS, V) u<= RHS for any V
9462 return true;
9463
9464 // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
9465 const Value *X;
9466 const APInt *CLHS, *CRHS;
9467 if (match(LHS, m_NUWAddLike(m_Value(X), m_APInt(CLHS))) &&
9469 return CLHS->ule(*CRHS);
9470
9471 return false;
9472 }
9473 }
9474}
9475
9476/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
9477/// ALHS ARHS" is true. Otherwise, return std::nullopt.
9478static std::optional<bool>
9480 const Value *ARHS, const Value *BLHS, const Value *BRHS) {
9481 switch (Pred) {
9482 default:
9483 return std::nullopt;
9484
9485 case CmpInst::ICMP_SLT:
9486 case CmpInst::ICMP_SLE:
9487 if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS) &&
9489 return true;
9490 return std::nullopt;
9491
9492 case CmpInst::ICMP_SGT:
9493 case CmpInst::ICMP_SGE:
9494 if (isTruePredicate(CmpInst::ICMP_SLE, ALHS, BLHS) &&
9496 return true;
9497 return std::nullopt;
9498
9499 case CmpInst::ICMP_ULT:
9500 case CmpInst::ICMP_ULE:
9501 if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS) &&
9503 return true;
9504 return std::nullopt;
9505
9506 case CmpInst::ICMP_UGT:
9507 case CmpInst::ICMP_UGE:
9508 if (isTruePredicate(CmpInst::ICMP_ULE, ALHS, BLHS) &&
9510 return true;
9511 return std::nullopt;
9512 }
9513}
9514
9515/// Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
9516/// Return false if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is false.
9517/// Otherwise, return std::nullopt if we can't infer anything.
9518static std::optional<bool>
9520 CmpPredicate RPred, const ConstantRange &RCR) {
9521 auto CRImpliesPred = [&](ConstantRange CR,
9522 CmpInst::Predicate Pred) -> std::optional<bool> {
9523 // If all true values for lhs and true for rhs, lhs implies rhs
9524 if (CR.icmp(Pred, RCR))
9525 return true;
9526
9527 // If there is no overlap, lhs implies not rhs
9528 if (CR.icmp(CmpInst::getInversePredicate(Pred), RCR))
9529 return false;
9530
9531 return std::nullopt;
9532 };
9533 if (auto Res = CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9534 RPred))
9535 return Res;
9536 if (LPred.hasSameSign() ^ RPred.hasSameSign()) {
9538 : LPred.dropSameSign();
9540 : RPred.dropSameSign();
9541 return CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9542 RPred);
9543 }
9544 return std::nullopt;
9545}
9546
9547/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
9548/// is true. Return false if LHS implies RHS is false. Otherwise, return
9549/// std::nullopt if we can't infer anything.
9550static std::optional<bool>
9551isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1,
9552 CmpPredicate RPred, const Value *R0, const Value *R1,
9553 const DataLayout &DL, bool LHSIsTrue) {
9554 // The rest of the logic assumes the LHS condition is true. If that's not the
9555 // case, invert the predicate to make it so.
9556 if (!LHSIsTrue)
9557 LPred = ICmpInst::getInverseCmpPredicate(LPred);
9558
9559 // We can have non-canonical operands, so try to normalize any common operand
9560 // to L0/R0.
9561 if (L0 == R1) {
9562 std::swap(R0, R1);
9563 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9564 }
9565 if (R0 == L1) {
9566 std::swap(L0, L1);
9567 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9568 }
9569 if (L1 == R1) {
9570 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
9571 if (L0 != R0 || match(L0, m_ImmConstant())) {
9572 std::swap(L0, L1);
9573 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9574 std::swap(R0, R1);
9575 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9576 }
9577 }
9578
9579 // See if we can infer anything if operand-0 matches and we have at least one
9580 // constant.
9581 const APInt *Unused;
9582 if (L0 == R0 && (match(L1, m_APInt(Unused)) || match(R1, m_APInt(Unused)))) {
9583 // Potential TODO: We could also further use the constant range of L0/R0 to
9584 // further constraint the constant ranges. At the moment this leads to
9585 // several regressions related to not transforming `multi_use(A + C0) eq/ne
9586 // C1` (see discussion: D58633).
9588 L1, ICmpInst::isSigned(LPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,
9589 /*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1);
9591 R1, ICmpInst::isSigned(RPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,
9592 /*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1);
9593 // Even if L1/R1 are not both constant, we can still sometimes deduce
9594 // relationship from a single constant. For example X u> Y implies X != 0.
9595 if (auto R = isImpliedCondCommonOperandWithCR(LPred, LCR, RPred, RCR))
9596 return R;
9597 // If both L1/R1 were exact constant ranges and we didn't get anything
9598 // here, we won't be able to deduce this.
9599 if (match(L1, m_APInt(Unused)) && match(R1, m_APInt(Unused)))
9600 return std::nullopt;
9601 }
9602
9603 // Can we infer anything when the two compares have matching operands?
9604 if (L0 == R0 && L1 == R1)
9605 return ICmpInst::isImpliedByMatchingCmp(LPred, RPred);
9606
9607 // It only really makes sense in the context of signed comparison for "X - Y
9608 // must be positive if X >= Y and no overflow".
9609 // Take SGT as an example: L0:x > L1:y and C >= 0
9610 // ==> R0:(x -nsw y) < R1:(-C) is false
9611 CmpInst::Predicate SignedLPred = LPred.getPreferredSignedPredicate();
9612 if ((SignedLPred == ICmpInst::ICMP_SGT ||
9613 SignedLPred == ICmpInst::ICMP_SGE) &&
9614 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9615 if (match(R1, m_NonPositive()) &&
9616 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == false)
9617 return false;
9618 }
9619
9620 // Take SLT as an example: L0:x < L1:y and C <= 0
9621 // ==> R0:(x -nsw y) < R1:(-C) is true
9622 if ((SignedLPred == ICmpInst::ICMP_SLT ||
9623 SignedLPred == ICmpInst::ICMP_SLE) &&
9624 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9625 if (match(R1, m_NonNegative()) &&
9626 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == true)
9627 return true;
9628 }
9629
9630 // a - b == NonZero -> a != b
9631 // ptrtoint(a) - ptrtoint(b) == NonZero -> a != b
9632 const APInt *L1C;
9633 Value *A, *B;
9634 if (LPred == ICmpInst::ICMP_EQ && ICmpInst::isEquality(RPred) &&
9635 match(L1, m_APInt(L1C)) && !L1C->isZero() &&
9636 match(L0, m_Sub(m_Value(A), m_Value(B))) &&
9637 ((A == R0 && B == R1) || (A == R1 && B == R0) ||
9642 return RPred.dropSameSign() == ICmpInst::ICMP_NE;
9643 }
9644
9645 // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1
9646 if (L0 == R0 &&
9647 (LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) &&
9648 (RPred == ICmpInst::ICMP_ULT || RPred == ICmpInst::ICMP_UGE) &&
9649 match(L0, m_c_Add(m_Specific(L1), m_Specific(R1))))
9650 return CmpPredicate::getMatching(LPred, RPred).has_value();
9651
9652 if (auto P = CmpPredicate::getMatching(LPred, RPred))
9653 return isImpliedCondOperands(*P, L0, L1, R0, R1);
9654
9655 return std::nullopt;
9656}
9657
9658/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
9659/// is true. Return false if LHS implies RHS is false. Otherwise, return
9660/// std::nullopt if we can't infer anything.
9661static std::optional<bool>
9663 FCmpInst::Predicate RPred, const Value *R0, const Value *R1,
9664 const DataLayout &DL, bool LHSIsTrue) {
9665 // The rest of the logic assumes the LHS condition is true. If that's not the
9666 // case, invert the predicate to make it so.
9667 if (!LHSIsTrue)
9668 LPred = FCmpInst::getInversePredicate(LPred);
9669
9670 // We can have non-canonical operands, so try to normalize any common operand
9671 // to L0/R0.
9672 if (L0 == R1) {
9673 std::swap(R0, R1);
9674 RPred = FCmpInst::getSwappedPredicate(RPred);
9675 }
9676 if (R0 == L1) {
9677 std::swap(L0, L1);
9678 LPred = FCmpInst::getSwappedPredicate(LPred);
9679 }
9680 if (L1 == R1) {
9681 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
9682 if (L0 != R0 || match(L0, m_ImmConstant())) {
9683 std::swap(L0, L1);
9684 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9685 std::swap(R0, R1);
9686 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9687 }
9688 }
9689
9690 // Can we infer anything when the two compares have matching operands?
9691 if (L0 == R0 && L1 == R1) {
9692 if ((LPred & RPred) == LPred)
9693 return true;
9694 if ((LPred & ~RPred) == LPred)
9695 return false;
9696 }
9697
9698 // See if we can infer anything if operand-0 matches and we have at least one
9699 // constant.
9700 const APFloat *L1C, *R1C;
9701 if (L0 == R0 && match(L1, m_APFloat(L1C)) && match(R1, m_APFloat(R1C))) {
9702 if (std::optional<ConstantFPRange> DomCR =
9704 if (std::optional<ConstantFPRange> ImpliedCR =
9706 if (ImpliedCR->contains(*DomCR))
9707 return true;
9708 }
9709 if (std::optional<ConstantFPRange> ImpliedCR =
9711 FCmpInst::getInversePredicate(RPred), *R1C)) {
9712 if (ImpliedCR->contains(*DomCR))
9713 return false;
9714 }
9715 }
9716 }
9717
9718 return std::nullopt;
9719}
9720
9721/// Return true if LHS implies RHS is true. Return false if LHS implies RHS is
9722/// false. Otherwise, return std::nullopt if we can't infer anything. We
9723/// expect the RHS to be an icmp and the LHS to be an 'and', 'or', or a 'select'
9724/// instruction.
9725static std::optional<bool>
9727 const Value *RHSOp0, const Value *RHSOp1,
9728 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9729 // The LHS must be an 'or', 'and', or a 'select' instruction.
9730 assert((LHS->getOpcode() == Instruction::And ||
9731 LHS->getOpcode() == Instruction::Or ||
9732 LHS->getOpcode() == Instruction::Select) &&
9733 "Expected LHS to be 'and', 'or', or 'select'.");
9734
9735 assert(Depth <= MaxAnalysisRecursionDepth && "Hit recursion limit");
9736
9737 // If the result of an 'or' is false, then we know both legs of the 'or' are
9738 // false. Similarly, if the result of an 'and' is true, then we know both
9739 // legs of the 'and' are true.
9740 const Value *ALHS, *ARHS;
9741 if ((!LHSIsTrue && match(LHS, m_LogicalOr(m_Value(ALHS), m_Value(ARHS)))) ||
9742 (LHSIsTrue && match(LHS, m_LogicalAnd(m_Value(ALHS), m_Value(ARHS))))) {
9743 // FIXME: Make this non-recursion.
9744 if (std::optional<bool> Implication = isImpliedCondition(
9745 ALHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9746 return Implication;
9747 if (std::optional<bool> Implication = isImpliedCondition(
9748 ARHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9749 return Implication;
9750 return std::nullopt;
9751 }
9752 return std::nullopt;
9753}
9754
9755std::optional<bool>
9757 const Value *RHSOp0, const Value *RHSOp1,
9758 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9759 // Bail out when we hit the limit.
9761 return std::nullopt;
9762
9763 // A mismatch occurs when we compare a scalar cmp to a vector cmp, for
9764 // example.
9765 if (RHSOp0->getType()->isVectorTy() != LHS->getType()->isVectorTy())
9766 return std::nullopt;
9767
9768 assert(LHS->getType()->isIntOrIntVectorTy(1) &&
9769 "Expected integer type only!");
9770
9771 // Match not
9772 if (match(LHS, m_Not(m_Value(LHS))))
9773 LHSIsTrue = !LHSIsTrue;
9774
9775 // Both LHS and RHS are icmps.
9776 if (RHSOp0->getType()->getScalarType()->isIntOrPtrTy()) {
9777 if (const auto *LHSCmp = dyn_cast<ICmpInst>(LHS))
9778 return isImpliedCondICmps(LHSCmp->getCmpPredicate(),
9779 LHSCmp->getOperand(0), LHSCmp->getOperand(1),
9780 RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue);
9781 const Value *V;
9782 if (match(LHS, m_NUWTrunc(m_Value(V))))
9784 ConstantInt::get(V->getType(), 0), RHSPred,
9785 RHSOp0, RHSOp1, DL, LHSIsTrue);
9786 } else {
9787 assert(RHSOp0->getType()->isFPOrFPVectorTy() &&
9788 "Expected floating point type only!");
9789 if (const auto *LHSCmp = dyn_cast<FCmpInst>(LHS))
9790 return isImpliedCondFCmps(LHSCmp->getPredicate(), LHSCmp->getOperand(0),
9791 LHSCmp->getOperand(1), RHSPred, RHSOp0, RHSOp1,
9792 DL, LHSIsTrue);
9793 }
9794
9795 /// The LHS should be an 'or', 'and', or a 'select' instruction. We expect
9796 /// the RHS to be an icmp.
9797 /// FIXME: Add support for and/or/select on the RHS.
9798 if (const Instruction *LHSI = dyn_cast<Instruction>(LHS)) {
9799 if ((LHSI->getOpcode() == Instruction::And ||
9800 LHSI->getOpcode() == Instruction::Or ||
9801 LHSI->getOpcode() == Instruction::Select))
9802 return isImpliedCondAndOr(LHSI, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue,
9803 Depth);
9804 }
9805 return std::nullopt;
9806}
9807
9808std::optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS,
9809 const DataLayout &DL,
9810 bool LHSIsTrue, unsigned Depth) {
9811 // LHS ==> RHS by definition
9812 if (LHS == RHS)
9813 return LHSIsTrue;
9814
9815 // Match not
9816 bool InvertRHS = false;
9817 if (match(RHS, m_Not(m_Value(RHS)))) {
9818 if (LHS == RHS)
9819 return !LHSIsTrue;
9820 InvertRHS = true;
9821 }
9822
9823 if (const ICmpInst *RHSCmp = dyn_cast<ICmpInst>(RHS)) {
9824 if (auto Implied = isImpliedCondition(
9825 LHS, RHSCmp->getCmpPredicate(), RHSCmp->getOperand(0),
9826 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))
9827 return InvertRHS ? !*Implied : *Implied;
9828 return std::nullopt;
9829 }
9830 if (const FCmpInst *RHSCmp = dyn_cast<FCmpInst>(RHS)) {
9831 if (auto Implied = isImpliedCondition(
9832 LHS, RHSCmp->getPredicate(), RHSCmp->getOperand(0),
9833 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))
9834 return InvertRHS ? !*Implied : *Implied;
9835 return std::nullopt;
9836 }
9837
9838 const Value *V;
9839 if (match(RHS, m_NUWTrunc(m_Value(V)))) {
9840 if (auto Implied = isImpliedCondition(LHS, CmpInst::ICMP_NE, V,
9841 ConstantInt::get(V->getType(), 0), DL,
9842 LHSIsTrue, Depth))
9843 return InvertRHS ? !*Implied : *Implied;
9844 return std::nullopt;
9845 }
9846
9848 return std::nullopt;
9849
9850 // LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS2
9851 // LHS ==> !(RHS1 && RHS2) if LHS ==> !RHS1 or LHS ==> !RHS2
9852 const Value *RHS1, *RHS2;
9853 if (match(RHS, m_LogicalOr(m_Value(RHS1), m_Value(RHS2)))) {
9854 if (std::optional<bool> Imp =
9855 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9856 if (*Imp == true)
9857 return !InvertRHS;
9858 if (std::optional<bool> Imp =
9859 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9860 if (*Imp == true)
9861 return !InvertRHS;
9862 }
9863 if (match(RHS, m_LogicalAnd(m_Value(RHS1), m_Value(RHS2)))) {
9864 if (std::optional<bool> Imp =
9865 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9866 if (*Imp == false)
9867 return InvertRHS;
9868 if (std::optional<bool> Imp =
9869 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9870 if (*Imp == false)
9871 return InvertRHS;
9872 }
9873
9874 return std::nullopt;
9875}
9876
9877// Returns a pair (Condition, ConditionIsTrue), where Condition is a branch
9878// condition dominating ContextI or nullptr, if no condition is found.
9879static std::pair<Value *, bool>
9881 if (!ContextI || !ContextI->getParent())
9882 return {nullptr, false};
9883
9884 // TODO: This is a poor/cheap way to determine dominance. Should we use a
9885 // dominator tree (eg, from a SimplifyQuery) instead?
9886 const BasicBlock *ContextBB = ContextI->getParent();
9887 const BasicBlock *PredBB = ContextBB->getSinglePredecessor();
9888 if (!PredBB)
9889 return {nullptr, false};
9890
9891 // We need a conditional branch in the predecessor.
9892 Value *PredCond;
9893 BasicBlock *TrueBB, *FalseBB;
9894 if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))
9895 return {nullptr, false};
9896
9897 // The branch should get simplified. Don't bother simplifying this condition.
9898 if (TrueBB == FalseBB)
9899 return {nullptr, false};
9900
9901 assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
9902 "Predecessor block does not point to successor?");
9903
9904 // Is this condition implied by the predecessor condition?
9905 return {PredCond, TrueBB == ContextBB};
9906}
9907
9908std::optional<bool> llvm::isImpliedByDomCondition(const Value *Cond,
9909 const Instruction *ContextI,
9910 const DataLayout &DL) {
9911 assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool");
9912 auto PredCond = getDomPredecessorCondition(ContextI);
9913 if (PredCond.first)
9914 return isImpliedCondition(PredCond.first, Cond, DL, PredCond.second);
9915 return std::nullopt;
9916}
9917
9919 const Value *LHS,
9920 const Value *RHS,
9921 const Instruction *ContextI,
9922 const DataLayout &DL) {
9923 auto PredCond = getDomPredecessorCondition(ContextI);
9924 if (PredCond.first)
9925 return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL,
9926 PredCond.second);
9927 return std::nullopt;
9928}
9929
9931 APInt &Upper, const InstrInfoQuery &IIQ,
9932 bool PreferSignedRange) {
9933 unsigned Width = Lower.getBitWidth();
9934 const APInt *C;
9935 switch (BO.getOpcode()) {
9936 case Instruction::Sub:
9937 if (match(BO.getOperand(0), m_APInt(C))) {
9938 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
9939 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
9940
9941 // If the caller expects a signed compare, then try to use a signed range.
9942 // Otherwise if both no-wraps are set, use the unsigned range because it
9943 // is never larger than the signed range. Example:
9944 // "sub nuw nsw i8 -2, x" is unsigned [0, 254] vs. signed [-128, 126].
9945 // "sub nuw nsw i8 2, x" is unsigned [0, 2] vs. signed [-125, 127].
9946 if (PreferSignedRange && HasNSW && HasNUW)
9947 HasNUW = false;
9948
9949 if (HasNUW) {
9950 // 'sub nuw c, x' produces [0, C].
9951 Upper = *C + 1;
9952 } else if (HasNSW) {
9953 if (C->isNegative()) {
9954 // 'sub nsw -C, x' produces [SINT_MIN, -C - SINT_MIN].
9956 Upper = *C - APInt::getSignedMaxValue(Width);
9957 } else {
9958 // Note that sub 0, INT_MIN is not NSW. It techically is a signed wrap
9959 // 'sub nsw C, x' produces [C - SINT_MAX, SINT_MAX].
9960 Lower = *C - APInt::getSignedMaxValue(Width);
9962 }
9963 }
9964 }
9965 break;
9966 case Instruction::Add:
9967 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
9968 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
9969 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
9970
9971 // If the caller expects a signed compare, then try to use a signed
9972 // range. Otherwise if both no-wraps are set, use the unsigned range
9973 // because it is never larger than the signed range. Example: "add nuw
9974 // nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125].
9975 if (PreferSignedRange && HasNSW && HasNUW)
9976 HasNUW = false;
9977
9978 if (HasNUW) {
9979 // 'add nuw x, C' produces [C, UINT_MAX].
9980 Lower = *C;
9981 } else if (HasNSW) {
9982 if (C->isNegative()) {
9983 // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C].
9985 Upper = APInt::getSignedMaxValue(Width) + *C + 1;
9986 } else {
9987 // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX].
9988 Lower = APInt::getSignedMinValue(Width) + *C;
9989 Upper = APInt::getSignedMaxValue(Width) + 1;
9990 }
9991 }
9992 }
9993 break;
9994
9995 case Instruction::And:
9996 if (match(BO.getOperand(1), m_APInt(C)))
9997 // 'and x, C' produces [0, C].
9998 Upper = *C + 1;
9999 // X & -X is a power of two or zero. So we can cap the value at max power of
10000 // two.
10001 if (match(BO.getOperand(0), m_Neg(m_Specific(BO.getOperand(1)))) ||
10002 match(BO.getOperand(1), m_Neg(m_Specific(BO.getOperand(0)))))
10003 Upper = APInt::getSignedMinValue(Width) + 1;
10004 break;
10005
10006 case Instruction::Or:
10007 if (match(BO.getOperand(1), m_APInt(C)))
10008 // 'or x, C' produces [C, UINT_MAX].
10009 Lower = *C;
10010 break;
10011
10012 case Instruction::AShr:
10013 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10014 // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C].
10016 Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1;
10017 } else if (match(BO.getOperand(0), m_APInt(C))) {
10018 unsigned ShiftAmount = Width - 1;
10019 if (!C->isZero() && IIQ.isExact(&BO))
10020 ShiftAmount = C->countr_zero();
10021 if (C->isNegative()) {
10022 // 'ashr C, x' produces [C, C >> (Width-1)]
10023 Lower = *C;
10024 Upper = C->ashr(ShiftAmount) + 1;
10025 } else {
10026 // 'ashr C, x' produces [C >> (Width-1), C]
10027 Lower = C->ashr(ShiftAmount);
10028 Upper = *C + 1;
10029 }
10030 }
10031 break;
10032
10033 case Instruction::LShr:
10034 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10035 // 'lshr x, C' produces [0, UINT_MAX >> C].
10036 Upper = APInt::getAllOnes(Width).lshr(*C) + 1;
10037 } else if (match(BO.getOperand(0), m_APInt(C))) {
10038 // 'lshr C, x' produces [C >> (Width-1), C].
10039 unsigned ShiftAmount = Width - 1;
10040 if (!C->isZero() && IIQ.isExact(&BO))
10041 ShiftAmount = C->countr_zero();
10042 Lower = C->lshr(ShiftAmount);
10043 Upper = *C + 1;
10044 }
10045 break;
10046
10047 case Instruction::Shl:
10048 if (match(BO.getOperand(0), m_APInt(C))) {
10049 if (IIQ.hasNoUnsignedWrap(&BO)) {
10050 // 'shl nuw C, x' produces [C, C << CLZ(C)]
10051 Lower = *C;
10052 Upper = Lower.shl(Lower.countl_zero()) + 1;
10053 } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw?
10054 if (C->isNegative()) {
10055 // 'shl nsw C, x' produces [C << CLO(C)-1, C]
10056 unsigned ShiftAmount = C->countl_one() - 1;
10057 Lower = C->shl(ShiftAmount);
10058 Upper = *C + 1;
10059 } else {
10060 // 'shl nsw C, x' produces [C, C << CLZ(C)-1]
10061 unsigned ShiftAmount = C->countl_zero() - 1;
10062 Lower = *C;
10063 Upper = C->shl(ShiftAmount) + 1;
10064 }
10065 } else {
10066 // If lowbit is set, value can never be zero.
10067 if ((*C)[0])
10068 Lower = APInt::getOneBitSet(Width, 0);
10069 // If we are shifting a constant the largest it can be is if the longest
10070 // sequence of consecutive ones is shifted to the highbits (breaking
10071 // ties for which sequence is higher). At the moment we take a liberal
10072 // upper bound on this by just popcounting the constant.
10073 // TODO: There may be a bitwise trick for it longest/highest
10074 // consecutative sequence of ones (naive method is O(Width) loop).
10075 Upper = APInt::getHighBitsSet(Width, C->popcount()) + 1;
10076 }
10077 } else if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10078 Upper = APInt::getBitsSetFrom(Width, C->getZExtValue()) + 1;
10079 }
10080 break;
10081
10082 case Instruction::SDiv:
10083 if (match(BO.getOperand(1), m_APInt(C))) {
10084 APInt IntMin = APInt::getSignedMinValue(Width);
10085 APInt IntMax = APInt::getSignedMaxValue(Width);
10086 if (C->isAllOnes()) {
10087 // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX]
10088 // where C != -1 and C != 0 and C != 1
10089 Lower = IntMin + 1;
10090 Upper = IntMax + 1;
10091 } else if (C->countl_zero() < Width - 1) {
10092 // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C]
10093 // where C != -1 and C != 0 and C != 1
10094 Lower = IntMin.sdiv(*C);
10095 Upper = IntMax.sdiv(*C);
10096 if (Lower.sgt(Upper))
10098 Upper = Upper + 1;
10099 assert(Upper != Lower && "Upper part of range has wrapped!");
10100 }
10101 } else if (match(BO.getOperand(0), m_APInt(C))) {
10102 if (C->isMinSignedValue()) {
10103 // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2].
10104 Lower = *C;
10105 Upper = Lower.lshr(1) + 1;
10106 } else {
10107 // 'sdiv C, x' produces [-|C|, |C|].
10108 Upper = C->abs() + 1;
10109 Lower = (-Upper) + 1;
10110 }
10111 }
10112 break;
10113
10114 case Instruction::UDiv:
10115 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
10116 // 'udiv x, C' produces [0, UINT_MAX / C].
10117 Upper = APInt::getMaxValue(Width).udiv(*C) + 1;
10118 } else if (match(BO.getOperand(0), m_APInt(C))) {
10119 // 'udiv C, x' produces [0, C].
10120 Upper = *C + 1;
10121 }
10122 break;
10123
10124 case Instruction::SRem:
10125 if (match(BO.getOperand(1), m_APInt(C))) {
10126 // 'srem x, C' produces (-|C|, |C|).
10127 Upper = C->abs();
10128 Lower = (-Upper) + 1;
10129 } else if (match(BO.getOperand(0), m_APInt(C))) {
10130 if (C->isNegative()) {
10131 // 'srem -|C|, x' produces [-|C|, 0].
10132 Upper = 1;
10133 Lower = *C;
10134 } else {
10135 // 'srem |C|, x' produces [0, |C|].
10136 Upper = *C + 1;
10137 }
10138 }
10139 break;
10140
10141 case Instruction::URem:
10142 if (match(BO.getOperand(1), m_APInt(C)))
10143 // 'urem x, C' produces [0, C).
10144 Upper = *C;
10145 else if (match(BO.getOperand(0), m_APInt(C)))
10146 // 'urem C, x' produces [0, C].
10147 Upper = *C + 1;
10148 break;
10149
10150 default:
10151 break;
10152 }
10153}
10154
10156 bool UseInstrInfo) {
10157 unsigned Width = II.getType()->getScalarSizeInBits();
10158 const APInt *C;
10159 switch (II.getIntrinsicID()) {
10160 case Intrinsic::ctlz:
10161 case Intrinsic::cttz: {
10162 APInt Upper(Width, Width);
10163 if (!UseInstrInfo || !match(II.getArgOperand(1), m_One()))
10164 Upper += 1;
10165 // Maximum of set/clear bits is the bit width.
10167 }
10168 case Intrinsic::ctpop:
10169 // Maximum of set/clear bits is the bit width.
10171 APInt(Width, Width) + 1);
10172 case Intrinsic::uadd_sat:
10173 // uadd.sat(x, C) produces [C, UINT_MAX].
10174 if (match(II.getOperand(0), m_APInt(C)) ||
10175 match(II.getOperand(1), m_APInt(C)))
10177 break;
10178 case Intrinsic::sadd_sat:
10179 if (match(II.getOperand(0), m_APInt(C)) ||
10180 match(II.getOperand(1), m_APInt(C))) {
10181 if (C->isNegative())
10182 // sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)].
10184 APInt::getSignedMaxValue(Width) + *C +
10185 1);
10186
10187 // sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].
10189 APInt::getSignedMaxValue(Width) + 1);
10190 }
10191 break;
10192 case Intrinsic::usub_sat:
10193 // usub.sat(C, x) produces [0, C].
10194 if (match(II.getOperand(0), m_APInt(C)))
10195 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
10196
10197 // usub.sat(x, C) produces [0, UINT_MAX - C].
10198 if (match(II.getOperand(1), m_APInt(C)))
10200 APInt::getMaxValue(Width) - *C + 1);
10201 break;
10202 case Intrinsic::ssub_sat:
10203 if (match(II.getOperand(0), m_APInt(C))) {
10204 if (C->isNegative())
10205 // ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)].
10207 *C - APInt::getSignedMinValue(Width) +
10208 1);
10209
10210 // ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].
10212 APInt::getSignedMaxValue(Width) + 1);
10213 } else if (match(II.getOperand(1), m_APInt(C))) {
10214 if (C->isNegative())
10215 // ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]:
10217 APInt::getSignedMaxValue(Width) + 1);
10218
10219 // ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].
10221 APInt::getSignedMaxValue(Width) - *C +
10222 1);
10223 }
10224 break;
10225 case Intrinsic::umin:
10226 case Intrinsic::umax:
10227 case Intrinsic::smin:
10228 case Intrinsic::smax:
10229 if (!match(II.getOperand(0), m_APInt(C)) &&
10230 !match(II.getOperand(1), m_APInt(C)))
10231 break;
10232
10233 switch (II.getIntrinsicID()) {
10234 case Intrinsic::umin:
10235 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
10236 case Intrinsic::umax:
10238 case Intrinsic::smin:
10240 *C + 1);
10241 case Intrinsic::smax:
10243 APInt::getSignedMaxValue(Width) + 1);
10244 default:
10245 llvm_unreachable("Must be min/max intrinsic");
10246 }
10247 break;
10248 case Intrinsic::abs:
10249 // If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX],
10250 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10251 if (match(II.getOperand(1), m_One()))
10253 APInt::getSignedMaxValue(Width) + 1);
10254
10256 APInt::getSignedMinValue(Width) + 1);
10257 case Intrinsic::vscale:
10258 if (!II.getParent() || !II.getFunction())
10259 break;
10260 return getVScaleRange(II.getFunction(), Width);
10261 default:
10262 break;
10263 }
10264
10265 return ConstantRange::getFull(Width);
10266}
10267
10269 const InstrInfoQuery &IIQ) {
10270 unsigned BitWidth = SI.getType()->getScalarSizeInBits();
10271 const Value *LHS = nullptr, *RHS = nullptr;
10273 if (R.Flavor == SPF_UNKNOWN)
10274 return ConstantRange::getFull(BitWidth);
10275
10276 if (R.Flavor == SelectPatternFlavor::SPF_ABS) {
10277 // If the negation part of the abs (in RHS) has the NSW flag,
10278 // then the result of abs(X) is [0..SIGNED_MAX],
10279 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10280 if (match(RHS, m_Neg(m_Specific(LHS))) &&
10284
10287 }
10288
10289 if (R.Flavor == SelectPatternFlavor::SPF_NABS) {
10290 // The result of -abs(X) is <= 0.
10292 APInt(BitWidth, 1));
10293 }
10294
10295 const APInt *C;
10296 if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C)))
10297 return ConstantRange::getFull(BitWidth);
10298
10299 switch (R.Flavor) {
10300 case SPF_UMIN:
10302 case SPF_UMAX:
10304 case SPF_SMIN:
10306 *C + 1);
10307 case SPF_SMAX:
10310 default:
10311 return ConstantRange::getFull(BitWidth);
10312 }
10313}
10314
10316 // The maximum representable value of a half is 65504. For floats the maximum
10317 // value is 3.4e38 which requires roughly 129 bits.
10318 unsigned BitWidth = I->getType()->getScalarSizeInBits();
10319 if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy())
10320 return;
10321 if (isa<FPToSIInst>(I) && BitWidth >= 17) {
10322 Lower = APInt(BitWidth, -65504, true);
10323 Upper = APInt(BitWidth, 65505);
10324 }
10325
10326 if (isa<FPToUIInst>(I) && BitWidth >= 16) {
10327 // For a fptoui the lower limit is left as 0.
10328 Upper = APInt(BitWidth, 65505);
10329 }
10330}
10331
10333 bool UseInstrInfo, AssumptionCache *AC,
10334 const Instruction *CtxI,
10335 const DominatorTree *DT,
10336 unsigned Depth) {
10337 assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction");
10338
10340 return ConstantRange::getFull(V->getType()->getScalarSizeInBits());
10341
10342 if (auto *C = dyn_cast<Constant>(V))
10343 return C->toConstantRange();
10344
10345 unsigned BitWidth = V->getType()->getScalarSizeInBits();
10346 InstrInfoQuery IIQ(UseInstrInfo);
10347 ConstantRange CR = ConstantRange::getFull(BitWidth);
10348 if (auto *BO = dyn_cast<BinaryOperator>(V)) {
10349 APInt Lower = APInt(BitWidth, 0);
10350 APInt Upper = APInt(BitWidth, 0);
10351 // TODO: Return ConstantRange.
10352 setLimitsForBinOp(*BO, Lower, Upper, IIQ, ForSigned);
10354 } else if (auto *II = dyn_cast<IntrinsicInst>(V))
10355 CR = getRangeForIntrinsic(*II, UseInstrInfo);
10356 else if (auto *SI = dyn_cast<SelectInst>(V)) {
10358 SI->getTrueValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
10360 SI->getFalseValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
10361 CR = CRTrue.unionWith(CRFalse);
10363 } else if (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {
10364 APInt Lower = APInt(BitWidth, 0);
10365 APInt Upper = APInt(BitWidth, 0);
10366 // TODO: Return ConstantRange.
10369 } else if (const auto *A = dyn_cast<Argument>(V))
10370 if (std::optional<ConstantRange> Range = A->getRange())
10371 CR = *Range;
10372
10373 if (auto *I = dyn_cast<Instruction>(V)) {
10374 if (auto *Range = IIQ.getMetadata(I, LLVMContext::MD_range))
10376
10377 if (const auto *CB = dyn_cast<CallBase>(V))
10378 if (std::optional<ConstantRange> Range = CB->getRange())
10379 CR = CR.intersectWith(*Range);
10380 }
10381
10382 if (CtxI && AC) {
10383 // Try to restrict the range based on information from assumptions.
10384 for (auto &AssumeVH : AC->assumptionsFor(V)) {
10385 if (!AssumeVH)
10386 continue;
10387 CallInst *I = cast<CallInst>(AssumeVH);
10388 assert(I->getParent()->getParent() == CtxI->getParent()->getParent() &&
10389 "Got assumption for the wrong function!");
10390 assert(I->getIntrinsicID() == Intrinsic::assume &&
10391 "must be an assume intrinsic");
10392
10393 if (!isValidAssumeForContext(I, CtxI, DT))
10394 continue;
10395 Value *Arg = I->getArgOperand(0);
10396 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
10397 // Currently we just use information from comparisons.
10398 if (!Cmp || Cmp->getOperand(0) != V)
10399 continue;
10400 // TODO: Set "ForSigned" parameter via Cmp->isSigned()?
10401 ConstantRange RHS =
10402 computeConstantRange(Cmp->getOperand(1), /* ForSigned */ false,
10403 UseInstrInfo, AC, I, DT, Depth + 1);
10404 CR = CR.intersectWith(
10405 ConstantRange::makeAllowedICmpRegion(Cmp->getPredicate(), RHS));
10406 }
10407 }
10408
10409 return CR;
10410}
10411
10412static void
10414 function_ref<void(Value *)> InsertAffected) {
10415 assert(V != nullptr);
10416 if (isa<Argument>(V) || isa<GlobalValue>(V)) {
10417 InsertAffected(V);
10418 } else if (auto *I = dyn_cast<Instruction>(V)) {
10419 InsertAffected(V);
10420
10421 // Peek through unary operators to find the source of the condition.
10422 Value *Op;
10424 m_Trunc(m_Value(Op))))) {
10426 InsertAffected(Op);
10427 }
10428 }
10429}
10430
10432 Value *Cond, bool IsAssume, function_ref<void(Value *)> InsertAffected) {
10433 auto AddAffected = [&InsertAffected](Value *V) {
10434 addValueAffectedByCondition(V, InsertAffected);
10435 };
10436
10437 auto AddCmpOperands = [&AddAffected, IsAssume](Value *LHS, Value *RHS) {
10438 if (IsAssume) {
10439 AddAffected(LHS);
10440 AddAffected(RHS);
10441 } else if (match(RHS, m_Constant()))
10442 AddAffected(LHS);
10443 };
10444
10445 SmallVector<Value *, 8> Worklist;
10447 Worklist.push_back(Cond);
10448 while (!Worklist.empty()) {
10449 Value *V = Worklist.pop_back_val();
10450 if (!Visited.insert(V).second)
10451 continue;
10452
10453 CmpPredicate Pred;
10454 Value *A, *B, *X;
10455
10456 if (IsAssume) {
10457 AddAffected(V);
10458 if (match(V, m_Not(m_Value(X))))
10459 AddAffected(X);
10460 }
10461
10462 if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
10463 // assume(A && B) is split to -> assume(A); assume(B);
10464 // assume(!(A || B)) is split to -> assume(!A); assume(!B);
10465 // Finally, assume(A || B) / assume(!(A && B)) generally don't provide
10466 // enough information to be worth handling (intersection of information as
10467 // opposed to union).
10468 if (!IsAssume) {
10469 Worklist.push_back(A);
10470 Worklist.push_back(B);
10471 }
10472 } else if (match(V, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
10473 bool HasRHSC = match(B, m_ConstantInt());
10474 if (ICmpInst::isEquality(Pred)) {
10475 AddAffected(A);
10476 if (IsAssume)
10477 AddAffected(B);
10478 if (HasRHSC) {
10479 Value *Y;
10480 // (X << C) or (X >>_s C) or (X >>_u C).
10481 if (match(A, m_Shift(m_Value(X), m_ConstantInt())))
10482 AddAffected(X);
10483 // (X & C) or (X | C).
10484 else if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10485 match(A, m_Or(m_Value(X), m_Value(Y)))) {
10486 AddAffected(X);
10487 AddAffected(Y);
10488 }
10489 // X - Y
10490 else if (match(A, m_Sub(m_Value(X), m_Value(Y)))) {
10491 AddAffected(X);
10492 AddAffected(Y);
10493 }
10494 }
10495 } else {
10496 AddCmpOperands(A, B);
10497 if (HasRHSC) {
10498 // Handle (A + C1) u< C2, which is the canonical form of
10499 // A > C3 && A < C4.
10501 AddAffected(X);
10502
10503 if (ICmpInst::isUnsigned(Pred)) {
10504 Value *Y;
10505 // X & Y u> C -> X >u C && Y >u C
10506 // X | Y u< C -> X u< C && Y u< C
10507 // X nuw+ Y u< C -> X u< C && Y u< C
10508 if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10509 match(A, m_Or(m_Value(X), m_Value(Y))) ||
10510 match(A, m_NUWAdd(m_Value(X), m_Value(Y)))) {
10511 AddAffected(X);
10512 AddAffected(Y);
10513 }
10514 // X nuw- Y u> C -> X u> C
10515 if (match(A, m_NUWSub(m_Value(X), m_Value())))
10516 AddAffected(X);
10517 }
10518 }
10519
10520 // Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported
10521 // by computeKnownFPClass().
10523 if (Pred == ICmpInst::ICMP_SLT && match(B, m_Zero()))
10524 InsertAffected(X);
10525 else if (Pred == ICmpInst::ICMP_SGT && match(B, m_AllOnes()))
10526 InsertAffected(X);
10527 }
10528 }
10529
10530 if (HasRHSC && match(A, m_Intrinsic<Intrinsic::ctpop>(m_Value(X))))
10531 AddAffected(X);
10532 } else if (match(V, m_FCmp(Pred, m_Value(A), m_Value(B)))) {
10533 AddCmpOperands(A, B);
10534
10535 // fcmp fneg(x), y
10536 // fcmp fabs(x), y
10537 // fcmp fneg(fabs(x)), y
10538 if (match(A, m_FNeg(m_Value(A))))
10539 AddAffected(A);
10540 if (match(A, m_FAbs(m_Value(A))))
10541 AddAffected(A);
10542
10544 m_Value()))) {
10545 // Handle patterns that computeKnownFPClass() support.
10546 AddAffected(A);
10547 } else if (!IsAssume && match(V, m_Trunc(m_Value(X)))) {
10548 // Assume is checked here as X is already added above for assumes in
10549 // addValueAffectedByCondition
10550 AddAffected(X);
10551 } else if (!IsAssume && match(V, m_Not(m_Value(X)))) {
10552 // Assume is checked here to avoid issues with ephemeral values
10553 Worklist.push_back(X);
10554 }
10555 }
10556}
10557
10559 // (X >> C) or/add (X & mask(C) != 0)
10560 if (const auto *BO = dyn_cast<BinaryOperator>(V)) {
10561 if (BO->getOpcode() == Instruction::Add ||
10562 BO->getOpcode() == Instruction::Or) {
10563 const Value *X;
10564 const APInt *C1, *C2;
10565 if (match(BO, m_c_BinOp(m_LShr(m_Value(X), m_APInt(C1)),
10569 m_Zero())))) &&
10570 C2->popcount() == C1->getZExtValue())
10571 return X;
10572 }
10573 }
10574 return nullptr;
10575}
10576
10578 return const_cast<Value *>(stripNullTest(const_cast<const Value *>(V)));
10579}
10580
10583 unsigned MaxCount, bool AllowUndefOrPoison) {
10586 auto Push = [&](const Value *V) -> bool {
10587 Constant *C;
10588 if (match(const_cast<Value *>(V), m_ImmConstant(C))) {
10589 if (!AllowUndefOrPoison && !isGuaranteedNotToBeUndefOrPoison(C))
10590 return false;
10591 // Check existence first to avoid unnecessary allocations.
10592 if (Constants.contains(C))
10593 return true;
10594 if (Constants.size() == MaxCount)
10595 return false;
10596 Constants.insert(C);
10597 return true;
10598 }
10599
10600 if (auto *Inst = dyn_cast<Instruction>(V)) {
10601 if (Visited.insert(Inst).second)
10602 Worklist.push_back(Inst);
10603 return true;
10604 }
10605 return false;
10606 };
10607 if (!Push(V))
10608 return false;
10609 while (!Worklist.empty()) {
10610 const Instruction *CurInst = Worklist.pop_back_val();
10611 switch (CurInst->getOpcode()) {
10612 case Instruction::Select:
10613 if (!Push(CurInst->getOperand(1)))
10614 return false;
10615 if (!Push(CurInst->getOperand(2)))
10616 return false;
10617 break;
10618 case Instruction::PHI:
10619 for (Value *IncomingValue : cast<PHINode>(CurInst)->incoming_values()) {
10620 // Fast path for recurrence PHI.
10621 if (IncomingValue == CurInst)
10622 continue;
10623 if (!Push(IncomingValue))
10624 return false;
10625 }
10626 break;
10627 default:
10628 return false;
10629 }
10630 }
10631 return true;
10632}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Register Bank Select
Rewrite undef for PHI
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis Results
This file contains the simple types necessary to represent the attributes associated with functions a...
static const Function * getParent(const Value *V)
#define X(NUM, ENUM, NAME)
Definition ELF.h:849
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Utilities for dealing with flags related to floating point properties and mode controls.
static Value * getCondition(Instruction *I)
Hexagon Common GEP
Module.h This file contains the declarations for the Module class.
static bool hasNoUnsignedWrap(BinaryOperator &I)
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
This file contains the declarations for metadata subclasses.
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
#define P(N)
PowerPC Reduce CR logical Operation
R600 Clause Merge
const SmallVectorImpl< MachineOperand > & Cond
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
std::pair< BasicBlock *, BasicBlock * > Edge
This file contains some templates that are useful if you are working with the STL at all.
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition VPlanSLP.cpp:247
static SmallVector< VPValue *, 4 > getOperands(ArrayRef< VPValue * > Values, unsigned OperandIndex)
Definition VPlanSLP.cpp:210
static void computeKnownFPClassFromCond(const Value *V, Value *Cond, bool CondIsTrue, const Instruction *CxtI, KnownFPClass &KnownFromContext, unsigned Depth=0)
static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero, SimplifyQuery &Q, unsigned Depth)
Try to detect a recurrence that the value of the induction variable is always a power of two (or zero...
static cl::opt< unsigned > DomConditionsMaxUses("dom-conditions-max-uses", cl::Hidden, cl::init(20))
static unsigned computeNumSignBitsVectorConstant(const Value *V, const APInt &DemandedElts, unsigned TyBits)
For vector constants, loop over the elements and find the constant with the minimum number of sign bi...
static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS, const Value *RHS)
Return true if "icmp Pred LHS RHS" is always true.
static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if V1 == (binop V2, X), where X is known non-zero.
static bool isGEPKnownNonNull(const GEPOperator *GEP, const SimplifyQuery &Q, unsigned Depth)
Test whether a GEP's result is known to be non-null.
static bool isNonEqualShl(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and the shift is nuw or nsw.
static bool isKnownNonNullFromDominatingCondition(const Value *V, const Instruction *CtxI, const DominatorTree *DT)
static const Value * getUnderlyingObjectFromInt(const Value *V)
This is the function that does the work of looking through basic ptrtoint+arithmetic+inttoptr sequenc...
static bool isNonZeroMul(const APInt &DemandedElts, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, bool NSW, bool NUW, unsigned Depth)
static bool rangeMetadataExcludesValue(const MDNode *Ranges, const APInt &Value)
Does the 'Range' metadata (which must be a valid MD_range operand list) ensure that the value it's at...
static KnownBits getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts, const KnownBits &KnownLHS, const KnownBits &KnownRHS, const SimplifyQuery &Q, unsigned Depth)
static void breakSelfRecursivePHI(const Use *U, const PHINode *PHI, Value *&ValOut, Instruction *&CtxIOut, const PHINode **PhiOut=nullptr)
static bool isNonZeroSub(const APInt &DemandedElts, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, unsigned Depth)
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR)
Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
static void addValueAffectedByCondition(Value *V, function_ref< void(Value *)> InsertAffected)
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
static bool haveNoCommonBitsSetSpecialCases(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
static void setLimitsForBinOp(const BinaryOperator &BO, APInt &Lower, APInt &Upper, const InstrInfoQuery &IIQ, bool PreferSignedRange)
static Value * lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2, Instruction::CastOps *CastOp)
Helps to match a select pattern in case of a type mismatch.
static std::pair< Value *, bool > getDomPredecessorCondition(const Instruction *ContextI)
static constexpr unsigned MaxInstrsToCheckForFree
Maximum number of instructions to check between assume and context instruction.
static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, const KnownBits &KnownVal, unsigned Depth)
static std::optional< bool > isImpliedCondFCmps(FCmpInst::Predicate LPred, const Value *L0, const Value *L1, FCmpInst::Predicate RPred, const Value *R0, const Value *R1, const DataLayout &DL, bool LHSIsTrue)
Return true if LHS implies RHS (expanded to its components as "R0 RPred R1") is true.
UndefPoisonKind
static bool isKnownNonEqualFromContext(const Value *V1, const Value *V2, const SimplifyQuery &Q, unsigned Depth)
static bool includesPoison(UndefPoisonKind Kind)
static SelectPatternResult matchFastFloatClamp(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS)
Match clamp pattern for float types without care about NaNs or signed zeros.
static std::optional< bool > isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1, CmpPredicate RPred, const Value *R0, const Value *R1, const DataLayout &DL, bool LHSIsTrue)
Return true if LHS implies RHS (expanded to its components as "R0 RPred R1") is true.
static bool includesUndef(UndefPoisonKind Kind)
static std::optional< bool > isImpliedCondCommonOperandWithCR(CmpPredicate LPred, const ConstantRange &LCR, CmpPredicate RPred, const ConstantRange &RCR)
Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
static ConstantRange getRangeForSelectPattern(const SelectInst &SI, const InstrInfoQuery &IIQ)
static void computeKnownBitsFromOperator(const Operator *I, const APInt &DemandedElts, KnownBits &Known, const SimplifyQuery &Q, unsigned Depth)
static uint64_t GetStringLengthH(const Value *V, SmallPtrSetImpl< const PHINode * > &PHIs, unsigned CharSize)
If we can compute the length of the string pointed to by the specified pointer, return 'len+1'.
static void computeKnownBitsFromShiftOperator(const Operator *I, const APInt &DemandedElts, KnownBits &Known, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth, function_ref< KnownBits(const KnownBits &, const KnownBits &, bool)> KF)
Compute known bits from a shift operator, including those with a non-constant shift amount.
static bool onlyUsedByLifetimeMarkersOrDroppableInstsHelper(const Value *V, bool AllowLifetime, bool AllowDroppable)
static std::optional< bool > isImpliedCondAndOr(const Instruction *LHS, CmpPredicate RHSPred, const Value *RHSOp0, const Value *RHSOp1, const DataLayout &DL, bool LHSIsTrue, unsigned Depth)
Return true if LHS implies RHS is true.
static bool isAbsoluteValueLessEqualOne(const Value *V)
static bool isSignedMinMaxClamp(const Value *Select, const Value *&In, const APInt *&CLow, const APInt *&CHigh)
static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, bool NSW, bool NUW, unsigned Depth)
static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V, unsigned Depth)
static bool isNonEqualSelect(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst, Value *&Init, Value *&OtherOp)
static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static void computeKnownBitsFromCmp(const Value *V, CmpInst::Predicate Pred, Value *LHS, Value *RHS, KnownBits &Known, const SimplifyQuery &Q)
static SelectPatternResult matchMinMaxOfMinMax(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TVal, Value *FVal, unsigned Depth)
Recognize variations of: a < c ?
static void unionWithMinMaxIntrinsicClamp(const IntrinsicInst *II, KnownBits &Known)
static void setLimitForFPToI(const Instruction *I, APInt &Lower, APInt &Upper)
static bool isSameUnderlyingObjectInLoop(const PHINode *PN, const LoopInfo *LI)
PN defines a loop-variant pointer to an object.
static bool isNonEqualPointersWithRecursiveGEP(const Value *A, const Value *B, const SimplifyQuery &Q)
static bool isSignedMinMaxIntrinsicClamp(const IntrinsicInst *II, const APInt *&CLow, const APInt *&CHigh)
static Value * lookThroughCastConst(CmpInst *CmpI, Type *SrcTy, Constant *C, Instruction::CastOps *CastOp)
static bool handleGuaranteedWellDefinedOps(const Instruction *I, const CallableT &Handle)
Enumerates all operands of I that are guaranteed to not be undef or poison.
static void computeKnownBitsFromLerpPattern(const Value *Op0, const Value *Op1, const APInt &DemandedElts, KnownBits &KnownOut, const SimplifyQuery &Q, unsigned Depth)
Try to detect the lerp pattern: a * (b - c) + c * d where a >= 0, b >= 0, c >= 0, d >= 0,...
static KnownFPClass computeKnownFPClassFromContext(const Value *V, const SimplifyQuery &Q)
static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1, bool NSW, bool NUW, const APInt &DemandedElts, KnownBits &KnownOut, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth)
static Value * getNotValue(Value *V)
If the input value is the result of a 'not' op, constant integer, or vector splat of a constant integ...
static constexpr KnownFPClass::MinMaxKind getMinMaxKind(Intrinsic::ID IID)
static unsigned ComputeNumSignBitsImpl(const Value *V, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return the number of times the sign bit of the register is replicated into the other bits.
static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp, KnownBits &Known, const SimplifyQuery &SQ, bool Invert)
static bool isKnownNonZeroFromOperator(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static bool matchOpWithOpEqZero(Value *Op0, Value *Op1)
static bool isNonZeroRecurrence(const PHINode *PN)
Try to detect a recurrence that monotonically increases/decreases from a non-zero starting value.
static SelectPatternResult matchClamp(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal)
Recognize variations of: CLAMP(v,l,h) ==> ((v) < (l) ?
static bool shiftAmountKnownInRange(const Value *ShiftAmount)
Shifts return poison if shiftwidth is larger than the bitwidth.
static bool isEphemeralValueOf(const Instruction *I, const Value *E)
static SelectPatternResult matchMinMax(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, unsigned Depth)
Match non-obvious integer minimum and maximum sequences.
static KnownBits computeKnownBitsForHorizontalOperation(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth, const function_ref< KnownBits(const KnownBits &, const KnownBits &)> KnownBitsFunc)
static bool handleGuaranteedNonPoisonOps(const Instruction *I, const CallableT &Handle)
Enumerates all operands of I that are guaranteed to not be poison.
static std::optional< std::pair< Value *, Value * > > getInvertibleOperands(const Operator *Op1, const Operator *Op2)
If the pair of operators are the same invertible function, return the the operands of the function co...
static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS)
static void computeKnownBitsFromCond(const Value *V, Value *Cond, KnownBits &Known, const SimplifyQuery &SQ, bool Invert, unsigned Depth)
static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q)
static std::optional< bool > isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS, const Value *ARHS, const Value *BLHS, const Value *BRHS)
Return true if "icmp Pred BLHS BRHS" is true whenever "icmp PredALHS ARHS" is true.
static const Instruction * safeCxtI(const Value *V, const Instruction *CxtI)
static bool isNonEqualMul(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and the multiplication is nuw o...
static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero, const Value *Cond, bool CondIsTrue)
Return true if we can infer that V is known to be a power of 2 from dominating condition Cond (e....
static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW, bool NUW, const APInt &DemandedElts, KnownBits &Known, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth)
static bool matchThreeInputRecurrence(const PHINode *PN, InstTy *&Inst, Value *&Init, Value *&OtherOp0, Value *&OtherOp1)
static bool isKnownNonNaN(const Value *V, FastMathFlags FMF)
static ConstantRange getRangeForIntrinsic(const IntrinsicInst &II, bool UseInstrInfo)
static void computeKnownFPClassForFPTrunc(const Operator *Op, const APInt &DemandedElts, FPClassTest InterestedClasses, KnownFPClass &Known, const SimplifyQuery &Q, unsigned Depth)
static Value * BuildSubAggregate(Value *From, Value *To, Type *IndexedType, SmallVectorImpl< unsigned > &Idxs, unsigned IdxSkip, BasicBlock::iterator InsertBefore)
Value * RHS
Value * LHS
bool isFinite() const
Definition APFloat.h:1521
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1193
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1153
bool isInteger() const
Definition APFloat.h:1533
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:2011
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1604
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:235
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition APInt.h:1421
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition APInt.h:424
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1555
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition APInt.h:1406
unsigned popcount() const
Count the number of bits set.
Definition APInt.h:1685
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1400
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition APInt.h:207
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition APInt.h:1345
unsigned ceilLogBase2() const
Definition APInt.h:1779
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition APInt.h:1208
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition APInt.h:372
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1189
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:381
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition APInt.cpp:1697
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1503
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1118
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:210
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition APInt.h:217
bool isNegative() const
Determine sign of this APInt.
Definition APInt.h:330
bool intersects(const APInt &RHS) const
This operation tests if there are any pairs of corresponding bits between this APInt and RHS that are...
Definition APInt.h:1256
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1675
void clearAllBits()
Set every bit to 0.
Definition APInt.h:1411
LLVM_ABI APInt reverseBits() const
Definition APInt.cpp:778
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition APInt.h:1173
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1643
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition APInt.h:1613
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:220
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1072
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition APInt.h:357
unsigned logBase2() const
Definition APInt.h:1776
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:834
bool getBoolValue() const
Convert APInt to a boolean value.
Definition APInt.h:472
bool isMaxSignedValue() const
Determine if this is the largest signed value.
Definition APInt.h:406
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:335
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition APInt.h:1157
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:880
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition APInt.h:1264
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition APInt.h:1137
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition APInt.h:297
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:201
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition APInt.h:1403
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1244
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:287
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:240
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:858
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1228
void clearSignBit()
Set the sign bit to 0.
Definition APInt.h:1464
an instruction to allocate memory on the stack
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator end() const
Definition ArrayRef.h:131
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
iterator begin() const
Definition ArrayRef.h:130
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Definition ArrayRef.h:186
Class to represent array types.
This represents the llvm.assume intrinsic.
A cache of @llvm.assume calls within a function.
MutableArrayRef< ResultElem > assumptionsFor(const Value *V)
Access the list of assumptions which affect this value.
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:105
LLVM_ABI std::optional< unsigned > getVScaleRangeMax() const
Returns the maximum value for the vscale_range attribute or std::nullopt when unknown.
LLVM_ABI unsigned getVScaleRangeMin() const
Returns the minimum value for the vscale_range attribute.
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition Attributes.h:261
LLVM 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:846
ConstantDataSequential - A vector or array constant whose element type is a simple 1/2/4/8-byte integ...
Definition Constants.h:736
StringRef getAsString() const
If this array is isString(), then this method returns the array as a StringRef.
Definition Constants.h:812
A vector constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition Constants.h:932
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 bool isAllNonNegative() const
Return true if all values in this range are non-negative.
static LLVM_ABI ConstantRange makeAllowedICmpRegion(CmpInst::Predicate Pred, const ConstantRange &Other)
Produce the smallest range such that all values that may satisfy the given predicate with any value c...
LLVM_ABI ConstantRange unionWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the union of this range with another range.
static LLVM_ABI ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred, const APInt &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
LLVM_ABI bool contains(const APInt &Val) const
Return true if the specified value is in the set.
LLVM_ABI OverflowResult signedAddMayOverflow(const ConstantRange &Other) const
Return whether signed add of the two ranges always/never overflows.
LLVM_ABI ConstantRange intersectWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the intersection of this range with another range.
OverflowResult
Represents whether an operation on the given constant range is known to always or never overflow.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
static ConstantRange getNonEmpty(APInt Lower, APInt Upper)
Create non-empty constant range with the given bounds.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
LLVM_ABI OverflowResult signedSubMayOverflow(const ConstantRange &Other) const
Return whether signed sub of the two ranges always/never overflows.
LLVM_ABI ConstantRange sub(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a subtraction of a value in this r...
This is an important base class in LLVM.
Definition Constant.h:43
static LLVM_ABI Constant * replaceUndefsWith(Constant *C, Constant *Replacement)
Try to replace undefined constant C or undefined elements in C with Replacement.
LLVM_ABI Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
LLVM_ABI Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
LLVM_ABI bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition Constants.cpp:74
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
bool isLittleEndian() const
Layout endianness...
Definition DataLayout.h:215
unsigned getAddressSizeInBits(unsigned AS) const
The size in bits of an address in for the given AS.
Definition DataLayout.h:511
LLVM_ABI const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
LLVM_ABI unsigned getIndexTypeSizeInBits(Type *Ty) const
The size in bits of the index used in GEP calculation for this type.
LLVM_ABI unsigned getPointerTypeSizeInBits(Type *) const
The pointer representation size in bits for this type.
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
Definition DataLayout.h:784
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:135
Type * getValueType() const
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
This instruction compares its operands according to the predicate given to the constructor.
CmpPredicate getSwappedCmpPredicate() const
CmpPredicate getInverseCmpPredicate() const
Predicate getFlippedSignednessPredicate() const
For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->EQ.
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
static LLVM_ABI std::optional< bool > isImpliedByMatchingCmp(CmpPredicate Pred1, CmpPredicate Pred2)
Determine if Pred1 implies Pred2 is true, false, or if nothing can be inferred about the implication,...
bool isRelational() const
Return true if the predicate is relational (not EQ or NE).
Predicate getUnsignedPredicate() const
For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
This instruction inserts a struct field of array element value into an aggregate value.
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
LLVM_ABI bool hasNoNaNs() const LLVM_READONLY
Determine whether the no-NaNs flag is set.
LLVM_ABI bool hasNoUnsignedWrap() const LLVM_READONLY
Determine whether the no unsigned wrap flag is set.
LLVM_ABI bool hasNoSignedWrap() const LLVM_READONLY
Determine whether the no signed wrap flag is set.
bool isBinaryOp() const
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI bool isExact() const LLVM_READONLY
Determine whether the exact flag is set.
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
LLVM_ABI bool comesBefore(const Instruction *Other) const
Given an instruction Other in the same basic block as this instruction, return true if this instructi...
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
bool isUnaryOp() const
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
A wrapper class for inspecting calls to intrinsic functions.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
An instruction for reading from memory.
Value * getPointerOperand()
Align getAlign() const
Return the alignment of the access that is being performed.
bool isLoopHeader(const BlockT *BB) const
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
Metadata node.
Definition Metadata.h:1080
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1444
This is a utility class that provides an abstraction for the common functionality between Instruction...
Definition Operator.h:33
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
Definition Operator.h:43
Utility class for integer operators which may exhibit overflow - Add, Sub, Mul, and Shl.
Definition Operator.h:78
iterator_range< const_block_iterator > blocks() const
Value * getIncomingValueForBlock(const BasicBlock *BB) const
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A udiv, sdiv, lshr, or ashr instruction, which can be marked as "exact", indicating that no bits are ...
Definition Operator.h:154
bool isExact() const
Test whether this division is known to be exact, with zero remainder.
Definition Operator.h:173
This class represents the LLVM 'select' instruction.
const Value * getFalseValue() const
const Value * getCondition() const
const Value * getTrueValue() const
This instruction constructs a fixed permutation of two input vectors.
VectorType * getType() const
Overload to return most specific vector type.
static LLVM_ABI void getShuffleMask(const Constant *Mask, SmallVectorImpl< int > &Result)
Convert the input shuffle mask operand to a vector of integers.
size_type size() const
Definition SmallPtrSet.h:99
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void reserve(size_type N)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:591
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition DataLayout.h:736
TypeSize getElementOffset(unsigned Idx) const
Definition DataLayout.h:767
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:256
const Value * stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, APInt &Offset) const
This is a wrapper around stripAndAccumulateConstantOffsets with the in-bounds requirement set to fals...
Definition Value.h:766
iterator_range< user_iterator > users()
Definition Value.h:427
LLVM_ABI const Value * stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, bool AllowInvariantGroup=false, function_ref< bool(Value &Value, APInt &Offset)> ExternalAnalysis=nullptr, bool LookThroughIntToPtr=false) const
Accumulate the constant offset this value has compared to a base pointer.
const KnownBits & getKnownBits(const SimplifyQuery &Q) const
Definition WithCache.h:59
PointerType getValue() const
Definition WithCache.h:57
Represents an op.with.overflow intrinsic.
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:168
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:165
An efficient, type-erasing, non-owning reference to a callable.
TypeSize getSequentialElementStride(const DataLayout &DL) const
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:123
A range adaptor for a pair of iterators.
CallInst * Call
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
#define UINT64_MAX
Definition DataTypes.h:77
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI APInt ScaleBitMask(const APInt &A, unsigned NewBitWidth, bool MatchAllBits=false)
Splat/Merge neighboring bits to widen/narrow the bitmask represented by.
Definition APInt.cpp:3049
const APInt & umax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be unsigned.
Definition APInt.h:2281
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
SpecificConstantMatch m_ZeroInt()
Convenience matchers for specific integer values.
BinaryOp_match< SpecificConstantMatch, SrcTy, TargetOpcode::G_SUB > m_Neg(const SrcTy &&Src)
Matches a register negated by a G_SUB.
BinaryOp_match< SrcTy, SpecificConstantMatch, TargetOpcode::G_XOR, true > m_Not(const SrcTy &&Src)
Matches a register not-ed by a G_XOR.
OneUse_match< SubPat > m_OneUse(const SubPat &SP)
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
cst_pred_ty< is_lowbit_mask > m_LowBitMask()
Match an integer or vector with only the low bit(s) set.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
PtrToIntSameSize_match< OpTy > m_PtrToIntSameSize(const DataLayout &DL, const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, FCmpInst > m_FCmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
cst_pred_ty< is_sign_mask > m_SignMask()
Match an integer or vector with only the sign bit(s) set.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWAdd(const LHS &L, const RHS &R)
auto m_PtrToIntOrAddr(const OpTy &Op)
Matches PtrToInt or PtrToAddr.
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
BinaryOp_match< LHS, RHS, Instruction::URem > m_URem(const LHS &L, const RHS &R)
auto m_LogicalOp()
Matches either L && R or L || R where L and R are arbitrary values.
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
ap_match< APInt > m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
BinaryOp_match< LHS, RHS, Instruction::And, true > m_c_And(const LHS &L, const RHS &R)
Matches an And with LHS and RHS in either order.
cst_pred_ty< is_power2_or_zero > m_Power2OrZero()
Match an integer or vector of 0 or power-of-2 values.
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWSub(const LHS &L, const RHS &R)
bool match(Val *V, const Pattern &P)
BinOpPred_match< LHS, RHS, is_idiv_op > m_IDiv(const LHS &L, const RHS &R)
Matches integer division operations.
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
ap_match< APFloat > m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
CmpClass_match< LHS, RHS, ICmpInst, true > m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
auto match_fn(const Pattern &P)
A match functor that can be used as a UnaryPredicate in functional algorithms like all_of.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true > m_c_NUWAdd(const LHS &L, const RHS &R)
cst_pred_ty< is_nonnegative > m_NonNegative()
Match an integer or vector of non-negative values.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
IntrinsicID_match m_Intrinsic()
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
IntrinsicID_match m_VScale()
Matches a call to llvm.vscale().
match_combine_or< MaxMin_match< FCmpInst, LHS, RHS, ofmin_pred_ty >, MaxMin_match< FCmpInst, LHS, RHS, ufmin_pred_ty > > m_OrdOrUnordFMin(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point minimum function.
ExtractValue_match< Ind, Val_t > m_ExtractValue(const Val_t &V)
Match a single index ExtractValue instruction.
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > m_SMin(const LHS &L, const RHS &R)
bind_ty< WithOverflowInst > m_WithOverflowInst(WithOverflowInst *&I)
Match a with overflow intrinsic, capturing it if we match.
BinaryOp_match< LHS, RHS, Instruction::Xor, true > m_c_Xor(const LHS &L, const RHS &R)
Matches an Xor with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty, true > m_c_SMin(const LHS &L, const RHS &R)
Matches an SMin with LHS and RHS in either order.
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty, true > m_c_UMax(const LHS &L, const RHS &R)
Matches a UMax with LHS and RHS in either order.
SpecificCmpClass_match< LHS, RHS, ICmpInst > m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
BinaryOp_match< LHS, RHS, Instruction::UDiv > m_UDiv(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty > m_UMax(const LHS &L, const RHS &R)
brc_match< Cond_t, bind_ty< BasicBlock >, bind_ty< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
match_immconstant_ty m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
NoWrapTrunc_match< OpTy, TruncInst::NoUnsignedWrap > m_NUWTrunc(const OpTy &Op)
Matches trunc nuw.
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty, true > m_c_UMin(const LHS &L, const RHS &R)
Matches a UMin with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Add, true > m_c_Add(const LHS &L, const RHS &R)
Matches a Add with LHS and RHS in either order.
match_combine_or< BinaryOp_match< LHS, RHS, Instruction::Add >, DisjointOr_match< LHS, RHS > > m_AddLike(const LHS &L, const RHS &R)
Match either "add" or "or disjoint".
match_combine_or< MaxMin_match< FCmpInst, LHS, RHS, ofmax_pred_ty >, MaxMin_match< FCmpInst, LHS, RHS, ufmax_pred_ty > > m_OrdOrUnordFMax(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point maximum function.
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty, true > m_c_SMax(const LHS &L, const RHS &R)
Matches an SMax with LHS and RHS in either order.
CastOperator_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
match_combine_or< match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty, true >, MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty, true > >, match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty, true >, MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty, true > > > m_c_MaxOrMin(const LHS &L, const RHS &R)
cstfp_pred_ty< custom_checkfn< APFloat > > m_CheckedFp(function_ref< bool(const APFloat &)> CheckFn)
Match a float or vector where CheckFn(ele) for each element is true.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWSub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty > m_SMax(const LHS &L, const RHS &R)
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap >, DisjointOr_match< LHS, RHS > > m_NSWAddLike(const LHS &L, const RHS &R)
Match either "add nsw" or "or disjoint".
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap > m_NSWAdd(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, SExtInst > > m_ZExtOrSExt(const OpTy &Op)
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match 'fneg X' as 'fsub -0.0, X'.
BinOpPred_match< LHS, RHS, is_shift_op > m_Shift(const LHS &L, const RHS &R)
Matches shift operations.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
BinOpPred_match< LHS, RHS, is_irem_op > m_IRem(const LHS &L, const RHS &R)
Matches integer remainder operations.
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
class_match< BasicBlock > m_BasicBlock()
Match an arbitrary basic block value and ignore it.
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
cst_pred_ty< is_nonpositive > m_NonPositive()
Match an integer or vector of non-positive values.
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap >, DisjointOr_match< LHS, RHS > > m_NUWAddLike(const LHS &L, const RHS &R)
Match either "add nuw" or "or disjoint".
ElementWiseBitCast_match< OpTy > m_ElementWiseBitCast(const OpTy &Op)
m_Intrinsic_Ty< Opnd0 >::Ty m_FAbs(const Opnd0 &Op0)
BinaryOp_match< LHS, RHS, Instruction::Mul, true > m_c_Mul(const LHS &L, const RHS &R)
Matches a Mul with LHS and RHS in either order.
CastOperator_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > m_UMin(const LHS &L, const RHS &R)
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
static unsigned decodeVSEW(unsigned VSEW)
LLVM_ABI unsigned getSEWLMULRatio(unsigned SEW, VLMUL VLMul)
static constexpr unsigned RVVBitsPerBlock
initializer< Ty > init(const Ty &Val)
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:668
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI bool haveNoCommonBitsSet(const WithCache< const Value * > &LHSCache, const WithCache< const Value * > &RHSCache, const SimplifyQuery &SQ)
Return true if LHS and RHS have no common bits set.
LLVM_ABI bool mustExecuteUBIfPoisonOnPathTo(Instruction *Root, Instruction *OnPathTo, DominatorTree *DT)
Return true if undefined behavior would provable be executed on the path to OnPathTo if Root produced...
LLVM_ABI Intrinsic::ID getInverseMinMaxIntrinsic(Intrinsic::ID MinMaxID)
LLVM_ABI bool willNotFreeBetween(const Instruction *Assume, const Instruction *CtxI)
Returns true, if no instruction between Assume and CtxI may free memory and the function is marked as...
@ Offset
Definition DWP.cpp:532
@ Length
Definition DWP.cpp:532
@ NeverOverflows
Never overflows.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
LLVM_ABI KnownFPClass computeKnownFPClass(const Value *V, const APInt &DemandedElts, FPClassTest InterestedClasses, const SimplifyQuery &SQ, unsigned Depth=0)
Determine which floating-point classes are valid for V, and return them in KnownFPClass bit sets.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1739
MaybeAlign getAlign(const CallInst &I, unsigned Index)
LLVM_ABI bool isValidAssumeForContext(const Instruction *I, const Instruction *CxtI, const DominatorTree *DT=nullptr, bool AllowEphemerals=false)
Return true if it is valid to use the assumptions provided by an assume intrinsic,...
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1669
LLVM_ABI bool canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
LLVM_ABI bool mustTriggerUB(const Instruction *I, const SmallPtrSetImpl< const Value * > &KnownPoison)
Return true if the given instruction must trigger undefined behavior when I is executed with any oper...
LLVM_ABI bool isKnownNeverInfinity(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point scalar value is not an infinity or if the floating-point vector val...
LLVM_ABI void computeKnownBitsFromContext(const Value *V, KnownBits &Known, const SimplifyQuery &Q, unsigned Depth=0)
Merge bits known from context-dependent facts into Known.
LLVM_ABI bool isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI)
LLVM_ABI bool isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS, bool &TrueIfSigned)
Given an exploded icmp instruction, return true if the comparison only checks the sign bit.
LLVM_ABI const Value * getArgumentAliasingToReturnedPointer(const CallBase *Call, bool MustPreserveNullness)
This function returns call pointer argument that is considered the same by aliasing rules.
LLVM_ABI bool isAssumeLikeIntrinsic(const Instruction *I)
Return true if it is an intrinsic that cannot be speculated but also cannot trap.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2554
LLVM_ABI AllocaInst * findAllocaForValue(Value *V, bool OffsetZero=false)
Returns unique alloca where the value comes from, or nullptr.
LLVM_ABI APInt getMinMaxLimit(SelectPatternFlavor SPF, unsigned BitWidth)
Return the minimum or maximum constant value for the specified integer min/max flavor and type.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI bool isOnlyUsedInZeroComparison(const Instruction *CxtI)
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
LLVM_ABI bool getConstantStringInfo(const Value *V, StringRef &Str, bool TrimAtNul=true)
This function computes the length of a null-terminated C string pointed to by V.
LLVM_ABI bool isDereferenceableAndAlignedPointer(const Value *V, Type *Ty, Align Alignment, const DataLayout &DL, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Returns true if V is always a dereferenceable pointer with alignment greater or equal than requested.
Definition Loads.cpp:229
LLVM_ABI bool onlyUsedByLifetimeMarkersOrDroppableInsts(const Value *V)
Return true if the only users of this pointer are lifetime markers or droppable instructions.
LLVM_ABI Constant * ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset)
LLVM_ABI Value * stripNullTest(Value *V)
Returns the inner value X if the expression has the form f(X) where f(X) == 0 if and only if X == 0,...
LLVM_ABI bool getUnderlyingObjectsForCodeGen(const Value *V, SmallVectorImpl< Value * > &Objects)
This is a wrapper around getUnderlyingObjects and adds support for basic ptrtoint+arithmetic+inttoptr...
LLVM_ABI std::pair< Intrinsic::ID, bool > canConvertToMinOrMaxIntrinsic(ArrayRef< Value * > VL)
Check if the values in VL are select instructions that can be converted to a min or max (vector) intr...
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ABI bool getConstantDataArrayInfo(const Value *V, ConstantDataArraySlice &Slice, unsigned ElementSize, uint64_t Offset=0)
Returns true if the value V is a pointer into a ConstantDataArray.
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition bit.h:323
LLVM_ABI bool isGuaranteedToExecuteForEveryIteration(const Instruction *I, const Loop *L)
Return true if this function can prove that the instruction I is executed for every iteration of the ...
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2208
LLVM_ABI bool mustSuppressSpeculation(const LoadInst &LI)
Return true if speculation of the given load must be suppressed to avoid ordering or interfering with...
Definition Loads.cpp:431
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition MathExtras.h:284
gep_type_iterator gep_type_end(const User *GEP)
int ilogb(const APFloat &Arg)
Returns the exponent of the internal representation of the APFloat.
Definition APFloat.h:1601
LLVM_ABI bool isSafeToSpeculativelyExecute(const Instruction *I, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true, bool IgnoreUBImplyingAttrs=true)
Return true if the instruction does not have any effects besides calculating the result and does not ...
LLVM_ABI Value * getSplatValue(const Value *V)
Get splat value if the input is a splat vector or return nullptr.
LLVM_ABI CmpInst::Predicate getMinMaxPred(SelectPatternFlavor SPF, bool Ordered=false)
Return the canonical comparison predicate for the specified minimum/maximum flavor.
bool isa_and_nonnull(const Y &Val)
Definition Casting.h:676
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:337
LLVM_ABI bool canIgnoreSignBitOfZero(const Use &U)
Return true if the sign bit of the FP value can be ignored by the user when the value is zero.
LLVM_ABI bool isGuaranteedNotToBeUndef(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be undef, but may be poison.
LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
std::tuple< Value *, FPClassTest, FPClassTest > fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS, FPClassTest RHSClass, bool LookThroughSrc=true)
LLVM_ABI ConstantRange computeConstantRange(const Value *V, bool ForSigned, bool UseInstrInfo=true, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Determine the possible constant range of an integer or vector of integer value.
const Value * getPointerOperand(const Value *V)
A helper function that returns the pointer operand of a load, store or GEP instruction.
LLVM_ABI bool MaskedValueIsZero(const Value *V, const APInt &Mask, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if 'V & Mask' is known to be zero.
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:202
LLVM_ABI bool isOverflowIntrinsicNoWrap(const WithOverflowInst *WO, const DominatorTree &DT)
Returns true if the arithmetic part of the WO 's result is used only along the paths control dependen...
LLVM_ABI RetainedKnowledge getKnowledgeFromBundle(AssumeInst &Assume, const CallBase::BundleOpInfo &BOI)
This extracts the Knowledge from an element of an operand bundle.
LLVM_ABI bool matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO, Value *&Start, Value *&Step)
Attempt to match a simple first order recurrence cycle of the form: iv = phi Ty [Start,...
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1746
LLVM_ABI OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ, bool IsNSW=false)
LLVM_ABI bool getShuffleDemandedElts(int SrcWidth, ArrayRef< int > Mask, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS, bool AllowUndefElts=false)
Transform a shuffle mask's output demanded element mask into demanded element masks for the 2 operand...
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:331
bool isGuard(const User *U)
Returns true iff U has semantics of a guard expressed in a form of call of llvm.experimental....
LLVM_ABI SelectPatternFlavor getInverseMinMaxFlavor(SelectPatternFlavor SPF)
Return the inverse minimum/maximum flavor of the specified flavor.
constexpr unsigned MaxAnalysisRecursionDepth
LLVM_ABI void adjustKnownBitsForSelectArm(KnownBits &Known, Value *Cond, Value *Arm, bool Invert, const SimplifyQuery &Q, unsigned Depth=0)
Adjust Known for the given select Arm to include information from the select Cond.
LLVM_ABI bool isKnownNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be negative (i.e.
LLVM_ABI OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
SelectPatternFlavor
Specific patterns of select instructions we can match.
@ SPF_ABS
Floating point maxnum.
@ SPF_NABS
Absolute value.
@ SPF_FMAXNUM
Floating point minnum.
@ SPF_UMIN
Signed minimum.
@ SPF_UMAX
Signed maximum.
@ SPF_SMAX
Unsigned minimum.
@ SPF_UNKNOWN
@ SPF_FMINNUM
Unsigned maximum.
LLVM_ABI bool isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(const CallBase *Call, bool MustPreserveNullness)
{launder,strip}.invariant.group returns pointer that aliases its argument, and it only captures point...
LLVM_ABI bool impliesPoison(const Value *ValAssumedPoison, const Value *V)
Return true if V is poison given that ValAssumedPoison is already poison.
LLVM_ABI void getHorizDemandedEltsForFirstOperand(unsigned VectorBitWidth, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS)
Compute the demanded elements mask of horizontal binary operations.
LLVM_ABI SelectPatternResult getSelectPattern(CmpInst::Predicate Pred, SelectPatternNaNBehavior NaNBehavior=SPNB_NA, bool Ordered=false)
Determine the pattern for predicate X Pred Y ? X : Y.
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI bool programUndefinedIfPoison(const Instruction *Inst)
LLVM_ABI SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
LLVM_ABI bool matchSimpleBinaryIntrinsicRecurrence(const IntrinsicInst *I, PHINode *&P, Value *&Init, Value *&OtherOp)
Attempt to match a simple value-accumulating recurrence of the form: llvm.intrinsic....
LLVM_ABI bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
LLVM_ABI bool cannotBeNegativeZero(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if we can prove that the specified FP value is never equal to -0.0.
LLVM_ABI bool programUndefinedIfUndefOrPoison(const Instruction *Inst)
Return true if this function can prove that if Inst is executed and yields a poison value or undef bi...
LLVM_ABI void adjustKnownFPClassForSelectArm(KnownFPClass &Known, Value *Cond, Value *Arm, bool Invert, const SimplifyQuery &Q, unsigned Depth=0)
Adjust Known for the given select Arm to include information from the select Cond.
generic_gep_type_iterator<> gep_type_iterator
LLVM_ABI bool collectPossibleValues(const Value *V, SmallPtrSetImpl< const Constant * > &Constants, unsigned MaxCount, bool AllowUndefOrPoison=true)
Enumerates all possible immediate values of V and inserts them into the set Constants.
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
LLVM_ABI uint64_t GetStringLength(const Value *V, unsigned CharSize=8)
If we can compute the length of the string pointed to by the specified pointer, return 'len+1'.
LLVM_ABI OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
LLVM_ABI ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
LLVM_ABI Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
LLVM_ABI bool canCreateUndefOrPoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
canCreateUndefOrPoison returns true if Op can create undef or poison from non-undef & non-poison oper...
LLVM_ABI bool matchSimpleTernaryIntrinsicRecurrence(const IntrinsicInst *I, PHINode *&P, Value *&Init, Value *&OtherOp0, Value *&OtherOp1)
Attempt to match a simple value-accumulating recurrence of the form: llvm.intrinsic....
LLVM_ABI EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
LLVM_ABI bool isKnownInversion(const Value *X, const Value *Y)
Return true iff:
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI bool intrinsicPropagatesPoison(Intrinsic::ID IID)
Return whether this intrinsic propagates poison for all operands.
LLVM_ABI bool isNotCrossLaneOperation(const Instruction *I)
Return true if the instruction doesn't potentially cross vector lanes.
LLVM_ABI bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth=0)
Return true if the given value is known to be non-zero when defined.
constexpr int PoisonMaskElem
LLVM_ABI RetainedKnowledge getKnowledgeValidInContext(const Value *V, ArrayRef< Attribute::AttrKind > AttrKinds, AssumptionCache &AC, const Instruction *CtxI, const DominatorTree *DT=nullptr)
Return a valid Knowledge associated to the Value V if its Attribute kind is in AttrKinds and the know...
LLVM_ABI bool isSafeToSpeculativelyExecuteWithOpcode(unsigned Opcode, const Instruction *Inst, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true, bool IgnoreUBImplyingAttrs=true)
This returns the same result as isSafeToSpeculativelyExecute if Opcode is the actual opcode of Inst.
LLVM_ABI bool onlyUsedByLifetimeMarkers(const Value *V)
Return true if the only users of this pointer are lifetime markers.
LLVM_ABI Intrinsic::ID getIntrinsicForCallSite(const CallBase &CB, const TargetLibraryInfo *TLI)
Map a call instruction to an intrinsic ID.
@ Other
Any other memory.
Definition ModRef.h:68
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
LLVM_ABI const Value * getUnderlyingObjectAggressive(const Value *V)
Like getUnderlyingObject(), but will try harder to find a single underlying object.
LLVM_ABI Intrinsic::ID getMinMaxIntrinsic(SelectPatternFlavor SPF)
Convert given SPF to equivalent min/max intrinsic.
LLVM_ABI SelectPatternResult matchDecomposedSelectPattern(CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, FastMathFlags FMF=FastMathFlags(), Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Determine the pattern that a select with the given compare as its predicate and given values as its t...
LLVM_ABI OverflowResult computeOverflowForSignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
LLVM_ABI bool propagatesPoison(const Use &PoisonOp)
Return true if PoisonOp's user yields poison or raises UB if its operand PoisonOp is poison.
@ Add
Sum of integers.
LLVM_ABI ConstantRange computeConstantRangeIncludingKnownBits(const WithCache< const Value * > &V, bool ForSigned, const SimplifyQuery &SQ)
Combine constant ranges from computeConstantRange() and computeKnownBits().
SelectPatternNaNBehavior
Behavior when a floating point min/max is given one NaN and one non-NaN as input.
@ SPNB_RETURNS_NAN
NaN behavior not applicable.
@ SPNB_RETURNS_OTHER
Given one NaN input, returns the NaN.
@ SPNB_RETURNS_ANY
Given one NaN input, returns the non-NaN.
LLVM_ABI bool isKnownNonEqual(const Value *V1, const Value *V2, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the given values are known to be non-equal when defined.
DWARFExpression::Operation Op
LLVM_ABI bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Return the number of times the sign bit of the register is replicated into the other bits.
constexpr unsigned BitWidth
LLVM_ABI KnownBits analyzeKnownBitsFromAndXorOr(const Operator *I, const KnownBits &KnownLHS, const KnownBits &KnownRHS, const SimplifyQuery &SQ, unsigned Depth=0)
Using KnownBits LHS/RHS produce the known bits for logic op (and/xor/or).
LLVM_ABI OverflowResult computeOverflowForUnsignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
LLVM_ABI bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I)
Return true if this function can prove that the instruction I will always transfer execution to one o...
LLVM_ABI bool isKnownNeverInfOrNaN(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point value can never contain a NaN or infinity.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI bool isKnownNeverNaN(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point scalar value is not a NaN or if the floating-point vector value has...
gep_type_iterator gep_type_begin(const User *GEP)
LLVM_ABI Value * isBytewiseValue(Value *V, const DataLayout &DL)
If the specified value can be set by repeating the same byte in memory, return the i8 value that it i...
LLVM_ABI std::optional< std::pair< CmpPredicate, Constant * > > getFlippedStrictnessPredicateAndConstant(CmpPredicate Pred, Constant *C)
Convert an integer comparison with a constant RHS into an equivalent form with the strictness flipped...
LLVM_ABI unsigned ComputeMaxSignificantBits(const Value *Op, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Get the upper bound on bit size for this Value Op as a signed integer.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1947
LLVM_ABI bool isKnownIntegral(const Value *V, const SimplifyQuery &SQ, FastMathFlags FMF)
Return true if the floating-point value V is known to be an integer value.
LLVM_ABI OverflowResult computeOverflowForUnsignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:197
LLVM_ABI bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Return true if the given value is known to have exactly one bit set when defined.
LLVM_ABI std::optional< bool > isImpliedByDomCondition(const Value *Cond, const Instruction *ContextI, const DataLayout &DL)
Return the boolean condition value in the context of the given instruction if it is known based on do...
LLVM_ABI bool isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be poison, but may be undef.
LLVM_ABI void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, KnownBits &Known)
Compute known bits from the range metadata.
LLVM_ABI Value * FindInsertedValue(Value *V, ArrayRef< unsigned > idx_range, std::optional< BasicBlock::iterator > InsertBefore=std::nullopt)
Given an aggregate and an sequence of indices, see if the scalar value indexed is already around as a...
LLVM_ABI bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW=false, bool AllowPoison=true)
Return true if the two given values are negation.
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
LLVM_ABI bool isKnownPositive(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be positive (i.e.
LLVM_ABI Constant * ConstantFoldIntegerCast(Constant *C, Type *DestTy, bool IsSigned, const DataLayout &DL)
Constant fold a zext, sext or trunc, depending on IsSigned and whether the DestTy is wider or narrowe...
LLVM_ABI bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the give value is known to be non-negative.
LLVM_ABI bool cannotBeOrderedLessThanZero(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if we can prove that the specified FP value is either NaN or never less than -0....
LLVM_ABI void getUnderlyingObjects(const Value *V, SmallVectorImpl< const Value * > &Objects, const LoopInfo *LI=nullptr, unsigned MaxLookup=MaxLookupSearchDepth)
This method is similar to getUnderlyingObject except that it can look through phi and select instruct...
LLVM_ABI bool mayHaveNonDefUseDependency(const Instruction &I)
Returns true if the result or effects of the given instructions I depend values not reachable through...
LLVM_ABI bool isTriviallyVectorizable(Intrinsic::ID ID)
Identify if the intrinsic is trivially vectorizable.
LLVM_ABI bool isIdentifiedObject(const Value *V)
Return true if this pointer refers to a distinct and identifiable object.
LLVM_ABI std::optional< bool > isImpliedCondition(const Value *LHS, const Value *RHS, const DataLayout &DL, bool LHSIsTrue=true, unsigned Depth=0)
Return true if RHS is known to be implied true by LHS.
LLVM_ABI std::optional< bool > computeKnownFPSignBit(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return false if we can prove that the specified FP value's sign bit is 0.
LLVM_ABI bool canIgnoreSignBitOfNaN(const Use &U)
Return true if the sign bit of the FP value can be ignored by the user when the value is NaN.
LLVM_ABI void findValuesAffectedByCondition(Value *Cond, bool IsAssume, function_ref< void(Value *)> InsertAffected)
Call InsertAffected on all Values whose known bits / value may be affected by the condition Cond.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:872
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
SmallPtrSet< Value *, 4 > AffectedValues
Represents offset+length into a ConstantDataArray.
const ConstantDataArray * Array
ConstantDataArray pointer.
Represent subnormal handling kind for floating point instruction inputs and outputs.
static constexpr DenormalMode getDynamic()
InstrInfoQuery provides an interface to query additional information for instructions like metadata o...
bool isExact(const BinaryOperator *Op) const
MDNode * getMetadata(const Instruction *I, unsigned KindID) const
bool hasNoSignedZeros(const InstT *Op) const
bool hasNoSignedWrap(const InstT *Op) const
bool hasNoUnsignedWrap(const InstT *Op) const
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition KnownBits.h:317
static LLVM_ABI KnownBits sadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)
KnownBits anyextOrTrunc(unsigned BitWidth) const
Return known bits for an "any" extension or truncation of the value we're tracking.
Definition KnownBits.h:192
static LLVM_ABI KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition KnownBits.h:271
static LLVM_ABI KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition KnownBits.h:108
bool isZero() const
Returns true if value is all zero.
Definition KnownBits.h:80
LLVM_ABI KnownBits blsi() const
Compute known bits for X & -X, which has only the lowest bit set of X set.
void makeNonNegative()
Make this value non-negative.
Definition KnownBits.h:127
static LLVM_ABI KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
unsigned countMinLeadingOnes() const
Returns the minimum number of leading one bits.
Definition KnownBits.h:267
LLVM_ABI KnownBits reduceAdd(unsigned NumElts) const
Compute known bits for horizontal add for a vector with NumElts elements, where each element has the ...
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition KnownBits.h:258
static LLVM_ABI KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
static LLVM_ABI KnownBits ssub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.ssub.sat(LHS, RHS)
static LLVM_ABI KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
bool isUnknown() const
Returns true if we don't know any bits.
Definition KnownBits.h:66
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition KnownBits.h:290
LLVM_ABI KnownBits blsmsk() const
Compute known bits for X ^ (X - 1), which has all bits up to and including the lowest set bit of X se...
void makeNegative()
Make this value negative.
Definition KnownBits.h:122
void setAllConflict()
Make all bits known to be both zero and one.
Definition KnownBits.h:99
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition KnownBits.h:167
KnownBits byteSwap() const
Definition KnownBits.h:547
bool hasConflict() const
Returns true if there is conflicting information.
Definition KnownBits.h:51
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition KnownBits.h:305
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition KnownBits.h:86
KnownBits reverseBits() const
Definition KnownBits.h:551
unsigned getBitWidth() const
Get the bit width of this value.
Definition KnownBits.h:44
static LLVM_ABI KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition KnownBits.h:178
bool isConstant() const
Returns true if we know the value of all bits.
Definition KnownBits.h:54
void resetAll()
Resets the known state of all bits.
Definition KnownBits.h:74
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:363
KnownBits unionWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for either this or RHS or both.
Definition KnownBits.h:337
static LLVM_ABI KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition KnownBits.h:111
bool isEven() const
Return if the value is known even (the low bit is 0).
Definition KnownBits.h:164
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition KnownBits.h:241
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition KnownBits.h:327
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition KnownBits.h:186
unsigned countMinTrailingOnes() const
Returns the minimum number of trailing one bits.
Definition KnownBits.h:261
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition KnownBits.h:202
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition KnownBits.h:264
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:148
static LLVM_ABI KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
static LLVM_ABI KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
static LLVM_ABI KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
static LLVM_ABI KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
APInt getMinValue() const
Return the minimal unsigned value possible given these KnownBits.
Definition KnownBits.h:132
static LLVM_ABI KnownBits computeForAddSub(bool Add, bool NSW, bool NUW, const KnownBits &LHS, const KnownBits &RHS)
Compute known bits resulting from adding LHS and RHS.
Definition KnownBits.cpp:61
static LLVM_ABI KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition KnownBits.h:342
bool isNegative() const
Returns true if this value is known to be negative.
Definition KnownBits.h:105
static KnownBits sub(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from subtraction of LHS and RHS.
Definition KnownBits.h:378
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition KnownBits.h:296
void setAllOnes()
Make all bits known to be one and discard any previous information.
Definition KnownBits.h:92
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition KnownBits.h:235
static LLVM_ABI KnownBits uadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.uadd.sat(LHS, RHS)
static LLVM_ABI KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
KnownBits anyext(unsigned BitWidth) const
Return known bits for an "any" extension of the value we're tracking, where we don't know anything ab...
Definition KnownBits.h:173
static LLVM_ABI KnownBits clmul(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for clmul(LHS, RHS).
LLVM_ABI KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
static LLVM_ABI std::optional< bool > sgt(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SGT result.
static LLVM_ABI std::optional< bool > uge(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_UGE result.
static LLVM_ABI KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
static LLVM_ABI KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
KnownBits sextOrTrunc(unsigned BitWidth) const
Return known bits for a sign extension or truncation of the value we're tracking.
Definition KnownBits.h:212
bool isKnownNeverInfOrNaN() const
Return true if it's known this can never be an infinity or nan.
FPClassTest KnownFPClasses
Floating-point classes the value could be one of.
bool isKnownNeverInfinity() const
Return true if it's known this can never be an infinity.
bool cannotBeOrderedGreaterThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never greater tha...
static LLVM_ABI KnownFPClass sin(const KnownFPClass &Src)
Report known values for sin.
static LLVM_ABI KnownFPClass fdiv_self(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fdiv x, x.
static constexpr FPClassTest OrderedGreaterThanZeroMask
static constexpr FPClassTest OrderedLessThanZeroMask
void knownNot(FPClassTest RuleOut)
static LLVM_ABI KnownFPClass fmul(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fmul.
static LLVM_ABI KnownFPClass fadd_self(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fadd x, x.
void copysign(const KnownFPClass &Sign)
static KnownFPClass square(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
static LLVM_ABI KnownFPClass fsub(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fsub.
KnownFPClass unionWith(const KnownFPClass &RHS) const
static LLVM_ABI KnownFPClass canonicalize(const KnownFPClass &Src, DenormalMode DenormMode=DenormalMode::getDynamic())
Apply the canonicalize intrinsic to this value.
LLVM_ABI bool isKnownNeverLogicalZero(DenormalMode Mode) const
Return true if it's known this can never be interpreted as a zero.
static LLVM_ABI KnownFPClass log(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for log/log2/log10.
static LLVM_ABI KnownFPClass fdiv(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fdiv.
static LLVM_ABI KnownFPClass roundToIntegral(const KnownFPClass &Src, bool IsTrunc, bool IsMultiUnitFPType)
Propagate known class for rounding intrinsics (trunc, floor, ceil, rint, nearbyint,...
static LLVM_ABI KnownFPClass cos(const KnownFPClass &Src)
Report known values for cos.
static LLVM_ABI KnownFPClass ldexp(const KnownFPClass &Src, const KnownBits &N, const fltSemantics &Flt, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for ldexp.
static LLVM_ABI KnownFPClass minMaxLike(const KnownFPClass &LHS, const KnownFPClass &RHS, MinMaxKind Kind, DenormalMode DenormMode=DenormalMode::getDynamic())
bool isUnknown() const
KnownFPClass intersectWith(const KnownFPClass &RHS) const
static LLVM_ABI KnownFPClass exp(const KnownFPClass &Src)
Report known values for exp, exp2 and exp10.
static LLVM_ABI KnownFPClass frexp_mant(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for mantissa component of frexp.
std::optional< bool > SignBit
std::nullopt if the sign bit is unknown, true if the sign bit is definitely set or false if the sign ...
bool isKnownNeverNaN() const
Return true if it's known this can never be a nan.
bool isKnownNever(FPClassTest Mask) const
Return true if it's known this can never be one of the mask entries.
static LLVM_ABI KnownFPClass fpext(const KnownFPClass &KnownSrc, const fltSemantics &DstTy, const fltSemantics &SrcTy)
Propagate known class for fpext.
bool isKnownNeverNegZero() const
Return true if it's known this can never be a negative zero.
static LLVM_ABI KnownFPClass fma(const KnownFPClass &LHS, const KnownFPClass &RHS, const KnownFPClass &Addend, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fma.
void propagateNaN(const KnownFPClass &Src, bool PreserveSign=false)
static LLVM_ABI KnownFPClass fptrunc(const KnownFPClass &KnownSrc)
Propagate known class for fptrunc.
bool cannotBeOrderedLessThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never less than -...
void signBitMustBeOne()
Assume the sign bit is one.
void signBitMustBeZero()
Assume the sign bit is zero.
static LLVM_ABI KnownFPClass sqrt(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for sqrt.
LLVM_ABI bool isKnownNeverLogicalPosZero(DenormalMode Mode) const
Return true if it's known this can never be interpreted as a positive zero.
bool isKnownNeverPosInfinity() const
Return true if it's known this can never be +infinity.
static LLVM_ABI KnownFPClass fadd(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fadd.
LLVM_ABI bool isKnownNeverLogicalNegZero(DenormalMode Mode) const
Return true if it's known this can never be interpreted as a negative zero.
static LLVM_ABI KnownFPClass 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 frem_self(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for frem.
static LLVM_ABI KnownFPClass powi(const KnownFPClass &Src, const KnownBits &N)
Propagate known class for powi.
Represent one information held inside an operand bundle of an llvm.assume.
SelectPatternFlavor Flavor
static bool isMinOrMax(SelectPatternFlavor SPF)
When implementing this min/max pattern as fcmp; select, does the fcmp have to be ordered?
const DataLayout & DL
SimplifyQuery getWithoutCondContext() const
const Instruction * CxtI
const DominatorTree * DT
SimplifyQuery getWithInstruction(const Instruction *I) const
AssumptionCache * AC
const DomConditionCache * DC
const InstrInfoQuery IIQ
const CondContext * CC