LLVM 23.0.0git
ValueTracking.cpp
Go to the documentation of this file.
1//===- ValueTracking.cpp - Walk computations to compute properties --------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains routines that help analyze properties that chains of
10// computations have.
11//
12//===----------------------------------------------------------------------===//
13
15#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/ScopeExit.h"
23#include "llvm/ADT/StringRef.h"
33#include "llvm/Analysis/Loads.h"
38#include "llvm/IR/Argument.h"
39#include "llvm/IR/Attributes.h"
40#include "llvm/IR/BasicBlock.h"
41#include "llvm/IR/Constant.h"
44#include "llvm/IR/Constants.h"
47#include "llvm/IR/Dominators.h"
49#include "llvm/IR/Function.h"
51#include "llvm/IR/GlobalAlias.h"
52#include "llvm/IR/GlobalValue.h"
54#include "llvm/IR/InstrTypes.h"
55#include "llvm/IR/Instruction.h"
58#include "llvm/IR/Intrinsics.h"
59#include "llvm/IR/IntrinsicsAArch64.h"
60#include "llvm/IR/IntrinsicsAMDGPU.h"
61#include "llvm/IR/IntrinsicsRISCV.h"
62#include "llvm/IR/IntrinsicsX86.h"
63#include "llvm/IR/LLVMContext.h"
64#include "llvm/IR/Metadata.h"
65#include "llvm/IR/Module.h"
66#include "llvm/IR/Operator.h"
68#include "llvm/IR/Type.h"
69#include "llvm/IR/User.h"
70#include "llvm/IR/Value.h"
79#include <algorithm>
80#include <cassert>
81#include <cstdint>
82#include <optional>
83#include <utility>
84
85using namespace llvm;
86using namespace llvm::PatternMatch;
87
88// Controls the number of uses of the value searched for possible
89// dominating comparisons.
90static cl::opt<unsigned> DomConditionsMaxUses("dom-conditions-max-uses",
91 cl::Hidden, cl::init(20));
92
93/// Maximum number of instructions to check between assume and context
94/// instruction.
95static constexpr unsigned MaxInstrsToCheckForFree = 16;
96
97/// Returns the bitwidth of the given scalar or pointer type. For vector types,
98/// returns the element type's bitwidth.
99static unsigned getBitWidth(Type *Ty, const DataLayout &DL) {
100 if (unsigned BitWidth = Ty->getScalarSizeInBits())
101 return BitWidth;
102
103 return DL.getPointerTypeSizeInBits(Ty);
104}
105
106// Given the provided Value and, potentially, a context instruction, return
107// the preferred context instruction (if any).
108static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI) {
109 // If we've been provided with a context instruction, then use that (provided
110 // it has been inserted).
111 if (CxtI && CxtI->getParent())
112 return CxtI;
113
114 // If the value is really an already-inserted instruction, then use that.
115 CxtI = dyn_cast<Instruction>(V);
116 if (CxtI && CxtI->getParent())
117 return CxtI;
118
119 return nullptr;
120}
121
123 const APInt &DemandedElts,
124 APInt &DemandedLHS, APInt &DemandedRHS) {
125 if (isa<ScalableVectorType>(Shuf->getType())) {
126 assert(DemandedElts == APInt(1,1));
127 DemandedLHS = DemandedRHS = DemandedElts;
128 return true;
129 }
130
131 int NumElts =
132 cast<FixedVectorType>(Shuf->getOperand(0)->getType())->getNumElements();
133 return llvm::getShuffleDemandedElts(NumElts, Shuf->getShuffleMask(),
134 DemandedElts, DemandedLHS, DemandedRHS);
135}
136
137static void computeKnownBits(const Value *V, const APInt &DemandedElts,
138 KnownBits &Known, const SimplifyQuery &Q,
139 unsigned Depth);
140
142 const SimplifyQuery &Q, unsigned Depth) {
143 // Since the number of lanes in a scalable vector is unknown at compile time,
144 // we track one bit which is implicitly broadcast to all lanes. This means
145 // that all lanes in a scalable vector are considered demanded.
146 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
147 APInt DemandedElts =
148 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
149 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);
150}
151
153 const DataLayout &DL, AssumptionCache *AC,
154 const Instruction *CxtI, const DominatorTree *DT,
155 bool UseInstrInfo, unsigned Depth) {
156 computeKnownBits(V, Known,
157 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo),
158 Depth);
159}
160
162 AssumptionCache *AC, const Instruction *CxtI,
163 const DominatorTree *DT, bool UseInstrInfo,
164 unsigned Depth) {
165 return computeKnownBits(
166 V, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
167}
168
169KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
170 const DataLayout &DL, AssumptionCache *AC,
171 const Instruction *CxtI,
172 const DominatorTree *DT, bool UseInstrInfo,
173 unsigned Depth) {
174 return computeKnownBits(
175 V, DemandedElts,
176 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
177}
178
180 const SimplifyQuery &SQ) {
181 // Look for an inverted mask: (X & ~M) op (Y & M).
182 {
183 Value *M;
184 if (match(LHS, m_c_And(m_Not(m_Value(M)), m_Value())) &&
186 isGuaranteedNotToBeUndef(M, SQ.AC, SQ.CxtI, SQ.DT))
187 return true;
188 }
189
190 // X op (Y & ~X)
193 return true;
194
195 // X op ((X & Y) ^ Y) -- this is the canonical form of the previous pattern
196 // for constant Y.
197 Value *Y;
198 if (match(RHS,
200 isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT) &&
201 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
202 return true;
203
204 // Peek through extends to find a 'not' of the other side:
205 // (ext Y) op ext(~Y)
206 if (match(LHS, m_ZExtOrSExt(m_Value(Y))) &&
208 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
209 return true;
210
211 // Look for: (A & B) op ~(A | B)
212 {
213 Value *A, *B;
214 if (match(LHS, m_And(m_Value(A), m_Value(B))) &&
216 isGuaranteedNotToBeUndef(A, SQ.AC, SQ.CxtI, SQ.DT) &&
217 isGuaranteedNotToBeUndef(B, SQ.AC, SQ.CxtI, SQ.DT))
218 return true;
219 }
220
221 // Look for: (X << V) op (Y >> (BitWidth - V))
222 // or (X >> V) op (Y << (BitWidth - V))
223 {
224 const Value *V;
225 const APInt *R;
226 if (((match(RHS, m_Shl(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&
227 match(LHS, m_LShr(m_Value(), m_Specific(V)))) ||
228 (match(RHS, m_LShr(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&
229 match(LHS, m_Shl(m_Value(), m_Specific(V))))) &&
230 R->uge(LHS->getType()->getScalarSizeInBits()))
231 return true;
232 }
233
234 return false;
235}
236
238 const WithCache<const Value *> &RHSCache,
239 const SimplifyQuery &SQ) {
240 const Value *LHS = LHSCache.getValue();
241 const Value *RHS = RHSCache.getValue();
242
243 assert(LHS->getType() == RHS->getType() &&
244 "LHS and RHS should have the same type");
245 assert(LHS->getType()->isIntOrIntVectorTy() &&
246 "LHS and RHS should be integers");
247
248 if (haveNoCommonBitsSetSpecialCases(LHS, RHS, SQ) ||
250 return true;
251
253 RHSCache.getKnownBits(SQ));
254}
255
257 return !I->user_empty() &&
258 all_of(I->users(), match_fn(m_ICmp(m_Value(), m_Zero())));
259}
260
262 return !I->user_empty() && all_of(I->users(), [](const User *U) {
263 CmpPredicate P;
264 return match(U, m_ICmp(P, m_Value(), m_Zero())) && ICmpInst::isEquality(P);
265 });
266}
267
269 bool OrZero, AssumptionCache *AC,
270 const Instruction *CxtI,
271 const DominatorTree *DT, bool UseInstrInfo,
272 unsigned Depth) {
273 return ::isKnownToBeAPowerOfTwo(
274 V, OrZero, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo),
275 Depth);
276}
277
278static bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
279 const SimplifyQuery &Q, unsigned Depth);
280
282 unsigned Depth) {
283 return computeKnownBits(V, SQ, Depth).isNonNegative();
284}
285
287 unsigned Depth) {
288 if (auto *CI = dyn_cast<ConstantInt>(V))
289 return CI->getValue().isStrictlyPositive();
290
291 // If `isKnownNonNegative` ever becomes more sophisticated, make sure to keep
292 // this updated.
293 KnownBits Known = computeKnownBits(V, SQ, Depth);
294 return Known.isNonNegative() &&
295 (Known.isNonZero() || isKnownNonZero(V, SQ, Depth));
296}
297
299 unsigned Depth) {
300 return computeKnownBits(V, SQ, Depth).isNegative();
301}
302
303static bool isKnownNonEqual(const Value *V1, const Value *V2,
304 const APInt &DemandedElts, const SimplifyQuery &Q,
305 unsigned Depth);
306
307bool llvm::isKnownNonEqual(const Value *V1, const Value *V2,
308 const SimplifyQuery &Q, unsigned Depth) {
309 // We don't support looking through casts.
310 if (V1 == V2 || V1->getType() != V2->getType())
311 return false;
312 auto *FVTy = dyn_cast<FixedVectorType>(V1->getType());
313 APInt DemandedElts =
314 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
315 return ::isKnownNonEqual(V1, V2, DemandedElts, Q, Depth);
316}
317
318bool llvm::MaskedValueIsZero(const Value *V, const APInt &Mask,
319 const SimplifyQuery &SQ, unsigned Depth) {
320 KnownBits Known(Mask.getBitWidth());
321 computeKnownBits(V, Known, SQ, Depth);
322 return Mask.isSubsetOf(Known.Zero);
323}
324
325static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
326 const SimplifyQuery &Q, unsigned Depth);
327
328static unsigned ComputeNumSignBits(const Value *V, const SimplifyQuery &Q,
329 unsigned Depth = 0) {
330 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
331 APInt DemandedElts =
332 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
333 return ComputeNumSignBits(V, DemandedElts, Q, Depth);
334}
335
336unsigned llvm::ComputeNumSignBits(const Value *V, const DataLayout &DL,
337 AssumptionCache *AC, const Instruction *CxtI,
338 const DominatorTree *DT, bool UseInstrInfo,
339 unsigned Depth) {
340 return ::ComputeNumSignBits(
341 V, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
342}
343
345 AssumptionCache *AC,
346 const Instruction *CxtI,
347 const DominatorTree *DT,
348 unsigned Depth) {
349 unsigned SignBits = ComputeNumSignBits(V, DL, AC, CxtI, DT, Depth);
350 return V->getType()->getScalarSizeInBits() - SignBits + 1;
351}
352
353/// Try to detect the lerp pattern: a * (b - c) + c * d
354/// where a >= 0, b >= 0, c >= 0, d >= 0, and b >= c.
355///
356/// In that particular case, we can use the following chain of reasoning:
357///
358/// a * (b - c) + c * d <= a' * (b - c) + a' * c = a' * b where a' = max(a, d)
359///
360/// Since that is true for arbitrary a, b, c and d within our constraints, we
361/// can conclude that:
362///
363/// max(a * (b - c) + c * d) <= max(max(a), max(d)) * max(b) = U
364///
365/// Considering that any result of the lerp would be less or equal to U, it
366/// would have at least the number of leading 0s as in U.
367///
368/// While being quite a specific situation, it is fairly common in computer
369/// graphics in the shape of alpha blending.
370///
371/// Modifies given KnownOut in-place with the inferred information.
372static void computeKnownBitsFromLerpPattern(const Value *Op0, const Value *Op1,
373 const APInt &DemandedElts,
374 KnownBits &KnownOut,
375 const SimplifyQuery &Q,
376 unsigned Depth) {
377
378 Type *Ty = Op0->getType();
379 const unsigned BitWidth = Ty->getScalarSizeInBits();
380
381 // Only handle scalar types for now
382 if (Ty->isVectorTy())
383 return;
384
385 // Try to match: a * (b - c) + c * d.
386 // When a == 1 => A == nullptr, the same applies to d/D as well.
387 const Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
388 const Instruction *SubBC = nullptr;
389
390 const auto MatchSubBC = [&]() {
391 // (b - c) can have two forms that interest us:
392 //
393 // 1. sub nuw %b, %c
394 // 2. xor %c, %b
395 //
396 // For the first case, nuw flag guarantees our requirement b >= c.
397 //
398 // The second case might happen when the analysis can infer that b is a mask
399 // for c and we can transform sub operation into xor (that is usually true
400 // for constant b's). Even though xor is symmetrical, canonicalization
401 // ensures that the constant will be the RHS. We have additional checks
402 // later on to ensure that this xor operation is equivalent to subtraction.
404 m_Xor(m_Value(C), m_Value(B))));
405 };
406
407 const auto MatchASubBC = [&]() {
408 // Cases:
409 // - a * (b - c)
410 // - (b - c) * a
411 // - (b - c) <- a implicitly equals 1
412 return m_CombineOr(m_c_Mul(m_Value(A), MatchSubBC()), MatchSubBC());
413 };
414
415 const auto MatchCD = [&]() {
416 // Cases:
417 // - d * c
418 // - c * d
419 // - c <- d implicitly equals 1
421 };
422
423 const auto Match = [&](const Value *LHS, const Value *RHS) {
424 // We do use m_Specific(C) in MatchCD, so we have to make sure that
425 // it's bound to anything and match(LHS, MatchASubBC()) absolutely
426 // has to evaluate first and return true.
427 //
428 // If Match returns true, it is guaranteed that B != nullptr, C != nullptr.
429 return match(LHS, MatchASubBC()) && match(RHS, MatchCD());
430 };
431
432 if (!Match(Op0, Op1) && !Match(Op1, Op0))
433 return;
434
435 const auto ComputeKnownBitsOrOne = [&](const Value *V) {
436 // For some of the values we use the convention of leaving
437 // it nullptr to signify an implicit constant 1.
438 return V ? computeKnownBits(V, DemandedElts, Q, Depth + 1)
440 };
441
442 // Check that all operands are non-negative
443 const KnownBits KnownA = ComputeKnownBitsOrOne(A);
444 if (!KnownA.isNonNegative())
445 return;
446
447 const KnownBits KnownD = ComputeKnownBitsOrOne(D);
448 if (!KnownD.isNonNegative())
449 return;
450
451 const KnownBits KnownB = computeKnownBits(B, DemandedElts, Q, Depth + 1);
452 if (!KnownB.isNonNegative())
453 return;
454
455 const KnownBits KnownC = computeKnownBits(C, DemandedElts, Q, Depth + 1);
456 if (!KnownC.isNonNegative())
457 return;
458
459 // If we matched subtraction as xor, we need to actually check that xor
460 // is semantically equivalent to subtraction.
461 //
462 // For that to be true, b has to be a mask for c or that b's known
463 // ones cover all known and possible ones of c.
464 if (SubBC->getOpcode() == Instruction::Xor &&
465 !KnownC.getMaxValue().isSubsetOf(KnownB.getMinValue()))
466 return;
467
468 const APInt MaxA = KnownA.getMaxValue();
469 const APInt MaxD = KnownD.getMaxValue();
470 const APInt MaxAD = APIntOps::umax(MaxA, MaxD);
471 const APInt MaxB = KnownB.getMaxValue();
472
473 // We can't infer leading zeros info if the upper-bound estimate wraps.
474 bool Overflow;
475 const APInt UpperBound = MaxAD.umul_ov(MaxB, Overflow);
476
477 if (Overflow)
478 return;
479
480 // If we know that x <= y and both are positive than x has at least the same
481 // number of leading zeros as y.
482 const unsigned MinimumNumberOfLeadingZeros = UpperBound.countl_zero();
483 KnownOut.Zero.setHighBits(MinimumNumberOfLeadingZeros);
484}
485
486static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1,
487 bool NSW, bool NUW,
488 const APInt &DemandedElts,
489 KnownBits &KnownOut, KnownBits &Known2,
490 const SimplifyQuery &Q, unsigned Depth) {
491 computeKnownBits(Op1, DemandedElts, KnownOut, Q, Depth + 1);
492
493 // If one operand is unknown and we have no nowrap information,
494 // the result will be unknown independently of the second operand.
495 if (KnownOut.isUnknown() && !NSW && !NUW)
496 return;
497
498 computeKnownBits(Op0, DemandedElts, Known2, Q, Depth + 1);
499 KnownOut = KnownBits::computeForAddSub(Add, NSW, NUW, Known2, KnownOut);
500
501 if (!Add && NSW && !KnownOut.isNonNegative() &&
503 .value_or(false))
504 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 continue;
839 bool AssumeImpliesNonNull = [&]() {
840 if (RK.AttrKind == Attribute::NonNull)
841 return true;
842
843 if (RK.AttrKind == Attribute::Dereferenceable) {
846 return false;
847 assert(RK.IRArgValue &&
848 "Dereferenceable attribute without IR argument?");
849
850 auto *CI = dyn_cast<ConstantInt>(RK.IRArgValue);
851 return CI && !CI->isZero();
852 }
853
854 return false;
855 }();
856 if (AssumeImpliesNonNull && isValidAssumeForContext(I, Q.CxtI, Q.DT))
857 return true;
858 }
859 continue;
860 }
861
862 // Warning: This loop can end up being somewhat performance sensitive.
863 // We're running this loop for once for each value queried resulting in a
864 // runtime of ~O(#assumes * #values).
865
866 Value *RHS;
867 CmpPredicate Pred;
868 auto m_V = m_CombineOr(m_Specific(V), m_PtrToInt(m_Specific(V)));
869 if (!match(I->getArgOperand(0), m_c_ICmp(Pred, m_V, m_Value(RHS))))
870 continue;
871
872 if (cmpExcludesZero(Pred, RHS) && isValidAssumeForContext(I, Q.CxtI, Q.DT))
873 return true;
874 }
875
876 return false;
877}
878
880 Value *LHS, Value *RHS, KnownBits &Known,
881 const SimplifyQuery &Q) {
882 if (RHS->getType()->isPointerTy()) {
883 // Handle comparison of pointer to null explicitly, as it will not be
884 // covered by the m_APInt() logic below.
885 if (LHS == V && match(RHS, m_Zero())) {
886 switch (Pred) {
888 Known.setAllZero();
889 break;
892 Known.makeNonNegative();
893 break;
895 Known.makeNegative();
896 break;
897 default:
898 break;
899 }
900 }
901 return;
902 }
903
904 unsigned BitWidth = Known.getBitWidth();
905 auto m_V =
907
908 Value *Y;
909 const APInt *Mask, *C;
910 if (!match(RHS, m_APInt(C)))
911 return;
912
913 uint64_t ShAmt;
914 switch (Pred) {
916 // assume(V = C)
917 if (match(LHS, m_V)) {
918 Known = Known.unionWith(KnownBits::makeConstant(*C));
919 // assume(V & Mask = C)
920 } else if (match(LHS, m_c_And(m_V, m_Value(Y)))) {
921 // For one bits in Mask, we can propagate bits from C to V.
922 Known.One |= *C;
923 if (match(Y, m_APInt(Mask)))
924 Known.Zero |= ~*C & *Mask;
925 // assume(V | Mask = C)
926 } else if (match(LHS, m_c_Or(m_V, m_Value(Y)))) {
927 // For zero bits in Mask, we can propagate bits from C to V.
928 Known.Zero |= ~*C;
929 if (match(Y, m_APInt(Mask)))
930 Known.One |= *C & ~*Mask;
931 // assume(V << ShAmt = C)
932 } else if (match(LHS, m_Shl(m_V, m_ConstantInt(ShAmt))) &&
933 ShAmt < BitWidth) {
934 // For those bits in C that are known, we can propagate them to known
935 // bits in V shifted to the right by ShAmt.
937 RHSKnown >>= ShAmt;
938 Known = Known.unionWith(RHSKnown);
939 // assume(V >> ShAmt = C)
940 } else if (match(LHS, m_Shr(m_V, m_ConstantInt(ShAmt))) &&
941 ShAmt < BitWidth) {
942 // For those bits in RHS that are known, we can propagate them to known
943 // bits in V shifted to the right by C.
945 RHSKnown <<= ShAmt;
946 Known = Known.unionWith(RHSKnown);
947 }
948 break;
949 case ICmpInst::ICMP_NE: {
950 // assume (V & B != 0) where B is a power of 2
951 const APInt *BPow2;
952 if (C->isZero() && match(LHS, m_And(m_V, m_Power2(BPow2))))
953 Known.One |= *BPow2;
954 break;
955 }
956 default: {
957 const APInt *Offset = nullptr;
958 if (match(LHS, m_CombineOr(m_V, m_AddLike(m_V, m_APInt(Offset))))) {
960 if (Offset)
961 LHSRange = LHSRange.sub(*Offset);
962 Known = Known.unionWith(LHSRange.toKnownBits());
963 }
964 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
965 // X & Y u> C -> X u> C && Y u> C
966 // X nuw- Y u> C -> X u> C
967 if (match(LHS, m_c_And(m_V, m_Value())) ||
968 match(LHS, m_NUWSub(m_V, m_Value())))
969 Known.One.setHighBits(
970 (*C + (Pred == ICmpInst::ICMP_UGT)).countLeadingOnes());
971 }
972 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
973 // X | Y u< C -> X u< C && Y u< C
974 // X nuw+ Y u< C -> X u< C && Y u< C
975 if (match(LHS, m_c_Or(m_V, m_Value())) ||
976 match(LHS, m_c_NUWAdd(m_V, m_Value()))) {
977 Known.Zero.setHighBits(
978 (*C - (Pred == ICmpInst::ICMP_ULT)).countLeadingZeros());
979 }
980 }
981 } break;
982 }
983}
984
985static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp,
986 KnownBits &Known,
987 const SimplifyQuery &SQ, bool Invert) {
989 Invert ? Cmp->getInversePredicate() : Cmp->getPredicate();
990 Value *LHS = Cmp->getOperand(0);
991 Value *RHS = Cmp->getOperand(1);
992
993 // Handle icmp pred (trunc V), C
994 if (match(LHS, m_Trunc(m_Specific(V)))) {
995 KnownBits DstKnown(LHS->getType()->getScalarSizeInBits());
996 computeKnownBitsFromCmp(LHS, Pred, LHS, RHS, DstKnown, SQ);
998 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
999 else
1000 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
1001 return;
1002 }
1003
1004 computeKnownBitsFromCmp(V, Pred, LHS, RHS, Known, SQ);
1005}
1006
1008 KnownBits &Known, const SimplifyQuery &SQ,
1009 bool Invert, unsigned Depth) {
1010 Value *A, *B;
1013 KnownBits Known2(Known.getBitWidth());
1014 KnownBits Known3(Known.getBitWidth());
1015 computeKnownBitsFromCond(V, A, Known2, SQ, Invert, Depth + 1);
1016 computeKnownBitsFromCond(V, B, Known3, SQ, Invert, Depth + 1);
1017 if (Invert ? match(Cond, m_LogicalOr(m_Value(), m_Value()))
1019 Known2 = Known2.unionWith(Known3);
1020 else
1021 Known2 = Known2.intersectWith(Known3);
1022 Known = Known.unionWith(Known2);
1023 return;
1024 }
1025
1026 if (auto *Cmp = dyn_cast<ICmpInst>(Cond)) {
1027 computeKnownBitsFromICmpCond(V, Cmp, Known, SQ, Invert);
1028 return;
1029 }
1030
1031 if (match(Cond, m_Trunc(m_Specific(V)))) {
1032 KnownBits DstKnown(1);
1033 if (Invert) {
1034 DstKnown.setAllZero();
1035 } else {
1036 DstKnown.setAllOnes();
1037 }
1039 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
1040 return;
1041 }
1042 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
1043 return;
1044 }
1045
1047 computeKnownBitsFromCond(V, A, Known, SQ, !Invert, Depth + 1);
1048}
1049
1051 const SimplifyQuery &Q, unsigned Depth) {
1052 // Handle injected condition.
1053 if (Q.CC && Q.CC->AffectedValues.contains(V))
1054 computeKnownBitsFromCond(V, Q.CC->Cond, Known, Q, Q.CC->Invert, Depth);
1055
1056 if (!Q.CxtI)
1057 return;
1058
1059 if (Q.DC && Q.DT) {
1060 // Handle dominating conditions.
1061 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
1062 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
1063 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
1064 computeKnownBitsFromCond(V, BI->getCondition(), Known, Q,
1065 /*Invert*/ false, Depth);
1066
1067 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
1068 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
1069 computeKnownBitsFromCond(V, BI->getCondition(), Known, Q,
1070 /*Invert*/ true, Depth);
1071 }
1072
1073 if (Known.hasConflict())
1074 Known.resetAll();
1075 }
1076
1077 if (!Q.AC)
1078 return;
1079
1080 unsigned BitWidth = Known.getBitWidth();
1081
1082 // Note that the patterns below need to be kept in sync with the code
1083 // in AssumptionCache::updateAffectedValues.
1084
1085 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
1086 if (!Elem.Assume)
1087 continue;
1088
1089 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
1090 assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() &&
1091 "Got assumption for the wrong function!");
1092
1093 if (Elem.Index != AssumptionCache::ExprResultIdx) {
1094 if (!V->getType()->isPointerTy())
1095 continue;
1097 *I, I->bundle_op_info_begin()[Elem.Index])) {
1098 // Allow AllowEphemerals in isValidAssumeForContext, as the CxtI might
1099 // be the producer of the pointer in the bundle. At the moment, align
1100 // assumptions aren't optimized away.
1101 if (RK.WasOn == V && RK.AttrKind == Attribute::Alignment &&
1102 isPowerOf2_64(RK.ArgValue) &&
1103 isValidAssumeForContext(I, Q.CxtI, Q.DT, /*AllowEphemerals*/ true))
1104 Known.Zero.setLowBits(Log2_64(RK.ArgValue));
1105 }
1106 continue;
1107 }
1108
1109 // Warning: This loop can end up being somewhat performance sensitive.
1110 // We're running this loop for once for each value queried resulting in a
1111 // runtime of ~O(#assumes * #values).
1112
1113 Value *Arg = I->getArgOperand(0);
1114
1115 if (Arg == V && isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
1116 assert(BitWidth == 1 && "assume operand is not i1?");
1117 (void)BitWidth;
1118 Known.setAllOnes();
1119 return;
1120 }
1121 if (match(Arg, m_Not(m_Specific(V))) &&
1123 assert(BitWidth == 1 && "assume operand is not i1?");
1124 (void)BitWidth;
1125 Known.setAllZero();
1126 return;
1127 }
1128 auto *Trunc = dyn_cast<TruncInst>(Arg);
1129 if (Trunc && Trunc->getOperand(0) == V &&
1131 if (Trunc->hasNoUnsignedWrap()) {
1133 return;
1134 }
1135 Known.One.setBit(0);
1136 return;
1137 }
1138
1139 // The remaining tests are all recursive, so bail out if we hit the limit.
1141 continue;
1142
1143 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
1144 if (!Cmp)
1145 continue;
1146
1147 if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
1148 continue;
1149
1150 computeKnownBitsFromICmpCond(V, Cmp, Known, Q, /*Invert=*/false);
1151 }
1152
1153 // Conflicting assumption: Undefined behavior will occur on this execution
1154 // path.
1155 if (Known.hasConflict())
1156 Known.resetAll();
1157}
1158
1159/// Compute known bits from a shift operator, including those with a
1160/// non-constant shift amount. Known is the output of this function. Known2 is a
1161/// pre-allocated temporary with the same bit width as Known and on return
1162/// contains the known bit of the shift value source. KF is an
1163/// operator-specific function that, given the known-bits and a shift amount,
1164/// compute the implied known-bits of the shift operator's result respectively
1165/// for that shift amount. The results from calling KF are conservatively
1166/// combined for all permitted shift amounts.
1168 const Operator *I, const APInt &DemandedElts, KnownBits &Known,
1169 KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth,
1170 function_ref<KnownBits(const KnownBits &, const KnownBits &, bool)> KF) {
1171 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1172 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1173 // To limit compile-time impact, only query isKnownNonZero() if we know at
1174 // least something about the shift amount.
1175 bool ShAmtNonZero =
1176 Known.isNonZero() ||
1177 (Known.getMaxValue().ult(Known.getBitWidth()) &&
1178 isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth + 1));
1179 Known = KF(Known2, Known, ShAmtNonZero);
1180}
1181
1182static KnownBits
1183getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts,
1184 const KnownBits &KnownLHS, const KnownBits &KnownRHS,
1185 const SimplifyQuery &Q, unsigned Depth) {
1186 unsigned BitWidth = KnownLHS.getBitWidth();
1187 KnownBits KnownOut(BitWidth);
1188 bool IsAnd = false;
1189 bool HasKnownOne = !KnownLHS.One.isZero() || !KnownRHS.One.isZero();
1190 Value *X = nullptr, *Y = nullptr;
1191
1192 switch (I->getOpcode()) {
1193 case Instruction::And:
1194 KnownOut = KnownLHS & KnownRHS;
1195 IsAnd = true;
1196 // and(x, -x) is common idioms that will clear all but lowest set
1197 // bit. If we have a single known bit in x, we can clear all bits
1198 // above it.
1199 // TODO: instcombine often reassociates independent `and` which can hide
1200 // this pattern. Try to match and(x, and(-x, y)) / and(and(x, y), -x).
1201 if (HasKnownOne && match(I, m_c_And(m_Value(X), m_Neg(m_Deferred(X))))) {
1202 // -(-x) == x so using whichever (LHS/RHS) gets us a better result.
1203 if (KnownLHS.countMaxTrailingZeros() <= KnownRHS.countMaxTrailingZeros())
1204 KnownOut = KnownLHS.blsi();
1205 else
1206 KnownOut = KnownRHS.blsi();
1207 }
1208 break;
1209 case Instruction::Or:
1210 KnownOut = KnownLHS | KnownRHS;
1211 break;
1212 case Instruction::Xor:
1213 KnownOut = KnownLHS ^ KnownRHS;
1214 // xor(x, x-1) is common idioms that will clear all but lowest set
1215 // bit. If we have a single known bit in x, we can clear all bits
1216 // above it.
1217 // TODO: xor(x, x-1) is often rewritting as xor(x, x-C) where C !=
1218 // -1 but for the purpose of demanded bits (xor(x, x-C) &
1219 // Demanded) == (xor(x, x-1) & Demanded). Extend the xor pattern
1220 // to use arbitrary C if xor(x, x-C) as the same as xor(x, x-1).
1221 if (HasKnownOne &&
1223 const KnownBits &XBits = I->getOperand(0) == X ? KnownLHS : KnownRHS;
1224 KnownOut = XBits.blsmsk();
1225 }
1226 break;
1227 default:
1228 llvm_unreachable("Invalid Op used in 'analyzeKnownBitsFromAndXorOr'");
1229 }
1230
1231 // and(x, add (x, -1)) is a common idiom that always clears the low bit;
1232 // xor/or(x, add (x, -1)) is an idiom that will always set the low bit.
1233 // here we handle the more general case of adding any odd number by
1234 // matching the form and/xor/or(x, add(x, y)) where y is odd.
1235 // TODO: This could be generalized to clearing any bit set in y where the
1236 // following bit is known to be unset in y.
1237 if (!KnownOut.Zero[0] && !KnownOut.One[0] &&
1241 KnownBits KnownY(BitWidth);
1242 computeKnownBits(Y, DemandedElts, KnownY, Q, Depth + 1);
1243 if (KnownY.countMinTrailingOnes() > 0) {
1244 if (IsAnd)
1245 KnownOut.Zero.setBit(0);
1246 else
1247 KnownOut.One.setBit(0);
1248 }
1249 }
1250 return KnownOut;
1251}
1252
1254 const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q,
1255 unsigned Depth,
1256 const function_ref<KnownBits(const KnownBits &, const KnownBits &)>
1257 KnownBitsFunc) {
1258 APInt DemandedEltsLHS, DemandedEltsRHS;
1260 DemandedElts, DemandedEltsLHS,
1261 DemandedEltsRHS);
1262
1263 const auto ComputeForSingleOpFunc =
1264 [Depth, &Q, KnownBitsFunc](const Value *Op, APInt &DemandedEltsOp) {
1265 return KnownBitsFunc(
1266 computeKnownBits(Op, DemandedEltsOp, Q, Depth + 1),
1267 computeKnownBits(Op, DemandedEltsOp << 1, Q, Depth + 1));
1268 };
1269
1270 if (DemandedEltsRHS.isZero())
1271 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS);
1272 if (DemandedEltsLHS.isZero())
1273 return ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS);
1274
1275 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS)
1276 .intersectWith(ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS));
1277}
1278
1279// Public so this can be used in `SimplifyDemandedUseBits`.
1281 const KnownBits &KnownLHS,
1282 const KnownBits &KnownRHS,
1283 const SimplifyQuery &SQ,
1284 unsigned Depth) {
1285 auto *FVTy = dyn_cast<FixedVectorType>(I->getType());
1286 APInt DemandedElts =
1287 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
1288
1289 return getKnownBitsFromAndXorOr(I, DemandedElts, KnownLHS, KnownRHS, SQ,
1290 Depth);
1291}
1292
1294 Attribute Attr = F->getFnAttribute(Attribute::VScaleRange);
1295 // Without vscale_range, we only know that vscale is non-zero.
1296 if (!Attr.isValid())
1298
1299 unsigned AttrMin = Attr.getVScaleRangeMin();
1300 // Minimum is larger than vscale width, result is always poison.
1301 if ((unsigned)llvm::bit_width(AttrMin) > BitWidth)
1302 return ConstantRange::getEmpty(BitWidth);
1303
1304 APInt Min(BitWidth, AttrMin);
1305 std::optional<unsigned> AttrMax = Attr.getVScaleRangeMax();
1306 if (!AttrMax || (unsigned)llvm::bit_width(*AttrMax) > BitWidth)
1308
1309 return ConstantRange(Min, APInt(BitWidth, *AttrMax) + 1);
1310}
1311
1313 Value *Arm, bool Invert,
1314 const SimplifyQuery &Q, unsigned Depth) {
1315 // If we have a constant arm, we are done.
1316 if (Known.isConstant())
1317 return;
1318
1319 // See what condition implies about the bits of the select arm.
1320 KnownBits CondRes(Known.getBitWidth());
1321 computeKnownBitsFromCond(Arm, Cond, CondRes, Q, Invert, Depth + 1);
1322 // If we don't get any information from the condition, no reason to
1323 // proceed.
1324 if (CondRes.isUnknown())
1325 return;
1326
1327 // We can have conflict if the condition is dead. I.e if we have
1328 // (x | 64) < 32 ? (x | 64) : y
1329 // we will have conflict at bit 6 from the condition/the `or`.
1330 // In that case just return. Its not particularly important
1331 // what we do, as this select is going to be simplified soon.
1332 CondRes = CondRes.unionWith(Known);
1333 if (CondRes.hasConflict())
1334 return;
1335
1336 // Finally make sure the information we found is valid. This is relatively
1337 // expensive so it's left for the very end.
1338 if (!isGuaranteedNotToBeUndef(Arm, Q.AC, Q.CxtI, Q.DT, Depth + 1))
1339 return;
1340
1341 // Finally, we know we get information from the condition and its valid,
1342 // so return it.
1343 Known = std::move(CondRes);
1344}
1345
1346// Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow).
1347// Returns the input and lower/upper bounds.
1348static bool isSignedMinMaxClamp(const Value *Select, const Value *&In,
1349 const APInt *&CLow, const APInt *&CHigh) {
1351 cast<Operator>(Select)->getOpcode() == Instruction::Select &&
1352 "Input should be a Select!");
1353
1354 const Value *LHS = nullptr, *RHS = nullptr;
1356 if (SPF != SPF_SMAX && SPF != SPF_SMIN)
1357 return false;
1358
1359 if (!match(RHS, m_APInt(CLow)))
1360 return false;
1361
1362 const Value *LHS2 = nullptr, *RHS2 = nullptr;
1364 if (getInverseMinMaxFlavor(SPF) != SPF2)
1365 return false;
1366
1367 if (!match(RHS2, m_APInt(CHigh)))
1368 return false;
1369
1370 if (SPF == SPF_SMIN)
1371 std::swap(CLow, CHigh);
1372
1373 In = LHS2;
1374 return CLow->sle(*CHigh);
1375}
1376
1378 const APInt *&CLow,
1379 const APInt *&CHigh) {
1380 assert((II->getIntrinsicID() == Intrinsic::smin ||
1381 II->getIntrinsicID() == Intrinsic::smax) &&
1382 "Must be smin/smax");
1383
1384 Intrinsic::ID InverseID = getInverseMinMaxIntrinsic(II->getIntrinsicID());
1385 auto *InnerII = dyn_cast<IntrinsicInst>(II->getArgOperand(0));
1386 if (!InnerII || InnerII->getIntrinsicID() != InverseID ||
1387 !match(II->getArgOperand(1), m_APInt(CLow)) ||
1388 !match(InnerII->getArgOperand(1), m_APInt(CHigh)))
1389 return false;
1390
1391 if (II->getIntrinsicID() == Intrinsic::smin)
1392 std::swap(CLow, CHigh);
1393 return CLow->sle(*CHigh);
1394}
1395
1397 KnownBits &Known) {
1398 const APInt *CLow, *CHigh;
1399 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
1400 Known = Known.unionWith(
1401 ConstantRange::getNonEmpty(*CLow, *CHigh + 1).toKnownBits());
1402}
1403
1405 const APInt &DemandedElts,
1406 KnownBits &Known,
1407 const SimplifyQuery &Q,
1408 unsigned Depth) {
1409 unsigned BitWidth = Known.getBitWidth();
1410
1411 KnownBits Known2(BitWidth);
1412 switch (I->getOpcode()) {
1413 default: break;
1414 case Instruction::Load:
1415 if (MDNode *MD =
1416 Q.IIQ.getMetadata(cast<LoadInst>(I), LLVMContext::MD_range))
1418 break;
1419 case Instruction::And:
1420 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1421 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1422
1423 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1424 break;
1425 case Instruction::Or:
1426 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1427 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1428
1429 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1430 break;
1431 case Instruction::Xor:
1432 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1433 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1434
1435 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1436 break;
1437 case Instruction::Mul: {
1440 computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, NUW,
1441 DemandedElts, Known, Known2, Q, Depth);
1442 break;
1443 }
1444 case Instruction::UDiv: {
1445 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1446 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1447 Known =
1448 KnownBits::udiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1449 break;
1450 }
1451 case Instruction::SDiv: {
1452 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1453 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1454 Known =
1455 KnownBits::sdiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1456 break;
1457 }
1458 case Instruction::Select: {
1459 auto ComputeForArm = [&](Value *Arm, bool Invert) {
1460 KnownBits Res(Known.getBitWidth());
1461 computeKnownBits(Arm, DemandedElts, Res, Q, Depth + 1);
1462 adjustKnownBitsForSelectArm(Res, I->getOperand(0), Arm, Invert, Q, Depth);
1463 return Res;
1464 };
1465 // Only known if known in both the LHS and RHS.
1466 Known =
1467 ComputeForArm(I->getOperand(1), /*Invert=*/false)
1468 .intersectWith(ComputeForArm(I->getOperand(2), /*Invert=*/true));
1469 break;
1470 }
1471 case Instruction::FPTrunc:
1472 case Instruction::FPExt:
1473 case Instruction::FPToUI:
1474 case Instruction::FPToSI:
1475 case Instruction::SIToFP:
1476 case Instruction::UIToFP:
1477 break; // Can't work with floating point.
1478 case Instruction::PtrToInt:
1479 case Instruction::PtrToAddr:
1480 case Instruction::IntToPtr:
1481 // Fall through and handle them the same as zext/trunc.
1482 [[fallthrough]];
1483 case Instruction::ZExt:
1484 case Instruction::Trunc: {
1485 Type *SrcTy = I->getOperand(0)->getType();
1486
1487 unsigned SrcBitWidth;
1488 // Note that we handle pointer operands here because of inttoptr/ptrtoint
1489 // which fall through here.
1490 Type *ScalarTy = SrcTy->getScalarType();
1491 SrcBitWidth = ScalarTy->isPointerTy() ?
1492 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
1493 Q.DL.getTypeSizeInBits(ScalarTy);
1494
1495 assert(SrcBitWidth && "SrcBitWidth can't be zero");
1496 Known = Known.anyextOrTrunc(SrcBitWidth);
1497 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1498 if (auto *Inst = dyn_cast<PossiblyNonNegInst>(I);
1499 Inst && Inst->hasNonNeg() && !Known.isNegative())
1500 Known.makeNonNegative();
1501 Known = Known.zextOrTrunc(BitWidth);
1502 break;
1503 }
1504 case Instruction::BitCast: {
1505 Type *SrcTy = I->getOperand(0)->getType();
1506 if (SrcTy->isIntOrPtrTy() &&
1507 // TODO: For now, not handling conversions like:
1508 // (bitcast i64 %x to <2 x i32>)
1509 !I->getType()->isVectorTy()) {
1510 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1511 break;
1512 }
1513
1514 const Value *V;
1515 // Handle bitcast from floating point to integer.
1516 if (match(I, m_ElementWiseBitCast(m_Value(V))) &&
1517 V->getType()->isFPOrFPVectorTy()) {
1518 Type *FPType = V->getType()->getScalarType();
1519 KnownFPClass Result =
1520 computeKnownFPClass(V, DemandedElts, fcAllFlags, Q, Depth + 1);
1521 FPClassTest FPClasses = Result.KnownFPClasses;
1522
1523 // TODO: Treat it as zero/poison if the use of I is unreachable.
1524 if (FPClasses == fcNone)
1525 break;
1526
1527 if (Result.isKnownNever(fcNormal | fcSubnormal | fcNan)) {
1528 Known.setAllConflict();
1529
1530 if (FPClasses & fcInf)
1532 APFloat::getInf(FPType->getFltSemantics()).bitcastToAPInt()));
1533
1534 if (FPClasses & fcZero)
1536 APInt::getZero(FPType->getScalarSizeInBits())));
1537
1538 Known.Zero.clearSignBit();
1539 Known.One.clearSignBit();
1540 }
1541
1542 if (Result.SignBit) {
1543 if (*Result.SignBit)
1544 Known.makeNegative();
1545 else
1546 Known.makeNonNegative();
1547 }
1548
1549 break;
1550 }
1551
1552 // Handle cast from vector integer type to scalar or vector integer.
1553 auto *SrcVecTy = dyn_cast<FixedVectorType>(SrcTy);
1554 if (!SrcVecTy || !SrcVecTy->getElementType()->isIntegerTy() ||
1555 !I->getType()->isIntOrIntVectorTy() ||
1556 isa<ScalableVectorType>(I->getType()))
1557 break;
1558
1559 unsigned NumElts = DemandedElts.getBitWidth();
1560 bool IsLE = Q.DL.isLittleEndian();
1561 // Look through a cast from narrow vector elements to wider type.
1562 // Examples: v4i32 -> v2i64, v3i8 -> v24
1563 unsigned SubBitWidth = SrcVecTy->getScalarSizeInBits();
1564 if (BitWidth % SubBitWidth == 0) {
1565 // Known bits are automatically intersected across demanded elements of a
1566 // vector. So for example, if a bit is computed as known zero, it must be
1567 // zero across all demanded elements of the vector.
1568 //
1569 // For this bitcast, each demanded element of the output is sub-divided
1570 // across a set of smaller vector elements in the source vector. To get
1571 // the known bits for an entire element of the output, compute the known
1572 // bits for each sub-element sequentially. This is done by shifting the
1573 // one-set-bit demanded elements parameter across the sub-elements for
1574 // consecutive calls to computeKnownBits. We are using the demanded
1575 // elements parameter as a mask operator.
1576 //
1577 // The known bits of each sub-element are then inserted into place
1578 // (dependent on endian) to form the full result of known bits.
1579 unsigned SubScale = BitWidth / SubBitWidth;
1580 APInt SubDemandedElts = APInt::getZero(NumElts * SubScale);
1581 for (unsigned i = 0; i != NumElts; ++i) {
1582 if (DemandedElts[i])
1583 SubDemandedElts.setBit(i * SubScale);
1584 }
1585
1586 KnownBits KnownSrc(SubBitWidth);
1587 for (unsigned i = 0; i != SubScale; ++i) {
1588 computeKnownBits(I->getOperand(0), SubDemandedElts.shl(i), KnownSrc, Q,
1589 Depth + 1);
1590 unsigned ShiftElt = IsLE ? i : SubScale - 1 - i;
1591 Known.insertBits(KnownSrc, ShiftElt * SubBitWidth);
1592 }
1593 }
1594 // Look through a cast from wider vector elements to narrow type.
1595 // Examples: v2i64 -> v4i32
1596 if (SubBitWidth % BitWidth == 0) {
1597 unsigned SubScale = SubBitWidth / BitWidth;
1598 KnownBits KnownSrc(SubBitWidth);
1599 APInt SubDemandedElts =
1600 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
1601 computeKnownBits(I->getOperand(0), SubDemandedElts, KnownSrc, Q,
1602 Depth + 1);
1603
1604 Known.setAllConflict();
1605 for (unsigned i = 0; i != NumElts; ++i) {
1606 if (DemandedElts[i]) {
1607 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
1608 unsigned Offset = (Shifts % SubScale) * BitWidth;
1609 Known = Known.intersectWith(KnownSrc.extractBits(BitWidth, Offset));
1610 if (Known.isUnknown())
1611 break;
1612 }
1613 }
1614 }
1615 break;
1616 }
1617 case Instruction::SExt: {
1618 // Compute the bits in the result that are not present in the input.
1619 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
1620
1621 Known = Known.trunc(SrcBitWidth);
1622 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1623 // If the sign bit of the input is known set or clear, then we know the
1624 // top bits of the result.
1625 Known = Known.sext(BitWidth);
1626 break;
1627 }
1628 case Instruction::Shl: {
1631 auto KF = [NUW, NSW](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1632 bool ShAmtNonZero) {
1633 return KnownBits::shl(KnownVal, KnownAmt, NUW, NSW, ShAmtNonZero);
1634 };
1635 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1636 KF);
1637 // Trailing zeros of a right-shifted constant never decrease.
1638 const APInt *C;
1639 if (match(I->getOperand(0), m_APInt(C)))
1640 Known.Zero.setLowBits(C->countr_zero());
1641 break;
1642 }
1643 case Instruction::LShr: {
1644 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1645 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1646 bool ShAmtNonZero) {
1647 return KnownBits::lshr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1648 };
1649 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1650 KF);
1651 // Leading zeros of a left-shifted constant never decrease.
1652 const APInt *C;
1653 if (match(I->getOperand(0), m_APInt(C)))
1654 Known.Zero.setHighBits(C->countl_zero());
1655 break;
1656 }
1657 case Instruction::AShr: {
1658 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1659 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1660 bool ShAmtNonZero) {
1661 return KnownBits::ashr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1662 };
1663 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1664 KF);
1665 break;
1666 }
1667 case Instruction::Sub: {
1670 computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW, NUW,
1671 DemandedElts, Known, Known2, Q, Depth);
1672 break;
1673 }
1674 case Instruction::Add: {
1677 computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW, NUW,
1678 DemandedElts, Known, Known2, Q, Depth);
1679 break;
1680 }
1681 case Instruction::SRem:
1682 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1683 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1684 Known = KnownBits::srem(Known, Known2);
1685 break;
1686
1687 case Instruction::URem:
1688 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1689 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1690 Known = KnownBits::urem(Known, Known2);
1691 break;
1692 case Instruction::Alloca:
1694 break;
1695 case Instruction::GetElementPtr: {
1696 // Analyze all of the subscripts of this getelementptr instruction
1697 // to determine if we can prove known low zero bits.
1698 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1699 // Accumulate the constant indices in a separate variable
1700 // to minimize the number of calls to computeForAddSub.
1701 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(I->getType());
1702 APInt AccConstIndices(IndexWidth, 0);
1703
1704 auto AddIndexToKnown = [&](KnownBits IndexBits) {
1705 if (IndexWidth == BitWidth) {
1706 // Note that inbounds does *not* guarantee nsw for the addition, as only
1707 // the offset is signed, while the base address is unsigned.
1708 Known = KnownBits::add(Known, IndexBits);
1709 } else {
1710 // If the index width is smaller than the pointer width, only add the
1711 // value to the low bits.
1712 assert(IndexWidth < BitWidth &&
1713 "Index width can't be larger than pointer width");
1714 Known.insertBits(KnownBits::add(Known.trunc(IndexWidth), IndexBits), 0);
1715 }
1716 };
1717
1719 for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
1720 // TrailZ can only become smaller, short-circuit if we hit zero.
1721 if (Known.isUnknown())
1722 break;
1723
1724 Value *Index = I->getOperand(i);
1725
1726 // Handle case when index is zero.
1727 Constant *CIndex = dyn_cast<Constant>(Index);
1728 if (CIndex && CIndex->isNullValue())
1729 continue;
1730
1731 if (StructType *STy = GTI.getStructTypeOrNull()) {
1732 // Handle struct member offset arithmetic.
1733
1734 assert(CIndex &&
1735 "Access to structure field must be known at compile time");
1736
1737 if (CIndex->getType()->isVectorTy())
1738 Index = CIndex->getSplatValue();
1739
1740 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
1741 const StructLayout *SL = Q.DL.getStructLayout(STy);
1742 uint64_t Offset = SL->getElementOffset(Idx);
1743 AccConstIndices += Offset;
1744 continue;
1745 }
1746
1747 // Handle array index arithmetic.
1748 Type *IndexedTy = GTI.getIndexedType();
1749 if (!IndexedTy->isSized()) {
1750 Known.resetAll();
1751 break;
1752 }
1753
1754 TypeSize Stride = GTI.getSequentialElementStride(Q.DL);
1755 uint64_t StrideInBytes = Stride.getKnownMinValue();
1756 if (!Stride.isScalable()) {
1757 // Fast path for constant offset.
1758 if (auto *CI = dyn_cast<ConstantInt>(Index)) {
1759 AccConstIndices +=
1760 CI->getValue().sextOrTrunc(IndexWidth) * StrideInBytes;
1761 continue;
1762 }
1763 }
1764
1765 KnownBits IndexBits =
1766 computeKnownBits(Index, Q, Depth + 1).sextOrTrunc(IndexWidth);
1767 KnownBits ScalingFactor(IndexWidth);
1768 // Multiply by current sizeof type.
1769 // &A[i] == A + i * sizeof(*A[i]).
1770 if (Stride.isScalable()) {
1771 // For scalable types the only thing we know about sizeof is
1772 // that this is a multiple of the minimum size.
1773 ScalingFactor.Zero.setLowBits(llvm::countr_zero(StrideInBytes));
1774 } else {
1775 ScalingFactor =
1776 KnownBits::makeConstant(APInt(IndexWidth, StrideInBytes));
1777 }
1778 AddIndexToKnown(KnownBits::mul(IndexBits, ScalingFactor));
1779 }
1780 if (!Known.isUnknown() && !AccConstIndices.isZero())
1781 AddIndexToKnown(KnownBits::makeConstant(AccConstIndices));
1782 break;
1783 }
1784 case Instruction::PHI: {
1785 const PHINode *P = cast<PHINode>(I);
1786 BinaryOperator *BO = nullptr;
1787 Value *R = nullptr, *L = nullptr;
1788 if (matchSimpleRecurrence(P, BO, R, L)) {
1789 // Handle the case of a simple two-predecessor recurrence PHI.
1790 // There's a lot more that could theoretically be done here, but
1791 // this is sufficient to catch some interesting cases.
1792 unsigned Opcode = BO->getOpcode();
1793
1794 switch (Opcode) {
1795 // If this is a shift recurrence, we know the bits being shifted in. We
1796 // can combine that with information about the start value of the
1797 // recurrence to conclude facts about the result. If this is a udiv
1798 // recurrence, we know that the result can never exceed either the
1799 // numerator or the start value, whichever is greater.
1800 case Instruction::LShr:
1801 case Instruction::AShr:
1802 case Instruction::Shl:
1803 case Instruction::UDiv:
1804 if (BO->getOperand(0) != I)
1805 break;
1806 [[fallthrough]];
1807
1808 // For a urem recurrence, the result can never exceed the start value. The
1809 // phi could either be the numerator or the denominator.
1810 case Instruction::URem: {
1811 // We have matched a recurrence of the form:
1812 // %iv = [R, %entry], [%iv.next, %backedge]
1813 // %iv.next = shift_op %iv, L
1814
1815 // Recurse with the phi context to avoid concern about whether facts
1816 // inferred hold at original context instruction. TODO: It may be
1817 // correct to use the original context. IF warranted, explore and
1818 // add sufficient tests to cover.
1820 RecQ.CxtI = P;
1821 computeKnownBits(R, DemandedElts, Known2, RecQ, Depth + 1);
1822 switch (Opcode) {
1823 case Instruction::Shl:
1824 // A shl recurrence will only increase the tailing zeros
1825 Known.Zero.setLowBits(Known2.countMinTrailingZeros());
1826 break;
1827 case Instruction::LShr:
1828 case Instruction::UDiv:
1829 case Instruction::URem:
1830 // lshr, udiv, and urem recurrences will preserve the leading zeros of
1831 // the start value.
1832 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1833 break;
1834 case Instruction::AShr:
1835 // An ashr recurrence will extend the initial sign bit
1836 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1837 Known.One.setHighBits(Known2.countMinLeadingOnes());
1838 break;
1839 }
1840 break;
1841 }
1842
1843 // Check for operations that have the property that if
1844 // both their operands have low zero bits, the result
1845 // will have low zero bits.
1846 case Instruction::Add:
1847 case Instruction::Sub:
1848 case Instruction::And:
1849 case Instruction::Or:
1850 case Instruction::Mul: {
1851 // Change the context instruction to the "edge" that flows into the
1852 // phi. This is important because that is where the value is actually
1853 // "evaluated" even though it is used later somewhere else. (see also
1854 // D69571).
1856
1857 unsigned OpNum = P->getOperand(0) == R ? 0 : 1;
1858 Instruction *RInst = P->getIncomingBlock(OpNum)->getTerminator();
1859 Instruction *LInst = P->getIncomingBlock(1 - OpNum)->getTerminator();
1860
1861 // Ok, we have a PHI of the form L op= R. Check for low
1862 // zero bits.
1863 RecQ.CxtI = RInst;
1864 computeKnownBits(R, DemandedElts, Known2, RecQ, Depth + 1);
1865
1866 // We need to take the minimum number of known bits
1867 KnownBits Known3(BitWidth);
1868 RecQ.CxtI = LInst;
1869 computeKnownBits(L, DemandedElts, Known3, RecQ, Depth + 1);
1870
1871 Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(),
1872 Known3.countMinTrailingZeros()));
1873
1874 auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(BO);
1875 if (!OverflowOp || !Q.IIQ.hasNoSignedWrap(OverflowOp))
1876 break;
1877
1878 switch (Opcode) {
1879 // If initial value of recurrence is nonnegative, and we are adding
1880 // a nonnegative number with nsw, the result can only be nonnegative
1881 // or poison value regardless of the number of times we execute the
1882 // add in phi recurrence. If initial value is negative and we are
1883 // adding a negative number with nsw, the result can only be
1884 // negative or poison value. Similar arguments apply to sub and mul.
1885 //
1886 // (add non-negative, non-negative) --> non-negative
1887 // (add negative, negative) --> negative
1888 case Instruction::Add: {
1889 if (Known2.isNonNegative() && Known3.isNonNegative())
1890 Known.makeNonNegative();
1891 else if (Known2.isNegative() && Known3.isNegative())
1892 Known.makeNegative();
1893 break;
1894 }
1895
1896 // (sub nsw non-negative, negative) --> non-negative
1897 // (sub nsw negative, non-negative) --> negative
1898 case Instruction::Sub: {
1899 if (BO->getOperand(0) != I)
1900 break;
1901 if (Known2.isNonNegative() && Known3.isNegative())
1902 Known.makeNonNegative();
1903 else if (Known2.isNegative() && Known3.isNonNegative())
1904 Known.makeNegative();
1905 break;
1906 }
1907
1908 // (mul nsw non-negative, non-negative) --> non-negative
1909 case Instruction::Mul:
1910 if (Known2.isNonNegative() && Known3.isNonNegative())
1911 Known.makeNonNegative();
1912 break;
1913
1914 default:
1915 break;
1916 }
1917 break;
1918 }
1919
1920 default:
1921 break;
1922 }
1923 }
1924
1925 // Unreachable blocks may have zero-operand PHI nodes.
1926 if (P->getNumIncomingValues() == 0)
1927 break;
1928
1929 // Otherwise take the unions of the known bit sets of the operands,
1930 // taking conservative care to avoid excessive recursion.
1931 if (Depth < MaxAnalysisRecursionDepth - 1 && Known.isUnknown()) {
1932 // Skip if every incoming value references to ourself.
1933 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
1934 break;
1935
1936 Known.setAllConflict();
1937 for (const Use &U : P->operands()) {
1938 Value *IncValue;
1939 const PHINode *CxtPhi;
1940 Instruction *CxtI;
1941 breakSelfRecursivePHI(&U, P, IncValue, CxtI, &CxtPhi);
1942 // Skip direct self references.
1943 if (IncValue == P)
1944 continue;
1945
1946 // Change the context instruction to the "edge" that flows into the
1947 // phi. This is important because that is where the value is actually
1948 // "evaluated" even though it is used later somewhere else. (see also
1949 // D69571).
1951
1952 Known2 = KnownBits(BitWidth);
1953
1954 // Recurse, but cap the recursion to one level, because we don't
1955 // want to waste time spinning around in loops.
1956 // TODO: See if we can base recursion limiter on number of incoming phi
1957 // edges so we don't overly clamp analysis.
1958 computeKnownBits(IncValue, DemandedElts, Known2, RecQ,
1960
1961 // See if we can further use a conditional branch into the phi
1962 // to help us determine the range of the value.
1963 if (!Known2.isConstant()) {
1964 CmpPredicate Pred;
1965 const APInt *RHSC;
1966 BasicBlock *TrueSucc, *FalseSucc;
1967 // TODO: Use RHS Value and compute range from its known bits.
1968 if (match(RecQ.CxtI,
1969 m_Br(m_c_ICmp(Pred, m_Specific(IncValue), m_APInt(RHSC)),
1970 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
1971 // Check for cases of duplicate successors.
1972 if ((TrueSucc == CxtPhi->getParent()) !=
1973 (FalseSucc == CxtPhi->getParent())) {
1974 // If we're using the false successor, invert the predicate.
1975 if (FalseSucc == CxtPhi->getParent())
1976 Pred = CmpInst::getInversePredicate(Pred);
1977 // Get the knownbits implied by the incoming phi condition.
1978 auto CR = ConstantRange::makeExactICmpRegion(Pred, *RHSC);
1979 KnownBits KnownUnion = Known2.unionWith(CR.toKnownBits());
1980 // We can have conflicts here if we are analyzing deadcode (its
1981 // impossible for us reach this BB based the icmp).
1982 if (KnownUnion.hasConflict()) {
1983 // No reason to continue analyzing in a known dead region, so
1984 // just resetAll and break. This will cause us to also exit the
1985 // outer loop.
1986 Known.resetAll();
1987 break;
1988 }
1989 Known2 = KnownUnion;
1990 }
1991 }
1992 }
1993
1994 Known = Known.intersectWith(Known2);
1995 // If all bits have been ruled out, there's no need to check
1996 // more operands.
1997 if (Known.isUnknown())
1998 break;
1999 }
2000 }
2001 break;
2002 }
2003 case Instruction::Call:
2004 case Instruction::Invoke: {
2005 // If range metadata is attached to this call, set known bits from that,
2006 // and then intersect with known bits based on other properties of the
2007 // function.
2008 if (MDNode *MD =
2009 Q.IIQ.getMetadata(cast<Instruction>(I), LLVMContext::MD_range))
2011
2012 const auto *CB = cast<CallBase>(I);
2013
2014 if (std::optional<ConstantRange> Range = CB->getRange())
2015 Known = Known.unionWith(Range->toKnownBits());
2016
2017 if (const Value *RV = CB->getReturnedArgOperand()) {
2018 if (RV->getType() == I->getType()) {
2019 computeKnownBits(RV, Known2, Q, Depth + 1);
2020 Known = Known.unionWith(Known2);
2021 // If the function doesn't return properly for all input values
2022 // (e.g. unreachable exits) then there might be conflicts between the
2023 // argument value and the range metadata. Simply discard the known bits
2024 // in case of conflicts.
2025 if (Known.hasConflict())
2026 Known.resetAll();
2027 }
2028 }
2029 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
2030 switch (II->getIntrinsicID()) {
2031 default:
2032 break;
2033 case Intrinsic::abs: {
2034 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2035 bool IntMinIsPoison = match(II->getArgOperand(1), m_One());
2036 Known = Known.unionWith(Known2.abs(IntMinIsPoison));
2037 break;
2038 }
2039 case Intrinsic::bitreverse:
2040 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2041 Known = Known.unionWith(Known2.reverseBits());
2042 break;
2043 case Intrinsic::bswap:
2044 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2045 Known = Known.unionWith(Known2.byteSwap());
2046 break;
2047 case Intrinsic::ctlz: {
2048 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2049 // If we have a known 1, its position is our upper bound.
2050 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
2051 // If this call is poison for 0 input, the result will be less than 2^n.
2052 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
2053 PossibleLZ = std::min(PossibleLZ, BitWidth - 1);
2054 unsigned LowBits = llvm::bit_width(PossibleLZ);
2055 Known.Zero.setBitsFrom(LowBits);
2056 break;
2057 }
2058 case Intrinsic::cttz: {
2059 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2060 // If we have a known 1, its position is our upper bound.
2061 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
2062 // If this call is poison for 0 input, the result will be less than 2^n.
2063 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
2064 PossibleTZ = std::min(PossibleTZ, BitWidth - 1);
2065 unsigned LowBits = llvm::bit_width(PossibleTZ);
2066 Known.Zero.setBitsFrom(LowBits);
2067 break;
2068 }
2069 case Intrinsic::ctpop: {
2070 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2071 // We can bound the space the count needs. Also, bits known to be zero
2072 // can't contribute to the population.
2073 unsigned BitsPossiblySet = Known2.countMaxPopulation();
2074 unsigned LowBits = llvm::bit_width(BitsPossiblySet);
2075 Known.Zero.setBitsFrom(LowBits);
2076 // TODO: we could bound KnownOne using the lower bound on the number
2077 // of bits which might be set provided by popcnt KnownOne2.
2078 break;
2079 }
2080 case Intrinsic::fshr:
2081 case Intrinsic::fshl: {
2082 const APInt *SA;
2083 if (!match(I->getOperand(2), m_APInt(SA)))
2084 break;
2085
2086 // Normalize to funnel shift left.
2087 uint64_t ShiftAmt = SA->urem(BitWidth);
2088 if (II->getIntrinsicID() == Intrinsic::fshr)
2089 ShiftAmt = BitWidth - ShiftAmt;
2090
2091 KnownBits Known3(BitWidth);
2092 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2093 computeKnownBits(I->getOperand(1), DemandedElts, Known3, Q, Depth + 1);
2094
2095 Known2 <<= ShiftAmt;
2096 Known3 >>= BitWidth - ShiftAmt;
2097 Known = Known2.unionWith(Known3);
2098 break;
2099 }
2100 case Intrinsic::clmul:
2101 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2102 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2103 Known = KnownBits::clmul(Known, Known2);
2104 break;
2105 case Intrinsic::uadd_sat:
2106 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2107 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2108 Known = KnownBits::uadd_sat(Known, Known2);
2109 break;
2110 case Intrinsic::usub_sat:
2111 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2112 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2113 Known = KnownBits::usub_sat(Known, Known2);
2114 break;
2115 case Intrinsic::sadd_sat:
2116 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2117 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2118 Known = KnownBits::sadd_sat(Known, Known2);
2119 break;
2120 case Intrinsic::ssub_sat:
2121 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2122 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2123 Known = KnownBits::ssub_sat(Known, Known2);
2124 break;
2125 // Vec reverse preserves bits from input vec.
2126 case Intrinsic::vector_reverse:
2127 computeKnownBits(I->getOperand(0), DemandedElts.reverseBits(), Known, Q,
2128 Depth + 1);
2129 break;
2130 // for min/max/and/or reduce, any bit common to each element in the
2131 // input vec is set in the output.
2132 case Intrinsic::vector_reduce_and:
2133 case Intrinsic::vector_reduce_or:
2134 case Intrinsic::vector_reduce_umax:
2135 case Intrinsic::vector_reduce_umin:
2136 case Intrinsic::vector_reduce_smax:
2137 case Intrinsic::vector_reduce_smin:
2138 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2139 break;
2140 case Intrinsic::vector_reduce_xor: {
2141 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2142 // The zeros common to all vecs are zero in the output.
2143 // If the number of elements is odd, then the common ones remain. If the
2144 // number of elements is even, then the common ones becomes zeros.
2145 auto *VecTy = cast<VectorType>(I->getOperand(0)->getType());
2146 // Even, so the ones become zeros.
2147 bool EvenCnt = VecTy->getElementCount().isKnownEven();
2148 if (EvenCnt)
2149 Known.Zero |= Known.One;
2150 // Maybe even element count so need to clear ones.
2151 if (VecTy->isScalableTy() || EvenCnt)
2152 Known.One.clearAllBits();
2153 break;
2154 }
2155 case Intrinsic::vector_reduce_add: {
2156 auto *VecTy = dyn_cast<FixedVectorType>(I->getOperand(0)->getType());
2157 if (!VecTy)
2158 break;
2159 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2160 Known = Known.reduceAdd(VecTy->getNumElements());
2161 break;
2162 }
2163 case Intrinsic::umin:
2164 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2165 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2166 Known = KnownBits::umin(Known, Known2);
2167 break;
2168 case Intrinsic::umax:
2169 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2170 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2171 Known = KnownBits::umax(Known, Known2);
2172 break;
2173 case Intrinsic::smin:
2174 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2175 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2176 Known = KnownBits::smin(Known, Known2);
2178 break;
2179 case Intrinsic::smax:
2180 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2181 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2182 Known = KnownBits::smax(Known, Known2);
2184 break;
2185 case Intrinsic::ptrmask: {
2186 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2187
2188 const Value *Mask = I->getOperand(1);
2189 Known2 = KnownBits(Mask->getType()->getScalarSizeInBits());
2190 computeKnownBits(Mask, DemandedElts, Known2, Q, Depth + 1);
2191 // TODO: 1-extend would be more precise.
2192 Known &= Known2.anyextOrTrunc(BitWidth);
2193 break;
2194 }
2195 case Intrinsic::x86_sse2_pmulh_w:
2196 case Intrinsic::x86_avx2_pmulh_w:
2197 case Intrinsic::x86_avx512_pmulh_w_512:
2198 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2199 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2200 Known = KnownBits::mulhs(Known, Known2);
2201 break;
2202 case Intrinsic::x86_sse2_pmulhu_w:
2203 case Intrinsic::x86_avx2_pmulhu_w:
2204 case Intrinsic::x86_avx512_pmulhu_w_512:
2205 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2206 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2207 Known = KnownBits::mulhu(Known, Known2);
2208 break;
2209 case Intrinsic::x86_sse42_crc32_64_64:
2210 Known.Zero.setBitsFrom(32);
2211 break;
2212 case Intrinsic::x86_ssse3_phadd_d_128:
2213 case Intrinsic::x86_ssse3_phadd_w_128:
2214 case Intrinsic::x86_avx2_phadd_d:
2215 case Intrinsic::x86_avx2_phadd_w: {
2217 I, DemandedElts, Q, Depth,
2218 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2219 return KnownBits::add(KnownLHS, KnownRHS);
2220 });
2221 break;
2222 }
2223 case Intrinsic::x86_ssse3_phadd_sw_128:
2224 case Intrinsic::x86_avx2_phadd_sw: {
2226 I, DemandedElts, Q, Depth, KnownBits::sadd_sat);
2227 break;
2228 }
2229 case Intrinsic::x86_ssse3_phsub_d_128:
2230 case Intrinsic::x86_ssse3_phsub_w_128:
2231 case Intrinsic::x86_avx2_phsub_d:
2232 case Intrinsic::x86_avx2_phsub_w: {
2234 I, DemandedElts, Q, Depth,
2235 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2236 return KnownBits::sub(KnownLHS, KnownRHS);
2237 });
2238 break;
2239 }
2240 case Intrinsic::x86_ssse3_phsub_sw_128:
2241 case Intrinsic::x86_avx2_phsub_sw: {
2243 I, DemandedElts, Q, Depth, KnownBits::ssub_sat);
2244 break;
2245 }
2246 case Intrinsic::riscv_vsetvli:
2247 case Intrinsic::riscv_vsetvlimax: {
2248 bool HasAVL = II->getIntrinsicID() == Intrinsic::riscv_vsetvli;
2249 const ConstantRange Range = getVScaleRange(II->getFunction(), BitWidth);
2251 cast<ConstantInt>(II->getArgOperand(HasAVL))->getZExtValue());
2252 RISCVVType::VLMUL VLMUL = static_cast<RISCVVType::VLMUL>(
2253 cast<ConstantInt>(II->getArgOperand(1 + HasAVL))->getZExtValue());
2254 uint64_t MaxVLEN =
2255 Range.getUnsignedMax().getZExtValue() * RISCV::RVVBitsPerBlock;
2256 uint64_t MaxVL = MaxVLEN / RISCVVType::getSEWLMULRatio(SEW, VLMUL);
2257
2258 // Result of vsetvli must be not larger than AVL.
2259 if (HasAVL)
2260 if (auto *CI = dyn_cast<ConstantInt>(II->getArgOperand(0)))
2261 MaxVL = std::min(MaxVL, CI->getZExtValue());
2262
2263 unsigned KnownZeroFirstBit = Log2_32(MaxVL) + 1;
2264 if (BitWidth > KnownZeroFirstBit)
2265 Known.Zero.setBitsFrom(KnownZeroFirstBit);
2266 break;
2267 }
2268 case Intrinsic::amdgcn_mbcnt_hi:
2269 case Intrinsic::amdgcn_mbcnt_lo: {
2270 // Wave64 mbcnt_lo returns at most 32 + src1. Otherwise these return at
2271 // most 31 + src1.
2272 Known.Zero.setBitsFrom(
2273 II->getIntrinsicID() == Intrinsic::amdgcn_mbcnt_lo ? 6 : 5);
2274 computeKnownBits(I->getOperand(1), Known2, Q, Depth + 1);
2275 Known = KnownBits::add(Known, Known2);
2276 break;
2277 }
2278 case Intrinsic::vscale: {
2279 if (!II->getParent() || !II->getFunction())
2280 break;
2281
2282 Known = getVScaleRange(II->getFunction(), BitWidth).toKnownBits();
2283 break;
2284 }
2285 }
2286 }
2287 break;
2288 }
2289 case Instruction::ShuffleVector: {
2290 if (auto *Splat = getSplatValue(I)) {
2291 computeKnownBits(Splat, Known, Q, Depth + 1);
2292 break;
2293 }
2294
2295 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
2296 // FIXME: Do we need to handle ConstantExpr involving shufflevectors?
2297 if (!Shuf) {
2298 Known.resetAll();
2299 return;
2300 }
2301 // For undef elements, we don't know anything about the common state of
2302 // the shuffle result.
2303 APInt DemandedLHS, DemandedRHS;
2304 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) {
2305 Known.resetAll();
2306 return;
2307 }
2308 Known.setAllConflict();
2309 if (!!DemandedLHS) {
2310 const Value *LHS = Shuf->getOperand(0);
2311 computeKnownBits(LHS, DemandedLHS, Known, Q, Depth + 1);
2312 // If we don't know any bits, early out.
2313 if (Known.isUnknown())
2314 break;
2315 }
2316 if (!!DemandedRHS) {
2317 const Value *RHS = Shuf->getOperand(1);
2318 computeKnownBits(RHS, DemandedRHS, Known2, Q, Depth + 1);
2319 Known = Known.intersectWith(Known2);
2320 }
2321 break;
2322 }
2323 case Instruction::InsertElement: {
2324 if (isa<ScalableVectorType>(I->getType())) {
2325 Known.resetAll();
2326 return;
2327 }
2328 const Value *Vec = I->getOperand(0);
2329 const Value *Elt = I->getOperand(1);
2330 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
2331 unsigned NumElts = DemandedElts.getBitWidth();
2332 APInt DemandedVecElts = DemandedElts;
2333 bool NeedsElt = true;
2334 // If we know the index we are inserting too, clear it from Vec check.
2335 if (CIdx && CIdx->getValue().ult(NumElts)) {
2336 DemandedVecElts.clearBit(CIdx->getZExtValue());
2337 NeedsElt = DemandedElts[CIdx->getZExtValue()];
2338 }
2339
2340 Known.setAllConflict();
2341 if (NeedsElt) {
2342 computeKnownBits(Elt, Known, Q, Depth + 1);
2343 // If we don't know any bits, early out.
2344 if (Known.isUnknown())
2345 break;
2346 }
2347
2348 if (!DemandedVecElts.isZero()) {
2349 computeKnownBits(Vec, DemandedVecElts, Known2, Q, Depth + 1);
2350 Known = Known.intersectWith(Known2);
2351 }
2352 break;
2353 }
2354 case Instruction::ExtractElement: {
2355 // Look through extract element. If the index is non-constant or
2356 // out-of-range demand all elements, otherwise just the extracted element.
2357 const Value *Vec = I->getOperand(0);
2358 const Value *Idx = I->getOperand(1);
2359 auto *CIdx = dyn_cast<ConstantInt>(Idx);
2360 if (isa<ScalableVectorType>(Vec->getType())) {
2361 // FIXME: there's probably *something* we can do with scalable vectors
2362 Known.resetAll();
2363 break;
2364 }
2365 unsigned NumElts = cast<FixedVectorType>(Vec->getType())->getNumElements();
2366 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
2367 if (CIdx && CIdx->getValue().ult(NumElts))
2368 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
2369 computeKnownBits(Vec, DemandedVecElts, Known, Q, Depth + 1);
2370 break;
2371 }
2372 case Instruction::ExtractValue:
2373 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
2375 if (EVI->getNumIndices() != 1) break;
2376 if (EVI->getIndices()[0] == 0) {
2377 switch (II->getIntrinsicID()) {
2378 default: break;
2379 case Intrinsic::uadd_with_overflow:
2380 case Intrinsic::sadd_with_overflow:
2382 true, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2383 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2384 break;
2385 case Intrinsic::usub_with_overflow:
2386 case Intrinsic::ssub_with_overflow:
2388 false, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2389 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2390 break;
2391 case Intrinsic::umul_with_overflow:
2392 case Intrinsic::smul_with_overflow:
2393 computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false,
2394 false, DemandedElts, Known, Known2, Q, Depth);
2395 break;
2396 }
2397 }
2398 }
2399 break;
2400 case Instruction::Freeze:
2401 if (isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
2402 Depth + 1))
2403 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2404 break;
2405 }
2406}
2407
2408/// Determine which bits of V are known to be either zero or one and return
2409/// them.
2410KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
2411 const SimplifyQuery &Q, unsigned Depth) {
2412 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2413 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);
2414 return Known;
2415}
2416
2417/// Determine which bits of V are known to be either zero or one and return
2418/// them.
2420 unsigned Depth) {
2421 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2422 computeKnownBits(V, Known, Q, Depth);
2423 return Known;
2424}
2425
2426/// Determine which bits of V are known to be either zero or one and return
2427/// them in the Known bit set.
2428///
2429/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that
2430/// we cannot optimize based on the assumption that it is zero without changing
2431/// it to be an explicit zero. If we don't change it to zero, other code could
2432/// optimized based on the contradictory assumption that it is non-zero.
2433/// Because instcombine aggressively folds operations with undef args anyway,
2434/// this won't lose us code quality.
2435///
2436/// This function is defined on values with integer type, values with pointer
2437/// type, and vectors of integers. In the case
2438/// where V is a vector, known zero, and known one values are the
2439/// same width as the vector element, and the bit is set only if it is true
2440/// for all of the demanded elements in the vector specified by DemandedElts.
2441void computeKnownBits(const Value *V, const APInt &DemandedElts,
2442 KnownBits &Known, const SimplifyQuery &Q,
2443 unsigned Depth) {
2444 if (!DemandedElts) {
2445 // No demanded elts, better to assume we don't know anything.
2446 Known.resetAll();
2447 return;
2448 }
2449
2450 assert(V && "No Value?");
2451 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2452
2453#ifndef NDEBUG
2454 Type *Ty = V->getType();
2455 unsigned BitWidth = Known.getBitWidth();
2456
2457 assert((Ty->isIntOrIntVectorTy(BitWidth) || Ty->isPtrOrPtrVectorTy()) &&
2458 "Not integer or pointer type!");
2459
2460 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
2461 assert(
2462 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
2463 "DemandedElt width should equal the fixed vector number of elements");
2464 } else {
2465 assert(DemandedElts == APInt(1, 1) &&
2466 "DemandedElt width should be 1 for scalars or scalable vectors");
2467 }
2468
2469 Type *ScalarTy = Ty->getScalarType();
2470 if (ScalarTy->isPointerTy()) {
2471 assert(BitWidth == Q.DL.getPointerTypeSizeInBits(ScalarTy) &&
2472 "V and Known should have same BitWidth");
2473 } else {
2474 assert(BitWidth == Q.DL.getTypeSizeInBits(ScalarTy) &&
2475 "V and Known should have same BitWidth");
2476 }
2477#endif
2478
2479 const APInt *C;
2480 if (match(V, m_APInt(C))) {
2481 // We know all of the bits for a scalar constant or a splat vector constant!
2482 Known = KnownBits::makeConstant(*C);
2483 return;
2484 }
2485 // Null and aggregate-zero are all-zeros.
2487 Known.setAllZero();
2488 return;
2489 }
2490 // Handle a constant vector by taking the intersection of the known bits of
2491 // each element.
2493 assert(!isa<ScalableVectorType>(V->getType()));
2494 // We know that CDV must be a vector of integers. Take the intersection of
2495 // each element.
2496 Known.setAllConflict();
2497 for (unsigned i = 0, e = CDV->getNumElements(); i != e; ++i) {
2498 if (!DemandedElts[i])
2499 continue;
2500 APInt Elt = CDV->getElementAsAPInt(i);
2501 Known.Zero &= ~Elt;
2502 Known.One &= Elt;
2503 }
2504 if (Known.hasConflict())
2505 Known.resetAll();
2506 return;
2507 }
2508
2509 if (const auto *CV = dyn_cast<ConstantVector>(V)) {
2510 assert(!isa<ScalableVectorType>(V->getType()));
2511 // We know that CV must be a vector of integers. Take the intersection of
2512 // each element.
2513 Known.setAllConflict();
2514 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
2515 if (!DemandedElts[i])
2516 continue;
2517 Constant *Element = CV->getAggregateElement(i);
2518 if (isa<PoisonValue>(Element))
2519 continue;
2520 auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
2521 if (!ElementCI) {
2522 Known.resetAll();
2523 return;
2524 }
2525 const APInt &Elt = ElementCI->getValue();
2526 Known.Zero &= ~Elt;
2527 Known.One &= Elt;
2528 }
2529 if (Known.hasConflict())
2530 Known.resetAll();
2531 return;
2532 }
2533
2534 // Start out not knowing anything.
2535 Known.resetAll();
2536
2537 // We can't imply anything about undefs.
2538 if (isa<UndefValue>(V))
2539 return;
2540
2541 // There's no point in looking through other users of ConstantData for
2542 // assumptions. Confirm that we've handled them all.
2543 assert(!isa<ConstantData>(V) && "Unhandled constant data!");
2544
2545 if (const auto *A = dyn_cast<Argument>(V))
2546 if (std::optional<ConstantRange> Range = A->getRange())
2547 Known = Range->toKnownBits();
2548
2549 // All recursive calls that increase depth must come after this.
2551 return;
2552
2553 // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
2554 // the bits of its aliasee.
2555 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
2556 if (!GA->isInterposable())
2557 computeKnownBits(GA->getAliasee(), Known, Q, Depth + 1);
2558 return;
2559 }
2560
2561 if (const Operator *I = dyn_cast<Operator>(V))
2562 computeKnownBitsFromOperator(I, DemandedElts, Known, Q, Depth);
2563 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2564 if (std::optional<ConstantRange> CR = GV->getAbsoluteSymbolRange())
2565 Known = CR->toKnownBits();
2566 }
2567
2568 // Aligned pointers have trailing zeros - refine Known.Zero set
2569 if (isa<PointerType>(V->getType())) {
2570 Align Alignment = V->getPointerAlignment(Q.DL);
2571 Known.Zero.setLowBits(Log2(Alignment));
2572 }
2573
2574 // computeKnownBitsFromContext strictly refines Known.
2575 // Therefore, we run them after computeKnownBitsFromOperator.
2576
2577 // Check whether we can determine known bits from context such as assumes.
2578 computeKnownBitsFromContext(V, Known, Q, Depth);
2579}
2580
2581/// Try to detect a recurrence that the value of the induction variable is
2582/// always a power of two (or zero).
2583static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero,
2584 SimplifyQuery &Q, unsigned Depth) {
2585 BinaryOperator *BO = nullptr;
2586 Value *Start = nullptr, *Step = nullptr;
2587 if (!matchSimpleRecurrence(PN, BO, Start, Step))
2588 return false;
2589
2590 // Initial value must be a power of two.
2591 for (const Use &U : PN->operands()) {
2592 if (U.get() == Start) {
2593 // Initial value comes from a different BB, need to adjust context
2594 // instruction for analysis.
2595 Q.CxtI = PN->getIncomingBlock(U)->getTerminator();
2596 if (!isKnownToBeAPowerOfTwo(Start, OrZero, Q, Depth))
2597 return false;
2598 }
2599 }
2600
2601 // Except for Mul, the induction variable must be on the left side of the
2602 // increment expression, otherwise its value can be arbitrary.
2603 if (BO->getOpcode() != Instruction::Mul && BO->getOperand(1) != Step)
2604 return false;
2605
2606 Q.CxtI = BO->getParent()->getTerminator();
2607 switch (BO->getOpcode()) {
2608 case Instruction::Mul:
2609 // Power of two is closed under multiplication.
2610 return (OrZero || Q.IIQ.hasNoUnsignedWrap(BO) ||
2611 Q.IIQ.hasNoSignedWrap(BO)) &&
2612 isKnownToBeAPowerOfTwo(Step, OrZero, Q, Depth);
2613 case Instruction::SDiv:
2614 // Start value must not be signmask for signed division, so simply being a
2615 // power of two is not sufficient, and it has to be a constant.
2616 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2617 return false;
2618 [[fallthrough]];
2619 case Instruction::UDiv:
2620 // Divisor must be a power of two.
2621 // If OrZero is false, cannot guarantee induction variable is non-zero after
2622 // division, same for Shr, unless it is exact division.
2623 return (OrZero || Q.IIQ.isExact(BO)) &&
2624 isKnownToBeAPowerOfTwo(Step, false, Q, Depth);
2625 case Instruction::Shl:
2626 return OrZero || Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO);
2627 case Instruction::AShr:
2628 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2629 return false;
2630 [[fallthrough]];
2631 case Instruction::LShr:
2632 return OrZero || Q.IIQ.isExact(BO);
2633 default:
2634 return false;
2635 }
2636}
2637
2638/// Return true if we can infer that \p V is known to be a power of 2 from
2639/// dominating condition \p Cond (e.g., ctpop(V) == 1).
2640static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero,
2641 const Value *Cond,
2642 bool CondIsTrue) {
2643 CmpPredicate Pred;
2644 const APInt *RHSC;
2646 m_APInt(RHSC))))
2647 return false;
2648 if (!CondIsTrue)
2649 Pred = ICmpInst::getInversePredicate(Pred);
2650 // ctpop(V) u< 2
2651 if (OrZero && Pred == ICmpInst::ICMP_ULT && *RHSC == 2)
2652 return true;
2653 // ctpop(V) == 1
2654 return Pred == ICmpInst::ICMP_EQ && *RHSC == 1;
2655}
2656
2657/// Return true if the given value is known to have exactly one
2658/// bit set when defined. For vectors return true if every element is known to
2659/// be a power of two when defined. Supports values with integer or pointer
2660/// types and vectors of integers.
2661bool llvm::isKnownToBeAPowerOfTwo(const Value *V, bool OrZero,
2662 const SimplifyQuery &Q, unsigned Depth) {
2663 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2664
2665 if (isa<Constant>(V))
2666 return OrZero ? match(V, m_Power2OrZero()) : match(V, m_Power2());
2667
2668 // i1 is by definition a power of 2 or zero.
2669 if (OrZero && V->getType()->getScalarSizeInBits() == 1)
2670 return true;
2671
2672 // Try to infer from assumptions.
2673 if (Q.AC && Q.CxtI) {
2674 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
2675 if (!AssumeVH)
2676 continue;
2677 CallInst *I = cast<CallInst>(AssumeVH);
2678 if (isImpliedToBeAPowerOfTwoFromCond(V, OrZero, I->getArgOperand(0),
2679 /*CondIsTrue=*/true) &&
2681 return true;
2682 }
2683 }
2684
2685 // Handle dominating conditions.
2686 if (Q.DC && Q.CxtI && Q.DT) {
2687 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
2688 Value *Cond = BI->getCondition();
2689
2690 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
2692 /*CondIsTrue=*/true) &&
2693 Q.DT->dominates(Edge0, Q.CxtI->getParent()))
2694 return true;
2695
2696 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
2698 /*CondIsTrue=*/false) &&
2699 Q.DT->dominates(Edge1, Q.CxtI->getParent()))
2700 return true;
2701 }
2702 }
2703
2704 auto *I = dyn_cast<Instruction>(V);
2705 if (!I)
2706 return false;
2707
2708 if (Q.CxtI && match(V, m_VScale())) {
2709 const Function *F = Q.CxtI->getFunction();
2710 // The vscale_range indicates vscale is a power-of-two.
2711 return F->hasFnAttribute(Attribute::VScaleRange);
2712 }
2713
2714 // 1 << X is clearly a power of two if the one is not shifted off the end. If
2715 // it is shifted off the end then the result is undefined.
2716 if (match(I, m_Shl(m_One(), m_Value())))
2717 return true;
2718
2719 // (signmask) >>l X is clearly a power of two if the one is not shifted off
2720 // the bottom. If it is shifted off the bottom then the result is undefined.
2721 if (match(I, m_LShr(m_SignMask(), m_Value())))
2722 return true;
2723
2724 // The remaining tests are all recursive, so bail out if we hit the limit.
2726 return false;
2727
2728 switch (I->getOpcode()) {
2729 case Instruction::ZExt:
2730 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2731 case Instruction::Trunc:
2732 return OrZero && isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2733 case Instruction::Shl:
2734 if (OrZero || Q.IIQ.hasNoUnsignedWrap(I) || Q.IIQ.hasNoSignedWrap(I))
2735 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2736 return false;
2737 case Instruction::LShr:
2738 if (OrZero || Q.IIQ.isExact(cast<BinaryOperator>(I)))
2739 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2740 return false;
2741 case Instruction::UDiv:
2743 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2744 return false;
2745 case Instruction::Mul:
2746 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
2747 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth) &&
2748 (OrZero || isKnownNonZero(I, Q, Depth));
2749 case Instruction::And:
2750 // A power of two and'd with anything is a power of two or zero.
2751 if (OrZero &&
2752 (isKnownToBeAPowerOfTwo(I->getOperand(1), /*OrZero*/ true, Q, Depth) ||
2753 isKnownToBeAPowerOfTwo(I->getOperand(0), /*OrZero*/ true, Q, Depth)))
2754 return true;
2755 // X & (-X) is always a power of two or zero.
2756 if (match(I->getOperand(0), m_Neg(m_Specific(I->getOperand(1)))) ||
2757 match(I->getOperand(1), m_Neg(m_Specific(I->getOperand(0)))))
2758 return OrZero || isKnownNonZero(I->getOperand(0), Q, Depth);
2759 return false;
2760 case Instruction::Add: {
2761 // Adding a power-of-two or zero to the same power-of-two or zero yields
2762 // either the original power-of-two, a larger power-of-two or zero.
2764 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) ||
2765 Q.IIQ.hasNoSignedWrap(VOBO)) {
2766 if (match(I->getOperand(0),
2767 m_c_And(m_Specific(I->getOperand(1)), m_Value())) &&
2768 isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth))
2769 return true;
2770 if (match(I->getOperand(1),
2771 m_c_And(m_Specific(I->getOperand(0)), m_Value())) &&
2772 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth))
2773 return true;
2774
2775 unsigned BitWidth = V->getType()->getScalarSizeInBits();
2776 KnownBits LHSBits(BitWidth);
2777 computeKnownBits(I->getOperand(0), LHSBits, Q, Depth);
2778
2779 KnownBits RHSBits(BitWidth);
2780 computeKnownBits(I->getOperand(1), RHSBits, Q, Depth);
2781 // If i8 V is a power of two or zero:
2782 // ZeroBits: 1 1 1 0 1 1 1 1
2783 // ~ZeroBits: 0 0 0 1 0 0 0 0
2784 if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2())
2785 // If OrZero isn't set, we cannot give back a zero result.
2786 // Make sure either the LHS or RHS has a bit set.
2787 if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue())
2788 return true;
2789 }
2790
2791 // LShr(UINT_MAX, Y) + 1 is a power of two (if add is nuw) or zero.
2792 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO))
2793 if (match(I, m_Add(m_LShr(m_AllOnes(), m_Value()), m_One())))
2794 return true;
2795 return false;
2796 }
2797 case Instruction::Select:
2798 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
2799 isKnownToBeAPowerOfTwo(I->getOperand(2), OrZero, Q, Depth);
2800 case Instruction::PHI: {
2801 // A PHI node is power of two if all incoming values are power of two, or if
2802 // it is an induction variable where in each step its value is a power of
2803 // two.
2804 auto *PN = cast<PHINode>(I);
2806
2807 // Check if it is an induction variable and always power of two.
2808 if (isPowerOfTwoRecurrence(PN, OrZero, RecQ, Depth))
2809 return true;
2810
2811 // Recursively check all incoming values. Limit recursion to 2 levels, so
2812 // that search complexity is limited to number of operands^2.
2813 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
2814 return llvm::all_of(PN->operands(), [&](const Use &U) {
2815 // Value is power of 2 if it is coming from PHI node itself by induction.
2816 if (U.get() == PN)
2817 return true;
2818
2819 // Change the context instruction to the incoming block where it is
2820 // evaluated.
2821 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
2822 return isKnownToBeAPowerOfTwo(U.get(), OrZero, RecQ, NewDepth);
2823 });
2824 }
2825 case Instruction::Invoke:
2826 case Instruction::Call: {
2827 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
2828 switch (II->getIntrinsicID()) {
2829 case Intrinsic::umax:
2830 case Intrinsic::smax:
2831 case Intrinsic::umin:
2832 case Intrinsic::smin:
2833 return isKnownToBeAPowerOfTwo(II->getArgOperand(1), OrZero, Q, Depth) &&
2834 isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2835 // bswap/bitreverse just move around bits, but don't change any 1s/0s
2836 // thus dont change pow2/non-pow2 status.
2837 case Intrinsic::bitreverse:
2838 case Intrinsic::bswap:
2839 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2840 case Intrinsic::fshr:
2841 case Intrinsic::fshl:
2842 // If Op0 == Op1, this is a rotate. is_pow2(rotate(x, y)) == is_pow2(x)
2843 if (II->getArgOperand(0) == II->getArgOperand(1))
2844 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2845 break;
2846 default:
2847 break;
2848 }
2849 }
2850 return false;
2851 }
2852 default:
2853 return false;
2854 }
2855}
2856
2857/// Test whether a GEP's result is known to be non-null.
2858///
2859/// Uses properties inherent in a GEP to try to determine whether it is known
2860/// to be non-null.
2861///
2862/// Currently this routine does not support vector GEPs.
2863static bool isGEPKnownNonNull(const GEPOperator *GEP, const SimplifyQuery &Q,
2864 unsigned Depth) {
2865 const Function *F = nullptr;
2866 if (const Instruction *I = dyn_cast<Instruction>(GEP))
2867 F = I->getFunction();
2868
2869 // If the gep is nuw or inbounds with invalid null pointer, then the GEP
2870 // may be null iff the base pointer is null and the offset is zero.
2871 if (!GEP->hasNoUnsignedWrap() &&
2872 !(GEP->isInBounds() &&
2873 !NullPointerIsDefined(F, GEP->getPointerAddressSpace())))
2874 return false;
2875
2876 // FIXME: Support vector-GEPs.
2877 assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP");
2878
2879 // If the base pointer is non-null, we cannot walk to a null address with an
2880 // inbounds GEP in address space zero.
2881 if (isKnownNonZero(GEP->getPointerOperand(), Q, Depth))
2882 return true;
2883
2884 // Walk the GEP operands and see if any operand introduces a non-zero offset.
2885 // If so, then the GEP cannot produce a null pointer, as doing so would
2886 // inherently violate the inbounds contract within address space zero.
2888 GTI != GTE; ++GTI) {
2889 // Struct types are easy -- they must always be indexed by a constant.
2890 if (StructType *STy = GTI.getStructTypeOrNull()) {
2891 ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
2892 unsigned ElementIdx = OpC->getZExtValue();
2893 const StructLayout *SL = Q.DL.getStructLayout(STy);
2894 uint64_t ElementOffset = SL->getElementOffset(ElementIdx);
2895 if (ElementOffset > 0)
2896 return true;
2897 continue;
2898 }
2899
2900 // If we have a zero-sized type, the index doesn't matter. Keep looping.
2901 if (GTI.getSequentialElementStride(Q.DL).isZero())
2902 continue;
2903
2904 // Fast path the constant operand case both for efficiency and so we don't
2905 // increment Depth when just zipping down an all-constant GEP.
2906 if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
2907 if (!OpC->isZero())
2908 return true;
2909 continue;
2910 }
2911
2912 // We post-increment Depth here because while isKnownNonZero increments it
2913 // as well, when we pop back up that increment won't persist. We don't want
2914 // to recurse 10k times just because we have 10k GEP operands. We don't
2915 // bail completely out because we want to handle constant GEPs regardless
2916 // of depth.
2918 continue;
2919
2920 if (isKnownNonZero(GTI.getOperand(), Q, Depth))
2921 return true;
2922 }
2923
2924 return false;
2925}
2926
2928 const Instruction *CtxI,
2929 const DominatorTree *DT) {
2930 assert(!isa<Constant>(V) && "Called for constant?");
2931
2932 if (!CtxI || !DT)
2933 return false;
2934
2935 unsigned NumUsesExplored = 0;
2936 for (auto &U : V->uses()) {
2937 // Avoid massive lists
2938 if (NumUsesExplored >= DomConditionsMaxUses)
2939 break;
2940 NumUsesExplored++;
2941
2942 const Instruction *UI = cast<Instruction>(U.getUser());
2943 // If the value is used as an argument to a call or invoke, then argument
2944 // attributes may provide an answer about null-ness.
2945 if (V->getType()->isPointerTy()) {
2946 if (const auto *CB = dyn_cast<CallBase>(UI)) {
2947 if (CB->isArgOperand(&U) &&
2948 CB->paramHasNonNullAttr(CB->getArgOperandNo(&U),
2949 /*AllowUndefOrPoison=*/false) &&
2950 DT->dominates(CB, CtxI))
2951 return true;
2952 }
2953 }
2954
2955 // If the value is used as a load/store, then the pointer must be non null.
2956 if (V == getLoadStorePointerOperand(UI)) {
2959 DT->dominates(UI, CtxI))
2960 return true;
2961 }
2962
2963 if ((match(UI, m_IDiv(m_Value(), m_Specific(V))) ||
2964 match(UI, m_IRem(m_Value(), m_Specific(V)))) &&
2965 isValidAssumeForContext(UI, CtxI, DT))
2966 return true;
2967
2968 // Consider only compare instructions uniquely controlling a branch
2969 Value *RHS;
2970 CmpPredicate Pred;
2971 if (!match(UI, m_c_ICmp(Pred, m_Specific(V), m_Value(RHS))))
2972 continue;
2973
2974 bool NonNullIfTrue;
2975 if (cmpExcludesZero(Pred, RHS))
2976 NonNullIfTrue = true;
2978 NonNullIfTrue = false;
2979 else
2980 continue;
2981
2984 for (const auto *CmpU : UI->users()) {
2985 assert(WorkList.empty() && "Should be!");
2986 if (Visited.insert(CmpU).second)
2987 WorkList.push_back(CmpU);
2988
2989 while (!WorkList.empty()) {
2990 auto *Curr = WorkList.pop_back_val();
2991
2992 // If a user is an AND, add all its users to the work list. We only
2993 // propagate "pred != null" condition through AND because it is only
2994 // correct to assume that all conditions of AND are met in true branch.
2995 // TODO: Support similar logic of OR and EQ predicate?
2996 if (NonNullIfTrue)
2997 if (match(Curr, m_LogicalAnd(m_Value(), m_Value()))) {
2998 for (const auto *CurrU : Curr->users())
2999 if (Visited.insert(CurrU).second)
3000 WorkList.push_back(CurrU);
3001 continue;
3002 }
3003
3004 if (const BranchInst *BI = dyn_cast<BranchInst>(Curr)) {
3005 assert(BI->isConditional() && "uses a comparison!");
3006
3007 BasicBlock *NonNullSuccessor =
3008 BI->getSuccessor(NonNullIfTrue ? 0 : 1);
3009 BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor);
3010 if (Edge.isSingleEdge() && DT->dominates(Edge, CtxI->getParent()))
3011 return true;
3012 } else if (NonNullIfTrue && isGuard(Curr) &&
3013 DT->dominates(cast<Instruction>(Curr), CtxI)) {
3014 return true;
3015 }
3016 }
3017 }
3018 }
3019
3020 return false;
3021}
3022
3023/// Does the 'Range' metadata (which must be a valid MD_range operand list)
3024/// ensure that the value it's attached to is never Value? 'RangeType' is
3025/// is the type of the value described by the range.
3026static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) {
3027 const unsigned NumRanges = Ranges->getNumOperands() / 2;
3028 assert(NumRanges >= 1);
3029 for (unsigned i = 0; i < NumRanges; ++i) {
3031 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
3033 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
3034 ConstantRange Range(Lower->getValue(), Upper->getValue());
3035 if (Range.contains(Value))
3036 return false;
3037 }
3038 return true;
3039}
3040
3041/// Try to detect a recurrence that monotonically increases/decreases from a
3042/// non-zero starting value. These are common as induction variables.
3043static bool isNonZeroRecurrence(const PHINode *PN) {
3044 BinaryOperator *BO = nullptr;
3045 Value *Start = nullptr, *Step = nullptr;
3046 const APInt *StartC, *StepC;
3047 if (!matchSimpleRecurrence(PN, BO, Start, Step) ||
3048 !match(Start, m_APInt(StartC)) || StartC->isZero())
3049 return false;
3050
3051 switch (BO->getOpcode()) {
3052 case Instruction::Add:
3053 // Starting from non-zero and stepping away from zero can never wrap back
3054 // to zero.
3055 return BO->hasNoUnsignedWrap() ||
3056 (BO->hasNoSignedWrap() && match(Step, m_APInt(StepC)) &&
3057 StartC->isNegative() == StepC->isNegative());
3058 case Instruction::Mul:
3059 return (BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap()) &&
3060 match(Step, m_APInt(StepC)) && !StepC->isZero();
3061 case Instruction::Shl:
3062 return BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap();
3063 case Instruction::AShr:
3064 case Instruction::LShr:
3065 return BO->isExact();
3066 default:
3067 return false;
3068 }
3069}
3070
3071static bool matchOpWithOpEqZero(Value *Op0, Value *Op1) {
3073 m_Specific(Op1), m_Zero()))) ||
3075 m_Specific(Op0), m_Zero())));
3076}
3077
3078static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q,
3079 unsigned BitWidth, Value *X, Value *Y, bool NSW,
3080 bool NUW, unsigned Depth) {
3081 // (X + (X != 0)) is non zero
3082 if (matchOpWithOpEqZero(X, Y))
3083 return true;
3084
3085 if (NUW)
3086 return isKnownNonZero(Y, DemandedElts, Q, Depth) ||
3087 isKnownNonZero(X, DemandedElts, Q, Depth);
3088
3089 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
3090 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
3091
3092 // If X and Y are both non-negative (as signed values) then their sum is not
3093 // zero unless both X and Y are zero.
3094 if (XKnown.isNonNegative() && YKnown.isNonNegative())
3095 if (isKnownNonZero(Y, DemandedElts, Q, Depth) ||
3096 isKnownNonZero(X, DemandedElts, Q, Depth))
3097 return true;
3098
3099 // If X and Y are both negative (as signed values) then their sum is not
3100 // zero unless both X and Y equal INT_MIN.
3101 if (XKnown.isNegative() && YKnown.isNegative()) {
3103 // The sign bit of X is set. If some other bit is set then X is not equal
3104 // to INT_MIN.
3105 if (XKnown.One.intersects(Mask))
3106 return true;
3107 // The sign bit of Y is set. If some other bit is set then Y is not equal
3108 // to INT_MIN.
3109 if (YKnown.One.intersects(Mask))
3110 return true;
3111 }
3112
3113 // The sum of a non-negative number and a power of two is not zero.
3114 if (XKnown.isNonNegative() &&
3115 isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Q, Depth))
3116 return true;
3117 if (YKnown.isNonNegative() &&
3118 isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Q, Depth))
3119 return true;
3120
3121 return KnownBits::add(XKnown, YKnown, NSW, NUW).isNonZero();
3122}
3123
3124static bool isNonZeroSub(const APInt &DemandedElts, const SimplifyQuery &Q,
3125 unsigned BitWidth, Value *X, Value *Y,
3126 unsigned Depth) {
3127 // (X - (X != 0)) is non zero
3128 // ((X != 0) - X) is non zero
3129 if (matchOpWithOpEqZero(X, Y))
3130 return true;
3131
3132 // TODO: Move this case into isKnownNonEqual().
3133 if (auto *C = dyn_cast<Constant>(X))
3134 if (C->isNullValue() && isKnownNonZero(Y, DemandedElts, Q, Depth))
3135 return true;
3136
3137 return ::isKnownNonEqual(X, Y, DemandedElts, Q, Depth);
3138}
3139
3140static bool isNonZeroMul(const APInt &DemandedElts, const SimplifyQuery &Q,
3141 unsigned BitWidth, Value *X, Value *Y, bool NSW,
3142 bool NUW, unsigned Depth) {
3143 // If X and Y are non-zero then so is X * Y as long as the multiplication
3144 // does not overflow.
3145 if (NSW || NUW)
3146 return isKnownNonZero(X, DemandedElts, Q, Depth) &&
3147 isKnownNonZero(Y, DemandedElts, Q, Depth);
3148
3149 // If either X or Y is odd, then if the other is non-zero the result can't
3150 // be zero.
3151 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
3152 if (XKnown.One[0])
3153 return isKnownNonZero(Y, DemandedElts, Q, Depth);
3154
3155 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
3156 if (YKnown.One[0])
3157 return XKnown.isNonZero() || isKnownNonZero(X, DemandedElts, Q, Depth);
3158
3159 // If there exists any subset of X (sX) and subset of Y (sY) s.t sX * sY is
3160 // non-zero, then X * Y is non-zero. We can find sX and sY by just taking
3161 // the lowest known One of X and Y. If they are non-zero, the result
3162 // must be non-zero. We can check if LSB(X) * LSB(Y) != 0 by doing
3163 // X.CountLeadingZeros + Y.CountLeadingZeros < BitWidth.
3164 return (XKnown.countMaxTrailingZeros() + YKnown.countMaxTrailingZeros()) <
3165 BitWidth;
3166}
3167
3168static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts,
3169 const SimplifyQuery &Q, const KnownBits &KnownVal,
3170 unsigned Depth) {
3171 auto ShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
3172 switch (I->getOpcode()) {
3173 case Instruction::Shl:
3174 return Lhs.shl(Rhs);
3175 case Instruction::LShr:
3176 return Lhs.lshr(Rhs);
3177 case Instruction::AShr:
3178 return Lhs.ashr(Rhs);
3179 default:
3180 llvm_unreachable("Unknown Shift Opcode");
3181 }
3182 };
3183
3184 auto InvShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
3185 switch (I->getOpcode()) {
3186 case Instruction::Shl:
3187 return Lhs.lshr(Rhs);
3188 case Instruction::LShr:
3189 case Instruction::AShr:
3190 return Lhs.shl(Rhs);
3191 default:
3192 llvm_unreachable("Unknown Shift Opcode");
3193 }
3194 };
3195
3196 if (KnownVal.isUnknown())
3197 return false;
3198
3199 KnownBits KnownCnt =
3200 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
3201 APInt MaxShift = KnownCnt.getMaxValue();
3202 unsigned NumBits = KnownVal.getBitWidth();
3203 if (MaxShift.uge(NumBits))
3204 return false;
3205
3206 if (!ShiftOp(KnownVal.One, MaxShift).isZero())
3207 return true;
3208
3209 // If all of the bits shifted out are known to be zero, and Val is known
3210 // non-zero then at least one non-zero bit must remain.
3211 if (InvShiftOp(KnownVal.Zero, NumBits - MaxShift)
3212 .eq(InvShiftOp(APInt::getAllOnes(NumBits), NumBits - MaxShift)) &&
3213 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth))
3214 return true;
3215
3216 return false;
3217}
3218
3220 const APInt &DemandedElts,
3221 const SimplifyQuery &Q, unsigned Depth) {
3222 unsigned BitWidth = getBitWidth(I->getType()->getScalarType(), Q.DL);
3223 switch (I->getOpcode()) {
3224 case Instruction::Alloca:
3225 // Alloca never returns null, malloc might.
3226 return I->getType()->getPointerAddressSpace() == 0;
3227 case Instruction::GetElementPtr:
3228 if (I->getType()->isPointerTy())
3230 break;
3231 case Instruction::BitCast: {
3232 // We need to be a bit careful here. We can only peek through the bitcast
3233 // if the scalar size of elements in the operand are smaller than and a
3234 // multiple of the size they are casting too. Take three cases:
3235 //
3236 // 1) Unsafe:
3237 // bitcast <2 x i16> %NonZero to <4 x i8>
3238 //
3239 // %NonZero can have 2 non-zero i16 elements, but isKnownNonZero on a
3240 // <4 x i8> requires that all 4 i8 elements be non-zero which isn't
3241 // guranteed (imagine just sign bit set in the 2 i16 elements).
3242 //
3243 // 2) Unsafe:
3244 // bitcast <4 x i3> %NonZero to <3 x i4>
3245 //
3246 // Even though the scalar size of the src (`i3`) is smaller than the
3247 // scalar size of the dst `i4`, because `i3` is not a multiple of `i4`
3248 // its possible for the `3 x i4` elements to be zero because there are
3249 // some elements in the destination that don't contain any full src
3250 // element.
3251 //
3252 // 3) Safe:
3253 // bitcast <4 x i8> %NonZero to <2 x i16>
3254 //
3255 // This is always safe as non-zero in the 4 i8 elements implies
3256 // non-zero in the combination of any two adjacent ones. Since i8 is a
3257 // multiple of i16, each i16 is guranteed to have 2 full i8 elements.
3258 // This all implies the 2 i16 elements are non-zero.
3259 Type *FromTy = I->getOperand(0)->getType();
3260 if ((FromTy->isIntOrIntVectorTy() || FromTy->isPtrOrPtrVectorTy()) &&
3261 (BitWidth % getBitWidth(FromTy->getScalarType(), Q.DL)) == 0)
3262 return isKnownNonZero(I->getOperand(0), Q, Depth);
3263 } break;
3264 case Instruction::IntToPtr:
3265 // Note that we have to take special care to avoid looking through
3266 // truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well
3267 // as casts that can alter the value, e.g., AddrSpaceCasts.
3268 if (!isa<ScalableVectorType>(I->getType()) &&
3269 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
3270 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
3271 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3272 break;
3273 case Instruction::PtrToAddr:
3274 // isKnownNonZero() for pointers refers to the address bits being non-zero,
3275 // so we can directly forward.
3276 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3277 case Instruction::PtrToInt:
3278 // For inttoptr, make sure the result size is >= the address size. If the
3279 // address is non-zero, any larger value is also non-zero.
3280 if (Q.DL.getAddressSizeInBits(I->getOperand(0)->getType()) <=
3281 I->getType()->getScalarSizeInBits())
3282 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3283 break;
3284 case Instruction::Trunc:
3285 // nuw/nsw trunc preserves zero/non-zero status of input.
3286 if (auto *TI = dyn_cast<TruncInst>(I))
3287 if (TI->hasNoSignedWrap() || TI->hasNoUnsignedWrap())
3288 return isKnownNonZero(TI->getOperand(0), DemandedElts, Q, Depth);
3289 break;
3290
3291 // Iff x - y != 0, then x ^ y != 0
3292 // Therefore we can do the same exact checks
3293 case Instruction::Xor:
3294 case Instruction::Sub:
3295 return isNonZeroSub(DemandedElts, Q, BitWidth, I->getOperand(0),
3296 I->getOperand(1), Depth);
3297 case Instruction::Or:
3298 // (X | (X != 0)) is non zero
3299 if (matchOpWithOpEqZero(I->getOperand(0), I->getOperand(1)))
3300 return true;
3301 // X | Y != 0 if X != Y.
3302 if (isKnownNonEqual(I->getOperand(0), I->getOperand(1), DemandedElts, Q,
3303 Depth))
3304 return true;
3305 // X | Y != 0 if X != 0 or Y != 0.
3306 return isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth) ||
3307 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3308 case Instruction::SExt:
3309 case Instruction::ZExt:
3310 // ext X != 0 if X != 0.
3311 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3312
3313 case Instruction::Shl: {
3314 // shl nsw/nuw can't remove any non-zero bits.
3316 if (Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO))
3317 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3318
3319 // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
3320 // if the lowest bit is shifted off the end.
3321 KnownBits Known(BitWidth);
3322 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth);
3323 if (Known.One[0])
3324 return true;
3325
3326 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3327 }
3328 case Instruction::LShr:
3329 case Instruction::AShr: {
3330 // shr exact can only shift out zero bits.
3332 if (BO->isExact())
3333 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3334
3335 // shr X, Y != 0 if X is negative. Note that the value of the shift is not
3336 // defined if the sign bit is shifted off the end.
3337 KnownBits Known =
3338 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3339 if (Known.isNegative())
3340 return true;
3341
3342 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3343 }
3344 case Instruction::UDiv:
3345 case Instruction::SDiv: {
3346 // X / Y
3347 // div exact can only produce a zero if the dividend is zero.
3348 if (cast<PossiblyExactOperator>(I)->isExact())
3349 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3350
3351 KnownBits XKnown =
3352 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3353 // If X is fully unknown we won't be able to figure anything out so don't
3354 // both computing knownbits for Y.
3355 if (XKnown.isUnknown())
3356 return false;
3357
3358 KnownBits YKnown =
3359 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
3360 if (I->getOpcode() == Instruction::SDiv) {
3361 // For signed division need to compare abs value of the operands.
3362 XKnown = XKnown.abs(/*IntMinIsPoison*/ false);
3363 YKnown = YKnown.abs(/*IntMinIsPoison*/ false);
3364 }
3365 // If X u>= Y then div is non zero (0/0 is UB).
3366 std::optional<bool> XUgeY = KnownBits::uge(XKnown, YKnown);
3367 // If X is total unknown or X u< Y we won't be able to prove non-zero
3368 // with compute known bits so just return early.
3369 return XUgeY && *XUgeY;
3370 }
3371 case Instruction::Add: {
3372 // X + Y.
3373
3374 // If Add has nuw wrap flag, then if either X or Y is non-zero the result is
3375 // non-zero.
3377 return isNonZeroAdd(DemandedElts, Q, BitWidth, I->getOperand(0),
3378 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3379 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3380 }
3381 case Instruction::Mul: {
3383 return isNonZeroMul(DemandedElts, Q, BitWidth, I->getOperand(0),
3384 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3385 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3386 }
3387 case Instruction::Select: {
3388 // (C ? X : Y) != 0 if X != 0 and Y != 0.
3389
3390 // First check if the arm is non-zero using `isKnownNonZero`. If that fails,
3391 // then see if the select condition implies the arm is non-zero. For example
3392 // (X != 0 ? X : Y), we know the true arm is non-zero as the `X` "return" is
3393 // dominated by `X != 0`.
3394 auto SelectArmIsNonZero = [&](bool IsTrueArm) {
3395 Value *Op;
3396 Op = IsTrueArm ? I->getOperand(1) : I->getOperand(2);
3397 // Op is trivially non-zero.
3398 if (isKnownNonZero(Op, DemandedElts, Q, Depth))
3399 return true;
3400
3401 // The condition of the select dominates the true/false arm. Check if the
3402 // condition implies that a given arm is non-zero.
3403 Value *X;
3404 CmpPredicate Pred;
3405 if (!match(I->getOperand(0), m_c_ICmp(Pred, m_Specific(Op), m_Value(X))))
3406 return false;
3407
3408 if (!IsTrueArm)
3409 Pred = ICmpInst::getInversePredicate(Pred);
3410
3411 return cmpExcludesZero(Pred, X);
3412 };
3413
3414 if (SelectArmIsNonZero(/* IsTrueArm */ true) &&
3415 SelectArmIsNonZero(/* IsTrueArm */ false))
3416 return true;
3417 break;
3418 }
3419 case Instruction::PHI: {
3420 auto *PN = cast<PHINode>(I);
3422 return true;
3423
3424 // Check if all incoming values are non-zero using recursion.
3426 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
3427 return llvm::all_of(PN->operands(), [&](const Use &U) {
3428 if (U.get() == PN)
3429 return true;
3430 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
3431 // Check if the branch on the phi excludes zero.
3432 CmpPredicate Pred;
3433 Value *X;
3434 BasicBlock *TrueSucc, *FalseSucc;
3435 if (match(RecQ.CxtI,
3436 m_Br(m_c_ICmp(Pred, m_Specific(U.get()), m_Value(X)),
3437 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
3438 // Check for cases of duplicate successors.
3439 if ((TrueSucc == PN->getParent()) != (FalseSucc == PN->getParent())) {
3440 // If we're using the false successor, invert the predicate.
3441 if (FalseSucc == PN->getParent())
3442 Pred = CmpInst::getInversePredicate(Pred);
3443 if (cmpExcludesZero(Pred, X))
3444 return true;
3445 }
3446 }
3447 // Finally recurse on the edge and check it directly.
3448 return isKnownNonZero(U.get(), DemandedElts, RecQ, NewDepth);
3449 });
3450 }
3451 case Instruction::InsertElement: {
3452 if (isa<ScalableVectorType>(I->getType()))
3453 break;
3454
3455 const Value *Vec = I->getOperand(0);
3456 const Value *Elt = I->getOperand(1);
3457 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
3458
3459 unsigned NumElts = DemandedElts.getBitWidth();
3460 APInt DemandedVecElts = DemandedElts;
3461 bool SkipElt = false;
3462 // If we know the index we are inserting too, clear it from Vec check.
3463 if (CIdx && CIdx->getValue().ult(NumElts)) {
3464 DemandedVecElts.clearBit(CIdx->getZExtValue());
3465 SkipElt = !DemandedElts[CIdx->getZExtValue()];
3466 }
3467
3468 // Result is zero if Elt is non-zero and rest of the demanded elts in Vec
3469 // are non-zero.
3470 return (SkipElt || isKnownNonZero(Elt, Q, Depth)) &&
3471 (DemandedVecElts.isZero() ||
3472 isKnownNonZero(Vec, DemandedVecElts, Q, Depth));
3473 }
3474 case Instruction::ExtractElement:
3475 if (const auto *EEI = dyn_cast<ExtractElementInst>(I)) {
3476 const Value *Vec = EEI->getVectorOperand();
3477 const Value *Idx = EEI->getIndexOperand();
3478 auto *CIdx = dyn_cast<ConstantInt>(Idx);
3479 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
3480 unsigned NumElts = VecTy->getNumElements();
3481 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
3482 if (CIdx && CIdx->getValue().ult(NumElts))
3483 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
3484 return isKnownNonZero(Vec, DemandedVecElts, Q, Depth);
3485 }
3486 }
3487 break;
3488 case Instruction::ShuffleVector: {
3489 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
3490 if (!Shuf)
3491 break;
3492 APInt DemandedLHS, DemandedRHS;
3493 // For undef elements, we don't know anything about the common state of
3494 // the shuffle result.
3495 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
3496 break;
3497 // If demanded elements for both vecs are non-zero, the shuffle is non-zero.
3498 return (DemandedRHS.isZero() ||
3499 isKnownNonZero(Shuf->getOperand(1), DemandedRHS, Q, Depth)) &&
3500 (DemandedLHS.isZero() ||
3501 isKnownNonZero(Shuf->getOperand(0), DemandedLHS, Q, Depth));
3502 }
3503 case Instruction::Freeze:
3504 return isKnownNonZero(I->getOperand(0), Q, Depth) &&
3505 isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
3506 Depth);
3507 case Instruction::Load: {
3508 auto *LI = cast<LoadInst>(I);
3509 // A Load tagged with nonnull or dereferenceable with null pointer undefined
3510 // is never null.
3511 if (auto *PtrT = dyn_cast<PointerType>(I->getType())) {
3512 if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull) ||
3513 (Q.IIQ.getMetadata(LI, LLVMContext::MD_dereferenceable) &&
3514 !NullPointerIsDefined(LI->getFunction(), PtrT->getAddressSpace())))
3515 return true;
3516 } else if (MDNode *Ranges = Q.IIQ.getMetadata(LI, LLVMContext::MD_range)) {
3518 }
3519
3520 // No need to fall through to computeKnownBits as range metadata is already
3521 // handled in isKnownNonZero.
3522 return false;
3523 }
3524 case Instruction::ExtractValue: {
3525 const WithOverflowInst *WO;
3527 switch (WO->getBinaryOp()) {
3528 default:
3529 break;
3530 case Instruction::Add:
3531 return isNonZeroAdd(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3532 WO->getArgOperand(1),
3533 /*NSW=*/false,
3534 /*NUW=*/false, Depth);
3535 case Instruction::Sub:
3536 return isNonZeroSub(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3537 WO->getArgOperand(1), Depth);
3538 case Instruction::Mul:
3539 return isNonZeroMul(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3540 WO->getArgOperand(1),
3541 /*NSW=*/false, /*NUW=*/false, Depth);
3542 break;
3543 }
3544 }
3545 break;
3546 }
3547 case Instruction::Call:
3548 case Instruction::Invoke: {
3549 const auto *Call = cast<CallBase>(I);
3550 if (I->getType()->isPointerTy()) {
3551 if (Call->isReturnNonNull())
3552 return true;
3553 if (const auto *RP = getArgumentAliasingToReturnedPointer(Call, true))
3554 return isKnownNonZero(RP, Q, Depth);
3555 } else {
3556 if (MDNode *Ranges = Q.IIQ.getMetadata(Call, LLVMContext::MD_range))
3558 if (std::optional<ConstantRange> Range = Call->getRange()) {
3559 const APInt ZeroValue(Range->getBitWidth(), 0);
3560 if (!Range->contains(ZeroValue))
3561 return true;
3562 }
3563 if (const Value *RV = Call->getReturnedArgOperand())
3564 if (RV->getType() == I->getType() && isKnownNonZero(RV, Q, Depth))
3565 return true;
3566 }
3567
3568 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
3569 switch (II->getIntrinsicID()) {
3570 case Intrinsic::sshl_sat:
3571 case Intrinsic::ushl_sat:
3572 case Intrinsic::abs:
3573 case Intrinsic::bitreverse:
3574 case Intrinsic::bswap:
3575 case Intrinsic::ctpop:
3576 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3577 // NB: We don't do usub_sat here as in any case we can prove its
3578 // non-zero, we will fold it to `sub nuw` in InstCombine.
3579 case Intrinsic::ssub_sat:
3580 // For most types, if x != y then ssub.sat x, y != 0. But
3581 // ssub.sat.i1 0, -1 = 0, because 1 saturates to 0. This means
3582 // isNonZeroSub will do the wrong thing for ssub.sat.i1.
3583 if (BitWidth == 1)
3584 return false;
3585 return isNonZeroSub(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3586 II->getArgOperand(1), Depth);
3587 case Intrinsic::sadd_sat:
3588 return isNonZeroAdd(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3589 II->getArgOperand(1),
3590 /*NSW=*/true, /* NUW=*/false, Depth);
3591 // Vec reverse preserves zero/non-zero status from input vec.
3592 case Intrinsic::vector_reverse:
3593 return isKnownNonZero(II->getArgOperand(0), DemandedElts.reverseBits(),
3594 Q, Depth);
3595 // umin/smin/smax/smin/or of all non-zero elements is always non-zero.
3596 case Intrinsic::vector_reduce_or:
3597 case Intrinsic::vector_reduce_umax:
3598 case Intrinsic::vector_reduce_umin:
3599 case Intrinsic::vector_reduce_smax:
3600 case Intrinsic::vector_reduce_smin:
3601 return isKnownNonZero(II->getArgOperand(0), Q, Depth);
3602 case Intrinsic::umax:
3603 case Intrinsic::uadd_sat:
3604 // umax(X, (X != 0)) is non zero
3605 // X +usat (X != 0) is non zero
3606 if (matchOpWithOpEqZero(II->getArgOperand(0), II->getArgOperand(1)))
3607 return true;
3608
3609 return isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth) ||
3610 isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3611 case Intrinsic::smax: {
3612 // If either arg is strictly positive the result is non-zero. Otherwise
3613 // the result is non-zero if both ops are non-zero.
3614 auto IsNonZero = [&](Value *Op, std::optional<bool> &OpNonZero,
3615 const KnownBits &OpKnown) {
3616 if (!OpNonZero.has_value())
3617 OpNonZero = OpKnown.isNonZero() ||
3618 isKnownNonZero(Op, DemandedElts, Q, Depth);
3619 return *OpNonZero;
3620 };
3621 // Avoid re-computing isKnownNonZero.
3622 std::optional<bool> Op0NonZero, Op1NonZero;
3623 KnownBits Op1Known =
3624 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3625 if (Op1Known.isNonNegative() &&
3626 IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known))
3627 return true;
3628 KnownBits Op0Known =
3629 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3630 if (Op0Known.isNonNegative() &&
3631 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known))
3632 return true;
3633 return IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known) &&
3634 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known);
3635 }
3636 case Intrinsic::smin: {
3637 // If either arg is negative the result is non-zero. Otherwise
3638 // the result is non-zero if both ops are non-zero.
3639 KnownBits Op1Known =
3640 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3641 if (Op1Known.isNegative())
3642 return true;
3643 KnownBits Op0Known =
3644 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3645 if (Op0Known.isNegative())
3646 return true;
3647
3648 if (Op1Known.isNonZero() && Op0Known.isNonZero())
3649 return true;
3650 }
3651 [[fallthrough]];
3652 case Intrinsic::umin:
3653 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth) &&
3654 isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth);
3655 case Intrinsic::cttz:
3656 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3657 .Zero[0];
3658 case Intrinsic::ctlz:
3659 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3660 .isNonNegative();
3661 case Intrinsic::fshr:
3662 case Intrinsic::fshl:
3663 // If Op0 == Op1, this is a rotate. rotate(x, y) != 0 iff x != 0.
3664 if (II->getArgOperand(0) == II->getArgOperand(1))
3665 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3666 break;
3667 case Intrinsic::vscale:
3668 return true;
3669 case Intrinsic::experimental_get_vector_length:
3670 return isKnownNonZero(I->getOperand(0), Q, Depth);
3671 default:
3672 break;
3673 }
3674 break;
3675 }
3676
3677 return false;
3678 }
3679 }
3680
3681 KnownBits Known(BitWidth);
3682 computeKnownBits(I, DemandedElts, Known, Q, Depth);
3683 return Known.One != 0;
3684}
3685
3686/// Return true if the given value is known to be non-zero when defined. For
3687/// vectors, return true if every demanded element is known to be non-zero when
3688/// defined. For pointers, if the context instruction and dominator tree are
3689/// specified, perform context-sensitive analysis and return true if the
3690/// pointer couldn't possibly be null at the specified instruction.
3691/// Supports values with integer or pointer type and vectors of integers.
3692bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
3693 const SimplifyQuery &Q, unsigned Depth) {
3694 Type *Ty = V->getType();
3695
3696#ifndef NDEBUG
3697 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
3698
3699 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3700 assert(
3701 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
3702 "DemandedElt width should equal the fixed vector number of elements");
3703 } else {
3704 assert(DemandedElts == APInt(1, 1) &&
3705 "DemandedElt width should be 1 for scalars");
3706 }
3707#endif
3708
3709 if (auto *C = dyn_cast<Constant>(V)) {
3710 if (C->isNullValue())
3711 return false;
3712 if (isa<ConstantInt>(C))
3713 // Must be non-zero due to null test above.
3714 return true;
3715
3716 // For constant vectors, check that all elements are poison or known
3717 // non-zero to determine that the whole vector is known non-zero.
3718 if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
3719 for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
3720 if (!DemandedElts[i])
3721 continue;
3722 Constant *Elt = C->getAggregateElement(i);
3723 if (!Elt || Elt->isNullValue())
3724 return false;
3725 if (!isa<PoisonValue>(Elt) && !isa<ConstantInt>(Elt))
3726 return false;
3727 }
3728 return true;
3729 }
3730
3731 // Constant ptrauth can be null, iff the base pointer can be.
3732 if (auto *CPA = dyn_cast<ConstantPtrAuth>(V))
3733 return isKnownNonZero(CPA->getPointer(), DemandedElts, Q, Depth);
3734
3735 // A global variable in address space 0 is non null unless extern weak
3736 // or an absolute symbol reference. Other address spaces may have null as a
3737 // valid address for a global, so we can't assume anything.
3738 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
3739 if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&
3740 GV->getType()->getAddressSpace() == 0)
3741 return true;
3742 }
3743
3744 // For constant expressions, fall through to the Operator code below.
3745 if (!isa<ConstantExpr>(V))
3746 return false;
3747 }
3748
3749 if (const auto *A = dyn_cast<Argument>(V))
3750 if (std::optional<ConstantRange> Range = A->getRange()) {
3751 const APInt ZeroValue(Range->getBitWidth(), 0);
3752 if (!Range->contains(ZeroValue))
3753 return true;
3754 }
3755
3756 if (!isa<Constant>(V) && isKnownNonZeroFromAssume(V, Q))
3757 return true;
3758
3759 // Some of the tests below are recursive, so bail out if we hit the limit.
3761 return false;
3762
3763 // Check for pointer simplifications.
3764
3765 if (PointerType *PtrTy = dyn_cast<PointerType>(Ty)) {
3766 // A byval, inalloca may not be null in a non-default addres space. A
3767 // nonnull argument is assumed never 0.
3768 if (const Argument *A = dyn_cast<Argument>(V)) {
3769 if (((A->hasPassPointeeByValueCopyAttr() &&
3770 !NullPointerIsDefined(A->getParent(), PtrTy->getAddressSpace())) ||
3771 A->hasNonNullAttr()))
3772 return true;
3773 }
3774 }
3775
3776 if (const auto *I = dyn_cast<Operator>(V))
3777 if (isKnownNonZeroFromOperator(I, DemandedElts, Q, Depth))
3778 return true;
3779
3780 if (!isa<Constant>(V) &&
3782 return true;
3783
3784 if (const Value *Stripped = stripNullTest(V))
3785 return isKnownNonZero(Stripped, DemandedElts, Q, Depth);
3786
3787 return false;
3788}
3789
3791 unsigned Depth) {
3792 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
3793 APInt DemandedElts =
3794 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
3795 return ::isKnownNonZero(V, DemandedElts, Q, Depth);
3796}
3797
3798/// If the pair of operators are the same invertible function, return the
3799/// the operands of the function corresponding to each input. Otherwise,
3800/// return std::nullopt. An invertible function is one that is 1-to-1 and maps
3801/// every input value to exactly one output value. This is equivalent to
3802/// saying that Op1 and Op2 are equal exactly when the specified pair of
3803/// operands are equal, (except that Op1 and Op2 may be poison more often.)
3804static std::optional<std::pair<Value*, Value*>>
3806 const Operator *Op2) {
3807 if (Op1->getOpcode() != Op2->getOpcode())
3808 return std::nullopt;
3809
3810 auto getOperands = [&](unsigned OpNum) -> auto {
3811 return std::make_pair(Op1->getOperand(OpNum), Op2->getOperand(OpNum));
3812 };
3813
3814 switch (Op1->getOpcode()) {
3815 default:
3816 break;
3817 case Instruction::Or:
3818 if (!cast<PossiblyDisjointInst>(Op1)->isDisjoint() ||
3819 !cast<PossiblyDisjointInst>(Op2)->isDisjoint())
3820 break;
3821 [[fallthrough]];
3822 case Instruction::Xor:
3823 case Instruction::Add: {
3824 Value *Other;
3825 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(0)), m_Value(Other))))
3826 return std::make_pair(Op1->getOperand(1), Other);
3827 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(1)), m_Value(Other))))
3828 return std::make_pair(Op1->getOperand(0), Other);
3829 break;
3830 }
3831 case Instruction::Sub:
3832 if (Op1->getOperand(0) == Op2->getOperand(0))
3833 return getOperands(1);
3834 if (Op1->getOperand(1) == Op2->getOperand(1))
3835 return getOperands(0);
3836 break;
3837 case Instruction::Mul: {
3838 // invertible if A * B == (A * B) mod 2^N where A, and B are integers
3839 // and N is the bitwdith. The nsw case is non-obvious, but proven by
3840 // alive2: https://alive2.llvm.org/ce/z/Z6D5qK
3841 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3842 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3843 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3844 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3845 break;
3846
3847 // Assume operand order has been canonicalized
3848 if (Op1->getOperand(1) == Op2->getOperand(1) &&
3849 isa<ConstantInt>(Op1->getOperand(1)) &&
3850 !cast<ConstantInt>(Op1->getOperand(1))->isZero())
3851 return getOperands(0);
3852 break;
3853 }
3854 case Instruction::Shl: {
3855 // Same as multiplies, with the difference that we don't need to check
3856 // for a non-zero multiply. Shifts always multiply by non-zero.
3857 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3858 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3859 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3860 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3861 break;
3862
3863 if (Op1->getOperand(1) == Op2->getOperand(1))
3864 return getOperands(0);
3865 break;
3866 }
3867 case Instruction::AShr:
3868 case Instruction::LShr: {
3869 auto *PEO1 = cast<PossiblyExactOperator>(Op1);
3870 auto *PEO2 = cast<PossiblyExactOperator>(Op2);
3871 if (!PEO1->isExact() || !PEO2->isExact())
3872 break;
3873
3874 if (Op1->getOperand(1) == Op2->getOperand(1))
3875 return getOperands(0);
3876 break;
3877 }
3878 case Instruction::SExt:
3879 case Instruction::ZExt:
3880 if (Op1->getOperand(0)->getType() == Op2->getOperand(0)->getType())
3881 return getOperands(0);
3882 break;
3883 case Instruction::PHI: {
3884 const PHINode *PN1 = cast<PHINode>(Op1);
3885 const PHINode *PN2 = cast<PHINode>(Op2);
3886
3887 // If PN1 and PN2 are both recurrences, can we prove the entire recurrences
3888 // are a single invertible function of the start values? Note that repeated
3889 // application of an invertible function is also invertible
3890 BinaryOperator *BO1 = nullptr;
3891 Value *Start1 = nullptr, *Step1 = nullptr;
3892 BinaryOperator *BO2 = nullptr;
3893 Value *Start2 = nullptr, *Step2 = nullptr;
3894 if (PN1->getParent() != PN2->getParent() ||
3895 !matchSimpleRecurrence(PN1, BO1, Start1, Step1) ||
3896 !matchSimpleRecurrence(PN2, BO2, Start2, Step2))
3897 break;
3898
3899 auto Values = getInvertibleOperands(cast<Operator>(BO1),
3900 cast<Operator>(BO2));
3901 if (!Values)
3902 break;
3903
3904 // We have to be careful of mutually defined recurrences here. Ex:
3905 // * X_i = X_(i-1) OP Y_(i-1), and Y_i = X_(i-1) OP V
3906 // * X_i = Y_i = X_(i-1) OP Y_(i-1)
3907 // The invertibility of these is complicated, and not worth reasoning
3908 // about (yet?).
3909 if (Values->first != PN1 || Values->second != PN2)
3910 break;
3911
3912 return std::make_pair(Start1, Start2);
3913 }
3914 }
3915 return std::nullopt;
3916}
3917
3918/// Return true if V1 == (binop V2, X), where X is known non-zero.
3919/// Only handle a small subset of binops where (binop V2, X) with non-zero X
3920/// implies V2 != V1.
3921static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2,
3922 const APInt &DemandedElts,
3923 const SimplifyQuery &Q, unsigned Depth) {
3925 if (!BO)
3926 return false;
3927 switch (BO->getOpcode()) {
3928 default:
3929 break;
3930 case Instruction::Or:
3931 if (!cast<PossiblyDisjointInst>(V1)->isDisjoint())
3932 break;
3933 [[fallthrough]];
3934 case Instruction::Xor:
3935 case Instruction::Add:
3936 Value *Op = nullptr;
3937 if (V2 == BO->getOperand(0))
3938 Op = BO->getOperand(1);
3939 else if (V2 == BO->getOperand(1))
3940 Op = BO->getOperand(0);
3941 else
3942 return false;
3943 return isKnownNonZero(Op, DemandedElts, Q, Depth + 1);
3944 }
3945 return false;
3946}
3947
3948/// Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and
3949/// the multiplication is nuw or nsw.
3950static bool isNonEqualMul(const Value *V1, const Value *V2,
3951 const APInt &DemandedElts, const SimplifyQuery &Q,
3952 unsigned Depth) {
3953 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3954 const APInt *C;
3955 return match(OBO, m_Mul(m_Specific(V1), m_APInt(C))) &&
3956 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3957 !C->isZero() && !C->isOne() &&
3958 isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3959 }
3960 return false;
3961}
3962
3963/// Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and
3964/// the shift is nuw or nsw.
3965static bool isNonEqualShl(const Value *V1, const Value *V2,
3966 const APInt &DemandedElts, const SimplifyQuery &Q,
3967 unsigned Depth) {
3968 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3969 const APInt *C;
3970 return match(OBO, m_Shl(m_Specific(V1), m_APInt(C))) &&
3971 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3972 !C->isZero() && isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3973 }
3974 return false;
3975}
3976
3977static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2,
3978 const APInt &DemandedElts, const SimplifyQuery &Q,
3979 unsigned Depth) {
3980 // Check two PHIs are in same block.
3981 if (PN1->getParent() != PN2->getParent())
3982 return false;
3983
3985 bool UsedFullRecursion = false;
3986 for (const BasicBlock *IncomBB : PN1->blocks()) {
3987 if (!VisitedBBs.insert(IncomBB).second)
3988 continue; // Don't reprocess blocks that we have dealt with already.
3989 const Value *IV1 = PN1->getIncomingValueForBlock(IncomBB);
3990 const Value *IV2 = PN2->getIncomingValueForBlock(IncomBB);
3991 const APInt *C1, *C2;
3992 if (match(IV1, m_APInt(C1)) && match(IV2, m_APInt(C2)) && *C1 != *C2)
3993 continue;
3994
3995 // Only one pair of phi operands is allowed for full recursion.
3996 if (UsedFullRecursion)
3997 return false;
3998
4000 RecQ.CxtI = IncomBB->getTerminator();
4001 if (!isKnownNonEqual(IV1, IV2, DemandedElts, RecQ, Depth + 1))
4002 return false;
4003 UsedFullRecursion = true;
4004 }
4005 return true;
4006}
4007
4008static bool isNonEqualSelect(const Value *V1, const Value *V2,
4009 const APInt &DemandedElts, const SimplifyQuery &Q,
4010 unsigned Depth) {
4011 const SelectInst *SI1 = dyn_cast<SelectInst>(V1);
4012 if (!SI1)
4013 return false;
4014
4015 if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2)) {
4016 const Value *Cond1 = SI1->getCondition();
4017 const Value *Cond2 = SI2->getCondition();
4018 if (Cond1 == Cond2)
4019 return isKnownNonEqual(SI1->getTrueValue(), SI2->getTrueValue(),
4020 DemandedElts, Q, Depth + 1) &&
4021 isKnownNonEqual(SI1->getFalseValue(), SI2->getFalseValue(),
4022 DemandedElts, Q, Depth + 1);
4023 }
4024 return isKnownNonEqual(SI1->getTrueValue(), V2, DemandedElts, Q, Depth + 1) &&
4025 isKnownNonEqual(SI1->getFalseValue(), V2, DemandedElts, Q, Depth + 1);
4026}
4027
4028// Check to see if A is both a GEP and is the incoming value for a PHI in the
4029// loop, and B is either a ptr or another GEP. If the PHI has 2 incoming values,
4030// one of them being the recursive GEP A and the other a ptr at same base and at
4031// the same/higher offset than B we are only incrementing the pointer further in
4032// loop if offset of recursive GEP is greater than 0.
4034 const SimplifyQuery &Q) {
4035 if (!A->getType()->isPointerTy() || !B->getType()->isPointerTy())
4036 return false;
4037
4038 auto *GEPA = dyn_cast<GEPOperator>(A);
4039 if (!GEPA || GEPA->getNumIndices() != 1 || !isa<Constant>(GEPA->idx_begin()))
4040 return false;
4041
4042 // Handle 2 incoming PHI values with one being a recursive GEP.
4043 auto *PN = dyn_cast<PHINode>(GEPA->getPointerOperand());
4044 if (!PN || PN->getNumIncomingValues() != 2)
4045 return false;
4046
4047 // Search for the recursive GEP as an incoming operand, and record that as
4048 // Step.
4049 Value *Start = nullptr;
4050 Value *Step = const_cast<Value *>(A);
4051 if (PN->getIncomingValue(0) == Step)
4052 Start = PN->getIncomingValue(1);
4053 else if (PN->getIncomingValue(1) == Step)
4054 Start = PN->getIncomingValue(0);
4055 else
4056 return false;
4057
4058 // Other incoming node base should match the B base.
4059 // StartOffset >= OffsetB && StepOffset > 0?
4060 // StartOffset <= OffsetB && StepOffset < 0?
4061 // Is non-equal if above are true.
4062 // We use stripAndAccumulateInBoundsConstantOffsets to restrict the
4063 // optimisation to inbounds GEPs only.
4064 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(Start->getType());
4065 APInt StartOffset(IndexWidth, 0);
4066 Start = Start->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StartOffset);
4067 APInt StepOffset(IndexWidth, 0);
4068 Step = Step->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StepOffset);
4069
4070 // Check if Base Pointer of Step matches the PHI.
4071 if (Step != PN)
4072 return false;
4073 APInt OffsetB(IndexWidth, 0);
4074 B = B->stripAndAccumulateInBoundsConstantOffsets(Q.DL, OffsetB);
4075 return Start == B &&
4076 ((StartOffset.sge(OffsetB) && StepOffset.isStrictlyPositive()) ||
4077 (StartOffset.sle(OffsetB) && StepOffset.isNegative()));
4078}
4079
4080static bool isKnownNonEqualFromContext(const Value *V1, const Value *V2,
4081 const SimplifyQuery &Q, unsigned Depth) {
4082 if (!Q.CxtI)
4083 return false;
4084
4085 // Try to infer NonEqual based on information from dominating conditions.
4086 if (Q.DC && Q.DT) {
4087 auto IsKnownNonEqualFromDominatingCondition = [&](const Value *V) {
4088 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
4089 Value *Cond = BI->getCondition();
4090 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4091 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()) &&
4093 /*LHSIsTrue=*/true, Depth)
4094 .value_or(false))
4095 return true;
4096
4097 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4098 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()) &&
4100 /*LHSIsTrue=*/false, Depth)
4101 .value_or(false))
4102 return true;
4103 }
4104
4105 return false;
4106 };
4107
4108 if (IsKnownNonEqualFromDominatingCondition(V1) ||
4109 IsKnownNonEqualFromDominatingCondition(V2))
4110 return true;
4111 }
4112
4113 if (!Q.AC)
4114 return false;
4115
4116 // Try to infer NonEqual based on information from assumptions.
4117 for (auto &AssumeVH : Q.AC->assumptionsFor(V1)) {
4118 if (!AssumeVH)
4119 continue;
4120 CallInst *I = cast<CallInst>(AssumeVH);
4121
4122 assert(I->getFunction() == Q.CxtI->getFunction() &&
4123 "Got assumption for the wrong function!");
4124 assert(I->getIntrinsicID() == Intrinsic::assume &&
4125 "must be an assume intrinsic");
4126
4127 if (isImpliedCondition(I->getArgOperand(0), ICmpInst::ICMP_NE, V1, V2, Q.DL,
4128 /*LHSIsTrue=*/true, Depth)
4129 .value_or(false) &&
4131 return true;
4132 }
4133
4134 return false;
4135}
4136
4137/// Return true if it is known that V1 != V2.
4138static bool isKnownNonEqual(const Value *V1, const Value *V2,
4139 const APInt &DemandedElts, const SimplifyQuery &Q,
4140 unsigned Depth) {
4141 if (V1 == V2)
4142 return false;
4143 if (V1->getType() != V2->getType())
4144 // We can't look through casts yet.
4145 return false;
4146
4148 return false;
4149
4150 // See if we can recurse through (exactly one of) our operands. This
4151 // requires our operation be 1-to-1 and map every input value to exactly
4152 // one output value. Such an operation is invertible.
4153 auto *O1 = dyn_cast<Operator>(V1);
4154 auto *O2 = dyn_cast<Operator>(V2);
4155 if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) {
4156 if (auto Values = getInvertibleOperands(O1, O2))
4157 return isKnownNonEqual(Values->first, Values->second, DemandedElts, Q,
4158 Depth + 1);
4159
4160 if (const PHINode *PN1 = dyn_cast<PHINode>(V1)) {
4161 const PHINode *PN2 = cast<PHINode>(V2);
4162 // FIXME: This is missing a generalization to handle the case where one is
4163 // a PHI and another one isn't.
4164 if (isNonEqualPHIs(PN1, PN2, DemandedElts, Q, Depth))
4165 return true;
4166 };
4167 }
4168
4169 if (isModifyingBinopOfNonZero(V1, V2, DemandedElts, Q, Depth) ||
4170 isModifyingBinopOfNonZero(V2, V1, DemandedElts, Q, Depth))
4171 return true;
4172
4173 if (isNonEqualMul(V1, V2, DemandedElts, Q, Depth) ||
4174 isNonEqualMul(V2, V1, DemandedElts, Q, Depth))
4175 return true;
4176
4177 if (isNonEqualShl(V1, V2, DemandedElts, Q, Depth) ||
4178 isNonEqualShl(V2, V1, DemandedElts, Q, Depth))
4179 return true;
4180
4181 if (V1->getType()->isIntOrIntVectorTy()) {
4182 // Are any known bits in V1 contradictory to known bits in V2? If V1
4183 // has a known zero where V2 has a known one, they must not be equal.
4184 KnownBits Known1 = computeKnownBits(V1, DemandedElts, Q, Depth);
4185 if (!Known1.isUnknown()) {
4186 KnownBits Known2 = computeKnownBits(V2, DemandedElts, Q, Depth);
4187 if (Known1.Zero.intersects(Known2.One) ||
4188 Known2.Zero.intersects(Known1.One))
4189 return true;
4190 }
4191 }
4192
4193 if (isNonEqualSelect(V1, V2, DemandedElts, Q, Depth) ||
4194 isNonEqualSelect(V2, V1, DemandedElts, Q, Depth))
4195 return true;
4196
4197 if (isNonEqualPointersWithRecursiveGEP(V1, V2, Q) ||
4199 return true;
4200
4201 Value *A, *B;
4202 // PtrToInts are NonEqual if their Ptrs are NonEqual.
4203 // Check PtrToInt type matches the pointer size.
4204 if (match(V1, m_PtrToIntSameSize(Q.DL, m_Value(A))) &&
4206 return isKnownNonEqual(A, B, DemandedElts, Q, Depth + 1);
4207
4208 if (isKnownNonEqualFromContext(V1, V2, Q, Depth))
4209 return true;
4210
4211 return false;
4212}
4213
4214/// For vector constants, loop over the elements and find the constant with the
4215/// minimum number of sign bits. Return 0 if the value is not a vector constant
4216/// or if any element was not analyzed; otherwise, return the count for the
4217/// element with the minimum number of sign bits.
4219 const APInt &DemandedElts,
4220 unsigned TyBits) {
4221 const auto *CV = dyn_cast<Constant>(V);
4222 if (!CV || !isa<FixedVectorType>(CV->getType()))
4223 return 0;
4224
4225 unsigned MinSignBits = TyBits;
4226 unsigned NumElts = cast<FixedVectorType>(CV->getType())->getNumElements();
4227 for (unsigned i = 0; i != NumElts; ++i) {
4228 if (!DemandedElts[i])
4229 continue;
4230 // If we find a non-ConstantInt, bail out.
4231 auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));
4232 if (!Elt)
4233 return 0;
4234
4235 MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());
4236 }
4237
4238 return MinSignBits;
4239}
4240
4241static unsigned ComputeNumSignBitsImpl(const Value *V,
4242 const APInt &DemandedElts,
4243 const SimplifyQuery &Q, unsigned Depth);
4244
4245static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
4246 const SimplifyQuery &Q, unsigned Depth) {
4247 unsigned Result = ComputeNumSignBitsImpl(V, DemandedElts, Q, Depth);
4248 assert(Result > 0 && "At least one sign bit needs to be present!");
4249 return Result;
4250}
4251
4252/// Return the number of times the sign bit of the register is replicated into
4253/// the other bits. We know that at least 1 bit is always equal to the sign bit
4254/// (itself), but other cases can give us information. For example, immediately
4255/// after an "ashr X, 2", we know that the top 3 bits are all equal to each
4256/// other, so we return 3. For vectors, return the number of sign bits for the
4257/// vector element with the minimum number of known sign bits of the demanded
4258/// elements in the vector specified by DemandedElts.
4259static unsigned ComputeNumSignBitsImpl(const Value *V,
4260 const APInt &DemandedElts,
4261 const SimplifyQuery &Q, unsigned Depth) {
4262 Type *Ty = V->getType();
4263#ifndef NDEBUG
4264 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4265
4266 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
4267 assert(
4268 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
4269 "DemandedElt width should equal the fixed vector number of elements");
4270 } else {
4271 assert(DemandedElts == APInt(1, 1) &&
4272 "DemandedElt width should be 1 for scalars");
4273 }
4274#endif
4275
4276 // We return the minimum number of sign bits that are guaranteed to be present
4277 // in V, so for undef we have to conservatively return 1. We don't have the
4278 // same behavior for poison though -- that's a FIXME today.
4279
4280 Type *ScalarTy = Ty->getScalarType();
4281 unsigned TyBits = ScalarTy->isPointerTy() ?
4282 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
4283 Q.DL.getTypeSizeInBits(ScalarTy);
4284
4285 unsigned Tmp, Tmp2;
4286 unsigned FirstAnswer = 1;
4287
4288 // Note that ConstantInt is handled by the general computeKnownBits case
4289 // below.
4290
4292 return 1;
4293
4294 if (auto *U = dyn_cast<Operator>(V)) {
4295 switch (Operator::getOpcode(V)) {
4296 default: break;
4297 case Instruction::BitCast: {
4298 Value *Src = U->getOperand(0);
4299 Type *SrcTy = Src->getType();
4300
4301 // Skip if the source type is not an integer or integer vector type
4302 // This ensures we only process integer-like types
4303 if (!SrcTy->isIntOrIntVectorTy())
4304 break;
4305
4306 unsigned SrcBits = SrcTy->getScalarSizeInBits();
4307
4308 // Bitcast 'large element' scalar/vector to 'small element' vector.
4309 if ((SrcBits % TyBits) != 0)
4310 break;
4311
4312 // Only proceed if the destination type is a fixed-size vector
4313 if (isa<FixedVectorType>(Ty)) {
4314 // Fast case - sign splat can be simply split across the small elements.
4315 // This works for both vector and scalar sources
4316 Tmp = ComputeNumSignBits(Src, Q, Depth + 1);
4317 if (Tmp == SrcBits)
4318 return TyBits;
4319 }
4320 break;
4321 }
4322 case Instruction::SExt:
4323 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
4324 return ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1) +
4325 Tmp;
4326
4327 case Instruction::SDiv: {
4328 const APInt *Denominator;
4329 // sdiv X, C -> adds log(C) sign bits.
4330 if (match(U->getOperand(1), m_APInt(Denominator))) {
4331
4332 // Ignore non-positive denominator.
4333 if (!Denominator->isStrictlyPositive())
4334 break;
4335
4336 // Calculate the incoming numerator bits.
4337 unsigned NumBits =
4338 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4339
4340 // Add floor(log(C)) bits to the numerator bits.
4341 return std::min(TyBits, NumBits + Denominator->logBase2());
4342 }
4343 break;
4344 }
4345
4346 case Instruction::SRem: {
4347 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4348
4349 const APInt *Denominator;
4350 // srem X, C -> we know that the result is within [-C+1,C) when C is a
4351 // positive constant. This let us put a lower bound on the number of sign
4352 // bits.
4353 if (match(U->getOperand(1), m_APInt(Denominator))) {
4354
4355 // Ignore non-positive denominator.
4356 if (Denominator->isStrictlyPositive()) {
4357 // Calculate the leading sign bit constraints by examining the
4358 // denominator. Given that the denominator is positive, there are two
4359 // cases:
4360 //
4361 // 1. The numerator is positive. The result range is [0,C) and
4362 // [0,C) u< (1 << ceilLogBase2(C)).
4363 //
4364 // 2. The numerator is negative. Then the result range is (-C,0] and
4365 // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
4366 //
4367 // Thus a lower bound on the number of sign bits is `TyBits -
4368 // ceilLogBase2(C)`.
4369
4370 unsigned ResBits = TyBits - Denominator->ceilLogBase2();
4371 Tmp = std::max(Tmp, ResBits);
4372 }
4373 }
4374 return Tmp;
4375 }
4376
4377 case Instruction::AShr: {
4378 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4379 // ashr X, C -> adds C sign bits. Vectors too.
4380 const APInt *ShAmt;
4381 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4382 if (ShAmt->uge(TyBits))
4383 break; // Bad shift.
4384 unsigned ShAmtLimited = ShAmt->getZExtValue();
4385 Tmp += ShAmtLimited;
4386 if (Tmp > TyBits) Tmp = TyBits;
4387 }
4388 return Tmp;
4389 }
4390 case Instruction::Shl: {
4391 const APInt *ShAmt;
4392 Value *X = nullptr;
4393 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4394 // shl destroys sign bits.
4395 if (ShAmt->uge(TyBits))
4396 break; // Bad shift.
4397 // We can look through a zext (more or less treating it as a sext) if
4398 // all extended bits are shifted out.
4399 if (match(U->getOperand(0), m_ZExt(m_Value(X))) &&
4400 ShAmt->uge(TyBits - X->getType()->getScalarSizeInBits())) {
4401 Tmp = ComputeNumSignBits(X, DemandedElts, Q, Depth + 1);
4402 Tmp += TyBits - X->getType()->getScalarSizeInBits();
4403 } else
4404 Tmp =
4405 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4406 if (ShAmt->uge(Tmp))
4407 break; // Shifted all sign bits out.
4408 Tmp2 = ShAmt->getZExtValue();
4409 return Tmp - Tmp2;
4410 }
4411 break;
4412 }
4413 case Instruction::And:
4414 case Instruction::Or:
4415 case Instruction::Xor: // NOT is handled here.
4416 // Logical binary ops preserve the number of sign bits at the worst.
4417 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4418 if (Tmp != 1) {
4419 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4420 FirstAnswer = std::min(Tmp, Tmp2);
4421 // We computed what we know about the sign bits as our first
4422 // answer. Now proceed to the generic code that uses
4423 // computeKnownBits, and pick whichever answer is better.
4424 }
4425 break;
4426
4427 case Instruction::Select: {
4428 // If we have a clamp pattern, we know that the number of sign bits will
4429 // be the minimum of the clamp min/max range.
4430 const Value *X;
4431 const APInt *CLow, *CHigh;
4432 if (isSignedMinMaxClamp(U, X, CLow, CHigh))
4433 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4434
4435 Tmp = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4436 if (Tmp == 1)
4437 break;
4438 Tmp2 = ComputeNumSignBits(U->getOperand(2), DemandedElts, Q, Depth + 1);
4439 return std::min(Tmp, Tmp2);
4440 }
4441
4442 case Instruction::Add:
4443 // Add can have at most one carry bit. Thus we know that the output
4444 // is, at worst, one more bit than the inputs.
4445 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4446 if (Tmp == 1) break;
4447
4448 // Special case decrementing a value (ADD X, -1):
4449 if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
4450 if (CRHS->isAllOnesValue()) {
4451 KnownBits Known(TyBits);
4452 computeKnownBits(U->getOperand(0), DemandedElts, Known, Q, Depth + 1);
4453
4454 // If the input is known to be 0 or 1, the output is 0/-1, which is
4455 // all sign bits set.
4456 if ((Known.Zero | 1).isAllOnes())
4457 return TyBits;
4458
4459 // If we are subtracting one from a positive number, there is no carry
4460 // out of the result.
4461 if (Known.isNonNegative())
4462 return Tmp;
4463 }
4464
4465 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4466 if (Tmp2 == 1)
4467 break;
4468 return std::min(Tmp, Tmp2) - 1;
4469
4470 case Instruction::Sub:
4471 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4472 if (Tmp2 == 1)
4473 break;
4474
4475 // Handle NEG.
4476 if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
4477 if (CLHS->isNullValue()) {
4478 KnownBits Known(TyBits);
4479 computeKnownBits(U->getOperand(1), DemandedElts, Known, Q, Depth + 1);
4480 // If the input is known to be 0 or 1, the output is 0/-1, which is
4481 // all sign bits set.
4482 if ((Known.Zero | 1).isAllOnes())
4483 return TyBits;
4484
4485 // If the input is known to be positive (the sign bit is known clear),
4486 // the output of the NEG has the same number of sign bits as the
4487 // input.
4488 if (Known.isNonNegative())
4489 return Tmp2;
4490
4491 // Otherwise, we treat this like a SUB.
4492 }
4493
4494 // Sub can have at most one carry bit. Thus we know that the output
4495 // is, at worst, one more bit than the inputs.
4496 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4497 if (Tmp == 1)
4498 break;
4499 return std::min(Tmp, Tmp2) - 1;
4500
4501 case Instruction::Mul: {
4502 // The output of the Mul can be at most twice the valid bits in the
4503 // inputs.
4504 unsigned SignBitsOp0 =
4505 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4506 if (SignBitsOp0 == 1)
4507 break;
4508 unsigned SignBitsOp1 =
4509 ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4510 if (SignBitsOp1 == 1)
4511 break;
4512 unsigned OutValidBits =
4513 (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);
4514 return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
4515 }
4516
4517 case Instruction::PHI: {
4518 const PHINode *PN = cast<PHINode>(U);
4519 unsigned NumIncomingValues = PN->getNumIncomingValues();
4520 // Don't analyze large in-degree PHIs.
4521 if (NumIncomingValues > 4) break;
4522 // Unreachable blocks may have zero-operand PHI nodes.
4523 if (NumIncomingValues == 0) break;
4524
4525 // Take the minimum of all incoming values. This can't infinitely loop
4526 // because of our depth threshold.
4528 Tmp = TyBits;
4529 for (unsigned i = 0, e = NumIncomingValues; i != e; ++i) {
4530 if (Tmp == 1) return Tmp;
4531 RecQ.CxtI = PN->getIncomingBlock(i)->getTerminator();
4532 Tmp = std::min(Tmp, ComputeNumSignBits(PN->getIncomingValue(i),
4533 DemandedElts, RecQ, Depth + 1));
4534 }
4535 return Tmp;
4536 }
4537
4538 case Instruction::Trunc: {
4539 // If the input contained enough sign bits that some remain after the
4540 // truncation, then we can make use of that. Otherwise we don't know
4541 // anything.
4542 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4543 unsigned OperandTyBits = U->getOperand(0)->getType()->getScalarSizeInBits();
4544 if (Tmp > (OperandTyBits - TyBits))
4545 return Tmp - (OperandTyBits - TyBits);
4546
4547 return 1;
4548 }
4549
4550 case Instruction::ExtractElement:
4551 // Look through extract element. At the moment we keep this simple and
4552 // skip tracking the specific element. But at least we might find
4553 // information valid for all elements of the vector (for example if vector
4554 // is sign extended, shifted, etc).
4555 return ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4556
4557 case Instruction::ShuffleVector: {
4558 // Collect the minimum number of sign bits that are shared by every vector
4559 // element referenced by the shuffle.
4560 auto *Shuf = dyn_cast<ShuffleVectorInst>(U);
4561 if (!Shuf) {
4562 // FIXME: Add support for shufflevector constant expressions.
4563 return 1;
4564 }
4565 APInt DemandedLHS, DemandedRHS;
4566 // For undef elements, we don't know anything about the common state of
4567 // the shuffle result.
4568 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
4569 return 1;
4570 Tmp = std::numeric_limits<unsigned>::max();
4571 if (!!DemandedLHS) {
4572 const Value *LHS = Shuf->getOperand(0);
4573 Tmp = ComputeNumSignBits(LHS, DemandedLHS, Q, Depth + 1);
4574 }
4575 // If we don't know anything, early out and try computeKnownBits
4576 // fall-back.
4577 if (Tmp == 1)
4578 break;
4579 if (!!DemandedRHS) {
4580 const Value *RHS = Shuf->getOperand(1);
4581 Tmp2 = ComputeNumSignBits(RHS, DemandedRHS, Q, Depth + 1);
4582 Tmp = std::min(Tmp, Tmp2);
4583 }
4584 // If we don't know anything, early out and try computeKnownBits
4585 // fall-back.
4586 if (Tmp == 1)
4587 break;
4588 assert(Tmp <= TyBits && "Failed to determine minimum sign bits");
4589 return Tmp;
4590 }
4591 case Instruction::Call: {
4592 if (const auto *II = dyn_cast<IntrinsicInst>(U)) {
4593 switch (II->getIntrinsicID()) {
4594 default:
4595 break;
4596 case Intrinsic::abs:
4597 Tmp =
4598 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4599 if (Tmp == 1)
4600 break;
4601
4602 // Absolute value reduces number of sign bits by at most 1.
4603 return Tmp - 1;
4604 case Intrinsic::smin:
4605 case Intrinsic::smax: {
4606 const APInt *CLow, *CHigh;
4607 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
4608 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4609 }
4610 }
4611 }
4612 }
4613 }
4614 }
4615
4616 // Finally, if we can prove that the top bits of the result are 0's or 1's,
4617 // use this information.
4618
4619 // If we can examine all elements of a vector constant successfully, we're
4620 // done (we can't do any better than that). If not, keep trying.
4621 if (unsigned VecSignBits =
4622 computeNumSignBitsVectorConstant(V, DemandedElts, TyBits))
4623 return VecSignBits;
4624
4625 KnownBits Known(TyBits);
4626 computeKnownBits(V, DemandedElts, Known, Q, Depth);
4627
4628 // If we know that the sign bit is either zero or one, determine the number of
4629 // identical bits in the top of the input value.
4630 return std::max(FirstAnswer, Known.countMinSignBits());
4631}
4632
4634 const TargetLibraryInfo *TLI) {
4635 const Function *F = CB.getCalledFunction();
4636 if (!F)
4638
4639 if (F->isIntrinsic())
4640 return F->getIntrinsicID();
4641
4642 // We are going to infer semantics of a library function based on mapping it
4643 // to an LLVM intrinsic. Check that the library function is available from
4644 // this callbase and in this environment.
4645 LibFunc Func;
4646 if (F->hasLocalLinkage() || !TLI || !TLI->getLibFunc(CB, Func) ||
4647 !CB.onlyReadsMemory())
4649
4650 switch (Func) {
4651 default:
4652 break;
4653 case LibFunc_sin:
4654 case LibFunc_sinf:
4655 case LibFunc_sinl:
4656 return Intrinsic::sin;
4657 case LibFunc_cos:
4658 case LibFunc_cosf:
4659 case LibFunc_cosl:
4660 return Intrinsic::cos;
4661 case LibFunc_tan:
4662 case LibFunc_tanf:
4663 case LibFunc_tanl:
4664 return Intrinsic::tan;
4665 case LibFunc_asin:
4666 case LibFunc_asinf:
4667 case LibFunc_asinl:
4668 return Intrinsic::asin;
4669 case LibFunc_acos:
4670 case LibFunc_acosf:
4671 case LibFunc_acosl:
4672 return Intrinsic::acos;
4673 case LibFunc_atan:
4674 case LibFunc_atanf:
4675 case LibFunc_atanl:
4676 return Intrinsic::atan;
4677 case LibFunc_atan2:
4678 case LibFunc_atan2f:
4679 case LibFunc_atan2l:
4680 return Intrinsic::atan2;
4681 case LibFunc_sinh:
4682 case LibFunc_sinhf:
4683 case LibFunc_sinhl:
4684 return Intrinsic::sinh;
4685 case LibFunc_cosh:
4686 case LibFunc_coshf:
4687 case LibFunc_coshl:
4688 return Intrinsic::cosh;
4689 case LibFunc_tanh:
4690 case LibFunc_tanhf:
4691 case LibFunc_tanhl:
4692 return Intrinsic::tanh;
4693 case LibFunc_exp:
4694 case LibFunc_expf:
4695 case LibFunc_expl:
4696 return Intrinsic::exp;
4697 case LibFunc_exp2:
4698 case LibFunc_exp2f:
4699 case LibFunc_exp2l:
4700 return Intrinsic::exp2;
4701 case LibFunc_exp10:
4702 case LibFunc_exp10f:
4703 case LibFunc_exp10l:
4704 return Intrinsic::exp10;
4705 case LibFunc_log:
4706 case LibFunc_logf:
4707 case LibFunc_logl:
4708 return Intrinsic::log;
4709 case LibFunc_log10:
4710 case LibFunc_log10f:
4711 case LibFunc_log10l:
4712 return Intrinsic::log10;
4713 case LibFunc_log2:
4714 case LibFunc_log2f:
4715 case LibFunc_log2l:
4716 return Intrinsic::log2;
4717 case LibFunc_fabs:
4718 case LibFunc_fabsf:
4719 case LibFunc_fabsl:
4720 return Intrinsic::fabs;
4721 case LibFunc_fmin:
4722 case LibFunc_fminf:
4723 case LibFunc_fminl:
4724 return Intrinsic::minnum;
4725 case LibFunc_fmax:
4726 case LibFunc_fmaxf:
4727 case LibFunc_fmaxl:
4728 return Intrinsic::maxnum;
4729 case LibFunc_copysign:
4730 case LibFunc_copysignf:
4731 case LibFunc_copysignl:
4732 return Intrinsic::copysign;
4733 case LibFunc_floor:
4734 case LibFunc_floorf:
4735 case LibFunc_floorl:
4736 return Intrinsic::floor;
4737 case LibFunc_ceil:
4738 case LibFunc_ceilf:
4739 case LibFunc_ceill:
4740 return Intrinsic::ceil;
4741 case LibFunc_trunc:
4742 case LibFunc_truncf:
4743 case LibFunc_truncl:
4744 return Intrinsic::trunc;
4745 case LibFunc_rint:
4746 case LibFunc_rintf:
4747 case LibFunc_rintl:
4748 return Intrinsic::rint;
4749 case LibFunc_nearbyint:
4750 case LibFunc_nearbyintf:
4751 case LibFunc_nearbyintl:
4752 return Intrinsic::nearbyint;
4753 case LibFunc_round:
4754 case LibFunc_roundf:
4755 case LibFunc_roundl:
4756 return Intrinsic::round;
4757 case LibFunc_roundeven:
4758 case LibFunc_roundevenf:
4759 case LibFunc_roundevenl:
4760 return Intrinsic::roundeven;
4761 case LibFunc_pow:
4762 case LibFunc_powf:
4763 case LibFunc_powl:
4764 return Intrinsic::pow;
4765 case LibFunc_sqrt:
4766 case LibFunc_sqrtf:
4767 case LibFunc_sqrtl:
4768 return Intrinsic::sqrt;
4769 }
4770
4772}
4773
4774/// Given an exploded icmp instruction, return true if the comparison only
4775/// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if
4776/// the result of the comparison is true when the input value is signed.
4778 bool &TrueIfSigned) {
4779 switch (Pred) {
4780 case ICmpInst::ICMP_SLT: // True if LHS s< 0
4781 TrueIfSigned = true;
4782 return RHS.isZero();
4783 case ICmpInst::ICMP_SLE: // True if LHS s<= -1
4784 TrueIfSigned = true;
4785 return RHS.isAllOnes();
4786 case ICmpInst::ICMP_SGT: // True if LHS s> -1
4787 TrueIfSigned = false;
4788 return RHS.isAllOnes();
4789 case ICmpInst::ICMP_SGE: // True if LHS s>= 0
4790 TrueIfSigned = false;
4791 return RHS.isZero();
4792 case ICmpInst::ICMP_UGT:
4793 // True if LHS u> RHS and RHS == sign-bit-mask - 1
4794 TrueIfSigned = true;
4795 return RHS.isMaxSignedValue();
4796 case ICmpInst::ICMP_UGE:
4797 // True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4798 TrueIfSigned = true;
4799 return RHS.isMinSignedValue();
4800 case ICmpInst::ICMP_ULT:
4801 // True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4802 TrueIfSigned = false;
4803 return RHS.isMinSignedValue();
4804 case ICmpInst::ICMP_ULE:
4805 // True if LHS u<= RHS and RHS == sign-bit-mask - 1
4806 TrueIfSigned = false;
4807 return RHS.isMaxSignedValue();
4808 default:
4809 return false;
4810 }
4811}
4812
4814 bool CondIsTrue,
4815 const Instruction *CxtI,
4816 KnownFPClass &KnownFromContext,
4817 unsigned Depth = 0) {
4818 Value *A, *B;
4820 (CondIsTrue ? match(Cond, m_LogicalAnd(m_Value(A), m_Value(B)))
4821 : match(Cond, m_LogicalOr(m_Value(A), m_Value(B))))) {
4822 computeKnownFPClassFromCond(V, A, CondIsTrue, CxtI, KnownFromContext,
4823 Depth + 1);
4824 computeKnownFPClassFromCond(V, B, CondIsTrue, CxtI, KnownFromContext,
4825 Depth + 1);
4826 return;
4827 }
4829 computeKnownFPClassFromCond(V, A, !CondIsTrue, CxtI, KnownFromContext,
4830 Depth + 1);
4831 return;
4832 }
4833 CmpPredicate Pred;
4834 Value *LHS;
4835 uint64_t ClassVal = 0;
4836 const APFloat *CRHS;
4837 const APInt *RHS;
4838 if (match(Cond, m_FCmp(Pred, m_Value(LHS), m_APFloat(CRHS)))) {
4839 auto [CmpVal, MaskIfTrue, MaskIfFalse] = fcmpImpliesClass(
4840 Pred, *cast<Instruction>(Cond)->getParent()->getParent(), LHS, *CRHS,
4841 LHS != V);
4842 if (CmpVal == V)
4843 KnownFromContext.knownNot(~(CondIsTrue ? MaskIfTrue : MaskIfFalse));
4845 m_Specific(V), m_ConstantInt(ClassVal)))) {
4846 FPClassTest Mask = static_cast<FPClassTest>(ClassVal);
4847 KnownFromContext.knownNot(CondIsTrue ? ~Mask : Mask);
4848 } else if (match(Cond, m_ICmp(Pred, m_ElementWiseBitCast(m_Specific(V)),
4849 m_APInt(RHS)))) {
4850 bool TrueIfSigned;
4851 if (!isSignBitCheck(Pred, *RHS, TrueIfSigned))
4852 return;
4853 if (TrueIfSigned == CondIsTrue)
4854 KnownFromContext.signBitMustBeOne();
4855 else
4856 KnownFromContext.signBitMustBeZero();
4857 }
4858}
4859
4861 const SimplifyQuery &Q) {
4862 KnownFPClass KnownFromContext;
4863
4864 if (Q.CC && Q.CC->AffectedValues.contains(V))
4866 KnownFromContext);
4867
4868 if (!Q.CxtI)
4869 return KnownFromContext;
4870
4871 if (Q.DC && Q.DT) {
4872 // Handle dominating conditions.
4873 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
4874 Value *Cond = BI->getCondition();
4875
4876 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4877 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
4878 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/true, Q.CxtI,
4879 KnownFromContext);
4880
4881 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4882 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
4883 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/false, Q.CxtI,
4884 KnownFromContext);
4885 }
4886 }
4887
4888 if (!Q.AC)
4889 return KnownFromContext;
4890
4891 // Try to restrict the floating-point classes based on information from
4892 // assumptions.
4893 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
4894 if (!AssumeVH)
4895 continue;
4896 CallInst *I = cast<CallInst>(AssumeVH);
4897
4898 assert(I->getFunction() == Q.CxtI->getParent()->getParent() &&
4899 "Got assumption for the wrong function!");
4900 assert(I->getIntrinsicID() == Intrinsic::assume &&
4901 "must be an assume intrinsic");
4902
4903 if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
4904 continue;
4905
4906 computeKnownFPClassFromCond(V, I->getArgOperand(0),
4907 /*CondIsTrue=*/true, Q.CxtI, KnownFromContext);
4908 }
4909
4910 return KnownFromContext;
4911}
4912
4914 Value *Arm, bool Invert,
4915 const SimplifyQuery &SQ,
4916 unsigned Depth) {
4917
4918 KnownFPClass KnownSrc;
4920 /*CondIsTrue=*/!Invert, SQ.CxtI, KnownSrc,
4921 Depth + 1);
4922 KnownSrc = KnownSrc.unionWith(Known);
4923 if (KnownSrc.isUnknown())
4924 return;
4925
4926 if (isGuaranteedNotToBeUndef(Arm, SQ.AC, SQ.CxtI, SQ.DT, Depth + 1))
4927 Known = KnownSrc;
4928}
4929
4930void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4931 FPClassTest InterestedClasses, KnownFPClass &Known,
4932 const SimplifyQuery &Q, unsigned Depth);
4933
4934static void computeKnownFPClass(const Value *V, KnownFPClass &Known,
4935 FPClassTest InterestedClasses,
4936 const SimplifyQuery &Q, unsigned Depth) {
4937 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
4938 APInt DemandedElts =
4939 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
4940 computeKnownFPClass(V, DemandedElts, InterestedClasses, Known, Q, Depth);
4941}
4942
4944 const APInt &DemandedElts,
4945 FPClassTest InterestedClasses,
4946 KnownFPClass &Known,
4947 const SimplifyQuery &Q,
4948 unsigned Depth) {
4949 if ((InterestedClasses &
4951 return;
4952
4953 KnownFPClass KnownSrc;
4954 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
4955 KnownSrc, Q, Depth + 1);
4956 Known = KnownFPClass::fptrunc(KnownSrc);
4957}
4958
4960 switch (IID) {
4961 case Intrinsic::minimum:
4963 case Intrinsic::maximum:
4965 case Intrinsic::minimumnum:
4967 case Intrinsic::maximumnum:
4969 case Intrinsic::minnum:
4971 case Intrinsic::maxnum:
4973 default:
4974 llvm_unreachable("not a floating-point min-max intrinsic");
4975 }
4976}
4977
4978/// \return true if this is a floating point value that is known to have a
4979/// magnitude smaller than 1. i.e., fabs(X) <= 1.0
4980static bool isAbsoluteValueLessEqualOne(const Value *V) {
4981 // TODO: Handle frexp and x - floor(x)?
4983}
4984
4985void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4986 FPClassTest InterestedClasses, KnownFPClass &Known,
4987 const SimplifyQuery &Q, unsigned Depth) {
4988 assert(Known.isUnknown() && "should not be called with known information");
4989
4990 if (!DemandedElts) {
4991 // No demanded elts, better to assume we don't know anything.
4992 Known.resetAll();
4993 return;
4994 }
4995
4996 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4997
4998 if (auto *CFP = dyn_cast<ConstantFP>(V)) {
4999 Known = KnownFPClass(CFP->getValueAPF());
5000 return;
5001 }
5002
5004 Known.KnownFPClasses = fcPosZero;
5005 Known.SignBit = false;
5006 return;
5007 }
5008
5009 if (isa<PoisonValue>(V)) {
5010 Known.KnownFPClasses = fcNone;
5011 Known.SignBit = false;
5012 return;
5013 }
5014
5015 // Try to handle fixed width vector constants
5016 auto *VFVTy = dyn_cast<FixedVectorType>(V->getType());
5017 const Constant *CV = dyn_cast<Constant>(V);
5018 if (VFVTy && CV) {
5019 Known.KnownFPClasses = fcNone;
5020 bool SignBitAllZero = true;
5021 bool SignBitAllOne = true;
5022
5023 // For vectors, verify that each element is not NaN.
5024 unsigned NumElts = VFVTy->getNumElements();
5025 for (unsigned i = 0; i != NumElts; ++i) {
5026 if (!DemandedElts[i])
5027 continue;
5028
5029 Constant *Elt = CV->getAggregateElement(i);
5030 if (!Elt) {
5031 Known = KnownFPClass();
5032 return;
5033 }
5034 if (isa<PoisonValue>(Elt))
5035 continue;
5036 auto *CElt = dyn_cast<ConstantFP>(Elt);
5037 if (!CElt) {
5038 Known = KnownFPClass();
5039 return;
5040 }
5041
5042 const APFloat &C = CElt->getValueAPF();
5043 Known.KnownFPClasses |= C.classify();
5044 if (C.isNegative())
5045 SignBitAllZero = false;
5046 else
5047 SignBitAllOne = false;
5048 }
5049 if (SignBitAllOne != SignBitAllZero)
5050 Known.SignBit = SignBitAllOne;
5051 return;
5052 }
5053
5054 if (const auto *CDS = dyn_cast<ConstantDataSequential>(V)) {
5055 Known.KnownFPClasses = fcNone;
5056 for (size_t I = 0, E = CDS->getNumElements(); I != E; ++I)
5057 Known |= CDS->getElementAsAPFloat(I).classify();
5058 return;
5059 }
5060
5061 if (const auto *CA = dyn_cast<ConstantAggregate>(V)) {
5062 // TODO: Handle complex aggregates
5063 Known.KnownFPClasses = fcNone;
5064 for (const Use &Op : CA->operands()) {
5065 auto *CFP = dyn_cast<ConstantFP>(Op.get());
5066 if (!CFP) {
5067 Known = KnownFPClass();
5068 return;
5069 }
5070
5071 Known |= CFP->getValueAPF().classify();
5072 }
5073
5074 return;
5075 }
5076
5077 FPClassTest KnownNotFromFlags = fcNone;
5078 if (const auto *CB = dyn_cast<CallBase>(V))
5079 KnownNotFromFlags |= CB->getRetNoFPClass();
5080 else if (const auto *Arg = dyn_cast<Argument>(V))
5081 KnownNotFromFlags |= Arg->getNoFPClass();
5082
5083 const Operator *Op = dyn_cast<Operator>(V);
5085 if (FPOp->hasNoNaNs())
5086 KnownNotFromFlags |= fcNan;
5087 if (FPOp->hasNoInfs())
5088 KnownNotFromFlags |= fcInf;
5089 }
5090
5091 KnownFPClass AssumedClasses = computeKnownFPClassFromContext(V, Q);
5092 KnownNotFromFlags |= ~AssumedClasses.KnownFPClasses;
5093
5094 // We no longer need to find out about these bits from inputs if we can
5095 // assume this from flags/attributes.
5096 InterestedClasses &= ~KnownNotFromFlags;
5097
5098 llvm::scope_exit ClearClassesFromFlags([=, &Known] {
5099 Known.knownNot(KnownNotFromFlags);
5100 if (!Known.SignBit && AssumedClasses.SignBit) {
5101 if (*AssumedClasses.SignBit)
5102 Known.signBitMustBeOne();
5103 else
5104 Known.signBitMustBeZero();
5105 }
5106 });
5107
5108 if (!Op)
5109 return;
5110
5111 // All recursive calls that increase depth must come after this.
5113 return;
5114
5115 const unsigned Opc = Op->getOpcode();
5116 switch (Opc) {
5117 case Instruction::FNeg: {
5118 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5119 Known, Q, Depth + 1);
5120 Known.fneg();
5121 break;
5122 }
5123 case Instruction::Select: {
5124 auto ComputeForArm = [&](Value *Arm, bool Invert) {
5125 KnownFPClass Res;
5126 computeKnownFPClass(Arm, DemandedElts, InterestedClasses, Res, Q,
5127 Depth + 1);
5128 adjustKnownFPClassForSelectArm(Res, Op->getOperand(0), Arm, Invert, Q,
5129 Depth);
5130 return Res;
5131 };
5132 // Only known if known in both the LHS and RHS.
5133 Known =
5134 ComputeForArm(Op->getOperand(1), /*Invert=*/false)
5135 .intersectWith(ComputeForArm(Op->getOperand(2), /*Invert=*/true));
5136 break;
5137 }
5138 case Instruction::Load: {
5139 const MDNode *NoFPClass =
5140 cast<LoadInst>(Op)->getMetadata(LLVMContext::MD_nofpclass);
5141 if (!NoFPClass)
5142 break;
5143
5144 ConstantInt *MaskVal =
5146 Known.knownNot(static_cast<FPClassTest>(MaskVal->getZExtValue()));
5147 break;
5148 }
5149 case Instruction::Call: {
5150 const CallInst *II = cast<CallInst>(Op);
5151 const Intrinsic::ID IID = II->getIntrinsicID();
5152 switch (IID) {
5153 case Intrinsic::fabs: {
5154 if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) {
5155 // If we only care about the sign bit we don't need to inspect the
5156 // operand.
5157 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5158 InterestedClasses, Known, Q, Depth + 1);
5159 }
5160
5161 Known.fabs();
5162 break;
5163 }
5164 case Intrinsic::copysign: {
5165 KnownFPClass KnownSign;
5166
5167 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5168 Known, Q, Depth + 1);
5169 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5170 KnownSign, Q, Depth + 1);
5171 Known.copysign(KnownSign);
5172 break;
5173 }
5174 case Intrinsic::fma:
5175 case Intrinsic::fmuladd: {
5176 if ((InterestedClasses & fcNegative) == fcNone)
5177 break;
5178
5179 // FIXME: This should check isGuaranteedNotToBeUndef
5180 if (II->getArgOperand(0) == II->getArgOperand(1)) {
5181 KnownFPClass KnownSrc, KnownAddend;
5182 computeKnownFPClass(II->getArgOperand(2), DemandedElts,
5183 InterestedClasses, KnownAddend, Q, Depth + 1);
5184 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5185 InterestedClasses, KnownSrc, Q, Depth + 1);
5186
5187 const Function *F = II->getFunction();
5188 const fltSemantics &FltSem =
5189 II->getType()->getScalarType()->getFltSemantics();
5191 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5192
5193 if (KnownNotFromFlags & fcNan) {
5194 KnownSrc.knownNot(fcNan);
5195 KnownAddend.knownNot(fcNan);
5196 }
5197
5198 if (KnownNotFromFlags & fcInf) {
5199 KnownSrc.knownNot(fcInf);
5200 KnownAddend.knownNot(fcInf);
5201 }
5202
5203 Known = KnownFPClass::fma_square(KnownSrc, KnownAddend, Mode);
5204 break;
5205 }
5206
5207 KnownFPClass KnownSrc[3];
5208 for (int I = 0; I != 3; ++I) {
5209 computeKnownFPClass(II->getArgOperand(I), DemandedElts,
5210 InterestedClasses, KnownSrc[I], Q, Depth + 1);
5211 if (KnownSrc[I].isUnknown())
5212 return;
5213
5214 if (KnownNotFromFlags & fcNan)
5215 KnownSrc[I].knownNot(fcNan);
5216 if (KnownNotFromFlags & fcInf)
5217 KnownSrc[I].knownNot(fcInf);
5218 }
5219
5220 const Function *F = II->getFunction();
5221 const fltSemantics &FltSem =
5222 II->getType()->getScalarType()->getFltSemantics();
5224 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5225 Known = KnownFPClass::fma(KnownSrc[0], KnownSrc[1], KnownSrc[2], Mode);
5226 break;
5227 }
5228 case Intrinsic::sqrt:
5229 case Intrinsic::experimental_constrained_sqrt: {
5230 KnownFPClass KnownSrc;
5231 FPClassTest InterestedSrcs = InterestedClasses;
5232 if (InterestedClasses & fcNan)
5233 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5234
5235 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5236 KnownSrc, Q, Depth + 1);
5237
5239
5240 bool HasNSZ = Q.IIQ.hasNoSignedZeros(II);
5241 if (!HasNSZ) {
5242 const Function *F = II->getFunction();
5243 const fltSemantics &FltSem =
5244 II->getType()->getScalarType()->getFltSemantics();
5245 Mode = F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5246 }
5247
5248 Known = KnownFPClass::sqrt(KnownSrc, Mode);
5249 if (HasNSZ)
5250 Known.knownNot(fcNegZero);
5251
5252 break;
5253 }
5254 case Intrinsic::sin:
5255 case Intrinsic::cos: {
5256 // Return NaN on infinite inputs.
5257 KnownFPClass KnownSrc;
5258 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5259 KnownSrc, Q, Depth + 1);
5260 Known = IID == Intrinsic::sin ? KnownFPClass::sin(KnownSrc)
5261 : KnownFPClass::cos(KnownSrc);
5262 break;
5263 }
5264 case Intrinsic::maxnum:
5265 case Intrinsic::minnum:
5266 case Intrinsic::minimum:
5267 case Intrinsic::maximum:
5268 case Intrinsic::minimumnum:
5269 case Intrinsic::maximumnum: {
5270 KnownFPClass KnownLHS, KnownRHS;
5271 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5272 KnownLHS, Q, Depth + 1);
5273 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5274 KnownRHS, Q, Depth + 1);
5275
5276 const Function *F = II->getFunction();
5277
5279 F ? F->getDenormalMode(
5280 II->getType()->getScalarType()->getFltSemantics())
5282
5283 Known = KnownFPClass::minMaxLike(KnownLHS, KnownRHS, getMinMaxKind(IID),
5284 Mode);
5285 break;
5286 }
5287 case Intrinsic::canonicalize: {
5288 KnownFPClass KnownSrc;
5289 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5290 KnownSrc, Q, Depth + 1);
5291
5292 const Function *F = II->getFunction();
5293 DenormalMode DenormMode =
5294 F ? F->getDenormalMode(
5295 II->getType()->getScalarType()->getFltSemantics())
5297 Known = KnownFPClass::canonicalize(KnownSrc, DenormMode);
5298 break;
5299 }
5300 case Intrinsic::vector_reduce_fmax:
5301 case Intrinsic::vector_reduce_fmin:
5302 case Intrinsic::vector_reduce_fmaximum:
5303 case Intrinsic::vector_reduce_fminimum: {
5304 // reduce min/max will choose an element from one of the vector elements,
5305 // so we can infer and class information that is common to all elements.
5306 Known = computeKnownFPClass(II->getArgOperand(0), II->getFastMathFlags(),
5307 InterestedClasses, Q, Depth + 1);
5308 // Can only propagate sign if output is never NaN.
5309 if (!Known.isKnownNeverNaN())
5310 Known.SignBit.reset();
5311 break;
5312 }
5313 // reverse preserves all characteristics of the input vec's element.
5314 case Intrinsic::vector_reverse:
5315 Known = computeKnownFPClass(
5316 II->getArgOperand(0), DemandedElts.reverseBits(),
5317 II->getFastMathFlags(), InterestedClasses, Q, Depth + 1);
5318 break;
5319 case Intrinsic::trunc:
5320 case Intrinsic::floor:
5321 case Intrinsic::ceil:
5322 case Intrinsic::rint:
5323 case Intrinsic::nearbyint:
5324 case Intrinsic::round:
5325 case Intrinsic::roundeven: {
5326 KnownFPClass KnownSrc;
5327 FPClassTest InterestedSrcs = InterestedClasses;
5328 if (InterestedSrcs & fcPosFinite)
5329 InterestedSrcs |= fcPosFinite;
5330 if (InterestedSrcs & fcNegFinite)
5331 InterestedSrcs |= fcNegFinite;
5332 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5333 KnownSrc, Q, Depth + 1);
5334
5336 KnownSrc, IID == Intrinsic::trunc,
5337 V->getType()->getScalarType()->isMultiUnitFPType());
5338 break;
5339 }
5340 case Intrinsic::exp:
5341 case Intrinsic::exp2:
5342 case Intrinsic::exp10:
5343 case Intrinsic::amdgcn_exp2: {
5344 KnownFPClass KnownSrc;
5345 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5346 KnownSrc, Q, Depth + 1);
5347
5348 Known = KnownFPClass::exp(KnownSrc);
5349
5350 Type *EltTy = II->getType()->getScalarType();
5351 if (IID == Intrinsic::amdgcn_exp2 && EltTy->isFloatTy())
5352 Known.knownNot(fcSubnormal);
5353
5354 break;
5355 }
5356 case Intrinsic::fptrunc_round: {
5357 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,
5358 Q, Depth);
5359 break;
5360 }
5361 case Intrinsic::log:
5362 case Intrinsic::log10:
5363 case Intrinsic::log2:
5364 case Intrinsic::experimental_constrained_log:
5365 case Intrinsic::experimental_constrained_log10:
5366 case Intrinsic::experimental_constrained_log2:
5367 case Intrinsic::amdgcn_log: {
5368 Type *EltTy = II->getType()->getScalarType();
5369
5370 // log(+inf) -> +inf
5371 // log([+-]0.0) -> -inf
5372 // log(-inf) -> nan
5373 // log(-x) -> nan
5374 if ((InterestedClasses & (fcNan | fcInf)) != fcNone) {
5375 FPClassTest InterestedSrcs = InterestedClasses;
5376 if ((InterestedClasses & fcNegInf) != fcNone)
5377 InterestedSrcs |= fcZero | fcSubnormal;
5378 if ((InterestedClasses & fcNan) != fcNone)
5379 InterestedSrcs |= fcNan | fcNegative;
5380
5381 KnownFPClass KnownSrc;
5382 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5383 KnownSrc, Q, Depth + 1);
5384
5385 const Function *F = II->getFunction();
5386 DenormalMode Mode = F ? F->getDenormalMode(EltTy->getFltSemantics())
5388 Known = KnownFPClass::log(KnownSrc, Mode);
5389 }
5390
5391 break;
5392 }
5393 case Intrinsic::powi: {
5394 if ((InterestedClasses & fcNegative) == fcNone)
5395 break;
5396
5397 const Value *Exp = II->getArgOperand(1);
5398 Type *ExpTy = Exp->getType();
5399 unsigned BitWidth = ExpTy->getScalarType()->getIntegerBitWidth();
5400 KnownBits ExponentKnownBits(BitWidth);
5401 computeKnownBits(Exp, isa<VectorType>(ExpTy) ? DemandedElts : APInt(1, 1),
5402 ExponentKnownBits, Q, Depth + 1);
5403
5404 KnownFPClass KnownSrc;
5405 if (ExponentKnownBits.isZero() || !ExponentKnownBits.isEven()) {
5406 computeKnownFPClass(II->getArgOperand(0), DemandedElts, fcNegative,
5407 KnownSrc, Q, Depth + 1);
5408 }
5409
5410 Known = KnownFPClass::powi(KnownSrc, ExponentKnownBits);
5411 break;
5412 }
5413 case Intrinsic::ldexp: {
5414 KnownFPClass KnownSrc;
5415 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5416 KnownSrc, Q, Depth + 1);
5417 // Can refine inf/zero handling based on the exponent operand.
5418 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;
5419
5420 KnownBits ExpBits;
5421 if ((KnownSrc.KnownFPClasses & ExpInfoMask) != fcNone) {
5422 const Value *ExpArg = II->getArgOperand(1);
5423 ExpBits = computeKnownBits(ExpArg, DemandedElts, Q, Depth + 1);
5424 }
5425
5426 const fltSemantics &Flt =
5427 II->getType()->getScalarType()->getFltSemantics();
5428
5429 const Function *F = II->getFunction();
5431 F ? F->getDenormalMode(Flt) : DenormalMode::getDynamic();
5432
5433 Known = KnownFPClass::ldexp(KnownSrc, ExpBits, Flt, Mode);
5434 break;
5435 }
5436 case Intrinsic::arithmetic_fence: {
5437 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5438 Known, Q, Depth + 1);
5439 break;
5440 }
5441 case Intrinsic::experimental_constrained_sitofp:
5442 case Intrinsic::experimental_constrained_uitofp:
5443 // Cannot produce nan
5444 Known.knownNot(fcNan);
5445
5446 // sitofp and uitofp turn into +0.0 for zero.
5447 Known.knownNot(fcNegZero);
5448
5449 // Integers cannot be subnormal
5450 Known.knownNot(fcSubnormal);
5451
5452 if (IID == Intrinsic::experimental_constrained_uitofp)
5453 Known.signBitMustBeZero();
5454
5455 // TODO: Copy inf handling from instructions
5456 break;
5457
5458 case Intrinsic::amdgcn_fract: {
5459 Known.knownNot(fcInf);
5460
5461 if (InterestedClasses & fcNan) {
5462 KnownFPClass KnownSrc;
5463 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5464 InterestedClasses, KnownSrc, Q, Depth + 1);
5465
5466 if (KnownSrc.isKnownNeverInfOrNaN())
5467 Known.knownNot(fcNan);
5468 else if (KnownSrc.isKnownNever(fcSNan))
5469 Known.knownNot(fcSNan);
5470 }
5471
5472 break;
5473 }
5474 case Intrinsic::amdgcn_rcp: {
5475 KnownFPClass KnownSrc;
5476 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5477 KnownSrc, Q, Depth + 1);
5478
5479 Known.propagateNaN(KnownSrc);
5480
5481 Type *EltTy = II->getType()->getScalarType();
5482
5483 // f32 denormal always flushed.
5484 if (EltTy->isFloatTy()) {
5485 Known.knownNot(fcSubnormal);
5486 KnownSrc.knownNot(fcSubnormal);
5487 }
5488
5489 if (KnownSrc.isKnownNever(fcNegative))
5490 Known.knownNot(fcNegative);
5491 if (KnownSrc.isKnownNever(fcPositive))
5492 Known.knownNot(fcPositive);
5493
5494 if (const Function *F = II->getFunction()) {
5495 DenormalMode Mode = F->getDenormalMode(EltTy->getFltSemantics());
5496 if (KnownSrc.isKnownNeverLogicalPosZero(Mode))
5497 Known.knownNot(fcPosInf);
5498 if (KnownSrc.isKnownNeverLogicalNegZero(Mode))
5499 Known.knownNot(fcNegInf);
5500 }
5501
5502 break;
5503 }
5504 case Intrinsic::amdgcn_rsq: {
5505 KnownFPClass KnownSrc;
5506 // The only negative value that can be returned is -inf for -0 inputs.
5508
5509 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5510 KnownSrc, Q, Depth + 1);
5511
5512 // Negative -> nan
5513 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
5514 Known.knownNot(fcNan);
5515 else if (KnownSrc.isKnownNever(fcSNan))
5516 Known.knownNot(fcSNan);
5517
5518 // +inf -> +0
5519 if (KnownSrc.isKnownNeverPosInfinity())
5520 Known.knownNot(fcPosZero);
5521
5522 Type *EltTy = II->getType()->getScalarType();
5523
5524 // f32 denormal always flushed.
5525 if (EltTy->isFloatTy())
5526 Known.knownNot(fcPosSubnormal);
5527
5528 if (const Function *F = II->getFunction()) {
5529 DenormalMode Mode = F->getDenormalMode(EltTy->getFltSemantics());
5530
5531 // -0 -> -inf
5532 if (KnownSrc.isKnownNeverLogicalNegZero(Mode))
5533 Known.knownNot(fcNegInf);
5534
5535 // +0 -> +inf
5536 if (KnownSrc.isKnownNeverLogicalPosZero(Mode))
5537 Known.knownNot(fcPosInf);
5538 }
5539
5540 break;
5541 }
5542 case Intrinsic::amdgcn_trig_preop: {
5543 // Always returns a value [0, 1)
5544 Known.knownNot(fcNan | fcInf | fcNegative);
5545 break;
5546 }
5547 default:
5548 break;
5549 }
5550
5551 break;
5552 }
5553 case Instruction::FAdd:
5554 case Instruction::FSub: {
5555 KnownFPClass KnownLHS, KnownRHS;
5556 bool WantNegative =
5557 Op->getOpcode() == Instruction::FAdd &&
5558 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;
5559 bool WantNaN = (InterestedClasses & fcNan) != fcNone;
5560 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;
5561
5562 if (!WantNaN && !WantNegative && !WantNegZero)
5563 break;
5564
5565 FPClassTest InterestedSrcs = InterestedClasses;
5566 if (WantNegative)
5567 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5568 if (InterestedClasses & fcNan)
5569 InterestedSrcs |= fcInf;
5570 computeKnownFPClass(Op->getOperand(1), DemandedElts, InterestedSrcs,
5571 KnownRHS, Q, Depth + 1);
5572
5573 // Special case fadd x, x, which is the canonical form of fmul x, 2.
5574 bool Self = Op->getOperand(0) == Op->getOperand(1) &&
5575 isGuaranteedNotToBeUndef(Op->getOperand(0), Q.AC, Q.CxtI, Q.DT,
5576 Depth + 1);
5577 if (Self)
5578 KnownLHS = KnownRHS;
5579
5580 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||
5581 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) ||
5582 WantNegZero || Opc == Instruction::FSub) {
5583
5584 // FIXME: Context function should always be passed in separately
5585 const Function *F = cast<Instruction>(Op)->getFunction();
5586 const fltSemantics &FltSem =
5587 Op->getType()->getScalarType()->getFltSemantics();
5589 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5590
5591 if (Self && Opc == Instruction::FAdd) {
5592 Known = KnownFPClass::fadd_self(KnownLHS, Mode);
5593 } else {
5594 // RHS is canonically cheaper to compute. Skip inspecting the LHS if
5595 // there's no point.
5596
5597 if (!Self) {
5598 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedSrcs,
5599 KnownLHS, Q, Depth + 1);
5600 }
5601
5602 Known = Opc == Instruction::FAdd
5603 ? KnownFPClass::fadd(KnownLHS, KnownRHS, Mode)
5604 : KnownFPClass::fsub(KnownLHS, KnownRHS, Mode);
5605 }
5606 }
5607
5608 break;
5609 }
5610 case Instruction::FMul: {
5611 const Function *F = cast<Instruction>(Op)->getFunction();
5613 F ? F->getDenormalMode(
5614 Op->getType()->getScalarType()->getFltSemantics())
5616
5617 Value *LHS = Op->getOperand(0);
5618 Value *RHS = Op->getOperand(1);
5619 // X * X is always non-negative or a NaN.
5620 // FIXME: Should check isGuaranteedNotToBeUndef
5621 if (LHS == RHS) {
5622 KnownFPClass KnownSrc;
5623 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownSrc, Q,
5624 Depth + 1);
5625 Known = KnownFPClass::square(KnownSrc, Mode);
5626 break;
5627 }
5628
5629 KnownFPClass KnownLHS, KnownRHS;
5630
5631 const APFloat *CRHS;
5632 if (match(RHS, m_APFloat(CRHS))) {
5633 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Q,
5634 Depth + 1);
5635 Known = KnownFPClass::fmul(KnownLHS, *CRHS, Mode);
5636 } else {
5637 computeKnownFPClass(RHS, DemandedElts, fcAllFlags, KnownRHS, Q,
5638 Depth + 1);
5639 // TODO: Improve accuracy in unfused FMA pattern. We can prove an
5640 // additional not-nan if the addend is known-not negative infinity if the
5641 // multiply is known-not infinity.
5642
5643 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Q,
5644 Depth + 1);
5645 Known = KnownFPClass::fmul(KnownLHS, KnownRHS, Mode);
5646 }
5647
5648 /// Propgate no-infs if the other source is known smaller than one, such
5649 /// that this cannot introduce overflow.
5651 Known.knownNot(fcInf);
5652 else if (KnownRHS.isKnownNever(fcInf) && isAbsoluteValueLessEqualOne(LHS))
5653 Known.knownNot(fcInf);
5654
5655 break;
5656 }
5657 case Instruction::FDiv:
5658 case Instruction::FRem: {
5659 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
5660
5661 if (Op->getOperand(0) == Op->getOperand(1) &&
5662 isGuaranteedNotToBeUndef(Op->getOperand(0), Q.AC, Q.CxtI, Q.DT)) {
5663 if (Op->getOpcode() == Instruction::FDiv) {
5664 // X / X is always exactly 1.0 or a NaN.
5666 } else {
5667 // X % X is always exactly [+-]0.0 or a NaN.
5668 Known.KnownFPClasses = fcNan | fcZero;
5669 }
5670
5671 if (!WantNan)
5672 break;
5673
5674 KnownFPClass KnownSrc;
5675 computeKnownFPClass(Op->getOperand(0), DemandedElts,
5676 fcNan | fcInf | fcZero | fcSubnormal, KnownSrc, Q,
5677 Depth + 1);
5678 const Function *F = cast<Instruction>(Op)->getFunction();
5679 const fltSemantics &FltSem =
5680 Op->getType()->getScalarType()->getFltSemantics();
5681
5683 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5684
5685 Known = Op->getOpcode() == Instruction::FDiv
5686 ? KnownFPClass::fdiv_self(KnownSrc, Mode)
5687 : KnownFPClass::frem_self(KnownSrc, Mode);
5688 break;
5689 }
5690
5691 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
5692 const bool WantPositive =
5693 Opc == Instruction::FRem && (InterestedClasses & fcPositive) != fcNone;
5694 if (!WantNan && !WantNegative && !WantPositive)
5695 break;
5696
5697 KnownFPClass KnownLHS, KnownRHS;
5698
5699 computeKnownFPClass(Op->getOperand(1), DemandedElts,
5700 fcNan | fcInf | fcZero | fcNegative, KnownRHS, Q,
5701 Depth + 1);
5702
5703 bool KnowSomethingUseful = KnownRHS.isKnownNeverNaN() ||
5704 KnownRHS.isKnownNever(fcNegative) ||
5705 KnownRHS.isKnownNever(fcPositive);
5706
5707 if (KnowSomethingUseful || WantPositive) {
5708 computeKnownFPClass(Op->getOperand(0), DemandedElts, fcAllFlags, KnownLHS,
5709 Q, Depth + 1);
5710 }
5711
5712 const Function *F = cast<Instruction>(Op)->getFunction();
5713 const fltSemantics &FltSem =
5714 Op->getType()->getScalarType()->getFltSemantics();
5715
5716 if (Op->getOpcode() == Instruction::FDiv) {
5718 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5719 Known = KnownFPClass::fdiv(KnownLHS, KnownRHS, Mode);
5720 } else {
5721 // Inf REM x and x REM 0 produce NaN.
5722 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5723 KnownLHS.isKnownNeverInfinity() && F &&
5724 KnownRHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))) {
5725 Known.knownNot(fcNan);
5726 }
5727
5728 // The sign for frem is the same as the first operand.
5729 if (KnownLHS.cannotBeOrderedLessThanZero())
5731 if (KnownLHS.cannotBeOrderedGreaterThanZero())
5733
5734 // See if we can be more aggressive about the sign of 0.
5735 if (KnownLHS.isKnownNever(fcNegative))
5736 Known.knownNot(fcNegative);
5737 if (KnownLHS.isKnownNever(fcPositive))
5738 Known.knownNot(fcPositive);
5739 }
5740
5741 break;
5742 }
5743 case Instruction::FPExt: {
5744 KnownFPClass KnownSrc;
5745 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5746 KnownSrc, Q, Depth + 1);
5747
5748 const fltSemantics &DstTy =
5749 Op->getType()->getScalarType()->getFltSemantics();
5750 const fltSemantics &SrcTy =
5751 Op->getOperand(0)->getType()->getScalarType()->getFltSemantics();
5752
5753 Known = KnownFPClass::fpext(KnownSrc, DstTy, SrcTy);
5754 break;
5755 }
5756 case Instruction::FPTrunc: {
5757 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known, Q,
5758 Depth);
5759 break;
5760 }
5761 case Instruction::SIToFP:
5762 case Instruction::UIToFP: {
5763 // Cannot produce nan
5764 Known.knownNot(fcNan);
5765
5766 // Integers cannot be subnormal
5767 Known.knownNot(fcSubnormal);
5768
5769 // sitofp and uitofp turn into +0.0 for zero.
5770 Known.knownNot(fcNegZero);
5771 if (Op->getOpcode() == Instruction::UIToFP)
5772 Known.signBitMustBeZero();
5773
5774 if (InterestedClasses & fcInf) {
5775 // Get width of largest magnitude integer (remove a bit if signed).
5776 // This still works for a signed minimum value because the largest FP
5777 // value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx).
5778 int IntSize = Op->getOperand(0)->getType()->getScalarSizeInBits();
5779 if (Op->getOpcode() == Instruction::SIToFP)
5780 --IntSize;
5781
5782 // If the exponent of the largest finite FP value can hold the largest
5783 // integer, the result of the cast must be finite.
5784 Type *FPTy = Op->getType()->getScalarType();
5785 if (ilogb(APFloat::getLargest(FPTy->getFltSemantics())) >= IntSize)
5786 Known.knownNot(fcInf);
5787 }
5788
5789 break;
5790 }
5791 case Instruction::ExtractElement: {
5792 // Look through extract element. If the index is non-constant or
5793 // out-of-range demand all elements, otherwise just the extracted element.
5794 const Value *Vec = Op->getOperand(0);
5795
5796 APInt DemandedVecElts;
5797 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
5798 unsigned NumElts = VecTy->getNumElements();
5799 DemandedVecElts = APInt::getAllOnes(NumElts);
5800 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(1));
5801 if (CIdx && CIdx->getValue().ult(NumElts))
5802 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
5803 } else {
5804 DemandedVecElts = APInt(1, 1);
5805 }
5806
5807 return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,
5808 Q, Depth + 1);
5809 }
5810 case Instruction::InsertElement: {
5811 if (isa<ScalableVectorType>(Op->getType()))
5812 return;
5813
5814 const Value *Vec = Op->getOperand(0);
5815 const Value *Elt = Op->getOperand(1);
5816 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(2));
5817 unsigned NumElts = DemandedElts.getBitWidth();
5818 APInt DemandedVecElts = DemandedElts;
5819 bool NeedsElt = true;
5820 // If we know the index we are inserting to, clear it from Vec check.
5821 if (CIdx && CIdx->getValue().ult(NumElts)) {
5822 DemandedVecElts.clearBit(CIdx->getZExtValue());
5823 NeedsElt = DemandedElts[CIdx->getZExtValue()];
5824 }
5825
5826 // Do we demand the inserted element?
5827 if (NeedsElt) {
5828 computeKnownFPClass(Elt, Known, InterestedClasses, Q, Depth + 1);
5829 // If we don't know any bits, early out.
5830 if (Known.isUnknown())
5831 break;
5832 } else {
5833 Known.KnownFPClasses = fcNone;
5834 }
5835
5836 // Do we need anymore elements from Vec?
5837 if (!DemandedVecElts.isZero()) {
5838 KnownFPClass Known2;
5839 computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2, Q,
5840 Depth + 1);
5841 Known |= Known2;
5842 }
5843
5844 break;
5845 }
5846 case Instruction::ShuffleVector: {
5847 // Handle vector splat idiom
5848 if (Value *Splat = getSplatValue(V)) {
5849 computeKnownFPClass(Splat, Known, InterestedClasses, Q, Depth + 1);
5850 break;
5851 }
5852
5853 // For undef elements, we don't know anything about the common state of
5854 // the shuffle result.
5855 APInt DemandedLHS, DemandedRHS;
5856 auto *Shuf = dyn_cast<ShuffleVectorInst>(Op);
5857 if (!Shuf || !getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
5858 return;
5859
5860 if (!!DemandedLHS) {
5861 const Value *LHS = Shuf->getOperand(0);
5862 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known, Q,
5863 Depth + 1);
5864
5865 // If we don't know any bits, early out.
5866 if (Known.isUnknown())
5867 break;
5868 } else {
5869 Known.KnownFPClasses = fcNone;
5870 }
5871
5872 if (!!DemandedRHS) {
5873 KnownFPClass Known2;
5874 const Value *RHS = Shuf->getOperand(1);
5875 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2, Q,
5876 Depth + 1);
5877 Known |= Known2;
5878 }
5879
5880 break;
5881 }
5882 case Instruction::ExtractValue: {
5883 const ExtractValueInst *Extract = cast<ExtractValueInst>(Op);
5884 ArrayRef<unsigned> Indices = Extract->getIndices();
5885 const Value *Src = Extract->getAggregateOperand();
5886 if (isa<StructType>(Src->getType()) && Indices.size() == 1 &&
5887 Indices[0] == 0) {
5888 if (const auto *II = dyn_cast<IntrinsicInst>(Src)) {
5889 switch (II->getIntrinsicID()) {
5890 case Intrinsic::frexp: {
5891 Known.knownNot(fcSubnormal);
5892
5893 KnownFPClass KnownSrc;
5894 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5895 InterestedClasses, KnownSrc, Q, Depth + 1);
5896
5897 const Function *F = cast<Instruction>(Op)->getFunction();
5898 const fltSemantics &FltSem =
5899 Op->getType()->getScalarType()->getFltSemantics();
5900
5902 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5903 Known = KnownFPClass::frexp_mant(KnownSrc, Mode);
5904 return;
5905 }
5906 default:
5907 break;
5908 }
5909 }
5910 }
5911
5912 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Q,
5913 Depth + 1);
5914 break;
5915 }
5916 case Instruction::PHI: {
5917 const PHINode *P = cast<PHINode>(Op);
5918 // Unreachable blocks may have zero-operand PHI nodes.
5919 if (P->getNumIncomingValues() == 0)
5920 break;
5921
5922 // Otherwise take the unions of the known bit sets of the operands,
5923 // taking conservative care to avoid excessive recursion.
5924 const unsigned PhiRecursionLimit = MaxAnalysisRecursionDepth - 2;
5925
5926 if (Depth < PhiRecursionLimit) {
5927 // Skip if every incoming value references to ourself.
5928 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
5929 break;
5930
5931 bool First = true;
5932
5933 for (const Use &U : P->operands()) {
5934 Value *IncValue;
5935 Instruction *CxtI;
5936 breakSelfRecursivePHI(&U, P, IncValue, CxtI);
5937 // Skip direct self references.
5938 if (IncValue == P)
5939 continue;
5940
5941 KnownFPClass KnownSrc;
5942 // Recurse, but cap the recursion to two levels, because we don't want
5943 // to waste time spinning around in loops. We need at least depth 2 to
5944 // detect known sign bits.
5945 computeKnownFPClass(IncValue, DemandedElts, InterestedClasses, KnownSrc,
5947 PhiRecursionLimit);
5948
5949 if (First) {
5950 Known = KnownSrc;
5951 First = false;
5952 } else {
5953 Known |= KnownSrc;
5954 }
5955
5956 if (Known.KnownFPClasses == fcAllFlags)
5957 break;
5958 }
5959 }
5960
5961 // Look for the case of a for loop which has a positive
5962 // initial value and is incremented by a squared value.
5963 // This will propagate sign information out of such loops.
5964 if (P->getNumIncomingValues() != 2 || Known.cannotBeOrderedLessThanZero())
5965 break;
5966 for (unsigned I = 0; I < 2; I++) {
5967 Value *RecurValue = P->getIncomingValue(1 - I);
5969 if (!II)
5970 continue;
5971 Value *R, *L, *Init;
5972 PHINode *PN;
5974 PN == P) {
5975 switch (II->getIntrinsicID()) {
5976 case Intrinsic::fma:
5977 case Intrinsic::fmuladd: {
5978 KnownFPClass KnownStart;
5979 computeKnownFPClass(Init, DemandedElts, InterestedClasses, KnownStart,
5980 Q, Depth + 1);
5981 if (KnownStart.cannotBeOrderedLessThanZero() && L == R &&
5982 isGuaranteedNotToBeUndef(L, Q.AC, Q.CxtI, Q.DT, Depth + 1))
5984 break;
5985 }
5986 }
5987 }
5988 }
5989 break;
5990 }
5991 case Instruction::BitCast: {
5992 const Value *Src;
5993 if (!match(Op, m_ElementWiseBitCast(m_Value(Src))) ||
5994 !Src->getType()->isIntOrIntVectorTy())
5995 break;
5996
5997 const Type *Ty = Op->getType();
5998
5999 Value *CastLHS, *CastRHS;
6000
6001 // Match bitcast(umax(bitcast(a), bitcast(b)))
6002 if (match(Src, m_c_MaxOrMin(m_BitCast(m_Value(CastLHS)),
6003 m_BitCast(m_Value(CastRHS)))) &&
6004 CastLHS->getType() == Ty && CastRHS->getType() == Ty) {
6005 KnownFPClass KnownLHS, KnownRHS;
6006 computeKnownFPClass(CastRHS, DemandedElts, InterestedClasses, KnownRHS, Q,
6007 Depth + 1);
6008 if (!KnownRHS.isUnknown()) {
6009 computeKnownFPClass(CastLHS, DemandedElts, InterestedClasses, KnownLHS,
6010 Q, Depth + 1);
6011 Known = KnownLHS | KnownRHS;
6012 }
6013
6014 return;
6015 }
6016
6017 const Type *EltTy = Ty->getScalarType();
6018 KnownBits Bits(EltTy->getPrimitiveSizeInBits());
6019 computeKnownBits(Src, DemandedElts, Bits, Q, Depth + 1);
6020
6021 // Transfer information from the sign bit.
6022 if (Bits.isNonNegative())
6023 Known.signBitMustBeZero();
6024 else if (Bits.isNegative())
6025 Known.signBitMustBeOne();
6026
6027 if (EltTy->isIEEELikeFPTy()) {
6028 // IEEE floats are NaN when all bits of the exponent plus at least one of
6029 // the fraction bits are 1. This means:
6030 // - If we assume unknown bits are 0 and the value is NaN, it will
6031 // always be NaN
6032 // - If we assume unknown bits are 1 and the value is not NaN, it can
6033 // never be NaN
6034 // Note: They do not hold for x86_fp80 format.
6035 if (APFloat(EltTy->getFltSemantics(), Bits.One).isNaN())
6036 Known.KnownFPClasses = fcNan;
6037 else if (!APFloat(EltTy->getFltSemantics(), ~Bits.Zero).isNaN())
6038 Known.knownNot(fcNan);
6039
6040 // Build KnownBits representing Inf and check if it must be equal or
6041 // unequal to this value.
6042 auto InfKB = KnownBits::makeConstant(
6043 APFloat::getInf(EltTy->getFltSemantics()).bitcastToAPInt());
6044 InfKB.Zero.clearSignBit();
6045 if (const auto InfResult = KnownBits::eq(Bits, InfKB)) {
6046 assert(!InfResult.value());
6047 Known.knownNot(fcInf);
6048 } else if (Bits == InfKB) {
6049 Known.KnownFPClasses = fcInf;
6050 }
6051
6052 // Build KnownBits representing Zero and check if it must be equal or
6053 // unequal to this value.
6054 auto ZeroKB = KnownBits::makeConstant(
6055 APFloat::getZero(EltTy->getFltSemantics()).bitcastToAPInt());
6056 ZeroKB.Zero.clearSignBit();
6057 if (const auto ZeroResult = KnownBits::eq(Bits, ZeroKB)) {
6058 assert(!ZeroResult.value());
6059 Known.knownNot(fcZero);
6060 } else if (Bits == ZeroKB) {
6061 Known.KnownFPClasses = fcZero;
6062 }
6063 }
6064
6065 break;
6066 }
6067 default:
6068 break;
6069 }
6070}
6071
6073 const APInt &DemandedElts,
6074 FPClassTest InterestedClasses,
6075 const SimplifyQuery &SQ,
6076 unsigned Depth) {
6077 KnownFPClass KnownClasses;
6078 ::computeKnownFPClass(V, DemandedElts, InterestedClasses, KnownClasses, SQ,
6079 Depth);
6080 return KnownClasses;
6081}
6082
6084 FPClassTest InterestedClasses,
6085 const SimplifyQuery &SQ,
6086 unsigned Depth) {
6087 KnownFPClass Known;
6088 ::computeKnownFPClass(V, Known, InterestedClasses, SQ, Depth);
6089 return Known;
6090}
6091
6093 const Value *V, const DataLayout &DL, FPClassTest InterestedClasses,
6094 const TargetLibraryInfo *TLI, AssumptionCache *AC, const Instruction *CxtI,
6095 const DominatorTree *DT, bool UseInstrInfo, unsigned Depth) {
6096 return computeKnownFPClass(V, InterestedClasses,
6097 SimplifyQuery(DL, TLI, DT, AC, CxtI, UseInstrInfo),
6098 Depth);
6099}
6100
6102llvm::computeKnownFPClass(const Value *V, const APInt &DemandedElts,
6103 FastMathFlags FMF, FPClassTest InterestedClasses,
6104 const SimplifyQuery &SQ, unsigned Depth) {
6105 if (FMF.noNaNs())
6106 InterestedClasses &= ~fcNan;
6107 if (FMF.noInfs())
6108 InterestedClasses &= ~fcInf;
6109
6110 KnownFPClass Result =
6111 computeKnownFPClass(V, DemandedElts, InterestedClasses, SQ, Depth);
6112
6113 if (FMF.noNaNs())
6114 Result.KnownFPClasses &= ~fcNan;
6115 if (FMF.noInfs())
6116 Result.KnownFPClasses &= ~fcInf;
6117 return Result;
6118}
6119
6121 FPClassTest InterestedClasses,
6122 const SimplifyQuery &SQ,
6123 unsigned Depth) {
6124 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
6125 APInt DemandedElts =
6126 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
6127 return computeKnownFPClass(V, DemandedElts, FMF, InterestedClasses, SQ,
6128 Depth);
6129}
6130
6132 unsigned Depth) {
6134 return Known.isKnownNeverNegZero();
6135}
6136
6143
6145 unsigned Depth) {
6147 return Known.isKnownNeverInfinity();
6148}
6149
6150/// Return true if the floating-point value can never contain a NaN or infinity.
6152 unsigned Depth) {
6154 return Known.isKnownNeverNaN() && Known.isKnownNeverInfinity();
6155}
6156
6157/// Return true if the floating-point scalar value is not a NaN or if the
6158/// floating-point vector value has no NaN elements. Return false if a value
6159/// could ever be NaN.
6161 unsigned Depth) {
6163 return Known.isKnownNeverNaN();
6164}
6165
6166/// Return false if we can prove that the specified FP value's sign bit is 0.
6167/// Return true if we can prove that the specified FP value's sign bit is 1.
6168/// Otherwise return std::nullopt.
6169std::optional<bool> llvm::computeKnownFPSignBit(const Value *V,
6170 const SimplifyQuery &SQ,
6171 unsigned Depth) {
6173 return Known.SignBit;
6174}
6175
6177 auto *User = cast<Instruction>(U.getUser());
6178 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6179 if (FPOp->hasNoSignedZeros())
6180 return true;
6181 }
6182
6183 switch (User->getOpcode()) {
6184 case Instruction::FPToSI:
6185 case Instruction::FPToUI:
6186 return true;
6187 case Instruction::FCmp:
6188 // fcmp treats both positive and negative zero as equal.
6189 return true;
6190 case Instruction::Call:
6191 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6192 switch (II->getIntrinsicID()) {
6193 case Intrinsic::fabs:
6194 return true;
6195 case Intrinsic::copysign:
6196 return U.getOperandNo() == 0;
6197 case Intrinsic::is_fpclass:
6198 case Intrinsic::vp_is_fpclass: {
6199 auto Test =
6200 static_cast<FPClassTest>(
6201 cast<ConstantInt>(II->getArgOperand(1))->getZExtValue()) &
6204 }
6205 default:
6206 return false;
6207 }
6208 }
6209 return false;
6210 default:
6211 return false;
6212 }
6213}
6214
6216 auto *User = cast<Instruction>(U.getUser());
6217 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6218 if (FPOp->hasNoNaNs())
6219 return true;
6220 }
6221
6222 switch (User->getOpcode()) {
6223 case Instruction::FPToSI:
6224 case Instruction::FPToUI:
6225 return true;
6226 // Proper FP math operations ignore the sign bit of NaN.
6227 case Instruction::FAdd:
6228 case Instruction::FSub:
6229 case Instruction::FMul:
6230 case Instruction::FDiv:
6231 case Instruction::FRem:
6232 case Instruction::FPTrunc:
6233 case Instruction::FPExt:
6234 case Instruction::FCmp:
6235 return true;
6236 // Bitwise FP operations should preserve the sign bit of NaN.
6237 case Instruction::FNeg:
6238 case Instruction::Select:
6239 case Instruction::PHI:
6240 return false;
6241 case Instruction::Ret:
6242 return User->getFunction()->getAttributes().getRetNoFPClass() &
6244 case Instruction::Call:
6245 case Instruction::Invoke: {
6246 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6247 switch (II->getIntrinsicID()) {
6248 case Intrinsic::fabs:
6249 return true;
6250 case Intrinsic::copysign:
6251 return U.getOperandNo() == 0;
6252 // Other proper FP math intrinsics ignore the sign bit of NaN.
6253 case Intrinsic::maxnum:
6254 case Intrinsic::minnum:
6255 case Intrinsic::maximum:
6256 case Intrinsic::minimum:
6257 case Intrinsic::maximumnum:
6258 case Intrinsic::minimumnum:
6259 case Intrinsic::canonicalize:
6260 case Intrinsic::fma:
6261 case Intrinsic::fmuladd:
6262 case Intrinsic::sqrt:
6263 case Intrinsic::pow:
6264 case Intrinsic::powi:
6265 case Intrinsic::fptoui_sat:
6266 case Intrinsic::fptosi_sat:
6267 case Intrinsic::is_fpclass:
6268 case Intrinsic::vp_is_fpclass:
6269 return true;
6270 default:
6271 return false;
6272 }
6273 }
6274
6275 FPClassTest NoFPClass =
6276 cast<CallBase>(User)->getParamNoFPClass(U.getOperandNo());
6277 return NoFPClass & FPClassTest::fcNan;
6278 }
6279 default:
6280 return false;
6281 }
6282}
6283
6285 FastMathFlags FMF) {
6286 if (isa<PoisonValue>(V))
6287 return true;
6288 if (isa<UndefValue>(V))
6289 return false;
6290
6291 if (match(V, m_CheckedFp([](const APFloat &Val) { return Val.isInteger(); })))
6292 return true;
6293
6295 if (!I)
6296 return false;
6297
6298 switch (I->getOpcode()) {
6299 case Instruction::SIToFP:
6300 case Instruction::UIToFP:
6301 // TODO: Could check nofpclass(inf) on incoming argument
6302 if (FMF.noInfs())
6303 return true;
6304
6305 // Need to check int size cannot produce infinity, which computeKnownFPClass
6306 // knows how to do already.
6307 return isKnownNeverInfinity(I, SQ);
6308 case Instruction::Call: {
6309 const CallInst *CI = cast<CallInst>(I);
6310 switch (CI->getIntrinsicID()) {
6311 case Intrinsic::trunc:
6312 case Intrinsic::floor:
6313 case Intrinsic::ceil:
6314 case Intrinsic::rint:
6315 case Intrinsic::nearbyint:
6316 case Intrinsic::round:
6317 case Intrinsic::roundeven:
6318 return (FMF.noInfs() && FMF.noNaNs()) || isKnownNeverInfOrNaN(I, SQ);
6319 default:
6320 break;
6321 }
6322
6323 break;
6324 }
6325 default:
6326 break;
6327 }
6328
6329 return false;
6330}
6331
6333
6334 // All byte-wide stores are splatable, even of arbitrary variables.
6335 if (V->getType()->isIntegerTy(8))
6336 return V;
6337
6338 LLVMContext &Ctx = V->getContext();
6339
6340 // Undef don't care.
6341 auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
6342 if (isa<UndefValue>(V))
6343 return UndefInt8;
6344
6345 // Return poison for zero-sized type.
6346 if (DL.getTypeStoreSize(V->getType()).isZero())
6347 return PoisonValue::get(Type::getInt8Ty(Ctx));
6348
6350 if (!C) {
6351 // Conceptually, we could handle things like:
6352 // %a = zext i8 %X to i16
6353 // %b = shl i16 %a, 8
6354 // %c = or i16 %a, %b
6355 // but until there is an example that actually needs this, it doesn't seem
6356 // worth worrying about.
6357 return nullptr;
6358 }
6359
6360 // Handle 'null' ConstantArrayZero etc.
6361 if (C->isNullValue())
6363
6364 // Constant floating-point values can be handled as integer values if the
6365 // corresponding integer value is "byteable". An important case is 0.0.
6366 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
6367 Type *ScalarTy = CFP->getType()->getScalarType();
6368 if (ScalarTy->isHalfTy() || ScalarTy->isFloatTy() || ScalarTy->isDoubleTy())
6369 return isBytewiseValue(
6370 ConstantInt::get(Ctx, CFP->getValue().bitcastToAPInt()), DL);
6371
6372 // Don't handle long double formats, which have strange constraints.
6373 return nullptr;
6374 }
6375
6376 // We can handle constant integers that are multiple of 8 bits.
6377 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
6378 if (CI->getBitWidth() % 8 == 0) {
6379 if (!CI->getValue().isSplat(8))
6380 return nullptr;
6381 return ConstantInt::get(Ctx, CI->getValue().trunc(8));
6382 }
6383 }
6384
6385 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
6386 if (CE->getOpcode() == Instruction::IntToPtr) {
6387 if (auto *PtrTy = dyn_cast<PointerType>(CE->getType())) {
6388 unsigned BitWidth = DL.getPointerSizeInBits(PtrTy->getAddressSpace());
6390 CE->getOperand(0), Type::getIntNTy(Ctx, BitWidth), false, DL))
6391 return isBytewiseValue(Op, DL);
6392 }
6393 }
6394 }
6395
6396 auto Merge = [&](Value *LHS, Value *RHS) -> Value * {
6397 if (LHS == RHS)
6398 return LHS;
6399 if (!LHS || !RHS)
6400 return nullptr;
6401 if (LHS == UndefInt8)
6402 return RHS;
6403 if (RHS == UndefInt8)
6404 return LHS;
6405 return nullptr;
6406 };
6407
6409 Value *Val = UndefInt8;
6410 for (uint64_t I = 0, E = CA->getNumElements(); I != E; ++I)
6411 if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))
6412 return nullptr;
6413 return Val;
6414 }
6415
6417 Value *Val = UndefInt8;
6418 for (Value *Op : C->operands())
6419 if (!(Val = Merge(Val, isBytewiseValue(Op, DL))))
6420 return nullptr;
6421 return Val;
6422 }
6423
6424 // Don't try to handle the handful of other constants.
6425 return nullptr;
6426}
6427
6428// This is the recursive version of BuildSubAggregate. It takes a few different
6429// arguments. Idxs is the index within the nested struct From that we are
6430// looking at now (which is of type IndexedType). IdxSkip is the number of
6431// indices from Idxs that should be left out when inserting into the resulting
6432// struct. To is the result struct built so far, new insertvalue instructions
6433// build on that.
6434static Value *BuildSubAggregate(Value *From, Value *To, Type *IndexedType,
6436 unsigned IdxSkip,
6437 BasicBlock::iterator InsertBefore) {
6438 StructType *STy = dyn_cast<StructType>(IndexedType);
6439 if (STy) {
6440 // Save the original To argument so we can modify it
6441 Value *OrigTo = To;
6442 // General case, the type indexed by Idxs is a struct
6443 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
6444 // Process each struct element recursively
6445 Idxs.push_back(i);
6446 Value *PrevTo = To;
6447 To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
6448 InsertBefore);
6449 Idxs.pop_back();
6450 if (!To) {
6451 // Couldn't find any inserted value for this index? Cleanup
6452 while (PrevTo != OrigTo) {
6454 PrevTo = Del->getAggregateOperand();
6455 Del->eraseFromParent();
6456 }
6457 // Stop processing elements
6458 break;
6459 }
6460 }
6461 // If we successfully found a value for each of our subaggregates
6462 if (To)
6463 return To;
6464 }
6465 // Base case, the type indexed by SourceIdxs is not a struct, or not all of
6466 // the struct's elements had a value that was inserted directly. In the latter
6467 // case, perhaps we can't determine each of the subelements individually, but
6468 // we might be able to find the complete struct somewhere.
6469
6470 // Find the value that is at that particular spot
6471 Value *V = FindInsertedValue(From, Idxs);
6472
6473 if (!V)
6474 return nullptr;
6475
6476 // Insert the value in the new (sub) aggregate
6477 return InsertValueInst::Create(To, V, ArrayRef(Idxs).slice(IdxSkip), "tmp",
6478 InsertBefore);
6479}
6480
6481// This helper takes a nested struct and extracts a part of it (which is again a
6482// struct) into a new value. For example, given the struct:
6483// { a, { b, { c, d }, e } }
6484// and the indices "1, 1" this returns
6485// { c, d }.
6486//
6487// It does this by inserting an insertvalue for each element in the resulting
6488// struct, as opposed to just inserting a single struct. This will only work if
6489// each of the elements of the substruct are known (ie, inserted into From by an
6490// insertvalue instruction somewhere).
6491//
6492// All inserted insertvalue instructions are inserted before InsertBefore
6494 BasicBlock::iterator InsertBefore) {
6495 Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
6496 idx_range);
6497 Value *To = PoisonValue::get(IndexedType);
6498 SmallVector<unsigned, 10> Idxs(idx_range);
6499 unsigned IdxSkip = Idxs.size();
6500
6501 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
6502}
6503
6504/// Given an aggregate and a sequence of indices, see if the scalar value
6505/// indexed is already around as a register, for example if it was inserted
6506/// directly into the aggregate.
6507///
6508/// If InsertBefore is not null, this function will duplicate (modified)
6509/// insertvalues when a part of a nested struct is extracted.
6510Value *
6512 std::optional<BasicBlock::iterator> InsertBefore) {
6513 // Nothing to index? Just return V then (this is useful at the end of our
6514 // recursion).
6515 if (idx_range.empty())
6516 return V;
6517 // We have indices, so V should have an indexable type.
6518 assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
6519 "Not looking at a struct or array?");
6520 assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
6521 "Invalid indices for type?");
6522
6523 if (Constant *C = dyn_cast<Constant>(V)) {
6524 C = C->getAggregateElement(idx_range[0]);
6525 if (!C) return nullptr;
6526 return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
6527 }
6528
6530 // Loop the indices for the insertvalue instruction in parallel with the
6531 // requested indices
6532 const unsigned *req_idx = idx_range.begin();
6533 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
6534 i != e; ++i, ++req_idx) {
6535 if (req_idx == idx_range.end()) {
6536 // We can't handle this without inserting insertvalues
6537 if (!InsertBefore)
6538 return nullptr;
6539
6540 // The requested index identifies a part of a nested aggregate. Handle
6541 // this specially. For example,
6542 // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
6543 // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
6544 // %C = extractvalue {i32, { i32, i32 } } %B, 1
6545 // This can be changed into
6546 // %A = insertvalue {i32, i32 } undef, i32 10, 0
6547 // %C = insertvalue {i32, i32 } %A, i32 11, 1
6548 // which allows the unused 0,0 element from the nested struct to be
6549 // removed.
6550 return BuildSubAggregate(V, ArrayRef(idx_range.begin(), req_idx),
6551 *InsertBefore);
6552 }
6553
6554 // This insert value inserts something else than what we are looking for.
6555 // See if the (aggregate) value inserted into has the value we are
6556 // looking for, then.
6557 if (*req_idx != *i)
6558 return FindInsertedValue(I->getAggregateOperand(), idx_range,
6559 InsertBefore);
6560 }
6561 // If we end up here, the indices of the insertvalue match with those
6562 // requested (though possibly only partially). Now we recursively look at
6563 // the inserted value, passing any remaining indices.
6564 return FindInsertedValue(I->getInsertedValueOperand(),
6565 ArrayRef(req_idx, idx_range.end()), InsertBefore);
6566 }
6567
6569 // If we're extracting a value from an aggregate that was extracted from
6570 // something else, we can extract from that something else directly instead.
6571 // However, we will need to chain I's indices with the requested indices.
6572
6573 // Calculate the number of indices required
6574 unsigned size = I->getNumIndices() + idx_range.size();
6575 // Allocate some space to put the new indices in
6577 Idxs.reserve(size);
6578 // Add indices from the extract value instruction
6579 Idxs.append(I->idx_begin(), I->idx_end());
6580
6581 // Add requested indices
6582 Idxs.append(idx_range.begin(), idx_range.end());
6583
6584 assert(Idxs.size() == size
6585 && "Number of indices added not correct?");
6586
6587 return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
6588 }
6589 // Otherwise, we don't know (such as, extracting from a function return value
6590 // or load instruction)
6591 return nullptr;
6592}
6593
6594// If V refers to an initialized global constant, set Slice either to
6595// its initializer if the size of its elements equals ElementSize, or,
6596// for ElementSize == 8, to its representation as an array of unsiged
6597// char. Return true on success.
6598// Offset is in the unit "nr of ElementSize sized elements".
6601 unsigned ElementSize, uint64_t Offset) {
6602 assert(V && "V should not be null.");
6603 assert((ElementSize % 8) == 0 &&
6604 "ElementSize expected to be a multiple of the size of a byte.");
6605 unsigned ElementSizeInBytes = ElementSize / 8;
6606
6607 // Drill down into the pointer expression V, ignoring any intervening
6608 // casts, and determine the identity of the object it references along
6609 // with the cumulative byte offset into it.
6610 const GlobalVariable *GV =
6612 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
6613 // Fail if V is not based on constant global object.
6614 return false;
6615
6616 const DataLayout &DL = GV->getDataLayout();
6617 APInt Off(DL.getIndexTypeSizeInBits(V->getType()), 0);
6618
6619 if (GV != V->stripAndAccumulateConstantOffsets(DL, Off,
6620 /*AllowNonInbounds*/ true))
6621 // Fail if a constant offset could not be determined.
6622 return false;
6623
6624 uint64_t StartIdx = Off.getLimitedValue();
6625 if (StartIdx == UINT64_MAX)
6626 // Fail if the constant offset is excessive.
6627 return false;
6628
6629 // Off/StartIdx is in the unit of bytes. So we need to convert to number of
6630 // elements. Simply bail out if that isn't possible.
6631 if ((StartIdx % ElementSizeInBytes) != 0)
6632 return false;
6633
6634 Offset += StartIdx / ElementSizeInBytes;
6635 ConstantDataArray *Array = nullptr;
6636 ArrayType *ArrayTy = nullptr;
6637
6638 if (GV->getInitializer()->isNullValue()) {
6639 Type *GVTy = GV->getValueType();
6640 uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy).getFixedValue();
6641 uint64_t Length = SizeInBytes / ElementSizeInBytes;
6642
6643 Slice.Array = nullptr;
6644 Slice.Offset = 0;
6645 // Return an empty Slice for undersized constants to let callers
6646 // transform even undefined library calls into simpler, well-defined
6647 // expressions. This is preferable to making the calls although it
6648 // prevents sanitizers from detecting such calls.
6649 Slice.Length = Length < Offset ? 0 : Length - Offset;
6650 return true;
6651 }
6652
6653 auto *Init = const_cast<Constant *>(GV->getInitializer());
6654 if (auto *ArrayInit = dyn_cast<ConstantDataArray>(Init)) {
6655 Type *InitElTy = ArrayInit->getElementType();
6656 if (InitElTy->isIntegerTy(ElementSize)) {
6657 // If Init is an initializer for an array of the expected type
6658 // and size, use it as is.
6659 Array = ArrayInit;
6660 ArrayTy = ArrayInit->getType();
6661 }
6662 }
6663
6664 if (!Array) {
6665 if (ElementSize != 8)
6666 // TODO: Handle conversions to larger integral types.
6667 return false;
6668
6669 // Otherwise extract the portion of the initializer starting
6670 // at Offset as an array of bytes, and reset Offset.
6672 if (!Init)
6673 return false;
6674
6675 Offset = 0;
6677 ArrayTy = dyn_cast<ArrayType>(Init->getType());
6678 }
6679
6680 uint64_t NumElts = ArrayTy->getArrayNumElements();
6681 if (Offset > NumElts)
6682 return false;
6683
6684 Slice.Array = Array;
6685 Slice.Offset = Offset;
6686 Slice.Length = NumElts - Offset;
6687 return true;
6688}
6689
6690/// Extract bytes from the initializer of the constant array V, which need
6691/// not be a nul-terminated string. On success, store the bytes in Str and
6692/// return true. When TrimAtNul is set, Str will contain only the bytes up
6693/// to but not including the first nul. Return false on failure.
6695 bool TrimAtNul) {
6697 if (!getConstantDataArrayInfo(V, Slice, 8))
6698 return false;
6699
6700 if (Slice.Array == nullptr) {
6701 if (TrimAtNul) {
6702 // Return a nul-terminated string even for an empty Slice. This is
6703 // safe because all existing SimplifyLibcalls callers require string
6704 // arguments and the behavior of the functions they fold is undefined
6705 // otherwise. Folding the calls this way is preferable to making
6706 // the undefined library calls, even though it prevents sanitizers
6707 // from reporting such calls.
6708 Str = StringRef();
6709 return true;
6710 }
6711 if (Slice.Length == 1) {
6712 Str = StringRef("", 1);
6713 return true;
6714 }
6715 // We cannot instantiate a StringRef as we do not have an appropriate string
6716 // of 0s at hand.
6717 return false;
6718 }
6719
6720 // Start out with the entire array in the StringRef.
6721 Str = Slice.Array->getAsString();
6722 // Skip over 'offset' bytes.
6723 Str = Str.substr(Slice.Offset);
6724
6725 if (TrimAtNul) {
6726 // Trim off the \0 and anything after it. If the array is not nul
6727 // terminated, we just return the whole end of string. The client may know
6728 // some other way that the string is length-bound.
6729 Str = Str.substr(0, Str.find('\0'));
6730 }
6731 return true;
6732}
6733
6734// These next two are very similar to the above, but also look through PHI
6735// nodes.
6736// TODO: See if we can integrate these two together.
6737
6738/// If we can compute the length of the string pointed to by
6739/// the specified pointer, return 'len+1'. If we can't, return 0.
6742 unsigned CharSize) {
6743 // Look through noop bitcast instructions.
6744 V = V->stripPointerCasts();
6745
6746 // If this is a PHI node, there are two cases: either we have already seen it
6747 // or we haven't.
6748 if (const PHINode *PN = dyn_cast<PHINode>(V)) {
6749 if (!PHIs.insert(PN).second)
6750 return ~0ULL; // already in the set.
6751
6752 // If it was new, see if all the input strings are the same length.
6753 uint64_t LenSoFar = ~0ULL;
6754 for (Value *IncValue : PN->incoming_values()) {
6755 uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize);
6756 if (Len == 0) return 0; // Unknown length -> unknown.
6757
6758 if (Len == ~0ULL) continue;
6759
6760 if (Len != LenSoFar && LenSoFar != ~0ULL)
6761 return 0; // Disagree -> unknown.
6762 LenSoFar = Len;
6763 }
6764
6765 // Success, all agree.
6766 return LenSoFar;
6767 }
6768
6769 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
6770 if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
6771 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);
6772 if (Len1 == 0) return 0;
6773 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);
6774 if (Len2 == 0) return 0;
6775 if (Len1 == ~0ULL) return Len2;
6776 if (Len2 == ~0ULL) return Len1;
6777 if (Len1 != Len2) return 0;
6778 return Len1;
6779 }
6780
6781 // Otherwise, see if we can read the string.
6783 if (!getConstantDataArrayInfo(V, Slice, CharSize))
6784 return 0;
6785
6786 if (Slice.Array == nullptr)
6787 // Zeroinitializer (including an empty one).
6788 return 1;
6789
6790 // Search for the first nul character. Return a conservative result even
6791 // when there is no nul. This is safe since otherwise the string function
6792 // being folded such as strlen is undefined, and can be preferable to
6793 // making the undefined library call.
6794 unsigned NullIndex = 0;
6795 for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) {
6796 if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0)
6797 break;
6798 }
6799
6800 return NullIndex + 1;
6801}
6802
6803/// If we can compute the length of the string pointed to by
6804/// the specified pointer, return 'len+1'. If we can't, return 0.
6805uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) {
6806 if (!V->getType()->isPointerTy())
6807 return 0;
6808
6810 uint64_t Len = GetStringLengthH(V, PHIs, CharSize);
6811 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
6812 // an empty string as a length.
6813 return Len == ~0ULL ? 1 : Len;
6814}
6815
6816const Value *
6818 bool MustPreserveNullness) {
6819 assert(Call &&
6820 "getArgumentAliasingToReturnedPointer only works on nonnull calls");
6821 if (const Value *RV = Call->getReturnedArgOperand())
6822 return RV;
6823 // This can be used only as a aliasing property.
6825 Call, MustPreserveNullness))
6826 return Call->getArgOperand(0);
6827 return nullptr;
6828}
6829
6831 const CallBase *Call, bool MustPreserveNullness) {
6832 switch (Call->getIntrinsicID()) {
6833 case Intrinsic::launder_invariant_group:
6834 case Intrinsic::strip_invariant_group:
6835 case Intrinsic::aarch64_irg:
6836 case Intrinsic::aarch64_tagp:
6837 // The amdgcn_make_buffer_rsrc function does not alter the address of the
6838 // input pointer (and thus preserve null-ness for the purposes of escape
6839 // analysis, which is where the MustPreserveNullness flag comes in to play).
6840 // However, it will not necessarily map ptr addrspace(N) null to ptr
6841 // addrspace(8) null, aka the "null descriptor", which has "all loads return
6842 // 0, all stores are dropped" semantics. Given the context of this intrinsic
6843 // list, no one should be relying on such a strict interpretation of
6844 // MustPreserveNullness (and, at time of writing, they are not), but we
6845 // document this fact out of an abundance of caution.
6846 case Intrinsic::amdgcn_make_buffer_rsrc:
6847 return true;
6848 case Intrinsic::ptrmask:
6849 return !MustPreserveNullness;
6850 case Intrinsic::threadlocal_address:
6851 // The underlying variable changes with thread ID. The Thread ID may change
6852 // at coroutine suspend points.
6853 return !Call->getParent()->getParent()->isPresplitCoroutine();
6854 default:
6855 return false;
6856 }
6857}
6858
6859/// \p PN defines a loop-variant pointer to an object. Check if the
6860/// previous iteration of the loop was referring to the same object as \p PN.
6862 const LoopInfo *LI) {
6863 // Find the loop-defined value.
6864 Loop *L = LI->getLoopFor(PN->getParent());
6865 if (PN->getNumIncomingValues() != 2)
6866 return true;
6867
6868 // Find the value from previous iteration.
6869 auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
6870 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6871 PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
6872 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6873 return true;
6874
6875 // If a new pointer is loaded in the loop, the pointer references a different
6876 // object in every iteration. E.g.:
6877 // for (i)
6878 // int *p = a[i];
6879 // ...
6880 if (auto *Load = dyn_cast<LoadInst>(PrevValue))
6881 if (!L->isLoopInvariant(Load->getPointerOperand()))
6882 return false;
6883 return true;
6884}
6885
6886const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup) {
6887 for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
6888 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
6889 const Value *PtrOp = GEP->getPointerOperand();
6890 if (!PtrOp->getType()->isPointerTy()) // Only handle scalar pointer base.
6891 return V;
6892 V = PtrOp;
6893 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
6894 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
6895 Value *NewV = cast<Operator>(V)->getOperand(0);
6896 if (!NewV->getType()->isPointerTy())
6897 return V;
6898 V = NewV;
6899 } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
6900 if (GA->isInterposable())
6901 return V;
6902 V = GA->getAliasee();
6903 } else {
6904 if (auto *PHI = dyn_cast<PHINode>(V)) {
6905 // Look through single-arg phi nodes created by LCSSA.
6906 if (PHI->getNumIncomingValues() == 1) {
6907 V = PHI->getIncomingValue(0);
6908 continue;
6909 }
6910 } else if (auto *Call = dyn_cast<CallBase>(V)) {
6911 // CaptureTracking can know about special capturing properties of some
6912 // intrinsics like launder.invariant.group, that can't be expressed with
6913 // the attributes, but have properties like returning aliasing pointer.
6914 // Because some analysis may assume that nocaptured pointer is not
6915 // returned from some special intrinsic (because function would have to
6916 // be marked with returns attribute), it is crucial to use this function
6917 // because it should be in sync with CaptureTracking. Not using it may
6918 // cause weird miscompilations where 2 aliasing pointers are assumed to
6919 // noalias.
6920 if (auto *RP = getArgumentAliasingToReturnedPointer(Call, false)) {
6921 V = RP;
6922 continue;
6923 }
6924 }
6925
6926 return V;
6927 }
6928 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
6929 }
6930 return V;
6931}
6932
6935 const LoopInfo *LI, unsigned MaxLookup) {
6938 Worklist.push_back(V);
6939 do {
6940 const Value *P = Worklist.pop_back_val();
6941 P = getUnderlyingObject(P, MaxLookup);
6942
6943 if (!Visited.insert(P).second)
6944 continue;
6945
6946 if (auto *SI = dyn_cast<SelectInst>(P)) {
6947 Worklist.push_back(SI->getTrueValue());
6948 Worklist.push_back(SI->getFalseValue());
6949 continue;
6950 }
6951
6952 if (auto *PN = dyn_cast<PHINode>(P)) {
6953 // If this PHI changes the underlying object in every iteration of the
6954 // loop, don't look through it. Consider:
6955 // int **A;
6956 // for (i) {
6957 // Prev = Curr; // Prev = PHI (Prev_0, Curr)
6958 // Curr = A[i];
6959 // *Prev, *Curr;
6960 //
6961 // Prev is tracking Curr one iteration behind so they refer to different
6962 // underlying objects.
6963 if (!LI || !LI->isLoopHeader(PN->getParent()) ||
6965 append_range(Worklist, PN->incoming_values());
6966 else
6967 Objects.push_back(P);
6968 continue;
6969 }
6970
6971 Objects.push_back(P);
6972 } while (!Worklist.empty());
6973}
6974
6976 const unsigned MaxVisited = 8;
6977
6980 Worklist.push_back(V);
6981 const Value *Object = nullptr;
6982 // Used as fallback if we can't find a common underlying object through
6983 // recursion.
6984 bool First = true;
6985 const Value *FirstObject = getUnderlyingObject(V);
6986 do {
6987 const Value *P = Worklist.pop_back_val();
6988 P = First ? FirstObject : getUnderlyingObject(P);
6989 First = false;
6990
6991 if (!Visited.insert(P).second)
6992 continue;
6993
6994 if (Visited.size() == MaxVisited)
6995 return FirstObject;
6996
6997 if (auto *SI = dyn_cast<SelectInst>(P)) {
6998 Worklist.push_back(SI->getTrueValue());
6999 Worklist.push_back(SI->getFalseValue());
7000 continue;
7001 }
7002
7003 if (auto *PN = dyn_cast<PHINode>(P)) {
7004 append_range(Worklist, PN->incoming_values());
7005 continue;
7006 }
7007
7008 if (!Object)
7009 Object = P;
7010 else if (Object != P)
7011 return FirstObject;
7012 } while (!Worklist.empty());
7013
7014 return Object ? Object : FirstObject;
7015}
7016
7017/// This is the function that does the work of looking through basic
7018/// ptrtoint+arithmetic+inttoptr sequences.
7019static const Value *getUnderlyingObjectFromInt(const Value *V) {
7020 do {
7021 if (const Operator *U = dyn_cast<Operator>(V)) {
7022 // If we find a ptrtoint, we can transfer control back to the
7023 // regular getUnderlyingObjectFromInt.
7024 if (U->getOpcode() == Instruction::PtrToInt)
7025 return U->getOperand(0);
7026 // If we find an add of a constant, a multiplied value, or a phi, it's
7027 // likely that the other operand will lead us to the base
7028 // object. We don't have to worry about the case where the
7029 // object address is somehow being computed by the multiply,
7030 // because our callers only care when the result is an
7031 // identifiable object.
7032 if (U->getOpcode() != Instruction::Add ||
7033 (!isa<ConstantInt>(U->getOperand(1)) &&
7034 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
7035 !isa<PHINode>(U->getOperand(1))))
7036 return V;
7037 V = U->getOperand(0);
7038 } else {
7039 return V;
7040 }
7041 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
7042 } while (true);
7043}
7044
7045/// This is a wrapper around getUnderlyingObjects and adds support for basic
7046/// ptrtoint+arithmetic+inttoptr sequences.
7047/// It returns false if unidentified object is found in getUnderlyingObjects.
7049 SmallVectorImpl<Value *> &Objects) {
7051 SmallVector<const Value *, 4> Working(1, V);
7052 do {
7053 V = Working.pop_back_val();
7054
7056 getUnderlyingObjects(V, Objs);
7057
7058 for (const Value *V : Objs) {
7059 if (!Visited.insert(V).second)
7060 continue;
7061 if (Operator::getOpcode(V) == Instruction::IntToPtr) {
7062 const Value *O =
7063 getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
7064 if (O->getType()->isPointerTy()) {
7065 Working.push_back(O);
7066 continue;
7067 }
7068 }
7069 // If getUnderlyingObjects fails to find an identifiable object,
7070 // getUnderlyingObjectsForCodeGen also fails for safety.
7071 if (!isIdentifiedObject(V)) {
7072 Objects.clear();
7073 return false;
7074 }
7075 Objects.push_back(const_cast<Value *>(V));
7076 }
7077 } while (!Working.empty());
7078 return true;
7079}
7080
7082 AllocaInst *Result = nullptr;
7084 SmallVector<Value *, 4> Worklist;
7085
7086 auto AddWork = [&](Value *V) {
7087 if (Visited.insert(V).second)
7088 Worklist.push_back(V);
7089 };
7090
7091 AddWork(V);
7092 do {
7093 V = Worklist.pop_back_val();
7094 assert(Visited.count(V));
7095
7096 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
7097 if (Result && Result != AI)
7098 return nullptr;
7099 Result = AI;
7100 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
7101 AddWork(CI->getOperand(0));
7102 } else if (PHINode *PN = dyn_cast<PHINode>(V)) {
7103 for (Value *IncValue : PN->incoming_values())
7104 AddWork(IncValue);
7105 } else if (auto *SI = dyn_cast<SelectInst>(V)) {
7106 AddWork(SI->getTrueValue());
7107 AddWork(SI->getFalseValue());
7109 if (OffsetZero && !GEP->hasAllZeroIndices())
7110 return nullptr;
7111 AddWork(GEP->getPointerOperand());
7112 } else if (CallBase *CB = dyn_cast<CallBase>(V)) {
7113 Value *Returned = CB->getReturnedArgOperand();
7114 if (Returned)
7115 AddWork(Returned);
7116 else
7117 return nullptr;
7118 } else {
7119 return nullptr;
7120 }
7121 } while (!Worklist.empty());
7122
7123 return Result;
7124}
7125
7127 const Value *V, bool AllowLifetime, bool AllowDroppable) {
7128 for (const User *U : V->users()) {
7130 if (!II)
7131 return false;
7132
7133 if (AllowLifetime && II->isLifetimeStartOrEnd())
7134 continue;
7135
7136 if (AllowDroppable && II->isDroppable())
7137 continue;
7138
7139 return false;
7140 }
7141 return true;
7142}
7143
7146 V, /* AllowLifetime */ true, /* AllowDroppable */ false);
7147}
7150 V, /* AllowLifetime */ true, /* AllowDroppable */ true);
7151}
7152
7154 if (auto *II = dyn_cast<IntrinsicInst>(I))
7155 return isTriviallyVectorizable(II->getIntrinsicID());
7156 auto *Shuffle = dyn_cast<ShuffleVectorInst>(I);
7157 return (!Shuffle || Shuffle->isSelect()) &&
7159}
7160
7162 const Instruction *Inst, const Instruction *CtxI, AssumptionCache *AC,
7163 const DominatorTree *DT, const TargetLibraryInfo *TLI, bool UseVariableInfo,
7164 bool IgnoreUBImplyingAttrs) {
7165 return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI,
7166 AC, DT, TLI, UseVariableInfo,
7167 IgnoreUBImplyingAttrs);
7168}
7169
7171 unsigned Opcode, const Instruction *Inst, const Instruction *CtxI,
7172 AssumptionCache *AC, const DominatorTree *DT, const TargetLibraryInfo *TLI,
7173 bool UseVariableInfo, bool IgnoreUBImplyingAttrs) {
7174#ifndef NDEBUG
7175 if (Inst->getOpcode() != Opcode) {
7176 // Check that the operands are actually compatible with the Opcode override.
7177 auto hasEqualReturnAndLeadingOperandTypes =
7178 [](const Instruction *Inst, unsigned NumLeadingOperands) {
7179 if (Inst->getNumOperands() < NumLeadingOperands)
7180 return false;
7181 const Type *ExpectedType = Inst->getType();
7182 for (unsigned ItOp = 0; ItOp < NumLeadingOperands; ++ItOp)
7183 if (Inst->getOperand(ItOp)->getType() != ExpectedType)
7184 return false;
7185 return true;
7186 };
7188 hasEqualReturnAndLeadingOperandTypes(Inst, 2));
7189 assert(!Instruction::isUnaryOp(Opcode) ||
7190 hasEqualReturnAndLeadingOperandTypes(Inst, 1));
7191 }
7192#endif
7193
7194 switch (Opcode) {
7195 default:
7196 return true;
7197 case Instruction::UDiv:
7198 case Instruction::URem: {
7199 // x / y is undefined if y == 0.
7200 const APInt *V;
7201 if (match(Inst->getOperand(1), m_APInt(V)))
7202 return *V != 0;
7203 return false;
7204 }
7205 case Instruction::SDiv:
7206 case Instruction::SRem: {
7207 // x / y is undefined if y == 0 or x == INT_MIN and y == -1
7208 const APInt *Numerator, *Denominator;
7209 if (!match(Inst->getOperand(1), m_APInt(Denominator)))
7210 return false;
7211 // We cannot hoist this division if the denominator is 0.
7212 if (*Denominator == 0)
7213 return false;
7214 // It's safe to hoist if the denominator is not 0 or -1.
7215 if (!Denominator->isAllOnes())
7216 return true;
7217 // At this point we know that the denominator is -1. It is safe to hoist as
7218 // long we know that the numerator is not INT_MIN.
7219 if (match(Inst->getOperand(0), m_APInt(Numerator)))
7220 return !Numerator->isMinSignedValue();
7221 // The numerator *might* be MinSignedValue.
7222 return false;
7223 }
7224 case Instruction::Load: {
7225 if (!UseVariableInfo)
7226 return false;
7227
7228 const LoadInst *LI = dyn_cast<LoadInst>(Inst);
7229 if (!LI)
7230 return false;
7231 if (mustSuppressSpeculation(*LI))
7232 return false;
7233 const DataLayout &DL = LI->getDataLayout();
7235 LI->getType(), LI->getAlign(), DL,
7236 CtxI, AC, DT, TLI);
7237 }
7238 case Instruction::Call: {
7239 auto *CI = dyn_cast<const CallInst>(Inst);
7240 if (!CI)
7241 return false;
7242 const Function *Callee = CI->getCalledFunction();
7243
7244 // The called function could have undefined behavior or side-effects, even
7245 // if marked readnone nounwind.
7246 if (!Callee || !Callee->isSpeculatable())
7247 return false;
7248 // Since the operands may be changed after hoisting, undefined behavior may
7249 // be triggered by some UB-implying attributes.
7250 return IgnoreUBImplyingAttrs || !CI->hasUBImplyingAttrs();
7251 }
7252 case Instruction::VAArg:
7253 case Instruction::Alloca:
7254 case Instruction::Invoke:
7255 case Instruction::CallBr:
7256 case Instruction::PHI:
7257 case Instruction::Store:
7258 case Instruction::Ret:
7259 case Instruction::Br:
7260 case Instruction::IndirectBr:
7261 case Instruction::Switch:
7262 case Instruction::Unreachable:
7263 case Instruction::Fence:
7264 case Instruction::AtomicRMW:
7265 case Instruction::AtomicCmpXchg:
7266 case Instruction::LandingPad:
7267 case Instruction::Resume:
7268 case Instruction::CatchSwitch:
7269 case Instruction::CatchPad:
7270 case Instruction::CatchRet:
7271 case Instruction::CleanupPad:
7272 case Instruction::CleanupRet:
7273 return false; // Misc instructions which have effects
7274 }
7275}
7276
7278 if (I.mayReadOrWriteMemory())
7279 // Memory dependency possible
7280 return true;
7282 // Can't move above a maythrow call or infinite loop. Or if an
7283 // inalloca alloca, above a stacksave call.
7284 return true;
7286 // 1) Can't reorder two inf-loop calls, even if readonly
7287 // 2) Also can't reorder an inf-loop call below a instruction which isn't
7288 // safe to speculative execute. (Inverse of above)
7289 return true;
7290 return false;
7291}
7292
7293/// Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
7307
7308/// Combine constant ranges from computeConstantRange() and computeKnownBits().
7311 bool ForSigned,
7312 const SimplifyQuery &SQ) {
7313 ConstantRange CR1 =
7314 ConstantRange::fromKnownBits(V.getKnownBits(SQ), ForSigned);
7315 ConstantRange CR2 = computeConstantRange(V, ForSigned, SQ.IIQ.UseInstrInfo);
7318 return CR1.intersectWith(CR2, RangeType);
7319}
7320
7322 const Value *RHS,
7323 const SimplifyQuery &SQ,
7324 bool IsNSW) {
7325 ConstantRange LHSRange =
7326 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7327 ConstantRange RHSRange =
7328 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7329
7330 // mul nsw of two non-negative numbers is also nuw.
7331 if (IsNSW && LHSRange.isAllNonNegative() && RHSRange.isAllNonNegative())
7333
7334 return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange));
7335}
7336
7338 const Value *RHS,
7339 const SimplifyQuery &SQ) {
7340 // Multiplying n * m significant bits yields a result of n + m significant
7341 // bits. If the total number of significant bits does not exceed the
7342 // result bit width (minus 1), there is no overflow.
7343 // This means if we have enough leading sign bits in the operands
7344 // we can guarantee that the result does not overflow.
7345 // Ref: "Hacker's Delight" by Henry Warren
7346 unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
7347
7348 // Note that underestimating the number of sign bits gives a more
7349 // conservative answer.
7350 unsigned SignBits =
7351 ::ComputeNumSignBits(LHS, SQ) + ::ComputeNumSignBits(RHS, SQ);
7352
7353 // First handle the easy case: if we have enough sign bits there's
7354 // definitely no overflow.
7355 if (SignBits > BitWidth + 1)
7357
7358 // There are two ambiguous cases where there can be no overflow:
7359 // SignBits == BitWidth + 1 and
7360 // SignBits == BitWidth
7361 // The second case is difficult to check, therefore we only handle the
7362 // first case.
7363 if (SignBits == BitWidth + 1) {
7364 // It overflows only when both arguments are negative and the true
7365 // product is exactly the minimum negative number.
7366 // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
7367 // For simplicity we just check if at least one side is not negative.
7368 KnownBits LHSKnown = computeKnownBits(LHS, SQ);
7369 KnownBits RHSKnown = computeKnownBits(RHS, SQ);
7370 if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative())
7372 }
7374}
7375
7378 const WithCache<const Value *> &RHS,
7379 const SimplifyQuery &SQ) {
7380 ConstantRange LHSRange =
7381 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7382 ConstantRange RHSRange =
7383 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7384 return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange));
7385}
7386
7387static OverflowResult
7390 const AddOperator *Add, const SimplifyQuery &SQ) {
7391 if (Add && Add->hasNoSignedWrap()) {
7393 }
7394
7395 // If LHS and RHS each have at least two sign bits, the addition will look
7396 // like
7397 //
7398 // XX..... +
7399 // YY.....
7400 //
7401 // If the carry into the most significant position is 0, X and Y can't both
7402 // be 1 and therefore the carry out of the addition is also 0.
7403 //
7404 // If the carry into the most significant position is 1, X and Y can't both
7405 // be 0 and therefore the carry out of the addition is also 1.
7406 //
7407 // Since the carry into the most significant position is always equal to
7408 // the carry out of the addition, there is no signed overflow.
7409 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
7411
7412 ConstantRange LHSRange =
7413 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7414 ConstantRange RHSRange =
7415 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7416 OverflowResult OR =
7417 mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange));
7419 return OR;
7420
7421 // The remaining code needs Add to be available. Early returns if not so.
7422 if (!Add)
7424
7425 // If the sign of Add is the same as at least one of the operands, this add
7426 // CANNOT overflow. If this can be determined from the known bits of the
7427 // operands the above signedAddMayOverflow() check will have already done so.
7428 // The only other way to improve on the known bits is from an assumption, so
7429 // call computeKnownBitsFromContext() directly.
7430 bool LHSOrRHSKnownNonNegative =
7431 (LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative());
7432 bool LHSOrRHSKnownNegative =
7433 (LHSRange.isAllNegative() || RHSRange.isAllNegative());
7434 if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
7435 KnownBits AddKnown(LHSRange.getBitWidth());
7436 computeKnownBitsFromContext(Add, AddKnown, SQ);
7437 if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||
7438 (AddKnown.isNegative() && LHSOrRHSKnownNegative))
7440 }
7441
7443}
7444
7446 const Value *RHS,
7447 const SimplifyQuery &SQ) {
7448 // X - (X % ?)
7449 // The remainder of a value can't have greater magnitude than itself,
7450 // so the subtraction can't overflow.
7451
7452 // X - (X -nuw ?)
7453 // In the minimal case, this would simplify to "?", so there's no subtract
7454 // at all. But if this analysis is used to peek through casts, for example,
7455 // then determining no-overflow may allow other transforms.
7456
7457 // TODO: There are other patterns like this.
7458 // See simplifyICmpWithBinOpOnLHS() for candidates.
7459 if (match(RHS, m_URem(m_Specific(LHS), m_Value())) ||
7460 match(RHS, m_NUWSub(m_Specific(LHS), m_Value())))
7461 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7463
7464 if (auto C = isImpliedByDomCondition(CmpInst::ICMP_UGE, LHS, RHS, SQ.CxtI,
7465 SQ.DL)) {
7466 if (*C)
7469 }
7470
7471 ConstantRange LHSRange =
7472 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7473 ConstantRange RHSRange =
7474 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7475 return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange));
7476}
7477
7479 const Value *RHS,
7480 const SimplifyQuery &SQ) {
7481 // X - (X % ?)
7482 // The remainder of a value can't have greater magnitude than itself,
7483 // so the subtraction can't overflow.
7484
7485 // X - (X -nsw ?)
7486 // In the minimal case, this would simplify to "?", so there's no subtract
7487 // at all. But if this analysis is used to peek through casts, for example,
7488 // then determining no-overflow may allow other transforms.
7489 if (match(RHS, m_SRem(m_Specific(LHS), m_Value())) ||
7490 match(RHS, m_NSWSub(m_Specific(LHS), m_Value())))
7491 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7493
7494 // If LHS and RHS each have at least two sign bits, the subtraction
7495 // cannot overflow.
7496 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
7498
7499 ConstantRange LHSRange =
7500 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7501 ConstantRange RHSRange =
7502 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7503 return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange));
7504}
7505
7507 const DominatorTree &DT) {
7508 SmallVector<const BranchInst *, 2> GuardingBranches;
7510
7511 for (const User *U : WO->users()) {
7512 if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) {
7513 assert(EVI->getNumIndices() == 1 && "Obvious from CI's type");
7514
7515 if (EVI->getIndices()[0] == 0)
7516 Results.push_back(EVI);
7517 else {
7518 assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type");
7519
7520 for (const auto *U : EVI->users())
7521 if (const auto *B = dyn_cast<BranchInst>(U)) {
7522 assert(B->isConditional() && "How else is it using an i1?");
7523 GuardingBranches.push_back(B);
7524 }
7525 }
7526 } else {
7527 // We are using the aggregate directly in a way we don't want to analyze
7528 // here (storing it to a global, say).
7529 return false;
7530 }
7531 }
7532
7533 auto AllUsesGuardedByBranch = [&](const BranchInst *BI) {
7534 BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1));
7535 if (!NoWrapEdge.isSingleEdge())
7536 return false;
7537
7538 // Check if all users of the add are provably no-wrap.
7539 for (const auto *Result : Results) {
7540 // If the extractvalue itself is not executed on overflow, the we don't
7541 // need to check each use separately, since domination is transitive.
7542 if (DT.dominates(NoWrapEdge, Result->getParent()))
7543 continue;
7544
7545 for (const auto &RU : Result->uses())
7546 if (!DT.dominates(NoWrapEdge, RU))
7547 return false;
7548 }
7549
7550 return true;
7551 };
7552
7553 return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
7554}
7555
7556/// Shifts return poison if shiftwidth is larger than the bitwidth.
7557static bool shiftAmountKnownInRange(const Value *ShiftAmount) {
7558 auto *C = dyn_cast<Constant>(ShiftAmount);
7559 if (!C)
7560 return false;
7561
7562 // Shifts return poison if shiftwidth is larger than the bitwidth.
7564 if (auto *FVTy = dyn_cast<FixedVectorType>(C->getType())) {
7565 unsigned NumElts = FVTy->getNumElements();
7566 for (unsigned i = 0; i < NumElts; ++i)
7567 ShiftAmounts.push_back(C->getAggregateElement(i));
7568 } else if (isa<ScalableVectorType>(C->getType()))
7569 return false; // Can't tell, just return false to be safe
7570 else
7571 ShiftAmounts.push_back(C);
7572
7573 bool Safe = llvm::all_of(ShiftAmounts, [](const Constant *C) {
7574 auto *CI = dyn_cast_or_null<ConstantInt>(C);
7575 return CI && CI->getValue().ult(C->getType()->getIntegerBitWidth());
7576 });
7577
7578 return Safe;
7579}
7580
7586
7588 return (unsigned(Kind) & unsigned(UndefPoisonKind::PoisonOnly)) != 0;
7589}
7590
7592 return (unsigned(Kind) & unsigned(UndefPoisonKind::UndefOnly)) != 0;
7593}
7594
7596 bool ConsiderFlagsAndMetadata) {
7597
7598 if (ConsiderFlagsAndMetadata && includesPoison(Kind) &&
7599 Op->hasPoisonGeneratingAnnotations())
7600 return true;
7601
7602 unsigned Opcode = Op->getOpcode();
7603
7604 // Check whether opcode is a poison/undef-generating operation
7605 switch (Opcode) {
7606 case Instruction::Shl:
7607 case Instruction::AShr:
7608 case Instruction::LShr:
7609 return includesPoison(Kind) && !shiftAmountKnownInRange(Op->getOperand(1));
7610 case Instruction::FPToSI:
7611 case Instruction::FPToUI:
7612 // fptosi/ui yields poison if the resulting value does not fit in the
7613 // destination type.
7614 return true;
7615 case Instruction::Call:
7616 if (auto *II = dyn_cast<IntrinsicInst>(Op)) {
7617 switch (II->getIntrinsicID()) {
7618 // NOTE: Use IntrNoCreateUndefOrPoison when possible.
7619 case Intrinsic::ctlz:
7620 case Intrinsic::cttz:
7621 case Intrinsic::abs:
7622 // We're not considering flags so it is safe to just return false.
7623 return false;
7624 case Intrinsic::sshl_sat:
7625 case Intrinsic::ushl_sat:
7626 if (!includesPoison(Kind) ||
7627 shiftAmountKnownInRange(II->getArgOperand(1)))
7628 return false;
7629 break;
7630 }
7631 }
7632 [[fallthrough]];
7633 case Instruction::CallBr:
7634 case Instruction::Invoke: {
7635 const auto *CB = cast<CallBase>(Op);
7636 return !CB->hasRetAttr(Attribute::NoUndef) &&
7637 !CB->hasFnAttr(Attribute::NoCreateUndefOrPoison);
7638 }
7639 case Instruction::InsertElement:
7640 case Instruction::ExtractElement: {
7641 // If index exceeds the length of the vector, it returns poison
7642 auto *VTy = cast<VectorType>(Op->getOperand(0)->getType());
7643 unsigned IdxOp = Op->getOpcode() == Instruction::InsertElement ? 2 : 1;
7644 auto *Idx = dyn_cast<ConstantInt>(Op->getOperand(IdxOp));
7645 if (includesPoison(Kind))
7646 return !Idx ||
7647 Idx->getValue().uge(VTy->getElementCount().getKnownMinValue());
7648 return false;
7649 }
7650 case Instruction::ShuffleVector: {
7652 ? cast<ConstantExpr>(Op)->getShuffleMask()
7653 : cast<ShuffleVectorInst>(Op)->getShuffleMask();
7654 return includesPoison(Kind) && is_contained(Mask, PoisonMaskElem);
7655 }
7656 case Instruction::FNeg:
7657 case Instruction::PHI:
7658 case Instruction::Select:
7659 case Instruction::ExtractValue:
7660 case Instruction::InsertValue:
7661 case Instruction::Freeze:
7662 case Instruction::ICmp:
7663 case Instruction::FCmp:
7664 case Instruction::GetElementPtr:
7665 return false;
7666 case Instruction::AddrSpaceCast:
7667 return true;
7668 default: {
7669 const auto *CE = dyn_cast<ConstantExpr>(Op);
7670 if (isa<CastInst>(Op) || (CE && CE->isCast()))
7671 return false;
7672 else if (Instruction::isBinaryOp(Opcode))
7673 return false;
7674 // Be conservative and return true.
7675 return true;
7676 }
7677 }
7678}
7679
7681 bool ConsiderFlagsAndMetadata) {
7682 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::UndefOrPoison,
7683 ConsiderFlagsAndMetadata);
7684}
7685
7686bool llvm::canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata) {
7687 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::PoisonOnly,
7688 ConsiderFlagsAndMetadata);
7689}
7690
7691static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V,
7692 unsigned Depth) {
7693 if (ValAssumedPoison == V)
7694 return true;
7695
7696 const unsigned MaxDepth = 2;
7697 if (Depth >= MaxDepth)
7698 return false;
7699
7700 if (const auto *I = dyn_cast<Instruction>(V)) {
7701 if (any_of(I->operands(), [=](const Use &Op) {
7702 return propagatesPoison(Op) &&
7703 directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1);
7704 }))
7705 return true;
7706
7707 // V = extractvalue V0, idx
7708 // V2 = extractvalue V0, idx2
7709 // V0's elements are all poison or not. (e.g., add_with_overflow)
7710 const WithOverflowInst *II;
7712 (match(ValAssumedPoison, m_ExtractValue(m_Specific(II))) ||
7713 llvm::is_contained(II->args(), ValAssumedPoison)))
7714 return true;
7715 }
7716 return false;
7717}
7718
7719static bool impliesPoison(const Value *ValAssumedPoison, const Value *V,
7720 unsigned Depth) {
7721 if (isGuaranteedNotToBePoison(ValAssumedPoison))
7722 return true;
7723
7724 if (directlyImpliesPoison(ValAssumedPoison, V, /* Depth */ 0))
7725 return true;
7726
7727 const unsigned MaxDepth = 2;
7728 if (Depth >= MaxDepth)
7729 return false;
7730
7731 const auto *I = dyn_cast<Instruction>(ValAssumedPoison);
7732 if (I && !canCreatePoison(cast<Operator>(I))) {
7733 return all_of(I->operands(), [=](const Value *Op) {
7734 return impliesPoison(Op, V, Depth + 1);
7735 });
7736 }
7737 return false;
7738}
7739
7740bool llvm::impliesPoison(const Value *ValAssumedPoison, const Value *V) {
7741 return ::impliesPoison(ValAssumedPoison, V, /* Depth */ 0);
7742}
7743
7744static bool programUndefinedIfUndefOrPoison(const Value *V, bool PoisonOnly);
7745
7747 const Value *V, AssumptionCache *AC, const Instruction *CtxI,
7748 const DominatorTree *DT, unsigned Depth, UndefPoisonKind Kind) {
7750 return false;
7751
7752 if (isa<MetadataAsValue>(V))
7753 return false;
7754
7755 if (const auto *A = dyn_cast<Argument>(V)) {
7756 if (A->hasAttribute(Attribute::NoUndef) ||
7757 A->hasAttribute(Attribute::Dereferenceable) ||
7758 A->hasAttribute(Attribute::DereferenceableOrNull))
7759 return true;
7760 }
7761
7762 if (auto *C = dyn_cast<Constant>(V)) {
7763 if (isa<PoisonValue>(C))
7764 return !includesPoison(Kind);
7765
7766 if (isa<UndefValue>(C))
7767 return !includesUndef(Kind);
7768
7771 return true;
7772
7773 if (C->getType()->isVectorTy()) {
7774 if (isa<ConstantExpr>(C)) {
7775 // Scalable vectors can use a ConstantExpr to build a splat.
7776 if (Constant *SplatC = C->getSplatValue())
7777 if (isa<ConstantInt>(SplatC) || isa<ConstantFP>(SplatC))
7778 return true;
7779 } else {
7780 if (includesUndef(Kind) && C->containsUndefElement())
7781 return false;
7782 if (includesPoison(Kind) && C->containsPoisonElement())
7783 return false;
7784 return !C->containsConstantExpression();
7785 }
7786 }
7787 }
7788
7789 // Strip cast operations from a pointer value.
7790 // Note that stripPointerCastsSameRepresentation can strip off getelementptr
7791 // inbounds with zero offset. To guarantee that the result isn't poison, the
7792 // stripped pointer is checked as it has to be pointing into an allocated
7793 // object or be null `null` to ensure `inbounds` getelement pointers with a
7794 // zero offset could not produce poison.
7795 // It can strip off addrspacecast that do not change bit representation as
7796 // well. We believe that such addrspacecast is equivalent to no-op.
7797 auto *StrippedV = V->stripPointerCastsSameRepresentation();
7798 if (isa<AllocaInst>(StrippedV) || isa<GlobalVariable>(StrippedV) ||
7799 isa<Function>(StrippedV) || isa<ConstantPointerNull>(StrippedV))
7800 return true;
7801
7802 auto OpCheck = [&](const Value *V) {
7803 return isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth + 1, Kind);
7804 };
7805
7806 if (auto *Opr = dyn_cast<Operator>(V)) {
7807 // If the value is a freeze instruction, then it can never
7808 // be undef or poison.
7809 if (isa<FreezeInst>(V))
7810 return true;
7811
7812 if (const auto *CB = dyn_cast<CallBase>(V)) {
7813 if (CB->hasRetAttr(Attribute::NoUndef) ||
7814 CB->hasRetAttr(Attribute::Dereferenceable) ||
7815 CB->hasRetAttr(Attribute::DereferenceableOrNull))
7816 return true;
7817 }
7818
7819 if (!::canCreateUndefOrPoison(Opr, Kind,
7820 /*ConsiderFlagsAndMetadata=*/true)) {
7821 if (const auto *PN = dyn_cast<PHINode>(V)) {
7822 unsigned Num = PN->getNumIncomingValues();
7823 bool IsWellDefined = true;
7824 for (unsigned i = 0; i < Num; ++i) {
7825 if (PN == PN->getIncomingValue(i))
7826 continue;
7827 auto *TI = PN->getIncomingBlock(i)->getTerminator();
7828 if (!isGuaranteedNotToBeUndefOrPoison(PN->getIncomingValue(i), AC, TI,
7829 DT, Depth + 1, Kind)) {
7830 IsWellDefined = false;
7831 break;
7832 }
7833 }
7834 if (IsWellDefined)
7835 return true;
7836 } else if (auto *Splat = isa<ShuffleVectorInst>(Opr) ? getSplatValue(Opr)
7837 : nullptr) {
7838 // For splats we only need to check the value being splatted.
7839 if (OpCheck(Splat))
7840 return true;
7841 } else if (all_of(Opr->operands(), OpCheck))
7842 return true;
7843 }
7844 }
7845
7846 if (auto *I = dyn_cast<LoadInst>(V))
7847 if (I->hasMetadata(LLVMContext::MD_noundef) ||
7848 I->hasMetadata(LLVMContext::MD_dereferenceable) ||
7849 I->hasMetadata(LLVMContext::MD_dereferenceable_or_null))
7850 return true;
7851
7853 return true;
7854
7855 // CxtI may be null or a cloned instruction.
7856 if (!CtxI || !CtxI->getParent() || !DT)
7857 return false;
7858
7859 auto *DNode = DT->getNode(CtxI->getParent());
7860 if (!DNode)
7861 // Unreachable block
7862 return false;
7863
7864 // If V is used as a branch condition before reaching CtxI, V cannot be
7865 // undef or poison.
7866 // br V, BB1, BB2
7867 // BB1:
7868 // CtxI ; V cannot be undef or poison here
7869 auto *Dominator = DNode->getIDom();
7870 // This check is purely for compile time reasons: we can skip the IDom walk
7871 // if what we are checking for includes undef and the value is not an integer.
7872 if (!includesUndef(Kind) || V->getType()->isIntegerTy())
7873 while (Dominator) {
7874 auto *TI = Dominator->getBlock()->getTerminator();
7875
7876 Value *Cond = nullptr;
7877 if (auto BI = dyn_cast_or_null<BranchInst>(TI)) {
7878 if (BI->isConditional())
7879 Cond = BI->getCondition();
7880 } else if (auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
7881 Cond = SI->getCondition();
7882 }
7883
7884 if (Cond) {
7885 if (Cond == V)
7886 return true;
7887 else if (!includesUndef(Kind) && isa<Operator>(Cond)) {
7888 // For poison, we can analyze further
7889 auto *Opr = cast<Operator>(Cond);
7890 if (any_of(Opr->operands(), [V](const Use &U) {
7891 return V == U && propagatesPoison(U);
7892 }))
7893 return true;
7894 }
7895 }
7896
7897 Dominator = Dominator->getIDom();
7898 }
7899
7900 if (AC && getKnowledgeValidInContext(V, {Attribute::NoUndef}, *AC, CtxI, DT))
7901 return true;
7902
7903 return false;
7904}
7905
7907 const Instruction *CtxI,
7908 const DominatorTree *DT,
7909 unsigned Depth) {
7910 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7912}
7913
7915 const Instruction *CtxI,
7916 const DominatorTree *DT, unsigned Depth) {
7917 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7919}
7920
7922 const Instruction *CtxI,
7923 const DominatorTree *DT, unsigned Depth) {
7924 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7926}
7927
7928/// Return true if undefined behavior would provably be executed on the path to
7929/// OnPathTo if Root produced a posion result. Note that this doesn't say
7930/// anything about whether OnPathTo is actually executed or whether Root is
7931/// actually poison. This can be used to assess whether a new use of Root can
7932/// be added at a location which is control equivalent with OnPathTo (such as
7933/// immediately before it) without introducing UB which didn't previously
7934/// exist. Note that a false result conveys no information.
7936 Instruction *OnPathTo,
7937 DominatorTree *DT) {
7938 // Basic approach is to assume Root is poison, propagate poison forward
7939 // through all users we can easily track, and then check whether any of those
7940 // users are provable UB and must execute before out exiting block might
7941 // exit.
7942
7943 // The set of all recursive users we've visited (which are assumed to all be
7944 // poison because of said visit)
7947 Worklist.push_back(Root);
7948 while (!Worklist.empty()) {
7949 const Instruction *I = Worklist.pop_back_val();
7950
7951 // If we know this must trigger UB on a path leading our target.
7952 if (mustTriggerUB(I, KnownPoison) && DT->dominates(I, OnPathTo))
7953 return true;
7954
7955 // If we can't analyze propagation through this instruction, just skip it
7956 // and transitive users. Safe as false is a conservative result.
7957 if (I != Root && !any_of(I->operands(), [&KnownPoison](const Use &U) {
7958 return KnownPoison.contains(U) && propagatesPoison(U);
7959 }))
7960 continue;
7961
7962 if (KnownPoison.insert(I).second)
7963 for (const User *User : I->users())
7964 Worklist.push_back(cast<Instruction>(User));
7965 }
7966
7967 // Might be non-UB, or might have a path we couldn't prove must execute on
7968 // way to exiting bb.
7969 return false;
7970}
7971
7973 const SimplifyQuery &SQ) {
7974 return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1),
7975 Add, SQ);
7976}
7977
7980 const WithCache<const Value *> &RHS,
7981 const SimplifyQuery &SQ) {
7982 return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, SQ);
7983}
7984
7986 // Note: An atomic operation isn't guaranteed to return in a reasonable amount
7987 // of time because it's possible for another thread to interfere with it for an
7988 // arbitrary length of time, but programs aren't allowed to rely on that.
7989
7990 // If there is no successor, then execution can't transfer to it.
7991 if (isa<ReturnInst>(I))
7992 return false;
7994 return false;
7995
7996 // Note: Do not add new checks here; instead, change Instruction::mayThrow or
7997 // Instruction::willReturn.
7998 //
7999 // FIXME: Move this check into Instruction::willReturn.
8000 if (isa<CatchPadInst>(I)) {
8001 switch (classifyEHPersonality(I->getFunction()->getPersonalityFn())) {
8002 default:
8003 // A catchpad may invoke exception object constructors and such, which
8004 // in some languages can be arbitrary code, so be conservative by default.
8005 return false;
8007 // For CoreCLR, it just involves a type test.
8008 return true;
8009 }
8010 }
8011
8012 // An instruction that returns without throwing must transfer control flow
8013 // to a successor.
8014 return !I->mayThrow() && I->willReturn();
8015}
8016
8018 // TODO: This is slightly conservative for invoke instruction since exiting
8019 // via an exception *is* normal control for them.
8020 for (const Instruction &I : *BB)
8022 return false;
8023 return true;
8024}
8025
8032
8035 assert(ScanLimit && "scan limit must be non-zero");
8036 for (const Instruction &I : Range) {
8037 if (--ScanLimit == 0)
8038 return false;
8040 return false;
8041 }
8042 return true;
8043}
8044
8046 const Loop *L) {
8047 // The loop header is guaranteed to be executed for every iteration.
8048 //
8049 // FIXME: Relax this constraint to cover all basic blocks that are
8050 // guaranteed to be executed at every iteration.
8051 if (I->getParent() != L->getHeader()) return false;
8052
8053 for (const Instruction &LI : *L->getHeader()) {
8054 if (&LI == I) return true;
8055 if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;
8056 }
8057 llvm_unreachable("Instruction not contained in its own parent basic block.");
8058}
8059
8061 switch (IID) {
8062 // TODO: Add more intrinsics.
8063 case Intrinsic::sadd_with_overflow:
8064 case Intrinsic::ssub_with_overflow:
8065 case Intrinsic::smul_with_overflow:
8066 case Intrinsic::uadd_with_overflow:
8067 case Intrinsic::usub_with_overflow:
8068 case Intrinsic::umul_with_overflow:
8069 // If an input is a vector containing a poison element, the
8070 // two output vectors (calculated results, overflow bits)'
8071 // corresponding lanes are poison.
8072 return true;
8073 case Intrinsic::ctpop:
8074 case Intrinsic::ctlz:
8075 case Intrinsic::cttz:
8076 case Intrinsic::abs:
8077 case Intrinsic::smax:
8078 case Intrinsic::smin:
8079 case Intrinsic::umax:
8080 case Intrinsic::umin:
8081 case Intrinsic::scmp:
8082 case Intrinsic::is_fpclass:
8083 case Intrinsic::ptrmask:
8084 case Intrinsic::ucmp:
8085 case Intrinsic::bitreverse:
8086 case Intrinsic::bswap:
8087 case Intrinsic::sadd_sat:
8088 case Intrinsic::ssub_sat:
8089 case Intrinsic::sshl_sat:
8090 case Intrinsic::uadd_sat:
8091 case Intrinsic::usub_sat:
8092 case Intrinsic::ushl_sat:
8093 case Intrinsic::smul_fix:
8094 case Intrinsic::smul_fix_sat:
8095 case Intrinsic::umul_fix:
8096 case Intrinsic::umul_fix_sat:
8097 case Intrinsic::pow:
8098 case Intrinsic::powi:
8099 case Intrinsic::sin:
8100 case Intrinsic::sinh:
8101 case Intrinsic::cos:
8102 case Intrinsic::cosh:
8103 case Intrinsic::sincos:
8104 case Intrinsic::sincospi:
8105 case Intrinsic::tan:
8106 case Intrinsic::tanh:
8107 case Intrinsic::asin:
8108 case Intrinsic::acos:
8109 case Intrinsic::atan:
8110 case Intrinsic::atan2:
8111 case Intrinsic::canonicalize:
8112 case Intrinsic::sqrt:
8113 case Intrinsic::exp:
8114 case Intrinsic::exp2:
8115 case Intrinsic::exp10:
8116 case Intrinsic::log:
8117 case Intrinsic::log2:
8118 case Intrinsic::log10:
8119 case Intrinsic::modf:
8120 case Intrinsic::floor:
8121 case Intrinsic::ceil:
8122 case Intrinsic::trunc:
8123 case Intrinsic::rint:
8124 case Intrinsic::nearbyint:
8125 case Intrinsic::round:
8126 case Intrinsic::roundeven:
8127 case Intrinsic::lrint:
8128 case Intrinsic::llrint:
8129 case Intrinsic::fshl:
8130 case Intrinsic::fshr:
8131 return true;
8132 default:
8133 return false;
8134 }
8135}
8136
8137bool llvm::propagatesPoison(const Use &PoisonOp) {
8138 const Operator *I = cast<Operator>(PoisonOp.getUser());
8139 switch (I->getOpcode()) {
8140 case Instruction::Freeze:
8141 case Instruction::PHI:
8142 case Instruction::Invoke:
8143 return false;
8144 case Instruction::Select:
8145 return PoisonOp.getOperandNo() == 0;
8146 case Instruction::Call:
8147 if (auto *II = dyn_cast<IntrinsicInst>(I))
8148 return intrinsicPropagatesPoison(II->getIntrinsicID());
8149 return false;
8150 case Instruction::ICmp:
8151 case Instruction::FCmp:
8152 case Instruction::GetElementPtr:
8153 return true;
8154 default:
8156 return true;
8157
8158 // Be conservative and return false.
8159 return false;
8160 }
8161}
8162
8163/// Enumerates all operands of \p I that are guaranteed to not be undef or
8164/// poison. If the callback \p Handle returns true, stop processing and return
8165/// true. Otherwise, return false.
8166template <typename CallableT>
8168 const CallableT &Handle) {
8169 switch (I->getOpcode()) {
8170 case Instruction::Store:
8171 if (Handle(cast<StoreInst>(I)->getPointerOperand()))
8172 return true;
8173 break;
8174
8175 case Instruction::Load:
8176 if (Handle(cast<LoadInst>(I)->getPointerOperand()))
8177 return true;
8178 break;
8179
8180 // Since dereferenceable attribute imply noundef, atomic operations
8181 // also implicitly have noundef pointers too
8182 case Instruction::AtomicCmpXchg:
8184 return true;
8185 break;
8186
8187 case Instruction::AtomicRMW:
8188 if (Handle(cast<AtomicRMWInst>(I)->getPointerOperand()))
8189 return true;
8190 break;
8191
8192 case Instruction::Call:
8193 case Instruction::Invoke: {
8194 const CallBase *CB = cast<CallBase>(I);
8195 if (CB->isIndirectCall() && Handle(CB->getCalledOperand()))
8196 return true;
8197 for (unsigned i = 0; i < CB->arg_size(); ++i)
8198 if ((CB->paramHasAttr(i, Attribute::NoUndef) ||
8199 CB->paramHasAttr(i, Attribute::Dereferenceable) ||
8200 CB->paramHasAttr(i, Attribute::DereferenceableOrNull)) &&
8201 Handle(CB->getArgOperand(i)))
8202 return true;
8203 break;
8204 }
8205 case Instruction::Ret:
8206 if (I->getFunction()->hasRetAttribute(Attribute::NoUndef) &&
8207 Handle(I->getOperand(0)))
8208 return true;
8209 break;
8210 case Instruction::Switch:
8211 if (Handle(cast<SwitchInst>(I)->getCondition()))
8212 return true;
8213 break;
8214 case Instruction::Br: {
8215 auto *BR = cast<BranchInst>(I);
8216 if (BR->isConditional() && Handle(BR->getCondition()))
8217 return true;
8218 break;
8219 }
8220 default:
8221 break;
8222 }
8223
8224 return false;
8225}
8226
8227/// Enumerates all operands of \p I that are guaranteed to not be poison.
8228template <typename CallableT>
8230 const CallableT &Handle) {
8231 if (handleGuaranteedWellDefinedOps(I, Handle))
8232 return true;
8233 switch (I->getOpcode()) {
8234 // Divisors of these operations are allowed to be partially undef.
8235 case Instruction::UDiv:
8236 case Instruction::SDiv:
8237 case Instruction::URem:
8238 case Instruction::SRem:
8239 return Handle(I->getOperand(1));
8240 default:
8241 return false;
8242 }
8243}
8244
8246 const SmallPtrSetImpl<const Value *> &KnownPoison) {
8248 I, [&](const Value *V) { return KnownPoison.count(V); });
8249}
8250
8252 bool PoisonOnly) {
8253 // We currently only look for uses of values within the same basic
8254 // block, as that makes it easier to guarantee that the uses will be
8255 // executed given that Inst is executed.
8256 //
8257 // FIXME: Expand this to consider uses beyond the same basic block. To do
8258 // this, look out for the distinction between post-dominance and strong
8259 // post-dominance.
8260 const BasicBlock *BB = nullptr;
8262 if (const auto *Inst = dyn_cast<Instruction>(V)) {
8263 BB = Inst->getParent();
8264 Begin = Inst->getIterator();
8265 Begin++;
8266 } else if (const auto *Arg = dyn_cast<Argument>(V)) {
8267 if (Arg->getParent()->isDeclaration())
8268 return false;
8269 BB = &Arg->getParent()->getEntryBlock();
8270 Begin = BB->begin();
8271 } else {
8272 return false;
8273 }
8274
8275 // Limit number of instructions we look at, to avoid scanning through large
8276 // blocks. The current limit is chosen arbitrarily.
8277 unsigned ScanLimit = 32;
8278 BasicBlock::const_iterator End = BB->end();
8279
8280 if (!PoisonOnly) {
8281 // Since undef does not propagate eagerly, be conservative & just check
8282 // whether a value is directly passed to an instruction that must take
8283 // well-defined operands.
8284
8285 for (const auto &I : make_range(Begin, End)) {
8286 if (--ScanLimit == 0)
8287 break;
8288
8289 if (handleGuaranteedWellDefinedOps(&I, [V](const Value *WellDefinedOp) {
8290 return WellDefinedOp == V;
8291 }))
8292 return true;
8293
8295 break;
8296 }
8297 return false;
8298 }
8299
8300 // Set of instructions that we have proved will yield poison if Inst
8301 // does.
8302 SmallPtrSet<const Value *, 16> YieldsPoison;
8304
8305 YieldsPoison.insert(V);
8306 Visited.insert(BB);
8307
8308 while (true) {
8309 for (const auto &I : make_range(Begin, End)) {
8310 if (--ScanLimit == 0)
8311 return false;
8312 if (mustTriggerUB(&I, YieldsPoison))
8313 return true;
8315 return false;
8316
8317 // If an operand is poison and propagates it, mark I as yielding poison.
8318 for (const Use &Op : I.operands()) {
8319 if (YieldsPoison.count(Op) && propagatesPoison(Op)) {
8320 YieldsPoison.insert(&I);
8321 break;
8322 }
8323 }
8324
8325 // Special handling for select, which returns poison if its operand 0 is
8326 // poison (handled in the loop above) *or* if both its true/false operands
8327 // are poison (handled here).
8328 if (I.getOpcode() == Instruction::Select &&
8329 YieldsPoison.count(I.getOperand(1)) &&
8330 YieldsPoison.count(I.getOperand(2))) {
8331 YieldsPoison.insert(&I);
8332 }
8333 }
8334
8335 BB = BB->getSingleSuccessor();
8336 if (!BB || !Visited.insert(BB).second)
8337 break;
8338
8339 Begin = BB->getFirstNonPHIIt();
8340 End = BB->end();
8341 }
8342 return false;
8343}
8344
8346 return ::programUndefinedIfUndefOrPoison(Inst, false);
8347}
8348
8350 return ::programUndefinedIfUndefOrPoison(Inst, true);
8351}
8352
8353static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) {
8354 if (FMF.noNaNs())
8355 return true;
8356
8357 if (auto *C = dyn_cast<ConstantFP>(V))
8358 return !C->isNaN();
8359
8360 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8361 if (!C->getElementType()->isFloatingPointTy())
8362 return false;
8363 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8364 if (C->getElementAsAPFloat(I).isNaN())
8365 return false;
8366 }
8367 return true;
8368 }
8369
8371 return true;
8372
8373 return false;
8374}
8375
8376static bool isKnownNonZero(const Value *V) {
8377 if (auto *C = dyn_cast<ConstantFP>(V))
8378 return !C->isZero();
8379
8380 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8381 if (!C->getElementType()->isFloatingPointTy())
8382 return false;
8383 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8384 if (C->getElementAsAPFloat(I).isZero())
8385 return false;
8386 }
8387 return true;
8388 }
8389
8390 return false;
8391}
8392
8393/// Match clamp pattern for float types without care about NaNs or signed zeros.
8394/// Given non-min/max outer cmp/select from the clamp pattern this
8395/// function recognizes if it can be substitued by a "canonical" min/max
8396/// pattern.
8398 Value *CmpLHS, Value *CmpRHS,
8399 Value *TrueVal, Value *FalseVal,
8400 Value *&LHS, Value *&RHS) {
8401 // Try to match
8402 // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2))
8403 // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2))
8404 // and return description of the outer Max/Min.
8405
8406 // First, check if select has inverse order:
8407 if (CmpRHS == FalseVal) {
8408 std::swap(TrueVal, FalseVal);
8409 Pred = CmpInst::getInversePredicate(Pred);
8410 }
8411
8412 // Assume success now. If there's no match, callers should not use these anyway.
8413 LHS = TrueVal;
8414 RHS = FalseVal;
8415
8416 const APFloat *FC1;
8417 if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite())
8418 return {SPF_UNKNOWN, SPNB_NA, false};
8419
8420 const APFloat *FC2;
8421 switch (Pred) {
8422 case CmpInst::FCMP_OLT:
8423 case CmpInst::FCMP_OLE:
8424 case CmpInst::FCMP_ULT:
8425 case CmpInst::FCMP_ULE:
8426 if (match(FalseVal, m_OrdOrUnordFMin(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8427 *FC1 < *FC2)
8428 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
8429 break;
8430 case CmpInst::FCMP_OGT:
8431 case CmpInst::FCMP_OGE:
8432 case CmpInst::FCMP_UGT:
8433 case CmpInst::FCMP_UGE:
8434 if (match(FalseVal, m_OrdOrUnordFMax(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8435 *FC1 > *FC2)
8436 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
8437 break;
8438 default:
8439 break;
8440 }
8441
8442 return {SPF_UNKNOWN, SPNB_NA, false};
8443}
8444
8445/// Recognize variations of:
8446/// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
8448 Value *CmpLHS, Value *CmpRHS,
8449 Value *TrueVal, Value *FalseVal) {
8450 // Swap the select operands and predicate to match the patterns below.
8451 if (CmpRHS != TrueVal) {
8452 Pred = ICmpInst::getSwappedPredicate(Pred);
8453 std::swap(TrueVal, FalseVal);
8454 }
8455 const APInt *C1;
8456 if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) {
8457 const APInt *C2;
8458 // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
8459 if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8460 C1->slt(*C2) && Pred == CmpInst::ICMP_SLT)
8461 return {SPF_SMAX, SPNB_NA, false};
8462
8463 // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
8464 if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8465 C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT)
8466 return {SPF_SMIN, SPNB_NA, false};
8467
8468 // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
8469 if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8470 C1->ult(*C2) && Pred == CmpInst::ICMP_ULT)
8471 return {SPF_UMAX, SPNB_NA, false};
8472
8473 // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
8474 if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8475 C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT)
8476 return {SPF_UMIN, SPNB_NA, false};
8477 }
8478 return {SPF_UNKNOWN, SPNB_NA, false};
8479}
8480
8481/// Recognize variations of:
8482/// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c))
8484 Value *CmpLHS, Value *CmpRHS,
8485 Value *TVal, Value *FVal,
8486 unsigned Depth) {
8487 // TODO: Allow FP min/max with nnan/nsz.
8488 assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison");
8489
8490 Value *A = nullptr, *B = nullptr;
8491 SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1);
8492 if (!SelectPatternResult::isMinOrMax(L.Flavor))
8493 return {SPF_UNKNOWN, SPNB_NA, false};
8494
8495 Value *C = nullptr, *D = nullptr;
8496 SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1);
8497 if (L.Flavor != R.Flavor)
8498 return {SPF_UNKNOWN, SPNB_NA, false};
8499
8500 // We have something like: x Pred y ? min(a, b) : min(c, d).
8501 // Try to match the compare to the min/max operations of the select operands.
8502 // First, make sure we have the right compare predicate.
8503 switch (L.Flavor) {
8504 case SPF_SMIN:
8505 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) {
8506 Pred = ICmpInst::getSwappedPredicate(Pred);
8507 std::swap(CmpLHS, CmpRHS);
8508 }
8509 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
8510 break;
8511 return {SPF_UNKNOWN, SPNB_NA, false};
8512 case SPF_SMAX:
8513 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) {
8514 Pred = ICmpInst::getSwappedPredicate(Pred);
8515 std::swap(CmpLHS, CmpRHS);
8516 }
8517 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE)
8518 break;
8519 return {SPF_UNKNOWN, SPNB_NA, false};
8520 case SPF_UMIN:
8521 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
8522 Pred = ICmpInst::getSwappedPredicate(Pred);
8523 std::swap(CmpLHS, CmpRHS);
8524 }
8525 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE)
8526 break;
8527 return {SPF_UNKNOWN, SPNB_NA, false};
8528 case SPF_UMAX:
8529 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
8530 Pred = ICmpInst::getSwappedPredicate(Pred);
8531 std::swap(CmpLHS, CmpRHS);
8532 }
8533 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
8534 break;
8535 return {SPF_UNKNOWN, SPNB_NA, false};
8536 default:
8537 return {SPF_UNKNOWN, SPNB_NA, false};
8538 }
8539
8540 // If there is a common operand in the already matched min/max and the other
8541 // min/max operands match the compare operands (either directly or inverted),
8542 // then this is min/max of the same flavor.
8543
8544 // a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8545 // ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8546 if (D == B) {
8547 if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8548 match(A, m_Not(m_Specific(CmpRHS)))))
8549 return {L.Flavor, SPNB_NA, false};
8550 }
8551 // a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8552 // ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8553 if (C == B) {
8554 if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8555 match(A, m_Not(m_Specific(CmpRHS)))))
8556 return {L.Flavor, SPNB_NA, false};
8557 }
8558 // b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8559 // ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8560 if (D == A) {
8561 if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8562 match(B, m_Not(m_Specific(CmpRHS)))))
8563 return {L.Flavor, SPNB_NA, false};
8564 }
8565 // b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8566 // ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8567 if (C == A) {
8568 if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8569 match(B, m_Not(m_Specific(CmpRHS)))))
8570 return {L.Flavor, SPNB_NA, false};
8571 }
8572
8573 return {SPF_UNKNOWN, SPNB_NA, false};
8574}
8575
8576/// If the input value is the result of a 'not' op, constant integer, or vector
8577/// splat of a constant integer, return the bitwise-not source value.
8578/// TODO: This could be extended to handle non-splat vector integer constants.
8580 Value *NotV;
8581 if (match(V, m_Not(m_Value(NotV))))
8582 return NotV;
8583
8584 const APInt *C;
8585 if (match(V, m_APInt(C)))
8586 return ConstantInt::get(V->getType(), ~(*C));
8587
8588 return nullptr;
8589}
8590
8591/// Match non-obvious integer minimum and maximum sequences.
8593 Value *CmpLHS, Value *CmpRHS,
8594 Value *TrueVal, Value *FalseVal,
8595 Value *&LHS, Value *&RHS,
8596 unsigned Depth) {
8597 // Assume success. If there's no match, callers should not use these anyway.
8598 LHS = TrueVal;
8599 RHS = FalseVal;
8600
8601 SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);
8603 return SPR;
8604
8605 SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth);
8607 return SPR;
8608
8609 // Look through 'not' ops to find disguised min/max.
8610 // (X > Y) ? ~X : ~Y ==> (~X < ~Y) ? ~X : ~Y ==> MIN(~X, ~Y)
8611 // (X < Y) ? ~X : ~Y ==> (~X > ~Y) ? ~X : ~Y ==> MAX(~X, ~Y)
8612 if (CmpLHS == getNotValue(TrueVal) && CmpRHS == getNotValue(FalseVal)) {
8613 switch (Pred) {
8614 case CmpInst::ICMP_SGT: return {SPF_SMIN, SPNB_NA, false};
8615 case CmpInst::ICMP_SLT: return {SPF_SMAX, SPNB_NA, false};
8616 case CmpInst::ICMP_UGT: return {SPF_UMIN, SPNB_NA, false};
8617 case CmpInst::ICMP_ULT: return {SPF_UMAX, SPNB_NA, false};
8618 default: break;
8619 }
8620 }
8621
8622 // (X > Y) ? ~Y : ~X ==> (~X < ~Y) ? ~Y : ~X ==> MAX(~Y, ~X)
8623 // (X < Y) ? ~Y : ~X ==> (~X > ~Y) ? ~Y : ~X ==> MIN(~Y, ~X)
8624 if (CmpLHS == getNotValue(FalseVal) && CmpRHS == getNotValue(TrueVal)) {
8625 switch (Pred) {
8626 case CmpInst::ICMP_SGT: return {SPF_SMAX, SPNB_NA, false};
8627 case CmpInst::ICMP_SLT: return {SPF_SMIN, SPNB_NA, false};
8628 case CmpInst::ICMP_UGT: return {SPF_UMAX, SPNB_NA, false};
8629 case CmpInst::ICMP_ULT: return {SPF_UMIN, SPNB_NA, false};
8630 default: break;
8631 }
8632 }
8633
8634 if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT)
8635 return {SPF_UNKNOWN, SPNB_NA, false};
8636
8637 const APInt *C1;
8638 if (!match(CmpRHS, m_APInt(C1)))
8639 return {SPF_UNKNOWN, SPNB_NA, false};
8640
8641 // An unsigned min/max can be written with a signed compare.
8642 const APInt *C2;
8643 if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) ||
8644 (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) {
8645 // Is the sign bit set?
8646 // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX
8647 // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN
8648 if (Pred == CmpInst::ICMP_SLT && C1->isZero() && C2->isMaxSignedValue())
8649 return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8650
8651 // Is the sign bit clear?
8652 // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX
8653 // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN
8654 if (Pred == CmpInst::ICMP_SGT && C1->isAllOnes() && C2->isMinSignedValue())
8655 return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8656 }
8657
8658 return {SPF_UNKNOWN, SPNB_NA, false};
8659}
8660
8661bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW,
8662 bool AllowPoison) {
8663 assert(X && Y && "Invalid operand");
8664
8665 auto IsNegationOf = [&](const Value *X, const Value *Y) {
8666 if (!match(X, m_Neg(m_Specific(Y))))
8667 return false;
8668
8669 auto *BO = cast<BinaryOperator>(X);
8670 if (NeedNSW && !BO->hasNoSignedWrap())
8671 return false;
8672
8673 auto *Zero = cast<Constant>(BO->getOperand(0));
8674 if (!AllowPoison && !Zero->isNullValue())
8675 return false;
8676
8677 return true;
8678 };
8679
8680 // X = -Y or Y = -X
8681 if (IsNegationOf(X, Y) || IsNegationOf(Y, X))
8682 return true;
8683
8684 // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A)
8685 Value *A, *B;
8686 return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) &&
8687 match(Y, m_Sub(m_Specific(B), m_Specific(A))))) ||
8688 (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) &&
8690}
8691
8692bool llvm::isKnownInversion(const Value *X, const Value *Y) {
8693 // Handle X = icmp pred A, B, Y = icmp pred A, C.
8694 Value *A, *B, *C;
8695 CmpPredicate Pred1, Pred2;
8696 if (!match(X, m_ICmp(Pred1, m_Value(A), m_Value(B))) ||
8697 !match(Y, m_c_ICmp(Pred2, m_Specific(A), m_Value(C))))
8698 return false;
8699
8700 // They must both have samesign flag or not.
8701 if (Pred1.hasSameSign() != Pred2.hasSameSign())
8702 return false;
8703
8704 if (B == C)
8705 return Pred1 == ICmpInst::getInversePredicate(Pred2);
8706
8707 // Try to infer the relationship from constant ranges.
8708 const APInt *RHSC1, *RHSC2;
8709 if (!match(B, m_APInt(RHSC1)) || !match(C, m_APInt(RHSC2)))
8710 return false;
8711
8712 // Sign bits of two RHSCs should match.
8713 if (Pred1.hasSameSign() && RHSC1->isNonNegative() != RHSC2->isNonNegative())
8714 return false;
8715
8716 const auto CR1 = ConstantRange::makeExactICmpRegion(Pred1, *RHSC1);
8717 const auto CR2 = ConstantRange::makeExactICmpRegion(Pred2, *RHSC2);
8718
8719 return CR1.inverse() == CR2;
8720}
8721
8723 SelectPatternNaNBehavior NaNBehavior,
8724 bool Ordered) {
8725 switch (Pred) {
8726 default:
8727 return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.
8728 case ICmpInst::ICMP_UGT:
8729 case ICmpInst::ICMP_UGE:
8730 return {SPF_UMAX, SPNB_NA, false};
8731 case ICmpInst::ICMP_SGT:
8732 case ICmpInst::ICMP_SGE:
8733 return {SPF_SMAX, SPNB_NA, false};
8734 case ICmpInst::ICMP_ULT:
8735 case ICmpInst::ICMP_ULE:
8736 return {SPF_UMIN, SPNB_NA, false};
8737 case ICmpInst::ICMP_SLT:
8738 case ICmpInst::ICMP_SLE:
8739 return {SPF_SMIN, SPNB_NA, false};
8740 case FCmpInst::FCMP_UGT:
8741 case FCmpInst::FCMP_UGE:
8742 case FCmpInst::FCMP_OGT:
8743 case FCmpInst::FCMP_OGE:
8744 return {SPF_FMAXNUM, NaNBehavior, Ordered};
8745 case FCmpInst::FCMP_ULT:
8746 case FCmpInst::FCMP_ULE:
8747 case FCmpInst::FCMP_OLT:
8748 case FCmpInst::FCMP_OLE:
8749 return {SPF_FMINNUM, NaNBehavior, Ordered};
8750 }
8751}
8752
8753std::optional<std::pair<CmpPredicate, Constant *>>
8756 "Only for relational integer predicates.");
8757 if (isa<UndefValue>(C))
8758 return std::nullopt;
8759
8760 Type *Type = C->getType();
8761 bool IsSigned = ICmpInst::isSigned(Pred);
8762
8764 bool WillIncrement =
8765 UnsignedPred == ICmpInst::ICMP_ULE || UnsignedPred == ICmpInst::ICMP_UGT;
8766
8767 // Check if the constant operand can be safely incremented/decremented
8768 // without overflowing/underflowing.
8769 auto ConstantIsOk = [WillIncrement, IsSigned](ConstantInt *C) {
8770 return WillIncrement ? !C->isMaxValue(IsSigned) : !C->isMinValue(IsSigned);
8771 };
8772
8773 Constant *SafeReplacementConstant = nullptr;
8774 if (auto *CI = dyn_cast<ConstantInt>(C)) {
8775 // Bail out if the constant can't be safely incremented/decremented.
8776 if (!ConstantIsOk(CI))
8777 return std::nullopt;
8778 } else if (auto *FVTy = dyn_cast<FixedVectorType>(Type)) {
8779 unsigned NumElts = FVTy->getNumElements();
8780 for (unsigned i = 0; i != NumElts; ++i) {
8781 Constant *Elt = C->getAggregateElement(i);
8782 if (!Elt)
8783 return std::nullopt;
8784
8785 if (isa<UndefValue>(Elt))
8786 continue;
8787
8788 // Bail out if we can't determine if this constant is min/max or if we
8789 // know that this constant is min/max.
8790 auto *CI = dyn_cast<ConstantInt>(Elt);
8791 if (!CI || !ConstantIsOk(CI))
8792 return std::nullopt;
8793
8794 if (!SafeReplacementConstant)
8795 SafeReplacementConstant = CI;
8796 }
8797 } else if (isa<VectorType>(C->getType())) {
8798 // Handle scalable splat
8799 Value *SplatC = C->getSplatValue();
8800 auto *CI = dyn_cast_or_null<ConstantInt>(SplatC);
8801 // Bail out if the constant can't be safely incremented/decremented.
8802 if (!CI || !ConstantIsOk(CI))
8803 return std::nullopt;
8804 } else {
8805 // ConstantExpr?
8806 return std::nullopt;
8807 }
8808
8809 // It may not be safe to change a compare predicate in the presence of
8810 // undefined elements, so replace those elements with the first safe constant
8811 // that we found.
8812 // TODO: in case of poison, it is safe; let's replace undefs only.
8813 if (C->containsUndefOrPoisonElement()) {
8814 assert(SafeReplacementConstant && "Replacement constant not set");
8815 C = Constant::replaceUndefsWith(C, SafeReplacementConstant);
8816 }
8817
8819
8820 // Increment or decrement the constant.
8821 Constant *OneOrNegOne = ConstantInt::get(Type, WillIncrement ? 1 : -1, true);
8822 Constant *NewC = ConstantExpr::getAdd(C, OneOrNegOne);
8823
8824 return std::make_pair(NewPred, NewC);
8825}
8826
8828 FastMathFlags FMF,
8829 Value *CmpLHS, Value *CmpRHS,
8830 Value *TrueVal, Value *FalseVal,
8831 Value *&LHS, Value *&RHS,
8832 unsigned Depth) {
8833 bool HasMismatchedZeros = false;
8834 if (CmpInst::isFPPredicate(Pred)) {
8835 // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one
8836 // 0.0 operand, set the compare's 0.0 operands to that same value for the
8837 // purpose of identifying min/max. Disregard vector constants with undefined
8838 // elements because those can not be back-propagated for analysis.
8839 Value *OutputZeroVal = nullptr;
8840 if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) &&
8841 !cast<Constant>(TrueVal)->containsUndefOrPoisonElement())
8842 OutputZeroVal = TrueVal;
8843 else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) &&
8844 !cast<Constant>(FalseVal)->containsUndefOrPoisonElement())
8845 OutputZeroVal = FalseVal;
8846
8847 if (OutputZeroVal) {
8848 if (match(CmpLHS, m_AnyZeroFP()) && CmpLHS != OutputZeroVal) {
8849 HasMismatchedZeros = true;
8850 CmpLHS = OutputZeroVal;
8851 }
8852 if (match(CmpRHS, m_AnyZeroFP()) && CmpRHS != OutputZeroVal) {
8853 HasMismatchedZeros = true;
8854 CmpRHS = OutputZeroVal;
8855 }
8856 }
8857 }
8858
8859 LHS = CmpLHS;
8860 RHS = CmpRHS;
8861
8862 // Signed zero may return inconsistent results between implementations.
8863 // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0
8864 // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)
8865 // Therefore, we behave conservatively and only proceed if at least one of the
8866 // operands is known to not be zero or if we don't care about signed zero.
8867 switch (Pred) {
8868 default: break;
8871 if (!HasMismatchedZeros)
8872 break;
8873 [[fallthrough]];
8876 if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8877 !isKnownNonZero(CmpRHS))
8878 return {SPF_UNKNOWN, SPNB_NA, false};
8879 }
8880
8881 SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
8882 bool Ordered = false;
8883
8884 // When given one NaN and one non-NaN input:
8885 // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
8886 // - A simple C99 (a < b ? a : b) construction will return 'b' (as the
8887 // ordered comparison fails), which could be NaN or non-NaN.
8888 // so here we discover exactly what NaN behavior is required/accepted.
8889 if (CmpInst::isFPPredicate(Pred)) {
8890 bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
8891 bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
8892
8893 if (LHSSafe && RHSSafe) {
8894 // Both operands are known non-NaN.
8895 NaNBehavior = SPNB_RETURNS_ANY;
8896 Ordered = CmpInst::isOrdered(Pred);
8897 } else if (CmpInst::isOrdered(Pred)) {
8898 // An ordered comparison will return false when given a NaN, so it
8899 // returns the RHS.
8900 Ordered = true;
8901 if (LHSSafe)
8902 // LHS is non-NaN, so if RHS is NaN then NaN will be returned.
8903 NaNBehavior = SPNB_RETURNS_NAN;
8904 else if (RHSSafe)
8905 NaNBehavior = SPNB_RETURNS_OTHER;
8906 else
8907 // Completely unsafe.
8908 return {SPF_UNKNOWN, SPNB_NA, false};
8909 } else {
8910 Ordered = false;
8911 // An unordered comparison will return true when given a NaN, so it
8912 // returns the LHS.
8913 if (LHSSafe)
8914 // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
8915 NaNBehavior = SPNB_RETURNS_OTHER;
8916 else if (RHSSafe)
8917 NaNBehavior = SPNB_RETURNS_NAN;
8918 else
8919 // Completely unsafe.
8920 return {SPF_UNKNOWN, SPNB_NA, false};
8921 }
8922 }
8923
8924 if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
8925 std::swap(CmpLHS, CmpRHS);
8926 Pred = CmpInst::getSwappedPredicate(Pred);
8927 if (NaNBehavior == SPNB_RETURNS_NAN)
8928 NaNBehavior = SPNB_RETURNS_OTHER;
8929 else if (NaNBehavior == SPNB_RETURNS_OTHER)
8930 NaNBehavior = SPNB_RETURNS_NAN;
8931 Ordered = !Ordered;
8932 }
8933
8934 // ([if]cmp X, Y) ? X : Y
8935 if (TrueVal == CmpLHS && FalseVal == CmpRHS)
8936 return getSelectPattern(Pred, NaNBehavior, Ordered);
8937
8938 if (isKnownNegation(TrueVal, FalseVal)) {
8939 // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can
8940 // match against either LHS or sext(LHS).
8941 auto MaybeSExtCmpLHS =
8942 m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS)));
8943 auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes());
8944 auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One());
8945 if (match(TrueVal, MaybeSExtCmpLHS)) {
8946 // Set the return values. If the compare uses the negated value (-X >s 0),
8947 // swap the return values because the negated value is always 'RHS'.
8948 LHS = TrueVal;
8949 RHS = FalseVal;
8950 if (match(CmpLHS, m_Neg(m_Specific(FalseVal))))
8951 std::swap(LHS, RHS);
8952
8953 // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X)
8954 // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X)
8955 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8956 return {SPF_ABS, SPNB_NA, false};
8957
8958 // (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X)
8959 if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne))
8960 return {SPF_ABS, SPNB_NA, false};
8961
8962 // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X)
8963 // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X)
8964 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8965 return {SPF_NABS, SPNB_NA, false};
8966 }
8967 else if (match(FalseVal, MaybeSExtCmpLHS)) {
8968 // Set the return values. If the compare uses the negated value (-X >s 0),
8969 // swap the return values because the negated value is always 'RHS'.
8970 LHS = FalseVal;
8971 RHS = TrueVal;
8972 if (match(CmpLHS, m_Neg(m_Specific(TrueVal))))
8973 std::swap(LHS, RHS);
8974
8975 // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X)
8976 // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X)
8977 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8978 return {SPF_NABS, SPNB_NA, false};
8979
8980 // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X)
8981 // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X)
8982 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8983 return {SPF_ABS, SPNB_NA, false};
8984 }
8985 }
8986
8987 if (CmpInst::isIntPredicate(Pred))
8988 return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth);
8989
8990 // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar
8991 // may return either -0.0 or 0.0, so fcmp/select pair has stricter
8992 // semantics than minNum. Be conservative in such case.
8993 if (NaNBehavior != SPNB_RETURNS_ANY ||
8994 (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8995 !isKnownNonZero(CmpRHS)))
8996 return {SPF_UNKNOWN, SPNB_NA, false};
8997
8998 return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
8999}
9000
9002 Instruction::CastOps *CastOp) {
9003 const DataLayout &DL = CmpI->getDataLayout();
9004
9005 Constant *CastedTo = nullptr;
9006 switch (*CastOp) {
9007 case Instruction::ZExt:
9008 if (CmpI->isUnsigned())
9009 CastedTo = ConstantExpr::getTrunc(C, SrcTy);
9010 break;
9011 case Instruction::SExt:
9012 if (CmpI->isSigned())
9013 CastedTo = ConstantExpr::getTrunc(C, SrcTy, true);
9014 break;
9015 case Instruction::Trunc:
9016 Constant *CmpConst;
9017 if (match(CmpI->getOperand(1), m_Constant(CmpConst)) &&
9018 CmpConst->getType() == SrcTy) {
9019 // Here we have the following case:
9020 //
9021 // %cond = cmp iN %x, CmpConst
9022 // %tr = trunc iN %x to iK
9023 // %narrowsel = select i1 %cond, iK %t, iK C
9024 //
9025 // We can always move trunc after select operation:
9026 //
9027 // %cond = cmp iN %x, CmpConst
9028 // %widesel = select i1 %cond, iN %x, iN CmpConst
9029 // %tr = trunc iN %widesel to iK
9030 //
9031 // Note that C could be extended in any way because we don't care about
9032 // upper bits after truncation. It can't be abs pattern, because it would
9033 // look like:
9034 //
9035 // select i1 %cond, x, -x.
9036 //
9037 // So only min/max pattern could be matched. Such match requires widened C
9038 // == CmpConst. That is why set widened C = CmpConst, condition trunc
9039 // CmpConst == C is checked below.
9040 CastedTo = CmpConst;
9041 } else {
9042 unsigned ExtOp = CmpI->isSigned() ? Instruction::SExt : Instruction::ZExt;
9043 CastedTo = ConstantFoldCastOperand(ExtOp, C, SrcTy, DL);
9044 }
9045 break;
9046 case Instruction::FPTrunc:
9047 CastedTo = ConstantFoldCastOperand(Instruction::FPExt, C, SrcTy, DL);
9048 break;
9049 case Instruction::FPExt:
9050 CastedTo = ConstantFoldCastOperand(Instruction::FPTrunc, C, SrcTy, DL);
9051 break;
9052 case Instruction::FPToUI:
9053 CastedTo = ConstantFoldCastOperand(Instruction::UIToFP, C, SrcTy, DL);
9054 break;
9055 case Instruction::FPToSI:
9056 CastedTo = ConstantFoldCastOperand(Instruction::SIToFP, C, SrcTy, DL);
9057 break;
9058 case Instruction::UIToFP:
9059 CastedTo = ConstantFoldCastOperand(Instruction::FPToUI, C, SrcTy, DL);
9060 break;
9061 case Instruction::SIToFP:
9062 CastedTo = ConstantFoldCastOperand(Instruction::FPToSI, C, SrcTy, DL);
9063 break;
9064 default:
9065 break;
9066 }
9067
9068 if (!CastedTo)
9069 return nullptr;
9070
9071 // Make sure the cast doesn't lose any information.
9072 Constant *CastedBack =
9073 ConstantFoldCastOperand(*CastOp, CastedTo, C->getType(), DL);
9074 if (CastedBack && CastedBack != C)
9075 return nullptr;
9076
9077 return CastedTo;
9078}
9079
9080/// Helps to match a select pattern in case of a type mismatch.
9081///
9082/// The function processes the case when type of true and false values of a
9083/// select instruction differs from type of the cmp instruction operands because
9084/// of a cast instruction. The function checks if it is legal to move the cast
9085/// operation after "select". If yes, it returns the new second value of
9086/// "select" (with the assumption that cast is moved):
9087/// 1. As operand of cast instruction when both values of "select" are same cast
9088/// instructions.
9089/// 2. As restored constant (by applying reverse cast operation) when the first
9090/// value of the "select" is a cast operation and the second value is a
9091/// constant. It is implemented in lookThroughCastConst().
9092/// 3. As one operand is cast instruction and the other is not. The operands in
9093/// sel(cmp) are in different type integer.
9094/// NOTE: We return only the new second value because the first value could be
9095/// accessed as operand of cast instruction.
9096static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2,
9097 Instruction::CastOps *CastOp) {
9098 auto *Cast1 = dyn_cast<CastInst>(V1);
9099 if (!Cast1)
9100 return nullptr;
9101
9102 *CastOp = Cast1->getOpcode();
9103 Type *SrcTy = Cast1->getSrcTy();
9104 if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
9105 // If V1 and V2 are both the same cast from the same type, look through V1.
9106 if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
9107 return Cast2->getOperand(0);
9108 return nullptr;
9109 }
9110
9111 auto *C = dyn_cast<Constant>(V2);
9112 if (C)
9113 return lookThroughCastConst(CmpI, SrcTy, C, CastOp);
9114
9115 Value *CastedTo = nullptr;
9116 if (*CastOp == Instruction::Trunc) {
9117 if (match(CmpI->getOperand(1), m_ZExtOrSExt(m_Specific(V2)))) {
9118 // Here we have the following case:
9119 // %y_ext = sext iK %y to iN
9120 // %cond = cmp iN %x, %y_ext
9121 // %tr = trunc iN %x to iK
9122 // %narrowsel = select i1 %cond, iK %tr, iK %y
9123 //
9124 // We can always move trunc after select operation:
9125 // %y_ext = sext iK %y to iN
9126 // %cond = cmp iN %x, %y_ext
9127 // %widesel = select i1 %cond, iN %x, iN %y_ext
9128 // %tr = trunc iN %widesel to iK
9129 assert(V2->getType() == Cast1->getType() &&
9130 "V2 and Cast1 should be the same type.");
9131 CastedTo = CmpI->getOperand(1);
9132 }
9133 }
9134
9135 return CastedTo;
9136}
9138 Instruction::CastOps *CastOp,
9139 unsigned Depth) {
9141 return {SPF_UNKNOWN, SPNB_NA, false};
9142
9144 if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
9145
9146 CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
9147 if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};
9148
9149 Value *TrueVal = SI->getTrueValue();
9150 Value *FalseVal = SI->getFalseValue();
9151
9153 CmpI, TrueVal, FalseVal, LHS, RHS,
9154 isa<FPMathOperator>(SI) ? SI->getFastMathFlags() : FastMathFlags(),
9155 CastOp, Depth);
9156}
9157
9159 CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS,
9160 FastMathFlags FMF, Instruction::CastOps *CastOp, unsigned Depth) {
9161 CmpInst::Predicate Pred = CmpI->getPredicate();
9162 Value *CmpLHS = CmpI->getOperand(0);
9163 Value *CmpRHS = CmpI->getOperand(1);
9164 if (isa<FPMathOperator>(CmpI) && CmpI->hasNoNaNs())
9165 FMF.setNoNaNs();
9166
9167 // Bail out early.
9168 if (CmpI->isEquality())
9169 return {SPF_UNKNOWN, SPNB_NA, false};
9170
9171 // Deal with type mismatches.
9172 if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
9173 if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) {
9174 // If this is a potential fmin/fmax with a cast to integer, then ignore
9175 // -0.0 because there is no corresponding integer value.
9176 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9177 FMF.setNoSignedZeros();
9178 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9179 cast<CastInst>(TrueVal)->getOperand(0), C,
9180 LHS, RHS, Depth);
9181 }
9182 if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) {
9183 // If this is a potential fmin/fmax with a cast to integer, then ignore
9184 // -0.0 because there is no corresponding integer value.
9185 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9186 FMF.setNoSignedZeros();
9187 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9188 C, cast<CastInst>(FalseVal)->getOperand(0),
9189 LHS, RHS, Depth);
9190 }
9191 }
9192 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
9193 LHS, RHS, Depth);
9194}
9195
9197 if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT;
9198 if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT;
9199 if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT;
9200 if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT;
9201 if (SPF == SPF_FMINNUM)
9202 return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT;
9203 if (SPF == SPF_FMAXNUM)
9204 return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT;
9205 llvm_unreachable("unhandled!");
9206}
9207
9209 switch (SPF) {
9211 return Intrinsic::umin;
9213 return Intrinsic::umax;
9215 return Intrinsic::smin;
9217 return Intrinsic::smax;
9218 default:
9219 llvm_unreachable("Unexpected SPF");
9220 }
9221}
9222
9224 if (SPF == SPF_SMIN) return SPF_SMAX;
9225 if (SPF == SPF_UMIN) return SPF_UMAX;
9226 if (SPF == SPF_SMAX) return SPF_SMIN;
9227 if (SPF == SPF_UMAX) return SPF_UMIN;
9228 llvm_unreachable("unhandled!");
9229}
9230
9232 switch (MinMaxID) {
9233 case Intrinsic::smax: return Intrinsic::smin;
9234 case Intrinsic::smin: return Intrinsic::smax;
9235 case Intrinsic::umax: return Intrinsic::umin;
9236 case Intrinsic::umin: return Intrinsic::umax;
9237 // Please note that next four intrinsics may produce the same result for
9238 // original and inverted case even if X != Y due to NaN is handled specially.
9239 case Intrinsic::maximum: return Intrinsic::minimum;
9240 case Intrinsic::minimum: return Intrinsic::maximum;
9241 case Intrinsic::maxnum: return Intrinsic::minnum;
9242 case Intrinsic::minnum: return Intrinsic::maxnum;
9243 case Intrinsic::maximumnum:
9244 return Intrinsic::minimumnum;
9245 case Intrinsic::minimumnum:
9246 return Intrinsic::maximumnum;
9247 default: llvm_unreachable("Unexpected intrinsic");
9248 }
9249}
9250
9252 switch (SPF) {
9255 case SPF_UMAX: return APInt::getMaxValue(BitWidth);
9256 case SPF_UMIN: return APInt::getMinValue(BitWidth);
9257 default: llvm_unreachable("Unexpected flavor");
9258 }
9259}
9260
9261std::pair<Intrinsic::ID, bool>
9263 // Check if VL contains select instructions that can be folded into a min/max
9264 // vector intrinsic and return the intrinsic if it is possible.
9265 // TODO: Support floating point min/max.
9266 bool AllCmpSingleUse = true;
9267 SelectPatternResult SelectPattern;
9268 SelectPattern.Flavor = SPF_UNKNOWN;
9269 if (all_of(VL, [&SelectPattern, &AllCmpSingleUse](Value *I) {
9270 Value *LHS, *RHS;
9271 auto CurrentPattern = matchSelectPattern(I, LHS, RHS);
9272 if (!SelectPatternResult::isMinOrMax(CurrentPattern.Flavor))
9273 return false;
9274 if (SelectPattern.Flavor != SPF_UNKNOWN &&
9275 SelectPattern.Flavor != CurrentPattern.Flavor)
9276 return false;
9277 SelectPattern = CurrentPattern;
9278 AllCmpSingleUse &=
9280 return true;
9281 })) {
9282 switch (SelectPattern.Flavor) {
9283 case SPF_SMIN:
9284 return {Intrinsic::smin, AllCmpSingleUse};
9285 case SPF_UMIN:
9286 return {Intrinsic::umin, AllCmpSingleUse};
9287 case SPF_SMAX:
9288 return {Intrinsic::smax, AllCmpSingleUse};
9289 case SPF_UMAX:
9290 return {Intrinsic::umax, AllCmpSingleUse};
9291 case SPF_FMAXNUM:
9292 return {Intrinsic::maxnum, AllCmpSingleUse};
9293 case SPF_FMINNUM:
9294 return {Intrinsic::minnum, AllCmpSingleUse};
9295 default:
9296 llvm_unreachable("unexpected select pattern flavor");
9297 }
9298 }
9299 return {Intrinsic::not_intrinsic, false};
9300}
9301
9302template <typename InstTy>
9303static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst,
9304 Value *&Init, Value *&OtherOp) {
9305 // Handle the case of a simple two-predecessor recurrence PHI.
9306 // There's a lot more that could theoretically be done here, but
9307 // this is sufficient to catch some interesting cases.
9308 // TODO: Expand list -- gep, uadd.sat etc.
9309 if (PN->getNumIncomingValues() != 2)
9310 return false;
9311
9312 for (unsigned I = 0; I != 2; ++I) {
9313 if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
9314 Operation && Operation->getNumOperands() >= 2) {
9315 Value *LHS = Operation->getOperand(0);
9316 Value *RHS = Operation->getOperand(1);
9317 if (LHS != PN && RHS != PN)
9318 continue;
9319
9320 Inst = Operation;
9321 Init = PN->getIncomingValue(!I);
9322 OtherOp = (LHS == PN) ? RHS : LHS;
9323 return true;
9324 }
9325 }
9326 return false;
9327}
9328
9329template <typename InstTy>
9330static bool matchThreeInputRecurrence(const PHINode *PN, InstTy *&Inst,
9331 Value *&Init, Value *&OtherOp0,
9332 Value *&OtherOp1) {
9333 if (PN->getNumIncomingValues() != 2)
9334 return false;
9335
9336 for (unsigned I = 0; I != 2; ++I) {
9337 if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
9338 Operation && Operation->getNumOperands() >= 3) {
9339 Value *Op0 = Operation->getOperand(0);
9340 Value *Op1 = Operation->getOperand(1);
9341 Value *Op2 = Operation->getOperand(2);
9342
9343 if (Op0 != PN && Op1 != PN && Op2 != PN)
9344 continue;
9345
9346 Inst = Operation;
9347 Init = PN->getIncomingValue(!I);
9348 if (Op0 == PN) {
9349 OtherOp0 = Op1;
9350 OtherOp1 = Op2;
9351 } else if (Op1 == PN) {
9352 OtherOp0 = Op0;
9353 OtherOp1 = Op2;
9354 } else {
9355 OtherOp0 = Op0;
9356 OtherOp1 = Op1;
9357 }
9358 return true;
9359 }
9360 }
9361 return false;
9362}
9364 Value *&Start, Value *&Step) {
9365 // We try to match a recurrence of the form:
9366 // %iv = [Start, %entry], [%iv.next, %backedge]
9367 // %iv.next = binop %iv, Step
9368 // Or:
9369 // %iv = [Start, %entry], [%iv.next, %backedge]
9370 // %iv.next = binop Step, %iv
9371 return matchTwoInputRecurrence(P, BO, Start, Step);
9372}
9373
9375 Value *&Start, Value *&Step) {
9376 BinaryOperator *BO = nullptr;
9377 P = dyn_cast<PHINode>(I->getOperand(0));
9378 if (!P)
9379 P = dyn_cast<PHINode>(I->getOperand(1));
9380 return P && matchSimpleRecurrence(P, BO, Start, Step) && BO == I;
9381}
9382
9384 PHINode *&P, Value *&Init,
9385 Value *&OtherOp) {
9386 // Binary intrinsics only supported for now.
9387 if (I->arg_size() != 2 || I->getType() != I->getArgOperand(0)->getType() ||
9388 I->getType() != I->getArgOperand(1)->getType())
9389 return false;
9390
9391 IntrinsicInst *II = nullptr;
9392 P = dyn_cast<PHINode>(I->getArgOperand(0));
9393 if (!P)
9394 P = dyn_cast<PHINode>(I->getArgOperand(1));
9395
9396 return P && matchTwoInputRecurrence(P, II, Init, OtherOp) && II == I;
9397}
9398
9400 PHINode *&P, Value *&Init,
9401 Value *&OtherOp0,
9402 Value *&OtherOp1) {
9403 if (I->arg_size() != 3 || I->getType() != I->getArgOperand(0)->getType() ||
9404 I->getType() != I->getArgOperand(1)->getType() ||
9405 I->getType() != I->getArgOperand(2)->getType())
9406 return false;
9407 IntrinsicInst *II = nullptr;
9408 P = dyn_cast<PHINode>(I->getArgOperand(0));
9409 if (!P) {
9410 P = dyn_cast<PHINode>(I->getArgOperand(1));
9411 if (!P)
9412 P = dyn_cast<PHINode>(I->getArgOperand(2));
9413 }
9414 return P && matchThreeInputRecurrence(P, II, Init, OtherOp0, OtherOp1) &&
9415 II == I;
9416}
9417
9418/// Return true if "icmp Pred LHS RHS" is always true.
9420 const Value *RHS) {
9421 if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS)
9422 return true;
9423
9424 switch (Pred) {
9425 default:
9426 return false;
9427
9428 case CmpInst::ICMP_SLE: {
9429 const APInt *C;
9430
9431 // LHS s<= LHS +_{nsw} C if C >= 0
9432 // LHS s<= LHS | C if C >= 0
9433 if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C))) ||
9435 return !C->isNegative();
9436
9437 // LHS s<= smax(LHS, V) for any V
9439 return true;
9440
9441 // smin(RHS, V) s<= RHS for any V
9443 return true;
9444
9445 // Match A to (X +_{nsw} CA) and B to (X +_{nsw} CB)
9446 const Value *X;
9447 const APInt *CLHS, *CRHS;
9448 if (match(LHS, m_NSWAddLike(m_Value(X), m_APInt(CLHS))) &&
9450 return CLHS->sle(*CRHS);
9451
9452 return false;
9453 }
9454
9455 case CmpInst::ICMP_ULE: {
9456 // LHS u<= LHS +_{nuw} V for any V
9457 if (match(RHS, m_c_Add(m_Specific(LHS), m_Value())) &&
9459 return true;
9460
9461 // LHS u<= LHS | V for any V
9462 if (match(RHS, m_c_Or(m_Specific(LHS), m_Value())))
9463 return true;
9464
9465 // LHS u<= umax(LHS, V) for any V
9467 return true;
9468
9469 // RHS >> V u<= RHS for any V
9470 if (match(LHS, m_LShr(m_Specific(RHS), m_Value())))
9471 return true;
9472
9473 // RHS u/ C_ugt_1 u<= RHS
9474 const APInt *C;
9475 if (match(LHS, m_UDiv(m_Specific(RHS), m_APInt(C))) && C->ugt(1))
9476 return true;
9477
9478 // RHS & V u<= RHS for any V
9480 return true;
9481
9482 // umin(RHS, V) u<= RHS for any V
9484 return true;
9485
9486 // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
9487 const Value *X;
9488 const APInt *CLHS, *CRHS;
9489 if (match(LHS, m_NUWAddLike(m_Value(X), m_APInt(CLHS))) &&
9491 return CLHS->ule(*CRHS);
9492
9493 return false;
9494 }
9495 }
9496}
9497
9498/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
9499/// ALHS ARHS" is true. Otherwise, return std::nullopt.
9500static std::optional<bool>
9502 const Value *ARHS, const Value *BLHS, const Value *BRHS) {
9503 switch (Pred) {
9504 default:
9505 return std::nullopt;
9506
9507 case CmpInst::ICMP_SLT:
9508 case CmpInst::ICMP_SLE:
9509 if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS) &&
9511 return true;
9512 return std::nullopt;
9513
9514 case CmpInst::ICMP_SGT:
9515 case CmpInst::ICMP_SGE:
9516 if (isTruePredicate(CmpInst::ICMP_SLE, ALHS, BLHS) &&
9518 return true;
9519 return std::nullopt;
9520
9521 case CmpInst::ICMP_ULT:
9522 case CmpInst::ICMP_ULE:
9523 if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS) &&
9525 return true;
9526 return std::nullopt;
9527
9528 case CmpInst::ICMP_UGT:
9529 case CmpInst::ICMP_UGE:
9530 if (isTruePredicate(CmpInst::ICMP_ULE, ALHS, BLHS) &&
9532 return true;
9533 return std::nullopt;
9534 }
9535}
9536
9537/// Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
9538/// Return false if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is false.
9539/// Otherwise, return std::nullopt if we can't infer anything.
9540static std::optional<bool>
9542 CmpPredicate RPred, const ConstantRange &RCR) {
9543 auto CRImpliesPred = [&](ConstantRange CR,
9544 CmpInst::Predicate Pred) -> std::optional<bool> {
9545 // If all true values for lhs and true for rhs, lhs implies rhs
9546 if (CR.icmp(Pred, RCR))
9547 return true;
9548
9549 // If there is no overlap, lhs implies not rhs
9550 if (CR.icmp(CmpInst::getInversePredicate(Pred), RCR))
9551 return false;
9552
9553 return std::nullopt;
9554 };
9555 if (auto Res = CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9556 RPred))
9557 return Res;
9558 if (LPred.hasSameSign() ^ RPred.hasSameSign()) {
9560 : LPred.dropSameSign();
9562 : RPred.dropSameSign();
9563 return CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9564 RPred);
9565 }
9566 return std::nullopt;
9567}
9568
9569/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
9570/// is true. Return false if LHS implies RHS is false. Otherwise, return
9571/// std::nullopt if we can't infer anything.
9572static std::optional<bool>
9573isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1,
9574 CmpPredicate RPred, const Value *R0, const Value *R1,
9575 const DataLayout &DL, bool LHSIsTrue) {
9576 // The rest of the logic assumes the LHS condition is true. If that's not the
9577 // case, invert the predicate to make it so.
9578 if (!LHSIsTrue)
9579 LPred = ICmpInst::getInverseCmpPredicate(LPred);
9580
9581 // We can have non-canonical operands, so try to normalize any common operand
9582 // to L0/R0.
9583 if (L0 == R1) {
9584 std::swap(R0, R1);
9585 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9586 }
9587 if (R0 == L1) {
9588 std::swap(L0, L1);
9589 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9590 }
9591 if (L1 == R1) {
9592 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
9593 if (L0 != R0 || match(L0, m_ImmConstant())) {
9594 std::swap(L0, L1);
9595 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9596 std::swap(R0, R1);
9597 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9598 }
9599 }
9600
9601 // See if we can infer anything if operand-0 matches and we have at least one
9602 // constant.
9603 const APInt *Unused;
9604 if (L0 == R0 && (match(L1, m_APInt(Unused)) || match(R1, m_APInt(Unused)))) {
9605 // Potential TODO: We could also further use the constant range of L0/R0 to
9606 // further constraint the constant ranges. At the moment this leads to
9607 // several regressions related to not transforming `multi_use(A + C0) eq/ne
9608 // C1` (see discussion: D58633).
9610 L1, ICmpInst::isSigned(LPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,
9611 /*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1);
9613 R1, ICmpInst::isSigned(RPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,
9614 /*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1);
9615 // Even if L1/R1 are not both constant, we can still sometimes deduce
9616 // relationship from a single constant. For example X u> Y implies X != 0.
9617 if (auto R = isImpliedCondCommonOperandWithCR(LPred, LCR, RPred, RCR))
9618 return R;
9619 // If both L1/R1 were exact constant ranges and we didn't get anything
9620 // here, we won't be able to deduce this.
9621 if (match(L1, m_APInt(Unused)) && match(R1, m_APInt(Unused)))
9622 return std::nullopt;
9623 }
9624
9625 // Can we infer anything when the two compares have matching operands?
9626 if (L0 == R0 && L1 == R1)
9627 return ICmpInst::isImpliedByMatchingCmp(LPred, RPred);
9628
9629 // It only really makes sense in the context of signed comparison for "X - Y
9630 // must be positive if X >= Y and no overflow".
9631 // Take SGT as an example: L0:x > L1:y and C >= 0
9632 // ==> R0:(x -nsw y) < R1:(-C) is false
9633 CmpInst::Predicate SignedLPred = LPred.getPreferredSignedPredicate();
9634 if ((SignedLPred == ICmpInst::ICMP_SGT ||
9635 SignedLPred == ICmpInst::ICMP_SGE) &&
9636 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9637 if (match(R1, m_NonPositive()) &&
9638 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == false)
9639 return false;
9640 }
9641
9642 // Take SLT as an example: L0:x < L1:y and C <= 0
9643 // ==> R0:(x -nsw y) < R1:(-C) is true
9644 if ((SignedLPred == ICmpInst::ICMP_SLT ||
9645 SignedLPred == ICmpInst::ICMP_SLE) &&
9646 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9647 if (match(R1, m_NonNegative()) &&
9648 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == true)
9649 return true;
9650 }
9651
9652 // a - b == NonZero -> a != b
9653 // ptrtoint(a) - ptrtoint(b) == NonZero -> a != b
9654 const APInt *L1C;
9655 Value *A, *B;
9656 if (LPred == ICmpInst::ICMP_EQ && ICmpInst::isEquality(RPred) &&
9657 match(L1, m_APInt(L1C)) && !L1C->isZero() &&
9658 match(L0, m_Sub(m_Value(A), m_Value(B))) &&
9659 ((A == R0 && B == R1) || (A == R1 && B == R0) ||
9664 return RPred.dropSameSign() == ICmpInst::ICMP_NE;
9665 }
9666
9667 // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1
9668 if (L0 == R0 &&
9669 (LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) &&
9670 (RPred == ICmpInst::ICMP_ULT || RPred == ICmpInst::ICMP_UGE) &&
9671 match(L0, m_c_Add(m_Specific(L1), m_Specific(R1))))
9672 return CmpPredicate::getMatching(LPred, RPred).has_value();
9673
9674 if (auto P = CmpPredicate::getMatching(LPred, RPred))
9675 return isImpliedCondOperands(*P, L0, L1, R0, R1);
9676
9677 return std::nullopt;
9678}
9679
9680/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
9681/// is true. Return false if LHS implies RHS is false. Otherwise, return
9682/// std::nullopt if we can't infer anything.
9683static std::optional<bool>
9685 FCmpInst::Predicate RPred, const Value *R0, const Value *R1,
9686 const DataLayout &DL, bool LHSIsTrue) {
9687 // The rest of the logic assumes the LHS condition is true. If that's not the
9688 // case, invert the predicate to make it so.
9689 if (!LHSIsTrue)
9690 LPred = FCmpInst::getInversePredicate(LPred);
9691
9692 // We can have non-canonical operands, so try to normalize any common operand
9693 // to L0/R0.
9694 if (L0 == R1) {
9695 std::swap(R0, R1);
9696 RPred = FCmpInst::getSwappedPredicate(RPred);
9697 }
9698 if (R0 == L1) {
9699 std::swap(L0, L1);
9700 LPred = FCmpInst::getSwappedPredicate(LPred);
9701 }
9702 if (L1 == R1) {
9703 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
9704 if (L0 != R0 || match(L0, m_ImmConstant())) {
9705 std::swap(L0, L1);
9706 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9707 std::swap(R0, R1);
9708 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9709 }
9710 }
9711
9712 // Can we infer anything when the two compares have matching operands?
9713 if (L0 == R0 && L1 == R1) {
9714 if ((LPred & RPred) == LPred)
9715 return true;
9716 if ((LPred & ~RPred) == LPred)
9717 return false;
9718 }
9719
9720 // See if we can infer anything if operand-0 matches and we have at least one
9721 // constant.
9722 const APFloat *L1C, *R1C;
9723 if (L0 == R0 && match(L1, m_APFloat(L1C)) && match(R1, m_APFloat(R1C))) {
9724 if (std::optional<ConstantFPRange> DomCR =
9726 if (std::optional<ConstantFPRange> ImpliedCR =
9728 if (ImpliedCR->contains(*DomCR))
9729 return true;
9730 }
9731 if (std::optional<ConstantFPRange> ImpliedCR =
9733 FCmpInst::getInversePredicate(RPred), *R1C)) {
9734 if (ImpliedCR->contains(*DomCR))
9735 return false;
9736 }
9737 }
9738 }
9739
9740 return std::nullopt;
9741}
9742
9743/// Return true if LHS implies RHS is true. Return false if LHS implies RHS is
9744/// false. Otherwise, return std::nullopt if we can't infer anything. We
9745/// expect the RHS to be an icmp and the LHS to be an 'and', 'or', or a 'select'
9746/// instruction.
9747static std::optional<bool>
9749 const Value *RHSOp0, const Value *RHSOp1,
9750 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9751 // The LHS must be an 'or', 'and', or a 'select' instruction.
9752 assert((LHS->getOpcode() == Instruction::And ||
9753 LHS->getOpcode() == Instruction::Or ||
9754 LHS->getOpcode() == Instruction::Select) &&
9755 "Expected LHS to be 'and', 'or', or 'select'.");
9756
9757 assert(Depth <= MaxAnalysisRecursionDepth && "Hit recursion limit");
9758
9759 // If the result of an 'or' is false, then we know both legs of the 'or' are
9760 // false. Similarly, if the result of an 'and' is true, then we know both
9761 // legs of the 'and' are true.
9762 const Value *ALHS, *ARHS;
9763 if ((!LHSIsTrue && match(LHS, m_LogicalOr(m_Value(ALHS), m_Value(ARHS)))) ||
9764 (LHSIsTrue && match(LHS, m_LogicalAnd(m_Value(ALHS), m_Value(ARHS))))) {
9765 // FIXME: Make this non-recursion.
9766 if (std::optional<bool> Implication = isImpliedCondition(
9767 ALHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9768 return Implication;
9769 if (std::optional<bool> Implication = isImpliedCondition(
9770 ARHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9771 return Implication;
9772 return std::nullopt;
9773 }
9774 return std::nullopt;
9775}
9776
9777std::optional<bool>
9779 const Value *RHSOp0, const Value *RHSOp1,
9780 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9781 // Bail out when we hit the limit.
9783 return std::nullopt;
9784
9785 // A mismatch occurs when we compare a scalar cmp to a vector cmp, for
9786 // example.
9787 if (RHSOp0->getType()->isVectorTy() != LHS->getType()->isVectorTy())
9788 return std::nullopt;
9789
9790 assert(LHS->getType()->isIntOrIntVectorTy(1) &&
9791 "Expected integer type only!");
9792
9793 // Match not
9794 if (match(LHS, m_Not(m_Value(LHS))))
9795 LHSIsTrue = !LHSIsTrue;
9796
9797 // Both LHS and RHS are icmps.
9798 if (RHSOp0->getType()->getScalarType()->isIntOrPtrTy()) {
9799 if (const auto *LHSCmp = dyn_cast<ICmpInst>(LHS))
9800 return isImpliedCondICmps(LHSCmp->getCmpPredicate(),
9801 LHSCmp->getOperand(0), LHSCmp->getOperand(1),
9802 RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue);
9803 const Value *V;
9804 if (match(LHS, m_NUWTrunc(m_Value(V))))
9806 ConstantInt::get(V->getType(), 0), RHSPred,
9807 RHSOp0, RHSOp1, DL, LHSIsTrue);
9808 } else {
9809 assert(RHSOp0->getType()->isFPOrFPVectorTy() &&
9810 "Expected floating point type only!");
9811 if (const auto *LHSCmp = dyn_cast<FCmpInst>(LHS))
9812 return isImpliedCondFCmps(LHSCmp->getPredicate(), LHSCmp->getOperand(0),
9813 LHSCmp->getOperand(1), RHSPred, RHSOp0, RHSOp1,
9814 DL, LHSIsTrue);
9815 }
9816
9817 /// The LHS should be an 'or', 'and', or a 'select' instruction. We expect
9818 /// the RHS to be an icmp.
9819 /// FIXME: Add support for and/or/select on the RHS.
9820 if (const Instruction *LHSI = dyn_cast<Instruction>(LHS)) {
9821 if ((LHSI->getOpcode() == Instruction::And ||
9822 LHSI->getOpcode() == Instruction::Or ||
9823 LHSI->getOpcode() == Instruction::Select))
9824 return isImpliedCondAndOr(LHSI, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue,
9825 Depth);
9826 }
9827 return std::nullopt;
9828}
9829
9830std::optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS,
9831 const DataLayout &DL,
9832 bool LHSIsTrue, unsigned Depth) {
9833 // LHS ==> RHS by definition
9834 if (LHS == RHS)
9835 return LHSIsTrue;
9836
9837 // Match not
9838 bool InvertRHS = false;
9839 if (match(RHS, m_Not(m_Value(RHS)))) {
9840 if (LHS == RHS)
9841 return !LHSIsTrue;
9842 InvertRHS = true;
9843 }
9844
9845 if (const ICmpInst *RHSCmp = dyn_cast<ICmpInst>(RHS)) {
9846 if (auto Implied = isImpliedCondition(
9847 LHS, RHSCmp->getCmpPredicate(), RHSCmp->getOperand(0),
9848 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))
9849 return InvertRHS ? !*Implied : *Implied;
9850 return std::nullopt;
9851 }
9852 if (const FCmpInst *RHSCmp = dyn_cast<FCmpInst>(RHS)) {
9853 if (auto Implied = isImpliedCondition(
9854 LHS, RHSCmp->getPredicate(), RHSCmp->getOperand(0),
9855 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))
9856 return InvertRHS ? !*Implied : *Implied;
9857 return std::nullopt;
9858 }
9859
9860 const Value *V;
9861 if (match(RHS, m_NUWTrunc(m_Value(V)))) {
9862 if (auto Implied = isImpliedCondition(LHS, CmpInst::ICMP_NE, V,
9863 ConstantInt::get(V->getType(), 0), DL,
9864 LHSIsTrue, Depth))
9865 return InvertRHS ? !*Implied : *Implied;
9866 return std::nullopt;
9867 }
9868
9870 return std::nullopt;
9871
9872 // LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS2
9873 // LHS ==> !(RHS1 && RHS2) if LHS ==> !RHS1 or LHS ==> !RHS2
9874 const Value *RHS1, *RHS2;
9875 if (match(RHS, m_LogicalOr(m_Value(RHS1), m_Value(RHS2)))) {
9876 if (std::optional<bool> Imp =
9877 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9878 if (*Imp == true)
9879 return !InvertRHS;
9880 if (std::optional<bool> Imp =
9881 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9882 if (*Imp == true)
9883 return !InvertRHS;
9884 }
9885 if (match(RHS, m_LogicalAnd(m_Value(RHS1), m_Value(RHS2)))) {
9886 if (std::optional<bool> Imp =
9887 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9888 if (*Imp == false)
9889 return InvertRHS;
9890 if (std::optional<bool> Imp =
9891 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9892 if (*Imp == false)
9893 return InvertRHS;
9894 }
9895
9896 return std::nullopt;
9897}
9898
9899// Returns a pair (Condition, ConditionIsTrue), where Condition is a branch
9900// condition dominating ContextI or nullptr, if no condition is found.
9901static std::pair<Value *, bool>
9903 if (!ContextI || !ContextI->getParent())
9904 return {nullptr, false};
9905
9906 // TODO: This is a poor/cheap way to determine dominance. Should we use a
9907 // dominator tree (eg, from a SimplifyQuery) instead?
9908 const BasicBlock *ContextBB = ContextI->getParent();
9909 const BasicBlock *PredBB = ContextBB->getSinglePredecessor();
9910 if (!PredBB)
9911 return {nullptr, false};
9912
9913 // We need a conditional branch in the predecessor.
9914 Value *PredCond;
9915 BasicBlock *TrueBB, *FalseBB;
9916 if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))
9917 return {nullptr, false};
9918
9919 // The branch should get simplified. Don't bother simplifying this condition.
9920 if (TrueBB == FalseBB)
9921 return {nullptr, false};
9922
9923 assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
9924 "Predecessor block does not point to successor?");
9925
9926 // Is this condition implied by the predecessor condition?
9927 return {PredCond, TrueBB == ContextBB};
9928}
9929
9930std::optional<bool> llvm::isImpliedByDomCondition(const Value *Cond,
9931 const Instruction *ContextI,
9932 const DataLayout &DL) {
9933 assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool");
9934 auto PredCond = getDomPredecessorCondition(ContextI);
9935 if (PredCond.first)
9936 return isImpliedCondition(PredCond.first, Cond, DL, PredCond.second);
9937 return std::nullopt;
9938}
9939
9941 const Value *LHS,
9942 const Value *RHS,
9943 const Instruction *ContextI,
9944 const DataLayout &DL) {
9945 auto PredCond = getDomPredecessorCondition(ContextI);
9946 if (PredCond.first)
9947 return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL,
9948 PredCond.second);
9949 return std::nullopt;
9950}
9951
9953 APInt &Upper, const InstrInfoQuery &IIQ,
9954 bool PreferSignedRange) {
9955 unsigned Width = Lower.getBitWidth();
9956 const APInt *C;
9957 switch (BO.getOpcode()) {
9958 case Instruction::Sub:
9959 if (match(BO.getOperand(0), m_APInt(C))) {
9960 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
9961 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
9962
9963 // If the caller expects a signed compare, then try to use a signed range.
9964 // Otherwise if both no-wraps are set, use the unsigned range because it
9965 // is never larger than the signed range. Example:
9966 // "sub nuw nsw i8 -2, x" is unsigned [0, 254] vs. signed [-128, 126].
9967 // "sub nuw nsw i8 2, x" is unsigned [0, 2] vs. signed [-125, 127].
9968 if (PreferSignedRange && HasNSW && HasNUW)
9969 HasNUW = false;
9970
9971 if (HasNUW) {
9972 // 'sub nuw c, x' produces [0, C].
9973 Upper = *C + 1;
9974 } else if (HasNSW) {
9975 if (C->isNegative()) {
9976 // 'sub nsw -C, x' produces [SINT_MIN, -C - SINT_MIN].
9978 Upper = *C - APInt::getSignedMaxValue(Width);
9979 } else {
9980 // Note that sub 0, INT_MIN is not NSW. It techically is a signed wrap
9981 // 'sub nsw C, x' produces [C - SINT_MAX, SINT_MAX].
9982 Lower = *C - APInt::getSignedMaxValue(Width);
9984 }
9985 }
9986 }
9987 break;
9988 case Instruction::Add:
9989 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
9990 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
9991 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
9992
9993 // If the caller expects a signed compare, then try to use a signed
9994 // range. Otherwise if both no-wraps are set, use the unsigned range
9995 // because it is never larger than the signed range. Example: "add nuw
9996 // nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125].
9997 if (PreferSignedRange && HasNSW && HasNUW)
9998 HasNUW = false;
9999
10000 if (HasNUW) {
10001 // 'add nuw x, C' produces [C, UINT_MAX].
10002 Lower = *C;
10003 } else if (HasNSW) {
10004 if (C->isNegative()) {
10005 // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C].
10007 Upper = APInt::getSignedMaxValue(Width) + *C + 1;
10008 } else {
10009 // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX].
10010 Lower = APInt::getSignedMinValue(Width) + *C;
10011 Upper = APInt::getSignedMaxValue(Width) + 1;
10012 }
10013 }
10014 }
10015 break;
10016
10017 case Instruction::And:
10018 if (match(BO.getOperand(1), m_APInt(C)))
10019 // 'and x, C' produces [0, C].
10020 Upper = *C + 1;
10021 // X & -X is a power of two or zero. So we can cap the value at max power of
10022 // two.
10023 if (match(BO.getOperand(0), m_Neg(m_Specific(BO.getOperand(1)))) ||
10024 match(BO.getOperand(1), m_Neg(m_Specific(BO.getOperand(0)))))
10025 Upper = APInt::getSignedMinValue(Width) + 1;
10026 break;
10027
10028 case Instruction::Or:
10029 if (match(BO.getOperand(1), m_APInt(C)))
10030 // 'or x, C' produces [C, UINT_MAX].
10031 Lower = *C;
10032 break;
10033
10034 case Instruction::AShr:
10035 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10036 // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C].
10038 Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1;
10039 } else if (match(BO.getOperand(0), m_APInt(C))) {
10040 unsigned ShiftAmount = Width - 1;
10041 if (!C->isZero() && IIQ.isExact(&BO))
10042 ShiftAmount = C->countr_zero();
10043 if (C->isNegative()) {
10044 // 'ashr C, x' produces [C, C >> (Width-1)]
10045 Lower = *C;
10046 Upper = C->ashr(ShiftAmount) + 1;
10047 } else {
10048 // 'ashr C, x' produces [C >> (Width-1), C]
10049 Lower = C->ashr(ShiftAmount);
10050 Upper = *C + 1;
10051 }
10052 }
10053 break;
10054
10055 case Instruction::LShr:
10056 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10057 // 'lshr x, C' produces [0, UINT_MAX >> C].
10058 Upper = APInt::getAllOnes(Width).lshr(*C) + 1;
10059 } else if (match(BO.getOperand(0), m_APInt(C))) {
10060 // 'lshr C, x' produces [C >> (Width-1), C].
10061 unsigned ShiftAmount = Width - 1;
10062 if (!C->isZero() && IIQ.isExact(&BO))
10063 ShiftAmount = C->countr_zero();
10064 Lower = C->lshr(ShiftAmount);
10065 Upper = *C + 1;
10066 }
10067 break;
10068
10069 case Instruction::Shl:
10070 if (match(BO.getOperand(0), m_APInt(C))) {
10071 if (IIQ.hasNoUnsignedWrap(&BO)) {
10072 // 'shl nuw C, x' produces [C, C << CLZ(C)]
10073 Lower = *C;
10074 Upper = Lower.shl(Lower.countl_zero()) + 1;
10075 } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw?
10076 if (C->isNegative()) {
10077 // 'shl nsw C, x' produces [C << CLO(C)-1, C]
10078 unsigned ShiftAmount = C->countl_one() - 1;
10079 Lower = C->shl(ShiftAmount);
10080 Upper = *C + 1;
10081 } else {
10082 // 'shl nsw C, x' produces [C, C << CLZ(C)-1]
10083 unsigned ShiftAmount = C->countl_zero() - 1;
10084 Lower = *C;
10085 Upper = C->shl(ShiftAmount) + 1;
10086 }
10087 } else {
10088 // If lowbit is set, value can never be zero.
10089 if ((*C)[0])
10090 Lower = APInt::getOneBitSet(Width, 0);
10091 // If we are shifting a constant the largest it can be is if the longest
10092 // sequence of consecutive ones is shifted to the highbits (breaking
10093 // ties for which sequence is higher). At the moment we take a liberal
10094 // upper bound on this by just popcounting the constant.
10095 // TODO: There may be a bitwise trick for it longest/highest
10096 // consecutative sequence of ones (naive method is O(Width) loop).
10097 Upper = APInt::getHighBitsSet(Width, C->popcount()) + 1;
10098 }
10099 } else if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10100 Upper = APInt::getBitsSetFrom(Width, C->getZExtValue()) + 1;
10101 }
10102 break;
10103
10104 case Instruction::SDiv:
10105 if (match(BO.getOperand(1), m_APInt(C))) {
10106 APInt IntMin = APInt::getSignedMinValue(Width);
10107 APInt IntMax = APInt::getSignedMaxValue(Width);
10108 if (C->isAllOnes()) {
10109 // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX]
10110 // where C != -1 and C != 0 and C != 1
10111 Lower = IntMin + 1;
10112 Upper = IntMax + 1;
10113 } else if (C->countl_zero() < Width - 1) {
10114 // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C]
10115 // where C != -1 and C != 0 and C != 1
10116 Lower = IntMin.sdiv(*C);
10117 Upper = IntMax.sdiv(*C);
10118 if (Lower.sgt(Upper))
10120 Upper = Upper + 1;
10121 assert(Upper != Lower && "Upper part of range has wrapped!");
10122 }
10123 } else if (match(BO.getOperand(0), m_APInt(C))) {
10124 if (C->isMinSignedValue()) {
10125 // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2].
10126 Lower = *C;
10127 Upper = Lower.lshr(1) + 1;
10128 } else {
10129 // 'sdiv C, x' produces [-|C|, |C|].
10130 Upper = C->abs() + 1;
10131 Lower = (-Upper) + 1;
10132 }
10133 }
10134 break;
10135
10136 case Instruction::UDiv:
10137 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
10138 // 'udiv x, C' produces [0, UINT_MAX / C].
10139 Upper = APInt::getMaxValue(Width).udiv(*C) + 1;
10140 } else if (match(BO.getOperand(0), m_APInt(C))) {
10141 // 'udiv C, x' produces [0, C].
10142 Upper = *C + 1;
10143 }
10144 break;
10145
10146 case Instruction::SRem:
10147 if (match(BO.getOperand(1), m_APInt(C))) {
10148 // 'srem x, C' produces (-|C|, |C|).
10149 Upper = C->abs();
10150 Lower = (-Upper) + 1;
10151 } else if (match(BO.getOperand(0), m_APInt(C))) {
10152 if (C->isNegative()) {
10153 // 'srem -|C|, x' produces [-|C|, 0].
10154 Upper = 1;
10155 Lower = *C;
10156 } else {
10157 // 'srem |C|, x' produces [0, |C|].
10158 Upper = *C + 1;
10159 }
10160 }
10161 break;
10162
10163 case Instruction::URem:
10164 if (match(BO.getOperand(1), m_APInt(C)))
10165 // 'urem x, C' produces [0, C).
10166 Upper = *C;
10167 else if (match(BO.getOperand(0), m_APInt(C)))
10168 // 'urem C, x' produces [0, C].
10169 Upper = *C + 1;
10170 break;
10171
10172 default:
10173 break;
10174 }
10175}
10176
10178 bool UseInstrInfo) {
10179 unsigned Width = II.getType()->getScalarSizeInBits();
10180 const APInt *C;
10181 switch (II.getIntrinsicID()) {
10182 case Intrinsic::ctlz:
10183 case Intrinsic::cttz: {
10184 APInt Upper(Width, Width);
10185 if (!UseInstrInfo || !match(II.getArgOperand(1), m_One()))
10186 Upper += 1;
10187 // Maximum of set/clear bits is the bit width.
10189 }
10190 case Intrinsic::ctpop:
10191 // Maximum of set/clear bits is the bit width.
10193 APInt(Width, Width) + 1);
10194 case Intrinsic::uadd_sat:
10195 // uadd.sat(x, C) produces [C, UINT_MAX].
10196 if (match(II.getOperand(0), m_APInt(C)) ||
10197 match(II.getOperand(1), m_APInt(C)))
10199 break;
10200 case Intrinsic::sadd_sat:
10201 if (match(II.getOperand(0), m_APInt(C)) ||
10202 match(II.getOperand(1), m_APInt(C))) {
10203 if (C->isNegative())
10204 // sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)].
10206 APInt::getSignedMaxValue(Width) + *C +
10207 1);
10208
10209 // sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].
10211 APInt::getSignedMaxValue(Width) + 1);
10212 }
10213 break;
10214 case Intrinsic::usub_sat:
10215 // usub.sat(C, x) produces [0, C].
10216 if (match(II.getOperand(0), m_APInt(C)))
10217 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
10218
10219 // usub.sat(x, C) produces [0, UINT_MAX - C].
10220 if (match(II.getOperand(1), m_APInt(C)))
10222 APInt::getMaxValue(Width) - *C + 1);
10223 break;
10224 case Intrinsic::ssub_sat:
10225 if (match(II.getOperand(0), m_APInt(C))) {
10226 if (C->isNegative())
10227 // ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)].
10229 *C - APInt::getSignedMinValue(Width) +
10230 1);
10231
10232 // ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].
10234 APInt::getSignedMaxValue(Width) + 1);
10235 } else if (match(II.getOperand(1), m_APInt(C))) {
10236 if (C->isNegative())
10237 // ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]:
10239 APInt::getSignedMaxValue(Width) + 1);
10240
10241 // ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].
10243 APInt::getSignedMaxValue(Width) - *C +
10244 1);
10245 }
10246 break;
10247 case Intrinsic::umin:
10248 case Intrinsic::umax:
10249 case Intrinsic::smin:
10250 case Intrinsic::smax:
10251 if (!match(II.getOperand(0), m_APInt(C)) &&
10252 !match(II.getOperand(1), m_APInt(C)))
10253 break;
10254
10255 switch (II.getIntrinsicID()) {
10256 case Intrinsic::umin:
10257 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
10258 case Intrinsic::umax:
10260 case Intrinsic::smin:
10262 *C + 1);
10263 case Intrinsic::smax:
10265 APInt::getSignedMaxValue(Width) + 1);
10266 default:
10267 llvm_unreachable("Must be min/max intrinsic");
10268 }
10269 break;
10270 case Intrinsic::abs:
10271 // If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX],
10272 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10273 if (match(II.getOperand(1), m_One()))
10275 APInt::getSignedMaxValue(Width) + 1);
10276
10278 APInt::getSignedMinValue(Width) + 1);
10279 case Intrinsic::vscale:
10280 if (!II.getParent() || !II.getFunction())
10281 break;
10282 return getVScaleRange(II.getFunction(), Width);
10283 default:
10284 break;
10285 }
10286
10287 return ConstantRange::getFull(Width);
10288}
10289
10291 const InstrInfoQuery &IIQ) {
10292 unsigned BitWidth = SI.getType()->getScalarSizeInBits();
10293 const Value *LHS = nullptr, *RHS = nullptr;
10295 if (R.Flavor == SPF_UNKNOWN)
10296 return ConstantRange::getFull(BitWidth);
10297
10298 if (R.Flavor == SelectPatternFlavor::SPF_ABS) {
10299 // If the negation part of the abs (in RHS) has the NSW flag,
10300 // then the result of abs(X) is [0..SIGNED_MAX],
10301 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10302 if (match(RHS, m_Neg(m_Specific(LHS))) &&
10306
10309 }
10310
10311 if (R.Flavor == SelectPatternFlavor::SPF_NABS) {
10312 // The result of -abs(X) is <= 0.
10314 APInt(BitWidth, 1));
10315 }
10316
10317 const APInt *C;
10318 if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C)))
10319 return ConstantRange::getFull(BitWidth);
10320
10321 switch (R.Flavor) {
10322 case SPF_UMIN:
10324 case SPF_UMAX:
10326 case SPF_SMIN:
10328 *C + 1);
10329 case SPF_SMAX:
10332 default:
10333 return ConstantRange::getFull(BitWidth);
10334 }
10335}
10336
10338 // The maximum representable value of a half is 65504. For floats the maximum
10339 // value is 3.4e38 which requires roughly 129 bits.
10340 unsigned BitWidth = I->getType()->getScalarSizeInBits();
10341 if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy())
10342 return;
10343 if (isa<FPToSIInst>(I) && BitWidth >= 17) {
10344 Lower = APInt(BitWidth, -65504, true);
10345 Upper = APInt(BitWidth, 65505);
10346 }
10347
10348 if (isa<FPToUIInst>(I) && BitWidth >= 16) {
10349 // For a fptoui the lower limit is left as 0.
10350 Upper = APInt(BitWidth, 65505);
10351 }
10352}
10353
10355 bool UseInstrInfo, AssumptionCache *AC,
10356 const Instruction *CtxI,
10357 const DominatorTree *DT,
10358 unsigned Depth) {
10359 assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction");
10360
10362 return ConstantRange::getFull(V->getType()->getScalarSizeInBits());
10363
10364 if (auto *C = dyn_cast<Constant>(V))
10365 return C->toConstantRange();
10366
10367 unsigned BitWidth = V->getType()->getScalarSizeInBits();
10368 InstrInfoQuery IIQ(UseInstrInfo);
10369 ConstantRange CR = ConstantRange::getFull(BitWidth);
10370 if (auto *BO = dyn_cast<BinaryOperator>(V)) {
10371 APInt Lower = APInt(BitWidth, 0);
10372 APInt Upper = APInt(BitWidth, 0);
10373 // TODO: Return ConstantRange.
10374 setLimitsForBinOp(*BO, Lower, Upper, IIQ, ForSigned);
10376 } else if (auto *II = dyn_cast<IntrinsicInst>(V))
10377 CR = getRangeForIntrinsic(*II, UseInstrInfo);
10378 else if (auto *SI = dyn_cast<SelectInst>(V)) {
10380 SI->getTrueValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
10382 SI->getFalseValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
10383 CR = CRTrue.unionWith(CRFalse);
10385 } else if (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {
10386 APInt Lower = APInt(BitWidth, 0);
10387 APInt Upper = APInt(BitWidth, 0);
10388 // TODO: Return ConstantRange.
10391 } else if (const auto *A = dyn_cast<Argument>(V))
10392 if (std::optional<ConstantRange> Range = A->getRange())
10393 CR = *Range;
10394
10395 if (auto *I = dyn_cast<Instruction>(V)) {
10396 if (auto *Range = IIQ.getMetadata(I, LLVMContext::MD_range))
10398
10399 if (const auto *CB = dyn_cast<CallBase>(V))
10400 if (std::optional<ConstantRange> Range = CB->getRange())
10401 CR = CR.intersectWith(*Range);
10402 }
10403
10404 if (CtxI && AC) {
10405 // Try to restrict the range based on information from assumptions.
10406 for (auto &AssumeVH : AC->assumptionsFor(V)) {
10407 if (!AssumeVH)
10408 continue;
10409 CallInst *I = cast<CallInst>(AssumeVH);
10410 assert(I->getParent()->getParent() == CtxI->getParent()->getParent() &&
10411 "Got assumption for the wrong function!");
10412 assert(I->getIntrinsicID() == Intrinsic::assume &&
10413 "must be an assume intrinsic");
10414
10415 if (!isValidAssumeForContext(I, CtxI, DT))
10416 continue;
10417 Value *Arg = I->getArgOperand(0);
10418 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
10419 // Currently we just use information from comparisons.
10420 if (!Cmp || Cmp->getOperand(0) != V)
10421 continue;
10422 // TODO: Set "ForSigned" parameter via Cmp->isSigned()?
10423 ConstantRange RHS =
10424 computeConstantRange(Cmp->getOperand(1), /* ForSigned */ false,
10425 UseInstrInfo, AC, I, DT, Depth + 1);
10426 CR = CR.intersectWith(
10427 ConstantRange::makeAllowedICmpRegion(Cmp->getPredicate(), RHS));
10428 }
10429 }
10430
10431 return CR;
10432}
10433
10434static void
10436 function_ref<void(Value *)> InsertAffected) {
10437 assert(V != nullptr);
10438 if (isa<Argument>(V) || isa<GlobalValue>(V)) {
10439 InsertAffected(V);
10440 } else if (auto *I = dyn_cast<Instruction>(V)) {
10441 InsertAffected(V);
10442
10443 // Peek through unary operators to find the source of the condition.
10444 Value *Op;
10446 m_Trunc(m_Value(Op))))) {
10448 InsertAffected(Op);
10449 }
10450 }
10451}
10452
10454 Value *Cond, bool IsAssume, function_ref<void(Value *)> InsertAffected) {
10455 auto AddAffected = [&InsertAffected](Value *V) {
10456 addValueAffectedByCondition(V, InsertAffected);
10457 };
10458
10459 auto AddCmpOperands = [&AddAffected, IsAssume](Value *LHS, Value *RHS) {
10460 if (IsAssume) {
10461 AddAffected(LHS);
10462 AddAffected(RHS);
10463 } else if (match(RHS, m_Constant()))
10464 AddAffected(LHS);
10465 };
10466
10467 SmallVector<Value *, 8> Worklist;
10469 Worklist.push_back(Cond);
10470 while (!Worklist.empty()) {
10471 Value *V = Worklist.pop_back_val();
10472 if (!Visited.insert(V).second)
10473 continue;
10474
10475 CmpPredicate Pred;
10476 Value *A, *B, *X;
10477
10478 if (IsAssume) {
10479 AddAffected(V);
10480 if (match(V, m_Not(m_Value(X))))
10481 AddAffected(X);
10482 }
10483
10484 if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
10485 // assume(A && B) is split to -> assume(A); assume(B);
10486 // assume(!(A || B)) is split to -> assume(!A); assume(!B);
10487 // Finally, assume(A || B) / assume(!(A && B)) generally don't provide
10488 // enough information to be worth handling (intersection of information as
10489 // opposed to union).
10490 if (!IsAssume) {
10491 Worklist.push_back(A);
10492 Worklist.push_back(B);
10493 }
10494 } else if (match(V, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
10495 bool HasRHSC = match(B, m_ConstantInt());
10496 if (ICmpInst::isEquality(Pred)) {
10497 AddAffected(A);
10498 if (IsAssume)
10499 AddAffected(B);
10500 if (HasRHSC) {
10501 Value *Y;
10502 // (X << C) or (X >>_s C) or (X >>_u C).
10503 if (match(A, m_Shift(m_Value(X), m_ConstantInt())))
10504 AddAffected(X);
10505 // (X & C) or (X | C).
10506 else if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10507 match(A, m_Or(m_Value(X), m_Value(Y)))) {
10508 AddAffected(X);
10509 AddAffected(Y);
10510 }
10511 // X - Y
10512 else if (match(A, m_Sub(m_Value(X), m_Value(Y)))) {
10513 AddAffected(X);
10514 AddAffected(Y);
10515 }
10516 }
10517 } else {
10518 AddCmpOperands(A, B);
10519 if (HasRHSC) {
10520 // Handle (A + C1) u< C2, which is the canonical form of
10521 // A > C3 && A < C4.
10523 AddAffected(X);
10524
10525 if (ICmpInst::isUnsigned(Pred)) {
10526 Value *Y;
10527 // X & Y u> C -> X >u C && Y >u C
10528 // X | Y u< C -> X u< C && Y u< C
10529 // X nuw+ Y u< C -> X u< C && Y u< C
10530 if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10531 match(A, m_Or(m_Value(X), m_Value(Y))) ||
10532 match(A, m_NUWAdd(m_Value(X), m_Value(Y)))) {
10533 AddAffected(X);
10534 AddAffected(Y);
10535 }
10536 // X nuw- Y u> C -> X u> C
10537 if (match(A, m_NUWSub(m_Value(X), m_Value())))
10538 AddAffected(X);
10539 }
10540 }
10541
10542 // Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported
10543 // by computeKnownFPClass().
10545 if (Pred == ICmpInst::ICMP_SLT && match(B, m_Zero()))
10546 InsertAffected(X);
10547 else if (Pred == ICmpInst::ICMP_SGT && match(B, m_AllOnes()))
10548 InsertAffected(X);
10549 }
10550 }
10551
10552 if (HasRHSC && match(A, m_Intrinsic<Intrinsic::ctpop>(m_Value(X))))
10553 AddAffected(X);
10554 } else if (match(V, m_FCmp(Pred, m_Value(A), m_Value(B)))) {
10555 AddCmpOperands(A, B);
10556
10557 // fcmp fneg(x), y
10558 // fcmp fabs(x), y
10559 // fcmp fneg(fabs(x)), y
10560 if (match(A, m_FNeg(m_Value(A))))
10561 AddAffected(A);
10562 if (match(A, m_FAbs(m_Value(A))))
10563 AddAffected(A);
10564
10566 m_Value()))) {
10567 // Handle patterns that computeKnownFPClass() support.
10568 AddAffected(A);
10569 } else if (!IsAssume && match(V, m_Trunc(m_Value(X)))) {
10570 // Assume is checked here as X is already added above for assumes in
10571 // addValueAffectedByCondition
10572 AddAffected(X);
10573 } else if (!IsAssume && match(V, m_Not(m_Value(X)))) {
10574 // Assume is checked here to avoid issues with ephemeral values
10575 Worklist.push_back(X);
10576 }
10577 }
10578}
10579
10581 // (X >> C) or/add (X & mask(C) != 0)
10582 if (const auto *BO = dyn_cast<BinaryOperator>(V)) {
10583 if (BO->getOpcode() == Instruction::Add ||
10584 BO->getOpcode() == Instruction::Or) {
10585 const Value *X;
10586 const APInt *C1, *C2;
10587 if (match(BO, m_c_BinOp(m_LShr(m_Value(X), m_APInt(C1)),
10591 m_Zero())))) &&
10592 C2->popcount() == C1->getZExtValue())
10593 return X;
10594 }
10595 }
10596 return nullptr;
10597}
10598
10600 return const_cast<Value *>(stripNullTest(const_cast<const Value *>(V)));
10601}
10602
10605 unsigned MaxCount, bool AllowUndefOrPoison) {
10608 auto Push = [&](const Value *V) -> bool {
10609 Constant *C;
10610 if (match(const_cast<Value *>(V), m_ImmConstant(C))) {
10611 if (!AllowUndefOrPoison && !isGuaranteedNotToBeUndefOrPoison(C))
10612 return false;
10613 // Check existence first to avoid unnecessary allocations.
10614 if (Constants.contains(C))
10615 return true;
10616 if (Constants.size() == MaxCount)
10617 return false;
10618 Constants.insert(C);
10619 return true;
10620 }
10621
10622 if (auto *Inst = dyn_cast<Instruction>(V)) {
10623 if (Visited.insert(Inst).second)
10624 Worklist.push_back(Inst);
10625 return true;
10626 }
10627 return false;
10628 };
10629 if (!Push(V))
10630 return false;
10631 while (!Worklist.empty()) {
10632 const Instruction *CurInst = Worklist.pop_back_val();
10633 switch (CurInst->getOpcode()) {
10634 case Instruction::Select:
10635 if (!Push(CurInst->getOperand(1)))
10636 return false;
10637 if (!Push(CurInst->getOperand(2)))
10638 return false;
10639 break;
10640 case Instruction::PHI:
10641 for (Value *IncomingValue : cast<PHINode>(CurInst)->incoming_values()) {
10642 // Fast path for recurrence PHI.
10643 if (IncomingValue == CurInst)
10644 continue;
10645 if (!Push(IncomingValue))
10646 return false;
10647 }
10648 break;
10649 default:
10650 return false;
10651 }
10652 }
10653 return true;
10654}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Register Bank Select
Rewrite undef for PHI
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis Results
This file contains the simple types necessary to represent the attributes associated with functions a...
static const Function * getParent(const Value *V)
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 KnownBits getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts, const KnownBits &KnownLHS, const KnownBits &KnownRHS, const SimplifyQuery &Q, unsigned Depth)
static void breakSelfRecursivePHI(const Use *U, const PHINode *PHI, Value *&ValOut, Instruction *&CtxIOut, const PHINode **PhiOut=nullptr)
static bool isNonZeroSub(const APInt &DemandedElts, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, unsigned Depth)
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR)
Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
static void addValueAffectedByCondition(Value *V, function_ref< void(Value *)> InsertAffected)
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
static bool haveNoCommonBitsSetSpecialCases(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
static void setLimitsForBinOp(const BinaryOperator &BO, APInt &Lower, APInt &Upper, const InstrInfoQuery &IIQ, bool PreferSignedRange)
static Value * lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2, Instruction::CastOps *CastOp)
Helps to match a select pattern in case of a type mismatch.
static std::pair< Value *, bool > getDomPredecessorCondition(const Instruction *ContextI)
static constexpr unsigned MaxInstrsToCheckForFree
Maximum number of instructions to check between assume and context instruction.
static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, const KnownBits &KnownVal, unsigned Depth)
static std::optional< bool > isImpliedCondFCmps(FCmpInst::Predicate LPred, const Value *L0, const Value *L1, FCmpInst::Predicate RPred, const Value *R0, const Value *R1, const DataLayout &DL, bool LHSIsTrue)
Return true if LHS implies RHS (expanded to its components as "R0 RPred R1") is true.
UndefPoisonKind
static bool isKnownNonEqualFromContext(const Value *V1, const Value *V2, const SimplifyQuery &Q, unsigned Depth)
static bool includesPoison(UndefPoisonKind Kind)
static SelectPatternResult matchFastFloatClamp(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS)
Match clamp pattern for float types without care about NaNs or signed zeros.
static std::optional< bool > isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1, CmpPredicate RPred, const Value *R0, const Value *R1, const DataLayout &DL, bool LHSIsTrue)
Return true if LHS implies RHS (expanded to its components as "R0 RPred R1") is true.
static bool includesUndef(UndefPoisonKind Kind)
static std::optional< bool > isImpliedCondCommonOperandWithCR(CmpPredicate LPred, const ConstantRange &LCR, CmpPredicate RPred, const ConstantRange &RCR)
Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
static ConstantRange getRangeForSelectPattern(const SelectInst &SI, const InstrInfoQuery &IIQ)
static void computeKnownBitsFromOperator(const Operator *I, const APInt &DemandedElts, KnownBits &Known, const SimplifyQuery &Q, unsigned Depth)
static uint64_t GetStringLengthH(const Value *V, SmallPtrSetImpl< const PHINode * > &PHIs, unsigned CharSize)
If we can compute the length of the string pointed to by the specified pointer, return 'len+1'.
static void computeKnownBitsFromShiftOperator(const Operator *I, const APInt &DemandedElts, KnownBits &Known, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth, function_ref< KnownBits(const KnownBits &, const KnownBits &, bool)> KF)
Compute known bits from a shift operator, including those with a non-constant shift amount.
static bool onlyUsedByLifetimeMarkersOrDroppableInstsHelper(const Value *V, bool AllowLifetime, bool AllowDroppable)
static std::optional< bool > isImpliedCondAndOr(const Instruction *LHS, CmpPredicate RHSPred, const Value *RHSOp0, const Value *RHSOp1, const DataLayout &DL, bool LHSIsTrue, unsigned Depth)
Return true if LHS implies RHS is true.
static bool isAbsoluteValueLessEqualOne(const Value *V)
static bool isSignedMinMaxClamp(const Value *Select, const Value *&In, const APInt *&CLow, const APInt *&CHigh)
static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, bool NSW, bool NUW, unsigned Depth)
static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V, unsigned Depth)
static bool isNonEqualSelect(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst, Value *&Init, Value *&OtherOp)
static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static void computeKnownBitsFromCmp(const Value *V, CmpInst::Predicate Pred, Value *LHS, Value *RHS, KnownBits &Known, const SimplifyQuery &Q)
static SelectPatternResult matchMinMaxOfMinMax(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TVal, Value *FVal, unsigned Depth)
Recognize variations of: a < c ?
static void unionWithMinMaxIntrinsicClamp(const IntrinsicInst *II, KnownBits &Known)
static void setLimitForFPToI(const Instruction *I, APInt &Lower, APInt &Upper)
static bool isSameUnderlyingObjectInLoop(const PHINode *PN, const LoopInfo *LI)
PN defines a loop-variant pointer to an object.
static bool isNonEqualPointersWithRecursiveGEP(const Value *A, const Value *B, const SimplifyQuery &Q)
static bool isSignedMinMaxIntrinsicClamp(const IntrinsicInst *II, const APInt *&CLow, const APInt *&CHigh)
static Value * lookThroughCastConst(CmpInst *CmpI, Type *SrcTy, Constant *C, Instruction::CastOps *CastOp)
static bool handleGuaranteedWellDefinedOps(const Instruction *I, const CallableT &Handle)
Enumerates all operands of I that are guaranteed to not be undef or poison.
static void computeKnownBitsFromLerpPattern(const Value *Op0, const Value *Op1, const APInt &DemandedElts, KnownBits &KnownOut, const SimplifyQuery &Q, unsigned Depth)
Try to detect the lerp pattern: a * (b - c) + c * d where a >= 0, b >= 0, c >= 0, d >= 0,...
static KnownFPClass computeKnownFPClassFromContext(const Value *V, const SimplifyQuery &Q)
static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1, bool NSW, bool NUW, const APInt &DemandedElts, KnownBits &KnownOut, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth)
static Value * getNotValue(Value *V)
If the input value is the result of a 'not' op, constant integer, or vector splat of a constant integ...
static constexpr KnownFPClass::MinMaxKind getMinMaxKind(Intrinsic::ID IID)
static unsigned ComputeNumSignBitsImpl(const Value *V, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return the number of times the sign bit of the register is replicated into the other bits.
static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp, KnownBits &Known, const SimplifyQuery &SQ, bool Invert)
static bool isKnownNonZeroFromOperator(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static bool matchOpWithOpEqZero(Value *Op0, Value *Op1)
static bool isNonZeroRecurrence(const PHINode *PN)
Try to detect a recurrence that monotonically increases/decreases from a non-zero starting value.
static SelectPatternResult matchClamp(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal)
Recognize variations of: CLAMP(v,l,h) ==> ((v) < (l) ?
static bool shiftAmountKnownInRange(const Value *ShiftAmount)
Shifts return poison if shiftwidth is larger than the bitwidth.
static bool isEphemeralValueOf(const Instruction *I, const Value *E)
static SelectPatternResult matchMinMax(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, unsigned Depth)
Match non-obvious integer minimum and maximum sequences.
static KnownBits computeKnownBitsForHorizontalOperation(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth, const function_ref< KnownBits(const KnownBits &, const KnownBits &)> KnownBitsFunc)
static bool handleGuaranteedNonPoisonOps(const Instruction *I, const CallableT &Handle)
Enumerates all operands of I that are guaranteed to not be poison.
static std::optional< std::pair< Value *, Value * > > getInvertibleOperands(const Operator *Op1, const Operator *Op2)
If the pair of operators are the same invertible function, return the the operands of the function co...
static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS)
static void computeKnownBitsFromCond(const Value *V, Value *Cond, KnownBits &Known, const SimplifyQuery &SQ, bool Invert, unsigned Depth)
static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q)
static std::optional< bool > isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS, const Value *ARHS, const Value *BLHS, const Value *BRHS)
Return true if "icmp Pred BLHS BRHS" is true whenever "icmp PredALHS ARHS" is true.
static const Instruction * safeCxtI(const Value *V, const Instruction *CxtI)
static bool isNonEqualMul(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and the multiplication is nuw o...
static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero, const Value *Cond, bool CondIsTrue)
Return true if we can infer that V is known to be a power of 2 from dominating condition Cond (e....
static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW, bool NUW, const APInt &DemandedElts, KnownBits &Known, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth)
static bool matchThreeInputRecurrence(const PHINode *PN, InstTy *&Inst, Value *&Init, Value *&OtherOp0, Value *&OtherOp1)
static bool isKnownNonNaN(const Value *V, FastMathFlags FMF)
static ConstantRange getRangeForIntrinsic(const IntrinsicInst &II, bool UseInstrInfo)
static void computeKnownFPClassForFPTrunc(const Operator *Op, const APInt &DemandedElts, FPClassTest InterestedClasses, KnownFPClass &Known, const SimplifyQuery &Q, unsigned Depth)
static Value * BuildSubAggregate(Value *From, Value *To, Type *IndexedType, SmallVectorImpl< unsigned > &Idxs, unsigned IdxSkip, BasicBlock::iterator InsertBefore)
Value * RHS
Value * LHS
bool isFinite() const
Definition APFloat.h:1521
bool isNaN() const
Definition APFloat.h:1514
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1193
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1153
bool isInteger() const
Definition APFloat.h:1533
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
Definition APFloat.h:1134
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:1982
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1584
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:235
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition APInt.h:1421
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition APInt.h:424
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1555
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition APInt.h:1406
unsigned popcount() const
Count the number of bits set.
Definition APInt.h:1685
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1400
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition APInt.h:207
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition APInt.h:1345
unsigned ceilLogBase2() const
Definition APInt.h:1779
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition APInt.h:1208
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition APInt.h:372
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1189
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:381
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition APInt.cpp:1677
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1503
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1118
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:210
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition APInt.h:217
bool isNegative() const
Determine sign of this APInt.
Definition APInt.h:330
bool intersects(const APInt &RHS) const
This operation tests if there are any pairs of corresponding bits between this APInt and RHS that are...
Definition APInt.h:1256
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1655
void clearAllBits()
Set every bit to 0.
Definition APInt.h:1411
LLVM_ABI APInt reverseBits() const
Definition APInt.cpp:768
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition APInt.h:1173
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1643
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition APInt.h:1613
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:220
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1052
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition APInt.h:357
unsigned logBase2() const
Definition APInt.h:1776
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:834
bool getBoolValue() const
Convert APInt to a boolean value.
Definition APInt.h:472
bool isMaxSignedValue() const
Determine if this is the largest signed value.
Definition APInt.h:406
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:335
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition APInt.h:1157
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:880
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition APInt.h:1264
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition APInt.h:1137
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition APInt.h:297
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:201
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition APInt.h:1403
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1244
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:287
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:240
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:858
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1228
void clearSignBit()
Set the sign bit to 0.
Definition APInt.h:1464
an instruction to allocate memory on the stack
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator end() const
Definition ArrayRef.h:131
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
iterator begin() const
Definition ArrayRef.h:130
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Definition ArrayRef.h:186
Class to represent array types.
This represents the llvm.assume intrinsic.
A cache of @llvm.assume calls within a function.
MutableArrayRef< ResultElem > assumptionsFor(const Value *V)
Access the list of assumptions which affect this value.
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:105
LLVM_ABI std::optional< unsigned > getVScaleRangeMax() const
Returns the maximum value for the vscale_range attribute or std::nullopt when unknown.
LLVM_ABI unsigned getVScaleRangeMin() const
Returns the minimum value for the vscale_range attribute.
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition Attributes.h:261
LLVM_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:483
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:470
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
LLVM_ABI Intrinsic::ID getIntrinsicID() const
Returns the intrinsic ID of the intrinsic called or Intrinsic::not_intrinsic if the called function i...
unsigned arg_size() const
This class represents a function call, abstracting a target machine's calling convention.
This is the base class for all instructions that perform data casts.
Definition InstrTypes.h:448
This class is the base class for the comparison instructions.
Definition InstrTypes.h:664
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
@ ICMP_SLT
signed less than
Definition InstrTypes.h:705
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:706
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:682
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition InstrTypes.h:691
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:680
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:681
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:700
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:699
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:703
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition InstrTypes.h:690
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:701
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition InstrTypes.h:688
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:683
@ ICMP_NE
not equal
Definition InstrTypes.h:698
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:704
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:702
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition InstrTypes.h:689
bool isSigned() const
Definition InstrTypes.h:930
static LLVM_ABI bool isEquality(Predicate pred)
Determine if this is an equals/not equals predicate.
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition InstrTypes.h:827
bool isTrueWhenEqual() const
This is just a convenience.
Definition InstrTypes.h:942
static bool isFPPredicate(Predicate P)
Definition InstrTypes.h:770
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition InstrTypes.h:789
Predicate getPredicate() const
Return the predicate for this instruction.
Definition InstrTypes.h:765
Predicate getFlippedStrictnessPredicate() const
For predicate of kind "is X or equal to 0" returns the predicate "is X".
Definition InstrTypes.h:893
static bool isIntPredicate(Predicate P)
Definition InstrTypes.h:776
static LLVM_ABI bool isOrdered(Predicate predicate)
Determine if the predicate is an ordered operation.
bool isUnsigned() const
Definition InstrTypes.h:936
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
static LLVM_ABI std::optional< CmpPredicate > getMatching(CmpPredicate A, CmpPredicate B)
Compares two CmpPredicates taking samesign into account and returns the canonicalized CmpPredicate if...
LLVM_ABI CmpInst::Predicate getPreferredSignedPredicate() const
Attempts to return a signed CmpInst::Predicate from the CmpPredicate.
CmpInst::Predicate dropSameSign() const
Drops samesign information.
bool hasSameSign() const
Query samesign information, for optimizations.
An array constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition Constants.h:707
ConstantDataSequential - A vector or array constant whose element type is a simple 1/2/4/8-byte integ...
Definition Constants.h:598
StringRef getAsString() const
If this array is isString(), then this method returns the array as a StringRef.
Definition Constants.h:673
A vector constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition Constants.h:781
static LLVM_ABI Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static LLVM_ABI Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static LLVM_ABI std::optional< ConstantFPRange > makeExactFCmpRegion(FCmpInst::Predicate Pred, const APFloat &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:282
This is the shared class of boolean and integer constants.
Definition Constants.h:87
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition Constants.h:168
This class represents a range of values.
PreferredRangeType
If represented precisely, the result of some range operations may consist of multiple disjoint ranges...
static LLVM_ABI ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
LLVM_ABI OverflowResult unsignedSubMayOverflow(const ConstantRange &Other) const
Return whether unsigned sub of the two ranges always/never overflows.
LLVM_ABI bool isAllNegative() const
Return true if all values in this range are negative.
LLVM_ABI OverflowResult unsignedAddMayOverflow(const ConstantRange &Other) const
Return whether unsigned add of the two ranges always/never overflows.
LLVM_ABI KnownBits toKnownBits() const
Return known bits for values in this range.
LLVM_ABI bool icmp(CmpInst::Predicate Pred, const ConstantRange &Other) const
Does the predicate Pred hold between ranges this and Other?
LLVM_ABI OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const
Return whether unsigned mul of the two ranges always/never overflows.
LLVM_ABI bool isAllNonNegative() const
Return true if all values in this range are non-negative.
static LLVM_ABI ConstantRange makeAllowedICmpRegion(CmpInst::Predicate Pred, const ConstantRange &Other)
Produce the smallest range such that all values that may satisfy the given predicate with any value c...
LLVM_ABI ConstantRange unionWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the union of this range with another range.
static LLVM_ABI ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred, const APInt &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
LLVM_ABI bool contains(const APInt &Val) const
Return true if the specified value is in the set.
LLVM_ABI OverflowResult signedAddMayOverflow(const ConstantRange &Other) const
Return whether signed add of the two ranges always/never overflows.
LLVM_ABI ConstantRange intersectWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the intersection of this range with another range.
OverflowResult
Represents whether an operation on the given constant range is known to always or never overflow.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
static ConstantRange getNonEmpty(APInt Lower, APInt Upper)
Create non-empty constant range with the given bounds.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
LLVM_ABI OverflowResult signedSubMayOverflow(const ConstantRange &Other) const
Return whether signed sub of the two ranges always/never overflows.
LLVM_ABI ConstantRange sub(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a subtraction of a value in this r...
This is an important base class in LLVM.
Definition Constant.h:43
static LLVM_ABI Constant * replaceUndefsWith(Constant *C, Constant *Replacement)
Try to replace undefined constant C or undefined elements in C with Replacement.
LLVM_ABI Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
LLVM_ABI Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
LLVM_ABI bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition Constants.cpp:74
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
bool isLittleEndian() const
Layout endianness...
Definition DataLayout.h: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:23
bool noSignedZeros() const
Definition FMF.h:70
bool noInfs() const
Definition FMF.h:69
void setNoSignedZeros(bool B=true)
Definition FMF.h:87
void setNoNaNs(bool B=true)
Definition FMF.h:81
bool noNaNs() const
Definition FMF.h:68
const BasicBlock & getEntryBlock() const
Definition Function.h:809
bool hasNoSync() const
Determine if the call can synchroize with other threads.
Definition Function.h:645
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
PointerType * getType() const
Global values are always pointers.
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this global belongs to.
Definition Globals.cpp:133
Type * getValueType() const
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
This instruction compares its operands according to the predicate given to the constructor.
CmpPredicate getSwappedCmpPredicate() const
CmpPredicate getInverseCmpPredicate() const
Predicate getFlippedSignednessPredicate() const
For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->EQ.
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
static LLVM_ABI std::optional< bool > isImpliedByMatchingCmp(CmpPredicate Pred1, CmpPredicate Pred2)
Determine if Pred1 implies Pred2 is true, false, or if nothing can be inferred about the implication,...
bool isRelational() const
Return true if the predicate is relational (not EQ or NE).
Predicate getUnsignedPredicate() const
For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
This instruction inserts a struct field of array element value into an aggregate value.
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
LLVM_ABI bool hasNoNaNs() const LLVM_READONLY
Determine whether the no-NaNs flag is set.
LLVM_ABI bool hasNoUnsignedWrap() const LLVM_READONLY
Determine whether the no unsigned wrap flag is set.
LLVM_ABI bool hasNoSignedWrap() const LLVM_READONLY
Determine whether the no signed wrap flag is set.
bool isBinaryOp() const
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI bool isExact() const LLVM_READONLY
Determine whether the exact flag is set.
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
LLVM_ABI bool comesBefore(const Instruction *Other) const
Given an instruction Other in the same basic block as this instruction, return true if this instructi...
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
bool isUnaryOp() const
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
A wrapper class for inspecting calls to intrinsic functions.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
An instruction for reading from memory.
Value * getPointerOperand()
Align getAlign() const
Return the alignment of the access that is being performed.
bool isLoopHeader(const BlockT *BB) const
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
Metadata node.
Definition Metadata.h:1080
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1444
This is a utility class that provides an abstraction for the common functionality between Instruction...
Definition Operator.h:33
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
Definition Operator.h:43
Utility class for integer operators which may exhibit overflow - Add, Sub, Mul, and Shl.
Definition Operator.h:78
iterator_range< const_block_iterator > blocks() const
Value * getIncomingValueForBlock(const BasicBlock *BB) const
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A udiv, sdiv, lshr, or ashr instruction, which can be marked as "exact", indicating that no bits are ...
Definition Operator.h:154
bool isExact() const
Test whether this division is known to be exact, with zero remainder.
Definition Operator.h:173
This class represents the LLVM 'select' instruction.
const Value * getFalseValue() const
const Value * getCondition() const
const Value * getTrueValue() const
This instruction constructs a fixed permutation of two input vectors.
VectorType * getType() const
Overload to return most specific vector type.
static LLVM_ABI void getShuffleMask(const Constant *Mask, SmallVectorImpl< int > &Result)
Convert the input shuffle mask operand to a vector of integers.
size_type size() const
Definition SmallPtrSet.h:99
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void reserve(size_type N)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:591
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition DataLayout.h: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
LLVM_ABI unsigned getIntegerBitWidth() const
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:273
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
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:197
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition Type.h:311
bool isHalfTy() const
Return true if this is 'half', a 16-bit IEEE fp type.
Definition Type.h:142
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 isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
Definition Type.h:156
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
bool isIEEELikeFPTy() const
Return true if this is a well-behaved IEEE-like type, which has a IEEE compatible layout,...
Definition Type.h:170
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:267
Value * getOperand(unsigned i) const
Definition User.h:207
unsigned getNumOperands() const
Definition User.h:229
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
const Value * stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, APInt &Offset) const
This is a wrapper around stripAndAccumulateConstantOffsets with the in-bounds requirement set to fals...
Definition Value.h:761
iterator_range< user_iterator > users()
Definition Value.h:427
LLVM_ABI const Value * stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, bool AllowInvariantGroup=false, function_ref< bool(Value &Value, APInt &Offset)> ExternalAnalysis=nullptr, bool LookThroughIntToPtr=false) const
Accumulate the constant offset this value has compared to a base pointer.
const KnownBits & getKnownBits(const SimplifyQuery &Q) const
Definition WithCache.h:59
PointerType getValue() const
Definition WithCache.h:57
Represents an op.with.overflow intrinsic.
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:168
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:165
An efficient, type-erasing, non-owning reference to a callable.
TypeSize getSequentialElementStride(const DataLayout &DL) const
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:123
A range adaptor for a pair of iterators.
CallInst * Call
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
#define UINT64_MAX
Definition DataTypes.h:77
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI APInt ScaleBitMask(const APInt &A, unsigned NewBitWidth, bool MatchAllBits=false)
Splat/Merge neighboring bits to widen/narrow the bitmask represented by.
Definition APInt.cpp:3020
const APInt & umax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be unsigned.
Definition APInt.h:2278
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
SpecificConstantMatch m_ZeroInt()
Convenience matchers for specific integer values.
BinaryOp_match< SpecificConstantMatch, SrcTy, TargetOpcode::G_SUB > m_Neg(const SrcTy &&Src)
Matches a register negated by a G_SUB.
BinaryOp_match< SrcTy, SpecificConstantMatch, TargetOpcode::G_XOR, true > m_Not(const SrcTy &&Src)
Matches a register not-ed by a G_XOR.
OneUse_match< SubPat > m_OneUse(const SubPat &SP)
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
cst_pred_ty< is_lowbit_mask > m_LowBitMask()
Match an integer or vector with only the low bit(s) set.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
PtrToIntSameSize_match< OpTy > m_PtrToIntSameSize(const DataLayout &DL, const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, FCmpInst > m_FCmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
cst_pred_ty< is_sign_mask > m_SignMask()
Match an integer or vector with only the sign bit(s) set.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWAdd(const LHS &L, const RHS &R)
auto m_PtrToIntOrAddr(const OpTy &Op)
Matches PtrToInt or PtrToAddr.
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
BinaryOp_match< LHS, RHS, Instruction::URem > m_URem(const LHS &L, const RHS &R)
auto m_LogicalOp()
Matches either L && R or L || R where L and R are arbitrary values.
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
ap_match< APInt > m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
BinaryOp_match< LHS, RHS, Instruction::And, true > m_c_And(const LHS &L, const RHS &R)
Matches an And with LHS and RHS in either order.
cst_pred_ty< is_power2_or_zero > m_Power2OrZero()
Match an integer or vector of 0 or power-of-2 values.
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWSub(const LHS &L, const RHS &R)
bool match(Val *V, const Pattern &P)
BinOpPred_match< LHS, RHS, is_idiv_op > m_IDiv(const LHS &L, const RHS &R)
Matches integer division operations.
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
ap_match< APFloat > m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
CmpClass_match< LHS, RHS, ICmpInst, true > m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
auto match_fn(const Pattern &P)
A match functor that can be used as a UnaryPredicate in functional algorithms like all_of.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true > m_c_NUWAdd(const LHS &L, const RHS &R)
cst_pred_ty< is_nonnegative > m_NonNegative()
Match an integer or vector of non-negative values.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
IntrinsicID_match m_Intrinsic()
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
IntrinsicID_match m_VScale()
Matches a call to llvm.vscale().
match_combine_or< MaxMin_match< FCmpInst, LHS, RHS, ofmin_pred_ty >, MaxMin_match< FCmpInst, LHS, RHS, ufmin_pred_ty > > m_OrdOrUnordFMin(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point minimum function.
ExtractValue_match< Ind, Val_t > m_ExtractValue(const Val_t &V)
Match a single index ExtractValue instruction.
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > m_SMin(const LHS &L, const RHS &R)
bind_ty< WithOverflowInst > m_WithOverflowInst(WithOverflowInst *&I)
Match a with overflow intrinsic, capturing it if we match.
BinaryOp_match< LHS, RHS, Instruction::Xor, true > m_c_Xor(const LHS &L, const RHS &R)
Matches an Xor with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty, true > m_c_SMin(const LHS &L, const RHS &R)
Matches an SMin with LHS and RHS in either order.
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty, true > m_c_UMax(const LHS &L, const RHS &R)
Matches a UMax with LHS and RHS in either order.
SpecificCmpClass_match< LHS, RHS, ICmpInst > m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
BinaryOp_match< LHS, RHS, Instruction::UDiv > m_UDiv(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty > m_UMax(const LHS &L, const RHS &R)
brc_match< Cond_t, bind_ty< BasicBlock >, bind_ty< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
match_immconstant_ty m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
NoWrapTrunc_match< OpTy, TruncInst::NoUnsignedWrap > m_NUWTrunc(const OpTy &Op)
Matches trunc nuw.
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty, true > m_c_UMin(const LHS &L, const RHS &R)
Matches a UMin with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Add, true > m_c_Add(const LHS &L, const RHS &R)
Matches a Add with LHS and RHS in either order.
match_combine_or< BinaryOp_match< LHS, RHS, Instruction::Add >, DisjointOr_match< LHS, RHS > > m_AddLike(const LHS &L, const RHS &R)
Match either "add" or "or disjoint".
match_combine_or< MaxMin_match< FCmpInst, LHS, RHS, ofmax_pred_ty >, MaxMin_match< FCmpInst, LHS, RHS, ufmax_pred_ty > > m_OrdOrUnordFMax(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point maximum function.
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty, true > m_c_SMax(const LHS &L, const RHS &R)
Matches an SMax with LHS and RHS in either order.
CastOperator_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
match_combine_or< match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty, true >, MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty, true > >, match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty, true >, MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty, true > > > m_c_MaxOrMin(const LHS &L, const RHS &R)
cstfp_pred_ty< custom_checkfn< APFloat > > m_CheckedFp(function_ref< bool(const APFloat &)> CheckFn)
Match a float or vector where CheckFn(ele) for each element is true.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWSub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty > m_SMax(const LHS &L, const RHS &R)
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap >, DisjointOr_match< LHS, RHS > > m_NSWAddLike(const LHS &L, const RHS &R)
Match either "add nsw" or "or disjoint".
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap > m_NSWAdd(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, SExtInst > > m_ZExtOrSExt(const OpTy &Op)
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match 'fneg X' as 'fsub -0.0, X'.
BinOpPred_match< LHS, RHS, is_shift_op > m_Shift(const LHS &L, const RHS &R)
Matches shift operations.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
BinOpPred_match< LHS, RHS, is_irem_op > m_IRem(const LHS &L, const RHS &R)
Matches integer remainder operations.
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
class_match< BasicBlock > m_BasicBlock()
Match an arbitrary basic block value and ignore it.
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
cst_pred_ty< is_nonpositive > m_NonPositive()
Match an integer or vector of non-positive values.
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap >, DisjointOr_match< LHS, RHS > > m_NUWAddLike(const LHS &L, const RHS &R)
Match either "add nuw" or "or disjoint".
ElementWiseBitCast_match< OpTy > m_ElementWiseBitCast(const OpTy &Op)
m_Intrinsic_Ty< Opnd0 >::Ty m_FAbs(const Opnd0 &Op0)
BinaryOp_match< LHS, RHS, Instruction::Mul, true > m_c_Mul(const LHS &L, const RHS &R)
Matches a Mul with LHS and RHS in either order.
CastOperator_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > m_UMin(const LHS &L, const RHS &R)
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
static unsigned decodeVSEW(unsigned VSEW)
LLVM_ABI unsigned getSEWLMULRatio(unsigned SEW, VLMUL VLMul)
static constexpr unsigned RVVBitsPerBlock
initializer< Ty > init(const Ty &Val)
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:668
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
LLVM_ABI bool haveNoCommonBitsSet(const WithCache< const Value * > &LHSCache, const WithCache< const Value * > &RHSCache, const SimplifyQuery &SQ)
Return true if LHS and RHS have no common bits set.
LLVM_ABI bool mustExecuteUBIfPoisonOnPathTo(Instruction *Root, Instruction *OnPathTo, DominatorTree *DT)
Return true if undefined behavior would provable be executed on the path to OnPathTo if Root produced...
LLVM_ABI Intrinsic::ID getInverseMinMaxIntrinsic(Intrinsic::ID MinMaxID)
LLVM_ABI bool willNotFreeBetween(const Instruction *Assume, const Instruction *CtxI)
Returns true, if no instruction between Assume and CtxI may free memory and the function is marked as...
@ Offset
Definition DWP.cpp:532
@ Length
Definition DWP.cpp:532
@ NeverOverflows
Never overflows.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
LLVM_ABI KnownFPClass computeKnownFPClass(const Value *V, const APInt &DemandedElts, FPClassTest InterestedClasses, const SimplifyQuery &SQ, unsigned Depth=0)
Determine which floating-point classes are valid for V, and return them in KnownFPClass bit sets.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1739
MaybeAlign getAlign(const CallInst &I, unsigned Index)
LLVM_ABI bool isValidAssumeForContext(const Instruction *I, const Instruction *CxtI, const DominatorTree *DT=nullptr, bool AllowEphemerals=false)
Return true if it is valid to use the assumptions provided by an assume intrinsic,...
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1669
LLVM_ABI bool canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
LLVM_ABI bool mustTriggerUB(const Instruction *I, const SmallPtrSetImpl< const Value * > &KnownPoison)
Return true if the given instruction must trigger undefined behavior when I is executed with any oper...
LLVM_ABI bool isKnownNeverInfinity(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point scalar value is not an infinity or if the floating-point vector val...
LLVM_ABI void computeKnownBitsFromContext(const Value *V, KnownBits &Known, const SimplifyQuery &Q, unsigned Depth=0)
Merge bits known from context-dependent facts into Known.
LLVM_ABI bool isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI)
LLVM_ABI bool isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS, bool &TrueIfSigned)
Given an exploded icmp instruction, return true if the comparison only checks the sign bit.
LLVM_ABI const Value * getArgumentAliasingToReturnedPointer(const CallBase *Call, bool MustPreserveNullness)
This function returns call pointer argument that is considered the same by aliasing rules.
LLVM_ABI bool isAssumeLikeIntrinsic(const Instruction *I)
Return true if it is an intrinsic that cannot be speculated but also cannot trap.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2554
LLVM_ABI AllocaInst * findAllocaForValue(Value *V, bool OffsetZero=false)
Returns unique alloca where the value comes from, or nullptr.
LLVM_ABI APInt getMinMaxLimit(SelectPatternFlavor SPF, unsigned BitWidth)
Return the minimum or maximum constant value for the specified integer min/max flavor and type.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI bool isOnlyUsedInZeroComparison(const Instruction *CxtI)
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
LLVM_ABI bool getConstantStringInfo(const Value *V, StringRef &Str, bool TrimAtNul=true)
This function computes the length of a null-terminated C string pointed to by V.
LLVM_ABI bool isDereferenceableAndAlignedPointer(const Value *V, Type *Ty, Align Alignment, const DataLayout &DL, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Returns true if V is always a dereferenceable pointer with alignment greater or equal than requested.
Definition Loads.cpp:229
LLVM_ABI bool onlyUsedByLifetimeMarkersOrDroppableInsts(const Value *V)
Return true if the only users of this pointer are lifetime markers or droppable instructions.
LLVM_ABI Constant * ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset)
LLVM_ABI Value * stripNullTest(Value *V)
Returns the inner value X if the expression has the form f(X) where f(X) == 0 if and only if X == 0,...
LLVM_ABI bool getUnderlyingObjectsForCodeGen(const Value *V, SmallVectorImpl< Value * > &Objects)
This is a wrapper around getUnderlyingObjects and adds support for basic ptrtoint+arithmetic+inttoptr...
LLVM_ABI std::pair< Intrinsic::ID, bool > canConvertToMinOrMaxIntrinsic(ArrayRef< Value * > VL)
Check if the values in VL are select instructions that can be converted to a min or max (vector) intr...
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ABI bool getConstantDataArrayInfo(const Value *V, ConstantDataArraySlice &Slice, unsigned ElementSize, uint64_t Offset=0)
Returns true if the value V is a pointer into a ConstantDataArray.
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition bit.h: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:2208
LLVM_ABI bool mustSuppressSpeculation(const LoadInst &LI)
Return true if speculation of the given load must be suppressed to avoid ordering or interfering with...
Definition Loads.cpp:431
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition MathExtras.h:284
gep_type_iterator gep_type_end(const User *GEP)
int ilogb(const APFloat &Arg)
Returns the exponent of the internal representation of the APFloat.
Definition APFloat.h:1601
LLVM_ABI bool isSafeToSpeculativelyExecute(const Instruction *I, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true, bool IgnoreUBImplyingAttrs=true)
Return true if the instruction does not have any effects besides calculating the result and does not ...
LLVM_ABI Value * getSplatValue(const Value *V)
Get splat value if the input is a splat vector or return nullptr.
LLVM_ABI CmpInst::Predicate getMinMaxPred(SelectPatternFlavor SPF, bool Ordered=false)
Return the canonical comparison predicate for the specified minimum/maximum flavor.
bool isa_and_nonnull(const Y &Val)
Definition Casting.h:676
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:337
LLVM_ABI bool canIgnoreSignBitOfZero(const Use &U)
Return true if the sign bit of the FP value can be ignored by the user when the value is zero.
LLVM_ABI bool isGuaranteedNotToBeUndef(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be undef, but may be poison.
LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
std::tuple< Value *, FPClassTest, FPClassTest > fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS, FPClassTest RHSClass, bool LookThroughSrc=true)
LLVM_ABI ConstantRange computeConstantRange(const Value *V, bool ForSigned, bool UseInstrInfo=true, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Determine the possible constant range of an integer or vector of integer value.
const Value * getPointerOperand(const Value *V)
A helper function that returns the pointer operand of a load, store or GEP instruction.
LLVM_ABI bool MaskedValueIsZero(const Value *V, const APInt &Mask, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if 'V & Mask' is known to be zero.
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:202
LLVM_ABI bool isOverflowIntrinsicNoWrap(const WithOverflowInst *WO, const DominatorTree &DT)
Returns true if the arithmetic part of the WO 's result is used only along the paths control dependen...
LLVM_ABI RetainedKnowledge getKnowledgeFromBundle(AssumeInst &Assume, const CallBase::BundleOpInfo &BOI)
This extracts the Knowledge from an element of an operand bundle.
LLVM_ABI bool matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO, Value *&Start, Value *&Step)
Attempt to match a simple first order recurrence cycle of the form: iv = phi Ty [Start,...
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1746
LLVM_ABI OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ, bool IsNSW=false)
LLVM_ABI bool getShuffleDemandedElts(int SrcWidth, ArrayRef< int > Mask, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS, bool AllowUndefElts=false)
Transform a shuffle mask's output demanded element mask into demanded element masks for the 2 operand...
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:331
bool isGuard(const User *U)
Returns true iff U has semantics of a guard expressed in a form of call of llvm.experimental....
LLVM_ABI SelectPatternFlavor getInverseMinMaxFlavor(SelectPatternFlavor SPF)
Return the inverse minimum/maximum flavor of the specified flavor.
constexpr unsigned MaxAnalysisRecursionDepth
LLVM_ABI void adjustKnownBitsForSelectArm(KnownBits &Known, Value *Cond, Value *Arm, bool Invert, const SimplifyQuery &Q, unsigned Depth=0)
Adjust Known for the given select Arm to include information from the select Cond.
LLVM_ABI bool isKnownNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be negative (i.e.
LLVM_ABI OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
SelectPatternFlavor
Specific patterns of select instructions we can match.
@ SPF_ABS
Floating point maxnum.
@ SPF_NABS
Absolute value.
@ SPF_FMAXNUM
Floating point minnum.
@ SPF_UMIN
Signed minimum.
@ SPF_UMAX
Signed maximum.
@ SPF_SMAX
Unsigned minimum.
@ SPF_UNKNOWN
@ SPF_FMINNUM
Unsigned maximum.
LLVM_ABI bool isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(const CallBase *Call, bool MustPreserveNullness)
{launder,strip}.invariant.group returns pointer that aliases its argument, and it only captures point...
LLVM_ABI bool impliesPoison(const Value *ValAssumedPoison, const Value *V)
Return true if V is poison given that ValAssumedPoison is already poison.
LLVM_ABI void getHorizDemandedEltsForFirstOperand(unsigned VectorBitWidth, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS)
Compute the demanded elements mask of horizontal binary operations.
LLVM_ABI SelectPatternResult getSelectPattern(CmpInst::Predicate Pred, SelectPatternNaNBehavior NaNBehavior=SPNB_NA, bool Ordered=false)
Determine the pattern for predicate X Pred Y ? X : Y.
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI bool programUndefinedIfPoison(const Instruction *Inst)
LLVM_ABI SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
LLVM_ABI bool matchSimpleBinaryIntrinsicRecurrence(const IntrinsicInst *I, PHINode *&P, Value *&Init, Value *&OtherOp)
Attempt to match a simple value-accumulating recurrence of the form: llvm.intrinsic....
LLVM_ABI bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
LLVM_ABI bool cannotBeNegativeZero(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if we can prove that the specified FP value is never equal to -0.0.
LLVM_ABI bool programUndefinedIfUndefOrPoison(const Instruction *Inst)
Return true if this function can prove that if Inst is executed and yields a poison value or undef bi...
LLVM_ABI void adjustKnownFPClassForSelectArm(KnownFPClass &Known, Value *Cond, Value *Arm, bool Invert, const SimplifyQuery &Q, unsigned Depth=0)
Adjust Known for the given select Arm to include information from the select Cond.
generic_gep_type_iterator<> gep_type_iterator
LLVM_ABI bool collectPossibleValues(const Value *V, SmallPtrSetImpl< const Constant * > &Constants, unsigned MaxCount, bool AllowUndefOrPoison=true)
Enumerates all possible immediate values of V and inserts them into the set Constants.
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
LLVM_ABI uint64_t GetStringLength(const Value *V, unsigned CharSize=8)
If we can compute the length of the string pointed to by the specified pointer, return 'len+1'.
LLVM_ABI OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
LLVM_ABI ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
LLVM_ABI Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
LLVM_ABI bool canCreateUndefOrPoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
canCreateUndefOrPoison returns true if Op can create undef or poison from non-undef & non-poison oper...
LLVM_ABI bool matchSimpleTernaryIntrinsicRecurrence(const IntrinsicInst *I, PHINode *&P, Value *&Init, Value *&OtherOp0, Value *&OtherOp1)
Attempt to match a simple value-accumulating recurrence of the form: llvm.intrinsic....
LLVM_ABI EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
LLVM_ABI bool isKnownInversion(const Value *X, const Value *Y)
Return true iff:
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI bool intrinsicPropagatesPoison(Intrinsic::ID IID)
Return whether this intrinsic propagates poison for all operands.
LLVM_ABI bool isNotCrossLaneOperation(const Instruction *I)
Return true if the instruction doesn't potentially cross vector lanes.
LLVM_ABI bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth=0)
Return true if the given value is known to be non-zero when defined.
constexpr int PoisonMaskElem
LLVM_ABI RetainedKnowledge getKnowledgeValidInContext(const Value *V, ArrayRef< Attribute::AttrKind > AttrKinds, AssumptionCache &AC, const Instruction *CtxI, const DominatorTree *DT=nullptr)
Return a valid Knowledge associated to the Value V if its Attribute kind is in AttrKinds and the know...
LLVM_ABI bool isSafeToSpeculativelyExecuteWithOpcode(unsigned Opcode, const Instruction *Inst, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true, bool IgnoreUBImplyingAttrs=true)
This returns the same result as isSafeToSpeculativelyExecute if Opcode is the actual opcode of Inst.
LLVM_ABI bool onlyUsedByLifetimeMarkers(const Value *V)
Return true if the only users of this pointer are lifetime markers.
LLVM_ABI Intrinsic::ID getIntrinsicForCallSite(const CallBase &CB, const TargetLibraryInfo *TLI)
Map a call instruction to an intrinsic ID.
@ Other
Any other memory.
Definition ModRef.h:68
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
LLVM_ABI const Value * getUnderlyingObjectAggressive(const Value *V)
Like getUnderlyingObject(), but will try harder to find a single underlying object.
LLVM_ABI Intrinsic::ID getMinMaxIntrinsic(SelectPatternFlavor SPF)
Convert given SPF to equivalent min/max intrinsic.
LLVM_ABI SelectPatternResult matchDecomposedSelectPattern(CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, FastMathFlags FMF=FastMathFlags(), Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Determine the pattern that a select with the given compare as its predicate and given values as its t...
LLVM_ABI OverflowResult computeOverflowForSignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
LLVM_ABI bool propagatesPoison(const Use &PoisonOp)
Return true if PoisonOp's user yields poison or raises UB if its operand PoisonOp is poison.
@ Add
Sum of integers.
LLVM_ABI ConstantRange computeConstantRangeIncludingKnownBits(const WithCache< const Value * > &V, bool ForSigned, const SimplifyQuery &SQ)
Combine constant ranges from computeConstantRange() and computeKnownBits().
SelectPatternNaNBehavior
Behavior when a floating point min/max is given one NaN and one non-NaN as input.
@ SPNB_RETURNS_NAN
NaN behavior not applicable.
@ SPNB_RETURNS_OTHER
Given one NaN input, returns the NaN.
@ SPNB_RETURNS_ANY
Given one NaN input, returns the non-NaN.
LLVM_ABI bool isKnownNonEqual(const Value *V1, const Value *V2, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the given values are known to be non-equal when defined.
DWARFExpression::Operation Op
LLVM_ABI bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Return the number of times the sign bit of the register is replicated into the other bits.
constexpr unsigned BitWidth
LLVM_ABI KnownBits analyzeKnownBitsFromAndXorOr(const Operator *I, const KnownBits &KnownLHS, const KnownBits &KnownRHS, const SimplifyQuery &SQ, unsigned Depth=0)
Using KnownBits LHS/RHS produce the known bits for logic op (and/xor/or).
LLVM_ABI OverflowResult computeOverflowForUnsignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
LLVM_ABI bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I)
Return true if this function can prove that the instruction I will always transfer execution to one o...
LLVM_ABI bool isKnownNeverInfOrNaN(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point value can never contain a NaN or infinity.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI bool isKnownNeverNaN(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point scalar value is not a NaN or if the floating-point vector value has...
gep_type_iterator gep_type_begin(const User *GEP)
LLVM_ABI Value * isBytewiseValue(Value *V, const DataLayout &DL)
If the specified value can be set by repeating the same byte in memory, return the i8 value that it i...
LLVM_ABI std::optional< std::pair< CmpPredicate, Constant * > > getFlippedStrictnessPredicateAndConstant(CmpPredicate Pred, Constant *C)
Convert an integer comparison with a constant RHS into an equivalent form with the strictness flipped...
LLVM_ABI unsigned ComputeMaxSignificantBits(const Value *Op, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Get the upper bound on bit size for this Value Op as a signed integer.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1947
LLVM_ABI bool isKnownIntegral(const Value *V, const SimplifyQuery &SQ, FastMathFlags FMF)
Return true if the floating-point value V is known to be an integer value.
LLVM_ABI OverflowResult computeOverflowForUnsignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:197
LLVM_ABI bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Return true if the given value is known to have exactly one bit set when defined.
LLVM_ABI std::optional< bool > isImpliedByDomCondition(const Value *Cond, const Instruction *ContextI, const DataLayout &DL)
Return the boolean condition value in the context of the given instruction if it is known based on do...
LLVM_ABI bool isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be poison, but may be undef.
LLVM_ABI void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, KnownBits &Known)
Compute known bits from the range metadata.
LLVM_ABI Value * FindInsertedValue(Value *V, ArrayRef< unsigned > idx_range, std::optional< BasicBlock::iterator > InsertBefore=std::nullopt)
Given an aggregate and an sequence of indices, see if the scalar value indexed is already around as a...
LLVM_ABI bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW=false, bool AllowPoison=true)
Return true if the two given values are negation.
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
LLVM_ABI bool isKnownPositive(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be positive (i.e.
LLVM_ABI Constant * ConstantFoldIntegerCast(Constant *C, Type *DestTy, bool IsSigned, const DataLayout &DL)
Constant fold a zext, sext or trunc, depending on IsSigned and whether the DestTy is wider or narrowe...
LLVM_ABI bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the give value is known to be non-negative.
LLVM_ABI bool cannotBeOrderedLessThanZero(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if we can prove that the specified FP value is either NaN or never less than -0....
LLVM_ABI void getUnderlyingObjects(const Value *V, SmallVectorImpl< const Value * > &Objects, const LoopInfo *LI=nullptr, unsigned MaxLookup=MaxLookupSearchDepth)
This method is similar to getUnderlyingObject except that it can look through phi and select instruct...
LLVM_ABI bool mayHaveNonDefUseDependency(const Instruction &I)
Returns true if the result or effects of the given instructions I depend values not reachable through...
LLVM_ABI bool isTriviallyVectorizable(Intrinsic::ID ID)
Identify if the intrinsic is trivially vectorizable.
LLVM_ABI bool isIdentifiedObject(const Value *V)
Return true if this pointer refers to a distinct and identifiable object.
LLVM_ABI std::optional< bool > isImpliedCondition(const Value *LHS, const Value *RHS, const DataLayout &DL, bool LHSIsTrue=true, unsigned Depth=0)
Return true if RHS is known to be implied true by LHS.
LLVM_ABI std::optional< bool > computeKnownFPSignBit(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return false if we can prove that the specified FP value's sign bit is 0.
LLVM_ABI bool canIgnoreSignBitOfNaN(const Use &U)
Return true if the sign bit of the FP value can be ignored by the user when the value is NaN.
LLVM_ABI void findValuesAffectedByCondition(Value *Cond, bool IsAssume, function_ref< void(Value *)> InsertAffected)
Call InsertAffected on all Values whose known bits / value may be affected by the condition Cond.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:872
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
SmallPtrSet< Value *, 4 > AffectedValues
Represents offset+length into a ConstantDataArray.
const ConstantDataArray * Array
ConstantDataArray pointer.
Represent subnormal handling kind for floating point instruction inputs and outputs.
static constexpr DenormalMode getDynamic()
InstrInfoQuery provides an interface to query additional information for instructions like metadata o...
bool isExact(const BinaryOperator *Op) const
MDNode * getMetadata(const Instruction *I, unsigned KindID) const
bool hasNoSignedZeros(const InstT *Op) const
bool hasNoSignedWrap(const InstT *Op) const
bool hasNoUnsignedWrap(const InstT *Op) const
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition KnownBits.h:317
static LLVM_ABI KnownBits sadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)
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:192
static LLVM_ABI KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition KnownBits.h:271
static LLVM_ABI KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition KnownBits.h:108
bool isZero() const
Returns true if value is all zero.
Definition KnownBits.h:80
LLVM_ABI KnownBits blsi() const
Compute known bits for X & -X, which has only the lowest bit set of X set.
void makeNonNegative()
Make this value non-negative.
Definition KnownBits.h:127
static LLVM_ABI KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
unsigned countMinLeadingOnes() const
Returns the minimum number of leading one bits.
Definition KnownBits.h:267
LLVM_ABI KnownBits reduceAdd(unsigned NumElts) const
Compute known bits for horizontal add for a vector with NumElts elements, where each element has the ...
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition KnownBits.h:258
static LLVM_ABI KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
static LLVM_ABI KnownBits ssub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.ssub.sat(LHS, RHS)
static LLVM_ABI KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
bool isUnknown() const
Returns true if we don't know any bits.
Definition KnownBits.h:66
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition KnownBits.h:290
LLVM_ABI KnownBits blsmsk() const
Compute known bits for X ^ (X - 1), which has all bits up to and including the lowest set bit of X se...
void makeNegative()
Make this value negative.
Definition KnownBits.h:122
void setAllConflict()
Make all bits known to be both zero and one.
Definition KnownBits.h:99
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition KnownBits.h:167
KnownBits byteSwap() const
Definition KnownBits.h:538
bool hasConflict() const
Returns true if there is conflicting information.
Definition KnownBits.h:51
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition KnownBits.h:305
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition KnownBits.h:86
KnownBits reverseBits() const
Definition KnownBits.h:542
unsigned getBitWidth() const
Get the bit width of this value.
Definition KnownBits.h:44
static LLVM_ABI KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition KnownBits.h:178
bool isConstant() const
Returns true if we know the value of all bits.
Definition KnownBits.h:54
void resetAll()
Resets the known state of all bits.
Definition KnownBits.h:74
KnownBits unionWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for either this or RHS or both.
Definition KnownBits.h:337
static LLVM_ABI KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition KnownBits.h:111
bool isEven() const
Return if the value is known even (the low bit is 0).
Definition KnownBits.h:164
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition KnownBits.h:241
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition KnownBits.h:327
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition KnownBits.h:186
unsigned countMinTrailingOnes() const
Returns the minimum number of trailing one bits.
Definition KnownBits.h:261
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:363
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition KnownBits.h:202
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition KnownBits.h:264
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:148
static LLVM_ABI KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
static LLVM_ABI KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
static LLVM_ABI KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
static LLVM_ABI KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
APInt getMinValue() const
Return the minimal unsigned value possible given these KnownBits.
Definition KnownBits.h:132
static LLVM_ABI KnownBits computeForAddSub(bool Add, bool NSW, bool NUW, const KnownBits &LHS, const KnownBits &RHS)
Compute known bits resulting from adding LHS and RHS.
Definition KnownBits.cpp:61
static LLVM_ABI KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition KnownBits.h:342
bool isNegative() const
Returns true if this value is known to be negative.
Definition KnownBits.h:105
static KnownBits sub(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from subtraction of LHS and RHS.
Definition KnownBits.h:369
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition KnownBits.h:296
void setAllOnes()
Make all bits known to be one and discard any previous information.
Definition KnownBits.h:92
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition KnownBits.h:235
static LLVM_ABI KnownBits uadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.uadd.sat(LHS, RHS)
static LLVM_ABI KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
KnownBits anyext(unsigned BitWidth) const
Return known bits for an "any" extension of the value we're tracking, where we don't know anything ab...
Definition KnownBits.h:173
static LLVM_ABI KnownBits clmul(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for clmul(LHS, RHS).
LLVM_ABI KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
static LLVM_ABI std::optional< bool > sgt(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SGT result.
static LLVM_ABI std::optional< bool > uge(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_UGE result.
static LLVM_ABI KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
static LLVM_ABI KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
KnownBits sextOrTrunc(unsigned BitWidth) const
Return known bits for a sign extension or truncation of the value we're tracking.
Definition KnownBits.h:212
bool isKnownNeverInfOrNaN() const
Return true if it's known this can never be an infinity or nan.
FPClassTest KnownFPClasses
Floating-point classes the value could be one of.
bool isKnownNeverInfinity() const
Return true if it's known this can never be an infinity.
bool cannotBeOrderedGreaterThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never greater tha...
static LLVM_ABI KnownFPClass sin(const KnownFPClass &Src)
Report known values for sin.
static LLVM_ABI KnownFPClass fdiv_self(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fdiv x, x.
static constexpr FPClassTest OrderedGreaterThanZeroMask
static constexpr FPClassTest OrderedLessThanZeroMask
void knownNot(FPClassTest RuleOut)
static LLVM_ABI KnownFPClass fmul(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fmul.
static LLVM_ABI KnownFPClass fadd_self(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fadd x, x.
void copysign(const KnownFPClass &Sign)
static KnownFPClass square(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
static LLVM_ABI KnownFPClass fsub(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fsub.
KnownFPClass unionWith(const KnownFPClass &RHS) const
static LLVM_ABI KnownFPClass canonicalize(const KnownFPClass &Src, DenormalMode DenormMode=DenormalMode::getDynamic())
Apply the canonicalize intrinsic to this value.
LLVM_ABI bool isKnownNeverLogicalZero(DenormalMode Mode) const
Return true if it's known this can never be interpreted as a zero.
static LLVM_ABI KnownFPClass log(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for log/log2/log10.
static LLVM_ABI KnownFPClass fdiv(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fdiv.
static LLVM_ABI KnownFPClass roundToIntegral(const KnownFPClass &Src, bool IsTrunc, bool IsMultiUnitFPType)
Propagate known class for rounding intrinsics (trunc, floor, ceil, rint, nearbyint,...
static LLVM_ABI KnownFPClass cos(const KnownFPClass &Src)
Report known values for cos.
static LLVM_ABI KnownFPClass ldexp(const KnownFPClass &Src, const KnownBits &N, const fltSemantics &Flt, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for ldexp.
static LLVM_ABI KnownFPClass minMaxLike(const KnownFPClass &LHS, const KnownFPClass &RHS, MinMaxKind Kind, DenormalMode DenormMode=DenormalMode::getDynamic())
bool isUnknown() const
KnownFPClass intersectWith(const KnownFPClass &RHS) const
static LLVM_ABI KnownFPClass exp(const KnownFPClass &Src)
Report known values for exp, exp2 and exp10.
static LLVM_ABI KnownFPClass frexp_mant(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for mantissa component of frexp.
std::optional< bool > SignBit
std::nullopt if the sign bit is unknown, true if the sign bit is definitely set or false if the sign ...
bool isKnownNeverNaN() const
Return true if it's known this can never be a nan.
bool isKnownNever(FPClassTest Mask) const
Return true if it's known this can never be one of the mask entries.
static LLVM_ABI KnownFPClass fpext(const KnownFPClass &KnownSrc, const fltSemantics &DstTy, const fltSemantics &SrcTy)
Propagate known class for fpext.
bool isKnownNeverNegZero() const
Return true if it's known this can never be a negative zero.
static LLVM_ABI KnownFPClass fma(const KnownFPClass &LHS, const KnownFPClass &RHS, const KnownFPClass &Addend, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fma.
void propagateNaN(const KnownFPClass &Src, bool PreserveSign=false)
static LLVM_ABI KnownFPClass fptrunc(const KnownFPClass &KnownSrc)
Propagate known class for fptrunc.
bool cannotBeOrderedLessThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never less than -...
void signBitMustBeOne()
Assume the sign bit is one.
void signBitMustBeZero()
Assume the sign bit is zero.
static LLVM_ABI KnownFPClass sqrt(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for sqrt.
LLVM_ABI bool isKnownNeverLogicalPosZero(DenormalMode Mode) const
Return true if it's known this can never be interpreted as a positive zero.
bool isKnownNeverPosInfinity() const
Return true if it's known this can never be +infinity.
static LLVM_ABI KnownFPClass fadd(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fadd.
LLVM_ABI bool isKnownNeverLogicalNegZero(DenormalMode Mode) const
Return true if it's known this can never be interpreted as a negative zero.
static LLVM_ABI KnownFPClass fma_square(const KnownFPClass &Squared, const KnownFPClass &Addend, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fma squared, squared, addend.
static LLVM_ABI KnownFPClass frem_self(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for frem.
static LLVM_ABI KnownFPClass powi(const KnownFPClass &Src, const KnownBits &N)
Propagate known class for powi.
Represent one information held inside an operand bundle of an llvm.assume.
SelectPatternFlavor Flavor
static bool isMinOrMax(SelectPatternFlavor SPF)
When implementing this min/max pattern as fcmp; select, does the fcmp have to be ordered?
const DataLayout & DL
SimplifyQuery getWithoutCondContext() const
const Instruction * CxtI
const DominatorTree * DT
SimplifyQuery getWithInstruction(const Instruction *I) const
AssumptionCache * AC
const DomConditionCache * DC
const InstrInfoQuery IIQ
const CondContext * CC