LLVM 22.0.0git
ValueTracking.cpp
Go to the documentation of this file.
1//===- ValueTracking.cpp - Walk computations to compute properties --------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains routines that help analyze properties that chains of
10// computations have.
11//
12//===----------------------------------------------------------------------===//
13
15#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/ScopeExit.h"
23#include "llvm/ADT/StringRef.h"
33#include "llvm/Analysis/Loads.h"
38#include "llvm/IR/Argument.h"
39#include "llvm/IR/Attributes.h"
40#include "llvm/IR/BasicBlock.h"
41#include "llvm/IR/Constant.h"
44#include "llvm/IR/Constants.h"
47#include "llvm/IR/Dominators.h"
49#include "llvm/IR/Function.h"
51#include "llvm/IR/GlobalAlias.h"
52#include "llvm/IR/GlobalValue.h"
54#include "llvm/IR/InstrTypes.h"
55#include "llvm/IR/Instruction.h"
58#include "llvm/IR/Intrinsics.h"
59#include "llvm/IR/IntrinsicsAArch64.h"
60#include "llvm/IR/IntrinsicsAMDGPU.h"
61#include "llvm/IR/IntrinsicsRISCV.h"
62#include "llvm/IR/IntrinsicsX86.h"
63#include "llvm/IR/LLVMContext.h"
64#include "llvm/IR/Metadata.h"
65#include "llvm/IR/Module.h"
66#include "llvm/IR/Operator.h"
68#include "llvm/IR/Type.h"
69#include "llvm/IR/User.h"
70#include "llvm/IR/Value.h"
79#include <algorithm>
80#include <cassert>
81#include <cstdint>
82#include <optional>
83#include <utility>
84
85using namespace llvm;
86using namespace llvm::PatternMatch;
87
88// Controls the number of uses of the value searched for possible
89// dominating comparisons.
90static cl::opt<unsigned> DomConditionsMaxUses("dom-conditions-max-uses",
91 cl::Hidden, cl::init(20));
92
93/// Maximum number of instructions to check between assume and context
94/// instruction.
95static constexpr unsigned MaxInstrsToCheckForFree = 16;
96
97/// Returns the bitwidth of the given scalar or pointer type. For vector types,
98/// returns the element type's bitwidth.
99static unsigned getBitWidth(Type *Ty, const DataLayout &DL) {
100 if (unsigned BitWidth = Ty->getScalarSizeInBits())
101 return BitWidth;
102
103 return DL.getPointerTypeSizeInBits(Ty);
104}
105
106// Given the provided Value and, potentially, a context instruction, return
107// the preferred context instruction (if any).
108static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI) {
109 // If we've been provided with a context instruction, then use that (provided
110 // it has been inserted).
111 if (CxtI && CxtI->getParent())
112 return CxtI;
113
114 // If the value is really an already-inserted instruction, then use that.
115 CxtI = dyn_cast<Instruction>(V);
116 if (CxtI && CxtI->getParent())
117 return CxtI;
118
119 return nullptr;
120}
121
123 const APInt &DemandedElts,
124 APInt &DemandedLHS, APInt &DemandedRHS) {
125 if (isa<ScalableVectorType>(Shuf->getType())) {
126 assert(DemandedElts == APInt(1,1));
127 DemandedLHS = DemandedRHS = DemandedElts;
128 return true;
129 }
130
131 int NumElts =
132 cast<FixedVectorType>(Shuf->getOperand(0)->getType())->getNumElements();
133 return llvm::getShuffleDemandedElts(NumElts, Shuf->getShuffleMask(),
134 DemandedElts, DemandedLHS, DemandedRHS);
135}
136
137static void computeKnownBits(const Value *V, const APInt &DemandedElts,
138 KnownBits &Known, const SimplifyQuery &Q,
139 unsigned Depth);
140
142 const SimplifyQuery &Q, unsigned Depth) {
143 // Since the number of lanes in a scalable vector is unknown at compile time,
144 // we track one bit which is implicitly broadcast to all lanes. This means
145 // that all lanes in a scalable vector are considered demanded.
146 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
147 APInt DemandedElts =
148 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
149 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);
150}
151
153 const DataLayout &DL, AssumptionCache *AC,
154 const Instruction *CxtI, const DominatorTree *DT,
155 bool UseInstrInfo, unsigned Depth) {
156 computeKnownBits(V, Known,
157 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo),
158 Depth);
159}
160
162 AssumptionCache *AC, const Instruction *CxtI,
163 const DominatorTree *DT, bool UseInstrInfo,
164 unsigned Depth) {
165 return computeKnownBits(
166 V, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
167}
168
169KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
170 const DataLayout &DL, AssumptionCache *AC,
171 const Instruction *CxtI,
172 const DominatorTree *DT, bool UseInstrInfo,
173 unsigned Depth) {
174 return computeKnownBits(
175 V, DemandedElts,
176 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
177}
178
180 const SimplifyQuery &SQ) {
181 // Look for an inverted mask: (X & ~M) op (Y & M).
182 {
183 Value *M;
184 if (match(LHS, m_c_And(m_Not(m_Value(M)), m_Value())) &&
186 isGuaranteedNotToBeUndef(M, SQ.AC, SQ.CxtI, SQ.DT))
187 return true;
188 }
189
190 // X op (Y & ~X)
193 return true;
194
195 // X op ((X & Y) ^ Y) -- this is the canonical form of the previous pattern
196 // for constant Y.
197 Value *Y;
198 if (match(RHS,
200 isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT) &&
201 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
202 return true;
203
204 // Peek through extends to find a 'not' of the other side:
205 // (ext Y) op ext(~Y)
206 if (match(LHS, m_ZExtOrSExt(m_Value(Y))) &&
208 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
209 return true;
210
211 // Look for: (A & B) op ~(A | B)
212 {
213 Value *A, *B;
214 if (match(LHS, m_And(m_Value(A), m_Value(B))) &&
216 isGuaranteedNotToBeUndef(A, SQ.AC, SQ.CxtI, SQ.DT) &&
217 isGuaranteedNotToBeUndef(B, SQ.AC, SQ.CxtI, SQ.DT))
218 return true;
219 }
220
221 // Look for: (X << V) op (Y >> (BitWidth - V))
222 // or (X >> V) op (Y << (BitWidth - V))
223 {
224 const Value *V;
225 const APInt *R;
226 if (((match(RHS, m_Shl(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&
227 match(LHS, m_LShr(m_Value(), m_Specific(V)))) ||
228 (match(RHS, m_LShr(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&
229 match(LHS, m_Shl(m_Value(), m_Specific(V))))) &&
230 R->uge(LHS->getType()->getScalarSizeInBits()))
231 return true;
232 }
233
234 return false;
235}
236
238 const WithCache<const Value *> &RHSCache,
239 const SimplifyQuery &SQ) {
240 const Value *LHS = LHSCache.getValue();
241 const Value *RHS = RHSCache.getValue();
242
243 assert(LHS->getType() == RHS->getType() &&
244 "LHS and RHS should have the same type");
245 assert(LHS->getType()->isIntOrIntVectorTy() &&
246 "LHS and RHS should be integers");
247
248 if (haveNoCommonBitsSetSpecialCases(LHS, RHS, SQ) ||
250 return true;
251
253 RHSCache.getKnownBits(SQ));
254}
255
257 return !I->user_empty() &&
258 all_of(I->users(), match_fn(m_ICmp(m_Value(), m_Zero())));
259}
260
262 return !I->user_empty() && all_of(I->users(), [](const User *U) {
263 CmpPredicate P;
264 return match(U, m_ICmp(P, m_Value(), m_Zero())) && ICmpInst::isEquality(P);
265 });
266}
267
269 bool OrZero, AssumptionCache *AC,
270 const Instruction *CxtI,
271 const DominatorTree *DT, bool UseInstrInfo,
272 unsigned Depth) {
273 return ::isKnownToBeAPowerOfTwo(
274 V, OrZero, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo),
275 Depth);
276}
277
278static bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
279 const SimplifyQuery &Q, unsigned Depth);
280
282 unsigned Depth) {
283 return computeKnownBits(V, SQ, Depth).isNonNegative();
284}
285
287 unsigned Depth) {
288 if (auto *CI = dyn_cast<ConstantInt>(V))
289 return CI->getValue().isStrictlyPositive();
290
291 // If `isKnownNonNegative` ever becomes more sophisticated, make sure to keep
292 // this updated.
293 KnownBits Known = computeKnownBits(V, SQ, Depth);
294 return Known.isNonNegative() &&
295 (Known.isNonZero() || isKnownNonZero(V, SQ, Depth));
296}
297
299 unsigned Depth) {
300 return computeKnownBits(V, SQ, Depth).isNegative();
301}
302
303static bool isKnownNonEqual(const Value *V1, const Value *V2,
304 const APInt &DemandedElts, const SimplifyQuery &Q,
305 unsigned Depth);
306
307bool llvm::isKnownNonEqual(const Value *V1, const Value *V2,
308 const SimplifyQuery &Q, unsigned Depth) {
309 // We don't support looking through casts.
310 if (V1 == V2 || V1->getType() != V2->getType())
311 return false;
312 auto *FVTy = dyn_cast<FixedVectorType>(V1->getType());
313 APInt DemandedElts =
314 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
315 return ::isKnownNonEqual(V1, V2, DemandedElts, Q, Depth);
316}
317
318bool llvm::MaskedValueIsZero(const Value *V, const APInt &Mask,
319 const SimplifyQuery &SQ, unsigned Depth) {
320 KnownBits Known(Mask.getBitWidth());
321 computeKnownBits(V, Known, SQ, Depth);
322 return Mask.isSubsetOf(Known.Zero);
323}
324
325static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
326 const SimplifyQuery &Q, unsigned Depth);
327
328static unsigned ComputeNumSignBits(const Value *V, const SimplifyQuery &Q,
329 unsigned Depth = 0) {
330 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
331 APInt DemandedElts =
332 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
333 return ComputeNumSignBits(V, DemandedElts, Q, Depth);
334}
335
336unsigned llvm::ComputeNumSignBits(const Value *V, const DataLayout &DL,
337 AssumptionCache *AC, const Instruction *CxtI,
338 const DominatorTree *DT, bool UseInstrInfo,
339 unsigned Depth) {
340 return ::ComputeNumSignBits(
341 V, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
342}
343
345 AssumptionCache *AC,
346 const Instruction *CxtI,
347 const DominatorTree *DT,
348 unsigned Depth) {
349 unsigned SignBits = ComputeNumSignBits(V, DL, AC, CxtI, DT, Depth);
350 return V->getType()->getScalarSizeInBits() - SignBits + 1;
351}
352
353/// Try to detect the lerp pattern: a * (b - c) + c * d
354/// where a >= 0, b >= 0, c >= 0, d >= 0, and b >= c.
355///
356/// In that particular case, we can use the following chain of reasoning:
357///
358/// a * (b - c) + c * d <= a' * (b - c) + a' * c = a' * b where a' = max(a, d)
359///
360/// Since that is true for arbitrary a, b, c and d within our constraints, we
361/// can conclude that:
362///
363/// max(a * (b - c) + c * d) <= max(max(a), max(d)) * max(b) = U
364///
365/// Considering that any result of the lerp would be less or equal to U, it
366/// would have at least the number of leading 0s as in U.
367///
368/// While being quite a specific situation, it is fairly common in computer
369/// graphics in the shape of alpha blending.
370///
371/// Modifies given KnownOut in-place with the inferred information.
372static void computeKnownBitsFromLerpPattern(const Value *Op0, const Value *Op1,
373 const APInt &DemandedElts,
374 KnownBits &KnownOut,
375 const SimplifyQuery &Q,
376 unsigned Depth) {
377
378 Type *Ty = Op0->getType();
379 const unsigned BitWidth = Ty->getScalarSizeInBits();
380
381 // Only handle scalar types for now
382 if (Ty->isVectorTy())
383 return;
384
385 // Try to match: a * (b - c) + c * d.
386 // When a == 1 => A == nullptr, the same applies to d/D as well.
387 const Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
388 const Instruction *SubBC = nullptr;
389
390 const auto MatchSubBC = [&]() {
391 // (b - c) can have two forms that interest us:
392 //
393 // 1. sub nuw %b, %c
394 // 2. xor %c, %b
395 //
396 // For the first case, nuw flag guarantees our requirement b >= c.
397 //
398 // The second case might happen when the analysis can infer that b is a mask
399 // for c and we can transform sub operation into xor (that is usually true
400 // for constant b's). Even though xor is symmetrical, canonicalization
401 // ensures that the constant will be the RHS. We have additional checks
402 // later on to ensure that this xor operation is equivalent to subtraction.
404 m_Xor(m_Value(C), m_Value(B))));
405 };
406
407 const auto MatchASubBC = [&]() {
408 // Cases:
409 // - a * (b - c)
410 // - (b - c) * a
411 // - (b - c) <- a implicitly equals 1
412 return m_CombineOr(m_c_Mul(m_Value(A), MatchSubBC()), MatchSubBC());
413 };
414
415 const auto MatchCD = [&]() {
416 // Cases:
417 // - d * c
418 // - c * d
419 // - c <- d implicitly equals 1
421 };
422
423 const auto Match = [&](const Value *LHS, const Value *RHS) {
424 // We do use m_Specific(C) in MatchCD, so we have to make sure that
425 // it's bound to anything and match(LHS, MatchASubBC()) absolutely
426 // has to evaluate first and return true.
427 //
428 // If Match returns true, it is guaranteed that B != nullptr, C != nullptr.
429 return match(LHS, MatchASubBC()) && match(RHS, MatchCD());
430 };
431
432 if (!Match(Op0, Op1) && !Match(Op1, Op0))
433 return;
434
435 const auto ComputeKnownBitsOrOne = [&](const Value *V) {
436 // For some of the values we use the convention of leaving
437 // it nullptr to signify an implicit constant 1.
438 return V ? computeKnownBits(V, DemandedElts, Q, Depth + 1)
440 };
441
442 // Check that all operands are non-negative
443 const KnownBits KnownA = ComputeKnownBitsOrOne(A);
444 if (!KnownA.isNonNegative())
445 return;
446
447 const KnownBits KnownD = ComputeKnownBitsOrOne(D);
448 if (!KnownD.isNonNegative())
449 return;
450
451 const KnownBits KnownB = computeKnownBits(B, DemandedElts, Q, Depth + 1);
452 if (!KnownB.isNonNegative())
453 return;
454
455 const KnownBits KnownC = computeKnownBits(C, DemandedElts, Q, Depth + 1);
456 if (!KnownC.isNonNegative())
457 return;
458
459 // If we matched subtraction as xor, we need to actually check that xor
460 // is semantically equivalent to subtraction.
461 //
462 // For that to be true, b has to be a mask for c or that b's known
463 // ones cover all known and possible ones of c.
464 if (SubBC->getOpcode() == Instruction::Xor &&
465 !KnownC.getMaxValue().isSubsetOf(KnownB.getMinValue()))
466 return;
467
468 const APInt MaxA = KnownA.getMaxValue();
469 const APInt MaxD = KnownD.getMaxValue();
470 const APInt MaxAD = APIntOps::umax(MaxA, MaxD);
471 const APInt MaxB = KnownB.getMaxValue();
472
473 // We can't infer leading zeros info if the upper-bound estimate wraps.
474 bool Overflow;
475 const APInt UpperBound = MaxAD.umul_ov(MaxB, Overflow);
476
477 if (Overflow)
478 return;
479
480 // If we know that x <= y and both are positive than x has at least the same
481 // number of leading zeros as y.
482 const unsigned MinimumNumberOfLeadingZeros = UpperBound.countl_zero();
483 KnownOut.Zero.setHighBits(MinimumNumberOfLeadingZeros);
484}
485
486static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1,
487 bool NSW, bool NUW,
488 const APInt &DemandedElts,
489 KnownBits &KnownOut, KnownBits &Known2,
490 const SimplifyQuery &Q, unsigned Depth) {
491 computeKnownBits(Op1, DemandedElts, KnownOut, Q, Depth + 1);
492
493 // If one operand is unknown and we have no nowrap information,
494 // the result will be unknown independently of the second operand.
495 if (KnownOut.isUnknown() && !NSW && !NUW)
496 return;
497
498 computeKnownBits(Op0, DemandedElts, Known2, Q, Depth + 1);
499 KnownOut = KnownBits::computeForAddSub(Add, NSW, NUW, Known2, KnownOut);
500
501 if (!Add && NSW && !KnownOut.isNonNegative() &&
503 .value_or(false))
504 KnownOut.makeNonNegative();
505
506 if (Add)
507 // Try to match lerp pattern and combine results
508 computeKnownBitsFromLerpPattern(Op0, Op1, DemandedElts, KnownOut, Q, Depth);
509}
510
511static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,
512 bool NUW, const APInt &DemandedElts,
513 KnownBits &Known, KnownBits &Known2,
514 const SimplifyQuery &Q, unsigned Depth) {
515 computeKnownBits(Op1, DemandedElts, Known, Q, Depth + 1);
516 computeKnownBits(Op0, DemandedElts, Known2, Q, Depth + 1);
517
518 bool isKnownNegative = false;
519 bool isKnownNonNegative = false;
520 // If the multiplication is known not to overflow, compute the sign bit.
521 if (NSW) {
522 if (Op0 == Op1) {
523 // The product of a number with itself is non-negative.
524 isKnownNonNegative = true;
525 } else {
526 bool isKnownNonNegativeOp1 = Known.isNonNegative();
527 bool isKnownNonNegativeOp0 = Known2.isNonNegative();
528 bool isKnownNegativeOp1 = Known.isNegative();
529 bool isKnownNegativeOp0 = Known2.isNegative();
530 // The product of two numbers with the same sign is non-negative.
531 isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) ||
532 (isKnownNonNegativeOp1 && isKnownNonNegativeOp0);
533 if (!isKnownNonNegative && NUW) {
534 // mul nuw nsw with a factor > 1 is non-negative.
536 isKnownNonNegative = KnownBits::sgt(Known, One).value_or(false) ||
537 KnownBits::sgt(Known2, One).value_or(false);
538 }
539
540 // The product of a negative number and a non-negative number is either
541 // negative or zero.
544 (isKnownNegativeOp1 && isKnownNonNegativeOp0 &&
545 Known2.isNonZero()) ||
546 (isKnownNegativeOp0 && isKnownNonNegativeOp1 && Known.isNonZero());
547 }
548 }
549
550 bool SelfMultiply = Op0 == Op1;
551 if (SelfMultiply)
552 SelfMultiply &=
553 isGuaranteedNotToBeUndef(Op0, Q.AC, Q.CxtI, Q.DT, Depth + 1);
554 Known = KnownBits::mul(Known, Known2, SelfMultiply);
555
556 if (SelfMultiply) {
557 unsigned SignBits = ComputeNumSignBits(Op0, DemandedElts, Q, Depth + 1);
558 unsigned TyBits = Op0->getType()->getScalarSizeInBits();
559 unsigned OutValidBits = 2 * (TyBits - SignBits + 1);
560
561 if (OutValidBits < TyBits) {
562 APInt KnownZeroMask =
563 APInt::getHighBitsSet(TyBits, TyBits - OutValidBits + 1);
564 Known.Zero |= KnownZeroMask;
565 }
566 }
567
568 // Only make use of no-wrap flags if we failed to compute the sign bit
569 // directly. This matters if the multiplication always overflows, in
570 // which case we prefer to follow the result of the direct computation,
571 // though as the program is invoking undefined behaviour we can choose
572 // whatever we like here.
573 if (isKnownNonNegative && !Known.isNegative())
574 Known.makeNonNegative();
575 else if (isKnownNegative && !Known.isNonNegative())
576 Known.makeNegative();
577}
578
580 KnownBits &Known) {
581 unsigned BitWidth = Known.getBitWidth();
582 unsigned NumRanges = Ranges.getNumOperands() / 2;
583 assert(NumRanges >= 1);
584
585 Known.setAllConflict();
586
587 for (unsigned i = 0; i < NumRanges; ++i) {
589 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
591 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
592 ConstantRange Range(Lower->getValue(), Upper->getValue());
593 // BitWidth must equal the Ranges BitWidth for the correct number of high
594 // bits to be set.
595 assert(BitWidth == Range.getBitWidth() &&
596 "Known bit width must match range bit width!");
597
598 // The first CommonPrefixBits of all values in Range are equal.
599 unsigned CommonPrefixBits =
600 (Range.getUnsignedMax() ^ Range.getUnsignedMin()).countl_zero();
601 APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits);
602 APInt UnsignedMax = Range.getUnsignedMax().zextOrTrunc(BitWidth);
603 Known.One &= UnsignedMax & Mask;
604 Known.Zero &= ~UnsignedMax & Mask;
605 }
606}
607
608static bool isEphemeralValueOf(const Instruction *I, const Value *E) {
612
613 // The instruction defining an assumption's condition itself is always
614 // considered ephemeral to that assumption (even if it has other
615 // non-ephemeral users). See r246696's test case for an example.
616 if (is_contained(I->operands(), E))
617 return true;
618
619 while (!WorkSet.empty()) {
620 const Instruction *V = WorkSet.pop_back_val();
621 if (!Visited.insert(V).second)
622 continue;
623
624 // If all uses of this value are ephemeral, then so is this value.
625 if (all_of(V->users(), [&](const User *U) {
626 return EphValues.count(cast<Instruction>(U));
627 })) {
628 if (V == E)
629 return true;
630
631 if (V == I || (!V->mayHaveSideEffects() && !V->isTerminator())) {
632 EphValues.insert(V);
633
634 if (const User *U = dyn_cast<User>(V)) {
635 for (const Use &U : U->operands()) {
636 if (const auto *I = dyn_cast<Instruction>(U.get()))
637 WorkSet.push_back(I);
638 }
639 }
640 }
641 }
642 }
643
644 return false;
645}
646
647// Is this an intrinsic that cannot be speculated but also cannot trap?
649 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(I))
650 return CI->isAssumeLikeIntrinsic();
651
652 return false;
653}
654
656 const Instruction *CxtI,
657 const DominatorTree *DT,
658 bool AllowEphemerals) {
659 // There are two restrictions on the use of an assume:
660 // 1. The assume must dominate the context (or the control flow must
661 // reach the assume whenever it reaches the context).
662 // 2. The context must not be in the assume's set of ephemeral values
663 // (otherwise we will use the assume to prove that the condition
664 // feeding the assume is trivially true, thus causing the removal of
665 // the assume).
666
667 if (Inv->getParent() == CxtI->getParent()) {
668 // If Inv and CtxI are in the same block, check if the assume (Inv) is first
669 // in the BB.
670 if (Inv->comesBefore(CxtI))
671 return true;
672
673 // Don't let an assume affect itself - this would cause the problems
674 // `isEphemeralValueOf` is trying to prevent, and it would also make
675 // the loop below go out of bounds.
676 if (!AllowEphemerals && Inv == CxtI)
677 return false;
678
679 // The context comes first, but they're both in the same block.
680 // Make sure there is nothing in between that might interrupt
681 // the control flow, not even CxtI itself.
682 // We limit the scan distance between the assume and its context instruction
683 // to avoid a compile-time explosion. This limit is chosen arbitrarily, so
684 // it can be adjusted if needed (could be turned into a cl::opt).
685 auto Range = make_range(CxtI->getIterator(), Inv->getIterator());
687 return false;
688
689 return AllowEphemerals || !isEphemeralValueOf(Inv, CxtI);
690 }
691
692 // Inv and CxtI are in different blocks.
693 if (DT) {
694 if (DT->dominates(Inv, CxtI))
695 return true;
696 } else if (Inv->getParent() == CxtI->getParent()->getSinglePredecessor() ||
697 Inv->getParent()->isEntryBlock()) {
698 // We don't have a DT, but this trivially dominates.
699 return true;
700 }
701
702 return false;
703}
704
706 const Instruction *CtxI) {
707 // Helper to check if there are any calls in the range that may free memory.
708 auto hasNoFreeCalls = [](auto Range) {
709 for (const auto &[Idx, I] : enumerate(Range)) {
710 if (Idx > MaxInstrsToCheckForFree)
711 return false;
712 if (const auto *CB = dyn_cast<CallBase>(&I))
713 if (!CB->hasFnAttr(Attribute::NoFree))
714 return false;
715 }
716 return true;
717 };
718
719 // Make sure the current function cannot arrange for another thread to free on
720 // its behalf.
721 if (!CtxI->getFunction()->hasNoSync())
722 return false;
723
724 // Handle cross-block case: CtxI in a successor of Assume's block.
725 const BasicBlock *CtxBB = CtxI->getParent();
726 const BasicBlock *AssumeBB = Assume->getParent();
727 BasicBlock::const_iterator CtxIter = CtxI->getIterator();
728 if (CtxBB != AssumeBB) {
729 if (CtxBB->getSinglePredecessor() != AssumeBB)
730 return false;
731
732 if (!hasNoFreeCalls(make_range(CtxBB->begin(), CtxIter)))
733 return false;
734
735 CtxIter = AssumeBB->end();
736 } else {
737 // Same block case: check that Assume comes before CtxI.
738 if (!Assume->comesBefore(CtxI))
739 return false;
740 }
741
742 // Check if there are any calls between Assume and CtxIter that may free
743 // memory.
744 return hasNoFreeCalls(make_range(Assume->getIterator(), CtxIter));
745}
746
747// TODO: cmpExcludesZero misses many cases where `RHS` is non-constant but
748// we still have enough information about `RHS` to conclude non-zero. For
749// example Pred=EQ, RHS=isKnownNonZero. cmpExcludesZero is called in loops
750// so the extra compile time may not be worth it, but possibly a second API
751// should be created for use outside of loops.
752static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS) {
753 // v u> y implies v != 0.
754 if (Pred == ICmpInst::ICMP_UGT)
755 return true;
756
757 // Special-case v != 0 to also handle v != null.
758 if (Pred == ICmpInst::ICMP_NE)
759 return match(RHS, m_Zero());
760
761 // All other predicates - rely on generic ConstantRange handling.
762 const APInt *C;
763 auto Zero = APInt::getZero(RHS->getType()->getScalarSizeInBits());
764 if (match(RHS, m_APInt(C))) {
766 return !TrueValues.contains(Zero);
767 }
768
770 if (VC == nullptr)
771 return false;
772
773 for (unsigned ElemIdx = 0, NElem = VC->getNumElements(); ElemIdx < NElem;
774 ++ElemIdx) {
776 Pred, VC->getElementAsAPInt(ElemIdx));
777 if (TrueValues.contains(Zero))
778 return false;
779 }
780 return true;
781}
782
783static void breakSelfRecursivePHI(const Use *U, const PHINode *PHI,
784 Value *&ValOut, Instruction *&CtxIOut,
785 const PHINode **PhiOut = nullptr) {
786 ValOut = U->get();
787 if (ValOut == PHI)
788 return;
789 CtxIOut = PHI->getIncomingBlock(*U)->getTerminator();
790 if (PhiOut)
791 *PhiOut = PHI;
792 Value *V;
793 // If the Use is a select of this phi, compute analysis on other arm to break
794 // recursion.
795 // TODO: Min/Max
796 if (match(ValOut, m_Select(m_Value(), m_Specific(PHI), m_Value(V))) ||
797 match(ValOut, m_Select(m_Value(), m_Value(V), m_Specific(PHI))))
798 ValOut = V;
799
800 // Same for select, if this phi is 2-operand phi, compute analysis on other
801 // incoming value to break recursion.
802 // TODO: We could handle any number of incoming edges as long as we only have
803 // two unique values.
804 if (auto *IncPhi = dyn_cast<PHINode>(ValOut);
805 IncPhi && IncPhi->getNumIncomingValues() == 2) {
806 for (int Idx = 0; Idx < 2; ++Idx) {
807 if (IncPhi->getIncomingValue(Idx) == PHI) {
808 ValOut = IncPhi->getIncomingValue(1 - Idx);
809 if (PhiOut)
810 *PhiOut = IncPhi;
811 CtxIOut = IncPhi->getIncomingBlock(1 - Idx)->getTerminator();
812 break;
813 }
814 }
815 }
816}
817
818static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q) {
819 // Use of assumptions is context-sensitive. If we don't have a context, we
820 // cannot use them!
821 if (!Q.AC || !Q.CxtI)
822 return false;
823
824 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
825 if (!Elem.Assume)
826 continue;
827
828 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
829 assert(I->getFunction() == Q.CxtI->getFunction() &&
830 "Got assumption for the wrong function!");
831
832 if (Elem.Index != AssumptionCache::ExprResultIdx) {
833 if (!V->getType()->isPointerTy())
834 continue;
836 *I, I->bundle_op_info_begin()[Elem.Index])) {
837 if (RK.WasOn == V &&
838 (RK.AttrKind == Attribute::NonNull ||
839 (RK.AttrKind == Attribute::Dereferenceable &&
841 V->getType()->getPointerAddressSpace()))) &&
843 return true;
844 }
845 continue;
846 }
847
848 // Warning: This loop can end up being somewhat performance sensitive.
849 // We're running this loop for once for each value queried resulting in a
850 // runtime of ~O(#assumes * #values).
851
852 Value *RHS;
853 CmpPredicate Pred;
854 auto m_V = m_CombineOr(m_Specific(V), m_PtrToInt(m_Specific(V)));
855 if (!match(I->getArgOperand(0), m_c_ICmp(Pred, m_V, m_Value(RHS))))
856 continue;
857
858 if (cmpExcludesZero(Pred, RHS) && isValidAssumeForContext(I, Q.CxtI, Q.DT))
859 return true;
860 }
861
862 return false;
863}
864
866 Value *LHS, Value *RHS, KnownBits &Known,
867 const SimplifyQuery &Q) {
868 if (RHS->getType()->isPointerTy()) {
869 // Handle comparison of pointer to null explicitly, as it will not be
870 // covered by the m_APInt() logic below.
871 if (LHS == V && match(RHS, m_Zero())) {
872 switch (Pred) {
874 Known.setAllZero();
875 break;
878 Known.makeNonNegative();
879 break;
881 Known.makeNegative();
882 break;
883 default:
884 break;
885 }
886 }
887 return;
888 }
889
890 unsigned BitWidth = Known.getBitWidth();
891 auto m_V =
893
894 Value *Y;
895 const APInt *Mask, *C;
896 if (!match(RHS, m_APInt(C)))
897 return;
898
899 uint64_t ShAmt;
900 switch (Pred) {
902 // assume(V = C)
903 if (match(LHS, m_V)) {
904 Known = Known.unionWith(KnownBits::makeConstant(*C));
905 // assume(V & Mask = C)
906 } else if (match(LHS, m_c_And(m_V, m_Value(Y)))) {
907 // For one bits in Mask, we can propagate bits from C to V.
908 Known.One |= *C;
909 if (match(Y, m_APInt(Mask)))
910 Known.Zero |= ~*C & *Mask;
911 // assume(V | Mask = C)
912 } else if (match(LHS, m_c_Or(m_V, m_Value(Y)))) {
913 // For zero bits in Mask, we can propagate bits from C to V.
914 Known.Zero |= ~*C;
915 if (match(Y, m_APInt(Mask)))
916 Known.One |= *C & ~*Mask;
917 // assume(V << ShAmt = C)
918 } else if (match(LHS, m_Shl(m_V, m_ConstantInt(ShAmt))) &&
919 ShAmt < BitWidth) {
920 // For those bits in C that are known, we can propagate them to known
921 // bits in V shifted to the right by ShAmt.
923 RHSKnown >>= ShAmt;
924 Known = Known.unionWith(RHSKnown);
925 // assume(V >> ShAmt = C)
926 } else if (match(LHS, m_Shr(m_V, m_ConstantInt(ShAmt))) &&
927 ShAmt < BitWidth) {
928 // For those bits in RHS that are known, we can propagate them to known
929 // bits in V shifted to the right by C.
931 RHSKnown <<= ShAmt;
932 Known = Known.unionWith(RHSKnown);
933 }
934 break;
935 case ICmpInst::ICMP_NE: {
936 // assume (V & B != 0) where B is a power of 2
937 const APInt *BPow2;
938 if (C->isZero() && match(LHS, m_And(m_V, m_Power2(BPow2))))
939 Known.One |= *BPow2;
940 break;
941 }
942 default: {
943 const APInt *Offset = nullptr;
944 if (match(LHS, m_CombineOr(m_V, m_AddLike(m_V, m_APInt(Offset))))) {
946 if (Offset)
947 LHSRange = LHSRange.sub(*Offset);
948 Known = Known.unionWith(LHSRange.toKnownBits());
949 }
950 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
951 // X & Y u> C -> X u> C && Y u> C
952 // X nuw- Y u> C -> X u> C
953 if (match(LHS, m_c_And(m_V, m_Value())) ||
954 match(LHS, m_NUWSub(m_V, m_Value())))
955 Known.One.setHighBits(
956 (*C + (Pred == ICmpInst::ICMP_UGT)).countLeadingOnes());
957 }
958 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
959 // X | Y u< C -> X u< C && Y u< C
960 // X nuw+ Y u< C -> X u< C && Y u< C
961 if (match(LHS, m_c_Or(m_V, m_Value())) ||
962 match(LHS, m_c_NUWAdd(m_V, m_Value()))) {
963 Known.Zero.setHighBits(
964 (*C - (Pred == ICmpInst::ICMP_ULT)).countLeadingZeros());
965 }
966 }
967 } break;
968 }
969}
970
971static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp,
972 KnownBits &Known,
973 const SimplifyQuery &SQ, bool Invert) {
975 Invert ? Cmp->getInversePredicate() : Cmp->getPredicate();
976 Value *LHS = Cmp->getOperand(0);
977 Value *RHS = Cmp->getOperand(1);
978
979 // Handle icmp pred (trunc V), C
980 if (match(LHS, m_Trunc(m_Specific(V)))) {
981 KnownBits DstKnown(LHS->getType()->getScalarSizeInBits());
982 computeKnownBitsFromCmp(LHS, Pred, LHS, RHS, DstKnown, SQ);
984 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
985 else
986 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
987 return;
988 }
989
990 computeKnownBitsFromCmp(V, Pred, LHS, RHS, Known, SQ);
991}
992
994 KnownBits &Known, const SimplifyQuery &SQ,
995 bool Invert, unsigned Depth) {
996 Value *A, *B;
999 KnownBits Known2(Known.getBitWidth());
1000 KnownBits Known3(Known.getBitWidth());
1001 computeKnownBitsFromCond(V, A, Known2, SQ, Invert, Depth + 1);
1002 computeKnownBitsFromCond(V, B, Known3, SQ, Invert, Depth + 1);
1003 if (Invert ? match(Cond, m_LogicalOr(m_Value(), m_Value()))
1005 Known2 = Known2.unionWith(Known3);
1006 else
1007 Known2 = Known2.intersectWith(Known3);
1008 Known = Known.unionWith(Known2);
1009 return;
1010 }
1011
1012 if (auto *Cmp = dyn_cast<ICmpInst>(Cond)) {
1013 computeKnownBitsFromICmpCond(V, Cmp, Known, SQ, Invert);
1014 return;
1015 }
1016
1017 if (match(Cond, m_Trunc(m_Specific(V)))) {
1018 KnownBits DstKnown(1);
1019 if (Invert) {
1020 DstKnown.setAllZero();
1021 } else {
1022 DstKnown.setAllOnes();
1023 }
1025 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
1026 return;
1027 }
1028 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
1029 return;
1030 }
1031
1033 computeKnownBitsFromCond(V, A, Known, SQ, !Invert, Depth + 1);
1034}
1035
1037 const SimplifyQuery &Q, unsigned Depth) {
1038 // Handle injected condition.
1039 if (Q.CC && Q.CC->AffectedValues.contains(V))
1040 computeKnownBitsFromCond(V, Q.CC->Cond, Known, Q, Q.CC->Invert, Depth);
1041
1042 if (!Q.CxtI)
1043 return;
1044
1045 if (Q.DC && Q.DT) {
1046 // Handle dominating conditions.
1047 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
1048 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
1049 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
1050 computeKnownBitsFromCond(V, BI->getCondition(), Known, Q,
1051 /*Invert*/ false, Depth);
1052
1053 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
1054 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
1055 computeKnownBitsFromCond(V, BI->getCondition(), Known, Q,
1056 /*Invert*/ true, Depth);
1057 }
1058
1059 if (Known.hasConflict())
1060 Known.resetAll();
1061 }
1062
1063 if (!Q.AC)
1064 return;
1065
1066 unsigned BitWidth = Known.getBitWidth();
1067
1068 // Note that the patterns below need to be kept in sync with the code
1069 // in AssumptionCache::updateAffectedValues.
1070
1071 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
1072 if (!Elem.Assume)
1073 continue;
1074
1075 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
1076 assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() &&
1077 "Got assumption for the wrong function!");
1078
1079 if (Elem.Index != AssumptionCache::ExprResultIdx) {
1080 if (!V->getType()->isPointerTy())
1081 continue;
1083 *I, I->bundle_op_info_begin()[Elem.Index])) {
1084 // Allow AllowEphemerals in isValidAssumeForContext, as the CxtI might
1085 // be the producer of the pointer in the bundle. At the moment, align
1086 // assumptions aren't optimized away.
1087 if (RK.WasOn == V && RK.AttrKind == Attribute::Alignment &&
1088 isPowerOf2_64(RK.ArgValue) &&
1089 isValidAssumeForContext(I, Q.CxtI, Q.DT, /*AllowEphemerals*/ true))
1090 Known.Zero.setLowBits(Log2_64(RK.ArgValue));
1091 }
1092 continue;
1093 }
1094
1095 // Warning: This loop can end up being somewhat performance sensitive.
1096 // We're running this loop for once for each value queried resulting in a
1097 // runtime of ~O(#assumes * #values).
1098
1099 Value *Arg = I->getArgOperand(0);
1100
1101 if (Arg == V && isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
1102 assert(BitWidth == 1 && "assume operand is not i1?");
1103 (void)BitWidth;
1104 Known.setAllOnes();
1105 return;
1106 }
1107 if (match(Arg, m_Not(m_Specific(V))) &&
1109 assert(BitWidth == 1 && "assume operand is not i1?");
1110 (void)BitWidth;
1111 Known.setAllZero();
1112 return;
1113 }
1114 auto *Trunc = dyn_cast<TruncInst>(Arg);
1115 if (Trunc && Trunc->getOperand(0) == V &&
1117 if (Trunc->hasNoUnsignedWrap()) {
1119 return;
1120 }
1121 Known.One.setBit(0);
1122 return;
1123 }
1124
1125 // The remaining tests are all recursive, so bail out if we hit the limit.
1127 continue;
1128
1129 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
1130 if (!Cmp)
1131 continue;
1132
1133 if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
1134 continue;
1135
1136 computeKnownBitsFromICmpCond(V, Cmp, Known, Q, /*Invert=*/false);
1137 }
1138
1139 // Conflicting assumption: Undefined behavior will occur on this execution
1140 // path.
1141 if (Known.hasConflict())
1142 Known.resetAll();
1143}
1144
1145/// Compute known bits from a shift operator, including those with a
1146/// non-constant shift amount. Known is the output of this function. Known2 is a
1147/// pre-allocated temporary with the same bit width as Known and on return
1148/// contains the known bit of the shift value source. KF is an
1149/// operator-specific function that, given the known-bits and a shift amount,
1150/// compute the implied known-bits of the shift operator's result respectively
1151/// for that shift amount. The results from calling KF are conservatively
1152/// combined for all permitted shift amounts.
1154 const Operator *I, const APInt &DemandedElts, KnownBits &Known,
1155 KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth,
1156 function_ref<KnownBits(const KnownBits &, const KnownBits &, bool)> KF) {
1157 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1158 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1159 // To limit compile-time impact, only query isKnownNonZero() if we know at
1160 // least something about the shift amount.
1161 bool ShAmtNonZero =
1162 Known.isNonZero() ||
1163 (Known.getMaxValue().ult(Known.getBitWidth()) &&
1164 isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth + 1));
1165 Known = KF(Known2, Known, ShAmtNonZero);
1166}
1167
1168static KnownBits
1169getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts,
1170 const KnownBits &KnownLHS, const KnownBits &KnownRHS,
1171 const SimplifyQuery &Q, unsigned Depth) {
1172 unsigned BitWidth = KnownLHS.getBitWidth();
1173 KnownBits KnownOut(BitWidth);
1174 bool IsAnd = false;
1175 bool HasKnownOne = !KnownLHS.One.isZero() || !KnownRHS.One.isZero();
1176 Value *X = nullptr, *Y = nullptr;
1177
1178 switch (I->getOpcode()) {
1179 case Instruction::And:
1180 KnownOut = KnownLHS & KnownRHS;
1181 IsAnd = true;
1182 // and(x, -x) is common idioms that will clear all but lowest set
1183 // bit. If we have a single known bit in x, we can clear all bits
1184 // above it.
1185 // TODO: instcombine often reassociates independent `and` which can hide
1186 // this pattern. Try to match and(x, and(-x, y)) / and(and(x, y), -x).
1187 if (HasKnownOne && match(I, m_c_And(m_Value(X), m_Neg(m_Deferred(X))))) {
1188 // -(-x) == x so using whichever (LHS/RHS) gets us a better result.
1189 if (KnownLHS.countMaxTrailingZeros() <= KnownRHS.countMaxTrailingZeros())
1190 KnownOut = KnownLHS.blsi();
1191 else
1192 KnownOut = KnownRHS.blsi();
1193 }
1194 break;
1195 case Instruction::Or:
1196 KnownOut = KnownLHS | KnownRHS;
1197 break;
1198 case Instruction::Xor:
1199 KnownOut = KnownLHS ^ KnownRHS;
1200 // xor(x, x-1) is common idioms that will clear all but lowest set
1201 // bit. If we have a single known bit in x, we can clear all bits
1202 // above it.
1203 // TODO: xor(x, x-1) is often rewritting as xor(x, x-C) where C !=
1204 // -1 but for the purpose of demanded bits (xor(x, x-C) &
1205 // Demanded) == (xor(x, x-1) & Demanded). Extend the xor pattern
1206 // to use arbitrary C if xor(x, x-C) as the same as xor(x, x-1).
1207 if (HasKnownOne &&
1209 const KnownBits &XBits = I->getOperand(0) == X ? KnownLHS : KnownRHS;
1210 KnownOut = XBits.blsmsk();
1211 }
1212 break;
1213 default:
1214 llvm_unreachable("Invalid Op used in 'analyzeKnownBitsFromAndXorOr'");
1215 }
1216
1217 // and(x, add (x, -1)) is a common idiom that always clears the low bit;
1218 // xor/or(x, add (x, -1)) is an idiom that will always set the low bit.
1219 // here we handle the more general case of adding any odd number by
1220 // matching the form and/xor/or(x, add(x, y)) where y is odd.
1221 // TODO: This could be generalized to clearing any bit set in y where the
1222 // following bit is known to be unset in y.
1223 if (!KnownOut.Zero[0] && !KnownOut.One[0] &&
1227 KnownBits KnownY(BitWidth);
1228 computeKnownBits(Y, DemandedElts, KnownY, Q, Depth + 1);
1229 if (KnownY.countMinTrailingOnes() > 0) {
1230 if (IsAnd)
1231 KnownOut.Zero.setBit(0);
1232 else
1233 KnownOut.One.setBit(0);
1234 }
1235 }
1236 return KnownOut;
1237}
1238
1240 const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q,
1241 unsigned Depth,
1242 const function_ref<KnownBits(const KnownBits &, const KnownBits &)>
1243 KnownBitsFunc) {
1244 APInt DemandedEltsLHS, DemandedEltsRHS;
1246 DemandedElts, DemandedEltsLHS,
1247 DemandedEltsRHS);
1248
1249 const auto ComputeForSingleOpFunc =
1250 [Depth, &Q, KnownBitsFunc](const Value *Op, APInt &DemandedEltsOp) {
1251 return KnownBitsFunc(
1252 computeKnownBits(Op, DemandedEltsOp, Q, Depth + 1),
1253 computeKnownBits(Op, DemandedEltsOp << 1, Q, Depth + 1));
1254 };
1255
1256 if (DemandedEltsRHS.isZero())
1257 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS);
1258 if (DemandedEltsLHS.isZero())
1259 return ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS);
1260
1261 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS)
1262 .intersectWith(ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS));
1263}
1264
1265// Public so this can be used in `SimplifyDemandedUseBits`.
1267 const KnownBits &KnownLHS,
1268 const KnownBits &KnownRHS,
1269 const SimplifyQuery &SQ,
1270 unsigned Depth) {
1271 auto *FVTy = dyn_cast<FixedVectorType>(I->getType());
1272 APInt DemandedElts =
1273 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
1274
1275 return getKnownBitsFromAndXorOr(I, DemandedElts, KnownLHS, KnownRHS, SQ,
1276 Depth);
1277}
1278
1280 Attribute Attr = F->getFnAttribute(Attribute::VScaleRange);
1281 // Without vscale_range, we only know that vscale is non-zero.
1282 if (!Attr.isValid())
1284
1285 unsigned AttrMin = Attr.getVScaleRangeMin();
1286 // Minimum is larger than vscale width, result is always poison.
1287 if ((unsigned)llvm::bit_width(AttrMin) > BitWidth)
1288 return ConstantRange::getEmpty(BitWidth);
1289
1290 APInt Min(BitWidth, AttrMin);
1291 std::optional<unsigned> AttrMax = Attr.getVScaleRangeMax();
1292 if (!AttrMax || (unsigned)llvm::bit_width(*AttrMax) > BitWidth)
1294
1295 return ConstantRange(Min, APInt(BitWidth, *AttrMax) + 1);
1296}
1297
1299 Value *Arm, bool Invert,
1300 const SimplifyQuery &Q, unsigned Depth) {
1301 // If we have a constant arm, we are done.
1302 if (Known.isConstant())
1303 return;
1304
1305 // See what condition implies about the bits of the select arm.
1306 KnownBits CondRes(Known.getBitWidth());
1307 computeKnownBitsFromCond(Arm, Cond, CondRes, Q, Invert, Depth + 1);
1308 // If we don't get any information from the condition, no reason to
1309 // proceed.
1310 if (CondRes.isUnknown())
1311 return;
1312
1313 // We can have conflict if the condition is dead. I.e if we have
1314 // (x | 64) < 32 ? (x | 64) : y
1315 // we will have conflict at bit 6 from the condition/the `or`.
1316 // In that case just return. Its not particularly important
1317 // what we do, as this select is going to be simplified soon.
1318 CondRes = CondRes.unionWith(Known);
1319 if (CondRes.hasConflict())
1320 return;
1321
1322 // Finally make sure the information we found is valid. This is relatively
1323 // expensive so it's left for the very end.
1324 if (!isGuaranteedNotToBeUndef(Arm, Q.AC, Q.CxtI, Q.DT, Depth + 1))
1325 return;
1326
1327 // Finally, we know we get information from the condition and its valid,
1328 // so return it.
1329 Known = CondRes;
1330}
1331
1332// Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow).
1333// Returns the input and lower/upper bounds.
1334static bool isSignedMinMaxClamp(const Value *Select, const Value *&In,
1335 const APInt *&CLow, const APInt *&CHigh) {
1337 cast<Operator>(Select)->getOpcode() == Instruction::Select &&
1338 "Input should be a Select!");
1339
1340 const Value *LHS = nullptr, *RHS = nullptr;
1342 if (SPF != SPF_SMAX && SPF != SPF_SMIN)
1343 return false;
1344
1345 if (!match(RHS, m_APInt(CLow)))
1346 return false;
1347
1348 const Value *LHS2 = nullptr, *RHS2 = nullptr;
1350 if (getInverseMinMaxFlavor(SPF) != SPF2)
1351 return false;
1352
1353 if (!match(RHS2, m_APInt(CHigh)))
1354 return false;
1355
1356 if (SPF == SPF_SMIN)
1357 std::swap(CLow, CHigh);
1358
1359 In = LHS2;
1360 return CLow->sle(*CHigh);
1361}
1362
1364 const APInt *&CLow,
1365 const APInt *&CHigh) {
1366 assert((II->getIntrinsicID() == Intrinsic::smin ||
1367 II->getIntrinsicID() == Intrinsic::smax) &&
1368 "Must be smin/smax");
1369
1370 Intrinsic::ID InverseID = getInverseMinMaxIntrinsic(II->getIntrinsicID());
1371 auto *InnerII = dyn_cast<IntrinsicInst>(II->getArgOperand(0));
1372 if (!InnerII || InnerII->getIntrinsicID() != InverseID ||
1373 !match(II->getArgOperand(1), m_APInt(CLow)) ||
1374 !match(InnerII->getArgOperand(1), m_APInt(CHigh)))
1375 return false;
1376
1377 if (II->getIntrinsicID() == Intrinsic::smin)
1378 std::swap(CLow, CHigh);
1379 return CLow->sle(*CHigh);
1380}
1381
1383 KnownBits &Known) {
1384 const APInt *CLow, *CHigh;
1385 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
1386 Known = Known.unionWith(
1387 ConstantRange::getNonEmpty(*CLow, *CHigh + 1).toKnownBits());
1388}
1389
1391 const APInt &DemandedElts,
1392 KnownBits &Known,
1393 const SimplifyQuery &Q,
1394 unsigned Depth) {
1395 unsigned BitWidth = Known.getBitWidth();
1396
1397 KnownBits Known2(BitWidth);
1398 switch (I->getOpcode()) {
1399 default: break;
1400 case Instruction::Load:
1401 if (MDNode *MD =
1402 Q.IIQ.getMetadata(cast<LoadInst>(I), LLVMContext::MD_range))
1404 break;
1405 case Instruction::And:
1406 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1407 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1408
1409 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1410 break;
1411 case Instruction::Or:
1412 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1413 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1414
1415 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1416 break;
1417 case Instruction::Xor:
1418 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1419 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1420
1421 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1422 break;
1423 case Instruction::Mul: {
1426 computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, NUW,
1427 DemandedElts, Known, Known2, Q, Depth);
1428 break;
1429 }
1430 case Instruction::UDiv: {
1431 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1432 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1433 Known =
1434 KnownBits::udiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1435 break;
1436 }
1437 case Instruction::SDiv: {
1438 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1439 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1440 Known =
1441 KnownBits::sdiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1442 break;
1443 }
1444 case Instruction::Select: {
1445 auto ComputeForArm = [&](Value *Arm, bool Invert) {
1446 KnownBits Res(Known.getBitWidth());
1447 computeKnownBits(Arm, DemandedElts, Res, Q, Depth + 1);
1448 adjustKnownBitsForSelectArm(Res, I->getOperand(0), Arm, Invert, Q, Depth);
1449 return Res;
1450 };
1451 // Only known if known in both the LHS and RHS.
1452 Known =
1453 ComputeForArm(I->getOperand(1), /*Invert=*/false)
1454 .intersectWith(ComputeForArm(I->getOperand(2), /*Invert=*/true));
1455 break;
1456 }
1457 case Instruction::FPTrunc:
1458 case Instruction::FPExt:
1459 case Instruction::FPToUI:
1460 case Instruction::FPToSI:
1461 case Instruction::SIToFP:
1462 case Instruction::UIToFP:
1463 break; // Can't work with floating point.
1464 case Instruction::PtrToInt:
1465 case Instruction::IntToPtr:
1466 // Fall through and handle them the same as zext/trunc.
1467 [[fallthrough]];
1468 case Instruction::ZExt:
1469 case Instruction::Trunc: {
1470 Type *SrcTy = I->getOperand(0)->getType();
1471
1472 unsigned SrcBitWidth;
1473 // Note that we handle pointer operands here because of inttoptr/ptrtoint
1474 // which fall through here.
1475 Type *ScalarTy = SrcTy->getScalarType();
1476 SrcBitWidth = ScalarTy->isPointerTy() ?
1477 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
1478 Q.DL.getTypeSizeInBits(ScalarTy);
1479
1480 assert(SrcBitWidth && "SrcBitWidth can't be zero");
1481 Known = Known.anyextOrTrunc(SrcBitWidth);
1482 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1483 if (auto *Inst = dyn_cast<PossiblyNonNegInst>(I);
1484 Inst && Inst->hasNonNeg() && !Known.isNegative())
1485 Known.makeNonNegative();
1486 Known = Known.zextOrTrunc(BitWidth);
1487 break;
1488 }
1489 case Instruction::BitCast: {
1490 Type *SrcTy = I->getOperand(0)->getType();
1491 if (SrcTy->isIntOrPtrTy() &&
1492 // TODO: For now, not handling conversions like:
1493 // (bitcast i64 %x to <2 x i32>)
1494 !I->getType()->isVectorTy()) {
1495 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1496 break;
1497 }
1498
1499 const Value *V;
1500 // Handle bitcast from floating point to integer.
1501 if (match(I, m_ElementWiseBitCast(m_Value(V))) &&
1502 V->getType()->isFPOrFPVectorTy()) {
1503 Type *FPType = V->getType()->getScalarType();
1504 KnownFPClass Result =
1505 computeKnownFPClass(V, DemandedElts, fcAllFlags, Q, Depth + 1);
1506 FPClassTest FPClasses = Result.KnownFPClasses;
1507
1508 // TODO: Treat it as zero/poison if the use of I is unreachable.
1509 if (FPClasses == fcNone)
1510 break;
1511
1512 if (Result.isKnownNever(fcNormal | fcSubnormal | fcNan)) {
1513 Known.setAllConflict();
1514
1515 if (FPClasses & fcInf)
1517 APFloat::getInf(FPType->getFltSemantics()).bitcastToAPInt()));
1518
1519 if (FPClasses & fcZero)
1521 APInt::getZero(FPType->getScalarSizeInBits())));
1522
1523 Known.Zero.clearSignBit();
1524 Known.One.clearSignBit();
1525 }
1526
1527 if (Result.SignBit) {
1528 if (*Result.SignBit)
1529 Known.makeNegative();
1530 else
1531 Known.makeNonNegative();
1532 }
1533
1534 break;
1535 }
1536
1537 // Handle cast from vector integer type to scalar or vector integer.
1538 auto *SrcVecTy = dyn_cast<FixedVectorType>(SrcTy);
1539 if (!SrcVecTy || !SrcVecTy->getElementType()->isIntegerTy() ||
1540 !I->getType()->isIntOrIntVectorTy() ||
1541 isa<ScalableVectorType>(I->getType()))
1542 break;
1543
1544 unsigned NumElts = DemandedElts.getBitWidth();
1545 bool IsLE = Q.DL.isLittleEndian();
1546 // Look through a cast from narrow vector elements to wider type.
1547 // Examples: v4i32 -> v2i64, v3i8 -> v24
1548 unsigned SubBitWidth = SrcVecTy->getScalarSizeInBits();
1549 if (BitWidth % SubBitWidth == 0) {
1550 // Known bits are automatically intersected across demanded elements of a
1551 // vector. So for example, if a bit is computed as known zero, it must be
1552 // zero across all demanded elements of the vector.
1553 //
1554 // For this bitcast, each demanded element of the output is sub-divided
1555 // across a set of smaller vector elements in the source vector. To get
1556 // the known bits for an entire element of the output, compute the known
1557 // bits for each sub-element sequentially. This is done by shifting the
1558 // one-set-bit demanded elements parameter across the sub-elements for
1559 // consecutive calls to computeKnownBits. We are using the demanded
1560 // elements parameter as a mask operator.
1561 //
1562 // The known bits of each sub-element are then inserted into place
1563 // (dependent on endian) to form the full result of known bits.
1564 unsigned SubScale = BitWidth / SubBitWidth;
1565 APInt SubDemandedElts = APInt::getZero(NumElts * SubScale);
1566 for (unsigned i = 0; i != NumElts; ++i) {
1567 if (DemandedElts[i])
1568 SubDemandedElts.setBit(i * SubScale);
1569 }
1570
1571 KnownBits KnownSrc(SubBitWidth);
1572 for (unsigned i = 0; i != SubScale; ++i) {
1573 computeKnownBits(I->getOperand(0), SubDemandedElts.shl(i), KnownSrc, Q,
1574 Depth + 1);
1575 unsigned ShiftElt = IsLE ? i : SubScale - 1 - i;
1576 Known.insertBits(KnownSrc, ShiftElt * SubBitWidth);
1577 }
1578 }
1579 // Look through a cast from wider vector elements to narrow type.
1580 // Examples: v2i64 -> v4i32
1581 if (SubBitWidth % BitWidth == 0) {
1582 unsigned SubScale = SubBitWidth / BitWidth;
1583 KnownBits KnownSrc(SubBitWidth);
1584 APInt SubDemandedElts =
1585 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
1586 computeKnownBits(I->getOperand(0), SubDemandedElts, KnownSrc, Q,
1587 Depth + 1);
1588
1589 Known.setAllConflict();
1590 for (unsigned i = 0; i != NumElts; ++i) {
1591 if (DemandedElts[i]) {
1592 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
1593 unsigned Offset = (Shifts % SubScale) * BitWidth;
1594 Known = Known.intersectWith(KnownSrc.extractBits(BitWidth, Offset));
1595 if (Known.isUnknown())
1596 break;
1597 }
1598 }
1599 }
1600 break;
1601 }
1602 case Instruction::SExt: {
1603 // Compute the bits in the result that are not present in the input.
1604 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
1605
1606 Known = Known.trunc(SrcBitWidth);
1607 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1608 // If the sign bit of the input is known set or clear, then we know the
1609 // top bits of the result.
1610 Known = Known.sext(BitWidth);
1611 break;
1612 }
1613 case Instruction::Shl: {
1616 auto KF = [NUW, NSW](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1617 bool ShAmtNonZero) {
1618 return KnownBits::shl(KnownVal, KnownAmt, NUW, NSW, ShAmtNonZero);
1619 };
1620 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1621 KF);
1622 // Trailing zeros of a right-shifted constant never decrease.
1623 const APInt *C;
1624 if (match(I->getOperand(0), m_APInt(C)))
1625 Known.Zero.setLowBits(C->countr_zero());
1626 break;
1627 }
1628 case Instruction::LShr: {
1629 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1630 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1631 bool ShAmtNonZero) {
1632 return KnownBits::lshr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1633 };
1634 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1635 KF);
1636 // Leading zeros of a left-shifted constant never decrease.
1637 const APInt *C;
1638 if (match(I->getOperand(0), m_APInt(C)))
1639 Known.Zero.setHighBits(C->countl_zero());
1640 break;
1641 }
1642 case Instruction::AShr: {
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::ashr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1647 };
1648 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1649 KF);
1650 break;
1651 }
1652 case Instruction::Sub: {
1655 computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW, NUW,
1656 DemandedElts, Known, Known2, Q, Depth);
1657 break;
1658 }
1659 case Instruction::Add: {
1662 computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW, NUW,
1663 DemandedElts, Known, Known2, Q, Depth);
1664 break;
1665 }
1666 case Instruction::SRem:
1667 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1668 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1669 Known = KnownBits::srem(Known, Known2);
1670 break;
1671
1672 case Instruction::URem:
1673 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1674 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1675 Known = KnownBits::urem(Known, Known2);
1676 break;
1677 case Instruction::Alloca:
1679 break;
1680 case Instruction::GetElementPtr: {
1681 // Analyze all of the subscripts of this getelementptr instruction
1682 // to determine if we can prove known low zero bits.
1683 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1684 // Accumulate the constant indices in a separate variable
1685 // to minimize the number of calls to computeForAddSub.
1686 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(I->getType());
1687 APInt AccConstIndices(IndexWidth, 0);
1688
1689 auto AddIndexToKnown = [&](KnownBits IndexBits) {
1690 if (IndexWidth == BitWidth) {
1691 // Note that inbounds does *not* guarantee nsw for the addition, as only
1692 // the offset is signed, while the base address is unsigned.
1693 Known = KnownBits::add(Known, IndexBits);
1694 } else {
1695 // If the index width is smaller than the pointer width, only add the
1696 // value to the low bits.
1697 assert(IndexWidth < BitWidth &&
1698 "Index width can't be larger than pointer width");
1699 Known.insertBits(KnownBits::add(Known.trunc(IndexWidth), IndexBits), 0);
1700 }
1701 };
1702
1704 for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
1705 // TrailZ can only become smaller, short-circuit if we hit zero.
1706 if (Known.isUnknown())
1707 break;
1708
1709 Value *Index = I->getOperand(i);
1710
1711 // Handle case when index is zero.
1712 Constant *CIndex = dyn_cast<Constant>(Index);
1713 if (CIndex && CIndex->isZeroValue())
1714 continue;
1715
1716 if (StructType *STy = GTI.getStructTypeOrNull()) {
1717 // Handle struct member offset arithmetic.
1718
1719 assert(CIndex &&
1720 "Access to structure field must be known at compile time");
1721
1722 if (CIndex->getType()->isVectorTy())
1723 Index = CIndex->getSplatValue();
1724
1725 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
1726 const StructLayout *SL = Q.DL.getStructLayout(STy);
1727 uint64_t Offset = SL->getElementOffset(Idx);
1728 AccConstIndices += Offset;
1729 continue;
1730 }
1731
1732 // Handle array index arithmetic.
1733 Type *IndexedTy = GTI.getIndexedType();
1734 if (!IndexedTy->isSized()) {
1735 Known.resetAll();
1736 break;
1737 }
1738
1739 TypeSize Stride = GTI.getSequentialElementStride(Q.DL);
1740 uint64_t StrideInBytes = Stride.getKnownMinValue();
1741 if (!Stride.isScalable()) {
1742 // Fast path for constant offset.
1743 if (auto *CI = dyn_cast<ConstantInt>(Index)) {
1744 AccConstIndices +=
1745 CI->getValue().sextOrTrunc(IndexWidth) * StrideInBytes;
1746 continue;
1747 }
1748 }
1749
1750 KnownBits IndexBits =
1751 computeKnownBits(Index, Q, Depth + 1).sextOrTrunc(IndexWidth);
1752 KnownBits ScalingFactor(IndexWidth);
1753 // Multiply by current sizeof type.
1754 // &A[i] == A + i * sizeof(*A[i]).
1755 if (Stride.isScalable()) {
1756 // For scalable types the only thing we know about sizeof is
1757 // that this is a multiple of the minimum size.
1758 ScalingFactor.Zero.setLowBits(llvm::countr_zero(StrideInBytes));
1759 } else {
1760 ScalingFactor =
1761 KnownBits::makeConstant(APInt(IndexWidth, StrideInBytes));
1762 }
1763 AddIndexToKnown(KnownBits::mul(IndexBits, ScalingFactor));
1764 }
1765 if (!Known.isUnknown() && !AccConstIndices.isZero())
1766 AddIndexToKnown(KnownBits::makeConstant(AccConstIndices));
1767 break;
1768 }
1769 case Instruction::PHI: {
1770 const PHINode *P = cast<PHINode>(I);
1771 BinaryOperator *BO = nullptr;
1772 Value *R = nullptr, *L = nullptr;
1773 if (matchSimpleRecurrence(P, BO, R, L)) {
1774 // Handle the case of a simple two-predecessor recurrence PHI.
1775 // There's a lot more that could theoretically be done here, but
1776 // this is sufficient to catch some interesting cases.
1777 unsigned Opcode = BO->getOpcode();
1778
1779 switch (Opcode) {
1780 // If this is a shift recurrence, we know the bits being shifted in. We
1781 // can combine that with information about the start value of the
1782 // recurrence to conclude facts about the result. If this is a udiv
1783 // recurrence, we know that the result can never exceed either the
1784 // numerator or the start value, whichever is greater.
1785 case Instruction::LShr:
1786 case Instruction::AShr:
1787 case Instruction::Shl:
1788 case Instruction::UDiv:
1789 if (BO->getOperand(0) != I)
1790 break;
1791 [[fallthrough]];
1792
1793 // For a urem recurrence, the result can never exceed the start value. The
1794 // phi could either be the numerator or the denominator.
1795 case Instruction::URem: {
1796 // We have matched a recurrence of the form:
1797 // %iv = [R, %entry], [%iv.next, %backedge]
1798 // %iv.next = shift_op %iv, L
1799
1800 // Recurse with the phi context to avoid concern about whether facts
1801 // inferred hold at original context instruction. TODO: It may be
1802 // correct to use the original context. IF warranted, explore and
1803 // add sufficient tests to cover.
1805 RecQ.CxtI = P;
1806 computeKnownBits(R, DemandedElts, Known2, RecQ, Depth + 1);
1807 switch (Opcode) {
1808 case Instruction::Shl:
1809 // A shl recurrence will only increase the tailing zeros
1810 Known.Zero.setLowBits(Known2.countMinTrailingZeros());
1811 break;
1812 case Instruction::LShr:
1813 case Instruction::UDiv:
1814 case Instruction::URem:
1815 // lshr, udiv, and urem recurrences will preserve the leading zeros of
1816 // the start value.
1817 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1818 break;
1819 case Instruction::AShr:
1820 // An ashr recurrence will extend the initial sign bit
1821 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1822 Known.One.setHighBits(Known2.countMinLeadingOnes());
1823 break;
1824 }
1825 break;
1826 }
1827
1828 // Check for operations that have the property that if
1829 // both their operands have low zero bits, the result
1830 // will have low zero bits.
1831 case Instruction::Add:
1832 case Instruction::Sub:
1833 case Instruction::And:
1834 case Instruction::Or:
1835 case Instruction::Mul: {
1836 // Change the context instruction to the "edge" that flows into the
1837 // phi. This is important because that is where the value is actually
1838 // "evaluated" even though it is used later somewhere else. (see also
1839 // D69571).
1841
1842 unsigned OpNum = P->getOperand(0) == R ? 0 : 1;
1843 Instruction *RInst = P->getIncomingBlock(OpNum)->getTerminator();
1844 Instruction *LInst = P->getIncomingBlock(1 - OpNum)->getTerminator();
1845
1846 // Ok, we have a PHI of the form L op= R. Check for low
1847 // zero bits.
1848 RecQ.CxtI = RInst;
1849 computeKnownBits(R, DemandedElts, Known2, RecQ, Depth + 1);
1850
1851 // We need to take the minimum number of known bits
1852 KnownBits Known3(BitWidth);
1853 RecQ.CxtI = LInst;
1854 computeKnownBits(L, DemandedElts, Known3, RecQ, Depth + 1);
1855
1856 Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(),
1857 Known3.countMinTrailingZeros()));
1858
1859 auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(BO);
1860 if (!OverflowOp || !Q.IIQ.hasNoSignedWrap(OverflowOp))
1861 break;
1862
1863 switch (Opcode) {
1864 // If initial value of recurrence is nonnegative, and we are adding
1865 // a nonnegative number with nsw, the result can only be nonnegative
1866 // or poison value regardless of the number of times we execute the
1867 // add in phi recurrence. If initial value is negative and we are
1868 // adding a negative number with nsw, the result can only be
1869 // negative or poison value. Similar arguments apply to sub and mul.
1870 //
1871 // (add non-negative, non-negative) --> non-negative
1872 // (add negative, negative) --> negative
1873 case Instruction::Add: {
1874 if (Known2.isNonNegative() && Known3.isNonNegative())
1875 Known.makeNonNegative();
1876 else if (Known2.isNegative() && Known3.isNegative())
1877 Known.makeNegative();
1878 break;
1879 }
1880
1881 // (sub nsw non-negative, negative) --> non-negative
1882 // (sub nsw negative, non-negative) --> negative
1883 case Instruction::Sub: {
1884 if (BO->getOperand(0) != I)
1885 break;
1886 if (Known2.isNonNegative() && Known3.isNegative())
1887 Known.makeNonNegative();
1888 else if (Known2.isNegative() && Known3.isNonNegative())
1889 Known.makeNegative();
1890 break;
1891 }
1892
1893 // (mul nsw non-negative, non-negative) --> non-negative
1894 case Instruction::Mul:
1895 if (Known2.isNonNegative() && Known3.isNonNegative())
1896 Known.makeNonNegative();
1897 break;
1898
1899 default:
1900 break;
1901 }
1902 break;
1903 }
1904
1905 default:
1906 break;
1907 }
1908 }
1909
1910 // Unreachable blocks may have zero-operand PHI nodes.
1911 if (P->getNumIncomingValues() == 0)
1912 break;
1913
1914 // Otherwise take the unions of the known bit sets of the operands,
1915 // taking conservative care to avoid excessive recursion.
1916 if (Depth < MaxAnalysisRecursionDepth - 1 && Known.isUnknown()) {
1917 // Skip if every incoming value references to ourself.
1918 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
1919 break;
1920
1921 Known.setAllConflict();
1922 for (const Use &U : P->operands()) {
1923 Value *IncValue;
1924 const PHINode *CxtPhi;
1925 Instruction *CxtI;
1926 breakSelfRecursivePHI(&U, P, IncValue, CxtI, &CxtPhi);
1927 // Skip direct self references.
1928 if (IncValue == P)
1929 continue;
1930
1931 // Change the context instruction to the "edge" that flows into the
1932 // phi. This is important because that is where the value is actually
1933 // "evaluated" even though it is used later somewhere else. (see also
1934 // D69571).
1936
1937 Known2 = KnownBits(BitWidth);
1938
1939 // Recurse, but cap the recursion to one level, because we don't
1940 // want to waste time spinning around in loops.
1941 // TODO: See if we can base recursion limiter on number of incoming phi
1942 // edges so we don't overly clamp analysis.
1943 computeKnownBits(IncValue, DemandedElts, Known2, RecQ,
1945
1946 // See if we can further use a conditional branch into the phi
1947 // to help us determine the range of the value.
1948 if (!Known2.isConstant()) {
1949 CmpPredicate Pred;
1950 const APInt *RHSC;
1951 BasicBlock *TrueSucc, *FalseSucc;
1952 // TODO: Use RHS Value and compute range from its known bits.
1953 if (match(RecQ.CxtI,
1954 m_Br(m_c_ICmp(Pred, m_Specific(IncValue), m_APInt(RHSC)),
1955 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
1956 // Check for cases of duplicate successors.
1957 if ((TrueSucc == CxtPhi->getParent()) !=
1958 (FalseSucc == CxtPhi->getParent())) {
1959 // If we're using the false successor, invert the predicate.
1960 if (FalseSucc == CxtPhi->getParent())
1961 Pred = CmpInst::getInversePredicate(Pred);
1962 // Get the knownbits implied by the incoming phi condition.
1963 auto CR = ConstantRange::makeExactICmpRegion(Pred, *RHSC);
1964 KnownBits KnownUnion = Known2.unionWith(CR.toKnownBits());
1965 // We can have conflicts here if we are analyzing deadcode (its
1966 // impossible for us reach this BB based the icmp).
1967 if (KnownUnion.hasConflict()) {
1968 // No reason to continue analyzing in a known dead region, so
1969 // just resetAll and break. This will cause us to also exit the
1970 // outer loop.
1971 Known.resetAll();
1972 break;
1973 }
1974 Known2 = KnownUnion;
1975 }
1976 }
1977 }
1978
1979 Known = Known.intersectWith(Known2);
1980 // If all bits have been ruled out, there's no need to check
1981 // more operands.
1982 if (Known.isUnknown())
1983 break;
1984 }
1985 }
1986 break;
1987 }
1988 case Instruction::Call:
1989 case Instruction::Invoke: {
1990 // If range metadata is attached to this call, set known bits from that,
1991 // and then intersect with known bits based on other properties of the
1992 // function.
1993 if (MDNode *MD =
1994 Q.IIQ.getMetadata(cast<Instruction>(I), LLVMContext::MD_range))
1996
1997 const auto *CB = cast<CallBase>(I);
1998
1999 if (std::optional<ConstantRange> Range = CB->getRange())
2000 Known = Known.unionWith(Range->toKnownBits());
2001
2002 if (const Value *RV = CB->getReturnedArgOperand()) {
2003 if (RV->getType() == I->getType()) {
2004 computeKnownBits(RV, Known2, Q, Depth + 1);
2005 Known = Known.unionWith(Known2);
2006 // If the function doesn't return properly for all input values
2007 // (e.g. unreachable exits) then there might be conflicts between the
2008 // argument value and the range metadata. Simply discard the known bits
2009 // in case of conflicts.
2010 if (Known.hasConflict())
2011 Known.resetAll();
2012 }
2013 }
2014 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
2015 switch (II->getIntrinsicID()) {
2016 default:
2017 break;
2018 case Intrinsic::abs: {
2019 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2020 bool IntMinIsPoison = match(II->getArgOperand(1), m_One());
2021 Known = Known.unionWith(Known2.abs(IntMinIsPoison));
2022 break;
2023 }
2024 case Intrinsic::bitreverse:
2025 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2026 Known = Known.unionWith(Known2.reverseBits());
2027 break;
2028 case Intrinsic::bswap:
2029 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2030 Known = Known.unionWith(Known2.byteSwap());
2031 break;
2032 case Intrinsic::ctlz: {
2033 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2034 // If we have a known 1, its position is our upper bound.
2035 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
2036 // If this call is poison for 0 input, the result will be less than 2^n.
2037 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
2038 PossibleLZ = std::min(PossibleLZ, BitWidth - 1);
2039 unsigned LowBits = llvm::bit_width(PossibleLZ);
2040 Known.Zero.setBitsFrom(LowBits);
2041 break;
2042 }
2043 case Intrinsic::cttz: {
2044 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2045 // If we have a known 1, its position is our upper bound.
2046 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
2047 // If this call is poison for 0 input, the result will be less than 2^n.
2048 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
2049 PossibleTZ = std::min(PossibleTZ, BitWidth - 1);
2050 unsigned LowBits = llvm::bit_width(PossibleTZ);
2051 Known.Zero.setBitsFrom(LowBits);
2052 break;
2053 }
2054 case Intrinsic::ctpop: {
2055 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2056 // We can bound the space the count needs. Also, bits known to be zero
2057 // can't contribute to the population.
2058 unsigned BitsPossiblySet = Known2.countMaxPopulation();
2059 unsigned LowBits = llvm::bit_width(BitsPossiblySet);
2060 Known.Zero.setBitsFrom(LowBits);
2061 // TODO: we could bound KnownOne using the lower bound on the number
2062 // of bits which might be set provided by popcnt KnownOne2.
2063 break;
2064 }
2065 case Intrinsic::fshr:
2066 case Intrinsic::fshl: {
2067 const APInt *SA;
2068 if (!match(I->getOperand(2), m_APInt(SA)))
2069 break;
2070
2071 // Normalize to funnel shift left.
2072 uint64_t ShiftAmt = SA->urem(BitWidth);
2073 if (II->getIntrinsicID() == Intrinsic::fshr)
2074 ShiftAmt = BitWidth - ShiftAmt;
2075
2076 KnownBits Known3(BitWidth);
2077 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2078 computeKnownBits(I->getOperand(1), DemandedElts, Known3, Q, Depth + 1);
2079
2080 Known2 <<= ShiftAmt;
2081 Known3 >>= BitWidth - ShiftAmt;
2082 Known = Known2.unionWith(Known3);
2083 break;
2084 }
2085 case Intrinsic::uadd_sat:
2086 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2087 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2088 Known = KnownBits::uadd_sat(Known, Known2);
2089 break;
2090 case Intrinsic::usub_sat:
2091 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2092 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2093 Known = KnownBits::usub_sat(Known, Known2);
2094 break;
2095 case Intrinsic::sadd_sat:
2096 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2097 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2098 Known = KnownBits::sadd_sat(Known, Known2);
2099 break;
2100 case Intrinsic::ssub_sat:
2101 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2102 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2103 Known = KnownBits::ssub_sat(Known, Known2);
2104 break;
2105 // Vec reverse preserves bits from input vec.
2106 case Intrinsic::vector_reverse:
2107 computeKnownBits(I->getOperand(0), DemandedElts.reverseBits(), Known, Q,
2108 Depth + 1);
2109 break;
2110 // for min/max/and/or reduce, any bit common to each element in the
2111 // input vec is set in the output.
2112 case Intrinsic::vector_reduce_and:
2113 case Intrinsic::vector_reduce_or:
2114 case Intrinsic::vector_reduce_umax:
2115 case Intrinsic::vector_reduce_umin:
2116 case Intrinsic::vector_reduce_smax:
2117 case Intrinsic::vector_reduce_smin:
2118 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2119 break;
2120 case Intrinsic::vector_reduce_xor: {
2121 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2122 // The zeros common to all vecs are zero in the output.
2123 // If the number of elements is odd, then the common ones remain. If the
2124 // number of elements is even, then the common ones becomes zeros.
2125 auto *VecTy = cast<VectorType>(I->getOperand(0)->getType());
2126 // Even, so the ones become zeros.
2127 bool EvenCnt = VecTy->getElementCount().isKnownEven();
2128 if (EvenCnt)
2129 Known.Zero |= Known.One;
2130 // Maybe even element count so need to clear ones.
2131 if (VecTy->isScalableTy() || EvenCnt)
2132 Known.One.clearAllBits();
2133 break;
2134 }
2135 case Intrinsic::umin:
2136 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2137 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2138 Known = KnownBits::umin(Known, Known2);
2139 break;
2140 case Intrinsic::umax:
2141 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2142 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2143 Known = KnownBits::umax(Known, Known2);
2144 break;
2145 case Intrinsic::smin:
2146 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2147 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2148 Known = KnownBits::smin(Known, Known2);
2150 break;
2151 case Intrinsic::smax:
2152 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2153 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2154 Known = KnownBits::smax(Known, Known2);
2156 break;
2157 case Intrinsic::ptrmask: {
2158 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2159
2160 const Value *Mask = I->getOperand(1);
2161 Known2 = KnownBits(Mask->getType()->getScalarSizeInBits());
2162 computeKnownBits(Mask, DemandedElts, Known2, Q, Depth + 1);
2163 // TODO: 1-extend would be more precise.
2164 Known &= Known2.anyextOrTrunc(BitWidth);
2165 break;
2166 }
2167 case Intrinsic::x86_sse2_pmulh_w:
2168 case Intrinsic::x86_avx2_pmulh_w:
2169 case Intrinsic::x86_avx512_pmulh_w_512:
2170 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2171 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2172 Known = KnownBits::mulhs(Known, Known2);
2173 break;
2174 case Intrinsic::x86_sse2_pmulhu_w:
2175 case Intrinsic::x86_avx2_pmulhu_w:
2176 case Intrinsic::x86_avx512_pmulhu_w_512:
2177 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2178 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2179 Known = KnownBits::mulhu(Known, Known2);
2180 break;
2181 case Intrinsic::x86_sse42_crc32_64_64:
2182 Known.Zero.setBitsFrom(32);
2183 break;
2184 case Intrinsic::x86_ssse3_phadd_d_128:
2185 case Intrinsic::x86_ssse3_phadd_w_128:
2186 case Intrinsic::x86_avx2_phadd_d:
2187 case Intrinsic::x86_avx2_phadd_w: {
2189 I, DemandedElts, Q, Depth,
2190 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2191 return KnownBits::add(KnownLHS, KnownRHS);
2192 });
2193 break;
2194 }
2195 case Intrinsic::x86_ssse3_phadd_sw_128:
2196 case Intrinsic::x86_avx2_phadd_sw: {
2198 I, DemandedElts, Q, Depth, KnownBits::sadd_sat);
2199 break;
2200 }
2201 case Intrinsic::x86_ssse3_phsub_d_128:
2202 case Intrinsic::x86_ssse3_phsub_w_128:
2203 case Intrinsic::x86_avx2_phsub_d:
2204 case Intrinsic::x86_avx2_phsub_w: {
2206 I, DemandedElts, Q, Depth,
2207 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2208 return KnownBits::sub(KnownLHS, KnownRHS);
2209 });
2210 break;
2211 }
2212 case Intrinsic::x86_ssse3_phsub_sw_128:
2213 case Intrinsic::x86_avx2_phsub_sw: {
2215 I, DemandedElts, Q, Depth, KnownBits::ssub_sat);
2216 break;
2217 }
2218 case Intrinsic::riscv_vsetvli:
2219 case Intrinsic::riscv_vsetvlimax: {
2220 bool HasAVL = II->getIntrinsicID() == Intrinsic::riscv_vsetvli;
2221 const ConstantRange Range = getVScaleRange(II->getFunction(), BitWidth);
2223 cast<ConstantInt>(II->getArgOperand(HasAVL))->getZExtValue());
2224 RISCVVType::VLMUL VLMUL = static_cast<RISCVVType::VLMUL>(
2225 cast<ConstantInt>(II->getArgOperand(1 + HasAVL))->getZExtValue());
2226 uint64_t MaxVLEN =
2227 Range.getUnsignedMax().getZExtValue() * RISCV::RVVBitsPerBlock;
2228 uint64_t MaxVL = MaxVLEN / RISCVVType::getSEWLMULRatio(SEW, VLMUL);
2229
2230 // Result of vsetvli must be not larger than AVL.
2231 if (HasAVL)
2232 if (auto *CI = dyn_cast<ConstantInt>(II->getArgOperand(0)))
2233 MaxVL = std::min(MaxVL, CI->getZExtValue());
2234
2235 unsigned KnownZeroFirstBit = Log2_32(MaxVL) + 1;
2236 if (BitWidth > KnownZeroFirstBit)
2237 Known.Zero.setBitsFrom(KnownZeroFirstBit);
2238 break;
2239 }
2240 case Intrinsic::vscale: {
2241 if (!II->getParent() || !II->getFunction())
2242 break;
2243
2244 Known = getVScaleRange(II->getFunction(), BitWidth).toKnownBits();
2245 break;
2246 }
2247 }
2248 }
2249 break;
2250 }
2251 case Instruction::ShuffleVector: {
2252 if (auto *Splat = getSplatValue(I)) {
2253 computeKnownBits(Splat, Known, Q, Depth + 1);
2254 break;
2255 }
2256
2257 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
2258 // FIXME: Do we need to handle ConstantExpr involving shufflevectors?
2259 if (!Shuf) {
2260 Known.resetAll();
2261 return;
2262 }
2263 // For undef elements, we don't know anything about the common state of
2264 // the shuffle result.
2265 APInt DemandedLHS, DemandedRHS;
2266 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) {
2267 Known.resetAll();
2268 return;
2269 }
2270 Known.setAllConflict();
2271 if (!!DemandedLHS) {
2272 const Value *LHS = Shuf->getOperand(0);
2273 computeKnownBits(LHS, DemandedLHS, Known, Q, Depth + 1);
2274 // If we don't know any bits, early out.
2275 if (Known.isUnknown())
2276 break;
2277 }
2278 if (!!DemandedRHS) {
2279 const Value *RHS = Shuf->getOperand(1);
2280 computeKnownBits(RHS, DemandedRHS, Known2, Q, Depth + 1);
2281 Known = Known.intersectWith(Known2);
2282 }
2283 break;
2284 }
2285 case Instruction::InsertElement: {
2286 if (isa<ScalableVectorType>(I->getType())) {
2287 Known.resetAll();
2288 return;
2289 }
2290 const Value *Vec = I->getOperand(0);
2291 const Value *Elt = I->getOperand(1);
2292 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
2293 unsigned NumElts = DemandedElts.getBitWidth();
2294 APInt DemandedVecElts = DemandedElts;
2295 bool NeedsElt = true;
2296 // If we know the index we are inserting too, clear it from Vec check.
2297 if (CIdx && CIdx->getValue().ult(NumElts)) {
2298 DemandedVecElts.clearBit(CIdx->getZExtValue());
2299 NeedsElt = DemandedElts[CIdx->getZExtValue()];
2300 }
2301
2302 Known.setAllConflict();
2303 if (NeedsElt) {
2304 computeKnownBits(Elt, Known, Q, Depth + 1);
2305 // If we don't know any bits, early out.
2306 if (Known.isUnknown())
2307 break;
2308 }
2309
2310 if (!DemandedVecElts.isZero()) {
2311 computeKnownBits(Vec, DemandedVecElts, Known2, Q, Depth + 1);
2312 Known = Known.intersectWith(Known2);
2313 }
2314 break;
2315 }
2316 case Instruction::ExtractElement: {
2317 // Look through extract element. If the index is non-constant or
2318 // out-of-range demand all elements, otherwise just the extracted element.
2319 const Value *Vec = I->getOperand(0);
2320 const Value *Idx = I->getOperand(1);
2321 auto *CIdx = dyn_cast<ConstantInt>(Idx);
2322 if (isa<ScalableVectorType>(Vec->getType())) {
2323 // FIXME: there's probably *something* we can do with scalable vectors
2324 Known.resetAll();
2325 break;
2326 }
2327 unsigned NumElts = cast<FixedVectorType>(Vec->getType())->getNumElements();
2328 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
2329 if (CIdx && CIdx->getValue().ult(NumElts))
2330 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
2331 computeKnownBits(Vec, DemandedVecElts, Known, Q, Depth + 1);
2332 break;
2333 }
2334 case Instruction::ExtractValue:
2335 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
2337 if (EVI->getNumIndices() != 1) break;
2338 if (EVI->getIndices()[0] == 0) {
2339 switch (II->getIntrinsicID()) {
2340 default: break;
2341 case Intrinsic::uadd_with_overflow:
2342 case Intrinsic::sadd_with_overflow:
2344 true, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2345 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2346 break;
2347 case Intrinsic::usub_with_overflow:
2348 case Intrinsic::ssub_with_overflow:
2350 false, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2351 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2352 break;
2353 case Intrinsic::umul_with_overflow:
2354 case Intrinsic::smul_with_overflow:
2355 computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false,
2356 false, DemandedElts, Known, Known2, Q, Depth);
2357 break;
2358 }
2359 }
2360 }
2361 break;
2362 case Instruction::Freeze:
2363 if (isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
2364 Depth + 1))
2365 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2366 break;
2367 }
2368}
2369
2370/// Determine which bits of V are known to be either zero or one and return
2371/// them.
2372KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
2373 const SimplifyQuery &Q, unsigned Depth) {
2374 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2375 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);
2376 return Known;
2377}
2378
2379/// Determine which bits of V are known to be either zero or one and return
2380/// them.
2382 unsigned Depth) {
2383 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2384 computeKnownBits(V, Known, Q, Depth);
2385 return Known;
2386}
2387
2388/// Determine which bits of V are known to be either zero or one and return
2389/// them in the Known bit set.
2390///
2391/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that
2392/// we cannot optimize based on the assumption that it is zero without changing
2393/// it to be an explicit zero. If we don't change it to zero, other code could
2394/// optimized based on the contradictory assumption that it is non-zero.
2395/// Because instcombine aggressively folds operations with undef args anyway,
2396/// this won't lose us code quality.
2397///
2398/// This function is defined on values with integer type, values with pointer
2399/// type, and vectors of integers. In the case
2400/// where V is a vector, known zero, and known one values are the
2401/// same width as the vector element, and the bit is set only if it is true
2402/// for all of the demanded elements in the vector specified by DemandedElts.
2403void computeKnownBits(const Value *V, const APInt &DemandedElts,
2404 KnownBits &Known, const SimplifyQuery &Q,
2405 unsigned Depth) {
2406 if (!DemandedElts) {
2407 // No demanded elts, better to assume we don't know anything.
2408 Known.resetAll();
2409 return;
2410 }
2411
2412 assert(V && "No Value?");
2413 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2414
2415#ifndef NDEBUG
2416 Type *Ty = V->getType();
2417 unsigned BitWidth = Known.getBitWidth();
2418
2419 assert((Ty->isIntOrIntVectorTy(BitWidth) || Ty->isPtrOrPtrVectorTy()) &&
2420 "Not integer or pointer type!");
2421
2422 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
2423 assert(
2424 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
2425 "DemandedElt width should equal the fixed vector number of elements");
2426 } else {
2427 assert(DemandedElts == APInt(1, 1) &&
2428 "DemandedElt width should be 1 for scalars or scalable vectors");
2429 }
2430
2431 Type *ScalarTy = Ty->getScalarType();
2432 if (ScalarTy->isPointerTy()) {
2433 assert(BitWidth == Q.DL.getPointerTypeSizeInBits(ScalarTy) &&
2434 "V and Known should have same BitWidth");
2435 } else {
2436 assert(BitWidth == Q.DL.getTypeSizeInBits(ScalarTy) &&
2437 "V and Known should have same BitWidth");
2438 }
2439#endif
2440
2441 const APInt *C;
2442 if (match(V, m_APInt(C))) {
2443 // We know all of the bits for a scalar constant or a splat vector constant!
2444 Known = KnownBits::makeConstant(*C);
2445 return;
2446 }
2447 // Null and aggregate-zero are all-zeros.
2449 Known.setAllZero();
2450 return;
2451 }
2452 // Handle a constant vector by taking the intersection of the known bits of
2453 // each element.
2455 assert(!isa<ScalableVectorType>(V->getType()));
2456 // We know that CDV must be a vector of integers. Take the intersection of
2457 // each element.
2458 Known.setAllConflict();
2459 for (unsigned i = 0, e = CDV->getNumElements(); i != e; ++i) {
2460 if (!DemandedElts[i])
2461 continue;
2462 APInt Elt = CDV->getElementAsAPInt(i);
2463 Known.Zero &= ~Elt;
2464 Known.One &= Elt;
2465 }
2466 if (Known.hasConflict())
2467 Known.resetAll();
2468 return;
2469 }
2470
2471 if (const auto *CV = dyn_cast<ConstantVector>(V)) {
2472 assert(!isa<ScalableVectorType>(V->getType()));
2473 // We know that CV must be a vector of integers. Take the intersection of
2474 // each element.
2475 Known.setAllConflict();
2476 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
2477 if (!DemandedElts[i])
2478 continue;
2479 Constant *Element = CV->getAggregateElement(i);
2480 if (isa<PoisonValue>(Element))
2481 continue;
2482 auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
2483 if (!ElementCI) {
2484 Known.resetAll();
2485 return;
2486 }
2487 const APInt &Elt = ElementCI->getValue();
2488 Known.Zero &= ~Elt;
2489 Known.One &= Elt;
2490 }
2491 if (Known.hasConflict())
2492 Known.resetAll();
2493 return;
2494 }
2495
2496 // Start out not knowing anything.
2497 Known.resetAll();
2498
2499 // We can't imply anything about undefs.
2500 if (isa<UndefValue>(V))
2501 return;
2502
2503 // There's no point in looking through other users of ConstantData for
2504 // assumptions. Confirm that we've handled them all.
2505 assert(!isa<ConstantData>(V) && "Unhandled constant data!");
2506
2507 if (const auto *A = dyn_cast<Argument>(V))
2508 if (std::optional<ConstantRange> Range = A->getRange())
2509 Known = Range->toKnownBits();
2510
2511 // All recursive calls that increase depth must come after this.
2513 return;
2514
2515 // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
2516 // the bits of its aliasee.
2517 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
2518 if (!GA->isInterposable())
2519 computeKnownBits(GA->getAliasee(), Known, Q, Depth + 1);
2520 return;
2521 }
2522
2523 if (const Operator *I = dyn_cast<Operator>(V))
2524 computeKnownBitsFromOperator(I, DemandedElts, Known, Q, Depth);
2525 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2526 if (std::optional<ConstantRange> CR = GV->getAbsoluteSymbolRange())
2527 Known = CR->toKnownBits();
2528 }
2529
2530 // Aligned pointers have trailing zeros - refine Known.Zero set
2531 if (isa<PointerType>(V->getType())) {
2532 Align Alignment = V->getPointerAlignment(Q.DL);
2533 Known.Zero.setLowBits(Log2(Alignment));
2534 }
2535
2536 // computeKnownBitsFromContext strictly refines Known.
2537 // Therefore, we run them after computeKnownBitsFromOperator.
2538
2539 // Check whether we can determine known bits from context such as assumes.
2540 computeKnownBitsFromContext(V, Known, Q, Depth);
2541}
2542
2543/// Try to detect a recurrence that the value of the induction variable is
2544/// always a power of two (or zero).
2545static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero,
2546 SimplifyQuery &Q, unsigned Depth) {
2547 BinaryOperator *BO = nullptr;
2548 Value *Start = nullptr, *Step = nullptr;
2549 if (!matchSimpleRecurrence(PN, BO, Start, Step))
2550 return false;
2551
2552 // Initial value must be a power of two.
2553 for (const Use &U : PN->operands()) {
2554 if (U.get() == Start) {
2555 // Initial value comes from a different BB, need to adjust context
2556 // instruction for analysis.
2557 Q.CxtI = PN->getIncomingBlock(U)->getTerminator();
2558 if (!isKnownToBeAPowerOfTwo(Start, OrZero, Q, Depth))
2559 return false;
2560 }
2561 }
2562
2563 // Except for Mul, the induction variable must be on the left side of the
2564 // increment expression, otherwise its value can be arbitrary.
2565 if (BO->getOpcode() != Instruction::Mul && BO->getOperand(1) != Step)
2566 return false;
2567
2568 Q.CxtI = BO->getParent()->getTerminator();
2569 switch (BO->getOpcode()) {
2570 case Instruction::Mul:
2571 // Power of two is closed under multiplication.
2572 return (OrZero || Q.IIQ.hasNoUnsignedWrap(BO) ||
2573 Q.IIQ.hasNoSignedWrap(BO)) &&
2574 isKnownToBeAPowerOfTwo(Step, OrZero, Q, Depth);
2575 case Instruction::SDiv:
2576 // Start value must not be signmask for signed division, so simply being a
2577 // power of two is not sufficient, and it has to be a constant.
2578 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2579 return false;
2580 [[fallthrough]];
2581 case Instruction::UDiv:
2582 // Divisor must be a power of two.
2583 // If OrZero is false, cannot guarantee induction variable is non-zero after
2584 // division, same for Shr, unless it is exact division.
2585 return (OrZero || Q.IIQ.isExact(BO)) &&
2586 isKnownToBeAPowerOfTwo(Step, false, Q, Depth);
2587 case Instruction::Shl:
2588 return OrZero || Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO);
2589 case Instruction::AShr:
2590 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2591 return false;
2592 [[fallthrough]];
2593 case Instruction::LShr:
2594 return OrZero || Q.IIQ.isExact(BO);
2595 default:
2596 return false;
2597 }
2598}
2599
2600/// Return true if we can infer that \p V is known to be a power of 2 from
2601/// dominating condition \p Cond (e.g., ctpop(V) == 1).
2602static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero,
2603 const Value *Cond,
2604 bool CondIsTrue) {
2605 CmpPredicate Pred;
2606 const APInt *RHSC;
2608 m_APInt(RHSC))))
2609 return false;
2610 if (!CondIsTrue)
2611 Pred = ICmpInst::getInversePredicate(Pred);
2612 // ctpop(V) u< 2
2613 if (OrZero && Pred == ICmpInst::ICMP_ULT && *RHSC == 2)
2614 return true;
2615 // ctpop(V) == 1
2616 return Pred == ICmpInst::ICMP_EQ && *RHSC == 1;
2617}
2618
2619/// Return true if the given value is known to have exactly one
2620/// bit set when defined. For vectors return true if every element is known to
2621/// be a power of two when defined. Supports values with integer or pointer
2622/// types and vectors of integers.
2623bool llvm::isKnownToBeAPowerOfTwo(const Value *V, bool OrZero,
2624 const SimplifyQuery &Q, unsigned Depth) {
2625 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2626
2627 if (isa<Constant>(V))
2628 return OrZero ? match(V, m_Power2OrZero()) : match(V, m_Power2());
2629
2630 // i1 is by definition a power of 2 or zero.
2631 if (OrZero && V->getType()->getScalarSizeInBits() == 1)
2632 return true;
2633
2634 // Try to infer from assumptions.
2635 if (Q.AC && Q.CxtI) {
2636 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
2637 if (!AssumeVH)
2638 continue;
2639 CallInst *I = cast<CallInst>(AssumeVH);
2640 if (isImpliedToBeAPowerOfTwoFromCond(V, OrZero, I->getArgOperand(0),
2641 /*CondIsTrue=*/true) &&
2643 return true;
2644 }
2645 }
2646
2647 // Handle dominating conditions.
2648 if (Q.DC && Q.CxtI && Q.DT) {
2649 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
2650 Value *Cond = BI->getCondition();
2651
2652 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
2654 /*CondIsTrue=*/true) &&
2655 Q.DT->dominates(Edge0, Q.CxtI->getParent()))
2656 return true;
2657
2658 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
2660 /*CondIsTrue=*/false) &&
2661 Q.DT->dominates(Edge1, Q.CxtI->getParent()))
2662 return true;
2663 }
2664 }
2665
2666 auto *I = dyn_cast<Instruction>(V);
2667 if (!I)
2668 return false;
2669
2670 if (Q.CxtI && match(V, m_VScale())) {
2671 const Function *F = Q.CxtI->getFunction();
2672 // The vscale_range indicates vscale is a power-of-two.
2673 return F->hasFnAttribute(Attribute::VScaleRange);
2674 }
2675
2676 // 1 << X is clearly a power of two if the one is not shifted off the end. If
2677 // it is shifted off the end then the result is undefined.
2678 if (match(I, m_Shl(m_One(), m_Value())))
2679 return true;
2680
2681 // (signmask) >>l X is clearly a power of two if the one is not shifted off
2682 // the bottom. If it is shifted off the bottom then the result is undefined.
2683 if (match(I, m_LShr(m_SignMask(), m_Value())))
2684 return true;
2685
2686 // The remaining tests are all recursive, so bail out if we hit the limit.
2688 return false;
2689
2690 switch (I->getOpcode()) {
2691 case Instruction::ZExt:
2692 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2693 case Instruction::Trunc:
2694 return OrZero && isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2695 case Instruction::Shl:
2696 if (OrZero || Q.IIQ.hasNoUnsignedWrap(I) || Q.IIQ.hasNoSignedWrap(I))
2697 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2698 return false;
2699 case Instruction::LShr:
2700 if (OrZero || Q.IIQ.isExact(cast<BinaryOperator>(I)))
2701 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2702 return false;
2703 case Instruction::UDiv:
2705 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2706 return false;
2707 case Instruction::Mul:
2708 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
2709 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth) &&
2710 (OrZero || isKnownNonZero(I, Q, Depth));
2711 case Instruction::And:
2712 // A power of two and'd with anything is a power of two or zero.
2713 if (OrZero &&
2714 (isKnownToBeAPowerOfTwo(I->getOperand(1), /*OrZero*/ true, Q, Depth) ||
2715 isKnownToBeAPowerOfTwo(I->getOperand(0), /*OrZero*/ true, Q, Depth)))
2716 return true;
2717 // X & (-X) is always a power of two or zero.
2718 if (match(I->getOperand(0), m_Neg(m_Specific(I->getOperand(1)))) ||
2719 match(I->getOperand(1), m_Neg(m_Specific(I->getOperand(0)))))
2720 return OrZero || isKnownNonZero(I->getOperand(0), Q, Depth);
2721 return false;
2722 case Instruction::Add: {
2723 // Adding a power-of-two or zero to the same power-of-two or zero yields
2724 // either the original power-of-two, a larger power-of-two or zero.
2726 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) ||
2727 Q.IIQ.hasNoSignedWrap(VOBO)) {
2728 if (match(I->getOperand(0),
2729 m_c_And(m_Specific(I->getOperand(1)), m_Value())) &&
2730 isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth))
2731 return true;
2732 if (match(I->getOperand(1),
2733 m_c_And(m_Specific(I->getOperand(0)), m_Value())) &&
2734 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth))
2735 return true;
2736
2737 unsigned BitWidth = V->getType()->getScalarSizeInBits();
2738 KnownBits LHSBits(BitWidth);
2739 computeKnownBits(I->getOperand(0), LHSBits, Q, Depth);
2740
2741 KnownBits RHSBits(BitWidth);
2742 computeKnownBits(I->getOperand(1), RHSBits, Q, Depth);
2743 // If i8 V is a power of two or zero:
2744 // ZeroBits: 1 1 1 0 1 1 1 1
2745 // ~ZeroBits: 0 0 0 1 0 0 0 0
2746 if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2())
2747 // If OrZero isn't set, we cannot give back a zero result.
2748 // Make sure either the LHS or RHS has a bit set.
2749 if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue())
2750 return true;
2751 }
2752
2753 // LShr(UINT_MAX, Y) + 1 is a power of two (if add is nuw) or zero.
2754 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO))
2755 if (match(I, m_Add(m_LShr(m_AllOnes(), m_Value()), m_One())))
2756 return true;
2757 return false;
2758 }
2759 case Instruction::Select:
2760 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
2761 isKnownToBeAPowerOfTwo(I->getOperand(2), OrZero, Q, Depth);
2762 case Instruction::PHI: {
2763 // A PHI node is power of two if all incoming values are power of two, or if
2764 // it is an induction variable where in each step its value is a power of
2765 // two.
2766 auto *PN = cast<PHINode>(I);
2768
2769 // Check if it is an induction variable and always power of two.
2770 if (isPowerOfTwoRecurrence(PN, OrZero, RecQ, Depth))
2771 return true;
2772
2773 // Recursively check all incoming values. Limit recursion to 2 levels, so
2774 // that search complexity is limited to number of operands^2.
2775 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
2776 return llvm::all_of(PN->operands(), [&](const Use &U) {
2777 // Value is power of 2 if it is coming from PHI node itself by induction.
2778 if (U.get() == PN)
2779 return true;
2780
2781 // Change the context instruction to the incoming block where it is
2782 // evaluated.
2783 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
2784 return isKnownToBeAPowerOfTwo(U.get(), OrZero, RecQ, NewDepth);
2785 });
2786 }
2787 case Instruction::Invoke:
2788 case Instruction::Call: {
2789 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
2790 switch (II->getIntrinsicID()) {
2791 case Intrinsic::umax:
2792 case Intrinsic::smax:
2793 case Intrinsic::umin:
2794 case Intrinsic::smin:
2795 return isKnownToBeAPowerOfTwo(II->getArgOperand(1), OrZero, Q, Depth) &&
2796 isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2797 // bswap/bitreverse just move around bits, but don't change any 1s/0s
2798 // thus dont change pow2/non-pow2 status.
2799 case Intrinsic::bitreverse:
2800 case Intrinsic::bswap:
2801 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2802 case Intrinsic::fshr:
2803 case Intrinsic::fshl:
2804 // If Op0 == Op1, this is a rotate. is_pow2(rotate(x, y)) == is_pow2(x)
2805 if (II->getArgOperand(0) == II->getArgOperand(1))
2806 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2807 break;
2808 default:
2809 break;
2810 }
2811 }
2812 return false;
2813 }
2814 default:
2815 return false;
2816 }
2817}
2818
2819/// Test whether a GEP's result is known to be non-null.
2820///
2821/// Uses properties inherent in a GEP to try to determine whether it is known
2822/// to be non-null.
2823///
2824/// Currently this routine does not support vector GEPs.
2825static bool isGEPKnownNonNull(const GEPOperator *GEP, const SimplifyQuery &Q,
2826 unsigned Depth) {
2827 const Function *F = nullptr;
2828 if (const Instruction *I = dyn_cast<Instruction>(GEP))
2829 F = I->getFunction();
2830
2831 // If the gep is nuw or inbounds with invalid null pointer, then the GEP
2832 // may be null iff the base pointer is null and the offset is zero.
2833 if (!GEP->hasNoUnsignedWrap() &&
2834 !(GEP->isInBounds() &&
2835 !NullPointerIsDefined(F, GEP->getPointerAddressSpace())))
2836 return false;
2837
2838 // FIXME: Support vector-GEPs.
2839 assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP");
2840
2841 // If the base pointer is non-null, we cannot walk to a null address with an
2842 // inbounds GEP in address space zero.
2843 if (isKnownNonZero(GEP->getPointerOperand(), Q, Depth))
2844 return true;
2845
2846 // Walk the GEP operands and see if any operand introduces a non-zero offset.
2847 // If so, then the GEP cannot produce a null pointer, as doing so would
2848 // inherently violate the inbounds contract within address space zero.
2850 GTI != GTE; ++GTI) {
2851 // Struct types are easy -- they must always be indexed by a constant.
2852 if (StructType *STy = GTI.getStructTypeOrNull()) {
2853 ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
2854 unsigned ElementIdx = OpC->getZExtValue();
2855 const StructLayout *SL = Q.DL.getStructLayout(STy);
2856 uint64_t ElementOffset = SL->getElementOffset(ElementIdx);
2857 if (ElementOffset > 0)
2858 return true;
2859 continue;
2860 }
2861
2862 // If we have a zero-sized type, the index doesn't matter. Keep looping.
2863 if (GTI.getSequentialElementStride(Q.DL).isZero())
2864 continue;
2865
2866 // Fast path the constant operand case both for efficiency and so we don't
2867 // increment Depth when just zipping down an all-constant GEP.
2868 if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
2869 if (!OpC->isZero())
2870 return true;
2871 continue;
2872 }
2873
2874 // We post-increment Depth here because while isKnownNonZero increments it
2875 // as well, when we pop back up that increment won't persist. We don't want
2876 // to recurse 10k times just because we have 10k GEP operands. We don't
2877 // bail completely out because we want to handle constant GEPs regardless
2878 // of depth.
2880 continue;
2881
2882 if (isKnownNonZero(GTI.getOperand(), Q, Depth))
2883 return true;
2884 }
2885
2886 return false;
2887}
2888
2890 const Instruction *CtxI,
2891 const DominatorTree *DT) {
2892 assert(!isa<Constant>(V) && "Called for constant?");
2893
2894 if (!CtxI || !DT)
2895 return false;
2896
2897 unsigned NumUsesExplored = 0;
2898 for (auto &U : V->uses()) {
2899 // Avoid massive lists
2900 if (NumUsesExplored >= DomConditionsMaxUses)
2901 break;
2902 NumUsesExplored++;
2903
2904 const Instruction *UI = cast<Instruction>(U.getUser());
2905 // If the value is used as an argument to a call or invoke, then argument
2906 // attributes may provide an answer about null-ness.
2907 if (V->getType()->isPointerTy()) {
2908 if (const auto *CB = dyn_cast<CallBase>(UI)) {
2909 if (CB->isArgOperand(&U) &&
2910 CB->paramHasNonNullAttr(CB->getArgOperandNo(&U),
2911 /*AllowUndefOrPoison=*/false) &&
2912 DT->dominates(CB, CtxI))
2913 return true;
2914 }
2915 }
2916
2917 // If the value is used as a load/store, then the pointer must be non null.
2918 if (V == getLoadStorePointerOperand(UI)) {
2921 DT->dominates(UI, CtxI))
2922 return true;
2923 }
2924
2925 if ((match(UI, m_IDiv(m_Value(), m_Specific(V))) ||
2926 match(UI, m_IRem(m_Value(), m_Specific(V)))) &&
2927 isValidAssumeForContext(UI, CtxI, DT))
2928 return true;
2929
2930 // Consider only compare instructions uniquely controlling a branch
2931 Value *RHS;
2932 CmpPredicate Pred;
2933 if (!match(UI, m_c_ICmp(Pred, m_Specific(V), m_Value(RHS))))
2934 continue;
2935
2936 bool NonNullIfTrue;
2937 if (cmpExcludesZero(Pred, RHS))
2938 NonNullIfTrue = true;
2940 NonNullIfTrue = false;
2941 else
2942 continue;
2943
2946 for (const auto *CmpU : UI->users()) {
2947 assert(WorkList.empty() && "Should be!");
2948 if (Visited.insert(CmpU).second)
2949 WorkList.push_back(CmpU);
2950
2951 while (!WorkList.empty()) {
2952 auto *Curr = WorkList.pop_back_val();
2953
2954 // If a user is an AND, add all its users to the work list. We only
2955 // propagate "pred != null" condition through AND because it is only
2956 // correct to assume that all conditions of AND are met in true branch.
2957 // TODO: Support similar logic of OR and EQ predicate?
2958 if (NonNullIfTrue)
2959 if (match(Curr, m_LogicalAnd(m_Value(), m_Value()))) {
2960 for (const auto *CurrU : Curr->users())
2961 if (Visited.insert(CurrU).second)
2962 WorkList.push_back(CurrU);
2963 continue;
2964 }
2965
2966 if (const BranchInst *BI = dyn_cast<BranchInst>(Curr)) {
2967 assert(BI->isConditional() && "uses a comparison!");
2968
2969 BasicBlock *NonNullSuccessor =
2970 BI->getSuccessor(NonNullIfTrue ? 0 : 1);
2971 BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor);
2972 if (Edge.isSingleEdge() && DT->dominates(Edge, CtxI->getParent()))
2973 return true;
2974 } else if (NonNullIfTrue && isGuard(Curr) &&
2975 DT->dominates(cast<Instruction>(Curr), CtxI)) {
2976 return true;
2977 }
2978 }
2979 }
2980 }
2981
2982 return false;
2983}
2984
2985/// Does the 'Range' metadata (which must be a valid MD_range operand list)
2986/// ensure that the value it's attached to is never Value? 'RangeType' is
2987/// is the type of the value described by the range.
2988static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) {
2989 const unsigned NumRanges = Ranges->getNumOperands() / 2;
2990 assert(NumRanges >= 1);
2991 for (unsigned i = 0; i < NumRanges; ++i) {
2993 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
2995 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
2996 ConstantRange Range(Lower->getValue(), Upper->getValue());
2997 if (Range.contains(Value))
2998 return false;
2999 }
3000 return true;
3001}
3002
3003/// Try to detect a recurrence that monotonically increases/decreases from a
3004/// non-zero starting value. These are common as induction variables.
3005static bool isNonZeroRecurrence(const PHINode *PN) {
3006 BinaryOperator *BO = nullptr;
3007 Value *Start = nullptr, *Step = nullptr;
3008 const APInt *StartC, *StepC;
3009 if (!matchSimpleRecurrence(PN, BO, Start, Step) ||
3010 !match(Start, m_APInt(StartC)) || StartC->isZero())
3011 return false;
3012
3013 switch (BO->getOpcode()) {
3014 case Instruction::Add:
3015 // Starting from non-zero and stepping away from zero can never wrap back
3016 // to zero.
3017 return BO->hasNoUnsignedWrap() ||
3018 (BO->hasNoSignedWrap() && match(Step, m_APInt(StepC)) &&
3019 StartC->isNegative() == StepC->isNegative());
3020 case Instruction::Mul:
3021 return (BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap()) &&
3022 match(Step, m_APInt(StepC)) && !StepC->isZero();
3023 case Instruction::Shl:
3024 return BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap();
3025 case Instruction::AShr:
3026 case Instruction::LShr:
3027 return BO->isExact();
3028 default:
3029 return false;
3030 }
3031}
3032
3033static bool matchOpWithOpEqZero(Value *Op0, Value *Op1) {
3035 m_Specific(Op1), m_Zero()))) ||
3037 m_Specific(Op0), m_Zero())));
3038}
3039
3040static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q,
3041 unsigned BitWidth, Value *X, Value *Y, bool NSW,
3042 bool NUW, unsigned Depth) {
3043 // (X + (X != 0)) is non zero
3044 if (matchOpWithOpEqZero(X, Y))
3045 return true;
3046
3047 if (NUW)
3048 return isKnownNonZero(Y, DemandedElts, Q, Depth) ||
3049 isKnownNonZero(X, DemandedElts, Q, Depth);
3050
3051 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
3052 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
3053
3054 // If X and Y are both non-negative (as signed values) then their sum is not
3055 // zero unless both X and Y are zero.
3056 if (XKnown.isNonNegative() && YKnown.isNonNegative())
3057 if (isKnownNonZero(Y, DemandedElts, Q, Depth) ||
3058 isKnownNonZero(X, DemandedElts, Q, Depth))
3059 return true;
3060
3061 // If X and Y are both negative (as signed values) then their sum is not
3062 // zero unless both X and Y equal INT_MIN.
3063 if (XKnown.isNegative() && YKnown.isNegative()) {
3065 // The sign bit of X is set. If some other bit is set then X is not equal
3066 // to INT_MIN.
3067 if (XKnown.One.intersects(Mask))
3068 return true;
3069 // The sign bit of Y is set. If some other bit is set then Y is not equal
3070 // to INT_MIN.
3071 if (YKnown.One.intersects(Mask))
3072 return true;
3073 }
3074
3075 // The sum of a non-negative number and a power of two is not zero.
3076 if (XKnown.isNonNegative() &&
3077 isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Q, Depth))
3078 return true;
3079 if (YKnown.isNonNegative() &&
3080 isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Q, Depth))
3081 return true;
3082
3083 return KnownBits::add(XKnown, YKnown, NSW, NUW).isNonZero();
3084}
3085
3086static bool isNonZeroSub(const APInt &DemandedElts, const SimplifyQuery &Q,
3087 unsigned BitWidth, Value *X, Value *Y,
3088 unsigned Depth) {
3089 // (X - (X != 0)) is non zero
3090 // ((X != 0) - X) is non zero
3091 if (matchOpWithOpEqZero(X, Y))
3092 return true;
3093
3094 // TODO: Move this case into isKnownNonEqual().
3095 if (auto *C = dyn_cast<Constant>(X))
3096 if (C->isNullValue() && isKnownNonZero(Y, DemandedElts, Q, Depth))
3097 return true;
3098
3099 return ::isKnownNonEqual(X, Y, DemandedElts, Q, Depth);
3100}
3101
3102static bool isNonZeroMul(const APInt &DemandedElts, const SimplifyQuery &Q,
3103 unsigned BitWidth, Value *X, Value *Y, bool NSW,
3104 bool NUW, unsigned Depth) {
3105 // If X and Y are non-zero then so is X * Y as long as the multiplication
3106 // does not overflow.
3107 if (NSW || NUW)
3108 return isKnownNonZero(X, DemandedElts, Q, Depth) &&
3109 isKnownNonZero(Y, DemandedElts, Q, Depth);
3110
3111 // If either X or Y is odd, then if the other is non-zero the result can't
3112 // be zero.
3113 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
3114 if (XKnown.One[0])
3115 return isKnownNonZero(Y, DemandedElts, Q, Depth);
3116
3117 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
3118 if (YKnown.One[0])
3119 return XKnown.isNonZero() || isKnownNonZero(X, DemandedElts, Q, Depth);
3120
3121 // If there exists any subset of X (sX) and subset of Y (sY) s.t sX * sY is
3122 // non-zero, then X * Y is non-zero. We can find sX and sY by just taking
3123 // the lowest known One of X and Y. If they are non-zero, the result
3124 // must be non-zero. We can check if LSB(X) * LSB(Y) != 0 by doing
3125 // X.CountLeadingZeros + Y.CountLeadingZeros < BitWidth.
3126 return (XKnown.countMaxTrailingZeros() + YKnown.countMaxTrailingZeros()) <
3127 BitWidth;
3128}
3129
3130static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts,
3131 const SimplifyQuery &Q, const KnownBits &KnownVal,
3132 unsigned Depth) {
3133 auto ShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
3134 switch (I->getOpcode()) {
3135 case Instruction::Shl:
3136 return Lhs.shl(Rhs);
3137 case Instruction::LShr:
3138 return Lhs.lshr(Rhs);
3139 case Instruction::AShr:
3140 return Lhs.ashr(Rhs);
3141 default:
3142 llvm_unreachable("Unknown Shift Opcode");
3143 }
3144 };
3145
3146 auto InvShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
3147 switch (I->getOpcode()) {
3148 case Instruction::Shl:
3149 return Lhs.lshr(Rhs);
3150 case Instruction::LShr:
3151 case Instruction::AShr:
3152 return Lhs.shl(Rhs);
3153 default:
3154 llvm_unreachable("Unknown Shift Opcode");
3155 }
3156 };
3157
3158 if (KnownVal.isUnknown())
3159 return false;
3160
3161 KnownBits KnownCnt =
3162 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
3163 APInt MaxShift = KnownCnt.getMaxValue();
3164 unsigned NumBits = KnownVal.getBitWidth();
3165 if (MaxShift.uge(NumBits))
3166 return false;
3167
3168 if (!ShiftOp(KnownVal.One, MaxShift).isZero())
3169 return true;
3170
3171 // If all of the bits shifted out are known to be zero, and Val is known
3172 // non-zero then at least one non-zero bit must remain.
3173 if (InvShiftOp(KnownVal.Zero, NumBits - MaxShift)
3174 .eq(InvShiftOp(APInt::getAllOnes(NumBits), NumBits - MaxShift)) &&
3175 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth))
3176 return true;
3177
3178 return false;
3179}
3180
3182 const APInt &DemandedElts,
3183 const SimplifyQuery &Q, unsigned Depth) {
3184 unsigned BitWidth = getBitWidth(I->getType()->getScalarType(), Q.DL);
3185 switch (I->getOpcode()) {
3186 case Instruction::Alloca:
3187 // Alloca never returns null, malloc might.
3188 return I->getType()->getPointerAddressSpace() == 0;
3189 case Instruction::GetElementPtr:
3190 if (I->getType()->isPointerTy())
3192 break;
3193 case Instruction::BitCast: {
3194 // We need to be a bit careful here. We can only peek through the bitcast
3195 // if the scalar size of elements in the operand are smaller than and a
3196 // multiple of the size they are casting too. Take three cases:
3197 //
3198 // 1) Unsafe:
3199 // bitcast <2 x i16> %NonZero to <4 x i8>
3200 //
3201 // %NonZero can have 2 non-zero i16 elements, but isKnownNonZero on a
3202 // <4 x i8> requires that all 4 i8 elements be non-zero which isn't
3203 // guranteed (imagine just sign bit set in the 2 i16 elements).
3204 //
3205 // 2) Unsafe:
3206 // bitcast <4 x i3> %NonZero to <3 x i4>
3207 //
3208 // Even though the scalar size of the src (`i3`) is smaller than the
3209 // scalar size of the dst `i4`, because `i3` is not a multiple of `i4`
3210 // its possible for the `3 x i4` elements to be zero because there are
3211 // some elements in the destination that don't contain any full src
3212 // element.
3213 //
3214 // 3) Safe:
3215 // bitcast <4 x i8> %NonZero to <2 x i16>
3216 //
3217 // This is always safe as non-zero in the 4 i8 elements implies
3218 // non-zero in the combination of any two adjacent ones. Since i8 is a
3219 // multiple of i16, each i16 is guranteed to have 2 full i8 elements.
3220 // This all implies the 2 i16 elements are non-zero.
3221 Type *FromTy = I->getOperand(0)->getType();
3222 if ((FromTy->isIntOrIntVectorTy() || FromTy->isPtrOrPtrVectorTy()) &&
3223 (BitWidth % getBitWidth(FromTy->getScalarType(), Q.DL)) == 0)
3224 return isKnownNonZero(I->getOperand(0), Q, Depth);
3225 } break;
3226 case Instruction::IntToPtr:
3227 // Note that we have to take special care to avoid looking through
3228 // truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well
3229 // as casts that can alter the value, e.g., AddrSpaceCasts.
3230 if (!isa<ScalableVectorType>(I->getType()) &&
3231 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
3232 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
3233 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3234 break;
3235 case Instruction::PtrToAddr:
3236 // isKnownNonZero() for pointers refers to the address bits being non-zero,
3237 // so we can directly forward.
3238 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3239 case Instruction::PtrToInt:
3240 // For inttoptr, make sure the result size is >= the address size. If the
3241 // address is non-zero, any larger value is also non-zero.
3242 if (Q.DL.getAddressSizeInBits(I->getOperand(0)->getType()) <=
3243 I->getType()->getScalarSizeInBits())
3244 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3245 break;
3246 case Instruction::Trunc:
3247 // nuw/nsw trunc preserves zero/non-zero status of input.
3248 if (auto *TI = dyn_cast<TruncInst>(I))
3249 if (TI->hasNoSignedWrap() || TI->hasNoUnsignedWrap())
3250 return isKnownNonZero(TI->getOperand(0), DemandedElts, Q, Depth);
3251 break;
3252
3253 // Iff x - y != 0, then x ^ y != 0
3254 // Therefore we can do the same exact checks
3255 case Instruction::Xor:
3256 case Instruction::Sub:
3257 return isNonZeroSub(DemandedElts, Q, BitWidth, I->getOperand(0),
3258 I->getOperand(1), Depth);
3259 case Instruction::Or:
3260 // (X | (X != 0)) is non zero
3261 if (matchOpWithOpEqZero(I->getOperand(0), I->getOperand(1)))
3262 return true;
3263 // X | Y != 0 if X != Y.
3264 if (isKnownNonEqual(I->getOperand(0), I->getOperand(1), DemandedElts, Q,
3265 Depth))
3266 return true;
3267 // X | Y != 0 if X != 0 or Y != 0.
3268 return isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth) ||
3269 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3270 case Instruction::SExt:
3271 case Instruction::ZExt:
3272 // ext X != 0 if X != 0.
3273 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3274
3275 case Instruction::Shl: {
3276 // shl nsw/nuw can't remove any non-zero bits.
3278 if (Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO))
3279 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3280
3281 // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
3282 // if the lowest bit is shifted off the end.
3283 KnownBits Known(BitWidth);
3284 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth);
3285 if (Known.One[0])
3286 return true;
3287
3288 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3289 }
3290 case Instruction::LShr:
3291 case Instruction::AShr: {
3292 // shr exact can only shift out zero bits.
3294 if (BO->isExact())
3295 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3296
3297 // shr X, Y != 0 if X is negative. Note that the value of the shift is not
3298 // defined if the sign bit is shifted off the end.
3299 KnownBits Known =
3300 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3301 if (Known.isNegative())
3302 return true;
3303
3304 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3305 }
3306 case Instruction::UDiv:
3307 case Instruction::SDiv: {
3308 // X / Y
3309 // div exact can only produce a zero if the dividend is zero.
3310 if (cast<PossiblyExactOperator>(I)->isExact())
3311 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3312
3313 KnownBits XKnown =
3314 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3315 // If X is fully unknown we won't be able to figure anything out so don't
3316 // both computing knownbits for Y.
3317 if (XKnown.isUnknown())
3318 return false;
3319
3320 KnownBits YKnown =
3321 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
3322 if (I->getOpcode() == Instruction::SDiv) {
3323 // For signed division need to compare abs value of the operands.
3324 XKnown = XKnown.abs(/*IntMinIsPoison*/ false);
3325 YKnown = YKnown.abs(/*IntMinIsPoison*/ false);
3326 }
3327 // If X u>= Y then div is non zero (0/0 is UB).
3328 std::optional<bool> XUgeY = KnownBits::uge(XKnown, YKnown);
3329 // If X is total unknown or X u< Y we won't be able to prove non-zero
3330 // with compute known bits so just return early.
3331 return XUgeY && *XUgeY;
3332 }
3333 case Instruction::Add: {
3334 // X + Y.
3335
3336 // If Add has nuw wrap flag, then if either X or Y is non-zero the result is
3337 // non-zero.
3339 return isNonZeroAdd(DemandedElts, Q, BitWidth, I->getOperand(0),
3340 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3341 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3342 }
3343 case Instruction::Mul: {
3345 return isNonZeroMul(DemandedElts, Q, BitWidth, I->getOperand(0),
3346 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3347 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3348 }
3349 case Instruction::Select: {
3350 // (C ? X : Y) != 0 if X != 0 and Y != 0.
3351
3352 // First check if the arm is non-zero using `isKnownNonZero`. If that fails,
3353 // then see if the select condition implies the arm is non-zero. For example
3354 // (X != 0 ? X : Y), we know the true arm is non-zero as the `X` "return" is
3355 // dominated by `X != 0`.
3356 auto SelectArmIsNonZero = [&](bool IsTrueArm) {
3357 Value *Op;
3358 Op = IsTrueArm ? I->getOperand(1) : I->getOperand(2);
3359 // Op is trivially non-zero.
3360 if (isKnownNonZero(Op, DemandedElts, Q, Depth))
3361 return true;
3362
3363 // The condition of the select dominates the true/false arm. Check if the
3364 // condition implies that a given arm is non-zero.
3365 Value *X;
3366 CmpPredicate Pred;
3367 if (!match(I->getOperand(0), m_c_ICmp(Pred, m_Specific(Op), m_Value(X))))
3368 return false;
3369
3370 if (!IsTrueArm)
3371 Pred = ICmpInst::getInversePredicate(Pred);
3372
3373 return cmpExcludesZero(Pred, X);
3374 };
3375
3376 if (SelectArmIsNonZero(/* IsTrueArm */ true) &&
3377 SelectArmIsNonZero(/* IsTrueArm */ false))
3378 return true;
3379 break;
3380 }
3381 case Instruction::PHI: {
3382 auto *PN = cast<PHINode>(I);
3384 return true;
3385
3386 // Check if all incoming values are non-zero using recursion.
3388 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
3389 return llvm::all_of(PN->operands(), [&](const Use &U) {
3390 if (U.get() == PN)
3391 return true;
3392 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
3393 // Check if the branch on the phi excludes zero.
3394 CmpPredicate Pred;
3395 Value *X;
3396 BasicBlock *TrueSucc, *FalseSucc;
3397 if (match(RecQ.CxtI,
3398 m_Br(m_c_ICmp(Pred, m_Specific(U.get()), m_Value(X)),
3399 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
3400 // Check for cases of duplicate successors.
3401 if ((TrueSucc == PN->getParent()) != (FalseSucc == PN->getParent())) {
3402 // If we're using the false successor, invert the predicate.
3403 if (FalseSucc == PN->getParent())
3404 Pred = CmpInst::getInversePredicate(Pred);
3405 if (cmpExcludesZero(Pred, X))
3406 return true;
3407 }
3408 }
3409 // Finally recurse on the edge and check it directly.
3410 return isKnownNonZero(U.get(), DemandedElts, RecQ, NewDepth);
3411 });
3412 }
3413 case Instruction::InsertElement: {
3414 if (isa<ScalableVectorType>(I->getType()))
3415 break;
3416
3417 const Value *Vec = I->getOperand(0);
3418 const Value *Elt = I->getOperand(1);
3419 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
3420
3421 unsigned NumElts = DemandedElts.getBitWidth();
3422 APInt DemandedVecElts = DemandedElts;
3423 bool SkipElt = false;
3424 // If we know the index we are inserting too, clear it from Vec check.
3425 if (CIdx && CIdx->getValue().ult(NumElts)) {
3426 DemandedVecElts.clearBit(CIdx->getZExtValue());
3427 SkipElt = !DemandedElts[CIdx->getZExtValue()];
3428 }
3429
3430 // Result is zero if Elt is non-zero and rest of the demanded elts in Vec
3431 // are non-zero.
3432 return (SkipElt || isKnownNonZero(Elt, Q, Depth)) &&
3433 (DemandedVecElts.isZero() ||
3434 isKnownNonZero(Vec, DemandedVecElts, Q, Depth));
3435 }
3436 case Instruction::ExtractElement:
3437 if (const auto *EEI = dyn_cast<ExtractElementInst>(I)) {
3438 const Value *Vec = EEI->getVectorOperand();
3439 const Value *Idx = EEI->getIndexOperand();
3440 auto *CIdx = dyn_cast<ConstantInt>(Idx);
3441 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
3442 unsigned NumElts = VecTy->getNumElements();
3443 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
3444 if (CIdx && CIdx->getValue().ult(NumElts))
3445 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
3446 return isKnownNonZero(Vec, DemandedVecElts, Q, Depth);
3447 }
3448 }
3449 break;
3450 case Instruction::ShuffleVector: {
3451 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
3452 if (!Shuf)
3453 break;
3454 APInt DemandedLHS, DemandedRHS;
3455 // For undef elements, we don't know anything about the common state of
3456 // the shuffle result.
3457 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
3458 break;
3459 // If demanded elements for both vecs are non-zero, the shuffle is non-zero.
3460 return (DemandedRHS.isZero() ||
3461 isKnownNonZero(Shuf->getOperand(1), DemandedRHS, Q, Depth)) &&
3462 (DemandedLHS.isZero() ||
3463 isKnownNonZero(Shuf->getOperand(0), DemandedLHS, Q, Depth));
3464 }
3465 case Instruction::Freeze:
3466 return isKnownNonZero(I->getOperand(0), Q, Depth) &&
3467 isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
3468 Depth);
3469 case Instruction::Load: {
3470 auto *LI = cast<LoadInst>(I);
3471 // A Load tagged with nonnull or dereferenceable with null pointer undefined
3472 // is never null.
3473 if (auto *PtrT = dyn_cast<PointerType>(I->getType())) {
3474 if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull) ||
3475 (Q.IIQ.getMetadata(LI, LLVMContext::MD_dereferenceable) &&
3476 !NullPointerIsDefined(LI->getFunction(), PtrT->getAddressSpace())))
3477 return true;
3478 } else if (MDNode *Ranges = Q.IIQ.getMetadata(LI, LLVMContext::MD_range)) {
3480 }
3481
3482 // No need to fall through to computeKnownBits as range metadata is already
3483 // handled in isKnownNonZero.
3484 return false;
3485 }
3486 case Instruction::ExtractValue: {
3487 const WithOverflowInst *WO;
3489 switch (WO->getBinaryOp()) {
3490 default:
3491 break;
3492 case Instruction::Add:
3493 return isNonZeroAdd(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3494 WO->getArgOperand(1),
3495 /*NSW=*/false,
3496 /*NUW=*/false, Depth);
3497 case Instruction::Sub:
3498 return isNonZeroSub(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3499 WO->getArgOperand(1), Depth);
3500 case Instruction::Mul:
3501 return isNonZeroMul(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3502 WO->getArgOperand(1),
3503 /*NSW=*/false, /*NUW=*/false, Depth);
3504 break;
3505 }
3506 }
3507 break;
3508 }
3509 case Instruction::Call:
3510 case Instruction::Invoke: {
3511 const auto *Call = cast<CallBase>(I);
3512 if (I->getType()->isPointerTy()) {
3513 if (Call->isReturnNonNull())
3514 return true;
3515 if (const auto *RP = getArgumentAliasingToReturnedPointer(Call, true))
3516 return isKnownNonZero(RP, Q, Depth);
3517 } else {
3518 if (MDNode *Ranges = Q.IIQ.getMetadata(Call, LLVMContext::MD_range))
3520 if (std::optional<ConstantRange> Range = Call->getRange()) {
3521 const APInt ZeroValue(Range->getBitWidth(), 0);
3522 if (!Range->contains(ZeroValue))
3523 return true;
3524 }
3525 if (const Value *RV = Call->getReturnedArgOperand())
3526 if (RV->getType() == I->getType() && isKnownNonZero(RV, Q, Depth))
3527 return true;
3528 }
3529
3530 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
3531 switch (II->getIntrinsicID()) {
3532 case Intrinsic::sshl_sat:
3533 case Intrinsic::ushl_sat:
3534 case Intrinsic::abs:
3535 case Intrinsic::bitreverse:
3536 case Intrinsic::bswap:
3537 case Intrinsic::ctpop:
3538 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3539 // NB: We don't do usub_sat here as in any case we can prove its
3540 // non-zero, we will fold it to `sub nuw` in InstCombine.
3541 case Intrinsic::ssub_sat:
3542 return isNonZeroSub(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3543 II->getArgOperand(1), Depth);
3544 case Intrinsic::sadd_sat:
3545 return isNonZeroAdd(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3546 II->getArgOperand(1),
3547 /*NSW=*/true, /* NUW=*/false, Depth);
3548 // Vec reverse preserves zero/non-zero status from input vec.
3549 case Intrinsic::vector_reverse:
3550 return isKnownNonZero(II->getArgOperand(0), DemandedElts.reverseBits(),
3551 Q, Depth);
3552 // umin/smin/smax/smin/or of all non-zero elements is always non-zero.
3553 case Intrinsic::vector_reduce_or:
3554 case Intrinsic::vector_reduce_umax:
3555 case Intrinsic::vector_reduce_umin:
3556 case Intrinsic::vector_reduce_smax:
3557 case Intrinsic::vector_reduce_smin:
3558 return isKnownNonZero(II->getArgOperand(0), Q, Depth);
3559 case Intrinsic::umax:
3560 case Intrinsic::uadd_sat:
3561 // umax(X, (X != 0)) is non zero
3562 // X +usat (X != 0) is non zero
3563 if (matchOpWithOpEqZero(II->getArgOperand(0), II->getArgOperand(1)))
3564 return true;
3565
3566 return isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth) ||
3567 isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3568 case Intrinsic::smax: {
3569 // If either arg is strictly positive the result is non-zero. Otherwise
3570 // the result is non-zero if both ops are non-zero.
3571 auto IsNonZero = [&](Value *Op, std::optional<bool> &OpNonZero,
3572 const KnownBits &OpKnown) {
3573 if (!OpNonZero.has_value())
3574 OpNonZero = OpKnown.isNonZero() ||
3575 isKnownNonZero(Op, DemandedElts, Q, Depth);
3576 return *OpNonZero;
3577 };
3578 // Avoid re-computing isKnownNonZero.
3579 std::optional<bool> Op0NonZero, Op1NonZero;
3580 KnownBits Op1Known =
3581 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3582 if (Op1Known.isNonNegative() &&
3583 IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known))
3584 return true;
3585 KnownBits Op0Known =
3586 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3587 if (Op0Known.isNonNegative() &&
3588 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known))
3589 return true;
3590 return IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known) &&
3591 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known);
3592 }
3593 case Intrinsic::smin: {
3594 // If either arg is negative the result is non-zero. Otherwise
3595 // the result is non-zero if both ops are non-zero.
3596 KnownBits Op1Known =
3597 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3598 if (Op1Known.isNegative())
3599 return true;
3600 KnownBits Op0Known =
3601 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3602 if (Op0Known.isNegative())
3603 return true;
3604
3605 if (Op1Known.isNonZero() && Op0Known.isNonZero())
3606 return true;
3607 }
3608 [[fallthrough]];
3609 case Intrinsic::umin:
3610 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth) &&
3611 isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth);
3612 case Intrinsic::cttz:
3613 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3614 .Zero[0];
3615 case Intrinsic::ctlz:
3616 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3617 .isNonNegative();
3618 case Intrinsic::fshr:
3619 case Intrinsic::fshl:
3620 // If Op0 == Op1, this is a rotate. rotate(x, y) != 0 iff x != 0.
3621 if (II->getArgOperand(0) == II->getArgOperand(1))
3622 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3623 break;
3624 case Intrinsic::vscale:
3625 return true;
3626 case Intrinsic::experimental_get_vector_length:
3627 return isKnownNonZero(I->getOperand(0), Q, Depth);
3628 default:
3629 break;
3630 }
3631 break;
3632 }
3633
3634 return false;
3635 }
3636 }
3637
3638 KnownBits Known(BitWidth);
3639 computeKnownBits(I, DemandedElts, Known, Q, Depth);
3640 return Known.One != 0;
3641}
3642
3643/// Return true if the given value is known to be non-zero when defined. For
3644/// vectors, return true if every demanded element is known to be non-zero when
3645/// defined. For pointers, if the context instruction and dominator tree are
3646/// specified, perform context-sensitive analysis and return true if the
3647/// pointer couldn't possibly be null at the specified instruction.
3648/// Supports values with integer or pointer type and vectors of integers.
3649bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
3650 const SimplifyQuery &Q, unsigned Depth) {
3651 Type *Ty = V->getType();
3652
3653#ifndef NDEBUG
3654 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
3655
3656 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3657 assert(
3658 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
3659 "DemandedElt width should equal the fixed vector number of elements");
3660 } else {
3661 assert(DemandedElts == APInt(1, 1) &&
3662 "DemandedElt width should be 1 for scalars");
3663 }
3664#endif
3665
3666 if (auto *C = dyn_cast<Constant>(V)) {
3667 if (C->isNullValue())
3668 return false;
3669 if (isa<ConstantInt>(C))
3670 // Must be non-zero due to null test above.
3671 return true;
3672
3673 // For constant vectors, check that all elements are poison or known
3674 // non-zero to determine that the whole vector is known non-zero.
3675 if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
3676 for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
3677 if (!DemandedElts[i])
3678 continue;
3679 Constant *Elt = C->getAggregateElement(i);
3680 if (!Elt || Elt->isNullValue())
3681 return false;
3682 if (!isa<PoisonValue>(Elt) && !isa<ConstantInt>(Elt))
3683 return false;
3684 }
3685 return true;
3686 }
3687
3688 // Constant ptrauth can be null, iff the base pointer can be.
3689 if (auto *CPA = dyn_cast<ConstantPtrAuth>(V))
3690 return isKnownNonZero(CPA->getPointer(), DemandedElts, Q, Depth);
3691
3692 // A global variable in address space 0 is non null unless extern weak
3693 // or an absolute symbol reference. Other address spaces may have null as a
3694 // valid address for a global, so we can't assume anything.
3695 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
3696 if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&
3697 GV->getType()->getAddressSpace() == 0)
3698 return true;
3699 }
3700
3701 // For constant expressions, fall through to the Operator code below.
3702 if (!isa<ConstantExpr>(V))
3703 return false;
3704 }
3705
3706 if (const auto *A = dyn_cast<Argument>(V))
3707 if (std::optional<ConstantRange> Range = A->getRange()) {
3708 const APInt ZeroValue(Range->getBitWidth(), 0);
3709 if (!Range->contains(ZeroValue))
3710 return true;
3711 }
3712
3713 if (!isa<Constant>(V) && isKnownNonZeroFromAssume(V, Q))
3714 return true;
3715
3716 // Some of the tests below are recursive, so bail out if we hit the limit.
3718 return false;
3719
3720 // Check for pointer simplifications.
3721
3722 if (PointerType *PtrTy = dyn_cast<PointerType>(Ty)) {
3723 // A byval, inalloca may not be null in a non-default addres space. A
3724 // nonnull argument is assumed never 0.
3725 if (const Argument *A = dyn_cast<Argument>(V)) {
3726 if (((A->hasPassPointeeByValueCopyAttr() &&
3727 !NullPointerIsDefined(A->getParent(), PtrTy->getAddressSpace())) ||
3728 A->hasNonNullAttr()))
3729 return true;
3730 }
3731 }
3732
3733 if (const auto *I = dyn_cast<Operator>(V))
3734 if (isKnownNonZeroFromOperator(I, DemandedElts, Q, Depth))
3735 return true;
3736
3737 if (!isa<Constant>(V) &&
3739 return true;
3740
3741 if (const Value *Stripped = stripNullTest(V))
3742 return isKnownNonZero(Stripped, DemandedElts, Q, Depth);
3743
3744 return false;
3745}
3746
3748 unsigned Depth) {
3749 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
3750 APInt DemandedElts =
3751 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
3752 return ::isKnownNonZero(V, DemandedElts, Q, Depth);
3753}
3754
3755/// If the pair of operators are the same invertible function, return the
3756/// the operands of the function corresponding to each input. Otherwise,
3757/// return std::nullopt. An invertible function is one that is 1-to-1 and maps
3758/// every input value to exactly one output value. This is equivalent to
3759/// saying that Op1 and Op2 are equal exactly when the specified pair of
3760/// operands are equal, (except that Op1 and Op2 may be poison more often.)
3761static std::optional<std::pair<Value*, Value*>>
3763 const Operator *Op2) {
3764 if (Op1->getOpcode() != Op2->getOpcode())
3765 return std::nullopt;
3766
3767 auto getOperands = [&](unsigned OpNum) -> auto {
3768 return std::make_pair(Op1->getOperand(OpNum), Op2->getOperand(OpNum));
3769 };
3770
3771 switch (Op1->getOpcode()) {
3772 default:
3773 break;
3774 case Instruction::Or:
3775 if (!cast<PossiblyDisjointInst>(Op1)->isDisjoint() ||
3776 !cast<PossiblyDisjointInst>(Op2)->isDisjoint())
3777 break;
3778 [[fallthrough]];
3779 case Instruction::Xor:
3780 case Instruction::Add: {
3781 Value *Other;
3782 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(0)), m_Value(Other))))
3783 return std::make_pair(Op1->getOperand(1), Other);
3784 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(1)), m_Value(Other))))
3785 return std::make_pair(Op1->getOperand(0), Other);
3786 break;
3787 }
3788 case Instruction::Sub:
3789 if (Op1->getOperand(0) == Op2->getOperand(0))
3790 return getOperands(1);
3791 if (Op1->getOperand(1) == Op2->getOperand(1))
3792 return getOperands(0);
3793 break;
3794 case Instruction::Mul: {
3795 // invertible if A * B == (A * B) mod 2^N where A, and B are integers
3796 // and N is the bitwdith. The nsw case is non-obvious, but proven by
3797 // alive2: https://alive2.llvm.org/ce/z/Z6D5qK
3798 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3799 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3800 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3801 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3802 break;
3803
3804 // Assume operand order has been canonicalized
3805 if (Op1->getOperand(1) == Op2->getOperand(1) &&
3806 isa<ConstantInt>(Op1->getOperand(1)) &&
3807 !cast<ConstantInt>(Op1->getOperand(1))->isZero())
3808 return getOperands(0);
3809 break;
3810 }
3811 case Instruction::Shl: {
3812 // Same as multiplies, with the difference that we don't need to check
3813 // for a non-zero multiply. Shifts always multiply by non-zero.
3814 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3815 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3816 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3817 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3818 break;
3819
3820 if (Op1->getOperand(1) == Op2->getOperand(1))
3821 return getOperands(0);
3822 break;
3823 }
3824 case Instruction::AShr:
3825 case Instruction::LShr: {
3826 auto *PEO1 = cast<PossiblyExactOperator>(Op1);
3827 auto *PEO2 = cast<PossiblyExactOperator>(Op2);
3828 if (!PEO1->isExact() || !PEO2->isExact())
3829 break;
3830
3831 if (Op1->getOperand(1) == Op2->getOperand(1))
3832 return getOperands(0);
3833 break;
3834 }
3835 case Instruction::SExt:
3836 case Instruction::ZExt:
3837 if (Op1->getOperand(0)->getType() == Op2->getOperand(0)->getType())
3838 return getOperands(0);
3839 break;
3840 case Instruction::PHI: {
3841 const PHINode *PN1 = cast<PHINode>(Op1);
3842 const PHINode *PN2 = cast<PHINode>(Op2);
3843
3844 // If PN1 and PN2 are both recurrences, can we prove the entire recurrences
3845 // are a single invertible function of the start values? Note that repeated
3846 // application of an invertible function is also invertible
3847 BinaryOperator *BO1 = nullptr;
3848 Value *Start1 = nullptr, *Step1 = nullptr;
3849 BinaryOperator *BO2 = nullptr;
3850 Value *Start2 = nullptr, *Step2 = nullptr;
3851 if (PN1->getParent() != PN2->getParent() ||
3852 !matchSimpleRecurrence(PN1, BO1, Start1, Step1) ||
3853 !matchSimpleRecurrence(PN2, BO2, Start2, Step2))
3854 break;
3855
3856 auto Values = getInvertibleOperands(cast<Operator>(BO1),
3857 cast<Operator>(BO2));
3858 if (!Values)
3859 break;
3860
3861 // We have to be careful of mutually defined recurrences here. Ex:
3862 // * X_i = X_(i-1) OP Y_(i-1), and Y_i = X_(i-1) OP V
3863 // * X_i = Y_i = X_(i-1) OP Y_(i-1)
3864 // The invertibility of these is complicated, and not worth reasoning
3865 // about (yet?).
3866 if (Values->first != PN1 || Values->second != PN2)
3867 break;
3868
3869 return std::make_pair(Start1, Start2);
3870 }
3871 }
3872 return std::nullopt;
3873}
3874
3875/// Return true if V1 == (binop V2, X), where X is known non-zero.
3876/// Only handle a small subset of binops where (binop V2, X) with non-zero X
3877/// implies V2 != V1.
3878static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2,
3879 const APInt &DemandedElts,
3880 const SimplifyQuery &Q, unsigned Depth) {
3882 if (!BO)
3883 return false;
3884 switch (BO->getOpcode()) {
3885 default:
3886 break;
3887 case Instruction::Or:
3888 if (!cast<PossiblyDisjointInst>(V1)->isDisjoint())
3889 break;
3890 [[fallthrough]];
3891 case Instruction::Xor:
3892 case Instruction::Add:
3893 Value *Op = nullptr;
3894 if (V2 == BO->getOperand(0))
3895 Op = BO->getOperand(1);
3896 else if (V2 == BO->getOperand(1))
3897 Op = BO->getOperand(0);
3898 else
3899 return false;
3900 return isKnownNonZero(Op, DemandedElts, Q, Depth + 1);
3901 }
3902 return false;
3903}
3904
3905/// Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and
3906/// the multiplication is nuw or nsw.
3907static bool isNonEqualMul(const Value *V1, const Value *V2,
3908 const APInt &DemandedElts, const SimplifyQuery &Q,
3909 unsigned Depth) {
3910 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3911 const APInt *C;
3912 return match(OBO, m_Mul(m_Specific(V1), m_APInt(C))) &&
3913 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3914 !C->isZero() && !C->isOne() &&
3915 isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3916 }
3917 return false;
3918}
3919
3920/// Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and
3921/// the shift is nuw or nsw.
3922static bool isNonEqualShl(const Value *V1, const Value *V2,
3923 const APInt &DemandedElts, const SimplifyQuery &Q,
3924 unsigned Depth) {
3925 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3926 const APInt *C;
3927 return match(OBO, m_Shl(m_Specific(V1), m_APInt(C))) &&
3928 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3929 !C->isZero() && isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3930 }
3931 return false;
3932}
3933
3934static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2,
3935 const APInt &DemandedElts, const SimplifyQuery &Q,
3936 unsigned Depth) {
3937 // Check two PHIs are in same block.
3938 if (PN1->getParent() != PN2->getParent())
3939 return false;
3940
3942 bool UsedFullRecursion = false;
3943 for (const BasicBlock *IncomBB : PN1->blocks()) {
3944 if (!VisitedBBs.insert(IncomBB).second)
3945 continue; // Don't reprocess blocks that we have dealt with already.
3946 const Value *IV1 = PN1->getIncomingValueForBlock(IncomBB);
3947 const Value *IV2 = PN2->getIncomingValueForBlock(IncomBB);
3948 const APInt *C1, *C2;
3949 if (match(IV1, m_APInt(C1)) && match(IV2, m_APInt(C2)) && *C1 != *C2)
3950 continue;
3951
3952 // Only one pair of phi operands is allowed for full recursion.
3953 if (UsedFullRecursion)
3954 return false;
3955
3957 RecQ.CxtI = IncomBB->getTerminator();
3958 if (!isKnownNonEqual(IV1, IV2, DemandedElts, RecQ, Depth + 1))
3959 return false;
3960 UsedFullRecursion = true;
3961 }
3962 return true;
3963}
3964
3965static bool isNonEqualSelect(const Value *V1, const Value *V2,
3966 const APInt &DemandedElts, const SimplifyQuery &Q,
3967 unsigned Depth) {
3968 const SelectInst *SI1 = dyn_cast<SelectInst>(V1);
3969 if (!SI1)
3970 return false;
3971
3972 if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2)) {
3973 const Value *Cond1 = SI1->getCondition();
3974 const Value *Cond2 = SI2->getCondition();
3975 if (Cond1 == Cond2)
3976 return isKnownNonEqual(SI1->getTrueValue(), SI2->getTrueValue(),
3977 DemandedElts, Q, Depth + 1) &&
3978 isKnownNonEqual(SI1->getFalseValue(), SI2->getFalseValue(),
3979 DemandedElts, Q, Depth + 1);
3980 }
3981 return isKnownNonEqual(SI1->getTrueValue(), V2, DemandedElts, Q, Depth + 1) &&
3982 isKnownNonEqual(SI1->getFalseValue(), V2, DemandedElts, Q, Depth + 1);
3983}
3984
3985// Check to see if A is both a GEP and is the incoming value for a PHI in the
3986// loop, and B is either a ptr or another GEP. If the PHI has 2 incoming values,
3987// one of them being the recursive GEP A and the other a ptr at same base and at
3988// the same/higher offset than B we are only incrementing the pointer further in
3989// loop if offset of recursive GEP is greater than 0.
3991 const SimplifyQuery &Q) {
3992 if (!A->getType()->isPointerTy() || !B->getType()->isPointerTy())
3993 return false;
3994
3995 auto *GEPA = dyn_cast<GEPOperator>(A);
3996 if (!GEPA || GEPA->getNumIndices() != 1 || !isa<Constant>(GEPA->idx_begin()))
3997 return false;
3998
3999 // Handle 2 incoming PHI values with one being a recursive GEP.
4000 auto *PN = dyn_cast<PHINode>(GEPA->getPointerOperand());
4001 if (!PN || PN->getNumIncomingValues() != 2)
4002 return false;
4003
4004 // Search for the recursive GEP as an incoming operand, and record that as
4005 // Step.
4006 Value *Start = nullptr;
4007 Value *Step = const_cast<Value *>(A);
4008 if (PN->getIncomingValue(0) == Step)
4009 Start = PN->getIncomingValue(1);
4010 else if (PN->getIncomingValue(1) == Step)
4011 Start = PN->getIncomingValue(0);
4012 else
4013 return false;
4014
4015 // Other incoming node base should match the B base.
4016 // StartOffset >= OffsetB && StepOffset > 0?
4017 // StartOffset <= OffsetB && StepOffset < 0?
4018 // Is non-equal if above are true.
4019 // We use stripAndAccumulateInBoundsConstantOffsets to restrict the
4020 // optimisation to inbounds GEPs only.
4021 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(Start->getType());
4022 APInt StartOffset(IndexWidth, 0);
4023 Start = Start->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StartOffset);
4024 APInt StepOffset(IndexWidth, 0);
4025 Step = Step->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StepOffset);
4026
4027 // Check if Base Pointer of Step matches the PHI.
4028 if (Step != PN)
4029 return false;
4030 APInt OffsetB(IndexWidth, 0);
4031 B = B->stripAndAccumulateInBoundsConstantOffsets(Q.DL, OffsetB);
4032 return Start == B &&
4033 ((StartOffset.sge(OffsetB) && StepOffset.isStrictlyPositive()) ||
4034 (StartOffset.sle(OffsetB) && StepOffset.isNegative()));
4035}
4036
4037static bool isKnownNonEqualFromContext(const Value *V1, const Value *V2,
4038 const SimplifyQuery &Q, unsigned Depth) {
4039 if (!Q.CxtI)
4040 return false;
4041
4042 // Try to infer NonEqual based on information from dominating conditions.
4043 if (Q.DC && Q.DT) {
4044 auto IsKnownNonEqualFromDominatingCondition = [&](const Value *V) {
4045 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
4046 Value *Cond = BI->getCondition();
4047 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4048 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()) &&
4050 /*LHSIsTrue=*/true, Depth)
4051 .value_or(false))
4052 return true;
4053
4054 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4055 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()) &&
4057 /*LHSIsTrue=*/false, Depth)
4058 .value_or(false))
4059 return true;
4060 }
4061
4062 return false;
4063 };
4064
4065 if (IsKnownNonEqualFromDominatingCondition(V1) ||
4066 IsKnownNonEqualFromDominatingCondition(V2))
4067 return true;
4068 }
4069
4070 if (!Q.AC)
4071 return false;
4072
4073 // Try to infer NonEqual based on information from assumptions.
4074 for (auto &AssumeVH : Q.AC->assumptionsFor(V1)) {
4075 if (!AssumeVH)
4076 continue;
4077 CallInst *I = cast<CallInst>(AssumeVH);
4078
4079 assert(I->getFunction() == Q.CxtI->getFunction() &&
4080 "Got assumption for the wrong function!");
4081 assert(I->getIntrinsicID() == Intrinsic::assume &&
4082 "must be an assume intrinsic");
4083
4084 if (isImpliedCondition(I->getArgOperand(0), ICmpInst::ICMP_NE, V1, V2, Q.DL,
4085 /*LHSIsTrue=*/true, Depth)
4086 .value_or(false) &&
4088 return true;
4089 }
4090
4091 return false;
4092}
4093
4094/// Return true if it is known that V1 != V2.
4095static bool isKnownNonEqual(const Value *V1, const Value *V2,
4096 const APInt &DemandedElts, const SimplifyQuery &Q,
4097 unsigned Depth) {
4098 if (V1 == V2)
4099 return false;
4100 if (V1->getType() != V2->getType())
4101 // We can't look through casts yet.
4102 return false;
4103
4105 return false;
4106
4107 // See if we can recurse through (exactly one of) our operands. This
4108 // requires our operation be 1-to-1 and map every input value to exactly
4109 // one output value. Such an operation is invertible.
4110 auto *O1 = dyn_cast<Operator>(V1);
4111 auto *O2 = dyn_cast<Operator>(V2);
4112 if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) {
4113 if (auto Values = getInvertibleOperands(O1, O2))
4114 return isKnownNonEqual(Values->first, Values->second, DemandedElts, Q,
4115 Depth + 1);
4116
4117 if (const PHINode *PN1 = dyn_cast<PHINode>(V1)) {
4118 const PHINode *PN2 = cast<PHINode>(V2);
4119 // FIXME: This is missing a generalization to handle the case where one is
4120 // a PHI and another one isn't.
4121 if (isNonEqualPHIs(PN1, PN2, DemandedElts, Q, Depth))
4122 return true;
4123 };
4124 }
4125
4126 if (isModifyingBinopOfNonZero(V1, V2, DemandedElts, Q, Depth) ||
4127 isModifyingBinopOfNonZero(V2, V1, DemandedElts, Q, Depth))
4128 return true;
4129
4130 if (isNonEqualMul(V1, V2, DemandedElts, Q, Depth) ||
4131 isNonEqualMul(V2, V1, DemandedElts, Q, Depth))
4132 return true;
4133
4134 if (isNonEqualShl(V1, V2, DemandedElts, Q, Depth) ||
4135 isNonEqualShl(V2, V1, DemandedElts, Q, Depth))
4136 return true;
4137
4138 if (V1->getType()->isIntOrIntVectorTy()) {
4139 // Are any known bits in V1 contradictory to known bits in V2? If V1
4140 // has a known zero where V2 has a known one, they must not be equal.
4141 KnownBits Known1 = computeKnownBits(V1, DemandedElts, Q, Depth);
4142 if (!Known1.isUnknown()) {
4143 KnownBits Known2 = computeKnownBits(V2, DemandedElts, Q, Depth);
4144 if (Known1.Zero.intersects(Known2.One) ||
4145 Known2.Zero.intersects(Known1.One))
4146 return true;
4147 }
4148 }
4149
4150 if (isNonEqualSelect(V1, V2, DemandedElts, Q, Depth) ||
4151 isNonEqualSelect(V2, V1, DemandedElts, Q, Depth))
4152 return true;
4153
4154 if (isNonEqualPointersWithRecursiveGEP(V1, V2, Q) ||
4156 return true;
4157
4158 Value *A, *B;
4159 // PtrToInts are NonEqual if their Ptrs are NonEqual.
4160 // Check PtrToInt type matches the pointer size.
4161 if (match(V1, m_PtrToIntSameSize(Q.DL, m_Value(A))) &&
4163 return isKnownNonEqual(A, B, DemandedElts, Q, Depth + 1);
4164
4165 if (isKnownNonEqualFromContext(V1, V2, Q, Depth))
4166 return true;
4167
4168 return false;
4169}
4170
4171/// For vector constants, loop over the elements and find the constant with the
4172/// minimum number of sign bits. Return 0 if the value is not a vector constant
4173/// or if any element was not analyzed; otherwise, return the count for the
4174/// element with the minimum number of sign bits.
4176 const APInt &DemandedElts,
4177 unsigned TyBits) {
4178 const auto *CV = dyn_cast<Constant>(V);
4179 if (!CV || !isa<FixedVectorType>(CV->getType()))
4180 return 0;
4181
4182 unsigned MinSignBits = TyBits;
4183 unsigned NumElts = cast<FixedVectorType>(CV->getType())->getNumElements();
4184 for (unsigned i = 0; i != NumElts; ++i) {
4185 if (!DemandedElts[i])
4186 continue;
4187 // If we find a non-ConstantInt, bail out.
4188 auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));
4189 if (!Elt)
4190 return 0;
4191
4192 MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());
4193 }
4194
4195 return MinSignBits;
4196}
4197
4198static unsigned ComputeNumSignBitsImpl(const Value *V,
4199 const APInt &DemandedElts,
4200 const SimplifyQuery &Q, unsigned Depth);
4201
4202static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
4203 const SimplifyQuery &Q, unsigned Depth) {
4204 unsigned Result = ComputeNumSignBitsImpl(V, DemandedElts, Q, Depth);
4205 assert(Result > 0 && "At least one sign bit needs to be present!");
4206 return Result;
4207}
4208
4209/// Return the number of times the sign bit of the register is replicated into
4210/// the other bits. We know that at least 1 bit is always equal to the sign bit
4211/// (itself), but other cases can give us information. For example, immediately
4212/// after an "ashr X, 2", we know that the top 3 bits are all equal to each
4213/// other, so we return 3. For vectors, return the number of sign bits for the
4214/// vector element with the minimum number of known sign bits of the demanded
4215/// elements in the vector specified by DemandedElts.
4216static unsigned ComputeNumSignBitsImpl(const Value *V,
4217 const APInt &DemandedElts,
4218 const SimplifyQuery &Q, unsigned Depth) {
4219 Type *Ty = V->getType();
4220#ifndef NDEBUG
4221 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4222
4223 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
4224 assert(
4225 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
4226 "DemandedElt width should equal the fixed vector number of elements");
4227 } else {
4228 assert(DemandedElts == APInt(1, 1) &&
4229 "DemandedElt width should be 1 for scalars");
4230 }
4231#endif
4232
4233 // We return the minimum number of sign bits that are guaranteed to be present
4234 // in V, so for undef we have to conservatively return 1. We don't have the
4235 // same behavior for poison though -- that's a FIXME today.
4236
4237 Type *ScalarTy = Ty->getScalarType();
4238 unsigned TyBits = ScalarTy->isPointerTy() ?
4239 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
4240 Q.DL.getTypeSizeInBits(ScalarTy);
4241
4242 unsigned Tmp, Tmp2;
4243 unsigned FirstAnswer = 1;
4244
4245 // Note that ConstantInt is handled by the general computeKnownBits case
4246 // below.
4247
4249 return 1;
4250
4251 if (auto *U = dyn_cast<Operator>(V)) {
4252 switch (Operator::getOpcode(V)) {
4253 default: break;
4254 case Instruction::BitCast: {
4255 Value *Src = U->getOperand(0);
4256 Type *SrcTy = Src->getType();
4257
4258 // Skip if the source type is not an integer or integer vector type
4259 // This ensures we only process integer-like types
4260 if (!SrcTy->isIntOrIntVectorTy())
4261 break;
4262
4263 unsigned SrcBits = SrcTy->getScalarSizeInBits();
4264
4265 // Bitcast 'large element' scalar/vector to 'small element' vector.
4266 if ((SrcBits % TyBits) != 0)
4267 break;
4268
4269 // Only proceed if the destination type is a fixed-size vector
4270 if (isa<FixedVectorType>(Ty)) {
4271 // Fast case - sign splat can be simply split across the small elements.
4272 // This works for both vector and scalar sources
4273 Tmp = ComputeNumSignBits(Src, Q, Depth + 1);
4274 if (Tmp == SrcBits)
4275 return TyBits;
4276 }
4277 break;
4278 }
4279 case Instruction::SExt:
4280 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
4281 return ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1) +
4282 Tmp;
4283
4284 case Instruction::SDiv: {
4285 const APInt *Denominator;
4286 // sdiv X, C -> adds log(C) sign bits.
4287 if (match(U->getOperand(1), m_APInt(Denominator))) {
4288
4289 // Ignore non-positive denominator.
4290 if (!Denominator->isStrictlyPositive())
4291 break;
4292
4293 // Calculate the incoming numerator bits.
4294 unsigned NumBits =
4295 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4296
4297 // Add floor(log(C)) bits to the numerator bits.
4298 return std::min(TyBits, NumBits + Denominator->logBase2());
4299 }
4300 break;
4301 }
4302
4303 case Instruction::SRem: {
4304 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4305
4306 const APInt *Denominator;
4307 // srem X, C -> we know that the result is within [-C+1,C) when C is a
4308 // positive constant. This let us put a lower bound on the number of sign
4309 // bits.
4310 if (match(U->getOperand(1), m_APInt(Denominator))) {
4311
4312 // Ignore non-positive denominator.
4313 if (Denominator->isStrictlyPositive()) {
4314 // Calculate the leading sign bit constraints by examining the
4315 // denominator. Given that the denominator is positive, there are two
4316 // cases:
4317 //
4318 // 1. The numerator is positive. The result range is [0,C) and
4319 // [0,C) u< (1 << ceilLogBase2(C)).
4320 //
4321 // 2. The numerator is negative. Then the result range is (-C,0] and
4322 // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
4323 //
4324 // Thus a lower bound on the number of sign bits is `TyBits -
4325 // ceilLogBase2(C)`.
4326
4327 unsigned ResBits = TyBits - Denominator->ceilLogBase2();
4328 Tmp = std::max(Tmp, ResBits);
4329 }
4330 }
4331 return Tmp;
4332 }
4333
4334 case Instruction::AShr: {
4335 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4336 // ashr X, C -> adds C sign bits. Vectors too.
4337 const APInt *ShAmt;
4338 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4339 if (ShAmt->uge(TyBits))
4340 break; // Bad shift.
4341 unsigned ShAmtLimited = ShAmt->getZExtValue();
4342 Tmp += ShAmtLimited;
4343 if (Tmp > TyBits) Tmp = TyBits;
4344 }
4345 return Tmp;
4346 }
4347 case Instruction::Shl: {
4348 const APInt *ShAmt;
4349 Value *X = nullptr;
4350 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4351 // shl destroys sign bits.
4352 if (ShAmt->uge(TyBits))
4353 break; // Bad shift.
4354 // We can look through a zext (more or less treating it as a sext) if
4355 // all extended bits are shifted out.
4356 if (match(U->getOperand(0), m_ZExt(m_Value(X))) &&
4357 ShAmt->uge(TyBits - X->getType()->getScalarSizeInBits())) {
4358 Tmp = ComputeNumSignBits(X, DemandedElts, Q, Depth + 1);
4359 Tmp += TyBits - X->getType()->getScalarSizeInBits();
4360 } else
4361 Tmp =
4362 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4363 if (ShAmt->uge(Tmp))
4364 break; // Shifted all sign bits out.
4365 Tmp2 = ShAmt->getZExtValue();
4366 return Tmp - Tmp2;
4367 }
4368 break;
4369 }
4370 case Instruction::And:
4371 case Instruction::Or:
4372 case Instruction::Xor: // NOT is handled here.
4373 // Logical binary ops preserve the number of sign bits at the worst.
4374 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4375 if (Tmp != 1) {
4376 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4377 FirstAnswer = std::min(Tmp, Tmp2);
4378 // We computed what we know about the sign bits as our first
4379 // answer. Now proceed to the generic code that uses
4380 // computeKnownBits, and pick whichever answer is better.
4381 }
4382 break;
4383
4384 case Instruction::Select: {
4385 // If we have a clamp pattern, we know that the number of sign bits will
4386 // be the minimum of the clamp min/max range.
4387 const Value *X;
4388 const APInt *CLow, *CHigh;
4389 if (isSignedMinMaxClamp(U, X, CLow, CHigh))
4390 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4391
4392 Tmp = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4393 if (Tmp == 1)
4394 break;
4395 Tmp2 = ComputeNumSignBits(U->getOperand(2), DemandedElts, Q, Depth + 1);
4396 return std::min(Tmp, Tmp2);
4397 }
4398
4399 case Instruction::Add:
4400 // Add can have at most one carry bit. Thus we know that the output
4401 // is, at worst, one more bit than the inputs.
4402 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4403 if (Tmp == 1) break;
4404
4405 // Special case decrementing a value (ADD X, -1):
4406 if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
4407 if (CRHS->isAllOnesValue()) {
4408 KnownBits Known(TyBits);
4409 computeKnownBits(U->getOperand(0), DemandedElts, Known, Q, Depth + 1);
4410
4411 // If the input is known to be 0 or 1, the output is 0/-1, which is
4412 // all sign bits set.
4413 if ((Known.Zero | 1).isAllOnes())
4414 return TyBits;
4415
4416 // If we are subtracting one from a positive number, there is no carry
4417 // out of the result.
4418 if (Known.isNonNegative())
4419 return Tmp;
4420 }
4421
4422 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4423 if (Tmp2 == 1)
4424 break;
4425 return std::min(Tmp, Tmp2) - 1;
4426
4427 case Instruction::Sub:
4428 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4429 if (Tmp2 == 1)
4430 break;
4431
4432 // Handle NEG.
4433 if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
4434 if (CLHS->isNullValue()) {
4435 KnownBits Known(TyBits);
4436 computeKnownBits(U->getOperand(1), DemandedElts, Known, Q, Depth + 1);
4437 // If the input is known to be 0 or 1, the output is 0/-1, which is
4438 // all sign bits set.
4439 if ((Known.Zero | 1).isAllOnes())
4440 return TyBits;
4441
4442 // If the input is known to be positive (the sign bit is known clear),
4443 // the output of the NEG has the same number of sign bits as the
4444 // input.
4445 if (Known.isNonNegative())
4446 return Tmp2;
4447
4448 // Otherwise, we treat this like a SUB.
4449 }
4450
4451 // Sub can have at most one carry bit. Thus we know that the output
4452 // is, at worst, one more bit than the inputs.
4453 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4454 if (Tmp == 1)
4455 break;
4456 return std::min(Tmp, Tmp2) - 1;
4457
4458 case Instruction::Mul: {
4459 // The output of the Mul can be at most twice the valid bits in the
4460 // inputs.
4461 unsigned SignBitsOp0 =
4462 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4463 if (SignBitsOp0 == 1)
4464 break;
4465 unsigned SignBitsOp1 =
4466 ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4467 if (SignBitsOp1 == 1)
4468 break;
4469 unsigned OutValidBits =
4470 (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);
4471 return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
4472 }
4473
4474 case Instruction::PHI: {
4475 const PHINode *PN = cast<PHINode>(U);
4476 unsigned NumIncomingValues = PN->getNumIncomingValues();
4477 // Don't analyze large in-degree PHIs.
4478 if (NumIncomingValues > 4) break;
4479 // Unreachable blocks may have zero-operand PHI nodes.
4480 if (NumIncomingValues == 0) break;
4481
4482 // Take the minimum of all incoming values. This can't infinitely loop
4483 // because of our depth threshold.
4485 Tmp = TyBits;
4486 for (unsigned i = 0, e = NumIncomingValues; i != e; ++i) {
4487 if (Tmp == 1) return Tmp;
4488 RecQ.CxtI = PN->getIncomingBlock(i)->getTerminator();
4489 Tmp = std::min(Tmp, ComputeNumSignBits(PN->getIncomingValue(i),
4490 DemandedElts, RecQ, Depth + 1));
4491 }
4492 return Tmp;
4493 }
4494
4495 case Instruction::Trunc: {
4496 // If the input contained enough sign bits that some remain after the
4497 // truncation, then we can make use of that. Otherwise we don't know
4498 // anything.
4499 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4500 unsigned OperandTyBits = U->getOperand(0)->getType()->getScalarSizeInBits();
4501 if (Tmp > (OperandTyBits - TyBits))
4502 return Tmp - (OperandTyBits - TyBits);
4503
4504 return 1;
4505 }
4506
4507 case Instruction::ExtractElement:
4508 // Look through extract element. At the moment we keep this simple and
4509 // skip tracking the specific element. But at least we might find
4510 // information valid for all elements of the vector (for example if vector
4511 // is sign extended, shifted, etc).
4512 return ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4513
4514 case Instruction::ShuffleVector: {
4515 // Collect the minimum number of sign bits that are shared by every vector
4516 // element referenced by the shuffle.
4517 auto *Shuf = dyn_cast<ShuffleVectorInst>(U);
4518 if (!Shuf) {
4519 // FIXME: Add support for shufflevector constant expressions.
4520 return 1;
4521 }
4522 APInt DemandedLHS, DemandedRHS;
4523 // For undef elements, we don't know anything about the common state of
4524 // the shuffle result.
4525 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
4526 return 1;
4527 Tmp = std::numeric_limits<unsigned>::max();
4528 if (!!DemandedLHS) {
4529 const Value *LHS = Shuf->getOperand(0);
4530 Tmp = ComputeNumSignBits(LHS, DemandedLHS, Q, Depth + 1);
4531 }
4532 // If we don't know anything, early out and try computeKnownBits
4533 // fall-back.
4534 if (Tmp == 1)
4535 break;
4536 if (!!DemandedRHS) {
4537 const Value *RHS = Shuf->getOperand(1);
4538 Tmp2 = ComputeNumSignBits(RHS, DemandedRHS, Q, Depth + 1);
4539 Tmp = std::min(Tmp, Tmp2);
4540 }
4541 // If we don't know anything, early out and try computeKnownBits
4542 // fall-back.
4543 if (Tmp == 1)
4544 break;
4545 assert(Tmp <= TyBits && "Failed to determine minimum sign bits");
4546 return Tmp;
4547 }
4548 case Instruction::Call: {
4549 if (const auto *II = dyn_cast<IntrinsicInst>(U)) {
4550 switch (II->getIntrinsicID()) {
4551 default:
4552 break;
4553 case Intrinsic::abs:
4554 Tmp =
4555 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4556 if (Tmp == 1)
4557 break;
4558
4559 // Absolute value reduces number of sign bits by at most 1.
4560 return Tmp - 1;
4561 case Intrinsic::smin:
4562 case Intrinsic::smax: {
4563 const APInt *CLow, *CHigh;
4564 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
4565 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4566 }
4567 }
4568 }
4569 }
4570 }
4571 }
4572
4573 // Finally, if we can prove that the top bits of the result are 0's or 1's,
4574 // use this information.
4575
4576 // If we can examine all elements of a vector constant successfully, we're
4577 // done (we can't do any better than that). If not, keep trying.
4578 if (unsigned VecSignBits =
4579 computeNumSignBitsVectorConstant(V, DemandedElts, TyBits))
4580 return VecSignBits;
4581
4582 KnownBits Known(TyBits);
4583 computeKnownBits(V, DemandedElts, Known, Q, Depth);
4584
4585 // If we know that the sign bit is either zero or one, determine the number of
4586 // identical bits in the top of the input value.
4587 return std::max(FirstAnswer, Known.countMinSignBits());
4588}
4589
4591 const TargetLibraryInfo *TLI) {
4592 const Function *F = CB.getCalledFunction();
4593 if (!F)
4595
4596 if (F->isIntrinsic())
4597 return F->getIntrinsicID();
4598
4599 // We are going to infer semantics of a library function based on mapping it
4600 // to an LLVM intrinsic. Check that the library function is available from
4601 // this callbase and in this environment.
4602 LibFunc Func;
4603 if (F->hasLocalLinkage() || !TLI || !TLI->getLibFunc(CB, Func) ||
4604 !CB.onlyReadsMemory())
4606
4607 switch (Func) {
4608 default:
4609 break;
4610 case LibFunc_sin:
4611 case LibFunc_sinf:
4612 case LibFunc_sinl:
4613 return Intrinsic::sin;
4614 case LibFunc_cos:
4615 case LibFunc_cosf:
4616 case LibFunc_cosl:
4617 return Intrinsic::cos;
4618 case LibFunc_tan:
4619 case LibFunc_tanf:
4620 case LibFunc_tanl:
4621 return Intrinsic::tan;
4622 case LibFunc_asin:
4623 case LibFunc_asinf:
4624 case LibFunc_asinl:
4625 return Intrinsic::asin;
4626 case LibFunc_acos:
4627 case LibFunc_acosf:
4628 case LibFunc_acosl:
4629 return Intrinsic::acos;
4630 case LibFunc_atan:
4631 case LibFunc_atanf:
4632 case LibFunc_atanl:
4633 return Intrinsic::atan;
4634 case LibFunc_atan2:
4635 case LibFunc_atan2f:
4636 case LibFunc_atan2l:
4637 return Intrinsic::atan2;
4638 case LibFunc_sinh:
4639 case LibFunc_sinhf:
4640 case LibFunc_sinhl:
4641 return Intrinsic::sinh;
4642 case LibFunc_cosh:
4643 case LibFunc_coshf:
4644 case LibFunc_coshl:
4645 return Intrinsic::cosh;
4646 case LibFunc_tanh:
4647 case LibFunc_tanhf:
4648 case LibFunc_tanhl:
4649 return Intrinsic::tanh;
4650 case LibFunc_exp:
4651 case LibFunc_expf:
4652 case LibFunc_expl:
4653 return Intrinsic::exp;
4654 case LibFunc_exp2:
4655 case LibFunc_exp2f:
4656 case LibFunc_exp2l:
4657 return Intrinsic::exp2;
4658 case LibFunc_exp10:
4659 case LibFunc_exp10f:
4660 case LibFunc_exp10l:
4661 return Intrinsic::exp10;
4662 case LibFunc_log:
4663 case LibFunc_logf:
4664 case LibFunc_logl:
4665 return Intrinsic::log;
4666 case LibFunc_log10:
4667 case LibFunc_log10f:
4668 case LibFunc_log10l:
4669 return Intrinsic::log10;
4670 case LibFunc_log2:
4671 case LibFunc_log2f:
4672 case LibFunc_log2l:
4673 return Intrinsic::log2;
4674 case LibFunc_fabs:
4675 case LibFunc_fabsf:
4676 case LibFunc_fabsl:
4677 return Intrinsic::fabs;
4678 case LibFunc_fmin:
4679 case LibFunc_fminf:
4680 case LibFunc_fminl:
4681 return Intrinsic::minnum;
4682 case LibFunc_fmax:
4683 case LibFunc_fmaxf:
4684 case LibFunc_fmaxl:
4685 return Intrinsic::maxnum;
4686 case LibFunc_copysign:
4687 case LibFunc_copysignf:
4688 case LibFunc_copysignl:
4689 return Intrinsic::copysign;
4690 case LibFunc_floor:
4691 case LibFunc_floorf:
4692 case LibFunc_floorl:
4693 return Intrinsic::floor;
4694 case LibFunc_ceil:
4695 case LibFunc_ceilf:
4696 case LibFunc_ceill:
4697 return Intrinsic::ceil;
4698 case LibFunc_trunc:
4699 case LibFunc_truncf:
4700 case LibFunc_truncl:
4701 return Intrinsic::trunc;
4702 case LibFunc_rint:
4703 case LibFunc_rintf:
4704 case LibFunc_rintl:
4705 return Intrinsic::rint;
4706 case LibFunc_nearbyint:
4707 case LibFunc_nearbyintf:
4708 case LibFunc_nearbyintl:
4709 return Intrinsic::nearbyint;
4710 case LibFunc_round:
4711 case LibFunc_roundf:
4712 case LibFunc_roundl:
4713 return Intrinsic::round;
4714 case LibFunc_roundeven:
4715 case LibFunc_roundevenf:
4716 case LibFunc_roundevenl:
4717 return Intrinsic::roundeven;
4718 case LibFunc_pow:
4719 case LibFunc_powf:
4720 case LibFunc_powl:
4721 return Intrinsic::pow;
4722 case LibFunc_sqrt:
4723 case LibFunc_sqrtf:
4724 case LibFunc_sqrtl:
4725 return Intrinsic::sqrt;
4726 }
4727
4729}
4730
4731static bool outputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty) {
4732 Ty = Ty->getScalarType();
4733 DenormalMode Mode = F.getDenormalMode(Ty->getFltSemantics());
4734 return Mode.Output == DenormalMode::IEEE ||
4736}
4737/// Given an exploded icmp instruction, return true if the comparison only
4738/// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if
4739/// the result of the comparison is true when the input value is signed.
4741 bool &TrueIfSigned) {
4742 switch (Pred) {
4743 case ICmpInst::ICMP_SLT: // True if LHS s< 0
4744 TrueIfSigned = true;
4745 return RHS.isZero();
4746 case ICmpInst::ICMP_SLE: // True if LHS s<= -1
4747 TrueIfSigned = true;
4748 return RHS.isAllOnes();
4749 case ICmpInst::ICMP_SGT: // True if LHS s> -1
4750 TrueIfSigned = false;
4751 return RHS.isAllOnes();
4752 case ICmpInst::ICMP_SGE: // True if LHS s>= 0
4753 TrueIfSigned = false;
4754 return RHS.isZero();
4755 case ICmpInst::ICMP_UGT:
4756 // True if LHS u> RHS and RHS == sign-bit-mask - 1
4757 TrueIfSigned = true;
4758 return RHS.isMaxSignedValue();
4759 case ICmpInst::ICMP_UGE:
4760 // True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4761 TrueIfSigned = true;
4762 return RHS.isMinSignedValue();
4763 case ICmpInst::ICMP_ULT:
4764 // True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4765 TrueIfSigned = false;
4766 return RHS.isMinSignedValue();
4767 case ICmpInst::ICMP_ULE:
4768 // True if LHS u<= RHS and RHS == sign-bit-mask - 1
4769 TrueIfSigned = false;
4770 return RHS.isMaxSignedValue();
4771 default:
4772 return false;
4773 }
4774}
4775
4777 bool CondIsTrue,
4778 const Instruction *CxtI,
4779 KnownFPClass &KnownFromContext,
4780 unsigned Depth = 0) {
4781 Value *A, *B;
4783 (CondIsTrue ? match(Cond, m_LogicalAnd(m_Value(A), m_Value(B)))
4784 : match(Cond, m_LogicalOr(m_Value(A), m_Value(B))))) {
4785 computeKnownFPClassFromCond(V, A, CondIsTrue, CxtI, KnownFromContext,
4786 Depth + 1);
4787 computeKnownFPClassFromCond(V, B, CondIsTrue, CxtI, KnownFromContext,
4788 Depth + 1);
4789 return;
4790 }
4792 computeKnownFPClassFromCond(V, A, !CondIsTrue, CxtI, KnownFromContext,
4793 Depth + 1);
4794 return;
4795 }
4796 CmpPredicate Pred;
4797 Value *LHS;
4798 uint64_t ClassVal = 0;
4799 const APFloat *CRHS;
4800 const APInt *RHS;
4801 if (match(Cond, m_FCmp(Pred, m_Value(LHS), m_APFloat(CRHS)))) {
4802 auto [CmpVal, MaskIfTrue, MaskIfFalse] = fcmpImpliesClass(
4803 Pred, *CxtI->getParent()->getParent(), LHS, *CRHS, LHS != V);
4804 if (CmpVal == V)
4805 KnownFromContext.knownNot(~(CondIsTrue ? MaskIfTrue : MaskIfFalse));
4807 m_Specific(V), m_ConstantInt(ClassVal)))) {
4808 FPClassTest Mask = static_cast<FPClassTest>(ClassVal);
4809 KnownFromContext.knownNot(CondIsTrue ? ~Mask : Mask);
4810 } else if (match(Cond, m_ICmp(Pred, m_ElementWiseBitCast(m_Specific(V)),
4811 m_APInt(RHS)))) {
4812 bool TrueIfSigned;
4813 if (!isSignBitCheck(Pred, *RHS, TrueIfSigned))
4814 return;
4815 if (TrueIfSigned == CondIsTrue)
4816 KnownFromContext.signBitMustBeOne();
4817 else
4818 KnownFromContext.signBitMustBeZero();
4819 }
4820}
4821
4823 const SimplifyQuery &Q) {
4824 KnownFPClass KnownFromContext;
4825
4826 if (Q.CC && Q.CC->AffectedValues.contains(V))
4828 KnownFromContext);
4829
4830 if (!Q.CxtI)
4831 return KnownFromContext;
4832
4833 if (Q.DC && Q.DT) {
4834 // Handle dominating conditions.
4835 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
4836 Value *Cond = BI->getCondition();
4837
4838 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4839 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
4840 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/true, Q.CxtI,
4841 KnownFromContext);
4842
4843 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4844 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
4845 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/false, Q.CxtI,
4846 KnownFromContext);
4847 }
4848 }
4849
4850 if (!Q.AC)
4851 return KnownFromContext;
4852
4853 // Try to restrict the floating-point classes based on information from
4854 // assumptions.
4855 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
4856 if (!AssumeVH)
4857 continue;
4858 CallInst *I = cast<CallInst>(AssumeVH);
4859
4860 assert(I->getFunction() == Q.CxtI->getParent()->getParent() &&
4861 "Got assumption for the wrong function!");
4862 assert(I->getIntrinsicID() == Intrinsic::assume &&
4863 "must be an assume intrinsic");
4864
4865 if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
4866 continue;
4867
4868 computeKnownFPClassFromCond(V, I->getArgOperand(0),
4869 /*CondIsTrue=*/true, Q.CxtI, KnownFromContext);
4870 }
4871
4872 return KnownFromContext;
4873}
4874
4875void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4876 FPClassTest InterestedClasses, KnownFPClass &Known,
4877 const SimplifyQuery &Q, unsigned Depth);
4878
4879static void computeKnownFPClass(const Value *V, KnownFPClass &Known,
4880 FPClassTest InterestedClasses,
4881 const SimplifyQuery &Q, unsigned Depth) {
4882 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
4883 APInt DemandedElts =
4884 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
4885 computeKnownFPClass(V, DemandedElts, InterestedClasses, Known, Q, Depth);
4886}
4887
4889 const APInt &DemandedElts,
4890 FPClassTest InterestedClasses,
4891 KnownFPClass &Known,
4892 const SimplifyQuery &Q,
4893 unsigned Depth) {
4894 if ((InterestedClasses &
4896 return;
4897
4898 KnownFPClass KnownSrc;
4899 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
4900 KnownSrc, Q, Depth + 1);
4901
4902 // Sign should be preserved
4903 // TODO: Handle cannot be ordered greater than zero
4904 if (KnownSrc.cannotBeOrderedLessThanZero())
4906
4907 Known.propagateNaN(KnownSrc, true);
4908
4909 // Infinity needs a range check.
4910}
4911
4912void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4913 FPClassTest InterestedClasses, KnownFPClass &Known,
4914 const SimplifyQuery &Q, unsigned Depth) {
4915 assert(Known.isUnknown() && "should not be called with known information");
4916
4917 if (!DemandedElts) {
4918 // No demanded elts, better to assume we don't know anything.
4919 Known.resetAll();
4920 return;
4921 }
4922
4923 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4924
4925 if (auto *CFP = dyn_cast<ConstantFP>(V)) {
4926 Known = KnownFPClass(CFP->getValueAPF());
4927 return;
4928 }
4929
4931 Known.KnownFPClasses = fcPosZero;
4932 Known.SignBit = false;
4933 return;
4934 }
4935
4936 if (isa<PoisonValue>(V)) {
4937 Known.KnownFPClasses = fcNone;
4938 Known.SignBit = false;
4939 return;
4940 }
4941
4942 // Try to handle fixed width vector constants
4943 auto *VFVTy = dyn_cast<FixedVectorType>(V->getType());
4944 const Constant *CV = dyn_cast<Constant>(V);
4945 if (VFVTy && CV) {
4946 Known.KnownFPClasses = fcNone;
4947 bool SignBitAllZero = true;
4948 bool SignBitAllOne = true;
4949
4950 // For vectors, verify that each element is not NaN.
4951 unsigned NumElts = VFVTy->getNumElements();
4952 for (unsigned i = 0; i != NumElts; ++i) {
4953 if (!DemandedElts[i])
4954 continue;
4955
4956 Constant *Elt = CV->getAggregateElement(i);
4957 if (!Elt) {
4958 Known = KnownFPClass();
4959 return;
4960 }
4961 if (isa<PoisonValue>(Elt))
4962 continue;
4963 auto *CElt = dyn_cast<ConstantFP>(Elt);
4964 if (!CElt) {
4965 Known = KnownFPClass();
4966 return;
4967 }
4968
4969 const APFloat &C = CElt->getValueAPF();
4970 Known.KnownFPClasses |= C.classify();
4971 if (C.isNegative())
4972 SignBitAllZero = false;
4973 else
4974 SignBitAllOne = false;
4975 }
4976 if (SignBitAllOne != SignBitAllZero)
4977 Known.SignBit = SignBitAllOne;
4978 return;
4979 }
4980
4981 FPClassTest KnownNotFromFlags = fcNone;
4982 if (const auto *CB = dyn_cast<CallBase>(V))
4983 KnownNotFromFlags |= CB->getRetNoFPClass();
4984 else if (const auto *Arg = dyn_cast<Argument>(V))
4985 KnownNotFromFlags |= Arg->getNoFPClass();
4986
4987 const Operator *Op = dyn_cast<Operator>(V);
4989 if (FPOp->hasNoNaNs())
4990 KnownNotFromFlags |= fcNan;
4991 if (FPOp->hasNoInfs())
4992 KnownNotFromFlags |= fcInf;
4993 }
4994
4995 KnownFPClass AssumedClasses = computeKnownFPClassFromContext(V, Q);
4996 KnownNotFromFlags |= ~AssumedClasses.KnownFPClasses;
4997
4998 // We no longer need to find out about these bits from inputs if we can
4999 // assume this from flags/attributes.
5000 InterestedClasses &= ~KnownNotFromFlags;
5001
5002 auto ClearClassesFromFlags = make_scope_exit([=, &Known] {
5003 Known.knownNot(KnownNotFromFlags);
5004 if (!Known.SignBit && AssumedClasses.SignBit) {
5005 if (*AssumedClasses.SignBit)
5006 Known.signBitMustBeOne();
5007 else
5008 Known.signBitMustBeZero();
5009 }
5010 });
5011
5012 if (!Op)
5013 return;
5014
5015 // All recursive calls that increase depth must come after this.
5017 return;
5018
5019 const unsigned Opc = Op->getOpcode();
5020 switch (Opc) {
5021 case Instruction::FNeg: {
5022 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5023 Known, Q, Depth + 1);
5024 Known.fneg();
5025 break;
5026 }
5027 case Instruction::Select: {
5028 Value *Cond = Op->getOperand(0);
5029 Value *LHS = Op->getOperand(1);
5030 Value *RHS = Op->getOperand(2);
5031
5032 FPClassTest FilterLHS = fcAllFlags;
5033 FPClassTest FilterRHS = fcAllFlags;
5034
5035 Value *TestedValue = nullptr;
5036 FPClassTest MaskIfTrue = fcAllFlags;
5037 FPClassTest MaskIfFalse = fcAllFlags;
5038 uint64_t ClassVal = 0;
5039 const Function *F = cast<Instruction>(Op)->getFunction();
5040 CmpPredicate Pred;
5041 Value *CmpLHS, *CmpRHS;
5042 if (F && match(Cond, m_FCmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS)))) {
5043 // If the select filters out a value based on the class, it no longer
5044 // participates in the class of the result
5045
5046 // TODO: In some degenerate cases we can infer something if we try again
5047 // without looking through sign operations.
5048 bool LookThroughFAbsFNeg = CmpLHS != LHS && CmpLHS != RHS;
5049 std::tie(TestedValue, MaskIfTrue, MaskIfFalse) =
5050 fcmpImpliesClass(Pred, *F, CmpLHS, CmpRHS, LookThroughFAbsFNeg);
5051 } else if (match(Cond,
5053 m_Value(TestedValue), m_ConstantInt(ClassVal)))) {
5054 FPClassTest TestedMask = static_cast<FPClassTest>(ClassVal);
5055 MaskIfTrue = TestedMask;
5056 MaskIfFalse = ~TestedMask;
5057 }
5058
5059 if (TestedValue == LHS) {
5060 // match !isnan(x) ? x : y
5061 FilterLHS = MaskIfTrue;
5062 } else if (TestedValue == RHS) { // && IsExactClass
5063 // match !isnan(x) ? y : x
5064 FilterRHS = MaskIfFalse;
5065 }
5066
5067 KnownFPClass Known2;
5068 computeKnownFPClass(LHS, DemandedElts, InterestedClasses & FilterLHS, Known,
5069 Q, Depth + 1);
5070 Known.KnownFPClasses &= FilterLHS;
5071
5072 computeKnownFPClass(RHS, DemandedElts, InterestedClasses & FilterRHS,
5073 Known2, Q, Depth + 1);
5074 Known2.KnownFPClasses &= FilterRHS;
5075
5076 Known |= Known2;
5077 break;
5078 }
5079 case Instruction::Call: {
5080 const CallInst *II = cast<CallInst>(Op);
5081 const Intrinsic::ID IID = II->getIntrinsicID();
5082 switch (IID) {
5083 case Intrinsic::fabs: {
5084 if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) {
5085 // If we only care about the sign bit we don't need to inspect the
5086 // operand.
5087 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5088 InterestedClasses, Known, Q, Depth + 1);
5089 }
5090
5091 Known.fabs();
5092 break;
5093 }
5094 case Intrinsic::copysign: {
5095 KnownFPClass KnownSign;
5096
5097 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5098 Known, Q, Depth + 1);
5099 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5100 KnownSign, Q, Depth + 1);
5101 Known.copysign(KnownSign);
5102 break;
5103 }
5104 case Intrinsic::fma:
5105 case Intrinsic::fmuladd: {
5106 if ((InterestedClasses & fcNegative) == fcNone)
5107 break;
5108
5109 if (II->getArgOperand(0) != II->getArgOperand(1))
5110 break;
5111
5112 // The multiply cannot be -0 and therefore the add can't be -0
5113 Known.knownNot(fcNegZero);
5114
5115 // x * x + y is non-negative if y is non-negative.
5116 KnownFPClass KnownAddend;
5117 computeKnownFPClass(II->getArgOperand(2), DemandedElts, InterestedClasses,
5118 KnownAddend, Q, Depth + 1);
5119
5120 if (KnownAddend.cannotBeOrderedLessThanZero())
5121 Known.knownNot(fcNegative);
5122 break;
5123 }
5124 case Intrinsic::sqrt:
5125 case Intrinsic::experimental_constrained_sqrt: {
5126 KnownFPClass KnownSrc;
5127 FPClassTest InterestedSrcs = InterestedClasses;
5128 if (InterestedClasses & fcNan)
5129 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5130
5131 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5132 KnownSrc, Q, Depth + 1);
5133
5134 if (KnownSrc.isKnownNeverPosInfinity())
5135 Known.knownNot(fcPosInf);
5136 if (KnownSrc.isKnownNever(fcSNan))
5137 Known.knownNot(fcSNan);
5138
5139 // Any negative value besides -0 returns a nan.
5140 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
5141 Known.knownNot(fcNan);
5142
5143 // The only negative value that can be returned is -0 for -0 inputs.
5145
5146 // If the input denormal mode could be PreserveSign, a negative
5147 // subnormal input could produce a negative zero output.
5148 const Function *F = II->getFunction();
5149 const fltSemantics &FltSem =
5150 II->getType()->getScalarType()->getFltSemantics();
5151
5152 if (Q.IIQ.hasNoSignedZeros(II) ||
5153 (F &&
5154 KnownSrc.isKnownNeverLogicalNegZero(F->getDenormalMode(FltSem))))
5155 Known.knownNot(fcNegZero);
5156
5157 break;
5158 }
5159 case Intrinsic::sin:
5160 case Intrinsic::cos: {
5161 // Return NaN on infinite inputs.
5162 KnownFPClass KnownSrc;
5163 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5164 KnownSrc, Q, Depth + 1);
5165 Known.knownNot(fcInf);
5166 if (KnownSrc.isKnownNeverNaN() && KnownSrc.isKnownNeverInfinity())
5167 Known.knownNot(fcNan);
5168 break;
5169 }
5170 case Intrinsic::maxnum:
5171 case Intrinsic::minnum:
5172 case Intrinsic::minimum:
5173 case Intrinsic::maximum:
5174 case Intrinsic::minimumnum:
5175 case Intrinsic::maximumnum: {
5176 KnownFPClass KnownLHS, KnownRHS;
5177 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5178 KnownLHS, Q, Depth + 1);
5179 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5180 KnownRHS, Q, Depth + 1);
5181
5182 bool NeverNaN = KnownLHS.isKnownNeverNaN() || KnownRHS.isKnownNeverNaN();
5183 Known = KnownLHS | KnownRHS;
5184
5185 // If either operand is not NaN, the result is not NaN.
5186 if (NeverNaN &&
5187 (IID == Intrinsic::minnum || IID == Intrinsic::maxnum ||
5188 IID == Intrinsic::minimumnum || IID == Intrinsic::maximumnum))
5189 Known.knownNot(fcNan);
5190
5191 if (IID == Intrinsic::maxnum || IID == Intrinsic::maximumnum) {
5192 // If at least one operand is known to be positive, the result must be
5193 // positive.
5194 if ((KnownLHS.cannotBeOrderedLessThanZero() &&
5195 KnownLHS.isKnownNeverNaN()) ||
5196 (KnownRHS.cannotBeOrderedLessThanZero() &&
5197 KnownRHS.isKnownNeverNaN()))
5199 } else if (IID == Intrinsic::maximum) {
5200 // If at least one operand is known to be positive, the result must be
5201 // positive.
5202 if (KnownLHS.cannotBeOrderedLessThanZero() ||
5203 KnownRHS.cannotBeOrderedLessThanZero())
5205 } else if (IID == Intrinsic::minnum || IID == Intrinsic::minimumnum) {
5206 // If at least one operand is known to be negative, the result must be
5207 // negative.
5208 if ((KnownLHS.cannotBeOrderedGreaterThanZero() &&
5209 KnownLHS.isKnownNeverNaN()) ||
5210 (KnownRHS.cannotBeOrderedGreaterThanZero() &&
5211 KnownRHS.isKnownNeverNaN()))
5213 } else if (IID == Intrinsic::minimum) {
5214 // If at least one operand is known to be negative, the result must be
5215 // negative.
5216 if (KnownLHS.cannotBeOrderedGreaterThanZero() ||
5219 } else
5220 llvm_unreachable("unhandled intrinsic");
5221
5222 // Fixup zero handling if denormals could be returned as a zero.
5223 //
5224 // As there's no spec for denormal flushing, be conservative with the
5225 // treatment of denormals that could be flushed to zero. For older
5226 // subtargets on AMDGPU the min/max instructions would not flush the
5227 // output and return the original value.
5228 //
5229 if ((Known.KnownFPClasses & fcZero) != fcNone &&
5230 !Known.isKnownNeverSubnormal()) {
5231 const Function *Parent = II->getFunction();
5232 if (!Parent)
5233 break;
5234
5236 II->getType()->getScalarType()->getFltSemantics());
5237 if (Mode != DenormalMode::getIEEE())
5238 Known.KnownFPClasses |= fcZero;
5239 }
5240
5241 if (Known.isKnownNeverNaN()) {
5242 if (KnownLHS.SignBit && KnownRHS.SignBit &&
5243 *KnownLHS.SignBit == *KnownRHS.SignBit) {
5244 if (*KnownLHS.SignBit)
5245 Known.signBitMustBeOne();
5246 else
5247 Known.signBitMustBeZero();
5248 } else if ((IID == Intrinsic::maximum || IID == Intrinsic::minimum ||
5249 IID == Intrinsic::maximumnum ||
5250 IID == Intrinsic::minimumnum) ||
5251 // FIXME: Should be using logical zero versions
5252 ((KnownLHS.isKnownNeverNegZero() ||
5253 KnownRHS.isKnownNeverPosZero()) &&
5254 (KnownLHS.isKnownNeverPosZero() ||
5255 KnownRHS.isKnownNeverNegZero()))) {
5256 // Don't take sign bit from NaN operands.
5257 if (!KnownLHS.isKnownNeverNaN())
5258 KnownLHS.SignBit = std::nullopt;
5259 if (!KnownRHS.isKnownNeverNaN())
5260 KnownRHS.SignBit = std::nullopt;
5261 if ((IID == Intrinsic::maximum || IID == Intrinsic::maximumnum ||
5262 IID == Intrinsic::maxnum) &&
5263 (KnownLHS.SignBit == false || KnownRHS.SignBit == false))
5264 Known.signBitMustBeZero();
5265 else if ((IID == Intrinsic::minimum || IID == Intrinsic::minimumnum ||
5266 IID == Intrinsic::minnum) &&
5267 (KnownLHS.SignBit == true || KnownRHS.SignBit == true))
5268 Known.signBitMustBeOne();
5269 }
5270 }
5271 break;
5272 }
5273 case Intrinsic::canonicalize: {
5274 KnownFPClass KnownSrc;
5275 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5276 KnownSrc, Q, Depth + 1);
5277
5278 // This is essentially a stronger form of
5279 // propagateCanonicalizingSrc. Other "canonicalizing" operations don't
5280 // actually have an IR canonicalization guarantee.
5281
5282 // Canonicalize may flush denormals to zero, so we have to consider the
5283 // denormal mode to preserve known-not-0 knowledge.
5284 Known.KnownFPClasses = KnownSrc.KnownFPClasses | fcZero | fcQNan;
5285
5286 // Stronger version of propagateNaN
5287 // Canonicalize is guaranteed to quiet signaling nans.
5288 if (KnownSrc.isKnownNeverNaN())
5289 Known.knownNot(fcNan);
5290 else
5291 Known.knownNot(fcSNan);
5292
5293 const Function *F = II->getFunction();
5294 if (!F)
5295 break;
5296
5297 // If the parent function flushes denormals, the canonical output cannot
5298 // be a denormal.
5299 const fltSemantics &FPType =
5300 II->getType()->getScalarType()->getFltSemantics();
5301 DenormalMode DenormMode = F->getDenormalMode(FPType);
5302 if (DenormMode == DenormalMode::getIEEE()) {
5303 if (KnownSrc.isKnownNever(fcPosZero))
5304 Known.knownNot(fcPosZero);
5305 if (KnownSrc.isKnownNever(fcNegZero))
5306 Known.knownNot(fcNegZero);
5307 break;
5308 }
5309
5310 if (DenormMode.inputsAreZero() || DenormMode.outputsAreZero())
5311 Known.knownNot(fcSubnormal);
5312
5313 if (DenormMode == DenormalMode::getPreserveSign()) {
5314 if (KnownSrc.isKnownNever(fcPosZero | fcPosSubnormal))
5315 Known.knownNot(fcPosZero);
5316 if (KnownSrc.isKnownNever(fcNegZero | fcNegSubnormal))
5317 Known.knownNot(fcNegZero);
5318 break;
5319 }
5320
5321 if (DenormMode.Input == DenormalMode::PositiveZero ||
5322 (DenormMode.Output == DenormalMode::PositiveZero &&
5323 DenormMode.Input == DenormalMode::IEEE))
5324 Known.knownNot(fcNegZero);
5325
5326 break;
5327 }
5328 case Intrinsic::vector_reduce_fmax:
5329 case Intrinsic::vector_reduce_fmin:
5330 case Intrinsic::vector_reduce_fmaximum:
5331 case Intrinsic::vector_reduce_fminimum: {
5332 // reduce min/max will choose an element from one of the vector elements,
5333 // so we can infer and class information that is common to all elements.
5334 Known = computeKnownFPClass(II->getArgOperand(0), II->getFastMathFlags(),
5335 InterestedClasses, Q, Depth + 1);
5336 // Can only propagate sign if output is never NaN.
5337 if (!Known.isKnownNeverNaN())
5338 Known.SignBit.reset();
5339 break;
5340 }
5341 // reverse preserves all characteristics of the input vec's element.
5342 case Intrinsic::vector_reverse:
5343 Known = computeKnownFPClass(
5344 II->getArgOperand(0), DemandedElts.reverseBits(),
5345 II->getFastMathFlags(), InterestedClasses, Q, Depth + 1);
5346 break;
5347 case Intrinsic::trunc:
5348 case Intrinsic::floor:
5349 case Intrinsic::ceil:
5350 case Intrinsic::rint:
5351 case Intrinsic::nearbyint:
5352 case Intrinsic::round:
5353 case Intrinsic::roundeven: {
5354 KnownFPClass KnownSrc;
5355 FPClassTest InterestedSrcs = InterestedClasses;
5356 if (InterestedSrcs & fcPosFinite)
5357 InterestedSrcs |= fcPosFinite;
5358 if (InterestedSrcs & fcNegFinite)
5359 InterestedSrcs |= fcNegFinite;
5360 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5361 KnownSrc, Q, Depth + 1);
5362
5363 // Integer results cannot be subnormal.
5364 Known.knownNot(fcSubnormal);
5365
5366 Known.propagateNaN(KnownSrc, true);
5367
5368 // Pass through infinities, except PPC_FP128 is a special case for
5369 // intrinsics other than trunc.
5370 if (IID == Intrinsic::trunc || !V->getType()->isMultiUnitFPType()) {
5371 if (KnownSrc.isKnownNeverPosInfinity())
5372 Known.knownNot(fcPosInf);
5373 if (KnownSrc.isKnownNeverNegInfinity())
5374 Known.knownNot(fcNegInf);
5375 }
5376
5377 // Negative round ups to 0 produce -0
5378 if (KnownSrc.isKnownNever(fcPosFinite))
5379 Known.knownNot(fcPosFinite);
5380 if (KnownSrc.isKnownNever(fcNegFinite))
5381 Known.knownNot(fcNegFinite);
5382
5383 break;
5384 }
5385 case Intrinsic::exp:
5386 case Intrinsic::exp2:
5387 case Intrinsic::exp10:
5388 case Intrinsic::amdgcn_exp2: {
5389 Known.knownNot(fcNegative);
5390
5391 Type *EltTy = II->getType()->getScalarType();
5392 if (IID == Intrinsic::amdgcn_exp2 && EltTy->isFloatTy())
5393 Known.knownNot(fcSubnormal);
5394
5395 if ((InterestedClasses & fcNan) == fcNone)
5396 break;
5397
5398 KnownFPClass KnownSrc;
5399 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5400 KnownSrc, Q, Depth + 1);
5401 if (KnownSrc.isKnownNeverNaN()) {
5402 Known.knownNot(fcNan);
5403 Known.signBitMustBeZero();
5404 }
5405
5406 break;
5407 }
5408 case Intrinsic::fptrunc_round: {
5409 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,
5410 Q, Depth);
5411 break;
5412 }
5413 case Intrinsic::log:
5414 case Intrinsic::log10:
5415 case Intrinsic::log2:
5416 case Intrinsic::experimental_constrained_log:
5417 case Intrinsic::experimental_constrained_log10:
5418 case Intrinsic::experimental_constrained_log2:
5419 case Intrinsic::amdgcn_log: {
5420 Type *EltTy = II->getType()->getScalarType();
5421 if (IID == Intrinsic::amdgcn_log && EltTy->isFloatTy())
5422 Known.knownNot(fcSubnormal);
5423
5424 // log(+inf) -> +inf
5425 // log([+-]0.0) -> -inf
5426 // log(-inf) -> nan
5427 // log(-x) -> nan
5428 if ((InterestedClasses & (fcNan | fcInf)) == fcNone)
5429 break;
5430
5431 FPClassTest InterestedSrcs = InterestedClasses;
5432 if ((InterestedClasses & fcNegInf) != fcNone)
5433 InterestedSrcs |= fcZero | fcSubnormal;
5434 if ((InterestedClasses & fcNan) != fcNone)
5435 InterestedSrcs |= fcNan | (fcNegative & ~fcNan);
5436
5437 KnownFPClass KnownSrc;
5438 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5439 KnownSrc, Q, Depth + 1);
5440
5441 if (KnownSrc.isKnownNeverPosInfinity())
5442 Known.knownNot(fcPosInf);
5443
5444 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
5445 Known.knownNot(fcNan);
5446
5447 const Function *F = II->getFunction();
5448 if (!F)
5449 break;
5450
5451 const fltSemantics &FltSem = EltTy->getFltSemantics();
5452 DenormalMode Mode = F->getDenormalMode(FltSem);
5453
5454 if (KnownSrc.isKnownNeverLogicalZero(Mode))
5455 Known.knownNot(fcNegInf);
5456
5457 break;
5458 }
5459 case Intrinsic::powi: {
5460 if ((InterestedClasses & fcNegative) == fcNone)
5461 break;
5462
5463 const Value *Exp = II->getArgOperand(1);
5464 Type *ExpTy = Exp->getType();
5465 unsigned BitWidth = ExpTy->getScalarType()->getIntegerBitWidth();
5466 KnownBits ExponentKnownBits(BitWidth);
5467 computeKnownBits(Exp, isa<VectorType>(ExpTy) ? DemandedElts : APInt(1, 1),
5468 ExponentKnownBits, Q, Depth + 1);
5469
5470 if (ExponentKnownBits.Zero[0]) { // Is even
5471 Known.knownNot(fcNegative);
5472 break;
5473 }
5474
5475 // Given that exp is an integer, here are the
5476 // ways that pow can return a negative value:
5477 //
5478 // pow(-x, exp) --> negative if exp is odd and x is negative.
5479 // pow(-0, exp) --> -inf if exp is negative odd.
5480 // pow(-0, exp) --> -0 if exp is positive odd.
5481 // pow(-inf, exp) --> -0 if exp is negative odd.
5482 // pow(-inf, exp) --> -inf if exp is positive odd.
5483 KnownFPClass KnownSrc;
5484 computeKnownFPClass(II->getArgOperand(0), DemandedElts, fcNegative,
5485 KnownSrc, Q, Depth + 1);
5486 if (KnownSrc.isKnownNever(fcNegative))
5487 Known.knownNot(fcNegative);
5488 break;
5489 }
5490 case Intrinsic::ldexp: {
5491 KnownFPClass KnownSrc;
5492 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5493 KnownSrc, Q, Depth + 1);
5494 Known.propagateNaN(KnownSrc, /*PropagateSign=*/true);
5495
5496 // Sign is preserved, but underflows may produce zeroes.
5497 if (KnownSrc.isKnownNever(fcNegative))
5498 Known.knownNot(fcNegative);
5499 else if (KnownSrc.cannotBeOrderedLessThanZero())
5501
5502 if (KnownSrc.isKnownNever(fcPositive))
5503 Known.knownNot(fcPositive);
5504 else if (KnownSrc.cannotBeOrderedGreaterThanZero())
5506
5507 // Can refine inf/zero handling based on the exponent operand.
5508 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;
5509 if ((InterestedClasses & ExpInfoMask) == fcNone)
5510 break;
5511 if ((KnownSrc.KnownFPClasses & ExpInfoMask) == fcNone)
5512 break;
5513
5514 const fltSemantics &Flt =
5515 II->getType()->getScalarType()->getFltSemantics();
5516 unsigned Precision = APFloat::semanticsPrecision(Flt);
5517 const Value *ExpArg = II->getArgOperand(1);
5519 ExpArg, true, Q.IIQ.UseInstrInfo, Q.AC, Q.CxtI, Q.DT, Depth + 1);
5520
5521 const int MantissaBits = Precision - 1;
5522 if (ExpRange.getSignedMin().sge(static_cast<int64_t>(MantissaBits)))
5523 Known.knownNot(fcSubnormal);
5524
5525 const Function *F = II->getFunction();
5526 const APInt *ConstVal = ExpRange.getSingleElement();
5527 const fltSemantics &FltSem =
5528 II->getType()->getScalarType()->getFltSemantics();
5529 if (ConstVal && ConstVal->isZero()) {
5530 // ldexp(x, 0) -> x, so propagate everything.
5531 Known.propagateCanonicalizingSrc(KnownSrc, F->getDenormalMode(FltSem));
5532 } else if (ExpRange.isAllNegative()) {
5533 // If we know the power is <= 0, can't introduce inf
5534 if (KnownSrc.isKnownNeverPosInfinity())
5535 Known.knownNot(fcPosInf);
5536 if (KnownSrc.isKnownNeverNegInfinity())
5537 Known.knownNot(fcNegInf);
5538 } else if (ExpRange.isAllNonNegative()) {
5539 // If we know the power is >= 0, can't introduce subnormal or zero
5540 if (KnownSrc.isKnownNeverPosSubnormal())
5541 Known.knownNot(fcPosSubnormal);
5542 if (KnownSrc.isKnownNeverNegSubnormal())
5543 Known.knownNot(fcNegSubnormal);
5544 if (F &&
5545 KnownSrc.isKnownNeverLogicalPosZero(F->getDenormalMode(FltSem)))
5546 Known.knownNot(fcPosZero);
5547 if (F &&
5548 KnownSrc.isKnownNeverLogicalNegZero(F->getDenormalMode(FltSem)))
5549 Known.knownNot(fcNegZero);
5550 }
5551
5552 break;
5553 }
5554 case Intrinsic::arithmetic_fence: {
5555 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5556 Known, Q, Depth + 1);
5557 break;
5558 }
5559 case Intrinsic::experimental_constrained_sitofp:
5560 case Intrinsic::experimental_constrained_uitofp:
5561 // Cannot produce nan
5562 Known.knownNot(fcNan);
5563
5564 // sitofp and uitofp turn into +0.0 for zero.
5565 Known.knownNot(fcNegZero);
5566
5567 // Integers cannot be subnormal
5568 Known.knownNot(fcSubnormal);
5569
5570 if (IID == Intrinsic::experimental_constrained_uitofp)
5571 Known.signBitMustBeZero();
5572
5573 // TODO: Copy inf handling from instructions
5574 break;
5575 case Intrinsic::amdgcn_rcp: {
5576 KnownFPClass KnownSrc;
5577 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5578 KnownSrc, Q, Depth + 1);
5579
5580 Known.propagateNaN(KnownSrc);
5581
5582 Type *EltTy = II->getType()->getScalarType();
5583
5584 // f32 denormal always flushed.
5585 if (EltTy->isFloatTy()) {
5586 Known.knownNot(fcSubnormal);
5587 KnownSrc.knownNot(fcSubnormal);
5588 }
5589
5590 if (KnownSrc.isKnownNever(fcNegative))
5591 Known.knownNot(fcNegative);
5592 if (KnownSrc.isKnownNever(fcPositive))
5593 Known.knownNot(fcPositive);
5594
5595 if (const Function *F = II->getFunction()) {
5596 DenormalMode Mode = F->getDenormalMode(EltTy->getFltSemantics());
5597 if (KnownSrc.isKnownNeverLogicalPosZero(Mode))
5598 Known.knownNot(fcPosInf);
5599 if (KnownSrc.isKnownNeverLogicalNegZero(Mode))
5600 Known.knownNot(fcNegInf);
5601 }
5602
5603 break;
5604 }
5605 case Intrinsic::amdgcn_rsq: {
5606 KnownFPClass KnownSrc;
5607 // The only negative value that can be returned is -inf for -0 inputs.
5609
5610 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5611 KnownSrc, Q, Depth + 1);
5612
5613 // Negative -> nan
5614 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
5615 Known.knownNot(fcNan);
5616 else if (KnownSrc.isKnownNever(fcSNan))
5617 Known.knownNot(fcSNan);
5618
5619 // +inf -> +0
5620 if (KnownSrc.isKnownNeverPosInfinity())
5621 Known.knownNot(fcPosZero);
5622
5623 Type *EltTy = II->getType()->getScalarType();
5624
5625 // f32 denormal always flushed.
5626 if (EltTy->isFloatTy())
5627 Known.knownNot(fcPosSubnormal);
5628
5629 if (const Function *F = II->getFunction()) {
5630 DenormalMode Mode = F->getDenormalMode(EltTy->getFltSemantics());
5631
5632 // -0 -> -inf
5633 if (KnownSrc.isKnownNeverLogicalNegZero(Mode))
5634 Known.knownNot(fcNegInf);
5635
5636 // +0 -> +inf
5637 if (KnownSrc.isKnownNeverLogicalPosZero(Mode))
5638 Known.knownNot(fcPosInf);
5639 }
5640
5641 break;
5642 }
5643 default:
5644 break;
5645 }
5646
5647 break;
5648 }
5649 case Instruction::FAdd:
5650 case Instruction::FSub: {
5651 KnownFPClass KnownLHS, KnownRHS;
5652 bool WantNegative =
5653 Op->getOpcode() == Instruction::FAdd &&
5654 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;
5655 bool WantNaN = (InterestedClasses & fcNan) != fcNone;
5656 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;
5657
5658 if (!WantNaN && !WantNegative && !WantNegZero)
5659 break;
5660
5661 FPClassTest InterestedSrcs = InterestedClasses;
5662 if (WantNegative)
5663 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5664 if (InterestedClasses & fcNan)
5665 InterestedSrcs |= fcInf;
5666 computeKnownFPClass(Op->getOperand(1), DemandedElts, InterestedSrcs,
5667 KnownRHS, Q, Depth + 1);
5668
5669 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||
5670 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) ||
5671 WantNegZero || Opc == Instruction::FSub) {
5672
5673 // RHS is canonically cheaper to compute. Skip inspecting the LHS if
5674 // there's no point.
5675 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedSrcs,
5676 KnownLHS, Q, Depth + 1);
5677 // Adding positive and negative infinity produces NaN.
5678 // TODO: Check sign of infinities.
5679 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5680 (KnownLHS.isKnownNeverInfinity() || KnownRHS.isKnownNeverInfinity()))
5681 Known.knownNot(fcNan);
5682
5683 // FIXME: Context function should always be passed in separately
5684 const Function *F = cast<Instruction>(Op)->getFunction();
5685
5686 if (Op->getOpcode() == Instruction::FAdd) {
5687 if (KnownLHS.cannotBeOrderedLessThanZero() &&
5688 KnownRHS.cannotBeOrderedLessThanZero())
5690 if (!F)
5691 break;
5692
5693 const fltSemantics &FltSem =
5694 Op->getType()->getScalarType()->getFltSemantics();
5695 DenormalMode Mode = F->getDenormalMode(FltSem);
5696
5697 // (fadd x, 0.0) is guaranteed to return +0.0, not -0.0.
5698 if ((KnownLHS.isKnownNeverLogicalNegZero(Mode) ||
5699 KnownRHS.isKnownNeverLogicalNegZero(Mode)) &&
5700 // Make sure output negative denormal can't flush to -0
5701 outputDenormalIsIEEEOrPosZero(*F, Op->getType()))
5702 Known.knownNot(fcNegZero);
5703 } else {
5704 if (!F)
5705 break;
5706
5707 const fltSemantics &FltSem =
5708 Op->getType()->getScalarType()->getFltSemantics();
5709 DenormalMode Mode = F->getDenormalMode(FltSem);
5710
5711 // Only fsub -0, +0 can return -0
5712 if ((KnownLHS.isKnownNeverLogicalNegZero(Mode) ||
5713 KnownRHS.isKnownNeverLogicalPosZero(Mode)) &&
5714 // Make sure output negative denormal can't flush to -0
5715 outputDenormalIsIEEEOrPosZero(*F, Op->getType()))
5716 Known.knownNot(fcNegZero);
5717 }
5718 }
5719
5720 break;
5721 }
5722 case Instruction::FMul: {
5723 // X * X is always non-negative or a NaN.
5724 if (Op->getOperand(0) == Op->getOperand(1))
5725 Known.knownNot(fcNegative);
5726
5727 KnownFPClass KnownLHS, KnownRHS;
5728
5729 const APFloat *CRHS;
5730 if (match(Op->getOperand(1), m_APFloat(CRHS))) {
5731 // Match denormal scaling pattern, similar to the case in ldexp. If the
5732 // constant's exponent is sufficiently large, the result cannot be
5733 // subnormal.
5734
5735 // TODO: Should do general ConstantFPRange analysis.
5736 const fltSemantics &Flt =
5737 Op->getType()->getScalarType()->getFltSemantics();
5738 unsigned Precision = APFloat::semanticsPrecision(Flt);
5739 const int MantissaBits = Precision - 1;
5740
5741 int MinKnownExponent = ilogb(*CRHS);
5742 if (MinKnownExponent >= MantissaBits)
5743 Known.knownNot(fcSubnormal);
5744
5745 KnownRHS = KnownFPClass(*CRHS);
5746 } else {
5747 computeKnownFPClass(Op->getOperand(1), DemandedElts, fcAllFlags, KnownRHS,
5748 Q, Depth + 1);
5749 }
5750
5751 computeKnownFPClass(Op->getOperand(0), DemandedElts, fcAllFlags, KnownLHS,
5752 Q, Depth + 1);
5753
5754 // xor sign bit.
5755 if ((KnownLHS.isKnownNever(fcNegative) &&
5756 KnownRHS.isKnownNever(fcNegative)) ||
5757 (KnownLHS.isKnownNever(fcPositive) &&
5758 KnownRHS.isKnownNever(fcPositive)))
5759 Known.knownNot(fcNegative);
5760
5761 if ((KnownLHS.isKnownAlways(fcNegative | fcNan) &&
5762 KnownRHS.isKnownNever(fcNegative)) ||
5763 (KnownLHS.isKnownNever(fcNegative) &&
5764 KnownRHS.isKnownAlways(fcNegative | fcNan)))
5765 Known.knownNot(fcPositive);
5766
5767 // inf * anything => inf or nan
5768 if (KnownLHS.isKnownAlways(fcInf | fcNan) ||
5769 KnownRHS.isKnownAlways(fcInf | fcNan))
5771
5772 // 0 * anything => 0 or nan
5773 if (KnownRHS.isKnownAlways(fcZero | fcNan) ||
5774 KnownLHS.isKnownAlways(fcZero | fcNan))
5776
5777 // +/-0 * +/-inf = nan
5778 if ((KnownLHS.isKnownAlways(fcZero | fcNan) &&
5779 KnownRHS.isKnownAlways(fcInf | fcNan)) ||
5780 (KnownLHS.isKnownAlways(fcInf | fcNan) &&
5781 KnownRHS.isKnownAlways(fcZero | fcNan)))
5782 Known.knownNot(~fcNan);
5783
5784 if (!KnownLHS.isKnownNeverNaN() || !KnownRHS.isKnownNeverNaN())
5785 break;
5786
5787 if (KnownLHS.SignBit && KnownRHS.SignBit) {
5788 if (*KnownLHS.SignBit == *KnownRHS.SignBit)
5789 Known.signBitMustBeZero();
5790 else
5791 Known.signBitMustBeOne();
5792 }
5793
5794 // If 0 * +/-inf produces NaN.
5795 if (KnownLHS.isKnownNeverInfinity() && KnownRHS.isKnownNeverInfinity()) {
5796 Known.knownNot(fcNan);
5797 break;
5798 }
5799
5800 const Function *F = cast<Instruction>(Op)->getFunction();
5801 if (!F)
5802 break;
5803
5804 Type *OpTy = Op->getType()->getScalarType();
5805 const fltSemantics &FltSem = OpTy->getFltSemantics();
5806 DenormalMode Mode = F->getDenormalMode(FltSem);
5807
5808 if ((KnownRHS.isKnownNeverInfinity() ||
5809 KnownLHS.isKnownNeverLogicalZero(Mode)) &&
5810 (KnownLHS.isKnownNeverInfinity() ||
5811 KnownRHS.isKnownNeverLogicalZero(Mode)))
5812 Known.knownNot(fcNan);
5813
5814 break;
5815 }
5816 case Instruction::FDiv:
5817 case Instruction::FRem: {
5818 if (Op->getOperand(0) == Op->getOperand(1)) {
5819 // TODO: Could filter out snan if we inspect the operand
5820 if (Op->getOpcode() == Instruction::FDiv) {
5821 // X / X is always exactly 1.0 or a NaN.
5823 } else {
5824 // X % X is always exactly [+-]0.0 or a NaN.
5825 Known.KnownFPClasses = fcNan | fcZero;
5826 }
5827
5828 break;
5829 }
5830
5831 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
5832 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
5833 const bool WantPositive =
5834 Opc == Instruction::FRem && (InterestedClasses & fcPositive) != fcNone;
5835 if (!WantNan && !WantNegative && !WantPositive)
5836 break;
5837
5838 KnownFPClass KnownLHS, KnownRHS;
5839
5840 computeKnownFPClass(Op->getOperand(1), DemandedElts,
5841 fcNan | fcInf | fcZero | fcNegative, KnownRHS, Q,
5842 Depth + 1);
5843
5844 bool KnowSomethingUseful =
5845 KnownRHS.isKnownNeverNaN() || KnownRHS.isKnownNever(fcNegative);
5846
5847 if (KnowSomethingUseful || WantPositive) {
5848 const FPClassTest InterestedLHS =
5849 WantPositive ? fcAllFlags
5851
5852 computeKnownFPClass(Op->getOperand(0), DemandedElts,
5853 InterestedClasses & InterestedLHS, KnownLHS, Q,
5854 Depth + 1);
5855 }
5856
5857 const Function *F = cast<Instruction>(Op)->getFunction();
5858 const fltSemantics &FltSem =
5859 Op->getType()->getScalarType()->getFltSemantics();
5860
5861 if (Op->getOpcode() == Instruction::FDiv) {
5862 // Only 0/0, Inf/Inf produce NaN.
5863 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5864 (KnownLHS.isKnownNeverInfinity() ||
5865 KnownRHS.isKnownNeverInfinity()) &&
5866 ((F &&
5867 KnownLHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))) ||
5868 (F &&
5869 KnownRHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))))) {
5870 Known.knownNot(fcNan);
5871 }
5872
5873 // X / -0.0 is -Inf (or NaN).
5874 // +X / +X is +X
5875 if (KnownLHS.isKnownNever(fcNegative) && KnownRHS.isKnownNever(fcNegative))
5876 Known.knownNot(fcNegative);
5877 } else {
5878 // Inf REM x and x REM 0 produce NaN.
5879 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5880 KnownLHS.isKnownNeverInfinity() && F &&
5881 KnownRHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))) {
5882 Known.knownNot(fcNan);
5883 }
5884
5885 // The sign for frem is the same as the first operand.
5886 if (KnownLHS.cannotBeOrderedLessThanZero())
5888 if (KnownLHS.cannotBeOrderedGreaterThanZero())
5890
5891 // See if we can be more aggressive about the sign of 0.
5892 if (KnownLHS.isKnownNever(fcNegative))
5893 Known.knownNot(fcNegative);
5894 if (KnownLHS.isKnownNever(fcPositive))
5895 Known.knownNot(fcPositive);
5896 }
5897
5898 break;
5899 }
5900 case Instruction::FPExt: {
5901 // Infinity, nan and zero propagate from source.
5902 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5903 Known, Q, Depth + 1);
5904
5905 const fltSemantics &DstTy =
5906 Op->getType()->getScalarType()->getFltSemantics();
5907 const fltSemantics &SrcTy =
5908 Op->getOperand(0)->getType()->getScalarType()->getFltSemantics();
5909
5910 // All subnormal inputs should be in the normal range in the result type.
5911 if (APFloat::isRepresentableAsNormalIn(SrcTy, DstTy)) {
5912 if (Known.KnownFPClasses & fcPosSubnormal)
5913 Known.KnownFPClasses |= fcPosNormal;
5914 if (Known.KnownFPClasses & fcNegSubnormal)
5915 Known.KnownFPClasses |= fcNegNormal;
5916 Known.knownNot(fcSubnormal);
5917 }
5918
5919 // Sign bit of a nan isn't guaranteed.
5920 if (!Known.isKnownNeverNaN())
5921 Known.SignBit = std::nullopt;
5922 break;
5923 }
5924 case Instruction::FPTrunc: {
5925 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known, Q,
5926 Depth);
5927 break;
5928 }
5929 case Instruction::SIToFP:
5930 case Instruction::UIToFP: {
5931 // Cannot produce nan
5932 Known.knownNot(fcNan);
5933
5934 // Integers cannot be subnormal
5935 Known.knownNot(fcSubnormal);
5936
5937 // sitofp and uitofp turn into +0.0 for zero.
5938 Known.knownNot(fcNegZero);
5939 if (Op->getOpcode() == Instruction::UIToFP)
5940 Known.signBitMustBeZero();
5941
5942 if (InterestedClasses & fcInf) {
5943 // Get width of largest magnitude integer (remove a bit if signed).
5944 // This still works for a signed minimum value because the largest FP
5945 // value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx).
5946 int IntSize = Op->getOperand(0)->getType()->getScalarSizeInBits();
5947 if (Op->getOpcode() == Instruction::SIToFP)
5948 --IntSize;
5949
5950 // If the exponent of the largest finite FP value can hold the largest
5951 // integer, the result of the cast must be finite.
5952 Type *FPTy = Op->getType()->getScalarType();
5953 if (ilogb(APFloat::getLargest(FPTy->getFltSemantics())) >= IntSize)
5954 Known.knownNot(fcInf);
5955 }
5956
5957 break;
5958 }
5959 case Instruction::ExtractElement: {
5960 // Look through extract element. If the index is non-constant or
5961 // out-of-range demand all elements, otherwise just the extracted element.
5962 const Value *Vec = Op->getOperand(0);
5963
5964 APInt DemandedVecElts;
5965 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
5966 unsigned NumElts = VecTy->getNumElements();
5967 DemandedVecElts = APInt::getAllOnes(NumElts);
5968 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(1));
5969 if (CIdx && CIdx->getValue().ult(NumElts))
5970 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
5971 } else {
5972 DemandedVecElts = APInt(1, 1);
5973 }
5974
5975 return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,
5976 Q, Depth + 1);
5977 }
5978 case Instruction::InsertElement: {
5979 if (isa<ScalableVectorType>(Op->getType()))
5980 return;
5981
5982 const Value *Vec = Op->getOperand(0);
5983 const Value *Elt = Op->getOperand(1);
5984 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(2));
5985 unsigned NumElts = DemandedElts.getBitWidth();
5986 APInt DemandedVecElts = DemandedElts;
5987 bool NeedsElt = true;
5988 // If we know the index we are inserting to, clear it from Vec check.
5989 if (CIdx && CIdx->getValue().ult(NumElts)) {
5990 DemandedVecElts.clearBit(CIdx->getZExtValue());
5991 NeedsElt = DemandedElts[CIdx->getZExtValue()];
5992 }
5993
5994 // Do we demand the inserted element?
5995 if (NeedsElt) {
5996 computeKnownFPClass(Elt, Known, InterestedClasses, Q, Depth + 1);
5997 // If we don't know any bits, early out.
5998 if (Known.isUnknown())
5999 break;
6000 } else {
6001 Known.KnownFPClasses = fcNone;
6002 }
6003
6004 // Do we need anymore elements from Vec?
6005 if (!DemandedVecElts.isZero()) {
6006 KnownFPClass Known2;
6007 computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2, Q,
6008 Depth + 1);
6009 Known |= Known2;
6010 }
6011
6012 break;
6013 }
6014 case Instruction::ShuffleVector: {
6015 // Handle vector splat idiom
6016 if (Value *Splat = getSplatValue(V)) {
6017 computeKnownFPClass(Splat, Known, InterestedClasses, Q, Depth + 1);
6018 break;
6019 }
6020
6021 // For undef elements, we don't know anything about the common state of
6022 // the shuffle result.
6023 APInt DemandedLHS, DemandedRHS;
6024 auto *Shuf = dyn_cast<ShuffleVectorInst>(Op);
6025 if (!Shuf || !getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
6026 return;
6027
6028 if (!!DemandedLHS) {
6029 const Value *LHS = Shuf->getOperand(0);
6030 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known, Q,
6031 Depth + 1);
6032
6033 // If we don't know any bits, early out.
6034 if (Known.isUnknown())
6035 break;
6036 } else {
6037 Known.KnownFPClasses = fcNone;
6038 }
6039
6040 if (!!DemandedRHS) {
6041 KnownFPClass Known2;
6042 const Value *RHS = Shuf->getOperand(1);
6043 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2, Q,
6044 Depth + 1);
6045 Known |= Known2;
6046 }
6047
6048 break;
6049 }
6050 case Instruction::ExtractValue: {
6051 const ExtractValueInst *Extract = cast<ExtractValueInst>(Op);
6052 ArrayRef<unsigned> Indices = Extract->getIndices();
6053 const Value *Src = Extract->getAggregateOperand();
6054 if (isa<StructType>(Src->getType()) && Indices.size() == 1 &&
6055 Indices[0] == 0) {
6056 if (const auto *II = dyn_cast<IntrinsicInst>(Src)) {
6057 switch (II->getIntrinsicID()) {
6058 case Intrinsic::frexp: {
6059 Known.knownNot(fcSubnormal);
6060
6061 KnownFPClass KnownSrc;
6062 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
6063 InterestedClasses, KnownSrc, Q, Depth + 1);
6064
6065 const Function *F = cast<Instruction>(Op)->getFunction();
6066 const fltSemantics &FltSem =
6067 Op->getType()->getScalarType()->getFltSemantics();
6068
6069 if (KnownSrc.isKnownNever(fcNegative))
6070 Known.knownNot(fcNegative);
6071 else {
6072 if (F &&
6073 KnownSrc.isKnownNeverLogicalNegZero(F->getDenormalMode(FltSem)))
6074 Known.knownNot(fcNegZero);
6075 if (KnownSrc.isKnownNever(fcNegInf))
6076 Known.knownNot(fcNegInf);
6077 }
6078
6079 if (KnownSrc.isKnownNever(fcPositive))
6080 Known.knownNot(fcPositive);
6081 else {
6082 if (F &&
6083 KnownSrc.isKnownNeverLogicalPosZero(F->getDenormalMode(FltSem)))
6084 Known.knownNot(fcPosZero);
6085 if (KnownSrc.isKnownNever(fcPosInf))
6086 Known.knownNot(fcPosInf);
6087 }
6088
6089 Known.propagateNaN(KnownSrc);
6090 return;
6091 }
6092 default:
6093 break;
6094 }
6095 }
6096 }
6097
6098 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Q,
6099 Depth + 1);
6100 break;
6101 }
6102 case Instruction::PHI: {
6103 const PHINode *P = cast<PHINode>(Op);
6104 // Unreachable blocks may have zero-operand PHI nodes.
6105 if (P->getNumIncomingValues() == 0)
6106 break;
6107
6108 // Otherwise take the unions of the known bit sets of the operands,
6109 // taking conservative care to avoid excessive recursion.
6110 const unsigned PhiRecursionLimit = MaxAnalysisRecursionDepth - 2;
6111
6112 if (Depth < PhiRecursionLimit) {
6113 // Skip if every incoming value references to ourself.
6114 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
6115 break;
6116
6117 bool First = true;
6118
6119 for (const Use &U : P->operands()) {
6120 Value *IncValue;
6121 Instruction *CxtI;
6122 breakSelfRecursivePHI(&U, P, IncValue, CxtI);
6123 // Skip direct self references.
6124 if (IncValue == P)
6125 continue;
6126
6127 KnownFPClass KnownSrc;
6128 // Recurse, but cap the recursion to two levels, because we don't want
6129 // to waste time spinning around in loops. We need at least depth 2 to
6130 // detect known sign bits.
6131 computeKnownFPClass(IncValue, DemandedElts, InterestedClasses, KnownSrc,
6133 PhiRecursionLimit);
6134
6135 if (First) {
6136 Known = KnownSrc;
6137 First = false;
6138 } else {
6139 Known |= KnownSrc;
6140 }
6141
6142 if (Known.KnownFPClasses == fcAllFlags)
6143 break;
6144 }
6145 }
6146
6147 break;
6148 }
6149 case Instruction::BitCast: {
6150 const Value *Src;
6151 if (!match(Op, m_ElementWiseBitCast(m_Value(Src))) ||
6152 !Src->getType()->isIntOrIntVectorTy())
6153 break;
6154
6155 const Type *Ty = Op->getType()->getScalarType();
6156 KnownBits Bits(Ty->getScalarSizeInBits());
6157 computeKnownBits(Src, DemandedElts, Bits, Q, Depth + 1);
6158
6159 // Transfer information from the sign bit.
6160 if (Bits.isNonNegative())
6161 Known.signBitMustBeZero();
6162 else if (Bits.isNegative())
6163 Known.signBitMustBeOne();
6164
6165 if (Ty->isIEEELikeFPTy()) {
6166 // IEEE floats are NaN when all bits of the exponent plus at least one of
6167 // the fraction bits are 1. This means:
6168 // - If we assume unknown bits are 0 and the value is NaN, it will
6169 // always be NaN
6170 // - If we assume unknown bits are 1 and the value is not NaN, it can
6171 // never be NaN
6172 // Note: They do not hold for x86_fp80 format.
6173 if (APFloat(Ty->getFltSemantics(), Bits.One).isNaN())
6174 Known.KnownFPClasses = fcNan;
6175 else if (!APFloat(Ty->getFltSemantics(), ~Bits.Zero).isNaN())
6176 Known.knownNot(fcNan);
6177
6178 // Build KnownBits representing Inf and check if it must be equal or
6179 // unequal to this value.
6180 auto InfKB = KnownBits::makeConstant(
6181 APFloat::getInf(Ty->getFltSemantics()).bitcastToAPInt());
6182 InfKB.Zero.clearSignBit();
6183 if (const auto InfResult = KnownBits::eq(Bits, InfKB)) {
6184 assert(!InfResult.value());
6185 Known.knownNot(fcInf);
6186 } else if (Bits == InfKB) {
6187 Known.KnownFPClasses = fcInf;
6188 }
6189
6190 // Build KnownBits representing Zero and check if it must be equal or
6191 // unequal to this value.
6192 auto ZeroKB = KnownBits::makeConstant(
6193 APFloat::getZero(Ty->getFltSemantics()).bitcastToAPInt());
6194 ZeroKB.Zero.clearSignBit();
6195 if (const auto ZeroResult = KnownBits::eq(Bits, ZeroKB)) {
6196 assert(!ZeroResult.value());
6197 Known.knownNot(fcZero);
6198 } else if (Bits == ZeroKB) {
6199 Known.KnownFPClasses = fcZero;
6200 }
6201 }
6202
6203 break;
6204 }
6205 default:
6206 break;
6207 }
6208}
6209
6211 const APInt &DemandedElts,
6212 FPClassTest InterestedClasses,
6213 const SimplifyQuery &SQ,
6214 unsigned Depth) {
6215 KnownFPClass KnownClasses;
6216 ::computeKnownFPClass(V, DemandedElts, InterestedClasses, KnownClasses, SQ,
6217 Depth);
6218 return KnownClasses;
6219}
6220
6222 FPClassTest InterestedClasses,
6223 const SimplifyQuery &SQ,
6224 unsigned Depth) {
6225 KnownFPClass Known;
6226 ::computeKnownFPClass(V, Known, InterestedClasses, SQ, Depth);
6227 return Known;
6228}
6229
6231 const Value *V, const DataLayout &DL, FPClassTest InterestedClasses,
6232 const TargetLibraryInfo *TLI, AssumptionCache *AC, const Instruction *CxtI,
6233 const DominatorTree *DT, bool UseInstrInfo, unsigned Depth) {
6234 return computeKnownFPClass(V, InterestedClasses,
6235 SimplifyQuery(DL, TLI, DT, AC, CxtI, UseInstrInfo),
6236 Depth);
6237}
6238
6240llvm::computeKnownFPClass(const Value *V, const APInt &DemandedElts,
6241 FastMathFlags FMF, FPClassTest InterestedClasses,
6242 const SimplifyQuery &SQ, unsigned Depth) {
6243 if (FMF.noNaNs())
6244 InterestedClasses &= ~fcNan;
6245 if (FMF.noInfs())
6246 InterestedClasses &= ~fcInf;
6247
6248 KnownFPClass Result =
6249 computeKnownFPClass(V, DemandedElts, InterestedClasses, SQ, Depth);
6250
6251 if (FMF.noNaNs())
6252 Result.KnownFPClasses &= ~fcNan;
6253 if (FMF.noInfs())
6254 Result.KnownFPClasses &= ~fcInf;
6255 return Result;
6256}
6257
6259 FPClassTest InterestedClasses,
6260 const SimplifyQuery &SQ,
6261 unsigned Depth) {
6262 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
6263 APInt DemandedElts =
6264 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
6265 return computeKnownFPClass(V, DemandedElts, FMF, InterestedClasses, SQ,
6266 Depth);
6267}
6268
6270 unsigned Depth) {
6272 return Known.isKnownNeverNegZero();
6273}
6274
6281
6283 unsigned Depth) {
6285 return Known.isKnownNeverInfinity();
6286}
6287
6288/// Return true if the floating-point value can never contain a NaN or infinity.
6290 unsigned Depth) {
6292 return Known.isKnownNeverNaN() && Known.isKnownNeverInfinity();
6293}
6294
6295/// Return true if the floating-point scalar value is not a NaN or if the
6296/// floating-point vector value has no NaN elements. Return false if a value
6297/// could ever be NaN.
6299 unsigned Depth) {
6301 return Known.isKnownNeverNaN();
6302}
6303
6304/// Return false if we can prove that the specified FP value's sign bit is 0.
6305/// Return true if we can prove that the specified FP value's sign bit is 1.
6306/// Otherwise return std::nullopt.
6307std::optional<bool> llvm::computeKnownFPSignBit(const Value *V,
6308 const SimplifyQuery &SQ,
6309 unsigned Depth) {
6311 return Known.SignBit;
6312}
6313
6315 auto *User = cast<Instruction>(U.getUser());
6316 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6317 if (FPOp->hasNoSignedZeros())
6318 return true;
6319 }
6320
6321 switch (User->getOpcode()) {
6322 case Instruction::FPToSI:
6323 case Instruction::FPToUI:
6324 return true;
6325 case Instruction::FCmp:
6326 // fcmp treats both positive and negative zero as equal.
6327 return true;
6328 case Instruction::Call:
6329 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6330 switch (II->getIntrinsicID()) {
6331 case Intrinsic::fabs:
6332 return true;
6333 case Intrinsic::copysign:
6334 return U.getOperandNo() == 0;
6335 case Intrinsic::is_fpclass:
6336 case Intrinsic::vp_is_fpclass: {
6337 auto Test =
6338 static_cast<FPClassTest>(
6339 cast<ConstantInt>(II->getArgOperand(1))->getZExtValue()) &
6342 }
6343 default:
6344 return false;
6345 }
6346 }
6347 return false;
6348 default:
6349 return false;
6350 }
6351}
6352
6354 auto *User = cast<Instruction>(U.getUser());
6355 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6356 if (FPOp->hasNoNaNs())
6357 return true;
6358 }
6359
6360 switch (User->getOpcode()) {
6361 case Instruction::FPToSI:
6362 case Instruction::FPToUI:
6363 return true;
6364 // Proper FP math operations ignore the sign bit of NaN.
6365 case Instruction::FAdd:
6366 case Instruction::FSub:
6367 case Instruction::FMul:
6368 case Instruction::FDiv:
6369 case Instruction::FRem:
6370 case Instruction::FPTrunc:
6371 case Instruction::FPExt:
6372 case Instruction::FCmp:
6373 return true;
6374 // Bitwise FP operations should preserve the sign bit of NaN.
6375 case Instruction::FNeg:
6376 case Instruction::Select:
6377 case Instruction::PHI:
6378 return false;
6379 case Instruction::Ret:
6380 return User->getFunction()->getAttributes().getRetNoFPClass() &
6382 case Instruction::Call:
6383 case Instruction::Invoke: {
6384 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6385 switch (II->getIntrinsicID()) {
6386 case Intrinsic::fabs:
6387 return true;
6388 case Intrinsic::copysign:
6389 return U.getOperandNo() == 0;
6390 // Other proper FP math intrinsics ignore the sign bit of NaN.
6391 case Intrinsic::maxnum:
6392 case Intrinsic::minnum:
6393 case Intrinsic::maximum:
6394 case Intrinsic::minimum:
6395 case Intrinsic::maximumnum:
6396 case Intrinsic::minimumnum:
6397 case Intrinsic::canonicalize:
6398 case Intrinsic::fma:
6399 case Intrinsic::fmuladd:
6400 case Intrinsic::sqrt:
6401 case Intrinsic::pow:
6402 case Intrinsic::powi:
6403 case Intrinsic::fptoui_sat:
6404 case Intrinsic::fptosi_sat:
6405 case Intrinsic::is_fpclass:
6406 case Intrinsic::vp_is_fpclass:
6407 return true;
6408 default:
6409 return false;
6410 }
6411 }
6412
6413 FPClassTest NoFPClass =
6414 cast<CallBase>(User)->getParamNoFPClass(U.getOperandNo());
6415 return NoFPClass & FPClassTest::fcNan;
6416 }
6417 default:
6418 return false;
6419 }
6420}
6421
6423
6424 // All byte-wide stores are splatable, even of arbitrary variables.
6425 if (V->getType()->isIntegerTy(8))
6426 return V;
6427
6428 LLVMContext &Ctx = V->getContext();
6429
6430 // Undef don't care.
6431 auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
6432 if (isa<UndefValue>(V))
6433 return UndefInt8;
6434
6435 // Return poison for zero-sized type.
6436 if (DL.getTypeStoreSize(V->getType()).isZero())
6437 return PoisonValue::get(Type::getInt8Ty(Ctx));
6438
6440 if (!C) {
6441 // Conceptually, we could handle things like:
6442 // %a = zext i8 %X to i16
6443 // %b = shl i16 %a, 8
6444 // %c = or i16 %a, %b
6445 // but until there is an example that actually needs this, it doesn't seem
6446 // worth worrying about.
6447 return nullptr;
6448 }
6449
6450 // Handle 'null' ConstantArrayZero etc.
6451 if (C->isNullValue())
6453
6454 // Constant floating-point values can be handled as integer values if the
6455 // corresponding integer value is "byteable". An important case is 0.0.
6456 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
6457 Type *Ty = nullptr;
6458 if (CFP->getType()->isHalfTy())
6459 Ty = Type::getInt16Ty(Ctx);
6460 else if (CFP->getType()->isFloatTy())
6461 Ty = Type::getInt32Ty(Ctx);
6462 else if (CFP->getType()->isDoubleTy())
6463 Ty = Type::getInt64Ty(Ctx);
6464 // Don't handle long double formats, which have strange constraints.
6465 return Ty ? isBytewiseValue(ConstantExpr::getBitCast(CFP, Ty), DL)
6466 : nullptr;
6467 }
6468
6469 // We can handle constant integers that are multiple of 8 bits.
6470 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
6471 if (CI->getBitWidth() % 8 == 0) {
6472 assert(CI->getBitWidth() > 8 && "8 bits should be handled above!");
6473 if (!CI->getValue().isSplat(8))
6474 return nullptr;
6475 return ConstantInt::get(Ctx, CI->getValue().trunc(8));
6476 }
6477 }
6478
6479 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
6480 if (CE->getOpcode() == Instruction::IntToPtr) {
6481 if (auto *PtrTy = dyn_cast<PointerType>(CE->getType())) {
6482 unsigned BitWidth = DL.getPointerSizeInBits(PtrTy->getAddressSpace());
6484 CE->getOperand(0), Type::getIntNTy(Ctx, BitWidth), false, DL))
6485 return isBytewiseValue(Op, DL);
6486 }
6487 }
6488 }
6489
6490 auto Merge = [&](Value *LHS, Value *RHS) -> Value * {
6491 if (LHS == RHS)
6492 return LHS;
6493 if (!LHS || !RHS)
6494 return nullptr;
6495 if (LHS == UndefInt8)
6496 return RHS;
6497 if (RHS == UndefInt8)
6498 return LHS;
6499 return nullptr;
6500 };
6501
6503 Value *Val = UndefInt8;
6504 for (uint64_t I = 0, E = CA->getNumElements(); I != E; ++I)
6505 if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))
6506 return nullptr;
6507 return Val;
6508 }
6509
6511 Value *Val = UndefInt8;
6512 for (Value *Op : C->operands())
6513 if (!(Val = Merge(Val, isBytewiseValue(Op, DL))))
6514 return nullptr;
6515 return Val;
6516 }
6517
6518 // Don't try to handle the handful of other constants.
6519 return nullptr;
6520}
6521
6522// This is the recursive version of BuildSubAggregate. It takes a few different
6523// arguments. Idxs is the index within the nested struct From that we are
6524// looking at now (which is of type IndexedType). IdxSkip is the number of
6525// indices from Idxs that should be left out when inserting into the resulting
6526// struct. To is the result struct built so far, new insertvalue instructions
6527// build on that.
6528static Value *BuildSubAggregate(Value *From, Value *To, Type *IndexedType,
6530 unsigned IdxSkip,
6531 BasicBlock::iterator InsertBefore) {
6532 StructType *STy = dyn_cast<StructType>(IndexedType);
6533 if (STy) {
6534 // Save the original To argument so we can modify it
6535 Value *OrigTo = To;
6536 // General case, the type indexed by Idxs is a struct
6537 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
6538 // Process each struct element recursively
6539 Idxs.push_back(i);
6540 Value *PrevTo = To;
6541 To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
6542 InsertBefore);
6543 Idxs.pop_back();
6544 if (!To) {
6545 // Couldn't find any inserted value for this index? Cleanup
6546 while (PrevTo != OrigTo) {
6548 PrevTo = Del->getAggregateOperand();
6549 Del->eraseFromParent();
6550 }
6551 // Stop processing elements
6552 break;
6553 }
6554 }
6555 // If we successfully found a value for each of our subaggregates
6556 if (To)
6557 return To;
6558 }
6559 // Base case, the type indexed by SourceIdxs is not a struct, or not all of
6560 // the struct's elements had a value that was inserted directly. In the latter
6561 // case, perhaps we can't determine each of the subelements individually, but
6562 // we might be able to find the complete struct somewhere.
6563
6564 // Find the value that is at that particular spot
6565 Value *V = FindInsertedValue(From, Idxs);
6566
6567 if (!V)
6568 return nullptr;
6569
6570 // Insert the value in the new (sub) aggregate
6571 return InsertValueInst::Create(To, V, ArrayRef(Idxs).slice(IdxSkip), "tmp",
6572 InsertBefore);
6573}
6574
6575// This helper takes a nested struct and extracts a part of it (which is again a
6576// struct) into a new value. For example, given the struct:
6577// { a, { b, { c, d }, e } }
6578// and the indices "1, 1" this returns
6579// { c, d }.
6580//
6581// It does this by inserting an insertvalue for each element in the resulting
6582// struct, as opposed to just inserting a single struct. This will only work if
6583// each of the elements of the substruct are known (ie, inserted into From by an
6584// insertvalue instruction somewhere).
6585//
6586// All inserted insertvalue instructions are inserted before InsertBefore
6588 BasicBlock::iterator InsertBefore) {
6589 Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
6590 idx_range);
6591 Value *To = PoisonValue::get(IndexedType);
6592 SmallVector<unsigned, 10> Idxs(idx_range);
6593 unsigned IdxSkip = Idxs.size();
6594
6595 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
6596}
6597
6598/// Given an aggregate and a sequence of indices, see if the scalar value
6599/// indexed is already around as a register, for example if it was inserted
6600/// directly into the aggregate.
6601///
6602/// If InsertBefore is not null, this function will duplicate (modified)
6603/// insertvalues when a part of a nested struct is extracted.
6604Value *
6606 std::optional<BasicBlock::iterator> InsertBefore) {
6607 // Nothing to index? Just return V then (this is useful at the end of our
6608 // recursion).
6609 if (idx_range.empty())
6610 return V;
6611 // We have indices, so V should have an indexable type.
6612 assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
6613 "Not looking at a struct or array?");
6614 assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
6615 "Invalid indices for type?");
6616
6617 if (Constant *C = dyn_cast<Constant>(V)) {
6618 C = C->getAggregateElement(idx_range[0]);
6619 if (!C) return nullptr;
6620 return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
6621 }
6622
6624 // Loop the indices for the insertvalue instruction in parallel with the
6625 // requested indices
6626 const unsigned *req_idx = idx_range.begin();
6627 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
6628 i != e; ++i, ++req_idx) {
6629 if (req_idx == idx_range.end()) {
6630 // We can't handle this without inserting insertvalues
6631 if (!InsertBefore)
6632 return nullptr;
6633
6634 // The requested index identifies a part of a nested aggregate. Handle
6635 // this specially. For example,
6636 // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
6637 // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
6638 // %C = extractvalue {i32, { i32, i32 } } %B, 1
6639 // This can be changed into
6640 // %A = insertvalue {i32, i32 } undef, i32 10, 0
6641 // %C = insertvalue {i32, i32 } %A, i32 11, 1
6642 // which allows the unused 0,0 element from the nested struct to be
6643 // removed.
6644 return BuildSubAggregate(V, ArrayRef(idx_range.begin(), req_idx),
6645 *InsertBefore);
6646 }
6647
6648 // This insert value inserts something else than what we are looking for.
6649 // See if the (aggregate) value inserted into has the value we are
6650 // looking for, then.
6651 if (*req_idx != *i)
6652 return FindInsertedValue(I->getAggregateOperand(), idx_range,
6653 InsertBefore);
6654 }
6655 // If we end up here, the indices of the insertvalue match with those
6656 // requested (though possibly only partially). Now we recursively look at
6657 // the inserted value, passing any remaining indices.
6658 return FindInsertedValue(I->getInsertedValueOperand(),
6659 ArrayRef(req_idx, idx_range.end()), InsertBefore);
6660 }
6661
6663 // If we're extracting a value from an aggregate that was extracted from
6664 // something else, we can extract from that something else directly instead.
6665 // However, we will need to chain I's indices with the requested indices.
6666
6667 // Calculate the number of indices required
6668 unsigned size = I->getNumIndices() + idx_range.size();
6669 // Allocate some space to put the new indices in
6671 Idxs.reserve(size);
6672 // Add indices from the extract value instruction
6673 Idxs.append(I->idx_begin(), I->idx_end());
6674
6675 // Add requested indices
6676 Idxs.append(idx_range.begin(), idx_range.end());
6677
6678 assert(Idxs.size() == size
6679 && "Number of indices added not correct?");
6680
6681 return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
6682 }
6683 // Otherwise, we don't know (such as, extracting from a function return value
6684 // or load instruction)
6685 return nullptr;
6686}
6687
6688// If V refers to an initialized global constant, set Slice either to
6689// its initializer if the size of its elements equals ElementSize, or,
6690// for ElementSize == 8, to its representation as an array of unsiged
6691// char. Return true on success.
6692// Offset is in the unit "nr of ElementSize sized elements".
6695 unsigned ElementSize, uint64_t Offset) {
6696 assert(V && "V should not be null.");
6697 assert((ElementSize % 8) == 0 &&
6698 "ElementSize expected to be a multiple of the size of a byte.");
6699 unsigned ElementSizeInBytes = ElementSize / 8;
6700
6701 // Drill down into the pointer expression V, ignoring any intervening
6702 // casts, and determine the identity of the object it references along
6703 // with the cumulative byte offset into it.
6704 const GlobalVariable *GV =
6706 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
6707 // Fail if V is not based on constant global object.
6708 return false;
6709
6710 const DataLayout &DL = GV->getDataLayout();
6711 APInt Off(DL.getIndexTypeSizeInBits(V->getType()), 0);
6712
6713 if (GV != V->stripAndAccumulateConstantOffsets(DL, Off,
6714 /*AllowNonInbounds*/ true))
6715 // Fail if a constant offset could not be determined.
6716 return false;
6717
6718 uint64_t StartIdx = Off.getLimitedValue();
6719 if (StartIdx == UINT64_MAX)
6720 // Fail if the constant offset is excessive.
6721 return false;
6722
6723 // Off/StartIdx is in the unit of bytes. So we need to convert to number of
6724 // elements. Simply bail out if that isn't possible.
6725 if ((StartIdx % ElementSizeInBytes) != 0)
6726 return false;
6727
6728 Offset += StartIdx / ElementSizeInBytes;
6729 ConstantDataArray *Array = nullptr;
6730 ArrayType *ArrayTy = nullptr;
6731
6732 if (GV->getInitializer()->isNullValue()) {
6733 Type *GVTy = GV->getValueType();
6734 uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy).getFixedValue();
6735 uint64_t Length = SizeInBytes / ElementSizeInBytes;
6736
6737 Slice.Array = nullptr;
6738 Slice.Offset = 0;
6739 // Return an empty Slice for undersized constants to let callers
6740 // transform even undefined library calls into simpler, well-defined
6741 // expressions. This is preferable to making the calls although it
6742 // prevents sanitizers from detecting such calls.
6743 Slice.Length = Length < Offset ? 0 : Length - Offset;
6744 return true;
6745 }
6746
6747 auto *Init = const_cast<Constant *>(GV->getInitializer());
6748 if (auto *ArrayInit = dyn_cast<ConstantDataArray>(Init)) {
6749 Type *InitElTy = ArrayInit->getElementType();
6750 if (InitElTy->isIntegerTy(ElementSize)) {
6751 // If Init is an initializer for an array of the expected type
6752 // and size, use it as is.
6753 Array = ArrayInit;
6754 ArrayTy = ArrayInit->getType();
6755 }
6756 }
6757
6758 if (!Array) {
6759 if (ElementSize != 8)
6760 // TODO: Handle conversions to larger integral types.
6761 return false;
6762
6763 // Otherwise extract the portion of the initializer starting
6764 // at Offset as an array of bytes, and reset Offset.
6766 if (!Init)
6767 return false;
6768
6769 Offset = 0;
6771 ArrayTy = dyn_cast<ArrayType>(Init->getType());
6772 }
6773
6774 uint64_t NumElts = ArrayTy->getArrayNumElements();
6775 if (Offset > NumElts)
6776 return false;
6777
6778 Slice.Array = Array;
6779 Slice.Offset = Offset;
6780 Slice.Length = NumElts - Offset;
6781 return true;
6782}
6783
6784/// Extract bytes from the initializer of the constant array V, which need
6785/// not be a nul-terminated string. On success, store the bytes in Str and
6786/// return true. When TrimAtNul is set, Str will contain only the bytes up
6787/// to but not including the first nul. Return false on failure.
6789 bool TrimAtNul) {
6791 if (!getConstantDataArrayInfo(V, Slice, 8))
6792 return false;
6793
6794 if (Slice.Array == nullptr) {
6795 if (TrimAtNul) {
6796 // Return a nul-terminated string even for an empty Slice. This is
6797 // safe because all existing SimplifyLibcalls callers require string
6798 // arguments and the behavior of the functions they fold is undefined
6799 // otherwise. Folding the calls this way is preferable to making
6800 // the undefined library calls, even though it prevents sanitizers
6801 // from reporting such calls.
6802 Str = StringRef();
6803 return true;
6804 }
6805 if (Slice.Length == 1) {
6806 Str = StringRef("", 1);
6807 return true;
6808 }
6809 // We cannot instantiate a StringRef as we do not have an appropriate string
6810 // of 0s at hand.
6811 return false;
6812 }
6813
6814 // Start out with the entire array in the StringRef.
6815 Str = Slice.Array->getAsString();
6816 // Skip over 'offset' bytes.
6817 Str = Str.substr(Slice.Offset);
6818
6819 if (TrimAtNul) {
6820 // Trim off the \0 and anything after it. If the array is not nul
6821 // terminated, we just return the whole end of string. The client may know
6822 // some other way that the string is length-bound.
6823 Str = Str.substr(0, Str.find('\0'));
6824 }
6825 return true;
6826}
6827
6828// These next two are very similar to the above, but also look through PHI
6829// nodes.
6830// TODO: See if we can integrate these two together.
6831
6832/// If we can compute the length of the string pointed to by
6833/// the specified pointer, return 'len+1'. If we can't, return 0.
6836 unsigned CharSize) {
6837 // Look through noop bitcast instructions.
6838 V = V->stripPointerCasts();
6839
6840 // If this is a PHI node, there are two cases: either we have already seen it
6841 // or we haven't.
6842 if (const PHINode *PN = dyn_cast<PHINode>(V)) {
6843 if (!PHIs.insert(PN).second)
6844 return ~0ULL; // already in the set.
6845
6846 // If it was new, see if all the input strings are the same length.
6847 uint64_t LenSoFar = ~0ULL;
6848 for (Value *IncValue : PN->incoming_values()) {
6849 uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize);
6850 if (Len == 0) return 0; // Unknown length -> unknown.
6851
6852 if (Len == ~0ULL) continue;
6853
6854 if (Len != LenSoFar && LenSoFar != ~0ULL)
6855 return 0; // Disagree -> unknown.
6856 LenSoFar = Len;
6857 }
6858
6859 // Success, all agree.
6860 return LenSoFar;
6861 }
6862
6863 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
6864 if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
6865 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);
6866 if (Len1 == 0) return 0;
6867 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);
6868 if (Len2 == 0) return 0;
6869 if (Len1 == ~0ULL) return Len2;
6870 if (Len2 == ~0ULL) return Len1;
6871 if (Len1 != Len2) return 0;
6872 return Len1;
6873 }
6874
6875 // Otherwise, see if we can read the string.
6877 if (!getConstantDataArrayInfo(V, Slice, CharSize))
6878 return 0;
6879
6880 if (Slice.Array == nullptr)
6881 // Zeroinitializer (including an empty one).
6882 return 1;
6883
6884 // Search for the first nul character. Return a conservative result even
6885 // when there is no nul. This is safe since otherwise the string function
6886 // being folded such as strlen is undefined, and can be preferable to
6887 // making the undefined library call.
6888 unsigned NullIndex = 0;
6889 for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) {
6890 if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0)
6891 break;
6892 }
6893
6894 return NullIndex + 1;
6895}
6896
6897/// If we can compute the length of the string pointed to by
6898/// the specified pointer, return 'len+1'. If we can't, return 0.
6899uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) {
6900 if (!V->getType()->isPointerTy())
6901 return 0;
6902
6904 uint64_t Len = GetStringLengthH(V, PHIs, CharSize);
6905 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
6906 // an empty string as a length.
6907 return Len == ~0ULL ? 1 : Len;
6908}
6909
6910const Value *
6912 bool MustPreserveNullness) {
6913 assert(Call &&
6914 "getArgumentAliasingToReturnedPointer only works on nonnull calls");
6915 if (const Value *RV = Call->getReturnedArgOperand())
6916 return RV;
6917 // This can be used only as a aliasing property.
6919 Call, MustPreserveNullness))
6920 return Call->getArgOperand(0);
6921 return nullptr;
6922}
6923
6925 const CallBase *Call, bool MustPreserveNullness) {
6926 switch (Call->getIntrinsicID()) {
6927 case Intrinsic::launder_invariant_group:
6928 case Intrinsic::strip_invariant_group:
6929 case Intrinsic::aarch64_irg:
6930 case Intrinsic::aarch64_tagp:
6931 // The amdgcn_make_buffer_rsrc function does not alter the address of the
6932 // input pointer (and thus preserve null-ness for the purposes of escape
6933 // analysis, which is where the MustPreserveNullness flag comes in to play).
6934 // However, it will not necessarily map ptr addrspace(N) null to ptr
6935 // addrspace(8) null, aka the "null descriptor", which has "all loads return
6936 // 0, all stores are dropped" semantics. Given the context of this intrinsic
6937 // list, no one should be relying on such a strict interpretation of
6938 // MustPreserveNullness (and, at time of writing, they are not), but we
6939 // document this fact out of an abundance of caution.
6940 case Intrinsic::amdgcn_make_buffer_rsrc:
6941 return true;
6942 case Intrinsic::ptrmask:
6943 return !MustPreserveNullness;
6944 case Intrinsic::threadlocal_address:
6945 // The underlying variable changes with thread ID. The Thread ID may change
6946 // at coroutine suspend points.
6947 return !Call->getParent()->getParent()->isPresplitCoroutine();
6948 default:
6949 return false;
6950 }
6951}
6952
6953/// \p PN defines a loop-variant pointer to an object. Check if the
6954/// previous iteration of the loop was referring to the same object as \p PN.
6956 const LoopInfo *LI) {
6957 // Find the loop-defined value.
6958 Loop *L = LI->getLoopFor(PN->getParent());
6959 if (PN->getNumIncomingValues() != 2)
6960 return true;
6961
6962 // Find the value from previous iteration.
6963 auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
6964 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6965 PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
6966 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6967 return true;
6968
6969 // If a new pointer is loaded in the loop, the pointer references a different
6970 // object in every iteration. E.g.:
6971 // for (i)
6972 // int *p = a[i];
6973 // ...
6974 if (auto *Load = dyn_cast<LoadInst>(PrevValue))
6975 if (!L->isLoopInvariant(Load->getPointerOperand()))
6976 return false;
6977 return true;
6978}
6979
6980const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup) {
6981 for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
6982 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
6983 const Value *PtrOp = GEP->getPointerOperand();
6984 if (!PtrOp->getType()->isPointerTy()) // Only handle scalar pointer base.
6985 return V;
6986 V = PtrOp;
6987 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
6988 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
6989 Value *NewV = cast<Operator>(V)->getOperand(0);
6990 if (!NewV->getType()->isPointerTy())
6991 return V;
6992 V = NewV;
6993 } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
6994 if (GA->isInterposable())
6995 return V;
6996 V = GA->getAliasee();
6997 } else {
6998 if (auto *PHI = dyn_cast<PHINode>(V)) {
6999 // Look through single-arg phi nodes created by LCSSA.
7000 if (PHI->getNumIncomingValues() == 1) {
7001 V = PHI->getIncomingValue(0);
7002 continue;
7003 }
7004 } else if (auto *Call = dyn_cast<CallBase>(V)) {
7005 // CaptureTracking can know about special capturing properties of some
7006 // intrinsics like launder.invariant.group, that can't be expressed with
7007 // the attributes, but have properties like returning aliasing pointer.
7008 // Because some analysis may assume that nocaptured pointer is not
7009 // returned from some special intrinsic (because function would have to
7010 // be marked with returns attribute), it is crucial to use this function
7011 // because it should be in sync with CaptureTracking. Not using it may
7012 // cause weird miscompilations where 2 aliasing pointers are assumed to
7013 // noalias.
7014 if (auto *RP = getArgumentAliasingToReturnedPointer(Call, false)) {
7015 V = RP;
7016 continue;
7017 }
7018 }
7019
7020 return V;
7021 }
7022 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
7023 }
7024 return V;
7025}
7026
7029 const LoopInfo *LI, unsigned MaxLookup) {
7032 Worklist.push_back(V);
7033 do {
7034 const Value *P = Worklist.pop_back_val();
7035 P = getUnderlyingObject(P, MaxLookup);
7036
7037 if (!Visited.insert(P).second)
7038 continue;
7039
7040 if (auto *SI = dyn_cast<SelectInst>(P)) {
7041 Worklist.push_back(SI->getTrueValue());
7042 Worklist.push_back(SI->getFalseValue());
7043 continue;
7044 }
7045
7046 if (auto *PN = dyn_cast<PHINode>(P)) {
7047 // If this PHI changes the underlying object in every iteration of the
7048 // loop, don't look through it. Consider:
7049 // int **A;
7050 // for (i) {
7051 // Prev = Curr; // Prev = PHI (Prev_0, Curr)
7052 // Curr = A[i];
7053 // *Prev, *Curr;
7054 //
7055 // Prev is tracking Curr one iteration behind so they refer to different
7056 // underlying objects.
7057 if (!LI || !LI->isLoopHeader(PN->getParent()) ||
7059 append_range(Worklist, PN->incoming_values());
7060 else
7061 Objects.push_back(P);
7062 continue;
7063 }
7064
7065 Objects.push_back(P);
7066 } while (!Worklist.empty());
7067}
7068
7070 const unsigned MaxVisited = 8;
7071
7074 Worklist.push_back(V);
7075 const Value *Object = nullptr;
7076 // Used as fallback if we can't find a common underlying object through
7077 // recursion.
7078 bool First = true;
7079 const Value *FirstObject = getUnderlyingObject(V);
7080 do {
7081 const Value *P = Worklist.pop_back_val();
7082 P = First ? FirstObject : getUnderlyingObject(P);
7083 First = false;
7084
7085 if (!Visited.insert(P).second)
7086 continue;
7087
7088 if (Visited.size() == MaxVisited)
7089 return FirstObject;
7090
7091 if (auto *SI = dyn_cast<SelectInst>(P)) {
7092 Worklist.push_back(SI->getTrueValue());
7093 Worklist.push_back(SI->getFalseValue());
7094 continue;
7095 }
7096
7097 if (auto *PN = dyn_cast<PHINode>(P)) {
7098 append_range(Worklist, PN->incoming_values());
7099 continue;
7100 }
7101
7102 if (!Object)
7103 Object = P;
7104 else if (Object != P)
7105 return FirstObject;
7106 } while (!Worklist.empty());
7107
7108 return Object ? Object : FirstObject;
7109}
7110
7111/// This is the function that does the work of looking through basic
7112/// ptrtoint+arithmetic+inttoptr sequences.
7113static const Value *getUnderlyingObjectFromInt(const Value *V) {
7114 do {
7115 if (const Operator *U = dyn_cast<Operator>(V)) {
7116 // If we find a ptrtoint, we can transfer control back to the
7117 // regular getUnderlyingObjectFromInt.
7118 if (U->getOpcode() == Instruction::PtrToInt)
7119 return U->getOperand(0);
7120 // If we find an add of a constant, a multiplied value, or a phi, it's
7121 // likely that the other operand will lead us to the base
7122 // object. We don't have to worry about the case where the
7123 // object address is somehow being computed by the multiply,
7124 // because our callers only care when the result is an
7125 // identifiable object.
7126 if (U->getOpcode() != Instruction::Add ||
7127 (!isa<ConstantInt>(U->getOperand(1)) &&
7128 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
7129 !isa<PHINode>(U->getOperand(1))))
7130 return V;
7131 V = U->getOperand(0);
7132 } else {
7133 return V;
7134 }
7135 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
7136 } while (true);
7137}
7138
7139/// This is a wrapper around getUnderlyingObjects and adds support for basic
7140/// ptrtoint+arithmetic+inttoptr sequences.
7141/// It returns false if unidentified object is found in getUnderlyingObjects.
7143 SmallVectorImpl<Value *> &Objects) {
7145 SmallVector<const Value *, 4> Working(1, V);
7146 do {
7147 V = Working.pop_back_val();
7148
7150 getUnderlyingObjects(V, Objs);
7151
7152 for (const Value *V : Objs) {
7153 if (!Visited.insert(V).second)
7154 continue;
7155 if (Operator::getOpcode(V) == Instruction::IntToPtr) {
7156 const Value *O =
7157 getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
7158 if (O->getType()->isPointerTy()) {
7159 Working.push_back(O);
7160 continue;
7161 }
7162 }
7163 // If getUnderlyingObjects fails to find an identifiable object,
7164 // getUnderlyingObjectsForCodeGen also fails for safety.
7165 if (!isIdentifiedObject(V)) {
7166 Objects.clear();
7167 return false;
7168 }
7169 Objects.push_back(const_cast<Value *>(V));
7170 }
7171 } while (!Working.empty());
7172 return true;
7173}
7174
7176 AllocaInst *Result = nullptr;
7178 SmallVector<Value *, 4> Worklist;
7179
7180 auto AddWork = [&](Value *V) {
7181 if (Visited.insert(V).second)
7182 Worklist.push_back(V);
7183 };
7184
7185 AddWork(V);
7186 do {
7187 V = Worklist.pop_back_val();
7188 assert(Visited.count(V));
7189
7190 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
7191 if (Result && Result != AI)
7192 return nullptr;
7193 Result = AI;
7194 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
7195 AddWork(CI->getOperand(0));
7196 } else if (PHINode *PN = dyn_cast<PHINode>(V)) {
7197 for (Value *IncValue : PN->incoming_values())
7198 AddWork(IncValue);
7199 } else if (auto *SI = dyn_cast<SelectInst>(V)) {
7200 AddWork(SI->getTrueValue());
7201 AddWork(SI->getFalseValue());
7203 if (OffsetZero && !GEP->hasAllZeroIndices())
7204 return nullptr;
7205 AddWork(GEP->getPointerOperand());
7206 } else if (CallBase *CB = dyn_cast<CallBase>(V)) {
7207 Value *Returned = CB->getReturnedArgOperand();
7208 if (Returned)
7209 AddWork(Returned);
7210 else
7211 return nullptr;
7212 } else {
7213 return nullptr;
7214 }
7215 } while (!Worklist.empty());
7216
7217 return Result;
7218}
7219
7221 const Value *V, bool AllowLifetime, bool AllowDroppable) {
7222 for (const User *U : V->users()) {
7224 if (!II)
7225 return false;
7226
7227 if (AllowLifetime && II->isLifetimeStartOrEnd())
7228 continue;
7229
7230 if (AllowDroppable && II->isDroppable())
7231 continue;
7232
7233 return false;
7234 }
7235 return true;
7236}
7237
7240 V, /* AllowLifetime */ true, /* AllowDroppable */ false);
7241}
7244 V, /* AllowLifetime */ true, /* AllowDroppable */ true);
7245}
7246
7248 if (auto *II = dyn_cast<IntrinsicInst>(I))
7249 return isTriviallyVectorizable(II->getIntrinsicID());
7250 auto *Shuffle = dyn_cast<ShuffleVectorInst>(I);
7251 return (!Shuffle || Shuffle->isSelect()) &&
7253}
7254
7256 const Instruction *Inst, const Instruction *CtxI, AssumptionCache *AC,
7257 const DominatorTree *DT, const TargetLibraryInfo *TLI, bool UseVariableInfo,
7258 bool IgnoreUBImplyingAttrs) {
7259 return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI,
7260 AC, DT, TLI, UseVariableInfo,
7261 IgnoreUBImplyingAttrs);
7262}
7263
7265 unsigned Opcode, const Instruction *Inst, const Instruction *CtxI,
7266 AssumptionCache *AC, const DominatorTree *DT, const TargetLibraryInfo *TLI,
7267 bool UseVariableInfo, bool IgnoreUBImplyingAttrs) {
7268#ifndef NDEBUG
7269 if (Inst->getOpcode() != Opcode) {
7270 // Check that the operands are actually compatible with the Opcode override.
7271 auto hasEqualReturnAndLeadingOperandTypes =
7272 [](const Instruction *Inst, unsigned NumLeadingOperands) {
7273 if (Inst->getNumOperands() < NumLeadingOperands)
7274 return false;
7275 const Type *ExpectedType = Inst->getType();
7276 for (unsigned ItOp = 0; ItOp < NumLeadingOperands; ++ItOp)
7277 if (Inst->getOperand(ItOp)->getType() != ExpectedType)
7278 return false;
7279 return true;
7280 };
7282 hasEqualReturnAndLeadingOperandTypes(Inst, 2));
7283 assert(!Instruction::isUnaryOp(Opcode) ||
7284 hasEqualReturnAndLeadingOperandTypes(Inst, 1));
7285 }
7286#endif
7287
7288 switch (Opcode) {
7289 default:
7290 return true;
7291 case Instruction::UDiv:
7292 case Instruction::URem: {
7293 // x / y is undefined if y == 0.
7294 const APInt *V;
7295 if (match(Inst->getOperand(1), m_APInt(V)))
7296 return *V != 0;
7297 return false;
7298 }
7299 case Instruction::SDiv:
7300 case Instruction::SRem: {
7301 // x / y is undefined if y == 0 or x == INT_MIN and y == -1
7302 const APInt *Numerator, *Denominator;
7303 if (!match(Inst->getOperand(1), m_APInt(Denominator)))
7304 return false;
7305 // We cannot hoist this division if the denominator is 0.
7306 if (*Denominator == 0)
7307 return false;
7308 // It's safe to hoist if the denominator is not 0 or -1.
7309 if (!Denominator->isAllOnes())
7310 return true;
7311 // At this point we know that the denominator is -1. It is safe to hoist as
7312 // long we know that the numerator is not INT_MIN.
7313 if (match(Inst->getOperand(0), m_APInt(Numerator)))
7314 return !Numerator->isMinSignedValue();
7315 // The numerator *might* be MinSignedValue.
7316 return false;
7317 }
7318 case Instruction::Load: {
7319 if (!UseVariableInfo)
7320 return false;
7321
7322 const LoadInst *LI = dyn_cast<LoadInst>(Inst);
7323 if (!LI)
7324 return false;
7325 if (mustSuppressSpeculation(*LI))
7326 return false;
7327 const DataLayout &DL = LI->getDataLayout();
7329 LI->getType(), LI->getAlign(), DL,
7330 CtxI, AC, DT, TLI);
7331 }
7332 case Instruction::Call: {
7333 auto *CI = dyn_cast<const CallInst>(Inst);
7334 if (!CI)
7335 return false;
7336 const Function *Callee = CI->getCalledFunction();
7337
7338 // The called function could have undefined behavior or side-effects, even
7339 // if marked readnone nounwind.
7340 if (!Callee || !Callee->isSpeculatable())
7341 return false;
7342 // Since the operands may be changed after hoisting, undefined behavior may
7343 // be triggered by some UB-implying attributes.
7344 return IgnoreUBImplyingAttrs || !CI->hasUBImplyingAttrs();
7345 }
7346 case Instruction::VAArg:
7347 case Instruction::Alloca:
7348 case Instruction::Invoke:
7349 case Instruction::CallBr:
7350 case Instruction::PHI:
7351 case Instruction::Store:
7352 case Instruction::Ret:
7353 case Instruction::Br:
7354 case Instruction::IndirectBr:
7355 case Instruction::Switch:
7356 case Instruction::Unreachable:
7357 case Instruction::Fence:
7358 case Instruction::AtomicRMW:
7359 case Instruction::AtomicCmpXchg:
7360 case Instruction::LandingPad:
7361 case Instruction::Resume:
7362 case Instruction::CatchSwitch:
7363 case Instruction::CatchPad:
7364 case Instruction::CatchRet:
7365 case Instruction::CleanupPad:
7366 case Instruction::CleanupRet:
7367 return false; // Misc instructions which have effects
7368 }
7369}
7370
7372 if (I.mayReadOrWriteMemory())
7373 // Memory dependency possible
7374 return true;
7376 // Can't move above a maythrow call or infinite loop. Or if an
7377 // inalloca alloca, above a stacksave call.
7378 return true;
7380 // 1) Can't reorder two inf-loop calls, even if readonly
7381 // 2) Also can't reorder an inf-loop call below a instruction which isn't
7382 // safe to speculative execute. (Inverse of above)
7383 return true;
7384 return false;
7385}
7386
7387/// Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
7401
7402/// Combine constant ranges from computeConstantRange() and computeKnownBits().
7405 bool ForSigned,
7406 const SimplifyQuery &SQ) {
7407 ConstantRange CR1 =
7408 ConstantRange::fromKnownBits(V.getKnownBits(SQ), ForSigned);
7409 ConstantRange CR2 = computeConstantRange(V, ForSigned, SQ.IIQ.UseInstrInfo);
7412 return CR1.intersectWith(CR2, RangeType);
7413}
7414
7416 const Value *RHS,
7417 const SimplifyQuery &SQ,
7418 bool IsNSW) {
7419 ConstantRange LHSRange =
7420 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7421 ConstantRange RHSRange =
7422 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7423
7424 // mul nsw of two non-negative numbers is also nuw.
7425 if (IsNSW && LHSRange.isAllNonNegative() && RHSRange.isAllNonNegative())
7427
7428 return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange));
7429}
7430
7432 const Value *RHS,
7433 const SimplifyQuery &SQ) {
7434 // Multiplying n * m significant bits yields a result of n + m significant
7435 // bits. If the total number of significant bits does not exceed the
7436 // result bit width (minus 1), there is no overflow.
7437 // This means if we have enough leading sign bits in the operands
7438 // we can guarantee that the result does not overflow.
7439 // Ref: "Hacker's Delight" by Henry Warren
7440 unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
7441
7442 // Note that underestimating the number of sign bits gives a more
7443 // conservative answer.
7444 unsigned SignBits =
7445 ::ComputeNumSignBits(LHS, SQ) + ::ComputeNumSignBits(RHS, SQ);
7446
7447 // First handle the easy case: if we have enough sign bits there's
7448 // definitely no overflow.
7449 if (SignBits > BitWidth + 1)
7451
7452 // There are two ambiguous cases where there can be no overflow:
7453 // SignBits == BitWidth + 1 and
7454 // SignBits == BitWidth
7455 // The second case is difficult to check, therefore we only handle the
7456 // first case.
7457 if (SignBits == BitWidth + 1) {
7458 // It overflows only when both arguments are negative and the true
7459 // product is exactly the minimum negative number.
7460 // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
7461 // For simplicity we just check if at least one side is not negative.
7462 KnownBits LHSKnown = computeKnownBits(LHS, SQ);
7463 KnownBits RHSKnown = computeKnownBits(RHS, SQ);
7464 if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative())
7466 }
7468}
7469
7472 const WithCache<const Value *> &RHS,
7473 const SimplifyQuery &SQ) {
7474 ConstantRange LHSRange =
7475 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7476 ConstantRange RHSRange =
7477 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7478 return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange));
7479}
7480
7481static OverflowResult
7484 const AddOperator *Add, const SimplifyQuery &SQ) {
7485 if (Add && Add->hasNoSignedWrap()) {
7487 }
7488
7489 // If LHS and RHS each have at least two sign bits, the addition will look
7490 // like
7491 //
7492 // XX..... +
7493 // YY.....
7494 //
7495 // If the carry into the most significant position is 0, X and Y can't both
7496 // be 1 and therefore the carry out of the addition is also 0.
7497 //
7498 // If the carry into the most significant position is 1, X and Y can't both
7499 // be 0 and therefore the carry out of the addition is also 1.
7500 //
7501 // Since the carry into the most significant position is always equal to
7502 // the carry out of the addition, there is no signed overflow.
7503 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
7505
7506 ConstantRange LHSRange =
7507 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7508 ConstantRange RHSRange =
7509 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7510 OverflowResult OR =
7511 mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange));
7513 return OR;
7514
7515 // The remaining code needs Add to be available. Early returns if not so.
7516 if (!Add)
7518
7519 // If the sign of Add is the same as at least one of the operands, this add
7520 // CANNOT overflow. If this can be determined from the known bits of the
7521 // operands the above signedAddMayOverflow() check will have already done so.
7522 // The only other way to improve on the known bits is from an assumption, so
7523 // call computeKnownBitsFromContext() directly.
7524 bool LHSOrRHSKnownNonNegative =
7525 (LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative());
7526 bool LHSOrRHSKnownNegative =
7527 (LHSRange.isAllNegative() || RHSRange.isAllNegative());
7528 if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
7529 KnownBits AddKnown(LHSRange.getBitWidth());
7530 computeKnownBitsFromContext(Add, AddKnown, SQ);
7531 if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||
7532 (AddKnown.isNegative() && LHSOrRHSKnownNegative))
7534 }
7535
7537}
7538
7540 const Value *RHS,
7541 const SimplifyQuery &SQ) {
7542 // X - (X % ?)
7543 // The remainder of a value can't have greater magnitude than itself,
7544 // so the subtraction can't overflow.
7545
7546 // X - (X -nuw ?)
7547 // In the minimal case, this would simplify to "?", so there's no subtract
7548 // at all. But if this analysis is used to peek through casts, for example,
7549 // then determining no-overflow may allow other transforms.
7550
7551 // TODO: There are other patterns like this.
7552 // See simplifyICmpWithBinOpOnLHS() for candidates.
7553 if (match(RHS, m_URem(m_Specific(LHS), m_Value())) ||
7554 match(RHS, m_NUWSub(m_Specific(LHS), m_Value())))
7555 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7557
7558 if (auto C = isImpliedByDomCondition(CmpInst::ICMP_UGE, LHS, RHS, SQ.CxtI,
7559 SQ.DL)) {
7560 if (*C)
7563 }
7564
7565 ConstantRange LHSRange =
7566 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7567 ConstantRange RHSRange =
7568 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7569 return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange));
7570}
7571
7573 const Value *RHS,
7574 const SimplifyQuery &SQ) {
7575 // X - (X % ?)
7576 // The remainder of a value can't have greater magnitude than itself,
7577 // so the subtraction can't overflow.
7578
7579 // X - (X -nsw ?)
7580 // In the minimal case, this would simplify to "?", so there's no subtract
7581 // at all. But if this analysis is used to peek through casts, for example,
7582 // then determining no-overflow may allow other transforms.
7583 if (match(RHS, m_SRem(m_Specific(LHS), m_Value())) ||
7584 match(RHS, m_NSWSub(m_Specific(LHS), m_Value())))
7585 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7587
7588 // If LHS and RHS each have at least two sign bits, the subtraction
7589 // cannot overflow.
7590 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
7592
7593 ConstantRange LHSRange =
7594 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7595 ConstantRange RHSRange =
7596 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7597 return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange));
7598}
7599
7601 const DominatorTree &DT) {
7602 SmallVector<const BranchInst *, 2> GuardingBranches;
7604
7605 for (const User *U : WO->users()) {
7606 if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) {
7607 assert(EVI->getNumIndices() == 1 && "Obvious from CI's type");
7608
7609 if (EVI->getIndices()[0] == 0)
7610 Results.push_back(EVI);
7611 else {
7612 assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type");
7613
7614 for (const auto *U : EVI->users())
7615 if (const auto *B = dyn_cast<BranchInst>(U)) {
7616 assert(B->isConditional() && "How else is it using an i1?");
7617 GuardingBranches.push_back(B);
7618 }
7619 }
7620 } else {
7621 // We are using the aggregate directly in a way we don't want to analyze
7622 // here (storing it to a global, say).
7623 return false;
7624 }
7625 }
7626
7627 auto AllUsesGuardedByBranch = [&](const BranchInst *BI) {
7628 BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1));
7629 if (!NoWrapEdge.isSingleEdge())
7630 return false;
7631
7632 // Check if all users of the add are provably no-wrap.
7633 for (const auto *Result : Results) {
7634 // If the extractvalue itself is not executed on overflow, the we don't
7635 // need to check each use separately, since domination is transitive.
7636 if (DT.dominates(NoWrapEdge, Result->getParent()))
7637 continue;
7638
7639 for (const auto &RU : Result->uses())
7640 if (!DT.dominates(NoWrapEdge, RU))
7641 return false;
7642 }
7643
7644 return true;
7645 };
7646
7647 return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
7648}
7649
7650/// Shifts return poison if shiftwidth is larger than the bitwidth.
7651static bool shiftAmountKnownInRange(const Value *ShiftAmount) {
7652 auto *C = dyn_cast<Constant>(ShiftAmount);
7653 if (!C)
7654 return false;
7655
7656 // Shifts return poison if shiftwidth is larger than the bitwidth.
7658 if (auto *FVTy = dyn_cast<FixedVectorType>(C->getType())) {
7659 unsigned NumElts = FVTy->getNumElements();
7660 for (unsigned i = 0; i < NumElts; ++i)
7661 ShiftAmounts.push_back(C->getAggregateElement(i));
7662 } else if (isa<ScalableVectorType>(C->getType()))
7663 return false; // Can't tell, just return false to be safe
7664 else
7665 ShiftAmounts.push_back(C);
7666
7667 bool Safe = llvm::all_of(ShiftAmounts, [](const Constant *C) {
7668 auto *CI = dyn_cast_or_null<ConstantInt>(C);
7669 return CI && CI->getValue().ult(C->getType()->getIntegerBitWidth());
7670 });
7671
7672 return Safe;
7673}
7674
7680
7682 return (unsigned(Kind) & unsigned(UndefPoisonKind::PoisonOnly)) != 0;
7683}
7684
7686 return (unsigned(Kind) & unsigned(UndefPoisonKind::UndefOnly)) != 0;
7687}
7688
7690 bool ConsiderFlagsAndMetadata) {
7691
7692 if (ConsiderFlagsAndMetadata && includesPoison(Kind) &&
7693 Op->hasPoisonGeneratingAnnotations())
7694 return true;
7695
7696 unsigned Opcode = Op->getOpcode();
7697
7698 // Check whether opcode is a poison/undef-generating operation
7699 switch (Opcode) {
7700 case Instruction::Shl:
7701 case Instruction::AShr:
7702 case Instruction::LShr:
7703 return includesPoison(Kind) && !shiftAmountKnownInRange(Op->getOperand(1));
7704 case Instruction::FPToSI:
7705 case Instruction::FPToUI:
7706 // fptosi/ui yields poison if the resulting value does not fit in the
7707 // destination type.
7708 return true;
7709 case Instruction::Call:
7710 if (auto *II = dyn_cast<IntrinsicInst>(Op)) {
7711 switch (II->getIntrinsicID()) {
7712 // TODO: Add more intrinsics.
7713 case Intrinsic::ctlz:
7714 case Intrinsic::cttz:
7715 case Intrinsic::abs:
7716 if (cast<ConstantInt>(II->getArgOperand(1))->isNullValue())
7717 return false;
7718 break;
7719 case Intrinsic::sshl_sat:
7720 case Intrinsic::ushl_sat:
7721 if (!includesPoison(Kind) ||
7722 shiftAmountKnownInRange(II->getArgOperand(1)))
7723 return false;
7724 break;
7725 }
7726 }
7727 [[fallthrough]];
7728 case Instruction::CallBr:
7729 case Instruction::Invoke: {
7730 const auto *CB = cast<CallBase>(Op);
7731 return !CB->hasRetAttr(Attribute::NoUndef) &&
7732 !CB->hasFnAttr(Attribute::NoCreateUndefOrPoison);
7733 }
7734 case Instruction::InsertElement:
7735 case Instruction::ExtractElement: {
7736 // If index exceeds the length of the vector, it returns poison
7737 auto *VTy = cast<VectorType>(Op->getOperand(0)->getType());
7738 unsigned IdxOp = Op->getOpcode() == Instruction::InsertElement ? 2 : 1;
7739 auto *Idx = dyn_cast<ConstantInt>(Op->getOperand(IdxOp));
7740 if (includesPoison(Kind))
7741 return !Idx ||
7742 Idx->getValue().uge(VTy->getElementCount().getKnownMinValue());
7743 return false;
7744 }
7745 case Instruction::ShuffleVector: {
7747 ? cast<ConstantExpr>(Op)->getShuffleMask()
7748 : cast<ShuffleVectorInst>(Op)->getShuffleMask();
7749 return includesPoison(Kind) && is_contained(Mask, PoisonMaskElem);
7750 }
7751 case Instruction::FNeg:
7752 case Instruction::PHI:
7753 case Instruction::Select:
7754 case Instruction::ExtractValue:
7755 case Instruction::InsertValue:
7756 case Instruction::Freeze:
7757 case Instruction::ICmp:
7758 case Instruction::FCmp:
7759 case Instruction::GetElementPtr:
7760 return false;
7761 case Instruction::AddrSpaceCast:
7762 return true;
7763 default: {
7764 const auto *CE = dyn_cast<ConstantExpr>(Op);
7765 if (isa<CastInst>(Op) || (CE && CE->isCast()))
7766 return false;
7767 else if (Instruction::isBinaryOp(Opcode))
7768 return false;
7769 // Be conservative and return true.
7770 return true;
7771 }
7772 }
7773}
7774
7776 bool ConsiderFlagsAndMetadata) {
7777 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::UndefOrPoison,
7778 ConsiderFlagsAndMetadata);
7779}
7780
7781bool llvm::canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata) {
7782 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::PoisonOnly,
7783 ConsiderFlagsAndMetadata);
7784}
7785
7786static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V,
7787 unsigned Depth) {
7788 if (ValAssumedPoison == V)
7789 return true;
7790
7791 const unsigned MaxDepth = 2;
7792 if (Depth >= MaxDepth)
7793 return false;
7794
7795 if (const auto *I = dyn_cast<Instruction>(V)) {
7796 if (any_of(I->operands(), [=](const Use &Op) {
7797 return propagatesPoison(Op) &&
7798 directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1);
7799 }))
7800 return true;
7801
7802 // V = extractvalue V0, idx
7803 // V2 = extractvalue V0, idx2
7804 // V0's elements are all poison or not. (e.g., add_with_overflow)
7805 const WithOverflowInst *II;
7807 (match(ValAssumedPoison, m_ExtractValue(m_Specific(II))) ||
7808 llvm::is_contained(II->args(), ValAssumedPoison)))
7809 return true;
7810 }
7811 return false;
7812}
7813
7814static bool impliesPoison(const Value *ValAssumedPoison, const Value *V,
7815 unsigned Depth) {
7816 if (isGuaranteedNotToBePoison(ValAssumedPoison))
7817 return true;
7818
7819 if (directlyImpliesPoison(ValAssumedPoison, V, /* Depth */ 0))
7820 return true;
7821
7822 const unsigned MaxDepth = 2;
7823 if (Depth >= MaxDepth)
7824 return false;
7825
7826 const auto *I = dyn_cast<Instruction>(ValAssumedPoison);
7827 if (I && !canCreatePoison(cast<Operator>(I))) {
7828 return all_of(I->operands(), [=](const Value *Op) {
7829 return impliesPoison(Op, V, Depth + 1);
7830 });
7831 }
7832 return false;
7833}
7834
7835bool llvm::impliesPoison(const Value *ValAssumedPoison, const Value *V) {
7836 return ::impliesPoison(ValAssumedPoison, V, /* Depth */ 0);
7837}
7838
7839static bool programUndefinedIfUndefOrPoison(const Value *V, bool PoisonOnly);
7840
7842 const Value *V, AssumptionCache *AC, const Instruction *CtxI,
7843 const DominatorTree *DT, unsigned Depth, UndefPoisonKind Kind) {
7845 return false;
7846
7847 if (isa<MetadataAsValue>(V))
7848 return false;
7849
7850 if (const auto *A = dyn_cast<Argument>(V)) {
7851 if (A->hasAttribute(Attribute::NoUndef) ||
7852 A->hasAttribute(Attribute::Dereferenceable) ||
7853 A->hasAttribute(Attribute::DereferenceableOrNull))
7854 return true;
7855 }
7856
7857 if (auto *C = dyn_cast<Constant>(V)) {
7858 if (isa<PoisonValue>(C))
7859 return !includesPoison(Kind);
7860
7861 if (isa<UndefValue>(C))
7862 return !includesUndef(Kind);
7863
7866 return true;
7867
7868 if (C->getType()->isVectorTy()) {
7869 if (isa<ConstantExpr>(C)) {
7870 // Scalable vectors can use a ConstantExpr to build a splat.
7871 if (Constant *SplatC = C->getSplatValue())
7872 if (isa<ConstantInt>(SplatC) || isa<ConstantFP>(SplatC))
7873 return true;
7874 } else {
7875 if (includesUndef(Kind) && C->containsUndefElement())
7876 return false;
7877 if (includesPoison(Kind) && C->containsPoisonElement())
7878 return false;
7879 return !C->containsConstantExpression();
7880 }
7881 }
7882 }
7883
7884 // Strip cast operations from a pointer value.
7885 // Note that stripPointerCastsSameRepresentation can strip off getelementptr
7886 // inbounds with zero offset. To guarantee that the result isn't poison, the
7887 // stripped pointer is checked as it has to be pointing into an allocated
7888 // object or be null `null` to ensure `inbounds` getelement pointers with a
7889 // zero offset could not produce poison.
7890 // It can strip off addrspacecast that do not change bit representation as
7891 // well. We believe that such addrspacecast is equivalent to no-op.
7892 auto *StrippedV = V->stripPointerCastsSameRepresentation();
7893 if (isa<AllocaInst>(StrippedV) || isa<GlobalVariable>(StrippedV) ||
7894 isa<Function>(StrippedV) || isa<ConstantPointerNull>(StrippedV))
7895 return true;
7896
7897 auto OpCheck = [&](const Value *V) {
7898 return isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth + 1, Kind);
7899 };
7900
7901 if (auto *Opr = dyn_cast<Operator>(V)) {
7902 // If the value is a freeze instruction, then it can never
7903 // be undef or poison.
7904 if (isa<FreezeInst>(V))
7905 return true;
7906
7907 if (const auto *CB = dyn_cast<CallBase>(V)) {
7908 if (CB->hasRetAttr(Attribute::NoUndef) ||
7909 CB->hasRetAttr(Attribute::Dereferenceable) ||
7910 CB->hasRetAttr(Attribute::DereferenceableOrNull))
7911 return true;
7912 }
7913
7914 if (!::canCreateUndefOrPoison(Opr, Kind,
7915 /*ConsiderFlagsAndMetadata=*/true)) {
7916 if (const auto *PN = dyn_cast<PHINode>(V)) {
7917 unsigned Num = PN->getNumIncomingValues();
7918 bool IsWellDefined = true;
7919 for (unsigned i = 0; i < Num; ++i) {
7920 if (PN == PN->getIncomingValue(i))
7921 continue;
7922 auto *TI = PN->getIncomingBlock(i)->getTerminator();
7923 if (!isGuaranteedNotToBeUndefOrPoison(PN->getIncomingValue(i), AC, TI,
7924 DT, Depth + 1, Kind)) {
7925 IsWellDefined = false;
7926 break;
7927 }
7928 }
7929 if (IsWellDefined)
7930 return true;
7931 } else if (auto *Splat = isa<ShuffleVectorInst>(Opr) ? getSplatValue(Opr)
7932 : nullptr) {
7933 // For splats we only need to check the value being splatted.
7934 if (OpCheck(Splat))
7935 return true;
7936 } else if (all_of(Opr->operands(), OpCheck))
7937 return true;
7938 }
7939 }
7940
7941 if (auto *I = dyn_cast<LoadInst>(V))
7942 if (I->hasMetadata(LLVMContext::MD_noundef) ||
7943 I->hasMetadata(LLVMContext::MD_dereferenceable) ||
7944 I->hasMetadata(LLVMContext::MD_dereferenceable_or_null))
7945 return true;
7946
7948 return true;
7949
7950 // CxtI may be null or a cloned instruction.
7951 if (!CtxI || !CtxI->getParent() || !DT)
7952 return false;
7953
7954 auto *DNode = DT->getNode(CtxI->getParent());
7955 if (!DNode)
7956 // Unreachable block
7957 return false;
7958
7959 // If V is used as a branch condition before reaching CtxI, V cannot be
7960 // undef or poison.
7961 // br V, BB1, BB2
7962 // BB1:
7963 // CtxI ; V cannot be undef or poison here
7964 auto *Dominator = DNode->getIDom();
7965 // This check is purely for compile time reasons: we can skip the IDom walk
7966 // if what we are checking for includes undef and the value is not an integer.
7967 if (!includesUndef(Kind) || V->getType()->isIntegerTy())
7968 while (Dominator) {
7969 auto *TI = Dominator->getBlock()->getTerminator();
7970
7971 Value *Cond = nullptr;
7972 if (auto BI = dyn_cast_or_null<BranchInst>(TI)) {
7973 if (BI->isConditional())
7974 Cond = BI->getCondition();
7975 } else if (auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
7976 Cond = SI->getCondition();
7977 }
7978
7979 if (Cond) {
7980 if (Cond == V)
7981 return true;
7982 else if (!includesUndef(Kind) && isa<Operator>(Cond)) {
7983 // For poison, we can analyze further
7984 auto *Opr = cast<Operator>(Cond);
7985 if (any_of(Opr->operands(), [V](const Use &U) {
7986 return V == U && propagatesPoison(U);
7987 }))
7988 return true;
7989 }
7990 }
7991
7992 Dominator = Dominator->getIDom();
7993 }
7994
7995 if (AC && getKnowledgeValidInContext(V, {Attribute::NoUndef}, *AC, CtxI, DT))
7996 return true;
7997
7998 return false;
7999}
8000
8002 const Instruction *CtxI,
8003 const DominatorTree *DT,
8004 unsigned Depth) {
8005 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
8007}
8008
8010 const Instruction *CtxI,
8011 const DominatorTree *DT, unsigned Depth) {
8012 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
8014}
8015
8017 const Instruction *CtxI,
8018 const DominatorTree *DT, unsigned Depth) {
8019 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
8021}
8022
8023/// Return true if undefined behavior would provably be executed on the path to
8024/// OnPathTo if Root produced a posion result. Note that this doesn't say
8025/// anything about whether OnPathTo is actually executed or whether Root is
8026/// actually poison. This can be used to assess whether a new use of Root can
8027/// be added at a location which is control equivalent with OnPathTo (such as
8028/// immediately before it) without introducing UB which didn't previously
8029/// exist. Note that a false result conveys no information.
8031 Instruction *OnPathTo,
8032 DominatorTree *DT) {
8033 // Basic approach is to assume Root is poison, propagate poison forward
8034 // through all users we can easily track, and then check whether any of those
8035 // users are provable UB and must execute before out exiting block might
8036 // exit.
8037
8038 // The set of all recursive users we've visited (which are assumed to all be
8039 // poison because of said visit)
8042 Worklist.push_back(Root);
8043 while (!Worklist.empty()) {
8044 const Instruction *I = Worklist.pop_back_val();
8045
8046 // If we know this must trigger UB on a path leading our target.
8047 if (mustTriggerUB(I, KnownPoison) && DT->dominates(I, OnPathTo))
8048 return true;
8049
8050 // If we can't analyze propagation through this instruction, just skip it
8051 // and transitive users. Safe as false is a conservative result.
8052 if (I != Root && !any_of(I->operands(), [&KnownPoison](const Use &U) {
8053 return KnownPoison.contains(U) && propagatesPoison(U);
8054 }))
8055 continue;
8056
8057 if (KnownPoison.insert(I).second)
8058 for (const User *User : I->users())
8059 Worklist.push_back(cast<Instruction>(User));
8060 }
8061
8062 // Might be non-UB, or might have a path we couldn't prove must execute on
8063 // way to exiting bb.
8064 return false;
8065}
8066
8068 const SimplifyQuery &SQ) {
8069 return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1),
8070 Add, SQ);
8071}
8072
8075 const WithCache<const Value *> &RHS,
8076 const SimplifyQuery &SQ) {
8077 return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, SQ);
8078}
8079
8081 // Note: An atomic operation isn't guaranteed to return in a reasonable amount
8082 // of time because it's possible for another thread to interfere with it for an
8083 // arbitrary length of time, but programs aren't allowed to rely on that.
8084
8085 // If there is no successor, then execution can't transfer to it.
8086 if (isa<ReturnInst>(I))
8087 return false;
8089 return false;
8090
8091 // Note: Do not add new checks here; instead, change Instruction::mayThrow or
8092 // Instruction::willReturn.
8093 //
8094 // FIXME: Move this check into Instruction::willReturn.
8095 if (isa<CatchPadInst>(I)) {
8096 switch (classifyEHPersonality(I->getFunction()->getPersonalityFn())) {
8097 default:
8098 // A catchpad may invoke exception object constructors and such, which
8099 // in some languages can be arbitrary code, so be conservative by default.
8100 return false;
8102 // For CoreCLR, it just involves a type test.
8103 return true;
8104 }
8105 }
8106
8107 // An instruction that returns without throwing must transfer control flow
8108 // to a successor.
8109 return !I->mayThrow() && I->willReturn();
8110}
8111
8113 // TODO: This is slightly conservative for invoke instruction since exiting
8114 // via an exception *is* normal control for them.
8115 for (const Instruction &I : *BB)
8117 return false;
8118 return true;
8119}
8120
8127
8130 assert(ScanLimit && "scan limit must be non-zero");
8131 for (const Instruction &I : Range) {
8132 if (--ScanLimit == 0)
8133 return false;
8135 return false;
8136 }
8137 return true;
8138}
8139
8141 const Loop *L) {
8142 // The loop header is guaranteed to be executed for every iteration.
8143 //
8144 // FIXME: Relax this constraint to cover all basic blocks that are
8145 // guaranteed to be executed at every iteration.
8146 if (I->getParent() != L->getHeader()) return false;
8147
8148 for (const Instruction &LI : *L->getHeader()) {
8149 if (&LI == I) return true;
8150 if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;
8151 }
8152 llvm_unreachable("Instruction not contained in its own parent basic block.");
8153}
8154
8156 switch (IID) {
8157 // TODO: Add more intrinsics.
8158 case Intrinsic::sadd_with_overflow:
8159 case Intrinsic::ssub_with_overflow:
8160 case Intrinsic::smul_with_overflow:
8161 case Intrinsic::uadd_with_overflow:
8162 case Intrinsic::usub_with_overflow:
8163 case Intrinsic::umul_with_overflow:
8164 // If an input is a vector containing a poison element, the
8165 // two output vectors (calculated results, overflow bits)'
8166 // corresponding lanes are poison.
8167 return true;
8168 case Intrinsic::ctpop:
8169 case Intrinsic::ctlz:
8170 case Intrinsic::cttz:
8171 case Intrinsic::abs:
8172 case Intrinsic::smax:
8173 case Intrinsic::smin:
8174 case Intrinsic::umax:
8175 case Intrinsic::umin:
8176 case Intrinsic::scmp:
8177 case Intrinsic::is_fpclass:
8178 case Intrinsic::ptrmask:
8179 case Intrinsic::ucmp:
8180 case Intrinsic::bitreverse:
8181 case Intrinsic::bswap:
8182 case Intrinsic::sadd_sat:
8183 case Intrinsic::ssub_sat:
8184 case Intrinsic::sshl_sat:
8185 case Intrinsic::uadd_sat:
8186 case Intrinsic::usub_sat:
8187 case Intrinsic::ushl_sat:
8188 case Intrinsic::smul_fix:
8189 case Intrinsic::smul_fix_sat:
8190 case Intrinsic::umul_fix:
8191 case Intrinsic::umul_fix_sat:
8192 case Intrinsic::pow:
8193 case Intrinsic::powi:
8194 case Intrinsic::sin:
8195 case Intrinsic::sinh:
8196 case Intrinsic::cos:
8197 case Intrinsic::cosh:
8198 case Intrinsic::sincos:
8199 case Intrinsic::sincospi:
8200 case Intrinsic::tan:
8201 case Intrinsic::tanh:
8202 case Intrinsic::asin:
8203 case Intrinsic::acos:
8204 case Intrinsic::atan:
8205 case Intrinsic::atan2:
8206 case Intrinsic::canonicalize:
8207 case Intrinsic::sqrt:
8208 case Intrinsic::exp:
8209 case Intrinsic::exp2:
8210 case Intrinsic::exp10:
8211 case Intrinsic::log:
8212 case Intrinsic::log2:
8213 case Intrinsic::log10:
8214 case Intrinsic::modf:
8215 case Intrinsic::floor:
8216 case Intrinsic::ceil:
8217 case Intrinsic::trunc:
8218 case Intrinsic::rint:
8219 case Intrinsic::nearbyint:
8220 case Intrinsic::round:
8221 case Intrinsic::roundeven:
8222 case Intrinsic::lrint:
8223 case Intrinsic::llrint:
8224 case Intrinsic::fshl:
8225 case Intrinsic::fshr:
8226 return true;
8227 default:
8228 return false;
8229 }
8230}
8231
8232bool llvm::propagatesPoison(const Use &PoisonOp) {
8233 const Operator *I = cast<Operator>(PoisonOp.getUser());
8234 switch (I->getOpcode()) {
8235 case Instruction::Freeze:
8236 case Instruction::PHI:
8237 case Instruction::Invoke:
8238 return false;
8239 case Instruction::Select:
8240 return PoisonOp.getOperandNo() == 0;
8241 case Instruction::Call:
8242 if (auto *II = dyn_cast<IntrinsicInst>(I))
8243 return intrinsicPropagatesPoison(II->getIntrinsicID());
8244 return false;
8245 case Instruction::ICmp:
8246 case Instruction::FCmp:
8247 case Instruction::GetElementPtr:
8248 return true;
8249 default:
8251 return true;
8252
8253 // Be conservative and return false.
8254 return false;
8255 }
8256}
8257
8258/// Enumerates all operands of \p I that are guaranteed to not be undef or
8259/// poison. If the callback \p Handle returns true, stop processing and return
8260/// true. Otherwise, return false.
8261template <typename CallableT>
8263 const CallableT &Handle) {
8264 switch (I->getOpcode()) {
8265 case Instruction::Store:
8266 if (Handle(cast<StoreInst>(I)->getPointerOperand()))
8267 return true;
8268 break;
8269
8270 case Instruction::Load:
8271 if (Handle(cast<LoadInst>(I)->getPointerOperand()))
8272 return true;
8273 break;
8274
8275 // Since dereferenceable attribute imply noundef, atomic operations
8276 // also implicitly have noundef pointers too
8277 case Instruction::AtomicCmpXchg:
8279 return true;
8280 break;
8281
8282 case Instruction::AtomicRMW:
8283 if (Handle(cast<AtomicRMWInst>(I)->getPointerOperand()))
8284 return true;
8285 break;
8286
8287 case Instruction::Call:
8288 case Instruction::Invoke: {
8289 const CallBase *CB = cast<CallBase>(I);
8290 if (CB->isIndirectCall() && Handle(CB->getCalledOperand()))
8291 return true;
8292 for (unsigned i = 0; i < CB->arg_size(); ++i)
8293 if ((CB->paramHasAttr(i, Attribute::NoUndef) ||
8294 CB->paramHasAttr(i, Attribute::Dereferenceable) ||
8295 CB->paramHasAttr(i, Attribute::DereferenceableOrNull)) &&
8296 Handle(CB->getArgOperand(i)))
8297 return true;
8298 break;
8299 }
8300 case Instruction::Ret:
8301 if (I->getFunction()->hasRetAttribute(Attribute::NoUndef) &&
8302 Handle(I->getOperand(0)))
8303 return true;
8304 break;
8305 case Instruction::Switch:
8306 if (Handle(cast<SwitchInst>(I)->getCondition()))
8307 return true;
8308 break;
8309 case Instruction::Br: {
8310 auto *BR = cast<BranchInst>(I);
8311 if (BR->isConditional() && Handle(BR->getCondition()))
8312 return true;
8313 break;
8314 }
8315 default:
8316 break;
8317 }
8318
8319 return false;
8320}
8321
8322/// Enumerates all operands of \p I that are guaranteed to not be poison.
8323template <typename CallableT>
8325 const CallableT &Handle) {
8326 if (handleGuaranteedWellDefinedOps(I, Handle))
8327 return true;
8328 switch (I->getOpcode()) {
8329 // Divisors of these operations are allowed to be partially undef.
8330 case Instruction::UDiv:
8331 case Instruction::SDiv:
8332 case Instruction::URem:
8333 case Instruction::SRem:
8334 return Handle(I->getOperand(1));
8335 default:
8336 return false;
8337 }
8338}
8339
8341 const SmallPtrSetImpl<const Value *> &KnownPoison) {
8343 I, [&](const Value *V) { return KnownPoison.count(V); });
8344}
8345
8347 bool PoisonOnly) {
8348 // We currently only look for uses of values within the same basic
8349 // block, as that makes it easier to guarantee that the uses will be
8350 // executed given that Inst is executed.
8351 //
8352 // FIXME: Expand this to consider uses beyond the same basic block. To do
8353 // this, look out for the distinction between post-dominance and strong
8354 // post-dominance.
8355 const BasicBlock *BB = nullptr;
8357 if (const auto *Inst = dyn_cast<Instruction>(V)) {
8358 BB = Inst->getParent();
8359 Begin = Inst->getIterator();
8360 Begin++;
8361 } else if (const auto *Arg = dyn_cast<Argument>(V)) {
8362 if (Arg->getParent()->isDeclaration())
8363 return false;
8364 BB = &Arg->getParent()->getEntryBlock();
8365 Begin = BB->begin();
8366 } else {
8367 return false;
8368 }
8369
8370 // Limit number of instructions we look at, to avoid scanning through large
8371 // blocks. The current limit is chosen arbitrarily.
8372 unsigned ScanLimit = 32;
8373 BasicBlock::const_iterator End = BB->end();
8374
8375 if (!PoisonOnly) {
8376 // Since undef does not propagate eagerly, be conservative & just check
8377 // whether a value is directly passed to an instruction that must take
8378 // well-defined operands.
8379
8380 for (const auto &I : make_range(Begin, End)) {
8381 if (--ScanLimit == 0)
8382 break;
8383
8384 if (handleGuaranteedWellDefinedOps(&I, [V](const Value *WellDefinedOp) {
8385 return WellDefinedOp == V;
8386 }))
8387 return true;
8388
8390 break;
8391 }
8392 return false;
8393 }
8394
8395 // Set of instructions that we have proved will yield poison if Inst
8396 // does.
8397 SmallPtrSet<const Value *, 16> YieldsPoison;
8399
8400 YieldsPoison.insert(V);
8401 Visited.insert(BB);
8402
8403 while (true) {
8404 for (const auto &I : make_range(Begin, End)) {
8405 if (--ScanLimit == 0)
8406 return false;
8407 if (mustTriggerUB(&I, YieldsPoison))
8408 return true;
8410 return false;
8411
8412 // If an operand is poison and propagates it, mark I as yielding poison.
8413 for (const Use &Op : I.operands()) {
8414 if (YieldsPoison.count(Op) && propagatesPoison(Op)) {
8415 YieldsPoison.insert(&I);
8416 break;
8417 }
8418 }
8419
8420 // Special handling for select, which returns poison if its operand 0 is
8421 // poison (handled in the loop above) *or* if both its true/false operands
8422 // are poison (handled here).
8423 if (I.getOpcode() == Instruction::Select &&
8424 YieldsPoison.count(I.getOperand(1)) &&
8425 YieldsPoison.count(I.getOperand(2))) {
8426 YieldsPoison.insert(&I);
8427 }
8428 }
8429
8430 BB = BB->getSingleSuccessor();
8431 if (!BB || !Visited.insert(BB).second)
8432 break;
8433
8434 Begin = BB->getFirstNonPHIIt();
8435 End = BB->end();
8436 }
8437 return false;
8438}
8439
8441 return ::programUndefinedIfUndefOrPoison(Inst, false);
8442}
8443
8445 return ::programUndefinedIfUndefOrPoison(Inst, true);
8446}
8447
8448static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) {
8449 if (FMF.noNaNs())
8450 return true;
8451
8452 if (auto *C = dyn_cast<ConstantFP>(V))
8453 return !C->isNaN();
8454
8455 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8456 if (!C->getElementType()->isFloatingPointTy())
8457 return false;
8458 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8459 if (C->getElementAsAPFloat(I).isNaN())
8460 return false;
8461 }
8462 return true;
8463 }
8464
8466 return true;
8467
8468 return false;
8469}
8470
8471static bool isKnownNonZero(const Value *V) {
8472 if (auto *C = dyn_cast<ConstantFP>(V))
8473 return !C->isZero();
8474
8475 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8476 if (!C->getElementType()->isFloatingPointTy())
8477 return false;
8478 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8479 if (C->getElementAsAPFloat(I).isZero())
8480 return false;
8481 }
8482 return true;
8483 }
8484
8485 return false;
8486}
8487
8488/// Match clamp pattern for float types without care about NaNs or signed zeros.
8489/// Given non-min/max outer cmp/select from the clamp pattern this
8490/// function recognizes if it can be substitued by a "canonical" min/max
8491/// pattern.
8493 Value *CmpLHS, Value *CmpRHS,
8494 Value *TrueVal, Value *FalseVal,
8495 Value *&LHS, Value *&RHS) {
8496 // Try to match
8497 // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2))
8498 // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2))
8499 // and return description of the outer Max/Min.
8500
8501 // First, check if select has inverse order:
8502 if (CmpRHS == FalseVal) {
8503 std::swap(TrueVal, FalseVal);
8504 Pred = CmpInst::getInversePredicate(Pred);
8505 }
8506
8507 // Assume success now. If there's no match, callers should not use these anyway.
8508 LHS = TrueVal;
8509 RHS = FalseVal;
8510
8511 const APFloat *FC1;
8512 if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite())
8513 return {SPF_UNKNOWN, SPNB_NA, false};
8514
8515 const APFloat *FC2;
8516 switch (Pred) {
8517 case CmpInst::FCMP_OLT:
8518 case CmpInst::FCMP_OLE:
8519 case CmpInst::FCMP_ULT:
8520 case CmpInst::FCMP_ULE:
8521 if (match(FalseVal, m_OrdOrUnordFMin(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8522 *FC1 < *FC2)
8523 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
8524 break;
8525 case CmpInst::FCMP_OGT:
8526 case CmpInst::FCMP_OGE:
8527 case CmpInst::FCMP_UGT:
8528 case CmpInst::FCMP_UGE:
8529 if (match(FalseVal, m_OrdOrUnordFMax(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8530 *FC1 > *FC2)
8531 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
8532 break;
8533 default:
8534 break;
8535 }
8536
8537 return {SPF_UNKNOWN, SPNB_NA, false};
8538}
8539
8540/// Recognize variations of:
8541/// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
8543 Value *CmpLHS, Value *CmpRHS,
8544 Value *TrueVal, Value *FalseVal) {
8545 // Swap the select operands and predicate to match the patterns below.
8546 if (CmpRHS != TrueVal) {
8547 Pred = ICmpInst::getSwappedPredicate(Pred);
8548 std::swap(TrueVal, FalseVal);
8549 }
8550 const APInt *C1;
8551 if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) {
8552 const APInt *C2;
8553 // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
8554 if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8555 C1->slt(*C2) && Pred == CmpInst::ICMP_SLT)
8556 return {SPF_SMAX, SPNB_NA, false};
8557
8558 // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
8559 if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8560 C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT)
8561 return {SPF_SMIN, SPNB_NA, false};
8562
8563 // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
8564 if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8565 C1->ult(*C2) && Pred == CmpInst::ICMP_ULT)
8566 return {SPF_UMAX, SPNB_NA, false};
8567
8568 // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
8569 if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8570 C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT)
8571 return {SPF_UMIN, SPNB_NA, false};
8572 }
8573 return {SPF_UNKNOWN, SPNB_NA, false};
8574}
8575
8576/// Recognize variations of:
8577/// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c))
8579 Value *CmpLHS, Value *CmpRHS,
8580 Value *TVal, Value *FVal,
8581 unsigned Depth) {
8582 // TODO: Allow FP min/max with nnan/nsz.
8583 assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison");
8584
8585 Value *A = nullptr, *B = nullptr;
8586 SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1);
8587 if (!SelectPatternResult::isMinOrMax(L.Flavor))
8588 return {SPF_UNKNOWN, SPNB_NA, false};
8589
8590 Value *C = nullptr, *D = nullptr;
8591 SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1);
8592 if (L.Flavor != R.Flavor)
8593 return {SPF_UNKNOWN, SPNB_NA, false};
8594
8595 // We have something like: x Pred y ? min(a, b) : min(c, d).
8596 // Try to match the compare to the min/max operations of the select operands.
8597 // First, make sure we have the right compare predicate.
8598 switch (L.Flavor) {
8599 case SPF_SMIN:
8600 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) {
8601 Pred = ICmpInst::getSwappedPredicate(Pred);
8602 std::swap(CmpLHS, CmpRHS);
8603 }
8604 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
8605 break;
8606 return {SPF_UNKNOWN, SPNB_NA, false};
8607 case SPF_SMAX:
8608 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) {
8609 Pred = ICmpInst::getSwappedPredicate(Pred);
8610 std::swap(CmpLHS, CmpRHS);
8611 }
8612 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE)
8613 break;
8614 return {SPF_UNKNOWN, SPNB_NA, false};
8615 case SPF_UMIN:
8616 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
8617 Pred = ICmpInst::getSwappedPredicate(Pred);
8618 std::swap(CmpLHS, CmpRHS);
8619 }
8620 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE)
8621 break;
8622 return {SPF_UNKNOWN, SPNB_NA, false};
8623 case SPF_UMAX:
8624 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
8625 Pred = ICmpInst::getSwappedPredicate(Pred);
8626 std::swap(CmpLHS, CmpRHS);
8627 }
8628 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
8629 break;
8630 return {SPF_UNKNOWN, SPNB_NA, false};
8631 default:
8632 return {SPF_UNKNOWN, SPNB_NA, false};
8633 }
8634
8635 // If there is a common operand in the already matched min/max and the other
8636 // min/max operands match the compare operands (either directly or inverted),
8637 // then this is min/max of the same flavor.
8638
8639 // a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8640 // ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8641 if (D == B) {
8642 if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8643 match(A, m_Not(m_Specific(CmpRHS)))))
8644 return {L.Flavor, SPNB_NA, false};
8645 }
8646 // a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8647 // ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8648 if (C == B) {
8649 if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8650 match(A, m_Not(m_Specific(CmpRHS)))))
8651 return {L.Flavor, SPNB_NA, false};
8652 }
8653 // b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8654 // ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8655 if (D == A) {
8656 if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8657 match(B, m_Not(m_Specific(CmpRHS)))))
8658 return {L.Flavor, SPNB_NA, false};
8659 }
8660 // b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8661 // ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8662 if (C == A) {
8663 if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8664 match(B, m_Not(m_Specific(CmpRHS)))))
8665 return {L.Flavor, SPNB_NA, false};
8666 }
8667
8668 return {SPF_UNKNOWN, SPNB_NA, false};
8669}
8670
8671/// If the input value is the result of a 'not' op, constant integer, or vector
8672/// splat of a constant integer, return the bitwise-not source value.
8673/// TODO: This could be extended to handle non-splat vector integer constants.
8675 Value *NotV;
8676 if (match(V, m_Not(m_Value(NotV))))
8677 return NotV;
8678
8679 const APInt *C;
8680 if (match(V, m_APInt(C)))
8681 return ConstantInt::get(V->getType(), ~(*C));
8682
8683 return nullptr;
8684}
8685
8686/// Match non-obvious integer minimum and maximum sequences.
8688 Value *CmpLHS, Value *CmpRHS,
8689 Value *TrueVal, Value *FalseVal,
8690 Value *&LHS, Value *&RHS,
8691 unsigned Depth) {
8692 // Assume success. If there's no match, callers should not use these anyway.
8693 LHS = TrueVal;
8694 RHS = FalseVal;
8695
8696 SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);
8698 return SPR;
8699
8700 SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth);
8702 return SPR;
8703
8704 // Look through 'not' ops to find disguised min/max.
8705 // (X > Y) ? ~X : ~Y ==> (~X < ~Y) ? ~X : ~Y ==> MIN(~X, ~Y)
8706 // (X < Y) ? ~X : ~Y ==> (~X > ~Y) ? ~X : ~Y ==> MAX(~X, ~Y)
8707 if (CmpLHS == getNotValue(TrueVal) && CmpRHS == getNotValue(FalseVal)) {
8708 switch (Pred) {
8709 case CmpInst::ICMP_SGT: return {SPF_SMIN, SPNB_NA, false};
8710 case CmpInst::ICMP_SLT: return {SPF_SMAX, SPNB_NA, false};
8711 case CmpInst::ICMP_UGT: return {SPF_UMIN, SPNB_NA, false};
8712 case CmpInst::ICMP_ULT: return {SPF_UMAX, SPNB_NA, false};
8713 default: break;
8714 }
8715 }
8716
8717 // (X > Y) ? ~Y : ~X ==> (~X < ~Y) ? ~Y : ~X ==> MAX(~Y, ~X)
8718 // (X < Y) ? ~Y : ~X ==> (~X > ~Y) ? ~Y : ~X ==> MIN(~Y, ~X)
8719 if (CmpLHS == getNotValue(FalseVal) && CmpRHS == getNotValue(TrueVal)) {
8720 switch (Pred) {
8721 case CmpInst::ICMP_SGT: return {SPF_SMAX, SPNB_NA, false};
8722 case CmpInst::ICMP_SLT: return {SPF_SMIN, SPNB_NA, false};
8723 case CmpInst::ICMP_UGT: return {SPF_UMAX, SPNB_NA, false};
8724 case CmpInst::ICMP_ULT: return {SPF_UMIN, SPNB_NA, false};
8725 default: break;
8726 }
8727 }
8728
8729 if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT)
8730 return {SPF_UNKNOWN, SPNB_NA, false};
8731
8732 const APInt *C1;
8733 if (!match(CmpRHS, m_APInt(C1)))
8734 return {SPF_UNKNOWN, SPNB_NA, false};
8735
8736 // An unsigned min/max can be written with a signed compare.
8737 const APInt *C2;
8738 if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) ||
8739 (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) {
8740 // Is the sign bit set?
8741 // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX
8742 // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN
8743 if (Pred == CmpInst::ICMP_SLT && C1->isZero() && C2->isMaxSignedValue())
8744 return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8745
8746 // Is the sign bit clear?
8747 // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX
8748 // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN
8749 if (Pred == CmpInst::ICMP_SGT && C1->isAllOnes() && C2->isMinSignedValue())
8750 return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8751 }
8752
8753 return {SPF_UNKNOWN, SPNB_NA, false};
8754}
8755
8756bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW,
8757 bool AllowPoison) {
8758 assert(X && Y && "Invalid operand");
8759
8760 auto IsNegationOf = [&](const Value *X, const Value *Y) {
8761 if (!match(X, m_Neg(m_Specific(Y))))
8762 return false;
8763
8764 auto *BO = cast<BinaryOperator>(X);
8765 if (NeedNSW && !BO->hasNoSignedWrap())
8766 return false;
8767
8768 auto *Zero = cast<Constant>(BO->getOperand(0));
8769 if (!AllowPoison && !Zero->isNullValue())
8770 return false;
8771
8772 return true;
8773 };
8774
8775 // X = -Y or Y = -X
8776 if (IsNegationOf(X, Y) || IsNegationOf(Y, X))
8777 return true;
8778
8779 // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A)
8780 Value *A, *B;
8781 return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) &&
8782 match(Y, m_Sub(m_Specific(B), m_Specific(A))))) ||
8783 (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) &&
8785}
8786
8787bool llvm::isKnownInversion(const Value *X, const Value *Y) {
8788 // Handle X = icmp pred A, B, Y = icmp pred A, C.
8789 Value *A, *B, *C;
8790 CmpPredicate Pred1, Pred2;
8791 if (!match(X, m_ICmp(Pred1, m_Value(A), m_Value(B))) ||
8792 !match(Y, m_c_ICmp(Pred2, m_Specific(A), m_Value(C))))
8793 return false;
8794
8795 // They must both have samesign flag or not.
8796 if (Pred1.hasSameSign() != Pred2.hasSameSign())
8797 return false;
8798
8799 if (B == C)
8800 return Pred1 == ICmpInst::getInversePredicate(Pred2);
8801
8802 // Try to infer the relationship from constant ranges.
8803 const APInt *RHSC1, *RHSC2;
8804 if (!match(B, m_APInt(RHSC1)) || !match(C, m_APInt(RHSC2)))
8805 return false;
8806
8807 // Sign bits of two RHSCs should match.
8808 if (Pred1.hasSameSign() && RHSC1->isNonNegative() != RHSC2->isNonNegative())
8809 return false;
8810
8811 const auto CR1 = ConstantRange::makeExactICmpRegion(Pred1, *RHSC1);
8812 const auto CR2 = ConstantRange::makeExactICmpRegion(Pred2, *RHSC2);
8813
8814 return CR1.inverse() == CR2;
8815}
8816
8818 SelectPatternNaNBehavior NaNBehavior,
8819 bool Ordered) {
8820 switch (Pred) {
8821 default:
8822 return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.
8823 case ICmpInst::ICMP_UGT:
8824 case ICmpInst::ICMP_UGE:
8825 return {SPF_UMAX, SPNB_NA, false};
8826 case ICmpInst::ICMP_SGT:
8827 case ICmpInst::ICMP_SGE:
8828 return {SPF_SMAX, SPNB_NA, false};
8829 case ICmpInst::ICMP_ULT:
8830 case ICmpInst::ICMP_ULE:
8831 return {SPF_UMIN, SPNB_NA, false};
8832 case ICmpInst::ICMP_SLT:
8833 case ICmpInst::ICMP_SLE:
8834 return {SPF_SMIN, SPNB_NA, false};
8835 case FCmpInst::FCMP_UGT:
8836 case FCmpInst::FCMP_UGE:
8837 case FCmpInst::FCMP_OGT:
8838 case FCmpInst::FCMP_OGE:
8839 return {SPF_FMAXNUM, NaNBehavior, Ordered};
8840 case FCmpInst::FCMP_ULT:
8841 case FCmpInst::FCMP_ULE:
8842 case FCmpInst::FCMP_OLT:
8843 case FCmpInst::FCMP_OLE:
8844 return {SPF_FMINNUM, NaNBehavior, Ordered};
8845 }
8846}
8847
8848std::optional<std::pair<CmpPredicate, Constant *>>
8851 "Only for relational integer predicates.");
8852 if (isa<UndefValue>(C))
8853 return std::nullopt;
8854
8855 Type *Type = C->getType();
8856 bool IsSigned = ICmpInst::isSigned(Pred);
8857
8859 bool WillIncrement =
8860 UnsignedPred == ICmpInst::ICMP_ULE || UnsignedPred == ICmpInst::ICMP_UGT;
8861
8862 // Check if the constant operand can be safely incremented/decremented
8863 // without overflowing/underflowing.
8864 auto ConstantIsOk = [WillIncrement, IsSigned](ConstantInt *C) {
8865 return WillIncrement ? !C->isMaxValue(IsSigned) : !C->isMinValue(IsSigned);
8866 };
8867
8868 Constant *SafeReplacementConstant = nullptr;
8869 if (auto *CI = dyn_cast<ConstantInt>(C)) {
8870 // Bail out if the constant can't be safely incremented/decremented.
8871 if (!ConstantIsOk(CI))
8872 return std::nullopt;
8873 } else if (auto *FVTy = dyn_cast<FixedVectorType>(Type)) {
8874 unsigned NumElts = FVTy->getNumElements();
8875 for (unsigned i = 0; i != NumElts; ++i) {
8876 Constant *Elt = C->getAggregateElement(i);
8877 if (!Elt)
8878 return std::nullopt;
8879
8880 if (isa<UndefValue>(Elt))
8881 continue;
8882
8883 // Bail out if we can't determine if this constant is min/max or if we
8884 // know that this constant is min/max.
8885 auto *CI = dyn_cast<ConstantInt>(Elt);
8886 if (!CI || !ConstantIsOk(CI))
8887 return std::nullopt;
8888
8889 if (!SafeReplacementConstant)
8890 SafeReplacementConstant = CI;
8891 }
8892 } else if (isa<VectorType>(C->getType())) {
8893 // Handle scalable splat
8894 Value *SplatC = C->getSplatValue();
8895 auto *CI = dyn_cast_or_null<ConstantInt>(SplatC);
8896 // Bail out if the constant can't be safely incremented/decremented.
8897 if (!CI || !ConstantIsOk(CI))
8898 return std::nullopt;
8899 } else {
8900 // ConstantExpr?
8901 return std::nullopt;
8902 }
8903
8904 // It may not be safe to change a compare predicate in the presence of
8905 // undefined elements, so replace those elements with the first safe constant
8906 // that we found.
8907 // TODO: in case of poison, it is safe; let's replace undefs only.
8908 if (C->containsUndefOrPoisonElement()) {
8909 assert(SafeReplacementConstant && "Replacement constant not set");
8910 C = Constant::replaceUndefsWith(C, SafeReplacementConstant);
8911 }
8912
8914
8915 // Increment or decrement the constant.
8916 Constant *OneOrNegOne = ConstantInt::get(Type, WillIncrement ? 1 : -1, true);
8917 Constant *NewC = ConstantExpr::getAdd(C, OneOrNegOne);
8918
8919 return std::make_pair(NewPred, NewC);
8920}
8921
8923 FastMathFlags FMF,
8924 Value *CmpLHS, Value *CmpRHS,
8925 Value *TrueVal, Value *FalseVal,
8926 Value *&LHS, Value *&RHS,
8927 unsigned Depth) {
8928 bool HasMismatchedZeros = false;
8929 if (CmpInst::isFPPredicate(Pred)) {
8930 // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one
8931 // 0.0 operand, set the compare's 0.0 operands to that same value for the
8932 // purpose of identifying min/max. Disregard vector constants with undefined
8933 // elements because those can not be back-propagated for analysis.
8934 Value *OutputZeroVal = nullptr;
8935 if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) &&
8936 !cast<Constant>(TrueVal)->containsUndefOrPoisonElement())
8937 OutputZeroVal = TrueVal;
8938 else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) &&
8939 !cast<Constant>(FalseVal)->containsUndefOrPoisonElement())
8940 OutputZeroVal = FalseVal;
8941
8942 if (OutputZeroVal) {
8943 if (match(CmpLHS, m_AnyZeroFP()) && CmpLHS != OutputZeroVal) {
8944 HasMismatchedZeros = true;
8945 CmpLHS = OutputZeroVal;
8946 }
8947 if (match(CmpRHS, m_AnyZeroFP()) && CmpRHS != OutputZeroVal) {
8948 HasMismatchedZeros = true;
8949 CmpRHS = OutputZeroVal;
8950 }
8951 }
8952 }
8953
8954 LHS = CmpLHS;
8955 RHS = CmpRHS;
8956
8957 // Signed zero may return inconsistent results between implementations.
8958 // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0
8959 // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)
8960 // Therefore, we behave conservatively and only proceed if at least one of the
8961 // operands is known to not be zero or if we don't care about signed zero.
8962 switch (Pred) {
8963 default: break;
8966 if (!HasMismatchedZeros)
8967 break;
8968 [[fallthrough]];
8971 if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8972 !isKnownNonZero(CmpRHS))
8973 return {SPF_UNKNOWN, SPNB_NA, false};
8974 }
8975
8976 SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
8977 bool Ordered = false;
8978
8979 // When given one NaN and one non-NaN input:
8980 // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
8981 // - A simple C99 (a < b ? a : b) construction will return 'b' (as the
8982 // ordered comparison fails), which could be NaN or non-NaN.
8983 // so here we discover exactly what NaN behavior is required/accepted.
8984 if (CmpInst::isFPPredicate(Pred)) {
8985 bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
8986 bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
8987
8988 if (LHSSafe && RHSSafe) {
8989 // Both operands are known non-NaN.
8990 NaNBehavior = SPNB_RETURNS_ANY;
8991 Ordered = CmpInst::isOrdered(Pred);
8992 } else if (CmpInst::isOrdered(Pred)) {
8993 // An ordered comparison will return false when given a NaN, so it
8994 // returns the RHS.
8995 Ordered = true;
8996 if (LHSSafe)
8997 // LHS is non-NaN, so if RHS is NaN then NaN will be returned.
8998 NaNBehavior = SPNB_RETURNS_NAN;
8999 else if (RHSSafe)
9000 NaNBehavior = SPNB_RETURNS_OTHER;
9001 else
9002 // Completely unsafe.
9003 return {SPF_UNKNOWN, SPNB_NA, false};
9004 } else {
9005 Ordered = false;
9006 // An unordered comparison will return true when given a NaN, so it
9007 // returns the LHS.
9008 if (LHSSafe)
9009 // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
9010 NaNBehavior = SPNB_RETURNS_OTHER;
9011 else if (RHSSafe)
9012 NaNBehavior = SPNB_RETURNS_NAN;
9013 else
9014 // Completely unsafe.
9015 return {SPF_UNKNOWN, SPNB_NA, false};
9016 }
9017 }
9018
9019 if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
9020 std::swap(CmpLHS, CmpRHS);
9021 Pred = CmpInst::getSwappedPredicate(Pred);
9022 if (NaNBehavior == SPNB_RETURNS_NAN)
9023 NaNBehavior = SPNB_RETURNS_OTHER;
9024 else if (NaNBehavior == SPNB_RETURNS_OTHER)
9025 NaNBehavior = SPNB_RETURNS_NAN;
9026 Ordered = !Ordered;
9027 }
9028
9029 // ([if]cmp X, Y) ? X : Y
9030 if (TrueVal == CmpLHS && FalseVal == CmpRHS)
9031 return getSelectPattern(Pred, NaNBehavior, Ordered);
9032
9033 if (isKnownNegation(TrueVal, FalseVal)) {
9034 // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can
9035 // match against either LHS or sext(LHS).
9036 auto MaybeSExtCmpLHS =
9037 m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS)));
9038 auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes());
9039 auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One());
9040 if (match(TrueVal, MaybeSExtCmpLHS)) {
9041 // Set the return values. If the compare uses the negated value (-X >s 0),
9042 // swap the return values because the negated value is always 'RHS'.
9043 LHS = TrueVal;
9044 RHS = FalseVal;
9045 if (match(CmpLHS, m_Neg(m_Specific(FalseVal))))
9046 std::swap(LHS, RHS);
9047
9048 // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X)
9049 // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X)
9050 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
9051 return {SPF_ABS, SPNB_NA, false};
9052
9053 // (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X)
9054 if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne))
9055 return {SPF_ABS, SPNB_NA, false};
9056
9057 // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X)
9058 // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X)
9059 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
9060 return {SPF_NABS, SPNB_NA, false};
9061 }
9062 else if (match(FalseVal, MaybeSExtCmpLHS)) {
9063 // Set the return values. If the compare uses the negated value (-X >s 0),
9064 // swap the return values because the negated value is always 'RHS'.
9065 LHS = FalseVal;
9066 RHS = TrueVal;
9067 if (match(CmpLHS, m_Neg(m_Specific(TrueVal))))
9068 std::swap(LHS, RHS);
9069
9070 // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X)
9071 // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X)
9072 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
9073 return {SPF_NABS, SPNB_NA, false};
9074
9075 // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X)
9076 // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X)
9077 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
9078 return {SPF_ABS, SPNB_NA, false};
9079 }
9080 }
9081
9082 if (CmpInst::isIntPredicate(Pred))
9083 return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth);
9084
9085 // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar
9086 // may return either -0.0 or 0.0, so fcmp/select pair has stricter
9087 // semantics than minNum. Be conservative in such case.
9088 if (NaNBehavior != SPNB_RETURNS_ANY ||
9089 (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
9090 !isKnownNonZero(CmpRHS)))
9091 return {SPF_UNKNOWN, SPNB_NA, false};
9092
9093 return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
9094}
9095
9097 Instruction::CastOps *CastOp) {
9098 const DataLayout &DL = CmpI->getDataLayout();
9099
9100 Constant *CastedTo = nullptr;
9101 switch (*CastOp) {
9102 case Instruction::ZExt:
9103 if (CmpI->isUnsigned())
9104 CastedTo = ConstantExpr::getTrunc(C, SrcTy);
9105 break;
9106 case Instruction::SExt:
9107 if (CmpI->isSigned())
9108 CastedTo = ConstantExpr::getTrunc(C, SrcTy, true);
9109 break;
9110 case Instruction::Trunc:
9111 Constant *CmpConst;
9112 if (match(CmpI->getOperand(1), m_Constant(CmpConst)) &&
9113 CmpConst->getType() == SrcTy) {
9114 // Here we have the following case:
9115 //
9116 // %cond = cmp iN %x, CmpConst
9117 // %tr = trunc iN %x to iK
9118 // %narrowsel = select i1 %cond, iK %t, iK C
9119 //
9120 // We can always move trunc after select operation:
9121 //
9122 // %cond = cmp iN %x, CmpConst
9123 // %widesel = select i1 %cond, iN %x, iN CmpConst
9124 // %tr = trunc iN %widesel to iK
9125 //
9126 // Note that C could be extended in any way because we don't care about
9127 // upper bits after truncation. It can't be abs pattern, because it would
9128 // look like:
9129 //
9130 // select i1 %cond, x, -x.
9131 //
9132 // So only min/max pattern could be matched. Such match requires widened C
9133 // == CmpConst. That is why set widened C = CmpConst, condition trunc
9134 // CmpConst == C is checked below.
9135 CastedTo = CmpConst;
9136 } else {
9137 unsigned ExtOp = CmpI->isSigned() ? Instruction::SExt : Instruction::ZExt;
9138 CastedTo = ConstantFoldCastOperand(ExtOp, C, SrcTy, DL);
9139 }
9140 break;
9141 case Instruction::FPTrunc:
9142 CastedTo = ConstantFoldCastOperand(Instruction::FPExt, C, SrcTy, DL);
9143 break;
9144 case Instruction::FPExt:
9145 CastedTo = ConstantFoldCastOperand(Instruction::FPTrunc, C, SrcTy, DL);
9146 break;
9147 case Instruction::FPToUI:
9148 CastedTo = ConstantFoldCastOperand(Instruction::UIToFP, C, SrcTy, DL);
9149 break;
9150 case Instruction::FPToSI:
9151 CastedTo = ConstantFoldCastOperand(Instruction::SIToFP, C, SrcTy, DL);
9152 break;
9153 case Instruction::UIToFP:
9154 CastedTo = ConstantFoldCastOperand(Instruction::FPToUI, C, SrcTy, DL);
9155 break;
9156 case Instruction::SIToFP:
9157 CastedTo = ConstantFoldCastOperand(Instruction::FPToSI, C, SrcTy, DL);
9158 break;
9159 default:
9160 break;
9161 }
9162
9163 if (!CastedTo)
9164 return nullptr;
9165
9166 // Make sure the cast doesn't lose any information.
9167 Constant *CastedBack =
9168 ConstantFoldCastOperand(*CastOp, CastedTo, C->getType(), DL);
9169 if (CastedBack && CastedBack != C)
9170 return nullptr;
9171
9172 return CastedTo;
9173}
9174
9175/// Helps to match a select pattern in case of a type mismatch.
9176///
9177/// The function processes the case when type of true and false values of a
9178/// select instruction differs from type of the cmp instruction operands because
9179/// of a cast instruction. The function checks if it is legal to move the cast
9180/// operation after "select". If yes, it returns the new second value of
9181/// "select" (with the assumption that cast is moved):
9182/// 1. As operand of cast instruction when both values of "select" are same cast
9183/// instructions.
9184/// 2. As restored constant (by applying reverse cast operation) when the first
9185/// value of the "select" is a cast operation and the second value is a
9186/// constant. It is implemented in lookThroughCastConst().
9187/// 3. As one operand is cast instruction and the other is not. The operands in
9188/// sel(cmp) are in different type integer.
9189/// NOTE: We return only the new second value because the first value could be
9190/// accessed as operand of cast instruction.
9191static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2,
9192 Instruction::CastOps *CastOp) {
9193 auto *Cast1 = dyn_cast<CastInst>(V1);
9194 if (!Cast1)
9195 return nullptr;
9196
9197 *CastOp = Cast1->getOpcode();
9198 Type *SrcTy = Cast1->getSrcTy();
9199 if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
9200 // If V1 and V2 are both the same cast from the same type, look through V1.
9201 if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
9202 return Cast2->getOperand(0);
9203 return nullptr;
9204 }
9205
9206 auto *C = dyn_cast<Constant>(V2);
9207 if (C)
9208 return lookThroughCastConst(CmpI, SrcTy, C, CastOp);
9209
9210 Value *CastedTo = nullptr;
9211 if (*CastOp == Instruction::Trunc) {
9212 if (match(CmpI->getOperand(1), m_ZExtOrSExt(m_Specific(V2)))) {
9213 // Here we have the following case:
9214 // %y_ext = sext iK %y to iN
9215 // %cond = cmp iN %x, %y_ext
9216 // %tr = trunc iN %x to iK
9217 // %narrowsel = select i1 %cond, iK %tr, iK %y
9218 //
9219 // We can always move trunc after select operation:
9220 // %y_ext = sext iK %y to iN
9221 // %cond = cmp iN %x, %y_ext
9222 // %widesel = select i1 %cond, iN %x, iN %y_ext
9223 // %tr = trunc iN %widesel to iK
9224 assert(V2->getType() == Cast1->getType() &&
9225 "V2 and Cast1 should be the same type.");
9226 CastedTo = CmpI->getOperand(1);
9227 }
9228 }
9229
9230 return CastedTo;
9231}
9233 Instruction::CastOps *CastOp,
9234 unsigned Depth) {
9236 return {SPF_UNKNOWN, SPNB_NA, false};
9237
9239 if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
9240
9241 CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
9242 if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};
9243
9244 Value *TrueVal = SI->getTrueValue();
9245 Value *FalseVal = SI->getFalseValue();
9246
9248 CmpI, TrueVal, FalseVal, LHS, RHS,
9249 isa<FPMathOperator>(SI) ? SI->getFastMathFlags() : FastMathFlags(),
9250 CastOp, Depth);
9251}
9252
9254 CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS,
9255 FastMathFlags FMF, Instruction::CastOps *CastOp, unsigned Depth) {
9256 CmpInst::Predicate Pred = CmpI->getPredicate();
9257 Value *CmpLHS = CmpI->getOperand(0);
9258 Value *CmpRHS = CmpI->getOperand(1);
9259 if (isa<FPMathOperator>(CmpI) && CmpI->hasNoNaNs())
9260 FMF.setNoNaNs();
9261
9262 // Bail out early.
9263 if (CmpI->isEquality())
9264 return {SPF_UNKNOWN, SPNB_NA, false};
9265
9266 // Deal with type mismatches.
9267 if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
9268 if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) {
9269 // If this is a potential fmin/fmax with a cast to integer, then ignore
9270 // -0.0 because there is no corresponding integer value.
9271 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9272 FMF.setNoSignedZeros();
9273 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9274 cast<CastInst>(TrueVal)->getOperand(0), C,
9275 LHS, RHS, Depth);
9276 }
9277 if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) {
9278 // If this is a potential fmin/fmax with a cast to integer, then ignore
9279 // -0.0 because there is no corresponding integer value.
9280 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9281 FMF.setNoSignedZeros();
9282 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9283 C, cast<CastInst>(FalseVal)->getOperand(0),
9284 LHS, RHS, Depth);
9285 }
9286 }
9287 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
9288 LHS, RHS, Depth);
9289}
9290
9292 if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT;
9293 if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT;
9294 if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT;
9295 if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT;
9296 if (SPF == SPF_FMINNUM)
9297 return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT;
9298 if (SPF == SPF_FMAXNUM)
9299 return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT;
9300 llvm_unreachable("unhandled!");
9301}
9302
9304 switch (SPF) {
9306 return Intrinsic::umin;
9308 return Intrinsic::umax;
9310 return Intrinsic::smin;
9312 return Intrinsic::smax;
9313 default:
9314 llvm_unreachable("Unexpected SPF");
9315 }
9316}
9317
9319 if (SPF == SPF_SMIN) return SPF_SMAX;
9320 if (SPF == SPF_UMIN) return SPF_UMAX;
9321 if (SPF == SPF_SMAX) return SPF_SMIN;
9322 if (SPF == SPF_UMAX) return SPF_UMIN;
9323 llvm_unreachable("unhandled!");
9324}
9325
9327 switch (MinMaxID) {
9328 case Intrinsic::smax: return Intrinsic::smin;
9329 case Intrinsic::smin: return Intrinsic::smax;
9330 case Intrinsic::umax: return Intrinsic::umin;
9331 case Intrinsic::umin: return Intrinsic::umax;
9332 // Please note that next four intrinsics may produce the same result for
9333 // original and inverted case even if X != Y due to NaN is handled specially.
9334 case Intrinsic::maximum: return Intrinsic::minimum;
9335 case Intrinsic::minimum: return Intrinsic::maximum;
9336 case Intrinsic::maxnum: return Intrinsic::minnum;
9337 case Intrinsic::minnum: return Intrinsic::maxnum;
9338 case Intrinsic::maximumnum:
9339 return Intrinsic::minimumnum;
9340 case Intrinsic::minimumnum:
9341 return Intrinsic::maximumnum;
9342 default: llvm_unreachable("Unexpected intrinsic");
9343 }
9344}
9345
9347 switch (SPF) {
9350 case SPF_UMAX: return APInt::getMaxValue(BitWidth);
9351 case SPF_UMIN: return APInt::getMinValue(BitWidth);
9352 default: llvm_unreachable("Unexpected flavor");
9353 }
9354}
9355
9356std::pair<Intrinsic::ID, bool>
9358 // Check if VL contains select instructions that can be folded into a min/max
9359 // vector intrinsic and return the intrinsic if it is possible.
9360 // TODO: Support floating point min/max.
9361 bool AllCmpSingleUse = true;
9362 SelectPatternResult SelectPattern;
9363 SelectPattern.Flavor = SPF_UNKNOWN;
9364 if (all_of(VL, [&SelectPattern, &AllCmpSingleUse](Value *I) {
9365 Value *LHS, *RHS;
9366 auto CurrentPattern = matchSelectPattern(I, LHS, RHS);
9367 if (!SelectPatternResult::isMinOrMax(CurrentPattern.Flavor))
9368 return false;
9369 if (SelectPattern.Flavor != SPF_UNKNOWN &&
9370 SelectPattern.Flavor != CurrentPattern.Flavor)
9371 return false;
9372 SelectPattern = CurrentPattern;
9373 AllCmpSingleUse &=
9375 return true;
9376 })) {
9377 switch (SelectPattern.Flavor) {
9378 case SPF_SMIN:
9379 return {Intrinsic::smin, AllCmpSingleUse};
9380 case SPF_UMIN:
9381 return {Intrinsic::umin, AllCmpSingleUse};
9382 case SPF_SMAX:
9383 return {Intrinsic::smax, AllCmpSingleUse};
9384 case SPF_UMAX:
9385 return {Intrinsic::umax, AllCmpSingleUse};
9386 case SPF_FMAXNUM:
9387 return {Intrinsic::maxnum, AllCmpSingleUse};
9388 case SPF_FMINNUM:
9389 return {Intrinsic::minnum, AllCmpSingleUse};
9390 default:
9391 llvm_unreachable("unexpected select pattern flavor");
9392 }
9393 }
9394 return {Intrinsic::not_intrinsic, false};
9395}
9396
9397template <typename InstTy>
9398static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst,
9399 Value *&Init, Value *&OtherOp) {
9400 // Handle the case of a simple two-predecessor recurrence PHI.
9401 // There's a lot more that could theoretically be done here, but
9402 // this is sufficient to catch some interesting cases.
9403 // TODO: Expand list -- gep, uadd.sat etc.
9404 if (PN->getNumIncomingValues() != 2)
9405 return false;
9406
9407 for (unsigned I = 0; I != 2; ++I) {
9408 if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
9409 Operation && Operation->getNumOperands() >= 2) {
9410 Value *LHS = Operation->getOperand(0);
9411 Value *RHS = Operation->getOperand(1);
9412 if (LHS != PN && RHS != PN)
9413 continue;
9414
9415 Inst = Operation;
9416 Init = PN->getIncomingValue(!I);
9417 OtherOp = (LHS == PN) ? RHS : LHS;
9418 return true;
9419 }
9420 }
9421 return false;
9422}
9423
9425 Value *&Start, Value *&Step) {
9426 // We try to match a recurrence of the form:
9427 // %iv = [Start, %entry], [%iv.next, %backedge]
9428 // %iv.next = binop %iv, Step
9429 // Or:
9430 // %iv = [Start, %entry], [%iv.next, %backedge]
9431 // %iv.next = binop Step, %iv
9432 return matchTwoInputRecurrence(P, BO, Start, Step);
9433}
9434
9436 Value *&Start, Value *&Step) {
9437 BinaryOperator *BO = nullptr;
9438 P = dyn_cast<PHINode>(I->getOperand(0));
9439 if (!P)
9440 P = dyn_cast<PHINode>(I->getOperand(1));
9441 return P && matchSimpleRecurrence(P, BO, Start, Step) && BO == I;
9442}
9443
9445 PHINode *&P, Value *&Init,
9446 Value *&OtherOp) {
9447 // Binary intrinsics only supported for now.
9448 if (I->arg_size() != 2 || I->getType() != I->getArgOperand(0)->getType() ||
9449 I->getType() != I->getArgOperand(1)->getType())
9450 return false;
9451
9452 IntrinsicInst *II = nullptr;
9453 P = dyn_cast<PHINode>(I->getArgOperand(0));
9454 if (!P)
9455 P = dyn_cast<PHINode>(I->getArgOperand(1));
9456
9457 return P && matchTwoInputRecurrence(P, II, Init, OtherOp) && II == I;
9458}
9459
9460/// Return true if "icmp Pred LHS RHS" is always true.
9462 const Value *RHS) {
9463 if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS)
9464 return true;
9465
9466 switch (Pred) {
9467 default:
9468 return false;
9469
9470 case CmpInst::ICMP_SLE: {
9471 const APInt *C;
9472
9473 // LHS s<= LHS +_{nsw} C if C >= 0
9474 // LHS s<= LHS | C if C >= 0
9475 if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C))) ||
9477 return !C->isNegative();
9478
9479 // LHS s<= smax(LHS, V) for any V
9481 return true;
9482
9483 // smin(RHS, V) s<= RHS for any V
9485 return true;
9486
9487 // Match A to (X +_{nsw} CA) and B to (X +_{nsw} CB)
9488 const Value *X;
9489 const APInt *CLHS, *CRHS;
9490 if (match(LHS, m_NSWAddLike(m_Value(X), m_APInt(CLHS))) &&
9492 return CLHS->sle(*CRHS);
9493
9494 return false;
9495 }
9496
9497 case CmpInst::ICMP_ULE: {
9498 // LHS u<= LHS +_{nuw} V for any V
9499 if (match(RHS, m_c_Add(m_Specific(LHS), m_Value())) &&
9501 return true;
9502
9503 // LHS u<= LHS | V for any V
9504 if (match(RHS, m_c_Or(m_Specific(LHS), m_Value())))
9505 return true;
9506
9507 // LHS u<= umax(LHS, V) for any V
9509 return true;
9510
9511 // RHS >> V u<= RHS for any V
9512 if (match(LHS, m_LShr(m_Specific(RHS), m_Value())))
9513 return true;
9514
9515 // RHS u/ C_ugt_1 u<= RHS
9516 const APInt *C;
9517 if (match(LHS, m_UDiv(m_Specific(RHS), m_APInt(C))) && C->ugt(1))
9518 return true;
9519
9520 // RHS & V u<= RHS for any V
9522 return true;
9523
9524 // umin(RHS, V) u<= RHS for any V
9526 return true;
9527
9528 // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
9529 const Value *X;
9530 const APInt *CLHS, *CRHS;
9531 if (match(LHS, m_NUWAddLike(m_Value(X), m_APInt(CLHS))) &&
9533 return CLHS->ule(*CRHS);
9534
9535 return false;
9536 }
9537 }
9538}
9539
9540/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
9541/// ALHS ARHS" is true. Otherwise, return std::nullopt.
9542static std::optional<bool>
9544 const Value *ARHS, const Value *BLHS, const Value *BRHS) {
9545 switch (Pred) {
9546 default:
9547 return std::nullopt;
9548
9549 case CmpInst::ICMP_SLT:
9550 case CmpInst::ICMP_SLE:
9551 if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS) &&
9553 return true;
9554 return std::nullopt;
9555
9556 case CmpInst::ICMP_SGT:
9557 case CmpInst::ICMP_SGE:
9558 if (isTruePredicate(CmpInst::ICMP_SLE, ALHS, BLHS) &&
9560 return true;
9561 return std::nullopt;
9562
9563 case CmpInst::ICMP_ULT:
9564 case CmpInst::ICMP_ULE:
9565 if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS) &&
9567 return true;
9568 return std::nullopt;
9569
9570 case CmpInst::ICMP_UGT:
9571 case CmpInst::ICMP_UGE:
9572 if (isTruePredicate(CmpInst::ICMP_ULE, ALHS, BLHS) &&
9574 return true;
9575 return std::nullopt;
9576 }
9577}
9578
9579/// Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
9580/// Return false if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is false.
9581/// Otherwise, return std::nullopt if we can't infer anything.
9582static std::optional<bool>
9584 CmpPredicate RPred, const ConstantRange &RCR) {
9585 auto CRImpliesPred = [&](ConstantRange CR,
9586 CmpInst::Predicate Pred) -> std::optional<bool> {
9587 // If all true values for lhs and true for rhs, lhs implies rhs
9588 if (CR.icmp(Pred, RCR))
9589 return true;
9590
9591 // If there is no overlap, lhs implies not rhs
9592 if (CR.icmp(CmpInst::getInversePredicate(Pred), RCR))
9593 return false;
9594
9595 return std::nullopt;
9596 };
9597 if (auto Res = CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9598 RPred))
9599 return Res;
9600 if (LPred.hasSameSign() ^ RPred.hasSameSign()) {
9602 : LPred.dropSameSign();
9604 : RPred.dropSameSign();
9605 return CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9606 RPred);
9607 }
9608 return std::nullopt;
9609}
9610
9611/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
9612/// is true. Return false if LHS implies RHS is false. Otherwise, return
9613/// std::nullopt if we can't infer anything.
9614static std::optional<bool>
9615isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1,
9616 CmpPredicate RPred, const Value *R0, const Value *R1,
9617 const DataLayout &DL, bool LHSIsTrue) {
9618 // The rest of the logic assumes the LHS condition is true. If that's not the
9619 // case, invert the predicate to make it so.
9620 if (!LHSIsTrue)
9621 LPred = ICmpInst::getInverseCmpPredicate(LPred);
9622
9623 // We can have non-canonical operands, so try to normalize any common operand
9624 // to L0/R0.
9625 if (L0 == R1) {
9626 std::swap(R0, R1);
9627 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9628 }
9629 if (R0 == L1) {
9630 std::swap(L0, L1);
9631 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9632 }
9633 if (L1 == R1) {
9634 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
9635 if (L0 != R0 || match(L0, m_ImmConstant())) {
9636 std::swap(L0, L1);
9637 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9638 std::swap(R0, R1);
9639 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9640 }
9641 }
9642
9643 // See if we can infer anything if operand-0 matches and we have at least one
9644 // constant.
9645 const APInt *Unused;
9646 if (L0 == R0 && (match(L1, m_APInt(Unused)) || match(R1, m_APInt(Unused)))) {
9647 // Potential TODO: We could also further use the constant range of L0/R0 to
9648 // further constraint the constant ranges. At the moment this leads to
9649 // several regressions related to not transforming `multi_use(A + C0) eq/ne
9650 // C1` (see discussion: D58633).
9652 L1, ICmpInst::isSigned(LPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,
9653 /*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1);
9655 R1, ICmpInst::isSigned(RPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,
9656 /*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1);
9657 // Even if L1/R1 are not both constant, we can still sometimes deduce
9658 // relationship from a single constant. For example X u> Y implies X != 0.
9659 if (auto R = isImpliedCondCommonOperandWithCR(LPred, LCR, RPred, RCR))
9660 return R;
9661 // If both L1/R1 were exact constant ranges and we didn't get anything
9662 // here, we won't be able to deduce this.
9663 if (match(L1, m_APInt(Unused)) && match(R1, m_APInt(Unused)))
9664 return std::nullopt;
9665 }
9666
9667 // Can we infer anything when the two compares have matching operands?
9668 if (L0 == R0 && L1 == R1)
9669 return ICmpInst::isImpliedByMatchingCmp(LPred, RPred);
9670
9671 // It only really makes sense in the context of signed comparison for "X - Y
9672 // must be positive if X >= Y and no overflow".
9673 // Take SGT as an example: L0:x > L1:y and C >= 0
9674 // ==> R0:(x -nsw y) < R1:(-C) is false
9675 CmpInst::Predicate SignedLPred = LPred.getPreferredSignedPredicate();
9676 if ((SignedLPred == ICmpInst::ICMP_SGT ||
9677 SignedLPred == ICmpInst::ICMP_SGE) &&
9678 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9679 if (match(R1, m_NonPositive()) &&
9680 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == false)
9681 return false;
9682 }
9683
9684 // Take SLT as an example: L0:x < L1:y and C <= 0
9685 // ==> R0:(x -nsw y) < R1:(-C) is true
9686 if ((SignedLPred == ICmpInst::ICMP_SLT ||
9687 SignedLPred == ICmpInst::ICMP_SLE) &&
9688 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9689 if (match(R1, m_NonNegative()) &&
9690 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == true)
9691 return true;
9692 }
9693
9694 // a - b == NonZero -> a != b
9695 // ptrtoint(a) - ptrtoint(b) == NonZero -> a != b
9696 const APInt *L1C;
9697 Value *A, *B;
9698 if (LPred == ICmpInst::ICMP_EQ && ICmpInst::isEquality(RPred) &&
9699 match(L1, m_APInt(L1C)) && !L1C->isZero() &&
9700 match(L0, m_Sub(m_Value(A), m_Value(B))) &&
9701 ((A == R0 && B == R1) || (A == R1 && B == R0) ||
9702 (match(A, m_PtrToInt(m_Specific(R0))) &&
9703 match(B, m_PtrToInt(m_Specific(R1)))) ||
9704 (match(A, m_PtrToInt(m_Specific(R1))) &&
9705 match(B, m_PtrToInt(m_Specific(R0)))))) {
9706 return RPred.dropSameSign() == ICmpInst::ICMP_NE;
9707 }
9708
9709 // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1
9710 if (L0 == R0 &&
9711 (LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) &&
9712 (RPred == ICmpInst::ICMP_ULT || RPred == ICmpInst::ICMP_UGE) &&
9713 match(L0, m_c_Add(m_Specific(L1), m_Specific(R1))))
9714 return CmpPredicate::getMatching(LPred, RPred).has_value();
9715
9716 if (auto P = CmpPredicate::getMatching(LPred, RPred))
9717 return isImpliedCondOperands(*P, L0, L1, R0, R1);
9718
9719 return std::nullopt;
9720}
9721
9722/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
9723/// is true. Return false if LHS implies RHS is false. Otherwise, return
9724/// std::nullopt if we can't infer anything.
9725static std::optional<bool>
9727 FCmpInst::Predicate RPred, const Value *R0, const Value *R1,
9728 const DataLayout &DL, bool LHSIsTrue) {
9729 // The rest of the logic assumes the LHS condition is true. If that's not the
9730 // case, invert the predicate to make it so.
9731 if (!LHSIsTrue)
9732 LPred = FCmpInst::getInversePredicate(LPred);
9733
9734 // We can have non-canonical operands, so try to normalize any common operand
9735 // to L0/R0.
9736 if (L0 == R1) {
9737 std::swap(R0, R1);
9738 RPred = FCmpInst::getSwappedPredicate(RPred);
9739 }
9740 if (R0 == L1) {
9741 std::swap(L0, L1);
9742 LPred = FCmpInst::getSwappedPredicate(LPred);
9743 }
9744 if (L1 == R1) {
9745 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
9746 if (L0 != R0 || match(L0, m_ImmConstant())) {
9747 std::swap(L0, L1);
9748 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9749 std::swap(R0, R1);
9750 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9751 }
9752 }
9753
9754 // Can we infer anything when the two compares have matching operands?
9755 if (L0 == R0 && L1 == R1) {
9756 if ((LPred & RPred) == LPred)
9757 return true;
9758 if ((LPred & ~RPred) == LPred)
9759 return false;
9760 }
9761
9762 // See if we can infer anything if operand-0 matches and we have at least one
9763 // constant.
9764 const APFloat *L1C, *R1C;
9765 if (L0 == R0 && match(L1, m_APFloat(L1C)) && match(R1, m_APFloat(R1C))) {
9766 if (std::optional<ConstantFPRange> DomCR =
9768 if (std::optional<ConstantFPRange> ImpliedCR =
9770 if (ImpliedCR->contains(*DomCR))
9771 return true;
9772 }
9773 if (std::optional<ConstantFPRange> ImpliedCR =
9775 FCmpInst::getInversePredicate(RPred), *R1C)) {
9776 if (ImpliedCR->contains(*DomCR))
9777 return false;
9778 }
9779 }
9780 }
9781
9782 return std::nullopt;
9783}
9784
9785/// Return true if LHS implies RHS is true. Return false if LHS implies RHS is
9786/// false. Otherwise, return std::nullopt if we can't infer anything. We
9787/// expect the RHS to be an icmp and the LHS to be an 'and', 'or', or a 'select'
9788/// instruction.
9789static std::optional<bool>
9791 const Value *RHSOp0, const Value *RHSOp1,
9792 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9793 // The LHS must be an 'or', 'and', or a 'select' instruction.
9794 assert((LHS->getOpcode() == Instruction::And ||
9795 LHS->getOpcode() == Instruction::Or ||
9796 LHS->getOpcode() == Instruction::Select) &&
9797 "Expected LHS to be 'and', 'or', or 'select'.");
9798
9799 assert(Depth <= MaxAnalysisRecursionDepth && "Hit recursion limit");
9800
9801 // If the result of an 'or' is false, then we know both legs of the 'or' are
9802 // false. Similarly, if the result of an 'and' is true, then we know both
9803 // legs of the 'and' are true.
9804 const Value *ALHS, *ARHS;
9805 if ((!LHSIsTrue && match(LHS, m_LogicalOr(m_Value(ALHS), m_Value(ARHS)))) ||
9806 (LHSIsTrue && match(LHS, m_LogicalAnd(m_Value(ALHS), m_Value(ARHS))))) {
9807 // FIXME: Make this non-recursion.
9808 if (std::optional<bool> Implication = isImpliedCondition(
9809 ALHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9810 return Implication;
9811 if (std::optional<bool> Implication = isImpliedCondition(
9812 ARHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9813 return Implication;
9814 return std::nullopt;
9815 }
9816 return std::nullopt;
9817}
9818
9819std::optional<bool>
9821 const Value *RHSOp0, const Value *RHSOp1,
9822 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9823 // Bail out when we hit the limit.
9825 return std::nullopt;
9826
9827 // A mismatch occurs when we compare a scalar cmp to a vector cmp, for
9828 // example.
9829 if (RHSOp0->getType()->isVectorTy() != LHS->getType()->isVectorTy())
9830 return std::nullopt;
9831
9832 assert(LHS->getType()->isIntOrIntVectorTy(1) &&
9833 "Expected integer type only!");
9834
9835 // Match not
9836 if (match(LHS, m_Not(m_Value(LHS))))
9837 LHSIsTrue = !LHSIsTrue;
9838
9839 // Both LHS and RHS are icmps.
9840 if (RHSOp0->getType()->getScalarType()->isIntOrPtrTy()) {
9841 if (const auto *LHSCmp = dyn_cast<ICmpInst>(LHS))
9842 return isImpliedCondICmps(LHSCmp->getCmpPredicate(),
9843 LHSCmp->getOperand(0), LHSCmp->getOperand(1),
9844 RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue);
9845 const Value *V;
9846 if (match(LHS, m_NUWTrunc(m_Value(V))))
9848 ConstantInt::get(V->getType(), 0), RHSPred,
9849 RHSOp0, RHSOp1, DL, LHSIsTrue);
9850 } else {
9851 assert(RHSOp0->getType()->isFPOrFPVectorTy() &&
9852 "Expected floating point type only!");
9853 if (const auto *LHSCmp = dyn_cast<FCmpInst>(LHS))
9854 return isImpliedCondFCmps(LHSCmp->getPredicate(), LHSCmp->getOperand(0),
9855 LHSCmp->getOperand(1), RHSPred, RHSOp0, RHSOp1,
9856 DL, LHSIsTrue);
9857 }
9858
9859 /// The LHS should be an 'or', 'and', or a 'select' instruction. We expect
9860 /// the RHS to be an icmp.
9861 /// FIXME: Add support for and/or/select on the RHS.
9862 if (const Instruction *LHSI = dyn_cast<Instruction>(LHS)) {
9863 if ((LHSI->getOpcode() == Instruction::And ||
9864 LHSI->getOpcode() == Instruction::Or ||
9865 LHSI->getOpcode() == Instruction::Select))
9866 return isImpliedCondAndOr(LHSI, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue,
9867 Depth);
9868 }
9869 return std::nullopt;
9870}
9871
9872std::optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS,
9873 const DataLayout &DL,
9874 bool LHSIsTrue, unsigned Depth) {
9875 // LHS ==> RHS by definition
9876 if (LHS == RHS)
9877 return LHSIsTrue;
9878
9879 // Match not
9880 bool InvertRHS = false;
9881 if (match(RHS, m_Not(m_Value(RHS)))) {
9882 if (LHS == RHS)
9883 return !LHSIsTrue;
9884 InvertRHS = true;
9885 }
9886
9887 if (const ICmpInst *RHSCmp = dyn_cast<ICmpInst>(RHS)) {
9888 if (auto Implied = isImpliedCondition(
9889 LHS, RHSCmp->getCmpPredicate(), RHSCmp->getOperand(0),
9890 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))
9891 return InvertRHS ? !*Implied : *Implied;
9892 return std::nullopt;
9893 }
9894 if (const FCmpInst *RHSCmp = dyn_cast<FCmpInst>(RHS)) {
9895 if (auto Implied = isImpliedCondition(
9896 LHS, RHSCmp->getPredicate(), RHSCmp->getOperand(0),
9897 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))
9898 return InvertRHS ? !*Implied : *Implied;
9899 return std::nullopt;
9900 }
9901
9902 const Value *V;
9903 if (match(RHS, m_NUWTrunc(m_Value(V)))) {
9904 if (auto Implied = isImpliedCondition(LHS, CmpInst::ICMP_NE, V,
9905 ConstantInt::get(V->getType(), 0), DL,
9906 LHSIsTrue, Depth))
9907 return InvertRHS ? !*Implied : *Implied;
9908 return std::nullopt;
9909 }
9910
9912 return std::nullopt;
9913
9914 // LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS2
9915 // LHS ==> !(RHS1 && RHS2) if LHS ==> !RHS1 or LHS ==> !RHS2
9916 const Value *RHS1, *RHS2;
9917 if (match(RHS, m_LogicalOr(m_Value(RHS1), m_Value(RHS2)))) {
9918 if (std::optional<bool> Imp =
9919 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9920 if (*Imp == true)
9921 return !InvertRHS;
9922 if (std::optional<bool> Imp =
9923 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9924 if (*Imp == true)
9925 return !InvertRHS;
9926 }
9927 if (match(RHS, m_LogicalAnd(m_Value(RHS1), m_Value(RHS2)))) {
9928 if (std::optional<bool> Imp =
9929 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9930 if (*Imp == false)
9931 return InvertRHS;
9932 if (std::optional<bool> Imp =
9933 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9934 if (*Imp == false)
9935 return InvertRHS;
9936 }
9937
9938 return std::nullopt;
9939}
9940
9941// Returns a pair (Condition, ConditionIsTrue), where Condition is a branch
9942// condition dominating ContextI or nullptr, if no condition is found.
9943static std::pair<Value *, bool>
9945 if (!ContextI || !ContextI->getParent())
9946 return {nullptr, false};
9947
9948 // TODO: This is a poor/cheap way to determine dominance. Should we use a
9949 // dominator tree (eg, from a SimplifyQuery) instead?
9950 const BasicBlock *ContextBB = ContextI->getParent();
9951 const BasicBlock *PredBB = ContextBB->getSinglePredecessor();
9952 if (!PredBB)
9953 return {nullptr, false};
9954
9955 // We need a conditional branch in the predecessor.
9956 Value *PredCond;
9957 BasicBlock *TrueBB, *FalseBB;
9958 if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))
9959 return {nullptr, false};
9960
9961 // The branch should get simplified. Don't bother simplifying this condition.
9962 if (TrueBB == FalseBB)
9963 return {nullptr, false};
9964
9965 assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
9966 "Predecessor block does not point to successor?");
9967
9968 // Is this condition implied by the predecessor condition?
9969 return {PredCond, TrueBB == ContextBB};
9970}
9971
9972std::optional<bool> llvm::isImpliedByDomCondition(const Value *Cond,
9973 const Instruction *ContextI,
9974 const DataLayout &DL) {
9975 assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool");
9976 auto PredCond = getDomPredecessorCondition(ContextI);
9977 if (PredCond.first)
9978 return isImpliedCondition(PredCond.first, Cond, DL, PredCond.second);
9979 return std::nullopt;
9980}
9981
9983 const Value *LHS,
9984 const Value *RHS,
9985 const Instruction *ContextI,
9986 const DataLayout &DL) {
9987 auto PredCond = getDomPredecessorCondition(ContextI);
9988 if (PredCond.first)
9989 return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL,
9990 PredCond.second);
9991 return std::nullopt;
9992}
9993
9995 APInt &Upper, const InstrInfoQuery &IIQ,
9996 bool PreferSignedRange) {
9997 unsigned Width = Lower.getBitWidth();
9998 const APInt *C;
9999 switch (BO.getOpcode()) {
10000 case Instruction::Sub:
10001 if (match(BO.getOperand(0), m_APInt(C))) {
10002 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
10003 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
10004
10005 // If the caller expects a signed compare, then try to use a signed range.
10006 // Otherwise if both no-wraps are set, use the unsigned range because it
10007 // is never larger than the signed range. Example:
10008 // "sub nuw nsw i8 -2, x" is unsigned [0, 254] vs. signed [-128, 126].
10009 // "sub nuw nsw i8 2, x" is unsigned [0, 2] vs. signed [-125, 127].
10010 if (PreferSignedRange && HasNSW && HasNUW)
10011 HasNUW = false;
10012
10013 if (HasNUW) {
10014 // 'sub nuw c, x' produces [0, C].
10015 Upper = *C + 1;
10016 } else if (HasNSW) {
10017 if (C->isNegative()) {
10018 // 'sub nsw -C, x' produces [SINT_MIN, -C - SINT_MIN].
10020 Upper = *C - APInt::getSignedMaxValue(Width);
10021 } else {
10022 // Note that sub 0, INT_MIN is not NSW. It techically is a signed wrap
10023 // 'sub nsw C, x' produces [C - SINT_MAX, SINT_MAX].
10024 Lower = *C - APInt::getSignedMaxValue(Width);
10026 }
10027 }
10028 }
10029 break;
10030 case Instruction::Add:
10031 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
10032 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
10033 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
10034
10035 // If the caller expects a signed compare, then try to use a signed
10036 // range. Otherwise if both no-wraps are set, use the unsigned range
10037 // because it is never larger than the signed range. Example: "add nuw
10038 // nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125].
10039 if (PreferSignedRange && HasNSW && HasNUW)
10040 HasNUW = false;
10041
10042 if (HasNUW) {
10043 // 'add nuw x, C' produces [C, UINT_MAX].
10044 Lower = *C;
10045 } else if (HasNSW) {
10046 if (C->isNegative()) {
10047 // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C].
10049 Upper = APInt::getSignedMaxValue(Width) + *C + 1;
10050 } else {
10051 // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX].
10052 Lower = APInt::getSignedMinValue(Width) + *C;
10053 Upper = APInt::getSignedMaxValue(Width) + 1;
10054 }
10055 }
10056 }
10057 break;
10058
10059 case Instruction::And:
10060 if (match(BO.getOperand(1), m_APInt(C)))
10061 // 'and x, C' produces [0, C].
10062 Upper = *C + 1;
10063 // X & -X is a power of two or zero. So we can cap the value at max power of
10064 // two.
10065 if (match(BO.getOperand(0), m_Neg(m_Specific(BO.getOperand(1)))) ||
10066 match(BO.getOperand(1), m_Neg(m_Specific(BO.getOperand(0)))))
10067 Upper = APInt::getSignedMinValue(Width) + 1;
10068 break;
10069
10070 case Instruction::Or:
10071 if (match(BO.getOperand(1), m_APInt(C)))
10072 // 'or x, C' produces [C, UINT_MAX].
10073 Lower = *C;
10074 break;
10075
10076 case Instruction::AShr:
10077 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10078 // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C].
10080 Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1;
10081 } else if (match(BO.getOperand(0), m_APInt(C))) {
10082 unsigned ShiftAmount = Width - 1;
10083 if (!C->isZero() && IIQ.isExact(&BO))
10084 ShiftAmount = C->countr_zero();
10085 if (C->isNegative()) {
10086 // 'ashr C, x' produces [C, C >> (Width-1)]
10087 Lower = *C;
10088 Upper = C->ashr(ShiftAmount) + 1;
10089 } else {
10090 // 'ashr C, x' produces [C >> (Width-1), C]
10091 Lower = C->ashr(ShiftAmount);
10092 Upper = *C + 1;
10093 }
10094 }
10095 break;
10096
10097 case Instruction::LShr:
10098 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10099 // 'lshr x, C' produces [0, UINT_MAX >> C].
10100 Upper = APInt::getAllOnes(Width).lshr(*C) + 1;
10101 } else if (match(BO.getOperand(0), m_APInt(C))) {
10102 // 'lshr C, x' produces [C >> (Width-1), C].
10103 unsigned ShiftAmount = Width - 1;
10104 if (!C->isZero() && IIQ.isExact(&BO))
10105 ShiftAmount = C->countr_zero();
10106 Lower = C->lshr(ShiftAmount);
10107 Upper = *C + 1;
10108 }
10109 break;
10110
10111 case Instruction::Shl:
10112 if (match(BO.getOperand(0), m_APInt(C))) {
10113 if (IIQ.hasNoUnsignedWrap(&BO)) {
10114 // 'shl nuw C, x' produces [C, C << CLZ(C)]
10115 Lower = *C;
10116 Upper = Lower.shl(Lower.countl_zero()) + 1;
10117 } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw?
10118 if (C->isNegative()) {
10119 // 'shl nsw C, x' produces [C << CLO(C)-1, C]
10120 unsigned ShiftAmount = C->countl_one() - 1;
10121 Lower = C->shl(ShiftAmount);
10122 Upper = *C + 1;
10123 } else {
10124 // 'shl nsw C, x' produces [C, C << CLZ(C)-1]
10125 unsigned ShiftAmount = C->countl_zero() - 1;
10126 Lower = *C;
10127 Upper = C->shl(ShiftAmount) + 1;
10128 }
10129 } else {
10130 // If lowbit is set, value can never be zero.
10131 if ((*C)[0])
10132 Lower = APInt::getOneBitSet(Width, 0);
10133 // If we are shifting a constant the largest it can be is if the longest
10134 // sequence of consecutive ones is shifted to the highbits (breaking
10135 // ties for which sequence is higher). At the moment we take a liberal
10136 // upper bound on this by just popcounting the constant.
10137 // TODO: There may be a bitwise trick for it longest/highest
10138 // consecutative sequence of ones (naive method is O(Width) loop).
10139 Upper = APInt::getHighBitsSet(Width, C->popcount()) + 1;
10140 }
10141 } else if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10142 Upper = APInt::getBitsSetFrom(Width, C->getZExtValue()) + 1;
10143 }
10144 break;
10145
10146 case Instruction::SDiv:
10147 if (match(BO.getOperand(1), m_APInt(C))) {
10148 APInt IntMin = APInt::getSignedMinValue(Width);
10149 APInt IntMax = APInt::getSignedMaxValue(Width);
10150 if (C->isAllOnes()) {
10151 // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX]
10152 // where C != -1 and C != 0 and C != 1
10153 Lower = IntMin + 1;
10154 Upper = IntMax + 1;
10155 } else if (C->countl_zero() < Width - 1) {
10156 // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C]
10157 // where C != -1 and C != 0 and C != 1
10158 Lower = IntMin.sdiv(*C);
10159 Upper = IntMax.sdiv(*C);
10160 if (Lower.sgt(Upper))
10162 Upper = Upper + 1;
10163 assert(Upper != Lower && "Upper part of range has wrapped!");
10164 }
10165 } else if (match(BO.getOperand(0), m_APInt(C))) {
10166 if (C->isMinSignedValue()) {
10167 // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2].
10168 Lower = *C;
10169 Upper = Lower.lshr(1) + 1;
10170 } else {
10171 // 'sdiv C, x' produces [-|C|, |C|].
10172 Upper = C->abs() + 1;
10173 Lower = (-Upper) + 1;
10174 }
10175 }
10176 break;
10177
10178 case Instruction::UDiv:
10179 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
10180 // 'udiv x, C' produces [0, UINT_MAX / C].
10181 Upper = APInt::getMaxValue(Width).udiv(*C) + 1;
10182 } else if (match(BO.getOperand(0), m_APInt(C))) {
10183 // 'udiv C, x' produces [0, C].
10184 Upper = *C + 1;
10185 }
10186 break;
10187
10188 case Instruction::SRem:
10189 if (match(BO.getOperand(1), m_APInt(C))) {
10190 // 'srem x, C' produces (-|C|, |C|).
10191 Upper = C->abs();
10192 Lower = (-Upper) + 1;
10193 } else if (match(BO.getOperand(0), m_APInt(C))) {
10194 if (C->isNegative()) {
10195 // 'srem -|C|, x' produces [-|C|, 0].
10196 Upper = 1;
10197 Lower = *C;
10198 } else {
10199 // 'srem |C|, x' produces [0, |C|].
10200 Upper = *C + 1;
10201 }
10202 }
10203 break;
10204
10205 case Instruction::URem:
10206 if (match(BO.getOperand(1), m_APInt(C)))
10207 // 'urem x, C' produces [0, C).
10208 Upper = *C;
10209 else if (match(BO.getOperand(0), m_APInt(C)))
10210 // 'urem C, x' produces [0, C].
10211 Upper = *C + 1;
10212 break;
10213
10214 default:
10215 break;
10216 }
10217}
10218
10220 bool UseInstrInfo) {
10221 unsigned Width = II.getType()->getScalarSizeInBits();
10222 const APInt *C;
10223 switch (II.getIntrinsicID()) {
10224 case Intrinsic::ctlz:
10225 case Intrinsic::cttz: {
10226 APInt Upper(Width, Width);
10227 if (!UseInstrInfo || !match(II.getArgOperand(1), m_One()))
10228 Upper += 1;
10229 // Maximum of set/clear bits is the bit width.
10231 }
10232 case Intrinsic::ctpop:
10233 // Maximum of set/clear bits is the bit width.
10235 APInt(Width, Width) + 1);
10236 case Intrinsic::uadd_sat:
10237 // uadd.sat(x, C) produces [C, UINT_MAX].
10238 if (match(II.getOperand(0), m_APInt(C)) ||
10239 match(II.getOperand(1), m_APInt(C)))
10241 break;
10242 case Intrinsic::sadd_sat:
10243 if (match(II.getOperand(0), m_APInt(C)) ||
10244 match(II.getOperand(1), m_APInt(C))) {
10245 if (C->isNegative())
10246 // sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)].
10248 APInt::getSignedMaxValue(Width) + *C +
10249 1);
10250
10251 // sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].
10253 APInt::getSignedMaxValue(Width) + 1);
10254 }
10255 break;
10256 case Intrinsic::usub_sat:
10257 // usub.sat(C, x) produces [0, C].
10258 if (match(II.getOperand(0), m_APInt(C)))
10259 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
10260
10261 // usub.sat(x, C) produces [0, UINT_MAX - C].
10262 if (match(II.getOperand(1), m_APInt(C)))
10264 APInt::getMaxValue(Width) - *C + 1);
10265 break;
10266 case Intrinsic::ssub_sat:
10267 if (match(II.getOperand(0), m_APInt(C))) {
10268 if (C->isNegative())
10269 // ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)].
10271 *C - APInt::getSignedMinValue(Width) +
10272 1);
10273
10274 // ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].
10276 APInt::getSignedMaxValue(Width) + 1);
10277 } else if (match(II.getOperand(1), m_APInt(C))) {
10278 if (C->isNegative())
10279 // ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]:
10281 APInt::getSignedMaxValue(Width) + 1);
10282
10283 // ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].
10285 APInt::getSignedMaxValue(Width) - *C +
10286 1);
10287 }
10288 break;
10289 case Intrinsic::umin:
10290 case Intrinsic::umax:
10291 case Intrinsic::smin:
10292 case Intrinsic::smax:
10293 if (!match(II.getOperand(0), m_APInt(C)) &&
10294 !match(II.getOperand(1), m_APInt(C)))
10295 break;
10296
10297 switch (II.getIntrinsicID()) {
10298 case Intrinsic::umin:
10299 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
10300 case Intrinsic::umax:
10302 case Intrinsic::smin:
10304 *C + 1);
10305 case Intrinsic::smax:
10307 APInt::getSignedMaxValue(Width) + 1);
10308 default:
10309 llvm_unreachable("Must be min/max intrinsic");
10310 }
10311 break;
10312 case Intrinsic::abs:
10313 // If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX],
10314 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10315 if (match(II.getOperand(1), m_One()))
10317 APInt::getSignedMaxValue(Width) + 1);
10318
10320 APInt::getSignedMinValue(Width) + 1);
10321 case Intrinsic::vscale:
10322 if (!II.getParent() || !II.getFunction())
10323 break;
10324 return getVScaleRange(II.getFunction(), Width);
10325 default:
10326 break;
10327 }
10328
10329 return ConstantRange::getFull(Width);
10330}
10331
10333 const InstrInfoQuery &IIQ) {
10334 unsigned BitWidth = SI.getType()->getScalarSizeInBits();
10335 const Value *LHS = nullptr, *RHS = nullptr;
10337 if (R.Flavor == SPF_UNKNOWN)
10338 return ConstantRange::getFull(BitWidth);
10339
10340 if (R.Flavor == SelectPatternFlavor::SPF_ABS) {
10341 // If the negation part of the abs (in RHS) has the NSW flag,
10342 // then the result of abs(X) is [0..SIGNED_MAX],
10343 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10344 if (match(RHS, m_Neg(m_Specific(LHS))) &&
10348
10351 }
10352
10353 if (R.Flavor == SelectPatternFlavor::SPF_NABS) {
10354 // The result of -abs(X) is <= 0.
10356 APInt(BitWidth, 1));
10357 }
10358
10359 const APInt *C;
10360 if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C)))
10361 return ConstantRange::getFull(BitWidth);
10362
10363 switch (R.Flavor) {
10364 case SPF_UMIN:
10366 case SPF_UMAX:
10368 case SPF_SMIN:
10370 *C + 1);
10371 case SPF_SMAX:
10374 default:
10375 return ConstantRange::getFull(BitWidth);
10376 }
10377}
10378
10380 // The maximum representable value of a half is 65504. For floats the maximum
10381 // value is 3.4e38 which requires roughly 129 bits.
10382 unsigned BitWidth = I->getType()->getScalarSizeInBits();
10383 if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy())
10384 return;
10385 if (isa<FPToSIInst>(I) && BitWidth >= 17) {
10386 Lower = APInt(BitWidth, -65504, true);
10387 Upper = APInt(BitWidth, 65505);
10388 }
10389
10390 if (isa<FPToUIInst>(I) && BitWidth >= 16) {
10391 // For a fptoui the lower limit is left as 0.
10392 Upper = APInt(BitWidth, 65505);
10393 }
10394}
10395
10397 bool UseInstrInfo, AssumptionCache *AC,
10398 const Instruction *CtxI,
10399 const DominatorTree *DT,
10400 unsigned Depth) {
10401 assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction");
10402
10404 return ConstantRange::getFull(V->getType()->getScalarSizeInBits());
10405
10406 if (auto *C = dyn_cast<Constant>(V))
10407 return C->toConstantRange();
10408
10409 unsigned BitWidth = V->getType()->getScalarSizeInBits();
10410 InstrInfoQuery IIQ(UseInstrInfo);
10411 ConstantRange CR = ConstantRange::getFull(BitWidth);
10412 if (auto *BO = dyn_cast<BinaryOperator>(V)) {
10413 APInt Lower = APInt(BitWidth, 0);
10414 APInt Upper = APInt(BitWidth, 0);
10415 // TODO: Return ConstantRange.
10416 setLimitsForBinOp(*BO, Lower, Upper, IIQ, ForSigned);
10418 } else if (auto *II = dyn_cast<IntrinsicInst>(V))
10419 CR = getRangeForIntrinsic(*II, UseInstrInfo);
10420 else if (auto *SI = dyn_cast<SelectInst>(V)) {
10422 SI->getTrueValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
10424 SI->getFalseValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
10425 CR = CRTrue.unionWith(CRFalse);
10427 } else if (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {
10428 APInt Lower = APInt(BitWidth, 0);
10429 APInt Upper = APInt(BitWidth, 0);
10430 // TODO: Return ConstantRange.
10433 } else if (const auto *A = dyn_cast<Argument>(V))
10434 if (std::optional<ConstantRange> Range = A->getRange())
10435 CR = *Range;
10436
10437 if (auto *I = dyn_cast<Instruction>(V)) {
10438 if (auto *Range = IIQ.getMetadata(I, LLVMContext::MD_range))
10440
10441 if (const auto *CB = dyn_cast<CallBase>(V))
10442 if (std::optional<ConstantRange> Range = CB->getRange())
10443 CR = CR.intersectWith(*Range);
10444 }
10445
10446 if (CtxI && AC) {
10447 // Try to restrict the range based on information from assumptions.
10448 for (auto &AssumeVH : AC->assumptionsFor(V)) {
10449 if (!AssumeVH)
10450 continue;
10451 CallInst *I = cast<CallInst>(AssumeVH);
10452 assert(I->getParent()->getParent() == CtxI->getParent()->getParent() &&
10453 "Got assumption for the wrong function!");
10454 assert(I->getIntrinsicID() == Intrinsic::assume &&
10455 "must be an assume intrinsic");
10456
10457 if (!isValidAssumeForContext(I, CtxI, DT))
10458 continue;
10459 Value *Arg = I->getArgOperand(0);
10460 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
10461 // Currently we just use information from comparisons.
10462 if (!Cmp || Cmp->getOperand(0) != V)
10463 continue;
10464 // TODO: Set "ForSigned" parameter via Cmp->isSigned()?
10465 ConstantRange RHS =
10466 computeConstantRange(Cmp->getOperand(1), /* ForSigned */ false,
10467 UseInstrInfo, AC, I, DT, Depth + 1);
10468 CR = CR.intersectWith(
10469 ConstantRange::makeAllowedICmpRegion(Cmp->getPredicate(), RHS));
10470 }
10471 }
10472
10473 return CR;
10474}
10475
10476static void
10478 function_ref<void(Value *)> InsertAffected) {
10479 assert(V != nullptr);
10480 if (isa<Argument>(V) || isa<GlobalValue>(V)) {
10481 InsertAffected(V);
10482 } else if (auto *I = dyn_cast<Instruction>(V)) {
10483 InsertAffected(V);
10484
10485 // Peek through unary operators to find the source of the condition.
10486 Value *Op;
10489 InsertAffected(Op);
10490 }
10491 }
10492}
10493
10495 Value *Cond, bool IsAssume, function_ref<void(Value *)> InsertAffected) {
10496 auto AddAffected = [&InsertAffected](Value *V) {
10497 addValueAffectedByCondition(V, InsertAffected);
10498 };
10499
10500 auto AddCmpOperands = [&AddAffected, IsAssume](Value *LHS, Value *RHS) {
10501 if (IsAssume) {
10502 AddAffected(LHS);
10503 AddAffected(RHS);
10504 } else if (match(RHS, m_Constant()))
10505 AddAffected(LHS);
10506 };
10507
10508 SmallVector<Value *, 8> Worklist;
10510 Worklist.push_back(Cond);
10511 while (!Worklist.empty()) {
10512 Value *V = Worklist.pop_back_val();
10513 if (!Visited.insert(V).second)
10514 continue;
10515
10516 CmpPredicate Pred;
10517 Value *A, *B, *X;
10518
10519 if (IsAssume) {
10520 AddAffected(V);
10521 if (match(V, m_Not(m_Value(X))))
10522 AddAffected(X);
10523 }
10524
10525 if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
10526 // assume(A && B) is split to -> assume(A); assume(B);
10527 // assume(!(A || B)) is split to -> assume(!A); assume(!B);
10528 // Finally, assume(A || B) / assume(!(A && B)) generally don't provide
10529 // enough information to be worth handling (intersection of information as
10530 // opposed to union).
10531 if (!IsAssume) {
10532 Worklist.push_back(A);
10533 Worklist.push_back(B);
10534 }
10535 } else if (match(V, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
10536 bool HasRHSC = match(B, m_ConstantInt());
10537 if (ICmpInst::isEquality(Pred)) {
10538 AddAffected(A);
10539 if (IsAssume)
10540 AddAffected(B);
10541 if (HasRHSC) {
10542 Value *Y;
10543 // (X << C) or (X >>_s C) or (X >>_u C).
10544 if (match(A, m_Shift(m_Value(X), m_ConstantInt())))
10545 AddAffected(X);
10546 // (X & C) or (X | C).
10547 else if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10548 match(A, m_Or(m_Value(X), m_Value(Y)))) {
10549 AddAffected(X);
10550 AddAffected(Y);
10551 }
10552 // X - Y
10553 else if (match(A, m_Sub(m_Value(X), m_Value(Y)))) {
10554 AddAffected(X);
10555 AddAffected(Y);
10556 }
10557 }
10558 } else {
10559 AddCmpOperands(A, B);
10560 if (HasRHSC) {
10561 // Handle (A + C1) u< C2, which is the canonical form of
10562 // A > C3 && A < C4.
10564 AddAffected(X);
10565
10566 if (ICmpInst::isUnsigned(Pred)) {
10567 Value *Y;
10568 // X & Y u> C -> X >u C && Y >u C
10569 // X | Y u< C -> X u< C && Y u< C
10570 // X nuw+ Y u< C -> X u< C && Y u< C
10571 if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10572 match(A, m_Or(m_Value(X), m_Value(Y))) ||
10573 match(A, m_NUWAdd(m_Value(X), m_Value(Y)))) {
10574 AddAffected(X);
10575 AddAffected(Y);
10576 }
10577 // X nuw- Y u> C -> X u> C
10578 if (match(A, m_NUWSub(m_Value(X), m_Value())))
10579 AddAffected(X);
10580 }
10581 }
10582
10583 // Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported
10584 // by computeKnownFPClass().
10586 if (Pred == ICmpInst::ICMP_SLT && match(B, m_Zero()))
10587 InsertAffected(X);
10588 else if (Pred == ICmpInst::ICMP_SGT && match(B, m_AllOnes()))
10589 InsertAffected(X);
10590 }
10591 }
10592
10593 if (HasRHSC && match(A, m_Intrinsic<Intrinsic::ctpop>(m_Value(X))))
10594 AddAffected(X);
10595 } else if (match(V, m_FCmp(Pred, m_Value(A), m_Value(B)))) {
10596 AddCmpOperands(A, B);
10597
10598 // fcmp fneg(x), y
10599 // fcmp fabs(x), y
10600 // fcmp fneg(fabs(x)), y
10601 if (match(A, m_FNeg(m_Value(A))))
10602 AddAffected(A);
10603 if (match(A, m_FAbs(m_Value(A))))
10604 AddAffected(A);
10605
10607 m_Value()))) {
10608 // Handle patterns that computeKnownFPClass() support.
10609 AddAffected(A);
10610 } else if (!IsAssume && match(V, m_Trunc(m_Value(X)))) {
10611 // Assume is checked here as X is already added above for assumes in
10612 // addValueAffectedByCondition
10613 AddAffected(X);
10614 } else if (!IsAssume && match(V, m_Not(m_Value(X)))) {
10615 // Assume is checked here to avoid issues with ephemeral values
10616 Worklist.push_back(X);
10617 }
10618 }
10619}
10620
10622 // (X >> C) or/add (X & mask(C) != 0)
10623 if (const auto *BO = dyn_cast<BinaryOperator>(V)) {
10624 if (BO->getOpcode() == Instruction::Add ||
10625 BO->getOpcode() == Instruction::Or) {
10626 const Value *X;
10627 const APInt *C1, *C2;
10628 if (match(BO, m_c_BinOp(m_LShr(m_Value(X), m_APInt(C1)),
10632 m_Zero())))) &&
10633 C2->popcount() == C1->getZExtValue())
10634 return X;
10635 }
10636 }
10637 return nullptr;
10638}
10639
10641 return const_cast<Value *>(stripNullTest(const_cast<const Value *>(V)));
10642}
10643
10646 unsigned MaxCount, bool AllowUndefOrPoison) {
10649 auto Push = [&](const Value *V) -> bool {
10650 Constant *C;
10651 if (match(const_cast<Value *>(V), m_ImmConstant(C))) {
10652 if (!AllowUndefOrPoison && !isGuaranteedNotToBeUndefOrPoison(C))
10653 return false;
10654 // Check existence first to avoid unnecessary allocations.
10655 if (Constants.contains(C))
10656 return true;
10657 if (Constants.size() == MaxCount)
10658 return false;
10659 Constants.insert(C);
10660 return true;
10661 }
10662
10663 if (auto *Inst = dyn_cast<Instruction>(V)) {
10664 if (Visited.insert(Inst).second)
10665 Worklist.push_back(Inst);
10666 return true;
10667 }
10668 return false;
10669 };
10670 if (!Push(V))
10671 return false;
10672 while (!Worklist.empty()) {
10673 const Instruction *CurInst = Worklist.pop_back_val();
10674 switch (CurInst->getOpcode()) {
10675 case Instruction::Select:
10676 if (!Push(CurInst->getOperand(1)))
10677 return false;
10678 if (!Push(CurInst->getOperand(2)))
10679 return false;
10680 break;
10681 case Instruction::PHI:
10682 for (Value *IncomingValue : cast<PHINode>(CurInst)->incoming_values()) {
10683 // Fast path for recurrence PHI.
10684 if (IncomingValue == CurInst)
10685 continue;
10686 if (!Push(IncomingValue))
10687 return false;
10688 }
10689 break;
10690 default:
10691 return false;
10692 }
10693 }
10694 return true;
10695}
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 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 TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
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 bool outputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty)
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 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 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 isKnownNonNaN(const Value *V, FastMathFlags FMF)
static ConstantRange getRangeForIntrinsic(const IntrinsicInst &II, bool UseInstrInfo)
static void computeKnownFPClassForFPTrunc(const Operator *Op, const APInt &DemandedElts, FPClassTest InterestedClasses, KnownFPClass &Known, const SimplifyQuery &Q, unsigned Depth)
static Value * BuildSubAggregate(Value *From, Value *To, Type *IndexedType, SmallVectorImpl< unsigned > &Idxs, unsigned IdxSkip, BasicBlock::iterator InsertBefore)
Value * RHS
Value * LHS
static LLVM_ABI unsigned int semanticsPrecision(const fltSemantics &)
Definition APFloat.cpp:290
static LLVM_ABI bool isRepresentableAsNormalIn(const fltSemantics &Src, const fltSemantics &Dst)
Definition APFloat.cpp:340
bool isFinite() const
Definition APFloat.h:1436
bool isNaN() const
Definition APFloat.h:1429
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1120
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1080
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
Definition APFloat.h:1061
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:1971
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1573
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:1407
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:1541
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition APInt.h:1392
unsigned popcount() const
Count the number of bits set.
Definition APInt.h:1671
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1386
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:1331
unsigned ceilLogBase2() const
Definition APInt.h:1765
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition APInt.h:1202
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:1183
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:1666
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1489
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1112
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:1250
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1644
void clearAllBits()
Set every bit to 0.
Definition APInt.h:1397
LLVM_ABI APInt reverseBits() const
Definition APInt.cpp:768
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition APInt.h:1167
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1629
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition APInt.h:1599
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:1041
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition APInt.h:357
unsigned logBase2() const
Definition APInt.h:1762
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:828
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:1151
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:874
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition APInt.h:1258
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition APInt.h:1131
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:1389
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1238
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:852
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1222
void clearSignBit()
Set the sign bit to 0.
Definition APInt.h:1450
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:69
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:223
LLVM_ABI bool isSingleEdge() const
Check if this is the only edge between Start and End.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator end()
Definition BasicBlock.h:472
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:459
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
InstListType::const_iterator const_iterator
Definition BasicBlock.h:171
LLVM_ABI const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
LLVM_ABI const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition BasicBlock.h:233
LLVM_ABI Instruction::BinaryOps getBinaryOp() const
Returns the binary operation underlying the intrinsic.
BinaryOps getOpcode() const
Definition InstrTypes.h:374
Conditional or Unconditional Branch instruction.
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
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.
An array constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition Constants.h:710
ConstantDataSequential - A vector or array constant whose element type is a simple 1/2/4/8-byte integ...
Definition Constants.h:601
StringRef getAsString() const
If this array is isString(), then this method returns the array as a StringRef.
Definition Constants.h:676
A vector constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition Constants.h:784
static LLVM_ABI Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static LLVM_ABI Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=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:285
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:171
This class represents a range of values.
PreferredRangeType
If represented precisely, the result of some range operations may consist of multiple disjoint ranges...
const APInt * getSingleElement() const
If this set contains a single element, return it, otherwise return null.
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 APInt getSignedMin() const
Return the smallest signed value contained in the ConstantRange.
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 isZeroValue() const
Return true if the value is negative zero or null value.
Definition Constants.cpp:76
LLVM_ABI bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition Constants.cpp:90
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
bool isLittleEndian() const
Layout endianness...
Definition DataLayout.h:214
unsigned getAddressSizeInBits(unsigned AS) const
The size in bits of an address in for the given AS.
Definition DataLayout.h:507
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:771
ArrayRef< BranchInst * > conditionsFor(const Value *V) const
Access the list of branches which affect this value.
DomTreeNodeBase * getIDom() const
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:164
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
This instruction extracts a struct member or array element value from an aggregate value.
ArrayRef< unsigned > getIndices() const
unsigned getNumIndices() const
static LLVM_ABI Type * getIndexedType(Type *Agg, ArrayRef< unsigned > Idxs)
Returns the type of the element that would be extracted with an extractvalue instruction with the spe...
This instruction compares its operands according to the predicate given to the constructor.
Utility class for floating point operations which can have information about relaxed accuracy require...
Definition Operator.h:200
Convenience struct for specifying and reasoning about fast-math flags.
Definition FMF.h:22
bool noSignedZeros() const
Definition FMF.h:67
bool noInfs() const
Definition FMF.h:66
void setNoSignedZeros(bool B=true)
Definition FMF.h:84
void setNoNaNs(bool B=true)
Definition FMF.h:78
bool noNaNs() const
Definition FMF.h:65
const BasicBlock & getEntryBlock() const
Definition Function.h:807
bool hasNoSync() const
Determine if the call can synchroize with other threads.
Definition Function.h:637
DenormalMode getDenormalMode(const fltSemantics &FPType) const
Returns the denormal handling type for the default rounding mode of the function.
Definition Function.cpp:806
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:132
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:1078
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:573
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition DataLayout.h:723
TypeSize getElementOffset(unsigned Idx) const
Definition DataLayout.h:754
Class to represent struct types.
unsigned getNumElements() const
Random access to the elements.
Type * getElementType(unsigned N) const
Provides information about what library functions are available for the current target.
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
static LLVM_ABI IntegerType * getInt64Ty(LLVMContext &C)
Definition Type.cpp:297
LLVM_ABI unsigned getIntegerBitWidth() const
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:273
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:296
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition Type.h:246
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:267
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
Definition Type.h:153
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
LLVM_ABI uint64_t getArrayNumElements() const
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:294
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:352
static LLVM_ABI IntegerType * getInt16Ty(LLVMContext &C)
Definition Type.cpp:295
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition Type.h:311
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:230
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
Definition Type.h:270
bool isIntOrPtrTy() const
Return true if this is an integer type or a pointer type.
Definition Type.h:255
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:240
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition Type.cpp:300
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
Definition Type.h:225
LLVM_ABI const fltSemantics & getFltSemantics() const
Definition Type.cpp:106
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM_ABI unsigned getOperandNo() const
Return the operand # of this use in its User.
Definition Use.cpp:35
User * getUser() const
Returns the User that contains this Use.
Definition Use.h:61
op_range operands()
Definition User.h:293
Value * getOperand(unsigned i) const
Definition User.h:233
unsigned getNumOperands() const
Definition User.h:255
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:759
iterator_range< user_iterator > users()
Definition Value.h:426
LLVM_ABI const Value * stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, bool AllowInvariantGroup=false, function_ref< bool(Value &Value, APInt &Offset)> ExternalAnalysis=nullptr, bool LookThroughIntToPtr=false) const
Accumulate the constant offset this value has compared to a base pointer.
const KnownBits & getKnownBits(const SimplifyQuery &Q) const
Definition WithCache.h:59
PointerType getValue() const
Definition WithCache.h:57
Represents an op.with.overflow intrinsic.
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:168
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:165
An efficient, type-erasing, non-owning reference to a callable.
TypeSize getSequentialElementStride(const DataLayout &DL) const
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:123
A range adaptor for a pair of iterators.
CallInst * Call
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
#define UINT64_MAX
Definition DataTypes.h:77
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI APInt ScaleBitMask(const APInt &A, unsigned NewBitWidth, bool MatchAllBits=false)
Splat/Merge neighboring bits to widen/narrow the bitmask represented by.
Definition APInt.cpp:3009
const APInt & umax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be unsigned.
Definition APInt.h:2264
@ 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)
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.
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.
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.
MatchFunctor< Val, Pattern > match_fn(const Pattern &P)
A match functor that can be used as a UnaryPredicate in functional algorithms like all_of.
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:667
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:1737
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:1667
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:2530
LLVM_ABI AllocaInst * findAllocaForValue(Value *V, bool OffsetZero=false)
Returns unique alloca where the value comes from, or nullptr.
LLVM_ABI APInt getMinMaxLimit(SelectPatternFlavor SPF, unsigned BitWidth)
Return the minimum or maximum constant value for the specified integer min/max flavor and type.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI bool isOnlyUsedInZeroComparison(const Instruction *CxtI)
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
LLVM_ABI bool getConstantStringInfo(const Value *V, StringRef &Str, bool TrimAtNul=true)
This function computes the length of a null-terminated C string pointed to by V.
LLVM_ABI bool isDereferenceableAndAlignedPointer(const Value *V, Type *Ty, Align Alignment, const DataLayout &DL, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Returns true if V is always a dereferenceable pointer with alignment greater or equal than requested.
Definition Loads.cpp:229
LLVM_ABI bool onlyUsedByLifetimeMarkersOrDroppableInsts(const Value *V)
Return true if the only users of this pointer are lifetime markers or droppable instructions.
LLVM_ABI Constant * ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset)
LLVM_ABI Value * stripNullTest(Value *V)
Returns the inner value X if the expression has the form f(X) where f(X) == 0 if and only if X == 0,...
LLVM_ABI bool getUnderlyingObjectsForCodeGen(const Value *V, SmallVectorImpl< Value * > &Objects)
This is a wrapper around getUnderlyingObjects and adds support for basic ptrtoint+arithmetic+inttoptr...
LLVM_ABI std::pair< Intrinsic::ID, bool > canConvertToMinOrMaxIntrinsic(ArrayRef< Value * > VL)
Check if the values in VL are select instructions that can be converted to a min or max (vector) intr...
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ABI bool getConstantDataArrayInfo(const Value *V, ConstantDataArraySlice &Slice, unsigned ElementSize, uint64_t Offset=0)
Returns true if the value V is a pointer into a ConstantDataArray.
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition bit.h:303
LLVM_ABI bool isGuaranteedToExecuteForEveryIteration(const Instruction *I, const Loop *L)
Return true if this function can prove that the instruction I is executed for every iteration of the ...
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2184
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:420
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:1516
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:1744
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...
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 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...
auto make_scope_exit(Callable &&F)
Definition ScopeExit.h:53
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:1945
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.
DenormalModeKind Input
Denormal treatment kind for floating point instruction inputs in the default floating-point environme...
constexpr bool outputsAreZero() const
Return true if output denormals should be flushed to 0.
@ PositiveZero
Denormals are flushed to positive zero.
@ IEEE
IEEE-754 denormal numbers preserved.
constexpr bool inputsAreZero() const
Return true if input denormals must be implicitly treated as 0.
static constexpr DenormalMode getPreserveSign()
DenormalModeKind Output
Denormal flushing mode for floating point instruction results in the default floating point environme...
static constexpr DenormalMode getIEEE()
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:301
static LLVM_ABI KnownBits sadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)
static LLVM_ABI std::optional< bool > eq(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_EQ result.
KnownBits anyextOrTrunc(unsigned BitWidth) const
Return known bits for an "any" extension or truncation of the value we're tracking.
Definition KnownBits.h:186
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:255
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
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:124
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:251
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition KnownBits.h:242
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:274
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:119
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:161
KnownBits byteSwap() const
Definition KnownBits.h:514
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:289
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition KnownBits.h:86
KnownBits reverseBits() const
Definition KnownBits.h:518
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:172
bool isConstant() const
Returns true if we know the value of all bits.
Definition KnownBits.h:54
void resetAll()
Resets the known state of all bits.
Definition KnownBits.h:74
KnownBits unionWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for either this or RHS or both.
Definition KnownBits.h:321
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
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition KnownBits.h:225
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition KnownBits.h:311
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition KnownBits.h:180
unsigned countMinTrailingOnes() const
Returns the minimum number of trailing one bits.
Definition KnownBits.h:245
static KnownBits add(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from addition of LHS and RHS.
Definition KnownBits.h:347
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition KnownBits.h:196
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition KnownBits.h:248
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:145
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:129
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:60
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:326
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:353
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition KnownBits.h:280
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:219
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:167
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:206
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 constexpr FPClassTest OrderedGreaterThanZeroMask
static constexpr FPClassTest OrderedLessThanZeroMask
void knownNot(FPClassTest RuleOut)
void copysign(const KnownFPClass &Sign)
bool isKnownNeverSubnormal() const
Return true if it's known this can never be a subnormal.
bool isKnownAlways(FPClassTest Mask) const
LLVM_ABI bool isKnownNeverLogicalZero(DenormalMode Mode) const
Return true if it's know this can never be interpreted as a zero.
bool isUnknown() const
bool isKnownNeverNegInfinity() const
Return true if it's known this can never be -infinity.
bool isKnownNeverNegSubnormal() const
Return true if it's known this can never be a negative subnormal.
bool isKnownNeverPosZero() const
Return true if it's known this can never be a literal positive zero.
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.
bool isKnownNeverNegZero() const
Return true if it's known this can never be a negative zero.
void propagateNaN(const KnownFPClass &Src, bool PreserveSign=false)
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.
LLVM_ABI void propagateCanonicalizingSrc(const KnownFPClass &Src, DenormalMode Mode)
Report known classes if Src is evaluated through a potentially canonicalizing operation.
void signBitMustBeZero()
Assume the sign bit is zero.
LLVM_ABI bool isKnownNeverLogicalPosZero(DenormalMode Mode) const
Return true if it's know this can never be interpreted as a positive zero.
bool isKnownNeverPosInfinity() const
Return true if it's known this can never be +infinity.
LLVM_ABI bool isKnownNeverLogicalNegZero(DenormalMode Mode) const
Return true if it's know this can never be interpreted as a negative zero.
bool isKnownNeverPosSubnormal() const
Return true if it's known this can never be a positive subnormal.
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