LLVM 23.0.0git
ValueTracking.cpp
Go to the documentation of this file.
1//===- ValueTracking.cpp - Walk computations to compute properties --------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains routines that help analyze properties that chains of
10// computations have.
11//
12//===----------------------------------------------------------------------===//
13
15#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/ScopeExit.h"
23#include "llvm/ADT/StringRef.h"
33#include "llvm/Analysis/Loads.h"
38#include "llvm/IR/Argument.h"
39#include "llvm/IR/Attributes.h"
40#include "llvm/IR/BasicBlock.h"
41#include "llvm/IR/Constant.h"
44#include "llvm/IR/Constants.h"
47#include "llvm/IR/Dominators.h"
49#include "llvm/IR/Function.h"
51#include "llvm/IR/GlobalAlias.h"
52#include "llvm/IR/GlobalValue.h"
54#include "llvm/IR/InstrTypes.h"
55#include "llvm/IR/Instruction.h"
58#include "llvm/IR/Intrinsics.h"
59#include "llvm/IR/IntrinsicsAArch64.h"
60#include "llvm/IR/IntrinsicsAMDGPU.h"
61#include "llvm/IR/IntrinsicsRISCV.h"
62#include "llvm/IR/IntrinsicsX86.h"
63#include "llvm/IR/LLVMContext.h"
64#include "llvm/IR/Metadata.h"
65#include "llvm/IR/Module.h"
66#include "llvm/IR/Operator.h"
68#include "llvm/IR/Type.h"
69#include "llvm/IR/User.h"
70#include "llvm/IR/Value.h"
79#include <algorithm>
80#include <cassert>
81#include <cstdint>
82#include <optional>
83#include <utility>
84
85using namespace llvm;
86using namespace llvm::PatternMatch;
87
88// Controls the number of uses of the value searched for possible
89// dominating comparisons.
90static cl::opt<unsigned> DomConditionsMaxUses("dom-conditions-max-uses",
91 cl::Hidden, cl::init(20));
92
93/// Maximum number of instructions to check between assume and context
94/// instruction.
95static constexpr unsigned MaxInstrsToCheckForFree = 16;
96
97/// Returns the bitwidth of the given scalar or pointer type. For vector types,
98/// returns the element type's bitwidth.
99static unsigned getBitWidth(Type *Ty, const DataLayout &DL) {
100 if (unsigned BitWidth = Ty->getScalarSizeInBits())
101 return BitWidth;
102
103 return DL.getPointerTypeSizeInBits(Ty);
104}
105
106// Given the provided Value and, potentially, a context instruction, return
107// the preferred context instruction (if any).
108static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI) {
109 // If we've been provided with a context instruction, then use that (provided
110 // it has been inserted).
111 if (CxtI && CxtI->getParent())
112 return CxtI;
113
114 // If the value is really an already-inserted instruction, then use that.
115 CxtI = dyn_cast<Instruction>(V);
116 if (CxtI && CxtI->getParent())
117 return CxtI;
118
119 return nullptr;
120}
121
123 const APInt &DemandedElts,
124 APInt &DemandedLHS, APInt &DemandedRHS) {
125 if (isa<ScalableVectorType>(Shuf->getType())) {
126 assert(DemandedElts == APInt(1,1));
127 DemandedLHS = DemandedRHS = DemandedElts;
128 return true;
129 }
130
131 int NumElts =
132 cast<FixedVectorType>(Shuf->getOperand(0)->getType())->getNumElements();
133 return llvm::getShuffleDemandedElts(NumElts, Shuf->getShuffleMask(),
134 DemandedElts, DemandedLHS, DemandedRHS);
135}
136
137static void computeKnownBits(const Value *V, const APInt &DemandedElts,
138 KnownBits &Known, const SimplifyQuery &Q,
139 unsigned Depth);
140
142 const SimplifyQuery &Q, unsigned Depth) {
143 // Since the number of lanes in a scalable vector is unknown at compile time,
144 // we track one bit which is implicitly broadcast to all lanes. This means
145 // that all lanes in a scalable vector are considered demanded.
146 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
147 APInt DemandedElts =
148 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
149 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);
150}
151
153 const DataLayout &DL, AssumptionCache *AC,
154 const Instruction *CxtI, const DominatorTree *DT,
155 bool UseInstrInfo, unsigned Depth) {
156 computeKnownBits(V, Known,
157 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo),
158 Depth);
159}
160
162 AssumptionCache *AC, const Instruction *CxtI,
163 const DominatorTree *DT, bool UseInstrInfo,
164 unsigned Depth) {
165 return computeKnownBits(
166 V, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
167}
168
169KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
170 const DataLayout &DL, AssumptionCache *AC,
171 const Instruction *CxtI,
172 const DominatorTree *DT, bool UseInstrInfo,
173 unsigned Depth) {
174 return computeKnownBits(
175 V, DemandedElts,
176 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
177}
178
180 const SimplifyQuery &SQ) {
181 // Look for an inverted mask: (X & ~M) op (Y & M).
182 {
183 Value *M;
184 if (match(LHS, m_c_And(m_Not(m_Value(M)), m_Value())) &&
186 isGuaranteedNotToBeUndef(M, SQ.AC, SQ.CxtI, SQ.DT))
187 return true;
188 }
189
190 // X op (Y & ~X)
193 return true;
194
195 // X op ((X & Y) ^ Y) -- this is the canonical form of the previous pattern
196 // for constant Y.
197 Value *Y;
198 if (match(RHS,
200 isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT) &&
201 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
202 return true;
203
204 // Peek through extends to find a 'not' of the other side:
205 // (ext Y) op ext(~Y)
206 if (match(LHS, m_ZExtOrSExt(m_Value(Y))) &&
208 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
209 return true;
210
211 // Look for: (A & B) op ~(A | B)
212 {
213 Value *A, *B;
214 if (match(LHS, m_And(m_Value(A), m_Value(B))) &&
216 isGuaranteedNotToBeUndef(A, SQ.AC, SQ.CxtI, SQ.DT) &&
217 isGuaranteedNotToBeUndef(B, SQ.AC, SQ.CxtI, SQ.DT))
218 return true;
219 }
220
221 // Look for: (X << V) op (Y >> (BitWidth - V))
222 // or (X >> V) op (Y << (BitWidth - V))
223 {
224 const Value *V;
225 const APInt *R;
226 if (((match(RHS, m_Shl(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&
227 match(LHS, m_LShr(m_Value(), m_Specific(V)))) ||
228 (match(RHS, m_LShr(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&
229 match(LHS, m_Shl(m_Value(), m_Specific(V))))) &&
230 R->uge(LHS->getType()->getScalarSizeInBits()))
231 return true;
232 }
233
234 return false;
235}
236
238 const WithCache<const Value *> &RHSCache,
239 const SimplifyQuery &SQ) {
240 const Value *LHS = LHSCache.getValue();
241 const Value *RHS = RHSCache.getValue();
242
243 assert(LHS->getType() == RHS->getType() &&
244 "LHS and RHS should have the same type");
245 assert(LHS->getType()->isIntOrIntVectorTy() &&
246 "LHS and RHS should be integers");
247
248 if (haveNoCommonBitsSetSpecialCases(LHS, RHS, SQ) ||
250 return true;
251
253 RHSCache.getKnownBits(SQ));
254}
255
257 return !I->user_empty() &&
258 all_of(I->users(), match_fn(m_ICmp(m_Value(), m_Zero())));
259}
260
262 return !I->user_empty() && all_of(I->users(), [](const User *U) {
263 CmpPredicate P;
264 return match(U, m_ICmp(P, m_Value(), m_Zero())) && ICmpInst::isEquality(P);
265 });
266}
267
269 bool OrZero, AssumptionCache *AC,
270 const Instruction *CxtI,
271 const DominatorTree *DT, bool UseInstrInfo,
272 unsigned Depth) {
273 return ::isKnownToBeAPowerOfTwo(
274 V, OrZero, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo),
275 Depth);
276}
277
278static bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
279 const SimplifyQuery &Q, unsigned Depth);
280
282 unsigned Depth) {
283 return computeKnownBits(V, SQ, Depth).isNonNegative();
284}
285
287 unsigned Depth) {
288 if (auto *CI = dyn_cast<ConstantInt>(V))
289 return CI->getValue().isStrictlyPositive();
290
291 // If `isKnownNonNegative` ever becomes more sophisticated, make sure to keep
292 // this updated.
293 KnownBits Known = computeKnownBits(V, SQ, Depth);
294 return Known.isNonNegative() &&
295 (Known.isNonZero() || isKnownNonZero(V, SQ, Depth));
296}
297
299 unsigned Depth) {
300 return computeKnownBits(V, SQ, Depth).isNegative();
301}
302
303static bool isKnownNonEqual(const Value *V1, const Value *V2,
304 const APInt &DemandedElts, const SimplifyQuery &Q,
305 unsigned Depth);
306
307bool llvm::isKnownNonEqual(const Value *V1, const Value *V2,
308 const SimplifyQuery &Q, unsigned Depth) {
309 // We don't support looking through casts.
310 if (V1 == V2 || V1->getType() != V2->getType())
311 return false;
312 auto *FVTy = dyn_cast<FixedVectorType>(V1->getType());
313 APInt DemandedElts =
314 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
315 return ::isKnownNonEqual(V1, V2, DemandedElts, Q, Depth);
316}
317
318bool llvm::MaskedValueIsZero(const Value *V, const APInt &Mask,
319 const SimplifyQuery &SQ, unsigned Depth) {
320 KnownBits Known(Mask.getBitWidth());
321 computeKnownBits(V, Known, SQ, Depth);
322 return Mask.isSubsetOf(Known.Zero);
323}
324
325static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
326 const SimplifyQuery &Q, unsigned Depth);
327
328static unsigned ComputeNumSignBits(const Value *V, const SimplifyQuery &Q,
329 unsigned Depth = 0) {
330 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
331 APInt DemandedElts =
332 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
333 return ComputeNumSignBits(V, DemandedElts, Q, Depth);
334}
335
336unsigned llvm::ComputeNumSignBits(const Value *V, const DataLayout &DL,
337 AssumptionCache *AC, const Instruction *CxtI,
338 const DominatorTree *DT, bool UseInstrInfo,
339 unsigned Depth) {
340 return ::ComputeNumSignBits(
341 V, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
342}
343
345 AssumptionCache *AC,
346 const Instruction *CxtI,
347 const DominatorTree *DT,
348 unsigned Depth) {
349 unsigned SignBits = ComputeNumSignBits(V, DL, AC, CxtI, DT, Depth);
350 return V->getType()->getScalarSizeInBits() - SignBits + 1;
351}
352
353/// Try to detect the lerp pattern: a * (b - c) + c * d
354/// where a >= 0, b >= 0, c >= 0, d >= 0, and b >= c.
355///
356/// In that particular case, we can use the following chain of reasoning:
357///
358/// a * (b - c) + c * d <= a' * (b - c) + a' * c = a' * b where a' = max(a, d)
359///
360/// Since that is true for arbitrary a, b, c and d within our constraints, we
361/// can conclude that:
362///
363/// max(a * (b - c) + c * d) <= max(max(a), max(d)) * max(b) = U
364///
365/// Considering that any result of the lerp would be less or equal to U, it
366/// would have at least the number of leading 0s as in U.
367///
368/// While being quite a specific situation, it is fairly common in computer
369/// graphics in the shape of alpha blending.
370///
371/// Modifies given KnownOut in-place with the inferred information.
372static void computeKnownBitsFromLerpPattern(const Value *Op0, const Value *Op1,
373 const APInt &DemandedElts,
374 KnownBits &KnownOut,
375 const SimplifyQuery &Q,
376 unsigned Depth) {
377
378 Type *Ty = Op0->getType();
379 const unsigned BitWidth = Ty->getScalarSizeInBits();
380
381 // Only handle scalar types for now
382 if (Ty->isVectorTy())
383 return;
384
385 // Try to match: a * (b - c) + c * d.
386 // When a == 1 => A == nullptr, the same applies to d/D as well.
387 const Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
388 const Instruction *SubBC = nullptr;
389
390 const auto MatchSubBC = [&]() {
391 // (b - c) can have two forms that interest us:
392 //
393 // 1. sub nuw %b, %c
394 // 2. xor %c, %b
395 //
396 // For the first case, nuw flag guarantees our requirement b >= c.
397 //
398 // The second case might happen when the analysis can infer that b is a mask
399 // for c and we can transform sub operation into xor (that is usually true
400 // for constant b's). Even though xor is symmetrical, canonicalization
401 // ensures that the constant will be the RHS. We have additional checks
402 // later on to ensure that this xor operation is equivalent to subtraction.
404 m_Xor(m_Value(C), m_Value(B))));
405 };
406
407 const auto MatchASubBC = [&]() {
408 // Cases:
409 // - a * (b - c)
410 // - (b - c) * a
411 // - (b - c) <- a implicitly equals 1
412 return m_CombineOr(m_c_Mul(m_Value(A), MatchSubBC()), MatchSubBC());
413 };
414
415 const auto MatchCD = [&]() {
416 // Cases:
417 // - d * c
418 // - c * d
419 // - c <- d implicitly equals 1
421 };
422
423 const auto Match = [&](const Value *LHS, const Value *RHS) {
424 // We do use m_Specific(C) in MatchCD, so we have to make sure that
425 // it's bound to anything and match(LHS, MatchASubBC()) absolutely
426 // has to evaluate first and return true.
427 //
428 // If Match returns true, it is guaranteed that B != nullptr, C != nullptr.
429 return match(LHS, MatchASubBC()) && match(RHS, MatchCD());
430 };
431
432 if (!Match(Op0, Op1) && !Match(Op1, Op0))
433 return;
434
435 const auto ComputeKnownBitsOrOne = [&](const Value *V) {
436 // For some of the values we use the convention of leaving
437 // it nullptr to signify an implicit constant 1.
438 return V ? computeKnownBits(V, DemandedElts, Q, Depth + 1)
440 };
441
442 // Check that all operands are non-negative
443 const KnownBits KnownA = ComputeKnownBitsOrOne(A);
444 if (!KnownA.isNonNegative())
445 return;
446
447 const KnownBits KnownD = ComputeKnownBitsOrOne(D);
448 if (!KnownD.isNonNegative())
449 return;
450
451 const KnownBits KnownB = computeKnownBits(B, DemandedElts, Q, Depth + 1);
452 if (!KnownB.isNonNegative())
453 return;
454
455 const KnownBits KnownC = computeKnownBits(C, DemandedElts, Q, Depth + 1);
456 if (!KnownC.isNonNegative())
457 return;
458
459 // If we matched subtraction as xor, we need to actually check that xor
460 // is semantically equivalent to subtraction.
461 //
462 // For that to be true, b has to be a mask for c or that b's known
463 // ones cover all known and possible ones of c.
464 if (SubBC->getOpcode() == Instruction::Xor &&
465 !KnownC.getMaxValue().isSubsetOf(KnownB.getMinValue()))
466 return;
467
468 const APInt MaxA = KnownA.getMaxValue();
469 const APInt MaxD = KnownD.getMaxValue();
470 const APInt MaxAD = APIntOps::umax(MaxA, MaxD);
471 const APInt MaxB = KnownB.getMaxValue();
472
473 // We can't infer leading zeros info if the upper-bound estimate wraps.
474 bool Overflow;
475 const APInt UpperBound = MaxAD.umul_ov(MaxB, Overflow);
476
477 if (Overflow)
478 return;
479
480 // If we know that x <= y and both are positive than x has at least the same
481 // number of leading zeros as y.
482 const unsigned MinimumNumberOfLeadingZeros = UpperBound.countl_zero();
483 KnownOut.Zero.setHighBits(MinimumNumberOfLeadingZeros);
484}
485
486static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1,
487 bool NSW, bool NUW,
488 const APInt &DemandedElts,
489 KnownBits &KnownOut, KnownBits &Known2,
490 const SimplifyQuery &Q, unsigned Depth) {
491 computeKnownBits(Op1, DemandedElts, KnownOut, Q, Depth + 1);
492
493 // If one operand is unknown and we have no nowrap information,
494 // the result will be unknown independently of the second operand.
495 if (KnownOut.isUnknown() && !NSW && !NUW)
496 return;
497
498 computeKnownBits(Op0, DemandedElts, Known2, Q, Depth + 1);
499 KnownOut = KnownBits::computeForAddSub(Add, NSW, NUW, Known2, KnownOut);
500
501 if (!Add && NSW && !KnownOut.isNonNegative() &&
503 .value_or(false) ||
504 match(Op1, m_c_SMin(m_Specific(Op0), m_Value()))))
505 KnownOut.makeNonNegative();
506
507 if (Add)
508 // Try to match lerp pattern and combine results
509 computeKnownBitsFromLerpPattern(Op0, Op1, DemandedElts, KnownOut, Q, Depth);
510}
511
512static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,
513 bool NUW, const APInt &DemandedElts,
514 KnownBits &Known, KnownBits &Known2,
515 const SimplifyQuery &Q, unsigned Depth) {
516 computeKnownBits(Op1, DemandedElts, Known, Q, Depth + 1);
517 computeKnownBits(Op0, DemandedElts, Known2, Q, Depth + 1);
518
519 bool isKnownNegative = false;
520 bool isKnownNonNegative = false;
521 // If the multiplication is known not to overflow, compute the sign bit.
522 if (NSW) {
523 if (Op0 == Op1) {
524 // The product of a number with itself is non-negative.
525 isKnownNonNegative = true;
526 } else {
527 bool isKnownNonNegativeOp1 = Known.isNonNegative();
528 bool isKnownNonNegativeOp0 = Known2.isNonNegative();
529 bool isKnownNegativeOp1 = Known.isNegative();
530 bool isKnownNegativeOp0 = Known2.isNegative();
531 // The product of two numbers with the same sign is non-negative.
532 isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) ||
533 (isKnownNonNegativeOp1 && isKnownNonNegativeOp0);
534 if (!isKnownNonNegative && NUW) {
535 // mul nuw nsw with a factor > 1 is non-negative.
537 isKnownNonNegative = KnownBits::sgt(Known, One).value_or(false) ||
538 KnownBits::sgt(Known2, One).value_or(false);
539 }
540
541 // The product of a negative number and a non-negative number is either
542 // negative or zero.
545 (isKnownNegativeOp1 && isKnownNonNegativeOp0 &&
546 Known2.isNonZero()) ||
547 (isKnownNegativeOp0 && isKnownNonNegativeOp1 && Known.isNonZero());
548 }
549 }
550
551 bool SelfMultiply = Op0 == Op1;
552 if (SelfMultiply)
553 SelfMultiply &=
554 isGuaranteedNotToBeUndef(Op0, Q.AC, Q.CxtI, Q.DT, Depth + 1);
555 Known = KnownBits::mul(Known, Known2, SelfMultiply);
556
557 if (SelfMultiply) {
558 unsigned SignBits = ComputeNumSignBits(Op0, DemandedElts, Q, Depth + 1);
559 unsigned TyBits = Op0->getType()->getScalarSizeInBits();
560 unsigned OutValidBits = 2 * (TyBits - SignBits + 1);
561
562 if (OutValidBits < TyBits) {
563 APInt KnownZeroMask =
564 APInt::getHighBitsSet(TyBits, TyBits - OutValidBits + 1);
565 Known.Zero |= KnownZeroMask;
566 }
567 }
568
569 // Only make use of no-wrap flags if we failed to compute the sign bit
570 // directly. This matters if the multiplication always overflows, in
571 // which case we prefer to follow the result of the direct computation,
572 // though as the program is invoking undefined behaviour we can choose
573 // whatever we like here.
574 if (isKnownNonNegative && !Known.isNegative())
575 Known.makeNonNegative();
576 else if (isKnownNegative && !Known.isNonNegative())
577 Known.makeNegative();
578}
579
581 KnownBits &Known) {
582 unsigned BitWidth = Known.getBitWidth();
583 unsigned NumRanges = Ranges.getNumOperands() / 2;
584 assert(NumRanges >= 1);
585
586 Known.setAllConflict();
587
588 for (unsigned i = 0; i < NumRanges; ++i) {
590 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
592 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
593 ConstantRange Range(Lower->getValue(), Upper->getValue());
594 // BitWidth must equal the Ranges BitWidth for the correct number of high
595 // bits to be set.
596 assert(BitWidth == Range.getBitWidth() &&
597 "Known bit width must match range bit width!");
598
599 // The first CommonPrefixBits of all values in Range are equal.
600 unsigned CommonPrefixBits =
601 (Range.getUnsignedMax() ^ Range.getUnsignedMin()).countl_zero();
602 APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits);
603 APInt UnsignedMax = Range.getUnsignedMax().zextOrTrunc(BitWidth);
604 Known.One &= UnsignedMax & Mask;
605 Known.Zero &= ~UnsignedMax & Mask;
606 }
607}
608
609static bool isEphemeralValueOf(const Instruction *I, const Value *E) {
613
614 // The instruction defining an assumption's condition itself is always
615 // considered ephemeral to that assumption (even if it has other
616 // non-ephemeral users). See r246696's test case for an example.
617 if (is_contained(I->operands(), E))
618 return true;
619
620 while (!WorkSet.empty()) {
621 const Instruction *V = WorkSet.pop_back_val();
622 if (!Visited.insert(V).second)
623 continue;
624
625 // If all uses of this value are ephemeral, then so is this value.
626 if (all_of(V->users(), [&](const User *U) {
627 return EphValues.count(cast<Instruction>(U));
628 })) {
629 if (V == E)
630 return true;
631
632 if (V == I || (!V->mayHaveSideEffects() && !V->isTerminator())) {
633 EphValues.insert(V);
634
635 for (const Use &U : V->operands()) {
636 if (const auto *I = dyn_cast<Instruction>(U.get()))
637 WorkSet.push_back(I);
638 }
639 }
640 }
641 }
642
643 return false;
644}
645
646// Is this an intrinsic that cannot be speculated but also cannot trap?
648 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(I))
649 return CI->isAssumeLikeIntrinsic();
650
651 return false;
652}
653
655 const Instruction *CxtI,
656 const DominatorTree *DT,
657 bool AllowEphemerals) {
658 // There are two restrictions on the use of an assume:
659 // 1. The assume must dominate the context (or the control flow must
660 // reach the assume whenever it reaches the context).
661 // 2. The context must not be in the assume's set of ephemeral values
662 // (otherwise we will use the assume to prove that the condition
663 // feeding the assume is trivially true, thus causing the removal of
664 // the assume).
665
666 if (Inv->getParent() == CxtI->getParent()) {
667 // If Inv and CtxI are in the same block, check if the assume (Inv) is first
668 // in the BB.
669 if (Inv->comesBefore(CxtI))
670 return true;
671
672 // Don't let an assume affect itself - this would cause the problems
673 // `isEphemeralValueOf` is trying to prevent, and it would also make
674 // the loop below go out of bounds.
675 if (!AllowEphemerals && Inv == CxtI)
676 return false;
677
678 // The context comes first, but they're both in the same block.
679 // Make sure there is nothing in between that might interrupt
680 // the control flow, not even CxtI itself.
681 // We limit the scan distance between the assume and its context instruction
682 // to avoid a compile-time explosion. This limit is chosen arbitrarily, so
683 // it can be adjusted if needed (could be turned into a cl::opt).
684 auto Range = make_range(CxtI->getIterator(), Inv->getIterator());
686 return false;
687
688 return AllowEphemerals || !isEphemeralValueOf(Inv, CxtI);
689 }
690
691 // Inv and CxtI are in different blocks.
692 if (DT) {
693 if (DT->dominates(Inv, CxtI))
694 return true;
695 } else if (Inv->getParent() == CxtI->getParent()->getSinglePredecessor() ||
696 Inv->getParent()->isEntryBlock()) {
697 // We don't have a DT, but this trivially dominates.
698 return true;
699 }
700
701 return false;
702}
703
705 const Instruction *CtxI) {
706 // Helper to check if there are any calls in the range that may free memory.
707 auto hasNoFreeCalls = [](auto Range) {
708 for (const auto &[Idx, I] : enumerate(Range)) {
709 if (Idx > MaxInstrsToCheckForFree)
710 return false;
711 if (const auto *CB = dyn_cast<CallBase>(&I))
712 if (!CB->hasFnAttr(Attribute::NoFree))
713 return false;
714 }
715 return true;
716 };
717
718 // Make sure the current function cannot arrange for another thread to free on
719 // its behalf.
720 if (!CtxI->getFunction()->hasNoSync())
721 return false;
722
723 // Handle cross-block case: CtxI in a successor of Assume's block.
724 const BasicBlock *CtxBB = CtxI->getParent();
725 const BasicBlock *AssumeBB = Assume->getParent();
726 BasicBlock::const_iterator CtxIter = CtxI->getIterator();
727 if (CtxBB != AssumeBB) {
728 if (CtxBB->getSinglePredecessor() != AssumeBB)
729 return false;
730
731 if (!hasNoFreeCalls(make_range(CtxBB->begin(), CtxIter)))
732 return false;
733
734 CtxIter = AssumeBB->end();
735 } else {
736 // Same block case: check that Assume comes before CtxI.
737 if (!Assume->comesBefore(CtxI))
738 return false;
739 }
740
741 // Check if there are any calls between Assume and CtxIter that may free
742 // memory.
743 return hasNoFreeCalls(make_range(Assume->getIterator(), CtxIter));
744}
745
746// TODO: cmpExcludesZero misses many cases where `RHS` is non-constant but
747// we still have enough information about `RHS` to conclude non-zero. For
748// example Pred=EQ, RHS=isKnownNonZero. cmpExcludesZero is called in loops
749// so the extra compile time may not be worth it, but possibly a second API
750// should be created for use outside of loops.
751static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS) {
752 // v u> y implies v != 0.
753 if (Pred == ICmpInst::ICMP_UGT)
754 return true;
755
756 // Special-case v != 0 to also handle v != null.
757 if (Pred == ICmpInst::ICMP_NE)
758 return match(RHS, m_Zero());
759
760 // All other predicates - rely on generic ConstantRange handling.
761 const APInt *C;
762 auto Zero = APInt::getZero(RHS->getType()->getScalarSizeInBits());
763 if (match(RHS, m_APInt(C))) {
765 return !TrueValues.contains(Zero);
766 }
767
769 if (VC == nullptr)
770 return false;
771
772 for (unsigned ElemIdx = 0, NElem = VC->getNumElements(); ElemIdx < NElem;
773 ++ElemIdx) {
775 Pred, VC->getElementAsAPInt(ElemIdx));
776 if (TrueValues.contains(Zero))
777 return false;
778 }
779 return true;
780}
781
782static void breakSelfRecursivePHI(const Use *U, const PHINode *PHI,
783 Value *&ValOut, Instruction *&CtxIOut,
784 const PHINode **PhiOut = nullptr) {
785 ValOut = U->get();
786 if (ValOut == PHI)
787 return;
788 CtxIOut = PHI->getIncomingBlock(*U)->getTerminator();
789 if (PhiOut)
790 *PhiOut = PHI;
791 Value *V;
792 // If the Use is a select of this phi, compute analysis on other arm to break
793 // recursion.
794 // TODO: Min/Max
795 if (match(ValOut, m_Select(m_Value(), m_Specific(PHI), m_Value(V))) ||
796 match(ValOut, m_Select(m_Value(), m_Value(V), m_Specific(PHI))))
797 ValOut = V;
798
799 // Same for select, if this phi is 2-operand phi, compute analysis on other
800 // incoming value to break recursion.
801 // TODO: We could handle any number of incoming edges as long as we only have
802 // two unique values.
803 if (auto *IncPhi = dyn_cast<PHINode>(ValOut);
804 IncPhi && IncPhi->getNumIncomingValues() == 2) {
805 for (int Idx = 0; Idx < 2; ++Idx) {
806 if (IncPhi->getIncomingValue(Idx) == PHI) {
807 ValOut = IncPhi->getIncomingValue(1 - Idx);
808 if (PhiOut)
809 *PhiOut = IncPhi;
810 CtxIOut = IncPhi->getIncomingBlock(1 - Idx)->getTerminator();
811 break;
812 }
813 }
814 }
815}
816
817static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q) {
818 // Use of assumptions is context-sensitive. If we don't have a context, we
819 // cannot use them!
820 if (!Q.AC || !Q.CxtI)
821 return false;
822
823 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
824 if (!Elem.Assume)
825 continue;
826
827 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
828 assert(I->getFunction() == Q.CxtI->getFunction() &&
829 "Got assumption for the wrong function!");
830
831 if (Elem.Index != AssumptionCache::ExprResultIdx) {
832 if (!V->getType()->isPointerTy())
833 continue;
835 *I, I->bundle_op_info_begin()[Elem.Index])) {
836 if (RK.WasOn != V)
837 continue;
838 bool AssumeImpliesNonNull = [&]() {
839 if (RK.AttrKind == Attribute::NonNull)
840 return true;
841
842 if (RK.AttrKind == Attribute::Dereferenceable) {
845 return false;
846 assert(RK.IRArgValue &&
847 "Dereferenceable attribute without IR argument?");
848
849 auto *CI = dyn_cast<ConstantInt>(RK.IRArgValue);
850 return CI && !CI->isZero();
851 }
852
853 return false;
854 }();
855 if (AssumeImpliesNonNull && isValidAssumeForContext(I, Q.CxtI, Q.DT))
856 return true;
857 }
858 continue;
859 }
860
861 // Warning: This loop can end up being somewhat performance sensitive.
862 // We're running this loop for once for each value queried resulting in a
863 // runtime of ~O(#assumes * #values).
864
865 Value *RHS;
866 CmpPredicate Pred;
867 auto m_V = m_CombineOr(m_Specific(V), m_PtrToInt(m_Specific(V)));
868 if (!match(I->getArgOperand(0), m_c_ICmp(Pred, m_V, m_Value(RHS))))
869 continue;
870
871 if (cmpExcludesZero(Pred, RHS) && isValidAssumeForContext(I, Q.CxtI, Q.DT))
872 return true;
873 }
874
875 return false;
876}
877
879 Value *LHS, Value *RHS, KnownBits &Known,
880 const SimplifyQuery &Q) {
881 if (RHS->getType()->isPointerTy()) {
882 // Handle comparison of pointer to null explicitly, as it will not be
883 // covered by the m_APInt() logic below.
884 if (LHS == V && match(RHS, m_Zero())) {
885 switch (Pred) {
887 Known.setAllZero();
888 break;
891 Known.makeNonNegative();
892 break;
894 Known.makeNegative();
895 break;
896 default:
897 break;
898 }
899 }
900 return;
901 }
902
903 unsigned BitWidth = Known.getBitWidth();
904 auto m_V =
906
907 Value *Y;
908 const APInt *Mask, *C;
909 if (!match(RHS, m_APInt(C)))
910 return;
911
912 uint64_t ShAmt;
913 switch (Pred) {
915 // assume(V = C)
916 if (match(LHS, m_V)) {
917 Known = Known.unionWith(KnownBits::makeConstant(*C));
918 // assume(V & Mask = C)
919 } else if (match(LHS, m_c_And(m_V, m_Value(Y)))) {
920 // For one bits in Mask, we can propagate bits from C to V.
921 Known.One |= *C;
922 if (match(Y, m_APInt(Mask)))
923 Known.Zero |= ~*C & *Mask;
924 // assume(V | Mask = C)
925 } else if (match(LHS, m_c_Or(m_V, m_Value(Y)))) {
926 // For zero bits in Mask, we can propagate bits from C to V.
927 Known.Zero |= ~*C;
928 if (match(Y, m_APInt(Mask)))
929 Known.One |= *C & ~*Mask;
930 // assume(V << ShAmt = C)
931 } else if (match(LHS, m_Shl(m_V, m_ConstantInt(ShAmt))) &&
932 ShAmt < BitWidth) {
933 // For those bits in C that are known, we can propagate them to known
934 // bits in V shifted to the right by ShAmt.
936 RHSKnown >>= ShAmt;
937 Known = Known.unionWith(RHSKnown);
938 // assume(V >> ShAmt = C)
939 } else if (match(LHS, m_Shr(m_V, m_ConstantInt(ShAmt))) &&
940 ShAmt < BitWidth) {
941 // For those bits in RHS that are known, we can propagate them to known
942 // bits in V shifted to the right by C.
944 RHSKnown <<= ShAmt;
945 Known = Known.unionWith(RHSKnown);
946 }
947 break;
948 case ICmpInst::ICMP_NE: {
949 // assume (V & B != 0) where B is a power of 2
950 const APInt *BPow2;
951 if (C->isZero() && match(LHS, m_And(m_V, m_Power2(BPow2))))
952 Known.One |= *BPow2;
953 break;
954 }
955 default: {
956 const APInt *Offset = nullptr;
957 if (match(LHS, m_CombineOr(m_V, m_AddLike(m_V, m_APInt(Offset))))) {
959 if (Offset)
960 LHSRange = LHSRange.sub(*Offset);
961 Known = Known.unionWith(LHSRange.toKnownBits());
962 }
963 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
964 // X & Y u> C -> X u> C && Y u> C
965 // X nuw- Y u> C -> X u> C
966 if (match(LHS, m_c_And(m_V, m_Value())) ||
967 match(LHS, m_NUWSub(m_V, m_Value())))
968 Known.One.setHighBits(
969 (*C + (Pred == ICmpInst::ICMP_UGT)).countLeadingOnes());
970 }
971 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
972 // X | Y u< C -> X u< C && Y u< C
973 // X nuw+ Y u< C -> X u< C && Y u< C
974 if (match(LHS, m_c_Or(m_V, m_Value())) ||
975 match(LHS, m_c_NUWAdd(m_V, m_Value()))) {
976 Known.Zero.setHighBits(
977 (*C - (Pred == ICmpInst::ICMP_ULT)).countLeadingZeros());
978 }
979 }
980 } break;
981 }
982}
983
984static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp,
985 KnownBits &Known,
986 const SimplifyQuery &SQ, bool Invert) {
988 Invert ? Cmp->getInversePredicate() : Cmp->getPredicate();
989 Value *LHS = Cmp->getOperand(0);
990 Value *RHS = Cmp->getOperand(1);
991
992 // Handle icmp pred (trunc V), C
993 if (match(LHS, m_Trunc(m_Specific(V)))) {
994 KnownBits DstKnown(LHS->getType()->getScalarSizeInBits());
995 computeKnownBitsFromCmp(LHS, Pred, LHS, RHS, DstKnown, SQ);
997 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
998 else
999 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
1000 return;
1001 }
1002
1003 computeKnownBitsFromCmp(V, Pred, LHS, RHS, Known, SQ);
1004}
1005
1007 KnownBits &Known, const SimplifyQuery &SQ,
1008 bool Invert, unsigned Depth) {
1009 Value *A, *B;
1012 KnownBits Known2(Known.getBitWidth());
1013 KnownBits Known3(Known.getBitWidth());
1014 computeKnownBitsFromCond(V, A, Known2, SQ, Invert, Depth + 1);
1015 computeKnownBitsFromCond(V, B, Known3, SQ, Invert, Depth + 1);
1016 if (Invert ? match(Cond, m_LogicalOr(m_Value(), m_Value()))
1018 Known2 = Known2.unionWith(Known3);
1019 else
1020 Known2 = Known2.intersectWith(Known3);
1021 Known = Known.unionWith(Known2);
1022 return;
1023 }
1024
1025 if (auto *Cmp = dyn_cast<ICmpInst>(Cond)) {
1026 computeKnownBitsFromICmpCond(V, Cmp, Known, SQ, Invert);
1027 return;
1028 }
1029
1030 if (match(Cond, m_Trunc(m_Specific(V)))) {
1031 KnownBits DstKnown(1);
1032 if (Invert) {
1033 DstKnown.setAllZero();
1034 } else {
1035 DstKnown.setAllOnes();
1036 }
1038 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
1039 return;
1040 }
1041 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
1042 return;
1043 }
1044
1046 computeKnownBitsFromCond(V, A, Known, SQ, !Invert, Depth + 1);
1047}
1048
1050 const SimplifyQuery &Q, unsigned Depth) {
1051 // Handle injected condition.
1052 if (Q.CC && Q.CC->AffectedValues.contains(V))
1053 computeKnownBitsFromCond(V, Q.CC->Cond, Known, Q, Q.CC->Invert, Depth);
1054
1055 if (!Q.CxtI)
1056 return;
1057
1058 if (Q.DC && Q.DT) {
1059 // Handle dominating conditions.
1060 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
1061 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
1062 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
1063 computeKnownBitsFromCond(V, BI->getCondition(), Known, Q,
1064 /*Invert*/ false, Depth);
1065
1066 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
1067 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
1068 computeKnownBitsFromCond(V, BI->getCondition(), Known, Q,
1069 /*Invert*/ true, Depth);
1070 }
1071
1072 if (Known.hasConflict())
1073 Known.resetAll();
1074 }
1075
1076 if (!Q.AC)
1077 return;
1078
1079 unsigned BitWidth = Known.getBitWidth();
1080
1081 // Note that the patterns below need to be kept in sync with the code
1082 // in AssumptionCache::updateAffectedValues.
1083
1084 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
1085 if (!Elem.Assume)
1086 continue;
1087
1088 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
1089 assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() &&
1090 "Got assumption for the wrong function!");
1091
1092 if (Elem.Index != AssumptionCache::ExprResultIdx) {
1093 if (!V->getType()->isPointerTy())
1094 continue;
1096 *I, I->bundle_op_info_begin()[Elem.Index])) {
1097 // Allow AllowEphemerals in isValidAssumeForContext, as the CxtI might
1098 // be the producer of the pointer in the bundle. At the moment, align
1099 // assumptions aren't optimized away.
1100 if (RK.WasOn == V && RK.AttrKind == Attribute::Alignment &&
1101 isPowerOf2_64(RK.ArgValue) &&
1102 isValidAssumeForContext(I, Q.CxtI, Q.DT, /*AllowEphemerals*/ true))
1103 Known.Zero.setLowBits(Log2_64(RK.ArgValue));
1104 }
1105 continue;
1106 }
1107
1108 // Warning: This loop can end up being somewhat performance sensitive.
1109 // We're running this loop for once for each value queried resulting in a
1110 // runtime of ~O(#assumes * #values).
1111
1112 Value *Arg = I->getArgOperand(0);
1113
1114 if (Arg == V && isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
1115 assert(BitWidth == 1 && "assume operand is not i1?");
1116 (void)BitWidth;
1117 Known.setAllOnes();
1118 return;
1119 }
1120 if (match(Arg, m_Not(m_Specific(V))) &&
1122 assert(BitWidth == 1 && "assume operand is not i1?");
1123 (void)BitWidth;
1124 Known.setAllZero();
1125 return;
1126 }
1127 auto *Trunc = dyn_cast<TruncInst>(Arg);
1128 if (Trunc && Trunc->getOperand(0) == V &&
1130 if (Trunc->hasNoUnsignedWrap()) {
1132 return;
1133 }
1134 Known.One.setBit(0);
1135 return;
1136 }
1137
1138 // The remaining tests are all recursive, so bail out if we hit the limit.
1140 continue;
1141
1142 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
1143 if (!Cmp)
1144 continue;
1145
1146 if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
1147 continue;
1148
1149 computeKnownBitsFromICmpCond(V, Cmp, Known, Q, /*Invert=*/false);
1150 }
1151
1152 // Conflicting assumption: Undefined behavior will occur on this execution
1153 // path.
1154 if (Known.hasConflict())
1155 Known.resetAll();
1156}
1157
1158/// Compute known bits from a shift operator, including those with a
1159/// non-constant shift amount. Known is the output of this function. Known2 is a
1160/// pre-allocated temporary with the same bit width as Known and on return
1161/// contains the known bit of the shift value source. KF is an
1162/// operator-specific function that, given the known-bits and a shift amount,
1163/// compute the implied known-bits of the shift operator's result respectively
1164/// for that shift amount. The results from calling KF are conservatively
1165/// combined for all permitted shift amounts.
1167 const Operator *I, const APInt &DemandedElts, KnownBits &Known,
1168 KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth,
1169 function_ref<KnownBits(const KnownBits &, const KnownBits &, bool)> KF) {
1170 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1171 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1172 // To limit compile-time impact, only query isKnownNonZero() if we know at
1173 // least something about the shift amount.
1174 bool ShAmtNonZero =
1175 Known.isNonZero() ||
1176 (Known.getMaxValue().ult(Known.getBitWidth()) &&
1177 isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth + 1));
1178 Known = KF(Known2, Known, ShAmtNonZero);
1179}
1180
1181static KnownBits
1182getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts,
1183 const KnownBits &KnownLHS, const KnownBits &KnownRHS,
1184 const SimplifyQuery &Q, unsigned Depth) {
1185 unsigned BitWidth = KnownLHS.getBitWidth();
1186 KnownBits KnownOut(BitWidth);
1187 bool IsAnd = false;
1188 bool HasKnownOne = !KnownLHS.One.isZero() || !KnownRHS.One.isZero();
1189 Value *X = nullptr, *Y = nullptr;
1190
1191 switch (I->getOpcode()) {
1192 case Instruction::And:
1193 KnownOut = KnownLHS & KnownRHS;
1194 IsAnd = true;
1195 // and(x, -x) is common idioms that will clear all but lowest set
1196 // bit. If we have a single known bit in x, we can clear all bits
1197 // above it.
1198 // TODO: instcombine often reassociates independent `and` which can hide
1199 // this pattern. Try to match and(x, and(-x, y)) / and(and(x, y), -x).
1200 if (HasKnownOne && match(I, m_c_And(m_Value(X), m_Neg(m_Deferred(X))))) {
1201 // -(-x) == x so using whichever (LHS/RHS) gets us a better result.
1202 if (KnownLHS.countMaxTrailingZeros() <= KnownRHS.countMaxTrailingZeros())
1203 KnownOut = KnownLHS.blsi();
1204 else
1205 KnownOut = KnownRHS.blsi();
1206 }
1207 break;
1208 case Instruction::Or:
1209 KnownOut = KnownLHS | KnownRHS;
1210 break;
1211 case Instruction::Xor:
1212 KnownOut = KnownLHS ^ KnownRHS;
1213 // xor(x, x-1) is common idioms that will clear all but lowest set
1214 // bit. If we have a single known bit in x, we can clear all bits
1215 // above it.
1216 // TODO: xor(x, x-1) is often rewritting as xor(x, x-C) where C !=
1217 // -1 but for the purpose of demanded bits (xor(x, x-C) &
1218 // Demanded) == (xor(x, x-1) & Demanded). Extend the xor pattern
1219 // to use arbitrary C if xor(x, x-C) as the same as xor(x, x-1).
1220 if (HasKnownOne &&
1222 const KnownBits &XBits = I->getOperand(0) == X ? KnownLHS : KnownRHS;
1223 KnownOut = XBits.blsmsk();
1224 }
1225 break;
1226 default:
1227 llvm_unreachable("Invalid Op used in 'analyzeKnownBitsFromAndXorOr'");
1228 }
1229
1230 // and(x, add (x, -1)) is a common idiom that always clears the low bit;
1231 // xor/or(x, add (x, -1)) is an idiom that will always set the low bit.
1232 // here we handle the more general case of adding any odd number by
1233 // matching the form and/xor/or(x, add(x, y)) where y is odd.
1234 // TODO: This could be generalized to clearing any bit set in y where the
1235 // following bit is known to be unset in y.
1236 if (!KnownOut.Zero[0] && !KnownOut.One[0] &&
1240 KnownBits KnownY(BitWidth);
1241 computeKnownBits(Y, DemandedElts, KnownY, Q, Depth + 1);
1242 if (KnownY.countMinTrailingOnes() > 0) {
1243 if (IsAnd)
1244 KnownOut.Zero.setBit(0);
1245 else
1246 KnownOut.One.setBit(0);
1247 }
1248 }
1249 return KnownOut;
1250}
1251
1253 const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q,
1254 unsigned Depth,
1255 const function_ref<KnownBits(const KnownBits &, const KnownBits &)>
1256 KnownBitsFunc) {
1257 APInt DemandedEltsLHS, DemandedEltsRHS;
1259 DemandedElts, DemandedEltsLHS,
1260 DemandedEltsRHS);
1261
1262 const auto ComputeForSingleOpFunc =
1263 [Depth, &Q, KnownBitsFunc](const Value *Op, APInt &DemandedEltsOp) {
1264 return KnownBitsFunc(
1265 computeKnownBits(Op, DemandedEltsOp, Q, Depth + 1),
1266 computeKnownBits(Op, DemandedEltsOp << 1, Q, Depth + 1));
1267 };
1268
1269 if (DemandedEltsRHS.isZero())
1270 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS);
1271 if (DemandedEltsLHS.isZero())
1272 return ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS);
1273
1274 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS)
1275 .intersectWith(ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS));
1276}
1277
1278// Public so this can be used in `SimplifyDemandedUseBits`.
1280 const KnownBits &KnownLHS,
1281 const KnownBits &KnownRHS,
1282 const SimplifyQuery &SQ,
1283 unsigned Depth) {
1284 auto *FVTy = dyn_cast<FixedVectorType>(I->getType());
1285 APInt DemandedElts =
1286 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
1287
1288 return getKnownBitsFromAndXorOr(I, DemandedElts, KnownLHS, KnownRHS, SQ,
1289 Depth);
1290}
1291
1293 Attribute Attr = F->getFnAttribute(Attribute::VScaleRange);
1294 // Without vscale_range, we only know that vscale is non-zero.
1295 if (!Attr.isValid())
1297
1298 unsigned AttrMin = Attr.getVScaleRangeMin();
1299 // Minimum is larger than vscale width, result is always poison.
1300 if ((unsigned)llvm::bit_width(AttrMin) > BitWidth)
1301 return ConstantRange::getEmpty(BitWidth);
1302
1303 APInt Min(BitWidth, AttrMin);
1304 std::optional<unsigned> AttrMax = Attr.getVScaleRangeMax();
1305 if (!AttrMax || (unsigned)llvm::bit_width(*AttrMax) > BitWidth)
1307
1308 return ConstantRange(Min, APInt(BitWidth, *AttrMax) + 1);
1309}
1310
1312 Value *Arm, bool Invert,
1313 const SimplifyQuery &Q, unsigned Depth) {
1314 // If we have a constant arm, we are done.
1315 if (Known.isConstant())
1316 return;
1317
1318 // See what condition implies about the bits of the select arm.
1319 KnownBits CondRes(Known.getBitWidth());
1320 computeKnownBitsFromCond(Arm, Cond, CondRes, Q, Invert, Depth + 1);
1321 // If we don't get any information from the condition, no reason to
1322 // proceed.
1323 if (CondRes.isUnknown())
1324 return;
1325
1326 // We can have conflict if the condition is dead. I.e if we have
1327 // (x | 64) < 32 ? (x | 64) : y
1328 // we will have conflict at bit 6 from the condition/the `or`.
1329 // In that case just return. Its not particularly important
1330 // what we do, as this select is going to be simplified soon.
1331 CondRes = CondRes.unionWith(Known);
1332 if (CondRes.hasConflict())
1333 return;
1334
1335 // Finally make sure the information we found is valid. This is relatively
1336 // expensive so it's left for the very end.
1337 if (!isGuaranteedNotToBeUndef(Arm, Q.AC, Q.CxtI, Q.DT, Depth + 1))
1338 return;
1339
1340 // Finally, we know we get information from the condition and its valid,
1341 // so return it.
1342 Known = std::move(CondRes);
1343}
1344
1345// Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow).
1346// Returns the input and lower/upper bounds.
1347static bool isSignedMinMaxClamp(const Value *Select, const Value *&In,
1348 const APInt *&CLow, const APInt *&CHigh) {
1350 cast<Operator>(Select)->getOpcode() == Instruction::Select &&
1351 "Input should be a Select!");
1352
1353 const Value *LHS = nullptr, *RHS = nullptr;
1355 if (SPF != SPF_SMAX && SPF != SPF_SMIN)
1356 return false;
1357
1358 if (!match(RHS, m_APInt(CLow)))
1359 return false;
1360
1361 const Value *LHS2 = nullptr, *RHS2 = nullptr;
1363 if (getInverseMinMaxFlavor(SPF) != SPF2)
1364 return false;
1365
1366 if (!match(RHS2, m_APInt(CHigh)))
1367 return false;
1368
1369 if (SPF == SPF_SMIN)
1370 std::swap(CLow, CHigh);
1371
1372 In = LHS2;
1373 return CLow->sle(*CHigh);
1374}
1375
1377 const APInt *&CLow,
1378 const APInt *&CHigh) {
1379 assert((II->getIntrinsicID() == Intrinsic::smin ||
1380 II->getIntrinsicID() == Intrinsic::smax) &&
1381 "Must be smin/smax");
1382
1383 Intrinsic::ID InverseID = getInverseMinMaxIntrinsic(II->getIntrinsicID());
1384 auto *InnerII = dyn_cast<IntrinsicInst>(II->getArgOperand(0));
1385 if (!InnerII || InnerII->getIntrinsicID() != InverseID ||
1386 !match(II->getArgOperand(1), m_APInt(CLow)) ||
1387 !match(InnerII->getArgOperand(1), m_APInt(CHigh)))
1388 return false;
1389
1390 if (II->getIntrinsicID() == Intrinsic::smin)
1391 std::swap(CLow, CHigh);
1392 return CLow->sle(*CHigh);
1393}
1394
1396 KnownBits &Known) {
1397 const APInt *CLow, *CHigh;
1398 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
1399 Known = Known.unionWith(
1400 ConstantRange::getNonEmpty(*CLow, *CHigh + 1).toKnownBits());
1401}
1402
1404 const APInt &DemandedElts,
1405 KnownBits &Known,
1406 const SimplifyQuery &Q,
1407 unsigned Depth) {
1408 unsigned BitWidth = Known.getBitWidth();
1409
1410 KnownBits Known2(BitWidth);
1411 switch (I->getOpcode()) {
1412 default: break;
1413 case Instruction::Load:
1414 if (MDNode *MD =
1415 Q.IIQ.getMetadata(cast<LoadInst>(I), LLVMContext::MD_range))
1417 break;
1418 case Instruction::And:
1419 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1420 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1421
1422 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1423 break;
1424 case Instruction::Or:
1425 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1426 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1427
1428 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1429 break;
1430 case Instruction::Xor:
1431 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1432 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1433
1434 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1435 break;
1436 case Instruction::Mul: {
1439 computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, NUW,
1440 DemandedElts, Known, Known2, Q, Depth);
1441 break;
1442 }
1443 case Instruction::UDiv: {
1444 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1445 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1446 Known =
1447 KnownBits::udiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1448 break;
1449 }
1450 case Instruction::SDiv: {
1451 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1452 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1453 Known =
1454 KnownBits::sdiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1455 break;
1456 }
1457 case Instruction::Select: {
1458 auto ComputeForArm = [&](Value *Arm, bool Invert) {
1459 KnownBits Res(Known.getBitWidth());
1460 computeKnownBits(Arm, DemandedElts, Res, Q, Depth + 1);
1461 adjustKnownBitsForSelectArm(Res, I->getOperand(0), Arm, Invert, Q, Depth);
1462 return Res;
1463 };
1464 // Only known if known in both the LHS and RHS.
1465 Known =
1466 ComputeForArm(I->getOperand(1), /*Invert=*/false)
1467 .intersectWith(ComputeForArm(I->getOperand(2), /*Invert=*/true));
1468 break;
1469 }
1470 case Instruction::FPTrunc:
1471 case Instruction::FPExt:
1472 case Instruction::FPToUI:
1473 case Instruction::FPToSI:
1474 case Instruction::SIToFP:
1475 case Instruction::UIToFP:
1476 break; // Can't work with floating point.
1477 case Instruction::PtrToInt:
1478 case Instruction::PtrToAddr:
1479 case Instruction::IntToPtr:
1480 // Fall through and handle them the same as zext/trunc.
1481 [[fallthrough]];
1482 case Instruction::ZExt:
1483 case Instruction::Trunc: {
1484 Type *SrcTy = I->getOperand(0)->getType();
1485
1486 unsigned SrcBitWidth;
1487 // Note that we handle pointer operands here because of inttoptr/ptrtoint
1488 // which fall through here.
1489 Type *ScalarTy = SrcTy->getScalarType();
1490 SrcBitWidth = ScalarTy->isPointerTy() ?
1491 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
1492 Q.DL.getTypeSizeInBits(ScalarTy);
1493
1494 assert(SrcBitWidth && "SrcBitWidth can't be zero");
1495 Known = Known.anyextOrTrunc(SrcBitWidth);
1496 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1497 if (auto *Inst = dyn_cast<PossiblyNonNegInst>(I);
1498 Inst && Inst->hasNonNeg() && !Known.isNegative())
1499 Known.makeNonNegative();
1500 Known = Known.zextOrTrunc(BitWidth);
1501 break;
1502 }
1503 case Instruction::BitCast: {
1504 Type *SrcTy = I->getOperand(0)->getType();
1505 if (SrcTy->isIntOrPtrTy() &&
1506 // TODO: For now, not handling conversions like:
1507 // (bitcast i64 %x to <2 x i32>)
1508 !I->getType()->isVectorTy()) {
1509 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1510 break;
1511 }
1512
1513 const Value *V;
1514 // Handle bitcast from floating point to integer.
1515 if (match(I, m_ElementWiseBitCast(m_Value(V))) &&
1516 V->getType()->isFPOrFPVectorTy()) {
1517 Type *FPType = V->getType()->getScalarType();
1518 KnownFPClass Result =
1519 computeKnownFPClass(V, DemandedElts, fcAllFlags, Q, Depth + 1);
1520 FPClassTest FPClasses = Result.KnownFPClasses;
1521
1522 // TODO: Treat it as zero/poison if the use of I is unreachable.
1523 if (FPClasses == fcNone)
1524 break;
1525
1526 if (Result.isKnownNever(fcNormal | fcSubnormal | fcNan)) {
1527 Known.setAllConflict();
1528
1529 if (FPClasses & fcInf)
1531 APFloat::getInf(FPType->getFltSemantics()).bitcastToAPInt()));
1532
1533 if (FPClasses & fcZero)
1535 APInt::getZero(FPType->getScalarSizeInBits())));
1536
1537 Known.Zero.clearSignBit();
1538 Known.One.clearSignBit();
1539 }
1540
1541 if (Result.SignBit) {
1542 if (*Result.SignBit)
1543 Known.makeNegative();
1544 else
1545 Known.makeNonNegative();
1546 }
1547
1548 break;
1549 }
1550
1551 // Handle cast from vector integer type to scalar or vector integer.
1552 auto *SrcVecTy = dyn_cast<FixedVectorType>(SrcTy);
1553 if (!SrcVecTy || !SrcVecTy->getElementType()->isIntegerTy() ||
1554 !I->getType()->isIntOrIntVectorTy() ||
1555 isa<ScalableVectorType>(I->getType()))
1556 break;
1557
1558 unsigned NumElts = DemandedElts.getBitWidth();
1559 bool IsLE = Q.DL.isLittleEndian();
1560 // Look through a cast from narrow vector elements to wider type.
1561 // Examples: v4i32 -> v2i64, v3i8 -> v24
1562 unsigned SubBitWidth = SrcVecTy->getScalarSizeInBits();
1563 if (BitWidth % SubBitWidth == 0) {
1564 // Known bits are automatically intersected across demanded elements of a
1565 // vector. So for example, if a bit is computed as known zero, it must be
1566 // zero across all demanded elements of the vector.
1567 //
1568 // For this bitcast, each demanded element of the output is sub-divided
1569 // across a set of smaller vector elements in the source vector. To get
1570 // the known bits for an entire element of the output, compute the known
1571 // bits for each sub-element sequentially. This is done by shifting the
1572 // one-set-bit demanded elements parameter across the sub-elements for
1573 // consecutive calls to computeKnownBits. We are using the demanded
1574 // elements parameter as a mask operator.
1575 //
1576 // The known bits of each sub-element are then inserted into place
1577 // (dependent on endian) to form the full result of known bits.
1578 unsigned SubScale = BitWidth / SubBitWidth;
1579 APInt SubDemandedElts = APInt::getZero(NumElts * SubScale);
1580 for (unsigned i = 0; i != NumElts; ++i) {
1581 if (DemandedElts[i])
1582 SubDemandedElts.setBit(i * SubScale);
1583 }
1584
1585 KnownBits KnownSrc(SubBitWidth);
1586 for (unsigned i = 0; i != SubScale; ++i) {
1587 computeKnownBits(I->getOperand(0), SubDemandedElts.shl(i), KnownSrc, Q,
1588 Depth + 1);
1589 unsigned ShiftElt = IsLE ? i : SubScale - 1 - i;
1590 Known.insertBits(KnownSrc, ShiftElt * SubBitWidth);
1591 }
1592 }
1593 // Look through a cast from wider vector elements to narrow type.
1594 // Examples: v2i64 -> v4i32
1595 if (SubBitWidth % BitWidth == 0) {
1596 unsigned SubScale = SubBitWidth / BitWidth;
1597 KnownBits KnownSrc(SubBitWidth);
1598 APInt SubDemandedElts =
1599 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
1600 computeKnownBits(I->getOperand(0), SubDemandedElts, KnownSrc, Q,
1601 Depth + 1);
1602
1603 Known.setAllConflict();
1604 for (unsigned i = 0; i != NumElts; ++i) {
1605 if (DemandedElts[i]) {
1606 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
1607 unsigned Offset = (Shifts % SubScale) * BitWidth;
1608 Known = Known.intersectWith(KnownSrc.extractBits(BitWidth, Offset));
1609 if (Known.isUnknown())
1610 break;
1611 }
1612 }
1613 }
1614 break;
1615 }
1616 case Instruction::SExt: {
1617 // Compute the bits in the result that are not present in the input.
1618 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
1619
1620 Known = Known.trunc(SrcBitWidth);
1621 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1622 // If the sign bit of the input is known set or clear, then we know the
1623 // top bits of the result.
1624 Known = Known.sext(BitWidth);
1625 break;
1626 }
1627 case Instruction::Shl: {
1630 auto KF = [NUW, NSW](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1631 bool ShAmtNonZero) {
1632 return KnownBits::shl(KnownVal, KnownAmt, NUW, NSW, ShAmtNonZero);
1633 };
1634 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1635 KF);
1636 // Trailing zeros of a right-shifted constant never decrease.
1637 const APInt *C;
1638 if (match(I->getOperand(0), m_APInt(C)))
1639 Known.Zero.setLowBits(C->countr_zero());
1640 break;
1641 }
1642 case Instruction::LShr: {
1643 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1644 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1645 bool ShAmtNonZero) {
1646 return KnownBits::lshr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1647 };
1648 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1649 KF);
1650 // Leading zeros of a left-shifted constant never decrease.
1651 const APInt *C;
1652 if (match(I->getOperand(0), m_APInt(C)))
1653 Known.Zero.setHighBits(C->countl_zero());
1654 break;
1655 }
1656 case Instruction::AShr: {
1657 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1658 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1659 bool ShAmtNonZero) {
1660 return KnownBits::ashr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1661 };
1662 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1663 KF);
1664 break;
1665 }
1666 case Instruction::Sub: {
1669 computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW, NUW,
1670 DemandedElts, Known, Known2, Q, Depth);
1671 break;
1672 }
1673 case Instruction::Add: {
1676 computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW, NUW,
1677 DemandedElts, Known, Known2, Q, Depth);
1678 break;
1679 }
1680 case Instruction::SRem:
1681 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1682 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1683 Known = KnownBits::srem(Known, Known2);
1684 break;
1685
1686 case Instruction::URem:
1687 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1688 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1689 Known = KnownBits::urem(Known, Known2);
1690 break;
1691 case Instruction::Alloca:
1693 break;
1694 case Instruction::GetElementPtr: {
1695 // Analyze all of the subscripts of this getelementptr instruction
1696 // to determine if we can prove known low zero bits.
1697 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1698 // Accumulate the constant indices in a separate variable
1699 // to minimize the number of calls to computeForAddSub.
1700 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(I->getType());
1701 APInt AccConstIndices(IndexWidth, 0);
1702
1703 auto AddIndexToKnown = [&](KnownBits IndexBits) {
1704 if (IndexWidth == BitWidth) {
1705 // Note that inbounds does *not* guarantee nsw for the addition, as only
1706 // the offset is signed, while the base address is unsigned.
1707 Known = KnownBits::add(Known, IndexBits);
1708 } else {
1709 // If the index width is smaller than the pointer width, only add the
1710 // value to the low bits.
1711 assert(IndexWidth < BitWidth &&
1712 "Index width can't be larger than pointer width");
1713 Known.insertBits(KnownBits::add(Known.trunc(IndexWidth), IndexBits), 0);
1714 }
1715 };
1716
1718 for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
1719 // TrailZ can only become smaller, short-circuit if we hit zero.
1720 if (Known.isUnknown())
1721 break;
1722
1723 Value *Index = I->getOperand(i);
1724
1725 // Handle case when index is zero.
1726 Constant *CIndex = dyn_cast<Constant>(Index);
1727 if (CIndex && CIndex->isNullValue())
1728 continue;
1729
1730 if (StructType *STy = GTI.getStructTypeOrNull()) {
1731 // Handle struct member offset arithmetic.
1732
1733 assert(CIndex &&
1734 "Access to structure field must be known at compile time");
1735
1736 if (CIndex->getType()->isVectorTy())
1737 Index = CIndex->getSplatValue();
1738
1739 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
1740 const StructLayout *SL = Q.DL.getStructLayout(STy);
1741 uint64_t Offset = SL->getElementOffset(Idx);
1742 AccConstIndices += Offset;
1743 continue;
1744 }
1745
1746 // Handle array index arithmetic.
1747 Type *IndexedTy = GTI.getIndexedType();
1748 if (!IndexedTy->isSized()) {
1749 Known.resetAll();
1750 break;
1751 }
1752
1753 TypeSize Stride = GTI.getSequentialElementStride(Q.DL);
1754 uint64_t StrideInBytes = Stride.getKnownMinValue();
1755 if (!Stride.isScalable()) {
1756 // Fast path for constant offset.
1757 if (auto *CI = dyn_cast<ConstantInt>(Index)) {
1758 AccConstIndices +=
1759 CI->getValue().sextOrTrunc(IndexWidth) * StrideInBytes;
1760 continue;
1761 }
1762 }
1763
1764 KnownBits IndexBits =
1765 computeKnownBits(Index, Q, Depth + 1).sextOrTrunc(IndexWidth);
1766 KnownBits ScalingFactor(IndexWidth);
1767 // Multiply by current sizeof type.
1768 // &A[i] == A + i * sizeof(*A[i]).
1769 if (Stride.isScalable()) {
1770 // For scalable types the only thing we know about sizeof is
1771 // that this is a multiple of the minimum size.
1772 ScalingFactor.Zero.setLowBits(llvm::countr_zero(StrideInBytes));
1773 } else {
1774 ScalingFactor =
1775 KnownBits::makeConstant(APInt(IndexWidth, StrideInBytes));
1776 }
1777 AddIndexToKnown(KnownBits::mul(IndexBits, ScalingFactor));
1778 }
1779 if (!Known.isUnknown() && !AccConstIndices.isZero())
1780 AddIndexToKnown(KnownBits::makeConstant(AccConstIndices));
1781 break;
1782 }
1783 case Instruction::PHI: {
1784 const PHINode *P = cast<PHINode>(I);
1785 BinaryOperator *BO = nullptr;
1786 Value *R = nullptr, *L = nullptr;
1787 if (matchSimpleRecurrence(P, BO, R, L)) {
1788 // Handle the case of a simple two-predecessor recurrence PHI.
1789 // There's a lot more that could theoretically be done here, but
1790 // this is sufficient to catch some interesting cases.
1791 unsigned Opcode = BO->getOpcode();
1792
1793 switch (Opcode) {
1794 // If this is a shift recurrence, we know the bits being shifted in. We
1795 // can combine that with information about the start value of the
1796 // recurrence to conclude facts about the result. If this is a udiv
1797 // recurrence, we know that the result can never exceed either the
1798 // numerator or the start value, whichever is greater.
1799 case Instruction::LShr:
1800 case Instruction::AShr:
1801 case Instruction::Shl:
1802 case Instruction::UDiv:
1803 if (BO->getOperand(0) != I)
1804 break;
1805 [[fallthrough]];
1806
1807 // For a urem recurrence, the result can never exceed the start value. The
1808 // phi could either be the numerator or the denominator.
1809 case Instruction::URem: {
1810 // We have matched a recurrence of the form:
1811 // %iv = [R, %entry], [%iv.next, %backedge]
1812 // %iv.next = shift_op %iv, L
1813
1814 // Recurse with the phi context to avoid concern about whether facts
1815 // inferred hold at original context instruction. TODO: It may be
1816 // correct to use the original context. IF warranted, explore and
1817 // add sufficient tests to cover.
1819 RecQ.CxtI = P;
1820 computeKnownBits(R, DemandedElts, Known2, RecQ, Depth + 1);
1821 switch (Opcode) {
1822 case Instruction::Shl:
1823 // A shl recurrence will only increase the tailing zeros
1824 Known.Zero.setLowBits(Known2.countMinTrailingZeros());
1825 break;
1826 case Instruction::LShr:
1827 case Instruction::UDiv:
1828 case Instruction::URem:
1829 // lshr, udiv, and urem recurrences will preserve the leading zeros of
1830 // the start value.
1831 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1832 break;
1833 case Instruction::AShr:
1834 // An ashr recurrence will extend the initial sign bit
1835 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1836 Known.One.setHighBits(Known2.countMinLeadingOnes());
1837 break;
1838 }
1839 break;
1840 }
1841
1842 // Check for operations that have the property that if
1843 // both their operands have low zero bits, the result
1844 // will have low zero bits.
1845 case Instruction::Add:
1846 case Instruction::Sub:
1847 case Instruction::And:
1848 case Instruction::Or:
1849 case Instruction::Mul: {
1850 // Change the context instruction to the "edge" that flows into the
1851 // phi. This is important because that is where the value is actually
1852 // "evaluated" even though it is used later somewhere else. (see also
1853 // D69571).
1855
1856 unsigned OpNum = P->getOperand(0) == R ? 0 : 1;
1857 Instruction *RInst = P->getIncomingBlock(OpNum)->getTerminator();
1858 Instruction *LInst = P->getIncomingBlock(1 - OpNum)->getTerminator();
1859
1860 // Ok, we have a PHI of the form L op= R. Check for low
1861 // zero bits.
1862 RecQ.CxtI = RInst;
1863 computeKnownBits(R, DemandedElts, Known2, RecQ, Depth + 1);
1864
1865 // We need to take the minimum number of known bits
1866 KnownBits Known3(BitWidth);
1867 RecQ.CxtI = LInst;
1868 computeKnownBits(L, DemandedElts, Known3, RecQ, Depth + 1);
1869
1870 Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(),
1871 Known3.countMinTrailingZeros()));
1872
1873 auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(BO);
1874 if (!OverflowOp || !Q.IIQ.hasNoSignedWrap(OverflowOp))
1875 break;
1876
1877 switch (Opcode) {
1878 // If initial value of recurrence is nonnegative, and we are adding
1879 // a nonnegative number with nsw, the result can only be nonnegative
1880 // or poison value regardless of the number of times we execute the
1881 // add in phi recurrence. If initial value is negative and we are
1882 // adding a negative number with nsw, the result can only be
1883 // negative or poison value. Similar arguments apply to sub and mul.
1884 //
1885 // (add non-negative, non-negative) --> non-negative
1886 // (add negative, negative) --> negative
1887 case Instruction::Add: {
1888 if (Known2.isNonNegative() && Known3.isNonNegative())
1889 Known.makeNonNegative();
1890 else if (Known2.isNegative() && Known3.isNegative())
1891 Known.makeNegative();
1892 break;
1893 }
1894
1895 // (sub nsw non-negative, negative) --> non-negative
1896 // (sub nsw negative, non-negative) --> negative
1897 case Instruction::Sub: {
1898 if (BO->getOperand(0) != I)
1899 break;
1900 if (Known2.isNonNegative() && Known3.isNegative())
1901 Known.makeNonNegative();
1902 else if (Known2.isNegative() && Known3.isNonNegative())
1903 Known.makeNegative();
1904 break;
1905 }
1906
1907 // (mul nsw non-negative, non-negative) --> non-negative
1908 case Instruction::Mul:
1909 if (Known2.isNonNegative() && Known3.isNonNegative())
1910 Known.makeNonNegative();
1911 break;
1912
1913 default:
1914 break;
1915 }
1916 break;
1917 }
1918
1919 default:
1920 break;
1921 }
1922 }
1923
1924 // Unreachable blocks may have zero-operand PHI nodes.
1925 if (P->getNumIncomingValues() == 0)
1926 break;
1927
1928 // Otherwise take the unions of the known bit sets of the operands,
1929 // taking conservative care to avoid excessive recursion.
1930 if (Depth < MaxAnalysisRecursionDepth - 1 && Known.isUnknown()) {
1931 // Skip if every incoming value references to ourself.
1932 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
1933 break;
1934
1935 Known.setAllConflict();
1936 for (const Use &U : P->operands()) {
1937 Value *IncValue;
1938 const PHINode *CxtPhi;
1939 Instruction *CxtI;
1940 breakSelfRecursivePHI(&U, P, IncValue, CxtI, &CxtPhi);
1941 // Skip direct self references.
1942 if (IncValue == P)
1943 continue;
1944
1945 // Change the context instruction to the "edge" that flows into the
1946 // phi. This is important because that is where the value is actually
1947 // "evaluated" even though it is used later somewhere else. (see also
1948 // D69571).
1950
1951 Known2 = KnownBits(BitWidth);
1952
1953 // Recurse, but cap the recursion to one level, because we don't
1954 // want to waste time spinning around in loops.
1955 // TODO: See if we can base recursion limiter on number of incoming phi
1956 // edges so we don't overly clamp analysis.
1957 computeKnownBits(IncValue, DemandedElts, Known2, RecQ,
1959
1960 // See if we can further use a conditional branch into the phi
1961 // to help us determine the range of the value.
1962 if (!Known2.isConstant()) {
1963 CmpPredicate Pred;
1964 const APInt *RHSC;
1965 BasicBlock *TrueSucc, *FalseSucc;
1966 // TODO: Use RHS Value and compute range from its known bits.
1967 if (match(RecQ.CxtI,
1968 m_Br(m_c_ICmp(Pred, m_Specific(IncValue), m_APInt(RHSC)),
1969 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
1970 // Check for cases of duplicate successors.
1971 if ((TrueSucc == CxtPhi->getParent()) !=
1972 (FalseSucc == CxtPhi->getParent())) {
1973 // If we're using the false successor, invert the predicate.
1974 if (FalseSucc == CxtPhi->getParent())
1975 Pred = CmpInst::getInversePredicate(Pred);
1976 // Get the knownbits implied by the incoming phi condition.
1977 auto CR = ConstantRange::makeExactICmpRegion(Pred, *RHSC);
1978 KnownBits KnownUnion = Known2.unionWith(CR.toKnownBits());
1979 // We can have conflicts here if we are analyzing deadcode (its
1980 // impossible for us reach this BB based the icmp).
1981 if (KnownUnion.hasConflict()) {
1982 // No reason to continue analyzing in a known dead region, so
1983 // just resetAll and break. This will cause us to also exit the
1984 // outer loop.
1985 Known.resetAll();
1986 break;
1987 }
1988 Known2 = KnownUnion;
1989 }
1990 }
1991 }
1992
1993 Known = Known.intersectWith(Known2);
1994 // If all bits have been ruled out, there's no need to check
1995 // more operands.
1996 if (Known.isUnknown())
1997 break;
1998 }
1999 }
2000 break;
2001 }
2002 case Instruction::Call:
2003 case Instruction::Invoke: {
2004 // If range metadata is attached to this call, set known bits from that,
2005 // and then intersect with known bits based on other properties of the
2006 // function.
2007 if (MDNode *MD =
2008 Q.IIQ.getMetadata(cast<Instruction>(I), LLVMContext::MD_range))
2010
2011 const auto *CB = cast<CallBase>(I);
2012
2013 if (std::optional<ConstantRange> Range = CB->getRange())
2014 Known = Known.unionWith(Range->toKnownBits());
2015
2016 if (const Value *RV = CB->getReturnedArgOperand()) {
2017 if (RV->getType() == I->getType()) {
2018 computeKnownBits(RV, Known2, Q, Depth + 1);
2019 Known = Known.unionWith(Known2);
2020 // If the function doesn't return properly for all input values
2021 // (e.g. unreachable exits) then there might be conflicts between the
2022 // argument value and the range metadata. Simply discard the known bits
2023 // in case of conflicts.
2024 if (Known.hasConflict())
2025 Known.resetAll();
2026 }
2027 }
2028 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
2029 switch (II->getIntrinsicID()) {
2030 default:
2031 break;
2032 case Intrinsic::abs: {
2033 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2034 bool IntMinIsPoison = match(II->getArgOperand(1), m_One());
2035 Known = Known.unionWith(Known2.abs(IntMinIsPoison));
2036 break;
2037 }
2038 case Intrinsic::bitreverse:
2039 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2040 Known = Known.unionWith(Known2.reverseBits());
2041 break;
2042 case Intrinsic::bswap:
2043 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2044 Known = Known.unionWith(Known2.byteSwap());
2045 break;
2046 case Intrinsic::ctlz: {
2047 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2048 // If we have a known 1, its position is our upper bound.
2049 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
2050 // If this call is poison for 0 input, the result will be less than 2^n.
2051 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
2052 PossibleLZ = std::min(PossibleLZ, BitWidth - 1);
2053 unsigned LowBits = llvm::bit_width(PossibleLZ);
2054 Known.Zero.setBitsFrom(LowBits);
2055 break;
2056 }
2057 case Intrinsic::cttz: {
2058 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2059 // If we have a known 1, its position is our upper bound.
2060 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
2061 // If this call is poison for 0 input, the result will be less than 2^n.
2062 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
2063 PossibleTZ = std::min(PossibleTZ, BitWidth - 1);
2064 unsigned LowBits = llvm::bit_width(PossibleTZ);
2065 Known.Zero.setBitsFrom(LowBits);
2066 break;
2067 }
2068 case Intrinsic::ctpop: {
2069 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2070 // We can bound the space the count needs. Also, bits known to be zero
2071 // can't contribute to the population.
2072 unsigned BitsPossiblySet = Known2.countMaxPopulation();
2073 unsigned LowBits = llvm::bit_width(BitsPossiblySet);
2074 Known.Zero.setBitsFrom(LowBits);
2075 // TODO: we could bound KnownOne using the lower bound on the number
2076 // of bits which might be set provided by popcnt KnownOne2.
2077 break;
2078 }
2079 case Intrinsic::fshr:
2080 case Intrinsic::fshl: {
2081 const APInt *SA;
2082 if (!match(I->getOperand(2), m_APInt(SA)))
2083 break;
2084
2085 // Normalize to funnel shift left.
2086 uint64_t ShiftAmt = SA->urem(BitWidth);
2087 if (II->getIntrinsicID() == Intrinsic::fshr)
2088 ShiftAmt = BitWidth - ShiftAmt;
2089
2090 KnownBits Known3(BitWidth);
2091 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2092 computeKnownBits(I->getOperand(1), DemandedElts, Known3, Q, Depth + 1);
2093
2094 Known2 <<= ShiftAmt;
2095 Known3 >>= BitWidth - ShiftAmt;
2096 Known = Known2.unionWith(Known3);
2097 break;
2098 }
2099 case Intrinsic::clmul:
2100 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2101 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2102 Known = KnownBits::clmul(Known, Known2);
2103 break;
2104 case Intrinsic::uadd_sat:
2105 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2106 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2107 Known = KnownBits::uadd_sat(Known, Known2);
2108 break;
2109 case Intrinsic::usub_sat:
2110 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2111 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2112 Known = KnownBits::usub_sat(Known, Known2);
2113 break;
2114 case Intrinsic::sadd_sat:
2115 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2116 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2117 Known = KnownBits::sadd_sat(Known, Known2);
2118 break;
2119 case Intrinsic::ssub_sat:
2120 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2121 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2122 Known = KnownBits::ssub_sat(Known, Known2);
2123 break;
2124 // Vec reverse preserves bits from input vec.
2125 case Intrinsic::vector_reverse:
2126 computeKnownBits(I->getOperand(0), DemandedElts.reverseBits(), Known, Q,
2127 Depth + 1);
2128 break;
2129 // for min/max/and/or reduce, any bit common to each element in the
2130 // input vec is set in the output.
2131 case Intrinsic::vector_reduce_and:
2132 case Intrinsic::vector_reduce_or:
2133 case Intrinsic::vector_reduce_umax:
2134 case Intrinsic::vector_reduce_umin:
2135 case Intrinsic::vector_reduce_smax:
2136 case Intrinsic::vector_reduce_smin:
2137 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2138 break;
2139 case Intrinsic::vector_reduce_xor: {
2140 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2141 // The zeros common to all vecs are zero in the output.
2142 // If the number of elements is odd, then the common ones remain. If the
2143 // number of elements is even, then the common ones becomes zeros.
2144 auto *VecTy = cast<VectorType>(I->getOperand(0)->getType());
2145 // Even, so the ones become zeros.
2146 bool EvenCnt = VecTy->getElementCount().isKnownEven();
2147 if (EvenCnt)
2148 Known.Zero |= Known.One;
2149 // Maybe even element count so need to clear ones.
2150 if (VecTy->isScalableTy() || EvenCnt)
2151 Known.One.clearAllBits();
2152 break;
2153 }
2154 case Intrinsic::vector_reduce_add: {
2155 auto *VecTy = dyn_cast<FixedVectorType>(I->getOperand(0)->getType());
2156 if (!VecTy)
2157 break;
2158 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2159 Known = Known.reduceAdd(VecTy->getNumElements());
2160 break;
2161 }
2162 case Intrinsic::umin:
2163 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2164 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2165 Known = KnownBits::umin(Known, Known2);
2166 break;
2167 case Intrinsic::umax:
2168 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2169 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2170 Known = KnownBits::umax(Known, Known2);
2171 break;
2172 case Intrinsic::smin:
2173 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2174 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2175 Known = KnownBits::smin(Known, Known2);
2177 break;
2178 case Intrinsic::smax:
2179 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2180 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2181 Known = KnownBits::smax(Known, Known2);
2183 break;
2184 case Intrinsic::ptrmask: {
2185 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2186
2187 const Value *Mask = I->getOperand(1);
2188 Known2 = KnownBits(Mask->getType()->getScalarSizeInBits());
2189 computeKnownBits(Mask, DemandedElts, Known2, Q, Depth + 1);
2190 // TODO: 1-extend would be more precise.
2191 Known &= Known2.anyextOrTrunc(BitWidth);
2192 break;
2193 }
2194 case Intrinsic::x86_sse2_pmulh_w:
2195 case Intrinsic::x86_avx2_pmulh_w:
2196 case Intrinsic::x86_avx512_pmulh_w_512:
2197 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2198 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2199 Known = KnownBits::mulhs(Known, Known2);
2200 break;
2201 case Intrinsic::x86_sse2_pmulhu_w:
2202 case Intrinsic::x86_avx2_pmulhu_w:
2203 case Intrinsic::x86_avx512_pmulhu_w_512:
2204 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2205 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2206 Known = KnownBits::mulhu(Known, Known2);
2207 break;
2208 case Intrinsic::x86_sse42_crc32_64_64:
2209 Known.Zero.setBitsFrom(32);
2210 break;
2211 case Intrinsic::x86_ssse3_phadd_d_128:
2212 case Intrinsic::x86_ssse3_phadd_w_128:
2213 case Intrinsic::x86_avx2_phadd_d:
2214 case Intrinsic::x86_avx2_phadd_w: {
2216 I, DemandedElts, Q, Depth,
2217 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2218 return KnownBits::add(KnownLHS, KnownRHS);
2219 });
2220 break;
2221 }
2222 case Intrinsic::x86_ssse3_phadd_sw_128:
2223 case Intrinsic::x86_avx2_phadd_sw: {
2225 I, DemandedElts, Q, Depth, KnownBits::sadd_sat);
2226 break;
2227 }
2228 case Intrinsic::x86_ssse3_phsub_d_128:
2229 case Intrinsic::x86_ssse3_phsub_w_128:
2230 case Intrinsic::x86_avx2_phsub_d:
2231 case Intrinsic::x86_avx2_phsub_w: {
2233 I, DemandedElts, Q, Depth,
2234 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2235 return KnownBits::sub(KnownLHS, KnownRHS);
2236 });
2237 break;
2238 }
2239 case Intrinsic::x86_ssse3_phsub_sw_128:
2240 case Intrinsic::x86_avx2_phsub_sw: {
2242 I, DemandedElts, Q, Depth, KnownBits::ssub_sat);
2243 break;
2244 }
2245 case Intrinsic::riscv_vsetvli:
2246 case Intrinsic::riscv_vsetvlimax: {
2247 bool HasAVL = II->getIntrinsicID() == Intrinsic::riscv_vsetvli;
2248 const ConstantRange Range = getVScaleRange(II->getFunction(), BitWidth);
2250 cast<ConstantInt>(II->getArgOperand(HasAVL))->getZExtValue());
2251 RISCVVType::VLMUL VLMUL = static_cast<RISCVVType::VLMUL>(
2252 cast<ConstantInt>(II->getArgOperand(1 + HasAVL))->getZExtValue());
2253 uint64_t MaxVLEN =
2254 Range.getUnsignedMax().getZExtValue() * RISCV::RVVBitsPerBlock;
2255 uint64_t MaxVL = MaxVLEN / RISCVVType::getSEWLMULRatio(SEW, VLMUL);
2256
2257 // Result of vsetvli must be not larger than AVL.
2258 if (HasAVL)
2259 if (auto *CI = dyn_cast<ConstantInt>(II->getArgOperand(0)))
2260 MaxVL = std::min(MaxVL, CI->getZExtValue());
2261
2262 unsigned KnownZeroFirstBit = Log2_32(MaxVL) + 1;
2263 if (BitWidth > KnownZeroFirstBit)
2264 Known.Zero.setBitsFrom(KnownZeroFirstBit);
2265 break;
2266 }
2267 case Intrinsic::amdgcn_mbcnt_hi:
2268 case Intrinsic::amdgcn_mbcnt_lo: {
2269 // Wave64 mbcnt_lo returns at most 32 + src1. Otherwise these return at
2270 // most 31 + src1.
2271 Known.Zero.setBitsFrom(
2272 II->getIntrinsicID() == Intrinsic::amdgcn_mbcnt_lo ? 6 : 5);
2273 computeKnownBits(I->getOperand(1), Known2, Q, Depth + 1);
2274 Known = KnownBits::add(Known, Known2);
2275 break;
2276 }
2277 case Intrinsic::vscale: {
2278 if (!II->getParent() || !II->getFunction())
2279 break;
2280
2281 Known = getVScaleRange(II->getFunction(), BitWidth).toKnownBits();
2282 break;
2283 }
2284 }
2285 }
2286 break;
2287 }
2288 case Instruction::ShuffleVector: {
2289 if (auto *Splat = getSplatValue(I)) {
2290 computeKnownBits(Splat, Known, Q, Depth + 1);
2291 break;
2292 }
2293
2294 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
2295 // FIXME: Do we need to handle ConstantExpr involving shufflevectors?
2296 if (!Shuf) {
2297 Known.resetAll();
2298 return;
2299 }
2300 // For undef elements, we don't know anything about the common state of
2301 // the shuffle result.
2302 APInt DemandedLHS, DemandedRHS;
2303 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) {
2304 Known.resetAll();
2305 return;
2306 }
2307 Known.setAllConflict();
2308 if (!!DemandedLHS) {
2309 const Value *LHS = Shuf->getOperand(0);
2310 computeKnownBits(LHS, DemandedLHS, Known, Q, Depth + 1);
2311 // If we don't know any bits, early out.
2312 if (Known.isUnknown())
2313 break;
2314 }
2315 if (!!DemandedRHS) {
2316 const Value *RHS = Shuf->getOperand(1);
2317 computeKnownBits(RHS, DemandedRHS, Known2, Q, Depth + 1);
2318 Known = Known.intersectWith(Known2);
2319 }
2320 break;
2321 }
2322 case Instruction::InsertElement: {
2323 if (isa<ScalableVectorType>(I->getType())) {
2324 Known.resetAll();
2325 return;
2326 }
2327 const Value *Vec = I->getOperand(0);
2328 const Value *Elt = I->getOperand(1);
2329 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
2330 unsigned NumElts = DemandedElts.getBitWidth();
2331 APInt DemandedVecElts = DemandedElts;
2332 bool NeedsElt = true;
2333 // If we know the index we are inserting too, clear it from Vec check.
2334 if (CIdx && CIdx->getValue().ult(NumElts)) {
2335 DemandedVecElts.clearBit(CIdx->getZExtValue());
2336 NeedsElt = DemandedElts[CIdx->getZExtValue()];
2337 }
2338
2339 Known.setAllConflict();
2340 if (NeedsElt) {
2341 computeKnownBits(Elt, Known, Q, Depth + 1);
2342 // If we don't know any bits, early out.
2343 if (Known.isUnknown())
2344 break;
2345 }
2346
2347 if (!DemandedVecElts.isZero()) {
2348 computeKnownBits(Vec, DemandedVecElts, Known2, Q, Depth + 1);
2349 Known = Known.intersectWith(Known2);
2350 }
2351 break;
2352 }
2353 case Instruction::ExtractElement: {
2354 // Look through extract element. If the index is non-constant or
2355 // out-of-range demand all elements, otherwise just the extracted element.
2356 const Value *Vec = I->getOperand(0);
2357 const Value *Idx = I->getOperand(1);
2358 auto *CIdx = dyn_cast<ConstantInt>(Idx);
2359 if (isa<ScalableVectorType>(Vec->getType())) {
2360 // FIXME: there's probably *something* we can do with scalable vectors
2361 Known.resetAll();
2362 break;
2363 }
2364 unsigned NumElts = cast<FixedVectorType>(Vec->getType())->getNumElements();
2365 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
2366 if (CIdx && CIdx->getValue().ult(NumElts))
2367 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
2368 computeKnownBits(Vec, DemandedVecElts, Known, Q, Depth + 1);
2369 break;
2370 }
2371 case Instruction::ExtractValue:
2372 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
2374 if (EVI->getNumIndices() != 1) break;
2375 if (EVI->getIndices()[0] == 0) {
2376 switch (II->getIntrinsicID()) {
2377 default: break;
2378 case Intrinsic::uadd_with_overflow:
2379 case Intrinsic::sadd_with_overflow:
2381 true, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2382 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2383 break;
2384 case Intrinsic::usub_with_overflow:
2385 case Intrinsic::ssub_with_overflow:
2387 false, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2388 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2389 break;
2390 case Intrinsic::umul_with_overflow:
2391 case Intrinsic::smul_with_overflow:
2392 computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false,
2393 false, DemandedElts, Known, Known2, Q, Depth);
2394 break;
2395 }
2396 }
2397 }
2398 break;
2399 case Instruction::Freeze:
2400 if (isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
2401 Depth + 1))
2402 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2403 break;
2404 }
2405}
2406
2407/// Determine which bits of V are known to be either zero or one and return
2408/// them.
2409KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
2410 const SimplifyQuery &Q, unsigned Depth) {
2411 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2412 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);
2413 return Known;
2414}
2415
2416/// Determine which bits of V are known to be either zero or one and return
2417/// them.
2419 unsigned Depth) {
2420 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2421 computeKnownBits(V, Known, Q, Depth);
2422 return Known;
2423}
2424
2425/// Determine which bits of V are known to be either zero or one and return
2426/// them in the Known bit set.
2427///
2428/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that
2429/// we cannot optimize based on the assumption that it is zero without changing
2430/// it to be an explicit zero. If we don't change it to zero, other code could
2431/// optimized based on the contradictory assumption that it is non-zero.
2432/// Because instcombine aggressively folds operations with undef args anyway,
2433/// this won't lose us code quality.
2434///
2435/// This function is defined on values with integer type, values with pointer
2436/// type, and vectors of integers. In the case
2437/// where V is a vector, known zero, and known one values are the
2438/// same width as the vector element, and the bit is set only if it is true
2439/// for all of the demanded elements in the vector specified by DemandedElts.
2440void computeKnownBits(const Value *V, const APInt &DemandedElts,
2441 KnownBits &Known, const SimplifyQuery &Q,
2442 unsigned Depth) {
2443 if (!DemandedElts) {
2444 // No demanded elts, better to assume we don't know anything.
2445 Known.resetAll();
2446 return;
2447 }
2448
2449 assert(V && "No Value?");
2450 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2451
2452#ifndef NDEBUG
2453 Type *Ty = V->getType();
2454 unsigned BitWidth = Known.getBitWidth();
2455
2456 assert((Ty->isIntOrIntVectorTy(BitWidth) || Ty->isPtrOrPtrVectorTy()) &&
2457 "Not integer or pointer type!");
2458
2459 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
2460 assert(
2461 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
2462 "DemandedElt width should equal the fixed vector number of elements");
2463 } else {
2464 assert(DemandedElts == APInt(1, 1) &&
2465 "DemandedElt width should be 1 for scalars or scalable vectors");
2466 }
2467
2468 Type *ScalarTy = Ty->getScalarType();
2469 if (ScalarTy->isPointerTy()) {
2470 assert(BitWidth == Q.DL.getPointerTypeSizeInBits(ScalarTy) &&
2471 "V and Known should have same BitWidth");
2472 } else {
2473 assert(BitWidth == Q.DL.getTypeSizeInBits(ScalarTy) &&
2474 "V and Known should have same BitWidth");
2475 }
2476#endif
2477
2478 const APInt *C;
2479 if (match(V, m_APInt(C))) {
2480 // We know all of the bits for a scalar constant or a splat vector constant!
2481 Known = KnownBits::makeConstant(*C);
2482 return;
2483 }
2484 // Null and aggregate-zero are all-zeros.
2486 Known.setAllZero();
2487 return;
2488 }
2489 // Handle a constant vector by taking the intersection of the known bits of
2490 // each element.
2492 assert(!isa<ScalableVectorType>(V->getType()));
2493 // We know that CDV must be a vector of integers. Take the intersection of
2494 // each element.
2495 Known.setAllConflict();
2496 for (unsigned i = 0, e = CDV->getNumElements(); i != e; ++i) {
2497 if (!DemandedElts[i])
2498 continue;
2499 APInt Elt = CDV->getElementAsAPInt(i);
2500 Known.Zero &= ~Elt;
2501 Known.One &= Elt;
2502 }
2503 if (Known.hasConflict())
2504 Known.resetAll();
2505 return;
2506 }
2507
2508 if (const auto *CV = dyn_cast<ConstantVector>(V)) {
2509 assert(!isa<ScalableVectorType>(V->getType()));
2510 // We know that CV must be a vector of integers. Take the intersection of
2511 // each element.
2512 Known.setAllConflict();
2513 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
2514 if (!DemandedElts[i])
2515 continue;
2516 Constant *Element = CV->getAggregateElement(i);
2517 if (isa<PoisonValue>(Element))
2518 continue;
2519 auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
2520 if (!ElementCI) {
2521 Known.resetAll();
2522 return;
2523 }
2524 const APInt &Elt = ElementCI->getValue();
2525 Known.Zero &= ~Elt;
2526 Known.One &= Elt;
2527 }
2528 if (Known.hasConflict())
2529 Known.resetAll();
2530 return;
2531 }
2532
2533 // Start out not knowing anything.
2534 Known.resetAll();
2535
2536 // We can't imply anything about undefs.
2537 if (isa<UndefValue>(V))
2538 return;
2539
2540 // There's no point in looking through other users of ConstantData for
2541 // assumptions. Confirm that we've handled them all.
2542 assert(!isa<ConstantData>(V) && "Unhandled constant data!");
2543
2544 if (const auto *A = dyn_cast<Argument>(V))
2545 if (std::optional<ConstantRange> Range = A->getRange())
2546 Known = Range->toKnownBits();
2547
2548 // All recursive calls that increase depth must come after this.
2550 return;
2551
2552 // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
2553 // the bits of its aliasee.
2554 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
2555 if (!GA->isInterposable())
2556 computeKnownBits(GA->getAliasee(), Known, Q, Depth + 1);
2557 return;
2558 }
2559
2560 if (const Operator *I = dyn_cast<Operator>(V))
2561 computeKnownBitsFromOperator(I, DemandedElts, Known, Q, Depth);
2562 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2563 if (std::optional<ConstantRange> CR = GV->getAbsoluteSymbolRange())
2564 Known = CR->toKnownBits();
2565 }
2566
2567 // Aligned pointers have trailing zeros - refine Known.Zero set
2568 if (isa<PointerType>(V->getType())) {
2569 Align Alignment = V->getPointerAlignment(Q.DL);
2570 Known.Zero.setLowBits(Log2(Alignment));
2571 }
2572
2573 // computeKnownBitsFromContext strictly refines Known.
2574 // Therefore, we run them after computeKnownBitsFromOperator.
2575
2576 // Check whether we can determine known bits from context such as assumes.
2577 computeKnownBitsFromContext(V, Known, Q, Depth);
2578}
2579
2580/// Try to detect a recurrence that the value of the induction variable is
2581/// always a power of two (or zero).
2582static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero,
2583 SimplifyQuery &Q, unsigned Depth) {
2584 BinaryOperator *BO = nullptr;
2585 Value *Start = nullptr, *Step = nullptr;
2586 if (!matchSimpleRecurrence(PN, BO, Start, Step))
2587 return false;
2588
2589 // Initial value must be a power of two.
2590 for (const Use &U : PN->operands()) {
2591 if (U.get() == Start) {
2592 // Initial value comes from a different BB, need to adjust context
2593 // instruction for analysis.
2594 Q.CxtI = PN->getIncomingBlock(U)->getTerminator();
2595 if (!isKnownToBeAPowerOfTwo(Start, OrZero, Q, Depth))
2596 return false;
2597 }
2598 }
2599
2600 // Except for Mul, the induction variable must be on the left side of the
2601 // increment expression, otherwise its value can be arbitrary.
2602 if (BO->getOpcode() != Instruction::Mul && BO->getOperand(1) != Step)
2603 return false;
2604
2605 Q.CxtI = BO->getParent()->getTerminator();
2606 switch (BO->getOpcode()) {
2607 case Instruction::Mul:
2608 // Power of two is closed under multiplication.
2609 return (OrZero || Q.IIQ.hasNoUnsignedWrap(BO) ||
2610 Q.IIQ.hasNoSignedWrap(BO)) &&
2611 isKnownToBeAPowerOfTwo(Step, OrZero, Q, Depth);
2612 case Instruction::SDiv:
2613 // Start value must not be signmask for signed division, so simply being a
2614 // power of two is not sufficient, and it has to be a constant.
2615 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2616 return false;
2617 [[fallthrough]];
2618 case Instruction::UDiv:
2619 // Divisor must be a power of two.
2620 // If OrZero is false, cannot guarantee induction variable is non-zero after
2621 // division, same for Shr, unless it is exact division.
2622 return (OrZero || Q.IIQ.isExact(BO)) &&
2623 isKnownToBeAPowerOfTwo(Step, false, Q, Depth);
2624 case Instruction::Shl:
2625 return OrZero || Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO);
2626 case Instruction::AShr:
2627 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2628 return false;
2629 [[fallthrough]];
2630 case Instruction::LShr:
2631 return OrZero || Q.IIQ.isExact(BO);
2632 default:
2633 return false;
2634 }
2635}
2636
2637/// Return true if we can infer that \p V is known to be a power of 2 from
2638/// dominating condition \p Cond (e.g., ctpop(V) == 1).
2639static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero,
2640 const Value *Cond,
2641 bool CondIsTrue) {
2642 CmpPredicate Pred;
2643 const APInt *RHSC;
2645 m_APInt(RHSC))))
2646 return false;
2647 if (!CondIsTrue)
2648 Pred = ICmpInst::getInversePredicate(Pred);
2649 // ctpop(V) u< 2
2650 if (OrZero && Pred == ICmpInst::ICMP_ULT && *RHSC == 2)
2651 return true;
2652 // ctpop(V) == 1
2653 return Pred == ICmpInst::ICMP_EQ && *RHSC == 1;
2654}
2655
2656/// Return true if the given value is known to have exactly one
2657/// bit set when defined. For vectors return true if every element is known to
2658/// be a power of two when defined. Supports values with integer or pointer
2659/// types and vectors of integers.
2660bool llvm::isKnownToBeAPowerOfTwo(const Value *V, bool OrZero,
2661 const SimplifyQuery &Q, unsigned Depth) {
2662 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2663
2664 if (isa<Constant>(V))
2665 return OrZero ? match(V, m_Power2OrZero()) : match(V, m_Power2());
2666
2667 // i1 is by definition a power of 2 or zero.
2668 if (OrZero && V->getType()->getScalarSizeInBits() == 1)
2669 return true;
2670
2671 // Try to infer from assumptions.
2672 if (Q.AC && Q.CxtI) {
2673 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
2674 if (!AssumeVH)
2675 continue;
2676 CallInst *I = cast<CallInst>(AssumeVH);
2677 if (isImpliedToBeAPowerOfTwoFromCond(V, OrZero, I->getArgOperand(0),
2678 /*CondIsTrue=*/true) &&
2680 return true;
2681 }
2682 }
2683
2684 // Handle dominating conditions.
2685 if (Q.DC && Q.CxtI && Q.DT) {
2686 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
2687 Value *Cond = BI->getCondition();
2688
2689 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
2691 /*CondIsTrue=*/true) &&
2692 Q.DT->dominates(Edge0, Q.CxtI->getParent()))
2693 return true;
2694
2695 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
2697 /*CondIsTrue=*/false) &&
2698 Q.DT->dominates(Edge1, Q.CxtI->getParent()))
2699 return true;
2700 }
2701 }
2702
2703 auto *I = dyn_cast<Instruction>(V);
2704 if (!I)
2705 return false;
2706
2707 if (Q.CxtI && match(V, m_VScale())) {
2708 const Function *F = Q.CxtI->getFunction();
2709 // The vscale_range indicates vscale is a power-of-two.
2710 return F->hasFnAttribute(Attribute::VScaleRange);
2711 }
2712
2713 // 1 << X is clearly a power of two if the one is not shifted off the end. If
2714 // it is shifted off the end then the result is undefined.
2715 if (match(I, m_Shl(m_One(), m_Value())))
2716 return true;
2717
2718 // (signmask) >>l X is clearly a power of two if the one is not shifted off
2719 // the bottom. If it is shifted off the bottom then the result is undefined.
2720 if (match(I, m_LShr(m_SignMask(), m_Value())))
2721 return true;
2722
2723 // The remaining tests are all recursive, so bail out if we hit the limit.
2725 return false;
2726
2727 switch (I->getOpcode()) {
2728 case Instruction::ZExt:
2729 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2730 case Instruction::Trunc:
2731 return OrZero && isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2732 case Instruction::Shl:
2733 if (OrZero || Q.IIQ.hasNoUnsignedWrap(I) || Q.IIQ.hasNoSignedWrap(I))
2734 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2735 return false;
2736 case Instruction::LShr:
2737 if (OrZero || Q.IIQ.isExact(cast<BinaryOperator>(I)))
2738 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2739 return false;
2740 case Instruction::UDiv:
2742 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2743 return false;
2744 case Instruction::Mul:
2745 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
2746 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth) &&
2747 (OrZero || isKnownNonZero(I, Q, Depth));
2748 case Instruction::And:
2749 // A power of two and'd with anything is a power of two or zero.
2750 if (OrZero &&
2751 (isKnownToBeAPowerOfTwo(I->getOperand(1), /*OrZero*/ true, Q, Depth) ||
2752 isKnownToBeAPowerOfTwo(I->getOperand(0), /*OrZero*/ true, Q, Depth)))
2753 return true;
2754 // X & (-X) is always a power of two or zero.
2755 if (match(I->getOperand(0), m_Neg(m_Specific(I->getOperand(1)))) ||
2756 match(I->getOperand(1), m_Neg(m_Specific(I->getOperand(0)))))
2757 return OrZero || isKnownNonZero(I->getOperand(0), Q, Depth);
2758 return false;
2759 case Instruction::Add: {
2760 // Adding a power-of-two or zero to the same power-of-two or zero yields
2761 // either the original power-of-two, a larger power-of-two or zero.
2763 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) ||
2764 Q.IIQ.hasNoSignedWrap(VOBO)) {
2765 if (match(I->getOperand(0),
2766 m_c_And(m_Specific(I->getOperand(1)), m_Value())) &&
2767 isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth))
2768 return true;
2769 if (match(I->getOperand(1),
2770 m_c_And(m_Specific(I->getOperand(0)), m_Value())) &&
2771 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth))
2772 return true;
2773
2774 unsigned BitWidth = V->getType()->getScalarSizeInBits();
2775 KnownBits LHSBits(BitWidth);
2776 computeKnownBits(I->getOperand(0), LHSBits, Q, Depth);
2777
2778 KnownBits RHSBits(BitWidth);
2779 computeKnownBits(I->getOperand(1), RHSBits, Q, Depth);
2780 // If i8 V is a power of two or zero:
2781 // ZeroBits: 1 1 1 0 1 1 1 1
2782 // ~ZeroBits: 0 0 0 1 0 0 0 0
2783 if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2())
2784 // If OrZero isn't set, we cannot give back a zero result.
2785 // Make sure either the LHS or RHS has a bit set.
2786 if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue())
2787 return true;
2788 }
2789
2790 // LShr(UINT_MAX, Y) + 1 is a power of two (if add is nuw) or zero.
2791 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO))
2792 if (match(I, m_Add(m_LShr(m_AllOnes(), m_Value()), m_One())))
2793 return true;
2794 return false;
2795 }
2796 case Instruction::Select:
2797 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
2798 isKnownToBeAPowerOfTwo(I->getOperand(2), OrZero, Q, Depth);
2799 case Instruction::PHI: {
2800 // A PHI node is power of two if all incoming values are power of two, or if
2801 // it is an induction variable where in each step its value is a power of
2802 // two.
2803 auto *PN = cast<PHINode>(I);
2805
2806 // Check if it is an induction variable and always power of two.
2807 if (isPowerOfTwoRecurrence(PN, OrZero, RecQ, Depth))
2808 return true;
2809
2810 // Recursively check all incoming values. Limit recursion to 2 levels, so
2811 // that search complexity is limited to number of operands^2.
2812 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
2813 return llvm::all_of(PN->operands(), [&](const Use &U) {
2814 // Value is power of 2 if it is coming from PHI node itself by induction.
2815 if (U.get() == PN)
2816 return true;
2817
2818 // Change the context instruction to the incoming block where it is
2819 // evaluated.
2820 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
2821 return isKnownToBeAPowerOfTwo(U.get(), OrZero, RecQ, NewDepth);
2822 });
2823 }
2824 case Instruction::Invoke:
2825 case Instruction::Call: {
2826 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
2827 switch (II->getIntrinsicID()) {
2828 case Intrinsic::umax:
2829 case Intrinsic::smax:
2830 case Intrinsic::umin:
2831 case Intrinsic::smin:
2832 return isKnownToBeAPowerOfTwo(II->getArgOperand(1), OrZero, Q, Depth) &&
2833 isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2834 // bswap/bitreverse just move around bits, but don't change any 1s/0s
2835 // thus dont change pow2/non-pow2 status.
2836 case Intrinsic::bitreverse:
2837 case Intrinsic::bswap:
2838 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2839 case Intrinsic::fshr:
2840 case Intrinsic::fshl:
2841 // If Op0 == Op1, this is a rotate. is_pow2(rotate(x, y)) == is_pow2(x)
2842 if (II->getArgOperand(0) == II->getArgOperand(1))
2843 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2844 break;
2845 default:
2846 break;
2847 }
2848 }
2849 return false;
2850 }
2851 default:
2852 return false;
2853 }
2854}
2855
2856/// Test whether a GEP's result is known to be non-null.
2857///
2858/// Uses properties inherent in a GEP to try to determine whether it is known
2859/// to be non-null.
2860///
2861/// Currently this routine does not support vector GEPs.
2862static bool isGEPKnownNonNull(const GEPOperator *GEP, const SimplifyQuery &Q,
2863 unsigned Depth) {
2864 const Function *F = nullptr;
2865 if (const Instruction *I = dyn_cast<Instruction>(GEP))
2866 F = I->getFunction();
2867
2868 // If the gep is nuw or inbounds with invalid null pointer, then the GEP
2869 // may be null iff the base pointer is null and the offset is zero.
2870 if (!GEP->hasNoUnsignedWrap() &&
2871 !(GEP->isInBounds() &&
2872 !NullPointerIsDefined(F, GEP->getPointerAddressSpace())))
2873 return false;
2874
2875 // FIXME: Support vector-GEPs.
2876 assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP");
2877
2878 // If the base pointer is non-null, we cannot walk to a null address with an
2879 // inbounds GEP in address space zero.
2880 if (isKnownNonZero(GEP->getPointerOperand(), Q, Depth))
2881 return true;
2882
2883 // Walk the GEP operands and see if any operand introduces a non-zero offset.
2884 // If so, then the GEP cannot produce a null pointer, as doing so would
2885 // inherently violate the inbounds contract within address space zero.
2887 GTI != GTE; ++GTI) {
2888 // Struct types are easy -- they must always be indexed by a constant.
2889 if (StructType *STy = GTI.getStructTypeOrNull()) {
2890 ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
2891 unsigned ElementIdx = OpC->getZExtValue();
2892 const StructLayout *SL = Q.DL.getStructLayout(STy);
2893 uint64_t ElementOffset = SL->getElementOffset(ElementIdx);
2894 if (ElementOffset > 0)
2895 return true;
2896 continue;
2897 }
2898
2899 // If we have a zero-sized type, the index doesn't matter. Keep looping.
2900 if (GTI.getSequentialElementStride(Q.DL).isZero())
2901 continue;
2902
2903 // Fast path the constant operand case both for efficiency and so we don't
2904 // increment Depth when just zipping down an all-constant GEP.
2905 if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
2906 if (!OpC->isZero())
2907 return true;
2908 continue;
2909 }
2910
2911 // We post-increment Depth here because while isKnownNonZero increments it
2912 // as well, when we pop back up that increment won't persist. We don't want
2913 // to recurse 10k times just because we have 10k GEP operands. We don't
2914 // bail completely out because we want to handle constant GEPs regardless
2915 // of depth.
2917 continue;
2918
2919 if (isKnownNonZero(GTI.getOperand(), Q, Depth))
2920 return true;
2921 }
2922
2923 return false;
2924}
2925
2927 const Instruction *CtxI,
2928 const DominatorTree *DT) {
2929 assert(!isa<Constant>(V) && "Called for constant?");
2930
2931 if (!CtxI || !DT)
2932 return false;
2933
2934 unsigned NumUsesExplored = 0;
2935 for (auto &U : V->uses()) {
2936 // Avoid massive lists
2937 if (NumUsesExplored >= DomConditionsMaxUses)
2938 break;
2939 NumUsesExplored++;
2940
2941 const Instruction *UI = cast<Instruction>(U.getUser());
2942 // If the value is used as an argument to a call or invoke, then argument
2943 // attributes may provide an answer about null-ness.
2944 if (V->getType()->isPointerTy()) {
2945 if (const auto *CB = dyn_cast<CallBase>(UI)) {
2946 if (CB->isArgOperand(&U) &&
2947 CB->paramHasNonNullAttr(CB->getArgOperandNo(&U),
2948 /*AllowUndefOrPoison=*/false) &&
2949 DT->dominates(CB, CtxI))
2950 return true;
2951 }
2952 }
2953
2954 // If the value is used as a load/store, then the pointer must be non null.
2955 if (V == getLoadStorePointerOperand(UI)) {
2958 DT->dominates(UI, CtxI))
2959 return true;
2960 }
2961
2962 if ((match(UI, m_IDiv(m_Value(), m_Specific(V))) ||
2963 match(UI, m_IRem(m_Value(), m_Specific(V)))) &&
2964 isValidAssumeForContext(UI, CtxI, DT))
2965 return true;
2966
2967 // Consider only compare instructions uniquely controlling a branch
2968 Value *RHS;
2969 CmpPredicate Pred;
2970 if (!match(UI, m_c_ICmp(Pred, m_Specific(V), m_Value(RHS))))
2971 continue;
2972
2973 bool NonNullIfTrue;
2974 if (cmpExcludesZero(Pred, RHS))
2975 NonNullIfTrue = true;
2977 NonNullIfTrue = false;
2978 else
2979 continue;
2980
2983 for (const auto *CmpU : UI->users()) {
2984 assert(WorkList.empty() && "Should be!");
2985 if (Visited.insert(CmpU).second)
2986 WorkList.push_back(CmpU);
2987
2988 while (!WorkList.empty()) {
2989 auto *Curr = WorkList.pop_back_val();
2990
2991 // If a user is an AND, add all its users to the work list. We only
2992 // propagate "pred != null" condition through AND because it is only
2993 // correct to assume that all conditions of AND are met in true branch.
2994 // TODO: Support similar logic of OR and EQ predicate?
2995 if (NonNullIfTrue)
2996 if (match(Curr, m_LogicalAnd(m_Value(), m_Value()))) {
2997 for (const auto *CurrU : Curr->users())
2998 if (Visited.insert(CurrU).second)
2999 WorkList.push_back(CurrU);
3000 continue;
3001 }
3002
3003 if (const CondBrInst *BI = dyn_cast<CondBrInst>(Curr)) {
3004 BasicBlock *NonNullSuccessor =
3005 BI->getSuccessor(NonNullIfTrue ? 0 : 1);
3006 BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor);
3007 if (DT->dominates(Edge, CtxI->getParent()))
3008 return true;
3009 } else if (NonNullIfTrue && isGuard(Curr) &&
3010 DT->dominates(cast<Instruction>(Curr), CtxI)) {
3011 return true;
3012 }
3013 }
3014 }
3015 }
3016
3017 return false;
3018}
3019
3020/// Does the 'Range' metadata (which must be a valid MD_range operand list)
3021/// ensure that the value it's attached to is never Value? 'RangeType' is
3022/// is the type of the value described by the range.
3023static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) {
3024 const unsigned NumRanges = Ranges->getNumOperands() / 2;
3025 assert(NumRanges >= 1);
3026 for (unsigned i = 0; i < NumRanges; ++i) {
3028 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
3030 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
3031 ConstantRange Range(Lower->getValue(), Upper->getValue());
3032 if (Range.contains(Value))
3033 return false;
3034 }
3035 return true;
3036}
3037
3038/// Try to detect a recurrence that monotonically increases/decreases from a
3039/// non-zero starting value. These are common as induction variables.
3040static bool isNonZeroRecurrence(const PHINode *PN) {
3041 BinaryOperator *BO = nullptr;
3042 Value *Start = nullptr, *Step = nullptr;
3043 const APInt *StartC, *StepC;
3044 if (!matchSimpleRecurrence(PN, BO, Start, Step) ||
3045 !match(Start, m_APInt(StartC)) || StartC->isZero())
3046 return false;
3047
3048 switch (BO->getOpcode()) {
3049 case Instruction::Add:
3050 // Starting from non-zero and stepping away from zero can never wrap back
3051 // to zero.
3052 return BO->hasNoUnsignedWrap() ||
3053 (BO->hasNoSignedWrap() && match(Step, m_APInt(StepC)) &&
3054 StartC->isNegative() == StepC->isNegative());
3055 case Instruction::Mul:
3056 return (BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap()) &&
3057 match(Step, m_APInt(StepC)) && !StepC->isZero();
3058 case Instruction::Shl:
3059 return BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap();
3060 case Instruction::AShr:
3061 case Instruction::LShr:
3062 return BO->isExact();
3063 default:
3064 return false;
3065 }
3066}
3067
3068static bool matchOpWithOpEqZero(Value *Op0, Value *Op1) {
3070 m_Specific(Op1), m_Zero()))) ||
3072 m_Specific(Op0), m_Zero())));
3073}
3074
3075static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q,
3076 unsigned BitWidth, Value *X, Value *Y, bool NSW,
3077 bool NUW, unsigned Depth) {
3078 // (X + (X != 0)) is non zero
3079 if (matchOpWithOpEqZero(X, Y))
3080 return true;
3081
3082 if (NUW)
3083 return isKnownNonZero(Y, DemandedElts, Q, Depth) ||
3084 isKnownNonZero(X, DemandedElts, Q, Depth);
3085
3086 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
3087 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
3088
3089 // If X and Y are both non-negative (as signed values) then their sum is not
3090 // zero unless both X and Y are zero.
3091 if (XKnown.isNonNegative() && YKnown.isNonNegative())
3092 if (isKnownNonZero(Y, DemandedElts, Q, Depth) ||
3093 isKnownNonZero(X, DemandedElts, Q, Depth))
3094 return true;
3095
3096 // If X and Y are both negative (as signed values) then their sum is not
3097 // zero unless both X and Y equal INT_MIN.
3098 if (XKnown.isNegative() && YKnown.isNegative()) {
3100 // The sign bit of X is set. If some other bit is set then X is not equal
3101 // to INT_MIN.
3102 if (XKnown.One.intersects(Mask))
3103 return true;
3104 // The sign bit of Y is set. If some other bit is set then Y is not equal
3105 // to INT_MIN.
3106 if (YKnown.One.intersects(Mask))
3107 return true;
3108 }
3109
3110 // The sum of a non-negative number and a power of two is not zero.
3111 if (XKnown.isNonNegative() &&
3112 isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Q, Depth))
3113 return true;
3114 if (YKnown.isNonNegative() &&
3115 isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Q, Depth))
3116 return true;
3117
3118 return KnownBits::add(XKnown, YKnown, NSW, NUW).isNonZero();
3119}
3120
3121static bool isNonZeroSub(const APInt &DemandedElts, const SimplifyQuery &Q,
3122 unsigned BitWidth, Value *X, Value *Y,
3123 unsigned Depth) {
3124 // (X - (X != 0)) is non zero
3125 // ((X != 0) - X) is non zero
3126 if (matchOpWithOpEqZero(X, Y))
3127 return true;
3128
3129 // TODO: Move this case into isKnownNonEqual().
3130 if (auto *C = dyn_cast<Constant>(X))
3131 if (C->isNullValue() && isKnownNonZero(Y, DemandedElts, Q, Depth))
3132 return true;
3133
3134 return ::isKnownNonEqual(X, Y, DemandedElts, Q, Depth);
3135}
3136
3137static bool isNonZeroMul(const APInt &DemandedElts, const SimplifyQuery &Q,
3138 unsigned BitWidth, Value *X, Value *Y, bool NSW,
3139 bool NUW, unsigned Depth) {
3140 // If X and Y are non-zero then so is X * Y as long as the multiplication
3141 // does not overflow.
3142 if (NSW || NUW)
3143 return isKnownNonZero(X, DemandedElts, Q, Depth) &&
3144 isKnownNonZero(Y, DemandedElts, Q, Depth);
3145
3146 // If either X or Y is odd, then if the other is non-zero the result can't
3147 // be zero.
3148 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
3149 if (XKnown.One[0])
3150 return isKnownNonZero(Y, DemandedElts, Q, Depth);
3151
3152 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
3153 if (YKnown.One[0])
3154 return XKnown.isNonZero() || isKnownNonZero(X, DemandedElts, Q, Depth);
3155
3156 // If there exists any subset of X (sX) and subset of Y (sY) s.t sX * sY is
3157 // non-zero, then X * Y is non-zero. We can find sX and sY by just taking
3158 // the lowest known One of X and Y. If they are non-zero, the result
3159 // must be non-zero. We can check if LSB(X) * LSB(Y) != 0 by doing
3160 // X.CountLeadingZeros + Y.CountLeadingZeros < BitWidth.
3161 return (XKnown.countMaxTrailingZeros() + YKnown.countMaxTrailingZeros()) <
3162 BitWidth;
3163}
3164
3165static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts,
3166 const SimplifyQuery &Q, const KnownBits &KnownVal,
3167 unsigned Depth) {
3168 auto ShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
3169 switch (I->getOpcode()) {
3170 case Instruction::Shl:
3171 return Lhs.shl(Rhs);
3172 case Instruction::LShr:
3173 return Lhs.lshr(Rhs);
3174 case Instruction::AShr:
3175 return Lhs.ashr(Rhs);
3176 default:
3177 llvm_unreachable("Unknown Shift Opcode");
3178 }
3179 };
3180
3181 auto InvShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
3182 switch (I->getOpcode()) {
3183 case Instruction::Shl:
3184 return Lhs.lshr(Rhs);
3185 case Instruction::LShr:
3186 case Instruction::AShr:
3187 return Lhs.shl(Rhs);
3188 default:
3189 llvm_unreachable("Unknown Shift Opcode");
3190 }
3191 };
3192
3193 if (KnownVal.isUnknown())
3194 return false;
3195
3196 KnownBits KnownCnt =
3197 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
3198 APInt MaxShift = KnownCnt.getMaxValue();
3199 unsigned NumBits = KnownVal.getBitWidth();
3200 if (MaxShift.uge(NumBits))
3201 return false;
3202
3203 if (!ShiftOp(KnownVal.One, MaxShift).isZero())
3204 return true;
3205
3206 // If all of the bits shifted out are known to be zero, and Val is known
3207 // non-zero then at least one non-zero bit must remain.
3208 if (InvShiftOp(KnownVal.Zero, NumBits - MaxShift)
3209 .eq(InvShiftOp(APInt::getAllOnes(NumBits), NumBits - MaxShift)) &&
3210 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth))
3211 return true;
3212
3213 return false;
3214}
3215
3217 const APInt &DemandedElts,
3218 const SimplifyQuery &Q, unsigned Depth) {
3219 unsigned BitWidth = getBitWidth(I->getType()->getScalarType(), Q.DL);
3220 switch (I->getOpcode()) {
3221 case Instruction::Alloca:
3222 // Alloca never returns null, malloc might.
3223 return I->getType()->getPointerAddressSpace() == 0;
3224 case Instruction::GetElementPtr:
3225 if (I->getType()->isPointerTy())
3227 break;
3228 case Instruction::BitCast: {
3229 // We need to be a bit careful here. We can only peek through the bitcast
3230 // if the scalar size of elements in the operand are smaller than and a
3231 // multiple of the size they are casting too. Take three cases:
3232 //
3233 // 1) Unsafe:
3234 // bitcast <2 x i16> %NonZero to <4 x i8>
3235 //
3236 // %NonZero can have 2 non-zero i16 elements, but isKnownNonZero on a
3237 // <4 x i8> requires that all 4 i8 elements be non-zero which isn't
3238 // guranteed (imagine just sign bit set in the 2 i16 elements).
3239 //
3240 // 2) Unsafe:
3241 // bitcast <4 x i3> %NonZero to <3 x i4>
3242 //
3243 // Even though the scalar size of the src (`i3`) is smaller than the
3244 // scalar size of the dst `i4`, because `i3` is not a multiple of `i4`
3245 // its possible for the `3 x i4` elements to be zero because there are
3246 // some elements in the destination that don't contain any full src
3247 // element.
3248 //
3249 // 3) Safe:
3250 // bitcast <4 x i8> %NonZero to <2 x i16>
3251 //
3252 // This is always safe as non-zero in the 4 i8 elements implies
3253 // non-zero in the combination of any two adjacent ones. Since i8 is a
3254 // multiple of i16, each i16 is guranteed to have 2 full i8 elements.
3255 // This all implies the 2 i16 elements are non-zero.
3256 Type *FromTy = I->getOperand(0)->getType();
3257 if ((FromTy->isIntOrIntVectorTy() || FromTy->isPtrOrPtrVectorTy()) &&
3258 (BitWidth % getBitWidth(FromTy->getScalarType(), Q.DL)) == 0)
3259 return isKnownNonZero(I->getOperand(0), Q, Depth);
3260 } break;
3261 case Instruction::IntToPtr:
3262 // Note that we have to take special care to avoid looking through
3263 // truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well
3264 // as casts that can alter the value, e.g., AddrSpaceCasts.
3265 if (!isa<ScalableVectorType>(I->getType()) &&
3266 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
3267 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
3268 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3269 break;
3270 case Instruction::PtrToAddr:
3271 // isKnownNonZero() for pointers refers to the address bits being non-zero,
3272 // so we can directly forward.
3273 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3274 case Instruction::PtrToInt:
3275 // For inttoptr, make sure the result size is >= the address size. If the
3276 // address is non-zero, any larger value is also non-zero.
3277 if (Q.DL.getAddressSizeInBits(I->getOperand(0)->getType()) <=
3278 I->getType()->getScalarSizeInBits())
3279 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3280 break;
3281 case Instruction::Trunc:
3282 // nuw/nsw trunc preserves zero/non-zero status of input.
3283 if (auto *TI = dyn_cast<TruncInst>(I))
3284 if (TI->hasNoSignedWrap() || TI->hasNoUnsignedWrap())
3285 return isKnownNonZero(TI->getOperand(0), DemandedElts, Q, Depth);
3286 break;
3287
3288 // Iff x - y != 0, then x ^ y != 0
3289 // Therefore we can do the same exact checks
3290 case Instruction::Xor:
3291 case Instruction::Sub:
3292 return isNonZeroSub(DemandedElts, Q, BitWidth, I->getOperand(0),
3293 I->getOperand(1), Depth);
3294 case Instruction::Or:
3295 // (X | (X != 0)) is non zero
3296 if (matchOpWithOpEqZero(I->getOperand(0), I->getOperand(1)))
3297 return true;
3298 // X | Y != 0 if X != Y.
3299 if (isKnownNonEqual(I->getOperand(0), I->getOperand(1), DemandedElts, Q,
3300 Depth))
3301 return true;
3302 // X | Y != 0 if X != 0 or Y != 0.
3303 return isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth) ||
3304 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3305 case Instruction::SExt:
3306 case Instruction::ZExt:
3307 // ext X != 0 if X != 0.
3308 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3309
3310 case Instruction::Shl: {
3311 // shl nsw/nuw can't remove any non-zero bits.
3313 if (Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO))
3314 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3315
3316 // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
3317 // if the lowest bit is shifted off the end.
3318 KnownBits Known(BitWidth);
3319 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth);
3320 if (Known.One[0])
3321 return true;
3322
3323 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3324 }
3325 case Instruction::LShr:
3326 case Instruction::AShr: {
3327 // shr exact can only shift out zero bits.
3329 if (BO->isExact())
3330 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3331
3332 // shr X, Y != 0 if X is negative. Note that the value of the shift is not
3333 // defined if the sign bit is shifted off the end.
3334 KnownBits Known =
3335 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3336 if (Known.isNegative())
3337 return true;
3338
3339 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3340 }
3341 case Instruction::UDiv:
3342 case Instruction::SDiv: {
3343 // X / Y
3344 // div exact can only produce a zero if the dividend is zero.
3345 if (cast<PossiblyExactOperator>(I)->isExact())
3346 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3347
3348 KnownBits XKnown =
3349 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3350 // If X is fully unknown we won't be able to figure anything out so don't
3351 // both computing knownbits for Y.
3352 if (XKnown.isUnknown())
3353 return false;
3354
3355 KnownBits YKnown =
3356 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
3357 if (I->getOpcode() == Instruction::SDiv) {
3358 // For signed division need to compare abs value of the operands.
3359 XKnown = XKnown.abs(/*IntMinIsPoison*/ false);
3360 YKnown = YKnown.abs(/*IntMinIsPoison*/ false);
3361 }
3362 // If X u>= Y then div is non zero (0/0 is UB).
3363 std::optional<bool> XUgeY = KnownBits::uge(XKnown, YKnown);
3364 // If X is total unknown or X u< Y we won't be able to prove non-zero
3365 // with compute known bits so just return early.
3366 return XUgeY && *XUgeY;
3367 }
3368 case Instruction::Add: {
3369 // X + Y.
3370
3371 // If Add has nuw wrap flag, then if either X or Y is non-zero the result is
3372 // non-zero.
3374 return isNonZeroAdd(DemandedElts, Q, BitWidth, I->getOperand(0),
3375 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3376 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3377 }
3378 case Instruction::Mul: {
3380 return isNonZeroMul(DemandedElts, Q, BitWidth, I->getOperand(0),
3381 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3382 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3383 }
3384 case Instruction::Select: {
3385 // (C ? X : Y) != 0 if X != 0 and Y != 0.
3386
3387 // First check if the arm is non-zero using `isKnownNonZero`. If that fails,
3388 // then see if the select condition implies the arm is non-zero. For example
3389 // (X != 0 ? X : Y), we know the true arm is non-zero as the `X` "return" is
3390 // dominated by `X != 0`.
3391 auto SelectArmIsNonZero = [&](bool IsTrueArm) {
3392 Value *Op;
3393 Op = IsTrueArm ? I->getOperand(1) : I->getOperand(2);
3394 // Op is trivially non-zero.
3395 if (isKnownNonZero(Op, DemandedElts, Q, Depth))
3396 return true;
3397
3398 // The condition of the select dominates the true/false arm. Check if the
3399 // condition implies that a given arm is non-zero.
3400 Value *X;
3401 CmpPredicate Pred;
3402 if (!match(I->getOperand(0), m_c_ICmp(Pred, m_Specific(Op), m_Value(X))))
3403 return false;
3404
3405 if (!IsTrueArm)
3406 Pred = ICmpInst::getInversePredicate(Pred);
3407
3408 return cmpExcludesZero(Pred, X);
3409 };
3410
3411 if (SelectArmIsNonZero(/* IsTrueArm */ true) &&
3412 SelectArmIsNonZero(/* IsTrueArm */ false))
3413 return true;
3414 break;
3415 }
3416 case Instruction::PHI: {
3417 auto *PN = cast<PHINode>(I);
3419 return true;
3420
3421 // Check if all incoming values are non-zero using recursion.
3423 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
3424 return llvm::all_of(PN->operands(), [&](const Use &U) {
3425 if (U.get() == PN)
3426 return true;
3427 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
3428 // Check if the branch on the phi excludes zero.
3429 CmpPredicate Pred;
3430 Value *X;
3431 BasicBlock *TrueSucc, *FalseSucc;
3432 if (match(RecQ.CxtI,
3433 m_Br(m_c_ICmp(Pred, m_Specific(U.get()), m_Value(X)),
3434 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
3435 // Check for cases of duplicate successors.
3436 if ((TrueSucc == PN->getParent()) != (FalseSucc == PN->getParent())) {
3437 // If we're using the false successor, invert the predicate.
3438 if (FalseSucc == PN->getParent())
3439 Pred = CmpInst::getInversePredicate(Pred);
3440 if (cmpExcludesZero(Pred, X))
3441 return true;
3442 }
3443 }
3444 // Finally recurse on the edge and check it directly.
3445 return isKnownNonZero(U.get(), DemandedElts, RecQ, NewDepth);
3446 });
3447 }
3448 case Instruction::InsertElement: {
3449 if (isa<ScalableVectorType>(I->getType()))
3450 break;
3451
3452 const Value *Vec = I->getOperand(0);
3453 const Value *Elt = I->getOperand(1);
3454 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
3455
3456 unsigned NumElts = DemandedElts.getBitWidth();
3457 APInt DemandedVecElts = DemandedElts;
3458 bool SkipElt = false;
3459 // If we know the index we are inserting too, clear it from Vec check.
3460 if (CIdx && CIdx->getValue().ult(NumElts)) {
3461 DemandedVecElts.clearBit(CIdx->getZExtValue());
3462 SkipElt = !DemandedElts[CIdx->getZExtValue()];
3463 }
3464
3465 // Result is zero if Elt is non-zero and rest of the demanded elts in Vec
3466 // are non-zero.
3467 return (SkipElt || isKnownNonZero(Elt, Q, Depth)) &&
3468 (DemandedVecElts.isZero() ||
3469 isKnownNonZero(Vec, DemandedVecElts, Q, Depth));
3470 }
3471 case Instruction::ExtractElement:
3472 if (const auto *EEI = dyn_cast<ExtractElementInst>(I)) {
3473 const Value *Vec = EEI->getVectorOperand();
3474 const Value *Idx = EEI->getIndexOperand();
3475 auto *CIdx = dyn_cast<ConstantInt>(Idx);
3476 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
3477 unsigned NumElts = VecTy->getNumElements();
3478 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
3479 if (CIdx && CIdx->getValue().ult(NumElts))
3480 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
3481 return isKnownNonZero(Vec, DemandedVecElts, Q, Depth);
3482 }
3483 }
3484 break;
3485 case Instruction::ShuffleVector: {
3486 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
3487 if (!Shuf)
3488 break;
3489 APInt DemandedLHS, DemandedRHS;
3490 // For undef elements, we don't know anything about the common state of
3491 // the shuffle result.
3492 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
3493 break;
3494 // If demanded elements for both vecs are non-zero, the shuffle is non-zero.
3495 return (DemandedRHS.isZero() ||
3496 isKnownNonZero(Shuf->getOperand(1), DemandedRHS, Q, Depth)) &&
3497 (DemandedLHS.isZero() ||
3498 isKnownNonZero(Shuf->getOperand(0), DemandedLHS, Q, Depth));
3499 }
3500 case Instruction::Freeze:
3501 return isKnownNonZero(I->getOperand(0), Q, Depth) &&
3502 isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
3503 Depth);
3504 case Instruction::Load: {
3505 auto *LI = cast<LoadInst>(I);
3506 // A Load tagged with nonnull or dereferenceable with null pointer undefined
3507 // is never null.
3508 if (auto *PtrT = dyn_cast<PointerType>(I->getType())) {
3509 if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull) ||
3510 (Q.IIQ.getMetadata(LI, LLVMContext::MD_dereferenceable) &&
3511 !NullPointerIsDefined(LI->getFunction(), PtrT->getAddressSpace())))
3512 return true;
3513 } else if (MDNode *Ranges = Q.IIQ.getMetadata(LI, LLVMContext::MD_range)) {
3515 }
3516
3517 // No need to fall through to computeKnownBits as range metadata is already
3518 // handled in isKnownNonZero.
3519 return false;
3520 }
3521 case Instruction::ExtractValue: {
3522 const WithOverflowInst *WO;
3524 switch (WO->getBinaryOp()) {
3525 default:
3526 break;
3527 case Instruction::Add:
3528 return isNonZeroAdd(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3529 WO->getArgOperand(1),
3530 /*NSW=*/false,
3531 /*NUW=*/false, Depth);
3532 case Instruction::Sub:
3533 return isNonZeroSub(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3534 WO->getArgOperand(1), Depth);
3535 case Instruction::Mul:
3536 return isNonZeroMul(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3537 WO->getArgOperand(1),
3538 /*NSW=*/false, /*NUW=*/false, Depth);
3539 break;
3540 }
3541 }
3542 break;
3543 }
3544 case Instruction::Call:
3545 case Instruction::Invoke: {
3546 const auto *Call = cast<CallBase>(I);
3547 if (I->getType()->isPointerTy()) {
3548 if (Call->isReturnNonNull())
3549 return true;
3550 if (const auto *RP = getArgumentAliasingToReturnedPointer(Call, true))
3551 return isKnownNonZero(RP, Q, Depth);
3552 } else {
3553 if (MDNode *Ranges = Q.IIQ.getMetadata(Call, LLVMContext::MD_range))
3555 if (std::optional<ConstantRange> Range = Call->getRange()) {
3556 const APInt ZeroValue(Range->getBitWidth(), 0);
3557 if (!Range->contains(ZeroValue))
3558 return true;
3559 }
3560 if (const Value *RV = Call->getReturnedArgOperand())
3561 if (RV->getType() == I->getType() && isKnownNonZero(RV, Q, Depth))
3562 return true;
3563 }
3564
3565 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
3566 switch (II->getIntrinsicID()) {
3567 case Intrinsic::sshl_sat:
3568 case Intrinsic::ushl_sat:
3569 case Intrinsic::abs:
3570 case Intrinsic::bitreverse:
3571 case Intrinsic::bswap:
3572 case Intrinsic::ctpop:
3573 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3574 // NB: We don't do usub_sat here as in any case we can prove its
3575 // non-zero, we will fold it to `sub nuw` in InstCombine.
3576 case Intrinsic::ssub_sat:
3577 // For most types, if x != y then ssub.sat x, y != 0. But
3578 // ssub.sat.i1 0, -1 = 0, because 1 saturates to 0. This means
3579 // isNonZeroSub will do the wrong thing for ssub.sat.i1.
3580 if (BitWidth == 1)
3581 return false;
3582 return isNonZeroSub(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3583 II->getArgOperand(1), Depth);
3584 case Intrinsic::sadd_sat:
3585 return isNonZeroAdd(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3586 II->getArgOperand(1),
3587 /*NSW=*/true, /* NUW=*/false, Depth);
3588 // Vec reverse preserves zero/non-zero status from input vec.
3589 case Intrinsic::vector_reverse:
3590 return isKnownNonZero(II->getArgOperand(0), DemandedElts.reverseBits(),
3591 Q, Depth);
3592 // umin/smin/smax/smin/or of all non-zero elements is always non-zero.
3593 case Intrinsic::vector_reduce_or:
3594 case Intrinsic::vector_reduce_umax:
3595 case Intrinsic::vector_reduce_umin:
3596 case Intrinsic::vector_reduce_smax:
3597 case Intrinsic::vector_reduce_smin:
3598 return isKnownNonZero(II->getArgOperand(0), Q, Depth);
3599 case Intrinsic::umax:
3600 case Intrinsic::uadd_sat:
3601 // umax(X, (X != 0)) is non zero
3602 // X +usat (X != 0) is non zero
3603 if (matchOpWithOpEqZero(II->getArgOperand(0), II->getArgOperand(1)))
3604 return true;
3605
3606 return isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth) ||
3607 isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3608 case Intrinsic::smax: {
3609 // If either arg is strictly positive the result is non-zero. Otherwise
3610 // the result is non-zero if both ops are non-zero.
3611 auto IsNonZero = [&](Value *Op, std::optional<bool> &OpNonZero,
3612 const KnownBits &OpKnown) {
3613 if (!OpNonZero.has_value())
3614 OpNonZero = OpKnown.isNonZero() ||
3615 isKnownNonZero(Op, DemandedElts, Q, Depth);
3616 return *OpNonZero;
3617 };
3618 // Avoid re-computing isKnownNonZero.
3619 std::optional<bool> Op0NonZero, Op1NonZero;
3620 KnownBits Op1Known =
3621 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3622 if (Op1Known.isNonNegative() &&
3623 IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known))
3624 return true;
3625 KnownBits Op0Known =
3626 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3627 if (Op0Known.isNonNegative() &&
3628 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known))
3629 return true;
3630 return IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known) &&
3631 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known);
3632 }
3633 case Intrinsic::smin: {
3634 // If either arg is negative the result is non-zero. Otherwise
3635 // the result is non-zero if both ops are non-zero.
3636 KnownBits Op1Known =
3637 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3638 if (Op1Known.isNegative())
3639 return true;
3640 KnownBits Op0Known =
3641 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3642 if (Op0Known.isNegative())
3643 return true;
3644
3645 if (Op1Known.isNonZero() && Op0Known.isNonZero())
3646 return true;
3647 }
3648 [[fallthrough]];
3649 case Intrinsic::umin:
3650 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth) &&
3651 isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth);
3652 case Intrinsic::cttz:
3653 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3654 .Zero[0];
3655 case Intrinsic::ctlz:
3656 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3657 .isNonNegative();
3658 case Intrinsic::fshr:
3659 case Intrinsic::fshl:
3660 // If Op0 == Op1, this is a rotate. rotate(x, y) != 0 iff x != 0.
3661 if (II->getArgOperand(0) == II->getArgOperand(1))
3662 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3663 break;
3664 case Intrinsic::vscale:
3665 return true;
3666 case Intrinsic::experimental_get_vector_length:
3667 return isKnownNonZero(I->getOperand(0), Q, Depth);
3668 default:
3669 break;
3670 }
3671 break;
3672 }
3673
3674 return false;
3675 }
3676 }
3677
3678 KnownBits Known(BitWidth);
3679 computeKnownBits(I, DemandedElts, Known, Q, Depth);
3680 return Known.One != 0;
3681}
3682
3683/// Return true if the given value is known to be non-zero when defined. For
3684/// vectors, return true if every demanded element is known to be non-zero when
3685/// defined. For pointers, if the context instruction and dominator tree are
3686/// specified, perform context-sensitive analysis and return true if the
3687/// pointer couldn't possibly be null at the specified instruction.
3688/// Supports values with integer or pointer type and vectors of integers.
3689bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
3690 const SimplifyQuery &Q, unsigned Depth) {
3691 Type *Ty = V->getType();
3692
3693#ifndef NDEBUG
3694 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
3695
3696 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3697 assert(
3698 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
3699 "DemandedElt width should equal the fixed vector number of elements");
3700 } else {
3701 assert(DemandedElts == APInt(1, 1) &&
3702 "DemandedElt width should be 1 for scalars");
3703 }
3704#endif
3705
3706 if (auto *C = dyn_cast<Constant>(V)) {
3707 if (C->isNullValue())
3708 return false;
3709 if (isa<ConstantInt>(C))
3710 // Must be non-zero due to null test above.
3711 return true;
3712
3713 // For constant vectors, check that all elements are poison or known
3714 // non-zero to determine that the whole vector is known non-zero.
3715 if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
3716 for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
3717 if (!DemandedElts[i])
3718 continue;
3719 Constant *Elt = C->getAggregateElement(i);
3720 if (!Elt || Elt->isNullValue())
3721 return false;
3722 if (!isa<PoisonValue>(Elt) && !isa<ConstantInt>(Elt))
3723 return false;
3724 }
3725 return true;
3726 }
3727
3728 // Constant ptrauth can be null, iff the base pointer can be.
3729 if (auto *CPA = dyn_cast<ConstantPtrAuth>(V))
3730 return isKnownNonZero(CPA->getPointer(), DemandedElts, Q, Depth);
3731
3732 // A global variable in address space 0 is non null unless extern weak
3733 // or an absolute symbol reference. Other address spaces may have null as a
3734 // valid address for a global, so we can't assume anything.
3735 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
3736 if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&
3737 GV->getType()->getAddressSpace() == 0)
3738 return true;
3739 }
3740
3741 // For constant expressions, fall through to the Operator code below.
3742 if (!isa<ConstantExpr>(V))
3743 return false;
3744 }
3745
3746 if (const auto *A = dyn_cast<Argument>(V))
3747 if (std::optional<ConstantRange> Range = A->getRange()) {
3748 const APInt ZeroValue(Range->getBitWidth(), 0);
3749 if (!Range->contains(ZeroValue))
3750 return true;
3751 }
3752
3753 if (!isa<Constant>(V) && isKnownNonZeroFromAssume(V, Q))
3754 return true;
3755
3756 // Some of the tests below are recursive, so bail out if we hit the limit.
3758 return false;
3759
3760 // Check for pointer simplifications.
3761
3762 if (PointerType *PtrTy = dyn_cast<PointerType>(Ty)) {
3763 // A byval, inalloca may not be null in a non-default addres space. A
3764 // nonnull argument is assumed never 0.
3765 if (const Argument *A = dyn_cast<Argument>(V)) {
3766 if (((A->hasPassPointeeByValueCopyAttr() &&
3767 !NullPointerIsDefined(A->getParent(), PtrTy->getAddressSpace())) ||
3768 A->hasNonNullAttr()))
3769 return true;
3770 }
3771 }
3772
3773 if (const auto *I = dyn_cast<Operator>(V))
3774 if (isKnownNonZeroFromOperator(I, DemandedElts, Q, Depth))
3775 return true;
3776
3777 if (!isa<Constant>(V) &&
3779 return true;
3780
3781 if (const Value *Stripped = stripNullTest(V))
3782 return isKnownNonZero(Stripped, DemandedElts, Q, Depth);
3783
3784 return false;
3785}
3786
3788 unsigned Depth) {
3789 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
3790 APInt DemandedElts =
3791 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
3792 return ::isKnownNonZero(V, DemandedElts, Q, Depth);
3793}
3794
3795/// If the pair of operators are the same invertible function, return the
3796/// the operands of the function corresponding to each input. Otherwise,
3797/// return std::nullopt. An invertible function is one that is 1-to-1 and maps
3798/// every input value to exactly one output value. This is equivalent to
3799/// saying that Op1 and Op2 are equal exactly when the specified pair of
3800/// operands are equal, (except that Op1 and Op2 may be poison more often.)
3801static std::optional<std::pair<Value*, Value*>>
3803 const Operator *Op2) {
3804 if (Op1->getOpcode() != Op2->getOpcode())
3805 return std::nullopt;
3806
3807 auto getOperands = [&](unsigned OpNum) -> auto {
3808 return std::make_pair(Op1->getOperand(OpNum), Op2->getOperand(OpNum));
3809 };
3810
3811 switch (Op1->getOpcode()) {
3812 default:
3813 break;
3814 case Instruction::Or:
3815 if (!cast<PossiblyDisjointInst>(Op1)->isDisjoint() ||
3816 !cast<PossiblyDisjointInst>(Op2)->isDisjoint())
3817 break;
3818 [[fallthrough]];
3819 case Instruction::Xor:
3820 case Instruction::Add: {
3821 Value *Other;
3822 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(0)), m_Value(Other))))
3823 return std::make_pair(Op1->getOperand(1), Other);
3824 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(1)), m_Value(Other))))
3825 return std::make_pair(Op1->getOperand(0), Other);
3826 break;
3827 }
3828 case Instruction::Sub:
3829 if (Op1->getOperand(0) == Op2->getOperand(0))
3830 return getOperands(1);
3831 if (Op1->getOperand(1) == Op2->getOperand(1))
3832 return getOperands(0);
3833 break;
3834 case Instruction::Mul: {
3835 // invertible if A * B == (A * B) mod 2^N where A, and B are integers
3836 // and N is the bitwdith. The nsw case is non-obvious, but proven by
3837 // alive2: https://alive2.llvm.org/ce/z/Z6D5qK
3838 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3839 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3840 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3841 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3842 break;
3843
3844 // Assume operand order has been canonicalized
3845 if (Op1->getOperand(1) == Op2->getOperand(1) &&
3846 isa<ConstantInt>(Op1->getOperand(1)) &&
3847 !cast<ConstantInt>(Op1->getOperand(1))->isZero())
3848 return getOperands(0);
3849 break;
3850 }
3851 case Instruction::Shl: {
3852 // Same as multiplies, with the difference that we don't need to check
3853 // for a non-zero multiply. Shifts always multiply by non-zero.
3854 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3855 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3856 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3857 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3858 break;
3859
3860 if (Op1->getOperand(1) == Op2->getOperand(1))
3861 return getOperands(0);
3862 break;
3863 }
3864 case Instruction::AShr:
3865 case Instruction::LShr: {
3866 auto *PEO1 = cast<PossiblyExactOperator>(Op1);
3867 auto *PEO2 = cast<PossiblyExactOperator>(Op2);
3868 if (!PEO1->isExact() || !PEO2->isExact())
3869 break;
3870
3871 if (Op1->getOperand(1) == Op2->getOperand(1))
3872 return getOperands(0);
3873 break;
3874 }
3875 case Instruction::SExt:
3876 case Instruction::ZExt:
3877 if (Op1->getOperand(0)->getType() == Op2->getOperand(0)->getType())
3878 return getOperands(0);
3879 break;
3880 case Instruction::PHI: {
3881 const PHINode *PN1 = cast<PHINode>(Op1);
3882 const PHINode *PN2 = cast<PHINode>(Op2);
3883
3884 // If PN1 and PN2 are both recurrences, can we prove the entire recurrences
3885 // are a single invertible function of the start values? Note that repeated
3886 // application of an invertible function is also invertible
3887 BinaryOperator *BO1 = nullptr;
3888 Value *Start1 = nullptr, *Step1 = nullptr;
3889 BinaryOperator *BO2 = nullptr;
3890 Value *Start2 = nullptr, *Step2 = nullptr;
3891 if (PN1->getParent() != PN2->getParent() ||
3892 !matchSimpleRecurrence(PN1, BO1, Start1, Step1) ||
3893 !matchSimpleRecurrence(PN2, BO2, Start2, Step2))
3894 break;
3895
3896 auto Values = getInvertibleOperands(cast<Operator>(BO1),
3897 cast<Operator>(BO2));
3898 if (!Values)
3899 break;
3900
3901 // We have to be careful of mutually defined recurrences here. Ex:
3902 // * X_i = X_(i-1) OP Y_(i-1), and Y_i = X_(i-1) OP V
3903 // * X_i = Y_i = X_(i-1) OP Y_(i-1)
3904 // The invertibility of these is complicated, and not worth reasoning
3905 // about (yet?).
3906 if (Values->first != PN1 || Values->second != PN2)
3907 break;
3908
3909 return std::make_pair(Start1, Start2);
3910 }
3911 }
3912 return std::nullopt;
3913}
3914
3915/// Return true if V1 == (binop V2, X), where X is known non-zero.
3916/// Only handle a small subset of binops where (binop V2, X) with non-zero X
3917/// implies V2 != V1.
3918static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2,
3919 const APInt &DemandedElts,
3920 const SimplifyQuery &Q, unsigned Depth) {
3922 if (!BO)
3923 return false;
3924 switch (BO->getOpcode()) {
3925 default:
3926 break;
3927 case Instruction::Or:
3928 if (!cast<PossiblyDisjointInst>(V1)->isDisjoint())
3929 break;
3930 [[fallthrough]];
3931 case Instruction::Xor:
3932 case Instruction::Add:
3933 Value *Op = nullptr;
3934 if (V2 == BO->getOperand(0))
3935 Op = BO->getOperand(1);
3936 else if (V2 == BO->getOperand(1))
3937 Op = BO->getOperand(0);
3938 else
3939 return false;
3940 return isKnownNonZero(Op, DemandedElts, Q, Depth + 1);
3941 }
3942 return false;
3943}
3944
3945/// Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and
3946/// the multiplication is nuw or nsw.
3947static bool isNonEqualMul(const Value *V1, const Value *V2,
3948 const APInt &DemandedElts, const SimplifyQuery &Q,
3949 unsigned Depth) {
3950 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3951 const APInt *C;
3952 return match(OBO, m_Mul(m_Specific(V1), m_APInt(C))) &&
3953 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3954 !C->isZero() && !C->isOne() &&
3955 isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3956 }
3957 return false;
3958}
3959
3960/// Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and
3961/// the shift is nuw or nsw.
3962static bool isNonEqualShl(const Value *V1, const Value *V2,
3963 const APInt &DemandedElts, const SimplifyQuery &Q,
3964 unsigned Depth) {
3965 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3966 const APInt *C;
3967 return match(OBO, m_Shl(m_Specific(V1), m_APInt(C))) &&
3968 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3969 !C->isZero() && isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3970 }
3971 return false;
3972}
3973
3974static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2,
3975 const APInt &DemandedElts, const SimplifyQuery &Q,
3976 unsigned Depth) {
3977 // Check two PHIs are in same block.
3978 if (PN1->getParent() != PN2->getParent())
3979 return false;
3980
3982 bool UsedFullRecursion = false;
3983 for (const BasicBlock *IncomBB : PN1->blocks()) {
3984 if (!VisitedBBs.insert(IncomBB).second)
3985 continue; // Don't reprocess blocks that we have dealt with already.
3986 const Value *IV1 = PN1->getIncomingValueForBlock(IncomBB);
3987 const Value *IV2 = PN2->getIncomingValueForBlock(IncomBB);
3988 const APInt *C1, *C2;
3989 if (match(IV1, m_APInt(C1)) && match(IV2, m_APInt(C2)) && *C1 != *C2)
3990 continue;
3991
3992 // Only one pair of phi operands is allowed for full recursion.
3993 if (UsedFullRecursion)
3994 return false;
3995
3997 RecQ.CxtI = IncomBB->getTerminator();
3998 if (!isKnownNonEqual(IV1, IV2, DemandedElts, RecQ, Depth + 1))
3999 return false;
4000 UsedFullRecursion = true;
4001 }
4002 return true;
4003}
4004
4005static bool isNonEqualSelect(const Value *V1, const Value *V2,
4006 const APInt &DemandedElts, const SimplifyQuery &Q,
4007 unsigned Depth) {
4008 const SelectInst *SI1 = dyn_cast<SelectInst>(V1);
4009 if (!SI1)
4010 return false;
4011
4012 if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2)) {
4013 const Value *Cond1 = SI1->getCondition();
4014 const Value *Cond2 = SI2->getCondition();
4015 if (Cond1 == Cond2)
4016 return isKnownNonEqual(SI1->getTrueValue(), SI2->getTrueValue(),
4017 DemandedElts, Q, Depth + 1) &&
4018 isKnownNonEqual(SI1->getFalseValue(), SI2->getFalseValue(),
4019 DemandedElts, Q, Depth + 1);
4020 }
4021 return isKnownNonEqual(SI1->getTrueValue(), V2, DemandedElts, Q, Depth + 1) &&
4022 isKnownNonEqual(SI1->getFalseValue(), V2, DemandedElts, Q, Depth + 1);
4023}
4024
4025// Check to see if A is both a GEP and is the incoming value for a PHI in the
4026// loop, and B is either a ptr or another GEP. If the PHI has 2 incoming values,
4027// one of them being the recursive GEP A and the other a ptr at same base and at
4028// the same/higher offset than B we are only incrementing the pointer further in
4029// loop if offset of recursive GEP is greater than 0.
4031 const SimplifyQuery &Q) {
4032 if (!A->getType()->isPointerTy() || !B->getType()->isPointerTy())
4033 return false;
4034
4035 auto *GEPA = dyn_cast<GEPOperator>(A);
4036 if (!GEPA || GEPA->getNumIndices() != 1 || !isa<Constant>(GEPA->idx_begin()))
4037 return false;
4038
4039 // Handle 2 incoming PHI values with one being a recursive GEP.
4040 auto *PN = dyn_cast<PHINode>(GEPA->getPointerOperand());
4041 if (!PN || PN->getNumIncomingValues() != 2)
4042 return false;
4043
4044 // Search for the recursive GEP as an incoming operand, and record that as
4045 // Step.
4046 Value *Start = nullptr;
4047 Value *Step = const_cast<Value *>(A);
4048 if (PN->getIncomingValue(0) == Step)
4049 Start = PN->getIncomingValue(1);
4050 else if (PN->getIncomingValue(1) == Step)
4051 Start = PN->getIncomingValue(0);
4052 else
4053 return false;
4054
4055 // Other incoming node base should match the B base.
4056 // StartOffset >= OffsetB && StepOffset > 0?
4057 // StartOffset <= OffsetB && StepOffset < 0?
4058 // Is non-equal if above are true.
4059 // We use stripAndAccumulateInBoundsConstantOffsets to restrict the
4060 // optimisation to inbounds GEPs only.
4061 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(Start->getType());
4062 APInt StartOffset(IndexWidth, 0);
4063 Start = Start->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StartOffset);
4064 APInt StepOffset(IndexWidth, 0);
4065 Step = Step->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StepOffset);
4066
4067 // Check if Base Pointer of Step matches the PHI.
4068 if (Step != PN)
4069 return false;
4070 APInt OffsetB(IndexWidth, 0);
4071 B = B->stripAndAccumulateInBoundsConstantOffsets(Q.DL, OffsetB);
4072 return Start == B &&
4073 ((StartOffset.sge(OffsetB) && StepOffset.isStrictlyPositive()) ||
4074 (StartOffset.sle(OffsetB) && StepOffset.isNegative()));
4075}
4076
4077static bool isKnownNonEqualFromContext(const Value *V1, const Value *V2,
4078 const SimplifyQuery &Q, unsigned Depth) {
4079 if (!Q.CxtI)
4080 return false;
4081
4082 // Try to infer NonEqual based on information from dominating conditions.
4083 if (Q.DC && Q.DT) {
4084 auto IsKnownNonEqualFromDominatingCondition = [&](const Value *V) {
4085 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
4086 Value *Cond = BI->getCondition();
4087 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4088 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()) &&
4090 /*LHSIsTrue=*/true, Depth)
4091 .value_or(false))
4092 return true;
4093
4094 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4095 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()) &&
4097 /*LHSIsTrue=*/false, Depth)
4098 .value_or(false))
4099 return true;
4100 }
4101
4102 return false;
4103 };
4104
4105 if (IsKnownNonEqualFromDominatingCondition(V1) ||
4106 IsKnownNonEqualFromDominatingCondition(V2))
4107 return true;
4108 }
4109
4110 if (!Q.AC)
4111 return false;
4112
4113 // Try to infer NonEqual based on information from assumptions.
4114 for (auto &AssumeVH : Q.AC->assumptionsFor(V1)) {
4115 if (!AssumeVH)
4116 continue;
4117 CallInst *I = cast<CallInst>(AssumeVH);
4118
4119 assert(I->getFunction() == Q.CxtI->getFunction() &&
4120 "Got assumption for the wrong function!");
4121 assert(I->getIntrinsicID() == Intrinsic::assume &&
4122 "must be an assume intrinsic");
4123
4124 if (isImpliedCondition(I->getArgOperand(0), ICmpInst::ICMP_NE, V1, V2, Q.DL,
4125 /*LHSIsTrue=*/true, Depth)
4126 .value_or(false) &&
4128 return true;
4129 }
4130
4131 return false;
4132}
4133
4134/// Return true if it is known that V1 != V2.
4135static bool isKnownNonEqual(const Value *V1, const Value *V2,
4136 const APInt &DemandedElts, const SimplifyQuery &Q,
4137 unsigned Depth) {
4138 if (V1 == V2)
4139 return false;
4140 if (V1->getType() != V2->getType())
4141 // We can't look through casts yet.
4142 return false;
4143
4145 return false;
4146
4147 // See if we can recurse through (exactly one of) our operands. This
4148 // requires our operation be 1-to-1 and map every input value to exactly
4149 // one output value. Such an operation is invertible.
4150 auto *O1 = dyn_cast<Operator>(V1);
4151 auto *O2 = dyn_cast<Operator>(V2);
4152 if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) {
4153 if (auto Values = getInvertibleOperands(O1, O2))
4154 return isKnownNonEqual(Values->first, Values->second, DemandedElts, Q,
4155 Depth + 1);
4156
4157 if (const PHINode *PN1 = dyn_cast<PHINode>(V1)) {
4158 const PHINode *PN2 = cast<PHINode>(V2);
4159 // FIXME: This is missing a generalization to handle the case where one is
4160 // a PHI and another one isn't.
4161 if (isNonEqualPHIs(PN1, PN2, DemandedElts, Q, Depth))
4162 return true;
4163 };
4164 }
4165
4166 if (isModifyingBinopOfNonZero(V1, V2, DemandedElts, Q, Depth) ||
4167 isModifyingBinopOfNonZero(V2, V1, DemandedElts, Q, Depth))
4168 return true;
4169
4170 if (isNonEqualMul(V1, V2, DemandedElts, Q, Depth) ||
4171 isNonEqualMul(V2, V1, DemandedElts, Q, Depth))
4172 return true;
4173
4174 if (isNonEqualShl(V1, V2, DemandedElts, Q, Depth) ||
4175 isNonEqualShl(V2, V1, DemandedElts, Q, Depth))
4176 return true;
4177
4178 if (V1->getType()->isIntOrIntVectorTy()) {
4179 // Are any known bits in V1 contradictory to known bits in V2? If V1
4180 // has a known zero where V2 has a known one, they must not be equal.
4181 KnownBits Known1 = computeKnownBits(V1, DemandedElts, Q, Depth);
4182 if (!Known1.isUnknown()) {
4183 KnownBits Known2 = computeKnownBits(V2, DemandedElts, Q, Depth);
4184 if (Known1.Zero.intersects(Known2.One) ||
4185 Known2.Zero.intersects(Known1.One))
4186 return true;
4187 }
4188 }
4189
4190 if (isNonEqualSelect(V1, V2, DemandedElts, Q, Depth) ||
4191 isNonEqualSelect(V2, V1, DemandedElts, Q, Depth))
4192 return true;
4193
4194 if (isNonEqualPointersWithRecursiveGEP(V1, V2, Q) ||
4196 return true;
4197
4198 Value *A, *B;
4199 // PtrToInts are NonEqual if their Ptrs are NonEqual.
4200 // Check PtrToInt type matches the pointer size.
4201 if (match(V1, m_PtrToIntSameSize(Q.DL, m_Value(A))) &&
4203 return isKnownNonEqual(A, B, DemandedElts, Q, Depth + 1);
4204
4205 if (isKnownNonEqualFromContext(V1, V2, Q, Depth))
4206 return true;
4207
4208 return false;
4209}
4210
4211/// For vector constants, loop over the elements and find the constant with the
4212/// minimum number of sign bits. Return 0 if the value is not a vector constant
4213/// or if any element was not analyzed; otherwise, return the count for the
4214/// element with the minimum number of sign bits.
4216 const APInt &DemandedElts,
4217 unsigned TyBits) {
4218 const auto *CV = dyn_cast<Constant>(V);
4219 if (!CV || !isa<FixedVectorType>(CV->getType()))
4220 return 0;
4221
4222 unsigned MinSignBits = TyBits;
4223 unsigned NumElts = cast<FixedVectorType>(CV->getType())->getNumElements();
4224 for (unsigned i = 0; i != NumElts; ++i) {
4225 if (!DemandedElts[i])
4226 continue;
4227 // If we find a non-ConstantInt, bail out.
4228 auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));
4229 if (!Elt)
4230 return 0;
4231
4232 MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());
4233 }
4234
4235 return MinSignBits;
4236}
4237
4238static unsigned ComputeNumSignBitsImpl(const Value *V,
4239 const APInt &DemandedElts,
4240 const SimplifyQuery &Q, unsigned Depth);
4241
4242static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
4243 const SimplifyQuery &Q, unsigned Depth) {
4244 unsigned Result = ComputeNumSignBitsImpl(V, DemandedElts, Q, Depth);
4245 assert(Result > 0 && "At least one sign bit needs to be present!");
4246 return Result;
4247}
4248
4249/// Return the number of times the sign bit of the register is replicated into
4250/// the other bits. We know that at least 1 bit is always equal to the sign bit
4251/// (itself), but other cases can give us information. For example, immediately
4252/// after an "ashr X, 2", we know that the top 3 bits are all equal to each
4253/// other, so we return 3. For vectors, return the number of sign bits for the
4254/// vector element with the minimum number of known sign bits of the demanded
4255/// elements in the vector specified by DemandedElts.
4256static unsigned ComputeNumSignBitsImpl(const Value *V,
4257 const APInt &DemandedElts,
4258 const SimplifyQuery &Q, unsigned Depth) {
4259 Type *Ty = V->getType();
4260#ifndef NDEBUG
4261 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4262
4263 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
4264 assert(
4265 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
4266 "DemandedElt width should equal the fixed vector number of elements");
4267 } else {
4268 assert(DemandedElts == APInt(1, 1) &&
4269 "DemandedElt width should be 1 for scalars");
4270 }
4271#endif
4272
4273 // We return the minimum number of sign bits that are guaranteed to be present
4274 // in V, so for undef we have to conservatively return 1. We don't have the
4275 // same behavior for poison though -- that's a FIXME today.
4276
4277 Type *ScalarTy = Ty->getScalarType();
4278 unsigned TyBits = ScalarTy->isPointerTy() ?
4279 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
4280 Q.DL.getTypeSizeInBits(ScalarTy);
4281
4282 unsigned Tmp, Tmp2;
4283 unsigned FirstAnswer = 1;
4284
4285 // Note that ConstantInt is handled by the general computeKnownBits case
4286 // below.
4287
4289 return 1;
4290
4291 if (auto *U = dyn_cast<Operator>(V)) {
4292 switch (Operator::getOpcode(V)) {
4293 default: break;
4294 case Instruction::BitCast: {
4295 Value *Src = U->getOperand(0);
4296 Type *SrcTy = Src->getType();
4297
4298 // Skip if the source type is not an integer or integer vector type
4299 // This ensures we only process integer-like types
4300 if (!SrcTy->isIntOrIntVectorTy())
4301 break;
4302
4303 unsigned SrcBits = SrcTy->getScalarSizeInBits();
4304
4305 // Bitcast 'large element' scalar/vector to 'small element' vector.
4306 if ((SrcBits % TyBits) != 0)
4307 break;
4308
4309 // Only proceed if the destination type is a fixed-size vector
4310 if (isa<FixedVectorType>(Ty)) {
4311 // Fast case - sign splat can be simply split across the small elements.
4312 // This works for both vector and scalar sources
4313 Tmp = ComputeNumSignBits(Src, Q, Depth + 1);
4314 if (Tmp == SrcBits)
4315 return TyBits;
4316 }
4317 break;
4318 }
4319 case Instruction::SExt:
4320 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
4321 return ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1) +
4322 Tmp;
4323
4324 case Instruction::SDiv: {
4325 const APInt *Denominator;
4326 // sdiv X, C -> adds log(C) sign bits.
4327 if (match(U->getOperand(1), m_APInt(Denominator))) {
4328
4329 // Ignore non-positive denominator.
4330 if (!Denominator->isStrictlyPositive())
4331 break;
4332
4333 // Calculate the incoming numerator bits.
4334 unsigned NumBits =
4335 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4336
4337 // Add floor(log(C)) bits to the numerator bits.
4338 return std::min(TyBits, NumBits + Denominator->logBase2());
4339 }
4340 break;
4341 }
4342
4343 case Instruction::SRem: {
4344 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4345
4346 const APInt *Denominator;
4347 // srem X, C -> we know that the result is within [-C+1,C) when C is a
4348 // positive constant. This let us put a lower bound on the number of sign
4349 // bits.
4350 if (match(U->getOperand(1), m_APInt(Denominator))) {
4351
4352 // Ignore non-positive denominator.
4353 if (Denominator->isStrictlyPositive()) {
4354 // Calculate the leading sign bit constraints by examining the
4355 // denominator. Given that the denominator is positive, there are two
4356 // cases:
4357 //
4358 // 1. The numerator is positive. The result range is [0,C) and
4359 // [0,C) u< (1 << ceilLogBase2(C)).
4360 //
4361 // 2. The numerator is negative. Then the result range is (-C,0] and
4362 // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
4363 //
4364 // Thus a lower bound on the number of sign bits is `TyBits -
4365 // ceilLogBase2(C)`.
4366
4367 unsigned ResBits = TyBits - Denominator->ceilLogBase2();
4368 Tmp = std::max(Tmp, ResBits);
4369 }
4370 }
4371 return Tmp;
4372 }
4373
4374 case Instruction::AShr: {
4375 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4376 // ashr X, C -> adds C sign bits. Vectors too.
4377 const APInt *ShAmt;
4378 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4379 if (ShAmt->uge(TyBits))
4380 break; // Bad shift.
4381 unsigned ShAmtLimited = ShAmt->getZExtValue();
4382 Tmp += ShAmtLimited;
4383 if (Tmp > TyBits) Tmp = TyBits;
4384 }
4385 return Tmp;
4386 }
4387 case Instruction::Shl: {
4388 const APInt *ShAmt;
4389 Value *X = nullptr;
4390 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4391 // shl destroys sign bits.
4392 if (ShAmt->uge(TyBits))
4393 break; // Bad shift.
4394 // We can look through a zext (more or less treating it as a sext) if
4395 // all extended bits are shifted out.
4396 if (match(U->getOperand(0), m_ZExt(m_Value(X))) &&
4397 ShAmt->uge(TyBits - X->getType()->getScalarSizeInBits())) {
4398 Tmp = ComputeNumSignBits(X, DemandedElts, Q, Depth + 1);
4399 Tmp += TyBits - X->getType()->getScalarSizeInBits();
4400 } else
4401 Tmp =
4402 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4403 if (ShAmt->uge(Tmp))
4404 break; // Shifted all sign bits out.
4405 Tmp2 = ShAmt->getZExtValue();
4406 return Tmp - Tmp2;
4407 }
4408 break;
4409 }
4410 case Instruction::And:
4411 case Instruction::Or:
4412 case Instruction::Xor: // NOT is handled here.
4413 // Logical binary ops preserve the number of sign bits at the worst.
4414 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4415 if (Tmp != 1) {
4416 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4417 FirstAnswer = std::min(Tmp, Tmp2);
4418 // We computed what we know about the sign bits as our first
4419 // answer. Now proceed to the generic code that uses
4420 // computeKnownBits, and pick whichever answer is better.
4421 }
4422 break;
4423
4424 case Instruction::Select: {
4425 // If we have a clamp pattern, we know that the number of sign bits will
4426 // be the minimum of the clamp min/max range.
4427 const Value *X;
4428 const APInt *CLow, *CHigh;
4429 if (isSignedMinMaxClamp(U, X, CLow, CHigh))
4430 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4431
4432 Tmp = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4433 if (Tmp == 1)
4434 break;
4435 Tmp2 = ComputeNumSignBits(U->getOperand(2), DemandedElts, Q, Depth + 1);
4436 return std::min(Tmp, Tmp2);
4437 }
4438
4439 case Instruction::Add:
4440 // Add can have at most one carry bit. Thus we know that the output
4441 // is, at worst, one more bit than the inputs.
4442 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4443 if (Tmp == 1) break;
4444
4445 // Special case decrementing a value (ADD X, -1):
4446 if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
4447 if (CRHS->isAllOnesValue()) {
4448 KnownBits Known(TyBits);
4449 computeKnownBits(U->getOperand(0), DemandedElts, Known, Q, Depth + 1);
4450
4451 // If the input is known to be 0 or 1, the output is 0/-1, which is
4452 // all sign bits set.
4453 if ((Known.Zero | 1).isAllOnes())
4454 return TyBits;
4455
4456 // If we are subtracting one from a positive number, there is no carry
4457 // out of the result.
4458 if (Known.isNonNegative())
4459 return Tmp;
4460 }
4461
4462 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4463 if (Tmp2 == 1)
4464 break;
4465 return std::min(Tmp, Tmp2) - 1;
4466
4467 case Instruction::Sub:
4468 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4469 if (Tmp2 == 1)
4470 break;
4471
4472 // Handle NEG.
4473 if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
4474 if (CLHS->isNullValue()) {
4475 KnownBits Known(TyBits);
4476 computeKnownBits(U->getOperand(1), DemandedElts, Known, Q, Depth + 1);
4477 // If the input is known to be 0 or 1, the output is 0/-1, which is
4478 // all sign bits set.
4479 if ((Known.Zero | 1).isAllOnes())
4480 return TyBits;
4481
4482 // If the input is known to be positive (the sign bit is known clear),
4483 // the output of the NEG has the same number of sign bits as the
4484 // input.
4485 if (Known.isNonNegative())
4486 return Tmp2;
4487
4488 // Otherwise, we treat this like a SUB.
4489 }
4490
4491 // Sub can have at most one carry bit. Thus we know that the output
4492 // is, at worst, one more bit than the inputs.
4493 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4494 if (Tmp == 1)
4495 break;
4496 return std::min(Tmp, Tmp2) - 1;
4497
4498 case Instruction::Mul: {
4499 // The output of the Mul can be at most twice the valid bits in the
4500 // inputs.
4501 unsigned SignBitsOp0 =
4502 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4503 if (SignBitsOp0 == 1)
4504 break;
4505 unsigned SignBitsOp1 =
4506 ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4507 if (SignBitsOp1 == 1)
4508 break;
4509 unsigned OutValidBits =
4510 (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);
4511 return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
4512 }
4513
4514 case Instruction::PHI: {
4515 const PHINode *PN = cast<PHINode>(U);
4516 unsigned NumIncomingValues = PN->getNumIncomingValues();
4517 // Don't analyze large in-degree PHIs.
4518 if (NumIncomingValues > 4) break;
4519 // Unreachable blocks may have zero-operand PHI nodes.
4520 if (NumIncomingValues == 0) break;
4521
4522 // Take the minimum of all incoming values. This can't infinitely loop
4523 // because of our depth threshold.
4525 Tmp = TyBits;
4526 for (unsigned i = 0, e = NumIncomingValues; i != e; ++i) {
4527 if (Tmp == 1) return Tmp;
4528 RecQ.CxtI = PN->getIncomingBlock(i)->getTerminator();
4529 Tmp = std::min(Tmp, ComputeNumSignBits(PN->getIncomingValue(i),
4530 DemandedElts, RecQ, Depth + 1));
4531 }
4532 return Tmp;
4533 }
4534
4535 case Instruction::Trunc: {
4536 // If the input contained enough sign bits that some remain after the
4537 // truncation, then we can make use of that. Otherwise we don't know
4538 // anything.
4539 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4540 unsigned OperandTyBits = U->getOperand(0)->getType()->getScalarSizeInBits();
4541 if (Tmp > (OperandTyBits - TyBits))
4542 return Tmp - (OperandTyBits - TyBits);
4543
4544 return 1;
4545 }
4546
4547 case Instruction::ExtractElement:
4548 // Look through extract element. At the moment we keep this simple and
4549 // skip tracking the specific element. But at least we might find
4550 // information valid for all elements of the vector (for example if vector
4551 // is sign extended, shifted, etc).
4552 return ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4553
4554 case Instruction::ShuffleVector: {
4555 // Collect the minimum number of sign bits that are shared by every vector
4556 // element referenced by the shuffle.
4557 auto *Shuf = dyn_cast<ShuffleVectorInst>(U);
4558 if (!Shuf) {
4559 // FIXME: Add support for shufflevector constant expressions.
4560 return 1;
4561 }
4562 APInt DemandedLHS, DemandedRHS;
4563 // For undef elements, we don't know anything about the common state of
4564 // the shuffle result.
4565 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
4566 return 1;
4567 Tmp = std::numeric_limits<unsigned>::max();
4568 if (!!DemandedLHS) {
4569 const Value *LHS = Shuf->getOperand(0);
4570 Tmp = ComputeNumSignBits(LHS, DemandedLHS, Q, Depth + 1);
4571 }
4572 // If we don't know anything, early out and try computeKnownBits
4573 // fall-back.
4574 if (Tmp == 1)
4575 break;
4576 if (!!DemandedRHS) {
4577 const Value *RHS = Shuf->getOperand(1);
4578 Tmp2 = ComputeNumSignBits(RHS, DemandedRHS, Q, Depth + 1);
4579 Tmp = std::min(Tmp, Tmp2);
4580 }
4581 // If we don't know anything, early out and try computeKnownBits
4582 // fall-back.
4583 if (Tmp == 1)
4584 break;
4585 assert(Tmp <= TyBits && "Failed to determine minimum sign bits");
4586 return Tmp;
4587 }
4588 case Instruction::Call: {
4589 if (const auto *II = dyn_cast<IntrinsicInst>(U)) {
4590 switch (II->getIntrinsicID()) {
4591 default:
4592 break;
4593 case Intrinsic::abs:
4594 Tmp =
4595 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4596 if (Tmp == 1)
4597 break;
4598
4599 // Absolute value reduces number of sign bits by at most 1.
4600 return Tmp - 1;
4601 case Intrinsic::smin:
4602 case Intrinsic::smax: {
4603 const APInt *CLow, *CHigh;
4604 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
4605 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4606 }
4607 }
4608 }
4609 }
4610 }
4611 }
4612
4613 // Finally, if we can prove that the top bits of the result are 0's or 1's,
4614 // use this information.
4615
4616 // If we can examine all elements of a vector constant successfully, we're
4617 // done (we can't do any better than that). If not, keep trying.
4618 if (unsigned VecSignBits =
4619 computeNumSignBitsVectorConstant(V, DemandedElts, TyBits))
4620 return VecSignBits;
4621
4622 KnownBits Known(TyBits);
4623 computeKnownBits(V, DemandedElts, Known, Q, Depth);
4624
4625 // If we know that the sign bit is either zero or one, determine the number of
4626 // identical bits in the top of the input value.
4627 return std::max(FirstAnswer, Known.countMinSignBits());
4628}
4629
4631 const TargetLibraryInfo *TLI) {
4632 const Function *F = CB.getCalledFunction();
4633 if (!F)
4635
4636 if (F->isIntrinsic())
4637 return F->getIntrinsicID();
4638
4639 // We are going to infer semantics of a library function based on mapping it
4640 // to an LLVM intrinsic. Check that the library function is available from
4641 // this callbase and in this environment.
4642 LibFunc Func;
4643 if (F->hasLocalLinkage() || !TLI || !TLI->getLibFunc(CB, Func) ||
4644 !CB.onlyReadsMemory())
4646
4647 switch (Func) {
4648 default:
4649 break;
4650 case LibFunc_sin:
4651 case LibFunc_sinf:
4652 case LibFunc_sinl:
4653 return Intrinsic::sin;
4654 case LibFunc_cos:
4655 case LibFunc_cosf:
4656 case LibFunc_cosl:
4657 return Intrinsic::cos;
4658 case LibFunc_tan:
4659 case LibFunc_tanf:
4660 case LibFunc_tanl:
4661 return Intrinsic::tan;
4662 case LibFunc_asin:
4663 case LibFunc_asinf:
4664 case LibFunc_asinl:
4665 return Intrinsic::asin;
4666 case LibFunc_acos:
4667 case LibFunc_acosf:
4668 case LibFunc_acosl:
4669 return Intrinsic::acos;
4670 case LibFunc_atan:
4671 case LibFunc_atanf:
4672 case LibFunc_atanl:
4673 return Intrinsic::atan;
4674 case LibFunc_atan2:
4675 case LibFunc_atan2f:
4676 case LibFunc_atan2l:
4677 return Intrinsic::atan2;
4678 case LibFunc_sinh:
4679 case LibFunc_sinhf:
4680 case LibFunc_sinhl:
4681 return Intrinsic::sinh;
4682 case LibFunc_cosh:
4683 case LibFunc_coshf:
4684 case LibFunc_coshl:
4685 return Intrinsic::cosh;
4686 case LibFunc_tanh:
4687 case LibFunc_tanhf:
4688 case LibFunc_tanhl:
4689 return Intrinsic::tanh;
4690 case LibFunc_exp:
4691 case LibFunc_expf:
4692 case LibFunc_expl:
4693 return Intrinsic::exp;
4694 case LibFunc_exp2:
4695 case LibFunc_exp2f:
4696 case LibFunc_exp2l:
4697 return Intrinsic::exp2;
4698 case LibFunc_exp10:
4699 case LibFunc_exp10f:
4700 case LibFunc_exp10l:
4701 return Intrinsic::exp10;
4702 case LibFunc_log:
4703 case LibFunc_logf:
4704 case LibFunc_logl:
4705 return Intrinsic::log;
4706 case LibFunc_log10:
4707 case LibFunc_log10f:
4708 case LibFunc_log10l:
4709 return Intrinsic::log10;
4710 case LibFunc_log2:
4711 case LibFunc_log2f:
4712 case LibFunc_log2l:
4713 return Intrinsic::log2;
4714 case LibFunc_fabs:
4715 case LibFunc_fabsf:
4716 case LibFunc_fabsl:
4717 return Intrinsic::fabs;
4718 case LibFunc_fmin:
4719 case LibFunc_fminf:
4720 case LibFunc_fminl:
4721 return Intrinsic::minnum;
4722 case LibFunc_fmax:
4723 case LibFunc_fmaxf:
4724 case LibFunc_fmaxl:
4725 return Intrinsic::maxnum;
4726 case LibFunc_copysign:
4727 case LibFunc_copysignf:
4728 case LibFunc_copysignl:
4729 return Intrinsic::copysign;
4730 case LibFunc_floor:
4731 case LibFunc_floorf:
4732 case LibFunc_floorl:
4733 return Intrinsic::floor;
4734 case LibFunc_ceil:
4735 case LibFunc_ceilf:
4736 case LibFunc_ceill:
4737 return Intrinsic::ceil;
4738 case LibFunc_trunc:
4739 case LibFunc_truncf:
4740 case LibFunc_truncl:
4741 return Intrinsic::trunc;
4742 case LibFunc_rint:
4743 case LibFunc_rintf:
4744 case LibFunc_rintl:
4745 return Intrinsic::rint;
4746 case LibFunc_nearbyint:
4747 case LibFunc_nearbyintf:
4748 case LibFunc_nearbyintl:
4749 return Intrinsic::nearbyint;
4750 case LibFunc_round:
4751 case LibFunc_roundf:
4752 case LibFunc_roundl:
4753 return Intrinsic::round;
4754 case LibFunc_roundeven:
4755 case LibFunc_roundevenf:
4756 case LibFunc_roundevenl:
4757 return Intrinsic::roundeven;
4758 case LibFunc_pow:
4759 case LibFunc_powf:
4760 case LibFunc_powl:
4761 return Intrinsic::pow;
4762 case LibFunc_sqrt:
4763 case LibFunc_sqrtf:
4764 case LibFunc_sqrtl:
4765 return Intrinsic::sqrt;
4766 }
4767
4769}
4770
4771/// Given an exploded icmp instruction, return true if the comparison only
4772/// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if
4773/// the result of the comparison is true when the input value is signed.
4775 bool &TrueIfSigned) {
4776 switch (Pred) {
4777 case ICmpInst::ICMP_SLT: // True if LHS s< 0
4778 TrueIfSigned = true;
4779 return RHS.isZero();
4780 case ICmpInst::ICMP_SLE: // True if LHS s<= -1
4781 TrueIfSigned = true;
4782 return RHS.isAllOnes();
4783 case ICmpInst::ICMP_SGT: // True if LHS s> -1
4784 TrueIfSigned = false;
4785 return RHS.isAllOnes();
4786 case ICmpInst::ICMP_SGE: // True if LHS s>= 0
4787 TrueIfSigned = false;
4788 return RHS.isZero();
4789 case ICmpInst::ICMP_UGT:
4790 // True if LHS u> RHS and RHS == sign-bit-mask - 1
4791 TrueIfSigned = true;
4792 return RHS.isMaxSignedValue();
4793 case ICmpInst::ICMP_UGE:
4794 // True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4795 TrueIfSigned = true;
4796 return RHS.isMinSignedValue();
4797 case ICmpInst::ICMP_ULT:
4798 // True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4799 TrueIfSigned = false;
4800 return RHS.isMinSignedValue();
4801 case ICmpInst::ICMP_ULE:
4802 // True if LHS u<= RHS and RHS == sign-bit-mask - 1
4803 TrueIfSigned = false;
4804 return RHS.isMaxSignedValue();
4805 default:
4806 return false;
4807 }
4808}
4809
4811 bool CondIsTrue,
4812 const Instruction *CxtI,
4813 KnownFPClass &KnownFromContext,
4814 unsigned Depth = 0) {
4815 Value *A, *B;
4817 (CondIsTrue ? match(Cond, m_LogicalAnd(m_Value(A), m_Value(B)))
4818 : match(Cond, m_LogicalOr(m_Value(A), m_Value(B))))) {
4819 computeKnownFPClassFromCond(V, A, CondIsTrue, CxtI, KnownFromContext,
4820 Depth + 1);
4821 computeKnownFPClassFromCond(V, B, CondIsTrue, CxtI, KnownFromContext,
4822 Depth + 1);
4823 return;
4824 }
4826 computeKnownFPClassFromCond(V, A, !CondIsTrue, CxtI, KnownFromContext,
4827 Depth + 1);
4828 return;
4829 }
4830 CmpPredicate Pred;
4831 Value *LHS;
4832 uint64_t ClassVal = 0;
4833 const APFloat *CRHS;
4834 const APInt *RHS;
4835 if (match(Cond, m_FCmp(Pred, m_Value(LHS), m_APFloat(CRHS)))) {
4836 auto [CmpVal, MaskIfTrue, MaskIfFalse] = fcmpImpliesClass(
4837 Pred, *cast<Instruction>(Cond)->getParent()->getParent(), LHS, *CRHS,
4838 LHS != V);
4839 if (CmpVal == V)
4840 KnownFromContext.knownNot(~(CondIsTrue ? MaskIfTrue : MaskIfFalse));
4842 m_Specific(V), m_ConstantInt(ClassVal)))) {
4843 FPClassTest Mask = static_cast<FPClassTest>(ClassVal);
4844 KnownFromContext.knownNot(CondIsTrue ? ~Mask : Mask);
4845 } else if (match(Cond, m_ICmp(Pred, m_ElementWiseBitCast(m_Specific(V)),
4846 m_APInt(RHS)))) {
4847 bool TrueIfSigned;
4848 if (!isSignBitCheck(Pred, *RHS, TrueIfSigned))
4849 return;
4850 if (TrueIfSigned == CondIsTrue)
4851 KnownFromContext.signBitMustBeOne();
4852 else
4853 KnownFromContext.signBitMustBeZero();
4854 }
4855}
4856
4858 const SimplifyQuery &Q) {
4859 KnownFPClass KnownFromContext;
4860
4861 if (Q.CC && Q.CC->AffectedValues.contains(V))
4863 KnownFromContext);
4864
4865 if (!Q.CxtI)
4866 return KnownFromContext;
4867
4868 if (Q.DC && Q.DT) {
4869 // Handle dominating conditions.
4870 for (CondBrInst *BI : Q.DC->conditionsFor(V)) {
4871 Value *Cond = BI->getCondition();
4872
4873 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4874 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
4875 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/true, Q.CxtI,
4876 KnownFromContext);
4877
4878 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4879 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
4880 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/false, Q.CxtI,
4881 KnownFromContext);
4882 }
4883 }
4884
4885 if (!Q.AC)
4886 return KnownFromContext;
4887
4888 // Try to restrict the floating-point classes based on information from
4889 // assumptions.
4890 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
4891 if (!AssumeVH)
4892 continue;
4893 CallInst *I = cast<CallInst>(AssumeVH);
4894
4895 assert(I->getFunction() == Q.CxtI->getParent()->getParent() &&
4896 "Got assumption for the wrong function!");
4897 assert(I->getIntrinsicID() == Intrinsic::assume &&
4898 "must be an assume intrinsic");
4899
4900 if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
4901 continue;
4902
4903 computeKnownFPClassFromCond(V, I->getArgOperand(0),
4904 /*CondIsTrue=*/true, Q.CxtI, KnownFromContext);
4905 }
4906
4907 return KnownFromContext;
4908}
4909
4911 Value *Arm, bool Invert,
4912 const SimplifyQuery &SQ,
4913 unsigned Depth) {
4914
4915 KnownFPClass KnownSrc;
4917 /*CondIsTrue=*/!Invert, SQ.CxtI, KnownSrc,
4918 Depth + 1);
4919 KnownSrc = KnownSrc.unionWith(Known);
4920 if (KnownSrc.isUnknown())
4921 return;
4922
4923 if (isGuaranteedNotToBeUndef(Arm, SQ.AC, SQ.CxtI, SQ.DT, Depth + 1))
4924 Known = KnownSrc;
4925}
4926
4927void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4928 FPClassTest InterestedClasses, KnownFPClass &Known,
4929 const SimplifyQuery &Q, unsigned Depth);
4930
4931static void computeKnownFPClass(const Value *V, KnownFPClass &Known,
4932 FPClassTest InterestedClasses,
4933 const SimplifyQuery &Q, unsigned Depth) {
4934 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
4935 APInt DemandedElts =
4936 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
4937 computeKnownFPClass(V, DemandedElts, InterestedClasses, Known, Q, Depth);
4938}
4939
4941 const APInt &DemandedElts,
4942 FPClassTest InterestedClasses,
4943 KnownFPClass &Known,
4944 const SimplifyQuery &Q,
4945 unsigned Depth) {
4946 if ((InterestedClasses &
4948 return;
4949
4950 KnownFPClass KnownSrc;
4951 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
4952 KnownSrc, Q, Depth + 1);
4953 Known = KnownFPClass::fptrunc(KnownSrc);
4954}
4955
4957 switch (IID) {
4958 case Intrinsic::minimum:
4960 case Intrinsic::maximum:
4962 case Intrinsic::minimumnum:
4964 case Intrinsic::maximumnum:
4966 case Intrinsic::minnum:
4968 case Intrinsic::maxnum:
4970 default:
4971 llvm_unreachable("not a floating-point min-max intrinsic");
4972 }
4973}
4974
4975/// \return true if this is a floating point value that is known to have a
4976/// magnitude smaller than 1. i.e., fabs(X) <= 1.0 or is nan.
4977static bool isAbsoluteValueULEOne(const Value *V) {
4978 // TODO: Handle frexp
4979 // TODO: Other rounding intrinsics?
4980
4981 // fabs(x - floor(x)) <= 1
4982 const Value *SubFloorX;
4983 if (match(V, m_FSub(m_Value(SubFloorX),
4985 return true;
4986
4989}
4990
4991void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4992 FPClassTest InterestedClasses, KnownFPClass &Known,
4993 const SimplifyQuery &Q, unsigned Depth) {
4994 assert(Known.isUnknown() && "should not be called with known information");
4995
4996 if (!DemandedElts) {
4997 // No demanded elts, better to assume we don't know anything.
4998 Known.resetAll();
4999 return;
5000 }
5001
5002 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
5003
5004 if (auto *CFP = dyn_cast<ConstantFP>(V)) {
5005 Known = KnownFPClass(CFP->getValueAPF());
5006 return;
5007 }
5008
5010 Known.KnownFPClasses = fcPosZero;
5011 Known.SignBit = false;
5012 return;
5013 }
5014
5015 if (isa<PoisonValue>(V)) {
5016 Known.KnownFPClasses = fcNone;
5017 Known.SignBit = false;
5018 return;
5019 }
5020
5021 // Try to handle fixed width vector constants
5022 auto *VFVTy = dyn_cast<FixedVectorType>(V->getType());
5023 const Constant *CV = dyn_cast<Constant>(V);
5024 if (VFVTy && CV) {
5025 Known.KnownFPClasses = fcNone;
5026 bool SignBitAllZero = true;
5027 bool SignBitAllOne = true;
5028
5029 // For vectors, verify that each element is not NaN.
5030 unsigned NumElts = VFVTy->getNumElements();
5031 for (unsigned i = 0; i != NumElts; ++i) {
5032 if (!DemandedElts[i])
5033 continue;
5034
5035 Constant *Elt = CV->getAggregateElement(i);
5036 if (!Elt) {
5037 Known = KnownFPClass();
5038 return;
5039 }
5040 if (isa<PoisonValue>(Elt))
5041 continue;
5042 auto *CElt = dyn_cast<ConstantFP>(Elt);
5043 if (!CElt) {
5044 Known = KnownFPClass();
5045 return;
5046 }
5047
5048 const APFloat &C = CElt->getValueAPF();
5049 Known.KnownFPClasses |= C.classify();
5050 if (C.isNegative())
5051 SignBitAllZero = false;
5052 else
5053 SignBitAllOne = false;
5054 }
5055 if (SignBitAllOne != SignBitAllZero)
5056 Known.SignBit = SignBitAllOne;
5057 return;
5058 }
5059
5060 if (const auto *CDS = dyn_cast<ConstantDataSequential>(V)) {
5061 Known.KnownFPClasses = fcNone;
5062 for (size_t I = 0, E = CDS->getNumElements(); I != E; ++I)
5063 Known |= CDS->getElementAsAPFloat(I).classify();
5064 return;
5065 }
5066
5067 if (const auto *CA = dyn_cast<ConstantAggregate>(V)) {
5068 // TODO: Handle complex aggregates
5069 Known.KnownFPClasses = fcNone;
5070 for (const Use &Op : CA->operands()) {
5071 auto *CFP = dyn_cast<ConstantFP>(Op.get());
5072 if (!CFP) {
5073 Known = KnownFPClass();
5074 return;
5075 }
5076
5077 Known |= CFP->getValueAPF().classify();
5078 }
5079
5080 return;
5081 }
5082
5083 FPClassTest KnownNotFromFlags = fcNone;
5084 if (const auto *CB = dyn_cast<CallBase>(V))
5085 KnownNotFromFlags |= CB->getRetNoFPClass();
5086 else if (const auto *Arg = dyn_cast<Argument>(V))
5087 KnownNotFromFlags |= Arg->getNoFPClass();
5088
5089 const Operator *Op = dyn_cast<Operator>(V);
5091 if (FPOp->hasNoNaNs())
5092 KnownNotFromFlags |= fcNan;
5093 if (FPOp->hasNoInfs())
5094 KnownNotFromFlags |= fcInf;
5095 }
5096
5097 KnownFPClass AssumedClasses = computeKnownFPClassFromContext(V, Q);
5098 KnownNotFromFlags |= ~AssumedClasses.KnownFPClasses;
5099
5100 // We no longer need to find out about these bits from inputs if we can
5101 // assume this from flags/attributes.
5102 InterestedClasses &= ~KnownNotFromFlags;
5103
5104 llvm::scope_exit ClearClassesFromFlags([=, &Known] {
5105 Known.knownNot(KnownNotFromFlags);
5106 if (!Known.SignBit && AssumedClasses.SignBit) {
5107 if (*AssumedClasses.SignBit)
5108 Known.signBitMustBeOne();
5109 else
5110 Known.signBitMustBeZero();
5111 }
5112 });
5113
5114 if (!Op)
5115 return;
5116
5117 // All recursive calls that increase depth must come after this.
5119 return;
5120
5121 const unsigned Opc = Op->getOpcode();
5122 switch (Opc) {
5123 case Instruction::FNeg: {
5124 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5125 Known, Q, Depth + 1);
5126 Known.fneg();
5127 break;
5128 }
5129 case Instruction::Select: {
5130 auto ComputeForArm = [&](Value *Arm, bool Invert) {
5131 KnownFPClass Res;
5132 computeKnownFPClass(Arm, DemandedElts, InterestedClasses, Res, Q,
5133 Depth + 1);
5134 adjustKnownFPClassForSelectArm(Res, Op->getOperand(0), Arm, Invert, Q,
5135 Depth);
5136 return Res;
5137 };
5138 // Only known if known in both the LHS and RHS.
5139 Known =
5140 ComputeForArm(Op->getOperand(1), /*Invert=*/false)
5141 .intersectWith(ComputeForArm(Op->getOperand(2), /*Invert=*/true));
5142 break;
5143 }
5144 case Instruction::Load: {
5145 const MDNode *NoFPClass =
5146 cast<LoadInst>(Op)->getMetadata(LLVMContext::MD_nofpclass);
5147 if (!NoFPClass)
5148 break;
5149
5150 ConstantInt *MaskVal =
5152 Known.knownNot(static_cast<FPClassTest>(MaskVal->getZExtValue()));
5153 break;
5154 }
5155 case Instruction::Call: {
5156 const CallInst *II = cast<CallInst>(Op);
5157 const Intrinsic::ID IID = II->getIntrinsicID();
5158 switch (IID) {
5159 case Intrinsic::fabs: {
5160 if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) {
5161 // If we only care about the sign bit we don't need to inspect the
5162 // operand.
5163 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5164 InterestedClasses, Known, Q, Depth + 1);
5165 }
5166
5167 Known.fabs();
5168 break;
5169 }
5170 case Intrinsic::copysign: {
5171 KnownFPClass KnownSign;
5172
5173 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5174 Known, Q, Depth + 1);
5175 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5176 KnownSign, Q, Depth + 1);
5177 Known.copysign(KnownSign);
5178 break;
5179 }
5180 case Intrinsic::fma:
5181 case Intrinsic::fmuladd: {
5182 if ((InterestedClasses & fcNegative) == fcNone)
5183 break;
5184
5185 // FIXME: This should check isGuaranteedNotToBeUndef
5186 if (II->getArgOperand(0) == II->getArgOperand(1)) {
5187 KnownFPClass KnownSrc, KnownAddend;
5188 computeKnownFPClass(II->getArgOperand(2), DemandedElts,
5189 InterestedClasses, KnownAddend, Q, Depth + 1);
5190 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5191 InterestedClasses, KnownSrc, Q, Depth + 1);
5192
5193 const Function *F = II->getFunction();
5194 const fltSemantics &FltSem =
5195 II->getType()->getScalarType()->getFltSemantics();
5197 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5198
5199 if (KnownNotFromFlags & fcNan) {
5200 KnownSrc.knownNot(fcNan);
5201 KnownAddend.knownNot(fcNan);
5202 }
5203
5204 if (KnownNotFromFlags & fcInf) {
5205 KnownSrc.knownNot(fcInf);
5206 KnownAddend.knownNot(fcInf);
5207 }
5208
5209 Known = KnownFPClass::fma_square(KnownSrc, KnownAddend, Mode);
5210 break;
5211 }
5212
5213 KnownFPClass KnownSrc[3];
5214 for (int I = 0; I != 3; ++I) {
5215 computeKnownFPClass(II->getArgOperand(I), DemandedElts,
5216 InterestedClasses, KnownSrc[I], Q, Depth + 1);
5217 if (KnownSrc[I].isUnknown())
5218 return;
5219
5220 if (KnownNotFromFlags & fcNan)
5221 KnownSrc[I].knownNot(fcNan);
5222 if (KnownNotFromFlags & fcInf)
5223 KnownSrc[I].knownNot(fcInf);
5224 }
5225
5226 const Function *F = II->getFunction();
5227 const fltSemantics &FltSem =
5228 II->getType()->getScalarType()->getFltSemantics();
5230 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5231 Known = KnownFPClass::fma(KnownSrc[0], KnownSrc[1], KnownSrc[2], Mode);
5232 break;
5233 }
5234 case Intrinsic::sqrt:
5235 case Intrinsic::experimental_constrained_sqrt: {
5236 KnownFPClass KnownSrc;
5237 FPClassTest InterestedSrcs = InterestedClasses;
5238 if (InterestedClasses & fcNan)
5239 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5240
5241 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5242 KnownSrc, Q, Depth + 1);
5243
5245
5246 bool HasNSZ = Q.IIQ.hasNoSignedZeros(II);
5247 if (!HasNSZ) {
5248 const Function *F = II->getFunction();
5249 const fltSemantics &FltSem =
5250 II->getType()->getScalarType()->getFltSemantics();
5251 Mode = F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5252 }
5253
5254 Known = KnownFPClass::sqrt(KnownSrc, Mode);
5255 if (HasNSZ)
5256 Known.knownNot(fcNegZero);
5257
5258 break;
5259 }
5260 case Intrinsic::sin:
5261 case Intrinsic::cos: {
5262 // Return NaN on infinite inputs.
5263 KnownFPClass KnownSrc;
5264 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5265 KnownSrc, Q, Depth + 1);
5266 Known = IID == Intrinsic::sin ? KnownFPClass::sin(KnownSrc)
5267 : KnownFPClass::cos(KnownSrc);
5268 break;
5269 }
5270 case Intrinsic::maxnum:
5271 case Intrinsic::minnum:
5272 case Intrinsic::minimum:
5273 case Intrinsic::maximum:
5274 case Intrinsic::minimumnum:
5275 case Intrinsic::maximumnum: {
5276 KnownFPClass KnownLHS, KnownRHS;
5277 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5278 KnownLHS, Q, Depth + 1);
5279 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5280 KnownRHS, Q, Depth + 1);
5281
5282 const Function *F = II->getFunction();
5283
5285 F ? F->getDenormalMode(
5286 II->getType()->getScalarType()->getFltSemantics())
5288
5289 Known = KnownFPClass::minMaxLike(KnownLHS, KnownRHS, getMinMaxKind(IID),
5290 Mode);
5291 break;
5292 }
5293 case Intrinsic::canonicalize: {
5294 KnownFPClass KnownSrc;
5295 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5296 KnownSrc, Q, Depth + 1);
5297
5298 const Function *F = II->getFunction();
5299 DenormalMode DenormMode =
5300 F ? F->getDenormalMode(
5301 II->getType()->getScalarType()->getFltSemantics())
5303 Known = KnownFPClass::canonicalize(KnownSrc, DenormMode);
5304 break;
5305 }
5306 case Intrinsic::vector_reduce_fmax:
5307 case Intrinsic::vector_reduce_fmin:
5308 case Intrinsic::vector_reduce_fmaximum:
5309 case Intrinsic::vector_reduce_fminimum: {
5310 // reduce min/max will choose an element from one of the vector elements,
5311 // so we can infer and class information that is common to all elements.
5312 Known = computeKnownFPClass(II->getArgOperand(0), II->getFastMathFlags(),
5313 InterestedClasses, Q, Depth + 1);
5314 // Can only propagate sign if output is never NaN.
5315 if (!Known.isKnownNeverNaN())
5316 Known.SignBit.reset();
5317 break;
5318 }
5319 // reverse preserves all characteristics of the input vec's element.
5320 case Intrinsic::vector_reverse:
5321 Known = computeKnownFPClass(
5322 II->getArgOperand(0), DemandedElts.reverseBits(),
5323 II->getFastMathFlags(), InterestedClasses, Q, Depth + 1);
5324 break;
5325 case Intrinsic::trunc:
5326 case Intrinsic::floor:
5327 case Intrinsic::ceil:
5328 case Intrinsic::rint:
5329 case Intrinsic::nearbyint:
5330 case Intrinsic::round:
5331 case Intrinsic::roundeven: {
5332 KnownFPClass KnownSrc;
5333 FPClassTest InterestedSrcs = InterestedClasses;
5334 if (InterestedSrcs & fcPosFinite)
5335 InterestedSrcs |= fcPosFinite;
5336 if (InterestedSrcs & fcNegFinite)
5337 InterestedSrcs |= fcNegFinite;
5338 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5339 KnownSrc, Q, Depth + 1);
5340
5342 KnownSrc, IID == Intrinsic::trunc,
5343 V->getType()->getScalarType()->isMultiUnitFPType());
5344 break;
5345 }
5346 case Intrinsic::exp:
5347 case Intrinsic::exp2:
5348 case Intrinsic::exp10:
5349 case Intrinsic::amdgcn_exp2: {
5350 KnownFPClass KnownSrc;
5351 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5352 KnownSrc, Q, Depth + 1);
5353
5354 Known = KnownFPClass::exp(KnownSrc);
5355
5356 Type *EltTy = II->getType()->getScalarType();
5357 if (IID == Intrinsic::amdgcn_exp2 && EltTy->isFloatTy())
5358 Known.knownNot(fcSubnormal);
5359
5360 break;
5361 }
5362 case Intrinsic::fptrunc_round: {
5363 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,
5364 Q, Depth);
5365 break;
5366 }
5367 case Intrinsic::log:
5368 case Intrinsic::log10:
5369 case Intrinsic::log2:
5370 case Intrinsic::experimental_constrained_log:
5371 case Intrinsic::experimental_constrained_log10:
5372 case Intrinsic::experimental_constrained_log2:
5373 case Intrinsic::amdgcn_log: {
5374 Type *EltTy = II->getType()->getScalarType();
5375
5376 // log(+inf) -> +inf
5377 // log([+-]0.0) -> -inf
5378 // log(-inf) -> nan
5379 // log(-x) -> nan
5380 if ((InterestedClasses & (fcNan | fcInf)) != fcNone) {
5381 FPClassTest InterestedSrcs = InterestedClasses;
5382 if ((InterestedClasses & fcNegInf) != fcNone)
5383 InterestedSrcs |= fcZero | fcSubnormal;
5384 if ((InterestedClasses & fcNan) != fcNone)
5385 InterestedSrcs |= fcNan | fcNegative;
5386
5387 KnownFPClass KnownSrc;
5388 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5389 KnownSrc, Q, Depth + 1);
5390
5391 const Function *F = II->getFunction();
5392 DenormalMode Mode = F ? F->getDenormalMode(EltTy->getFltSemantics())
5394 Known = KnownFPClass::log(KnownSrc, Mode);
5395 }
5396
5397 break;
5398 }
5399 case Intrinsic::powi: {
5400 if ((InterestedClasses & fcNegative) == fcNone)
5401 break;
5402
5403 const Value *Exp = II->getArgOperand(1);
5404 Type *ExpTy = Exp->getType();
5405 unsigned BitWidth = ExpTy->getScalarType()->getIntegerBitWidth();
5406 KnownBits ExponentKnownBits(BitWidth);
5407 computeKnownBits(Exp, isa<VectorType>(ExpTy) ? DemandedElts : APInt(1, 1),
5408 ExponentKnownBits, Q, Depth + 1);
5409
5410 KnownFPClass KnownSrc;
5411 if (ExponentKnownBits.isZero() || !ExponentKnownBits.isEven()) {
5412 computeKnownFPClass(II->getArgOperand(0), DemandedElts, fcNegative,
5413 KnownSrc, Q, Depth + 1);
5414 }
5415
5416 Known = KnownFPClass::powi(KnownSrc, ExponentKnownBits);
5417 break;
5418 }
5419 case Intrinsic::ldexp: {
5420 KnownFPClass KnownSrc;
5421 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5422 KnownSrc, Q, Depth + 1);
5423 // Can refine inf/zero handling based on the exponent operand.
5424 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;
5425
5426 KnownBits ExpBits;
5427 if ((KnownSrc.KnownFPClasses & ExpInfoMask) != fcNone) {
5428 const Value *ExpArg = II->getArgOperand(1);
5429 ExpBits = computeKnownBits(ExpArg, DemandedElts, Q, Depth + 1);
5430 }
5431
5432 const fltSemantics &Flt =
5433 II->getType()->getScalarType()->getFltSemantics();
5434
5435 const Function *F = II->getFunction();
5437 F ? F->getDenormalMode(Flt) : DenormalMode::getDynamic();
5438
5439 Known = KnownFPClass::ldexp(KnownSrc, ExpBits, Flt, Mode);
5440 break;
5441 }
5442 case Intrinsic::arithmetic_fence: {
5443 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5444 Known, Q, Depth + 1);
5445 break;
5446 }
5447 case Intrinsic::experimental_constrained_sitofp:
5448 case Intrinsic::experimental_constrained_uitofp:
5449 // Cannot produce nan
5450 Known.knownNot(fcNan);
5451
5452 // sitofp and uitofp turn into +0.0 for zero.
5453 Known.knownNot(fcNegZero);
5454
5455 // Integers cannot be subnormal
5456 Known.knownNot(fcSubnormal);
5457
5458 if (IID == Intrinsic::experimental_constrained_uitofp)
5459 Known.signBitMustBeZero();
5460
5461 // TODO: Copy inf handling from instructions
5462 break;
5463
5464 case Intrinsic::amdgcn_fract: {
5465 Known.knownNot(fcInf);
5466
5467 if (InterestedClasses & fcNan) {
5468 KnownFPClass KnownSrc;
5469 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5470 InterestedClasses, KnownSrc, Q, Depth + 1);
5471
5472 if (KnownSrc.isKnownNeverInfOrNaN())
5473 Known.knownNot(fcNan);
5474 else if (KnownSrc.isKnownNever(fcSNan))
5475 Known.knownNot(fcSNan);
5476 }
5477
5478 break;
5479 }
5480 case Intrinsic::amdgcn_rcp: {
5481 KnownFPClass KnownSrc;
5482 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5483 KnownSrc, Q, Depth + 1);
5484
5485 Known.propagateNaN(KnownSrc);
5486
5487 Type *EltTy = II->getType()->getScalarType();
5488
5489 // f32 denormal always flushed.
5490 if (EltTy->isFloatTy()) {
5491 Known.knownNot(fcSubnormal);
5492 KnownSrc.knownNot(fcSubnormal);
5493 }
5494
5495 if (KnownSrc.isKnownNever(fcNegative))
5496 Known.knownNot(fcNegative);
5497 if (KnownSrc.isKnownNever(fcPositive))
5498 Known.knownNot(fcPositive);
5499
5500 if (const Function *F = II->getFunction()) {
5501 DenormalMode Mode = F->getDenormalMode(EltTy->getFltSemantics());
5502 if (KnownSrc.isKnownNeverLogicalPosZero(Mode))
5503 Known.knownNot(fcPosInf);
5504 if (KnownSrc.isKnownNeverLogicalNegZero(Mode))
5505 Known.knownNot(fcNegInf);
5506 }
5507
5508 break;
5509 }
5510 case Intrinsic::amdgcn_rsq: {
5511 KnownFPClass KnownSrc;
5512 // The only negative value that can be returned is -inf for -0 inputs.
5514
5515 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5516 KnownSrc, Q, Depth + 1);
5517
5518 // Negative -> nan
5519 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
5520 Known.knownNot(fcNan);
5521 else if (KnownSrc.isKnownNever(fcSNan))
5522 Known.knownNot(fcSNan);
5523
5524 // +inf -> +0
5525 if (KnownSrc.isKnownNeverPosInfinity())
5526 Known.knownNot(fcPosZero);
5527
5528 Type *EltTy = II->getType()->getScalarType();
5529
5530 // f32 denormal always flushed.
5531 if (EltTy->isFloatTy())
5532 Known.knownNot(fcPosSubnormal);
5533
5534 if (const Function *F = II->getFunction()) {
5535 DenormalMode Mode = F->getDenormalMode(EltTy->getFltSemantics());
5536
5537 // -0 -> -inf
5538 if (KnownSrc.isKnownNeverLogicalNegZero(Mode))
5539 Known.knownNot(fcNegInf);
5540
5541 // +0 -> +inf
5542 if (KnownSrc.isKnownNeverLogicalPosZero(Mode))
5543 Known.knownNot(fcPosInf);
5544 }
5545
5546 break;
5547 }
5548 case Intrinsic::amdgcn_trig_preop: {
5549 // Always returns a value [0, 1)
5550 Known.knownNot(fcNan | fcInf | fcNegative);
5551 break;
5552 }
5553 default:
5554 break;
5555 }
5556
5557 break;
5558 }
5559 case Instruction::FAdd:
5560 case Instruction::FSub: {
5561 KnownFPClass KnownLHS, KnownRHS;
5562 bool WantNegative =
5563 Op->getOpcode() == Instruction::FAdd &&
5564 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;
5565 bool WantNaN = (InterestedClasses & fcNan) != fcNone;
5566 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;
5567
5568 if (!WantNaN && !WantNegative && !WantNegZero)
5569 break;
5570
5571 FPClassTest InterestedSrcs = InterestedClasses;
5572 if (WantNegative)
5573 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5574 if (InterestedClasses & fcNan)
5575 InterestedSrcs |= fcInf;
5576 computeKnownFPClass(Op->getOperand(1), DemandedElts, InterestedSrcs,
5577 KnownRHS, Q, Depth + 1);
5578
5579 // Special case fadd x, x, which is the canonical form of fmul x, 2.
5580 bool Self = Op->getOperand(0) == Op->getOperand(1) &&
5581 isGuaranteedNotToBeUndef(Op->getOperand(0), Q.AC, Q.CxtI, Q.DT,
5582 Depth + 1);
5583 if (Self)
5584 KnownLHS = KnownRHS;
5585
5586 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||
5587 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) ||
5588 WantNegZero || Opc == Instruction::FSub) {
5589
5590 // FIXME: Context function should always be passed in separately
5591 const Function *F = cast<Instruction>(Op)->getFunction();
5592 const fltSemantics &FltSem =
5593 Op->getType()->getScalarType()->getFltSemantics();
5595 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5596
5597 if (Self && Opc == Instruction::FAdd) {
5598 Known = KnownFPClass::fadd_self(KnownLHS, Mode);
5599 } else {
5600 // RHS is canonically cheaper to compute. Skip inspecting the LHS if
5601 // there's no point.
5602
5603 if (!Self) {
5604 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedSrcs,
5605 KnownLHS, Q, Depth + 1);
5606 }
5607
5608 Known = Opc == Instruction::FAdd
5609 ? KnownFPClass::fadd(KnownLHS, KnownRHS, Mode)
5610 : KnownFPClass::fsub(KnownLHS, KnownRHS, Mode);
5611 }
5612 }
5613
5614 break;
5615 }
5616 case Instruction::FMul: {
5617 const Function *F = cast<Instruction>(Op)->getFunction();
5619 F ? F->getDenormalMode(
5620 Op->getType()->getScalarType()->getFltSemantics())
5622
5623 Value *LHS = Op->getOperand(0);
5624 Value *RHS = Op->getOperand(1);
5625 // X * X is always non-negative or a NaN.
5626 // FIXME: Should check isGuaranteedNotToBeUndef
5627 if (LHS == RHS) {
5628 KnownFPClass KnownSrc;
5629 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownSrc, Q,
5630 Depth + 1);
5631 Known = KnownFPClass::square(KnownSrc, Mode);
5632 break;
5633 }
5634
5635 KnownFPClass KnownLHS, KnownRHS;
5636
5637 const APFloat *CRHS;
5638 if (match(RHS, m_APFloat(CRHS))) {
5639 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Q,
5640 Depth + 1);
5641 Known = KnownFPClass::fmul(KnownLHS, *CRHS, Mode);
5642 } else {
5643 computeKnownFPClass(RHS, DemandedElts, fcAllFlags, KnownRHS, Q,
5644 Depth + 1);
5645 // TODO: Improve accuracy in unfused FMA pattern. We can prove an
5646 // additional not-nan if the addend is known-not negative infinity if the
5647 // multiply is known-not infinity.
5648
5649 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Q,
5650 Depth + 1);
5651 Known = KnownFPClass::fmul(KnownLHS, KnownRHS, Mode);
5652 }
5653
5654 /// Propgate no-infs if the other source is known smaller than one, such
5655 /// that this cannot introduce overflow.
5656 if (KnownLHS.isKnownNever(fcInf) && isAbsoluteValueULEOne(RHS))
5657 Known.knownNot(fcInf);
5658 else if (KnownRHS.isKnownNever(fcInf) && isAbsoluteValueULEOne(LHS))
5659 Known.knownNot(fcInf);
5660
5661 break;
5662 }
5663 case Instruction::FDiv:
5664 case Instruction::FRem: {
5665 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
5666
5667 if (Op->getOpcode() == Instruction::FRem)
5668 Known.knownNot(fcInf);
5669
5670 if (Op->getOperand(0) == Op->getOperand(1) &&
5671 isGuaranteedNotToBeUndef(Op->getOperand(0), Q.AC, Q.CxtI, Q.DT)) {
5672 if (Op->getOpcode() == Instruction::FDiv) {
5673 // X / X is always exactly 1.0 or a NaN.
5675 } else {
5676 // X % X is always exactly [+-]0.0 or a NaN.
5677 Known.KnownFPClasses = fcNan | fcZero;
5678 }
5679
5680 if (!WantNan)
5681 break;
5682
5683 KnownFPClass KnownSrc;
5684 computeKnownFPClass(Op->getOperand(0), DemandedElts,
5685 fcNan | fcInf | fcZero | fcSubnormal, KnownSrc, Q,
5686 Depth + 1);
5687 const Function *F = cast<Instruction>(Op)->getFunction();
5688 const fltSemantics &FltSem =
5689 Op->getType()->getScalarType()->getFltSemantics();
5690
5692 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5693
5694 Known = Op->getOpcode() == Instruction::FDiv
5695 ? KnownFPClass::fdiv_self(KnownSrc, Mode)
5696 : KnownFPClass::frem_self(KnownSrc, Mode);
5697 break;
5698 }
5699
5700 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
5701 const bool WantPositive =
5702 Opc == Instruction::FRem && (InterestedClasses & fcPositive) != fcNone;
5703 if (!WantNan && !WantNegative && !WantPositive)
5704 break;
5705
5706 KnownFPClass KnownLHS, KnownRHS;
5707
5708 computeKnownFPClass(Op->getOperand(1), DemandedElts,
5709 fcNan | fcInf | fcZero | fcNegative, KnownRHS, Q,
5710 Depth + 1);
5711
5712 bool KnowSomethingUseful = KnownRHS.isKnownNeverNaN() ||
5713 KnownRHS.isKnownNever(fcNegative) ||
5714 KnownRHS.isKnownNever(fcPositive);
5715
5716 if (KnowSomethingUseful || WantPositive) {
5717 computeKnownFPClass(Op->getOperand(0), DemandedElts, fcAllFlags, KnownLHS,
5718 Q, Depth + 1);
5719 }
5720
5721 const Function *F = cast<Instruction>(Op)->getFunction();
5722 const fltSemantics &FltSem =
5723 Op->getType()->getScalarType()->getFltSemantics();
5724
5725 if (Op->getOpcode() == Instruction::FDiv) {
5727 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5728 Known = KnownFPClass::fdiv(KnownLHS, KnownRHS, Mode);
5729 } else {
5730 // Inf REM x and x REM 0 produce NaN.
5731 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5732 KnownLHS.isKnownNeverInfinity() && F &&
5733 KnownRHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))) {
5734 Known.knownNot(fcNan);
5735 }
5736
5737 // The sign for frem is the same as the first operand.
5738 if (KnownLHS.cannotBeOrderedLessThanZero())
5740 if (KnownLHS.cannotBeOrderedGreaterThanZero())
5742
5743 // See if we can be more aggressive about the sign of 0.
5744 if (KnownLHS.isKnownNever(fcNegative))
5745 Known.knownNot(fcNegative);
5746 if (KnownLHS.isKnownNever(fcPositive))
5747 Known.knownNot(fcPositive);
5748 }
5749
5750 break;
5751 }
5752 case Instruction::FPExt: {
5753 KnownFPClass KnownSrc;
5754 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5755 KnownSrc, Q, Depth + 1);
5756
5757 const fltSemantics &DstTy =
5758 Op->getType()->getScalarType()->getFltSemantics();
5759 const fltSemantics &SrcTy =
5760 Op->getOperand(0)->getType()->getScalarType()->getFltSemantics();
5761
5762 Known = KnownFPClass::fpext(KnownSrc, DstTy, SrcTy);
5763 break;
5764 }
5765 case Instruction::FPTrunc: {
5766 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known, Q,
5767 Depth);
5768 break;
5769 }
5770 case Instruction::SIToFP:
5771 case Instruction::UIToFP: {
5772 // Cannot produce nan
5773 Known.knownNot(fcNan);
5774
5775 // Integers cannot be subnormal
5776 Known.knownNot(fcSubnormal);
5777
5778 // sitofp and uitofp turn into +0.0 for zero.
5779 Known.knownNot(fcNegZero);
5780
5781 // UIToFP is always non-negative regardless of known bits.
5782 if (Op->getOpcode() == Instruction::UIToFP)
5783 Known.signBitMustBeZero();
5784
5785 // Only compute known bits if we can learn something useful from them.
5786 if (!(InterestedClasses & (fcPosZero | fcNormal | fcInf)))
5787 break;
5788
5789 KnownBits IntKnown =
5790 computeKnownBits(Op->getOperand(0), DemandedElts, Q, Depth + 1);
5791
5792 // If the integer is non-zero, the result cannot be +0.0
5793 if (IntKnown.isNonZero())
5794 Known.knownNot(fcPosZero);
5795
5796 if (Op->getOpcode() == Instruction::SIToFP) {
5797 // If the signed integer is known non-negative, the result is
5798 // non-negative. If the signed integer is known negative, the result is
5799 // negative.
5800 if (IntKnown.isNonNegative()) {
5801 Known.signBitMustBeZero();
5802 } else if (IntKnown.isNegative()) {
5803 Known.signBitMustBeOne();
5804 }
5805 }
5806
5807 // Guard kept for ilogb()
5808 if (InterestedClasses & fcInf) {
5809 // Get width of largest magnitude integer known.
5810 // This still works for a signed minimum value because the largest FP
5811 // value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx).
5812 int IntSize = IntKnown.getBitWidth();
5813 if (Op->getOpcode() == Instruction::UIToFP)
5814 IntSize -= IntKnown.countMinLeadingZeros();
5815 else if (Op->getOpcode() == Instruction::SIToFP)
5816 IntSize -= IntKnown.countMinSignBits();
5817
5818 // If the exponent of the largest finite FP value can hold the largest
5819 // integer, the result of the cast must be finite.
5820 Type *FPTy = Op->getType()->getScalarType();
5821 if (ilogb(APFloat::getLargest(FPTy->getFltSemantics())) >= IntSize)
5822 Known.knownNot(fcInf);
5823 }
5824
5825 break;
5826 }
5827 case Instruction::ExtractElement: {
5828 // Look through extract element. If the index is non-constant or
5829 // out-of-range demand all elements, otherwise just the extracted element.
5830 const Value *Vec = Op->getOperand(0);
5831
5832 APInt DemandedVecElts;
5833 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
5834 unsigned NumElts = VecTy->getNumElements();
5835 DemandedVecElts = APInt::getAllOnes(NumElts);
5836 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(1));
5837 if (CIdx && CIdx->getValue().ult(NumElts))
5838 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
5839 } else {
5840 DemandedVecElts = APInt(1, 1);
5841 }
5842
5843 return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,
5844 Q, Depth + 1);
5845 }
5846 case Instruction::InsertElement: {
5847 if (isa<ScalableVectorType>(Op->getType()))
5848 return;
5849
5850 const Value *Vec = Op->getOperand(0);
5851 const Value *Elt = Op->getOperand(1);
5852 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(2));
5853 unsigned NumElts = DemandedElts.getBitWidth();
5854 APInt DemandedVecElts = DemandedElts;
5855 bool NeedsElt = true;
5856 // If we know the index we are inserting to, clear it from Vec check.
5857 if (CIdx && CIdx->getValue().ult(NumElts)) {
5858 DemandedVecElts.clearBit(CIdx->getZExtValue());
5859 NeedsElt = DemandedElts[CIdx->getZExtValue()];
5860 }
5861
5862 // Do we demand the inserted element?
5863 if (NeedsElt) {
5864 computeKnownFPClass(Elt, Known, InterestedClasses, Q, Depth + 1);
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 // Do we need anymore elements from Vec?
5873 if (!DemandedVecElts.isZero()) {
5874 KnownFPClass Known2;
5875 computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2, Q,
5876 Depth + 1);
5877 Known |= Known2;
5878 }
5879
5880 break;
5881 }
5882 case Instruction::ShuffleVector: {
5883 // Handle vector splat idiom
5884 if (Value *Splat = getSplatValue(V)) {
5885 computeKnownFPClass(Splat, Known, InterestedClasses, Q, Depth + 1);
5886 break;
5887 }
5888
5889 // For undef elements, we don't know anything about the common state of
5890 // the shuffle result.
5891 APInt DemandedLHS, DemandedRHS;
5892 auto *Shuf = dyn_cast<ShuffleVectorInst>(Op);
5893 if (!Shuf || !getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
5894 return;
5895
5896 if (!!DemandedLHS) {
5897 const Value *LHS = Shuf->getOperand(0);
5898 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known, Q,
5899 Depth + 1);
5900
5901 // If we don't know any bits, early out.
5902 if (Known.isUnknown())
5903 break;
5904 } else {
5905 Known.KnownFPClasses = fcNone;
5906 }
5907
5908 if (!!DemandedRHS) {
5909 KnownFPClass Known2;
5910 const Value *RHS = Shuf->getOperand(1);
5911 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2, Q,
5912 Depth + 1);
5913 Known |= Known2;
5914 }
5915
5916 break;
5917 }
5918 case Instruction::ExtractValue: {
5919 const ExtractValueInst *Extract = cast<ExtractValueInst>(Op);
5920 ArrayRef<unsigned> Indices = Extract->getIndices();
5921 const Value *Src = Extract->getAggregateOperand();
5922 if (isa<StructType>(Src->getType()) && Indices.size() == 1 &&
5923 Indices[0] == 0) {
5924 if (const auto *II = dyn_cast<IntrinsicInst>(Src)) {
5925 switch (II->getIntrinsicID()) {
5926 case Intrinsic::frexp: {
5927 Known.knownNot(fcSubnormal);
5928
5929 KnownFPClass KnownSrc;
5930 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5931 InterestedClasses, KnownSrc, Q, Depth + 1);
5932
5933 const Function *F = cast<Instruction>(Op)->getFunction();
5934 const fltSemantics &FltSem =
5935 Op->getType()->getScalarType()->getFltSemantics();
5936
5938 F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5939 Known = KnownFPClass::frexp_mant(KnownSrc, Mode);
5940 return;
5941 }
5942 default:
5943 break;
5944 }
5945 }
5946 }
5947
5948 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Q,
5949 Depth + 1);
5950 break;
5951 }
5952 case Instruction::PHI: {
5953 const PHINode *P = cast<PHINode>(Op);
5954 // Unreachable blocks may have zero-operand PHI nodes.
5955 if (P->getNumIncomingValues() == 0)
5956 break;
5957
5958 // Otherwise take the unions of the known bit sets of the operands,
5959 // taking conservative care to avoid excessive recursion.
5960 const unsigned PhiRecursionLimit = MaxAnalysisRecursionDepth - 2;
5961
5962 if (Depth < PhiRecursionLimit) {
5963 // Skip if every incoming value references to ourself.
5964 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
5965 break;
5966
5967 bool First = true;
5968
5969 for (const Use &U : P->operands()) {
5970 Value *IncValue;
5971 Instruction *CxtI;
5972 breakSelfRecursivePHI(&U, P, IncValue, CxtI);
5973 // Skip direct self references.
5974 if (IncValue == P)
5975 continue;
5976
5977 KnownFPClass KnownSrc;
5978 // Recurse, but cap the recursion to two levels, because we don't want
5979 // to waste time spinning around in loops. We need at least depth 2 to
5980 // detect known sign bits.
5981 computeKnownFPClass(IncValue, DemandedElts, InterestedClasses, KnownSrc,
5983 PhiRecursionLimit);
5984
5985 if (First) {
5986 Known = KnownSrc;
5987 First = false;
5988 } else {
5989 Known |= KnownSrc;
5990 }
5991
5992 if (Known.KnownFPClasses == fcAllFlags)
5993 break;
5994 }
5995 }
5996
5997 // Look for the case of a for loop which has a positive
5998 // initial value and is incremented by a squared value.
5999 // This will propagate sign information out of such loops.
6000 if (P->getNumIncomingValues() != 2 || Known.cannotBeOrderedLessThanZero())
6001 break;
6002 for (unsigned I = 0; I < 2; I++) {
6003 Value *RecurValue = P->getIncomingValue(1 - I);
6005 if (!II)
6006 continue;
6007 Value *R, *L, *Init;
6008 PHINode *PN;
6010 PN == P) {
6011 switch (II->getIntrinsicID()) {
6012 case Intrinsic::fma:
6013 case Intrinsic::fmuladd: {
6014 KnownFPClass KnownStart;
6015 computeKnownFPClass(Init, DemandedElts, InterestedClasses, KnownStart,
6016 Q, Depth + 1);
6017 if (KnownStart.cannotBeOrderedLessThanZero() && L == R &&
6018 isGuaranteedNotToBeUndef(L, Q.AC, Q.CxtI, Q.DT, Depth + 1))
6020 break;
6021 }
6022 }
6023 }
6024 }
6025 break;
6026 }
6027 case Instruction::BitCast: {
6028 const Value *Src;
6029 if (!match(Op, m_ElementWiseBitCast(m_Value(Src))) ||
6030 !Src->getType()->isIntOrIntVectorTy())
6031 break;
6032
6033 const Type *Ty = Op->getType();
6034
6035 Value *CastLHS, *CastRHS;
6036
6037 // Match bitcast(umax(bitcast(a), bitcast(b)))
6038 if (match(Src, m_c_MaxOrMin(m_BitCast(m_Value(CastLHS)),
6039 m_BitCast(m_Value(CastRHS)))) &&
6040 CastLHS->getType() == Ty && CastRHS->getType() == Ty) {
6041 KnownFPClass KnownLHS, KnownRHS;
6042 computeKnownFPClass(CastRHS, DemandedElts, InterestedClasses, KnownRHS, Q,
6043 Depth + 1);
6044 if (!KnownRHS.isUnknown()) {
6045 computeKnownFPClass(CastLHS, DemandedElts, InterestedClasses, KnownLHS,
6046 Q, Depth + 1);
6047 Known = KnownLHS | KnownRHS;
6048 }
6049
6050 return;
6051 }
6052
6053 const Type *EltTy = Ty->getScalarType();
6054 KnownBits Bits(EltTy->getPrimitiveSizeInBits());
6055 computeKnownBits(Src, DemandedElts, Bits, Q, Depth + 1);
6056
6057 Known = KnownFPClass::bitcast(EltTy->getFltSemantics(), Bits);
6058 break;
6059 }
6060 default:
6061 break;
6062 }
6063}
6064
6066 const APInt &DemandedElts,
6067 FPClassTest InterestedClasses,
6068 const SimplifyQuery &SQ,
6069 unsigned Depth) {
6070 KnownFPClass KnownClasses;
6071 ::computeKnownFPClass(V, DemandedElts, InterestedClasses, KnownClasses, SQ,
6072 Depth);
6073 return KnownClasses;
6074}
6075
6077 FPClassTest InterestedClasses,
6078 const SimplifyQuery &SQ,
6079 unsigned Depth) {
6080 KnownFPClass Known;
6081 ::computeKnownFPClass(V, Known, InterestedClasses, SQ, Depth);
6082 return Known;
6083}
6084
6086 const Value *V, const DataLayout &DL, FPClassTest InterestedClasses,
6087 const TargetLibraryInfo *TLI, AssumptionCache *AC, const Instruction *CxtI,
6088 const DominatorTree *DT, bool UseInstrInfo, unsigned Depth) {
6089 return computeKnownFPClass(V, InterestedClasses,
6090 SimplifyQuery(DL, TLI, DT, AC, CxtI, UseInstrInfo),
6091 Depth);
6092}
6093
6095llvm::computeKnownFPClass(const Value *V, const APInt &DemandedElts,
6096 FastMathFlags FMF, FPClassTest InterestedClasses,
6097 const SimplifyQuery &SQ, unsigned Depth) {
6098 if (FMF.noNaNs())
6099 InterestedClasses &= ~fcNan;
6100 if (FMF.noInfs())
6101 InterestedClasses &= ~fcInf;
6102
6103 KnownFPClass Result =
6104 computeKnownFPClass(V, DemandedElts, InterestedClasses, SQ, Depth);
6105
6106 if (FMF.noNaNs())
6107 Result.KnownFPClasses &= ~fcNan;
6108 if (FMF.noInfs())
6109 Result.KnownFPClasses &= ~fcInf;
6110 return Result;
6111}
6112
6114 FPClassTest InterestedClasses,
6115 const SimplifyQuery &SQ,
6116 unsigned Depth) {
6117 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
6118 APInt DemandedElts =
6119 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
6120 return computeKnownFPClass(V, DemandedElts, FMF, InterestedClasses, SQ,
6121 Depth);
6122}
6123
6125 unsigned Depth) {
6127 return Known.isKnownNeverNegZero();
6128}
6129
6136
6138 unsigned Depth) {
6140 return Known.isKnownNeverInfinity();
6141}
6142
6143/// Return true if the floating-point value can never contain a NaN or infinity.
6145 unsigned Depth) {
6147 return Known.isKnownNeverNaN() && Known.isKnownNeverInfinity();
6148}
6149
6150/// Return true if the floating-point scalar value is not a NaN or if the
6151/// floating-point vector value has no NaN elements. Return false if a value
6152/// could ever be NaN.
6154 unsigned Depth) {
6156 return Known.isKnownNeverNaN();
6157}
6158
6159/// Return false if we can prove that the specified FP value's sign bit is 0.
6160/// Return true if we can prove that the specified FP value's sign bit is 1.
6161/// Otherwise return std::nullopt.
6162std::optional<bool> llvm::computeKnownFPSignBit(const Value *V,
6163 const SimplifyQuery &SQ,
6164 unsigned Depth) {
6166 return Known.SignBit;
6167}
6168
6170 auto *User = cast<Instruction>(U.getUser());
6171 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6172 if (FPOp->hasNoSignedZeros())
6173 return true;
6174 }
6175
6176 switch (User->getOpcode()) {
6177 case Instruction::FPToSI:
6178 case Instruction::FPToUI:
6179 return true;
6180 case Instruction::FCmp:
6181 // fcmp treats both positive and negative zero as equal.
6182 return true;
6183 case Instruction::Call:
6184 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6185 switch (II->getIntrinsicID()) {
6186 case Intrinsic::fabs:
6187 return true;
6188 case Intrinsic::copysign:
6189 return U.getOperandNo() == 0;
6190 case Intrinsic::is_fpclass:
6191 case Intrinsic::vp_is_fpclass: {
6192 auto Test =
6193 static_cast<FPClassTest>(
6194 cast<ConstantInt>(II->getArgOperand(1))->getZExtValue()) &
6197 }
6198 default:
6199 return false;
6200 }
6201 }
6202 return false;
6203 default:
6204 return false;
6205 }
6206}
6207
6209 auto *User = cast<Instruction>(U.getUser());
6210 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6211 if (FPOp->hasNoNaNs())
6212 return true;
6213 }
6214
6215 switch (User->getOpcode()) {
6216 case Instruction::FPToSI:
6217 case Instruction::FPToUI:
6218 return true;
6219 // Proper FP math operations ignore the sign bit of NaN.
6220 case Instruction::FAdd:
6221 case Instruction::FSub:
6222 case Instruction::FMul:
6223 case Instruction::FDiv:
6224 case Instruction::FRem:
6225 case Instruction::FPTrunc:
6226 case Instruction::FPExt:
6227 case Instruction::FCmp:
6228 return true;
6229 // Bitwise FP operations should preserve the sign bit of NaN.
6230 case Instruction::FNeg:
6231 case Instruction::Select:
6232 case Instruction::PHI:
6233 return false;
6234 case Instruction::Ret:
6235 return User->getFunction()->getAttributes().getRetNoFPClass() &
6237 case Instruction::Call:
6238 case Instruction::Invoke: {
6239 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6240 switch (II->getIntrinsicID()) {
6241 case Intrinsic::fabs:
6242 return true;
6243 case Intrinsic::copysign:
6244 return U.getOperandNo() == 0;
6245 // Other proper FP math intrinsics ignore the sign bit of NaN.
6246 case Intrinsic::maxnum:
6247 case Intrinsic::minnum:
6248 case Intrinsic::maximum:
6249 case Intrinsic::minimum:
6250 case Intrinsic::maximumnum:
6251 case Intrinsic::minimumnum:
6252 case Intrinsic::canonicalize:
6253 case Intrinsic::fma:
6254 case Intrinsic::fmuladd:
6255 case Intrinsic::sqrt:
6256 case Intrinsic::pow:
6257 case Intrinsic::powi:
6258 case Intrinsic::fptoui_sat:
6259 case Intrinsic::fptosi_sat:
6260 case Intrinsic::is_fpclass:
6261 case Intrinsic::vp_is_fpclass:
6262 return true;
6263 default:
6264 return false;
6265 }
6266 }
6267
6268 FPClassTest NoFPClass =
6269 cast<CallBase>(User)->getParamNoFPClass(U.getOperandNo());
6270 return NoFPClass & FPClassTest::fcNan;
6271 }
6272 default:
6273 return false;
6274 }
6275}
6276
6278 FastMathFlags FMF) {
6279 if (isa<PoisonValue>(V))
6280 return true;
6281 if (isa<UndefValue>(V))
6282 return false;
6283
6284 if (match(V, m_CheckedFp([](const APFloat &Val) { return Val.isInteger(); })))
6285 return true;
6286
6288 if (!I)
6289 return false;
6290
6291 switch (I->getOpcode()) {
6292 case Instruction::SIToFP:
6293 case Instruction::UIToFP:
6294 // TODO: Could check nofpclass(inf) on incoming argument
6295 if (FMF.noInfs())
6296 return true;
6297
6298 // Need to check int size cannot produce infinity, which computeKnownFPClass
6299 // knows how to do already.
6300 return isKnownNeverInfinity(I, SQ);
6301 case Instruction::Call: {
6302 const CallInst *CI = cast<CallInst>(I);
6303 switch (CI->getIntrinsicID()) {
6304 case Intrinsic::trunc:
6305 case Intrinsic::floor:
6306 case Intrinsic::ceil:
6307 case Intrinsic::rint:
6308 case Intrinsic::nearbyint:
6309 case Intrinsic::round:
6310 case Intrinsic::roundeven:
6311 return (FMF.noInfs() && FMF.noNaNs()) || isKnownNeverInfOrNaN(I, SQ);
6312 default:
6313 break;
6314 }
6315
6316 break;
6317 }
6318 default:
6319 break;
6320 }
6321
6322 return false;
6323}
6324
6326
6327 // All byte-wide stores are splatable, even of arbitrary variables.
6328 if (V->getType()->isIntegerTy(8))
6329 return V;
6330
6331 LLVMContext &Ctx = V->getContext();
6332
6333 // Undef don't care.
6334 auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
6335 if (isa<UndefValue>(V))
6336 return UndefInt8;
6337
6338 // Return poison for zero-sized type.
6339 if (DL.getTypeStoreSize(V->getType()).isZero())
6340 return PoisonValue::get(Type::getInt8Ty(Ctx));
6341
6343 if (!C) {
6344 // Conceptually, we could handle things like:
6345 // %a = zext i8 %X to i16
6346 // %b = shl i16 %a, 8
6347 // %c = or i16 %a, %b
6348 // but until there is an example that actually needs this, it doesn't seem
6349 // worth worrying about.
6350 return nullptr;
6351 }
6352
6353 // Handle 'null' ConstantArrayZero etc.
6354 if (C->isNullValue())
6356
6357 // Constant floating-point values can be handled as integer values if the
6358 // corresponding integer value is "byteable". An important case is 0.0.
6359 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
6360 Type *ScalarTy = CFP->getType()->getScalarType();
6361 if (ScalarTy->isHalfTy() || ScalarTy->isFloatTy() || ScalarTy->isDoubleTy())
6362 return isBytewiseValue(
6363 ConstantInt::get(Ctx, CFP->getValue().bitcastToAPInt()), DL);
6364
6365 // Don't handle long double formats, which have strange constraints.
6366 return nullptr;
6367 }
6368
6369 // We can handle constant integers that are multiple of 8 bits.
6370 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
6371 if (CI->getBitWidth() % 8 == 0) {
6372 if (!CI->getValue().isSplat(8))
6373 return nullptr;
6374 return ConstantInt::get(Ctx, CI->getValue().trunc(8));
6375 }
6376 }
6377
6378 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
6379 if (CE->getOpcode() == Instruction::IntToPtr) {
6380 if (auto *PtrTy = dyn_cast<PointerType>(CE->getType())) {
6381 unsigned BitWidth = DL.getPointerSizeInBits(PtrTy->getAddressSpace());
6383 CE->getOperand(0), Type::getIntNTy(Ctx, BitWidth), false, DL))
6384 return isBytewiseValue(Op, DL);
6385 }
6386 }
6387 }
6388
6389 auto Merge = [&](Value *LHS, Value *RHS) -> Value * {
6390 if (LHS == RHS)
6391 return LHS;
6392 if (!LHS || !RHS)
6393 return nullptr;
6394 if (LHS == UndefInt8)
6395 return RHS;
6396 if (RHS == UndefInt8)
6397 return LHS;
6398 return nullptr;
6399 };
6400
6402 Value *Val = UndefInt8;
6403 for (uint64_t I = 0, E = CA->getNumElements(); I != E; ++I)
6404 if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))
6405 return nullptr;
6406 return Val;
6407 }
6408
6410 Value *Val = UndefInt8;
6411 for (Value *Op : C->operands())
6412 if (!(Val = Merge(Val, isBytewiseValue(Op, DL))))
6413 return nullptr;
6414 return Val;
6415 }
6416
6417 // Don't try to handle the handful of other constants.
6418 return nullptr;
6419}
6420
6421// This is the recursive version of BuildSubAggregate. It takes a few different
6422// arguments. Idxs is the index within the nested struct From that we are
6423// looking at now (which is of type IndexedType). IdxSkip is the number of
6424// indices from Idxs that should be left out when inserting into the resulting
6425// struct. To is the result struct built so far, new insertvalue instructions
6426// build on that.
6427static Value *BuildSubAggregate(Value *From, Value *To, Type *IndexedType,
6429 unsigned IdxSkip,
6430 BasicBlock::iterator InsertBefore) {
6431 StructType *STy = dyn_cast<StructType>(IndexedType);
6432 if (STy) {
6433 // Save the original To argument so we can modify it
6434 Value *OrigTo = To;
6435 // General case, the type indexed by Idxs is a struct
6436 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
6437 // Process each struct element recursively
6438 Idxs.push_back(i);
6439 Value *PrevTo = To;
6440 To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
6441 InsertBefore);
6442 Idxs.pop_back();
6443 if (!To) {
6444 // Couldn't find any inserted value for this index? Cleanup
6445 while (PrevTo != OrigTo) {
6447 PrevTo = Del->getAggregateOperand();
6448 Del->eraseFromParent();
6449 }
6450 // Stop processing elements
6451 break;
6452 }
6453 }
6454 // If we successfully found a value for each of our subaggregates
6455 if (To)
6456 return To;
6457 }
6458 // Base case, the type indexed by SourceIdxs is not a struct, or not all of
6459 // the struct's elements had a value that was inserted directly. In the latter
6460 // case, perhaps we can't determine each of the subelements individually, but
6461 // we might be able to find the complete struct somewhere.
6462
6463 // Find the value that is at that particular spot
6464 Value *V = FindInsertedValue(From, Idxs);
6465
6466 if (!V)
6467 return nullptr;
6468
6469 // Insert the value in the new (sub) aggregate
6470 return InsertValueInst::Create(To, V, ArrayRef(Idxs).slice(IdxSkip), "tmp",
6471 InsertBefore);
6472}
6473
6474// This helper takes a nested struct and extracts a part of it (which is again a
6475// struct) into a new value. For example, given the struct:
6476// { a, { b, { c, d }, e } }
6477// and the indices "1, 1" this returns
6478// { c, d }.
6479//
6480// It does this by inserting an insertvalue for each element in the resulting
6481// struct, as opposed to just inserting a single struct. This will only work if
6482// each of the elements of the substruct are known (ie, inserted into From by an
6483// insertvalue instruction somewhere).
6484//
6485// All inserted insertvalue instructions are inserted before InsertBefore
6487 BasicBlock::iterator InsertBefore) {
6488 Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
6489 idx_range);
6490 Value *To = PoisonValue::get(IndexedType);
6491 SmallVector<unsigned, 10> Idxs(idx_range);
6492 unsigned IdxSkip = Idxs.size();
6493
6494 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
6495}
6496
6497/// Given an aggregate and a sequence of indices, see if the scalar value
6498/// indexed is already around as a register, for example if it was inserted
6499/// directly into the aggregate.
6500///
6501/// If InsertBefore is not null, this function will duplicate (modified)
6502/// insertvalues when a part of a nested struct is extracted.
6503Value *
6505 std::optional<BasicBlock::iterator> InsertBefore) {
6506 // Nothing to index? Just return V then (this is useful at the end of our
6507 // recursion).
6508 if (idx_range.empty())
6509 return V;
6510 // We have indices, so V should have an indexable type.
6511 assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
6512 "Not looking at a struct or array?");
6513 assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
6514 "Invalid indices for type?");
6515
6516 if (Constant *C = dyn_cast<Constant>(V)) {
6517 C = C->getAggregateElement(idx_range[0]);
6518 if (!C) return nullptr;
6519 return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
6520 }
6521
6523 // Loop the indices for the insertvalue instruction in parallel with the
6524 // requested indices
6525 const unsigned *req_idx = idx_range.begin();
6526 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
6527 i != e; ++i, ++req_idx) {
6528 if (req_idx == idx_range.end()) {
6529 // We can't handle this without inserting insertvalues
6530 if (!InsertBefore)
6531 return nullptr;
6532
6533 // The requested index identifies a part of a nested aggregate. Handle
6534 // this specially. For example,
6535 // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
6536 // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
6537 // %C = extractvalue {i32, { i32, i32 } } %B, 1
6538 // This can be changed into
6539 // %A = insertvalue {i32, i32 } undef, i32 10, 0
6540 // %C = insertvalue {i32, i32 } %A, i32 11, 1
6541 // which allows the unused 0,0 element from the nested struct to be
6542 // removed.
6543 return BuildSubAggregate(V, ArrayRef(idx_range.begin(), req_idx),
6544 *InsertBefore);
6545 }
6546
6547 // This insert value inserts something else than what we are looking for.
6548 // See if the (aggregate) value inserted into has the value we are
6549 // looking for, then.
6550 if (*req_idx != *i)
6551 return FindInsertedValue(I->getAggregateOperand(), idx_range,
6552 InsertBefore);
6553 }
6554 // If we end up here, the indices of the insertvalue match with those
6555 // requested (though possibly only partially). Now we recursively look at
6556 // the inserted value, passing any remaining indices.
6557 return FindInsertedValue(I->getInsertedValueOperand(),
6558 ArrayRef(req_idx, idx_range.end()), InsertBefore);
6559 }
6560
6562 // If we're extracting a value from an aggregate that was extracted from
6563 // something else, we can extract from that something else directly instead.
6564 // However, we will need to chain I's indices with the requested indices.
6565
6566 // Calculate the number of indices required
6567 unsigned size = I->getNumIndices() + idx_range.size();
6568 // Allocate some space to put the new indices in
6570 Idxs.reserve(size);
6571 // Add indices from the extract value instruction
6572 Idxs.append(I->idx_begin(), I->idx_end());
6573
6574 // Add requested indices
6575 Idxs.append(idx_range.begin(), idx_range.end());
6576
6577 assert(Idxs.size() == size
6578 && "Number of indices added not correct?");
6579
6580 return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
6581 }
6582 // Otherwise, we don't know (such as, extracting from a function return value
6583 // or load instruction)
6584 return nullptr;
6585}
6586
6587// If V refers to an initialized global constant, set Slice either to
6588// its initializer if the size of its elements equals ElementSize, or,
6589// for ElementSize == 8, to its representation as an array of unsiged
6590// char. Return true on success.
6591// Offset is in the unit "nr of ElementSize sized elements".
6594 unsigned ElementSize, uint64_t Offset) {
6595 assert(V && "V should not be null.");
6596 assert((ElementSize % 8) == 0 &&
6597 "ElementSize expected to be a multiple of the size of a byte.");
6598 unsigned ElementSizeInBytes = ElementSize / 8;
6599
6600 // Drill down into the pointer expression V, ignoring any intervening
6601 // casts, and determine the identity of the object it references along
6602 // with the cumulative byte offset into it.
6603 const GlobalVariable *GV =
6605 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
6606 // Fail if V is not based on constant global object.
6607 return false;
6608
6609 const DataLayout &DL = GV->getDataLayout();
6610 APInt Off(DL.getIndexTypeSizeInBits(V->getType()), 0);
6611
6612 if (GV != V->stripAndAccumulateConstantOffsets(DL, Off,
6613 /*AllowNonInbounds*/ true))
6614 // Fail if a constant offset could not be determined.
6615 return false;
6616
6617 uint64_t StartIdx = Off.getLimitedValue();
6618 if (StartIdx == UINT64_MAX)
6619 // Fail if the constant offset is excessive.
6620 return false;
6621
6622 // Off/StartIdx is in the unit of bytes. So we need to convert to number of
6623 // elements. Simply bail out if that isn't possible.
6624 if ((StartIdx % ElementSizeInBytes) != 0)
6625 return false;
6626
6627 Offset += StartIdx / ElementSizeInBytes;
6628 ConstantDataArray *Array = nullptr;
6629 ArrayType *ArrayTy = nullptr;
6630
6631 if (GV->getInitializer()->isNullValue()) {
6632 Type *GVTy = GV->getValueType();
6633 uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy).getFixedValue();
6634 uint64_t Length = SizeInBytes / ElementSizeInBytes;
6635
6636 Slice.Array = nullptr;
6637 Slice.Offset = 0;
6638 // Return an empty Slice for undersized constants to let callers
6639 // transform even undefined library calls into simpler, well-defined
6640 // expressions. This is preferable to making the calls although it
6641 // prevents sanitizers from detecting such calls.
6642 Slice.Length = Length < Offset ? 0 : Length - Offset;
6643 return true;
6644 }
6645
6646 auto *Init = const_cast<Constant *>(GV->getInitializer());
6647 if (auto *ArrayInit = dyn_cast<ConstantDataArray>(Init)) {
6648 Type *InitElTy = ArrayInit->getElementType();
6649 if (InitElTy->isIntegerTy(ElementSize)) {
6650 // If Init is an initializer for an array of the expected type
6651 // and size, use it as is.
6652 Array = ArrayInit;
6653 ArrayTy = ArrayInit->getType();
6654 }
6655 }
6656
6657 if (!Array) {
6658 if (ElementSize != 8)
6659 // TODO: Handle conversions to larger integral types.
6660 return false;
6661
6662 // Otherwise extract the portion of the initializer starting
6663 // at Offset as an array of bytes, and reset Offset.
6665 if (!Init)
6666 return false;
6667
6668 Offset = 0;
6670 ArrayTy = dyn_cast<ArrayType>(Init->getType());
6671 }
6672
6673 uint64_t NumElts = ArrayTy->getArrayNumElements();
6674 if (Offset > NumElts)
6675 return false;
6676
6677 Slice.Array = Array;
6678 Slice.Offset = Offset;
6679 Slice.Length = NumElts - Offset;
6680 return true;
6681}
6682
6683/// Extract bytes from the initializer of the constant array V, which need
6684/// not be a nul-terminated string. On success, store the bytes in Str and
6685/// return true. When TrimAtNul is set, Str will contain only the bytes up
6686/// to but not including the first nul. Return false on failure.
6688 bool TrimAtNul) {
6690 if (!getConstantDataArrayInfo(V, Slice, 8))
6691 return false;
6692
6693 if (Slice.Array == nullptr) {
6694 if (TrimAtNul) {
6695 // Return a nul-terminated string even for an empty Slice. This is
6696 // safe because all existing SimplifyLibcalls callers require string
6697 // arguments and the behavior of the functions they fold is undefined
6698 // otherwise. Folding the calls this way is preferable to making
6699 // the undefined library calls, even though it prevents sanitizers
6700 // from reporting such calls.
6701 Str = StringRef();
6702 return true;
6703 }
6704 if (Slice.Length == 1) {
6705 Str = StringRef("", 1);
6706 return true;
6707 }
6708 // We cannot instantiate a StringRef as we do not have an appropriate string
6709 // of 0s at hand.
6710 return false;
6711 }
6712
6713 // Start out with the entire array in the StringRef.
6714 Str = Slice.Array->getAsString();
6715 // Skip over 'offset' bytes.
6716 Str = Str.substr(Slice.Offset);
6717
6718 if (TrimAtNul) {
6719 // Trim off the \0 and anything after it. If the array is not nul
6720 // terminated, we just return the whole end of string. The client may know
6721 // some other way that the string is length-bound.
6722 Str = Str.substr(0, Str.find('\0'));
6723 }
6724 return true;
6725}
6726
6727// These next two are very similar to the above, but also look through PHI
6728// nodes.
6729// TODO: See if we can integrate these two together.
6730
6731/// If we can compute the length of the string pointed to by
6732/// the specified pointer, return 'len+1'. If we can't, return 0.
6735 unsigned CharSize) {
6736 // Look through noop bitcast instructions.
6737 V = V->stripPointerCasts();
6738
6739 // If this is a PHI node, there are two cases: either we have already seen it
6740 // or we haven't.
6741 if (const PHINode *PN = dyn_cast<PHINode>(V)) {
6742 if (!PHIs.insert(PN).second)
6743 return ~0ULL; // already in the set.
6744
6745 // If it was new, see if all the input strings are the same length.
6746 uint64_t LenSoFar = ~0ULL;
6747 for (Value *IncValue : PN->incoming_values()) {
6748 uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize);
6749 if (Len == 0) return 0; // Unknown length -> unknown.
6750
6751 if (Len == ~0ULL) continue;
6752
6753 if (Len != LenSoFar && LenSoFar != ~0ULL)
6754 return 0; // Disagree -> unknown.
6755 LenSoFar = Len;
6756 }
6757
6758 // Success, all agree.
6759 return LenSoFar;
6760 }
6761
6762 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
6763 if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
6764 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);
6765 if (Len1 == 0) return 0;
6766 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);
6767 if (Len2 == 0) return 0;
6768 if (Len1 == ~0ULL) return Len2;
6769 if (Len2 == ~0ULL) return Len1;
6770 if (Len1 != Len2) return 0;
6771 return Len1;
6772 }
6773
6774 // Otherwise, see if we can read the string.
6776 if (!getConstantDataArrayInfo(V, Slice, CharSize))
6777 return 0;
6778
6779 if (Slice.Array == nullptr)
6780 // Zeroinitializer (including an empty one).
6781 return 1;
6782
6783 // Search for the first nul character. Return a conservative result even
6784 // when there is no nul. This is safe since otherwise the string function
6785 // being folded such as strlen is undefined, and can be preferable to
6786 // making the undefined library call.
6787 unsigned NullIndex = 0;
6788 for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) {
6789 if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0)
6790 break;
6791 }
6792
6793 return NullIndex + 1;
6794}
6795
6796/// If we can compute the length of the string pointed to by
6797/// the specified pointer, return 'len+1'. If we can't, return 0.
6798uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) {
6799 if (!V->getType()->isPointerTy())
6800 return 0;
6801
6803 uint64_t Len = GetStringLengthH(V, PHIs, CharSize);
6804 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
6805 // an empty string as a length.
6806 return Len == ~0ULL ? 1 : Len;
6807}
6808
6809const Value *
6811 bool MustPreserveNullness) {
6812 assert(Call &&
6813 "getArgumentAliasingToReturnedPointer only works on nonnull calls");
6814 if (const Value *RV = Call->getReturnedArgOperand())
6815 return RV;
6816 // This can be used only as a aliasing property.
6818 Call, MustPreserveNullness))
6819 return Call->getArgOperand(0);
6820 return nullptr;
6821}
6822
6824 const CallBase *Call, bool MustPreserveNullness) {
6825 switch (Call->getIntrinsicID()) {
6826 case Intrinsic::launder_invariant_group:
6827 case Intrinsic::strip_invariant_group:
6828 case Intrinsic::aarch64_irg:
6829 case Intrinsic::aarch64_tagp:
6830 // The amdgcn_make_buffer_rsrc function does not alter the address of the
6831 // input pointer (and thus preserve null-ness for the purposes of escape
6832 // analysis, which is where the MustPreserveNullness flag comes in to play).
6833 // However, it will not necessarily map ptr addrspace(N) null to ptr
6834 // addrspace(8) null, aka the "null descriptor", which has "all loads return
6835 // 0, all stores are dropped" semantics. Given the context of this intrinsic
6836 // list, no one should be relying on such a strict interpretation of
6837 // MustPreserveNullness (and, at time of writing, they are not), but we
6838 // document this fact out of an abundance of caution.
6839 case Intrinsic::amdgcn_make_buffer_rsrc:
6840 return true;
6841 case Intrinsic::ptrmask:
6842 return !MustPreserveNullness;
6843 case Intrinsic::threadlocal_address:
6844 // The underlying variable changes with thread ID. The Thread ID may change
6845 // at coroutine suspend points.
6846 return !Call->getParent()->getParent()->isPresplitCoroutine();
6847 default:
6848 return false;
6849 }
6850}
6851
6852/// \p PN defines a loop-variant pointer to an object. Check if the
6853/// previous iteration of the loop was referring to the same object as \p PN.
6855 const LoopInfo *LI) {
6856 // Find the loop-defined value.
6857 Loop *L = LI->getLoopFor(PN->getParent());
6858 if (PN->getNumIncomingValues() != 2)
6859 return true;
6860
6861 // Find the value from previous iteration.
6862 auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
6863 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6864 PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
6865 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6866 return true;
6867
6868 // If a new pointer is loaded in the loop, the pointer references a different
6869 // object in every iteration. E.g.:
6870 // for (i)
6871 // int *p = a[i];
6872 // ...
6873 if (auto *Load = dyn_cast<LoadInst>(PrevValue))
6874 if (!L->isLoopInvariant(Load->getPointerOperand()))
6875 return false;
6876 return true;
6877}
6878
6879const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup) {
6880 for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
6881 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
6882 const Value *PtrOp = GEP->getPointerOperand();
6883 if (!PtrOp->getType()->isPointerTy()) // Only handle scalar pointer base.
6884 return V;
6885 V = PtrOp;
6886 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
6887 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
6888 Value *NewV = cast<Operator>(V)->getOperand(0);
6889 if (!NewV->getType()->isPointerTy())
6890 return V;
6891 V = NewV;
6892 } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
6893 if (GA->isInterposable())
6894 return V;
6895 V = GA->getAliasee();
6896 } else {
6897 if (auto *PHI = dyn_cast<PHINode>(V)) {
6898 // Look through single-arg phi nodes created by LCSSA.
6899 if (PHI->getNumIncomingValues() == 1) {
6900 V = PHI->getIncomingValue(0);
6901 continue;
6902 }
6903 } else if (auto *Call = dyn_cast<CallBase>(V)) {
6904 // CaptureTracking can know about special capturing properties of some
6905 // intrinsics like launder.invariant.group, that can't be expressed with
6906 // the attributes, but have properties like returning aliasing pointer.
6907 // Because some analysis may assume that nocaptured pointer is not
6908 // returned from some special intrinsic (because function would have to
6909 // be marked with returns attribute), it is crucial to use this function
6910 // because it should be in sync with CaptureTracking. Not using it may
6911 // cause weird miscompilations where 2 aliasing pointers are assumed to
6912 // noalias.
6913 if (auto *RP = getArgumentAliasingToReturnedPointer(Call, false)) {
6914 V = RP;
6915 continue;
6916 }
6917 }
6918
6919 return V;
6920 }
6921 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
6922 }
6923 return V;
6924}
6925
6928 const LoopInfo *LI, unsigned MaxLookup) {
6931 Worklist.push_back(V);
6932 do {
6933 const Value *P = Worklist.pop_back_val();
6934 P = getUnderlyingObject(P, MaxLookup);
6935
6936 if (!Visited.insert(P).second)
6937 continue;
6938
6939 if (auto *SI = dyn_cast<SelectInst>(P)) {
6940 Worklist.push_back(SI->getTrueValue());
6941 Worklist.push_back(SI->getFalseValue());
6942 continue;
6943 }
6944
6945 if (auto *PN = dyn_cast<PHINode>(P)) {
6946 // If this PHI changes the underlying object in every iteration of the
6947 // loop, don't look through it. Consider:
6948 // int **A;
6949 // for (i) {
6950 // Prev = Curr; // Prev = PHI (Prev_0, Curr)
6951 // Curr = A[i];
6952 // *Prev, *Curr;
6953 //
6954 // Prev is tracking Curr one iteration behind so they refer to different
6955 // underlying objects.
6956 if (!LI || !LI->isLoopHeader(PN->getParent()) ||
6958 append_range(Worklist, PN->incoming_values());
6959 else
6960 Objects.push_back(P);
6961 continue;
6962 }
6963
6964 Objects.push_back(P);
6965 } while (!Worklist.empty());
6966}
6967
6969 const unsigned MaxVisited = 8;
6970
6973 Worklist.push_back(V);
6974 const Value *Object = nullptr;
6975 // Used as fallback if we can't find a common underlying object through
6976 // recursion.
6977 bool First = true;
6978 const Value *FirstObject = getUnderlyingObject(V);
6979 do {
6980 const Value *P = Worklist.pop_back_val();
6981 P = First ? FirstObject : getUnderlyingObject(P);
6982 First = false;
6983
6984 if (!Visited.insert(P).second)
6985 continue;
6986
6987 if (Visited.size() == MaxVisited)
6988 return FirstObject;
6989
6990 if (auto *SI = dyn_cast<SelectInst>(P)) {
6991 Worklist.push_back(SI->getTrueValue());
6992 Worklist.push_back(SI->getFalseValue());
6993 continue;
6994 }
6995
6996 if (auto *PN = dyn_cast<PHINode>(P)) {
6997 append_range(Worklist, PN->incoming_values());
6998 continue;
6999 }
7000
7001 if (!Object)
7002 Object = P;
7003 else if (Object != P)
7004 return FirstObject;
7005 } while (!Worklist.empty());
7006
7007 return Object ? Object : FirstObject;
7008}
7009
7010/// This is the function that does the work of looking through basic
7011/// ptrtoint+arithmetic+inttoptr sequences.
7012static const Value *getUnderlyingObjectFromInt(const Value *V) {
7013 do {
7014 if (const Operator *U = dyn_cast<Operator>(V)) {
7015 // If we find a ptrtoint, we can transfer control back to the
7016 // regular getUnderlyingObjectFromInt.
7017 if (U->getOpcode() == Instruction::PtrToInt)
7018 return U->getOperand(0);
7019 // If we find an add of a constant, a multiplied value, or a phi, it's
7020 // likely that the other operand will lead us to the base
7021 // object. We don't have to worry about the case where the
7022 // object address is somehow being computed by the multiply,
7023 // because our callers only care when the result is an
7024 // identifiable object.
7025 if (U->getOpcode() != Instruction::Add ||
7026 (!isa<ConstantInt>(U->getOperand(1)) &&
7027 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
7028 !isa<PHINode>(U->getOperand(1))))
7029 return V;
7030 V = U->getOperand(0);
7031 } else {
7032 return V;
7033 }
7034 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
7035 } while (true);
7036}
7037
7038/// This is a wrapper around getUnderlyingObjects and adds support for basic
7039/// ptrtoint+arithmetic+inttoptr sequences.
7040/// It returns false if unidentified object is found in getUnderlyingObjects.
7042 SmallVectorImpl<Value *> &Objects) {
7044 SmallVector<const Value *, 4> Working(1, V);
7045 do {
7046 V = Working.pop_back_val();
7047
7049 getUnderlyingObjects(V, Objs);
7050
7051 for (const Value *V : Objs) {
7052 if (!Visited.insert(V).second)
7053 continue;
7054 if (Operator::getOpcode(V) == Instruction::IntToPtr) {
7055 const Value *O =
7056 getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
7057 if (O->getType()->isPointerTy()) {
7058 Working.push_back(O);
7059 continue;
7060 }
7061 }
7062 // If getUnderlyingObjects fails to find an identifiable object,
7063 // getUnderlyingObjectsForCodeGen also fails for safety.
7064 if (!isIdentifiedObject(V)) {
7065 Objects.clear();
7066 return false;
7067 }
7068 Objects.push_back(const_cast<Value *>(V));
7069 }
7070 } while (!Working.empty());
7071 return true;
7072}
7073
7075 AllocaInst *Result = nullptr;
7077 SmallVector<Value *, 4> Worklist;
7078
7079 auto AddWork = [&](Value *V) {
7080 if (Visited.insert(V).second)
7081 Worklist.push_back(V);
7082 };
7083
7084 AddWork(V);
7085 do {
7086 V = Worklist.pop_back_val();
7087 assert(Visited.count(V));
7088
7089 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
7090 if (Result && Result != AI)
7091 return nullptr;
7092 Result = AI;
7093 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
7094 AddWork(CI->getOperand(0));
7095 } else if (PHINode *PN = dyn_cast<PHINode>(V)) {
7096 for (Value *IncValue : PN->incoming_values())
7097 AddWork(IncValue);
7098 } else if (auto *SI = dyn_cast<SelectInst>(V)) {
7099 AddWork(SI->getTrueValue());
7100 AddWork(SI->getFalseValue());
7102 if (OffsetZero && !GEP->hasAllZeroIndices())
7103 return nullptr;
7104 AddWork(GEP->getPointerOperand());
7105 } else if (CallBase *CB = dyn_cast<CallBase>(V)) {
7106 Value *Returned = CB->getReturnedArgOperand();
7107 if (Returned)
7108 AddWork(Returned);
7109 else
7110 return nullptr;
7111 } else {
7112 return nullptr;
7113 }
7114 } while (!Worklist.empty());
7115
7116 return Result;
7117}
7118
7120 const Value *V, bool AllowLifetime, bool AllowDroppable) {
7121 for (const User *U : V->users()) {
7123 if (!II)
7124 return false;
7125
7126 if (AllowLifetime && II->isLifetimeStartOrEnd())
7127 continue;
7128
7129 if (AllowDroppable && II->isDroppable())
7130 continue;
7131
7132 return false;
7133 }
7134 return true;
7135}
7136
7139 V, /* AllowLifetime */ true, /* AllowDroppable */ false);
7140}
7143 V, /* AllowLifetime */ true, /* AllowDroppable */ true);
7144}
7145
7147 if (auto *II = dyn_cast<IntrinsicInst>(I))
7148 return isTriviallyVectorizable(II->getIntrinsicID());
7149 auto *Shuffle = dyn_cast<ShuffleVectorInst>(I);
7150 return (!Shuffle || Shuffle->isSelect()) &&
7152}
7153
7155 const Instruction *Inst, const Instruction *CtxI, AssumptionCache *AC,
7156 const DominatorTree *DT, const TargetLibraryInfo *TLI, bool UseVariableInfo,
7157 bool IgnoreUBImplyingAttrs) {
7158 return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI,
7159 AC, DT, TLI, UseVariableInfo,
7160 IgnoreUBImplyingAttrs);
7161}
7162
7164 unsigned Opcode, const Instruction *Inst, const Instruction *CtxI,
7165 AssumptionCache *AC, const DominatorTree *DT, const TargetLibraryInfo *TLI,
7166 bool UseVariableInfo, bool IgnoreUBImplyingAttrs) {
7167#ifndef NDEBUG
7168 if (Inst->getOpcode() != Opcode) {
7169 // Check that the operands are actually compatible with the Opcode override.
7170 auto hasEqualReturnAndLeadingOperandTypes =
7171 [](const Instruction *Inst, unsigned NumLeadingOperands) {
7172 if (Inst->getNumOperands() < NumLeadingOperands)
7173 return false;
7174 const Type *ExpectedType = Inst->getType();
7175 for (unsigned ItOp = 0; ItOp < NumLeadingOperands; ++ItOp)
7176 if (Inst->getOperand(ItOp)->getType() != ExpectedType)
7177 return false;
7178 return true;
7179 };
7181 hasEqualReturnAndLeadingOperandTypes(Inst, 2));
7182 assert(!Instruction::isUnaryOp(Opcode) ||
7183 hasEqualReturnAndLeadingOperandTypes(Inst, 1));
7184 }
7185#endif
7186
7187 switch (Opcode) {
7188 default:
7189 return true;
7190 case Instruction::UDiv:
7191 case Instruction::URem: {
7192 // x / y is undefined if y == 0.
7193 const APInt *V;
7194 if (match(Inst->getOperand(1), m_APInt(V)))
7195 return *V != 0;
7196 return false;
7197 }
7198 case Instruction::SDiv:
7199 case Instruction::SRem: {
7200 // x / y is undefined if y == 0 or x == INT_MIN and y == -1
7201 const APInt *Numerator, *Denominator;
7202 if (!match(Inst->getOperand(1), m_APInt(Denominator)))
7203 return false;
7204 // We cannot hoist this division if the denominator is 0.
7205 if (*Denominator == 0)
7206 return false;
7207 // It's safe to hoist if the denominator is not 0 or -1.
7208 if (!Denominator->isAllOnes())
7209 return true;
7210 // At this point we know that the denominator is -1. It is safe to hoist as
7211 // long we know that the numerator is not INT_MIN.
7212 if (match(Inst->getOperand(0), m_APInt(Numerator)))
7213 return !Numerator->isMinSignedValue();
7214 // The numerator *might* be MinSignedValue.
7215 return false;
7216 }
7217 case Instruction::Load: {
7218 if (!UseVariableInfo)
7219 return false;
7220
7221 const LoadInst *LI = dyn_cast<LoadInst>(Inst);
7222 if (!LI)
7223 return false;
7224 if (mustSuppressSpeculation(*LI))
7225 return false;
7226 const DataLayout &DL = LI->getDataLayout();
7228 LI->getType(), LI->getAlign(), DL,
7229 CtxI, AC, DT, TLI);
7230 }
7231 case Instruction::Call: {
7232 auto *CI = dyn_cast<const CallInst>(Inst);
7233 if (!CI)
7234 return false;
7235 const Function *Callee = CI->getCalledFunction();
7236
7237 // The called function could have undefined behavior or side-effects, even
7238 // if marked readnone nounwind.
7239 if (!Callee || !Callee->isSpeculatable())
7240 return false;
7241 // Since the operands may be changed after hoisting, undefined behavior may
7242 // be triggered by some UB-implying attributes.
7243 return IgnoreUBImplyingAttrs || !CI->hasUBImplyingAttrs();
7244 }
7245 case Instruction::VAArg:
7246 case Instruction::Alloca:
7247 case Instruction::Invoke:
7248 case Instruction::CallBr:
7249 case Instruction::PHI:
7250 case Instruction::Store:
7251 case Instruction::Ret:
7252 case Instruction::UncondBr:
7253 case Instruction::CondBr:
7254 case Instruction::IndirectBr:
7255 case Instruction::Switch:
7256 case Instruction::Unreachable:
7257 case Instruction::Fence:
7258 case Instruction::AtomicRMW:
7259 case Instruction::AtomicCmpXchg:
7260 case Instruction::LandingPad:
7261 case Instruction::Resume:
7262 case Instruction::CatchSwitch:
7263 case Instruction::CatchPad:
7264 case Instruction::CatchRet:
7265 case Instruction::CleanupPad:
7266 case Instruction::CleanupRet:
7267 return false; // Misc instructions which have effects
7268 }
7269}
7270
7272 if (I.mayReadOrWriteMemory())
7273 // Memory dependency possible
7274 return true;
7276 // Can't move above a maythrow call or infinite loop. Or if an
7277 // inalloca alloca, above a stacksave call.
7278 return true;
7280 // 1) Can't reorder two inf-loop calls, even if readonly
7281 // 2) Also can't reorder an inf-loop call below a instruction which isn't
7282 // safe to speculative execute. (Inverse of above)
7283 return true;
7284 return false;
7285}
7286
7287/// Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
7301
7302/// Combine constant ranges from computeConstantRange() and computeKnownBits().
7305 bool ForSigned,
7306 const SimplifyQuery &SQ) {
7307 ConstantRange CR1 =
7308 ConstantRange::fromKnownBits(V.getKnownBits(SQ), ForSigned);
7309 ConstantRange CR2 = computeConstantRange(V, ForSigned, SQ.IIQ.UseInstrInfo);
7312 return CR1.intersectWith(CR2, RangeType);
7313}
7314
7316 const Value *RHS,
7317 const SimplifyQuery &SQ,
7318 bool IsNSW) {
7319 ConstantRange LHSRange =
7320 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7321 ConstantRange RHSRange =
7322 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7323
7324 // mul nsw of two non-negative numbers is also nuw.
7325 if (IsNSW && LHSRange.isAllNonNegative() && RHSRange.isAllNonNegative())
7327
7328 return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange));
7329}
7330
7332 const Value *RHS,
7333 const SimplifyQuery &SQ) {
7334 // Multiplying n * m significant bits yields a result of n + m significant
7335 // bits. If the total number of significant bits does not exceed the
7336 // result bit width (minus 1), there is no overflow.
7337 // This means if we have enough leading sign bits in the operands
7338 // we can guarantee that the result does not overflow.
7339 // Ref: "Hacker's Delight" by Henry Warren
7340 unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
7341
7342 // Note that underestimating the number of sign bits gives a more
7343 // conservative answer.
7344 unsigned SignBits =
7345 ::ComputeNumSignBits(LHS, SQ) + ::ComputeNumSignBits(RHS, SQ);
7346
7347 // First handle the easy case: if we have enough sign bits there's
7348 // definitely no overflow.
7349 if (SignBits > BitWidth + 1)
7351
7352 // There are two ambiguous cases where there can be no overflow:
7353 // SignBits == BitWidth + 1 and
7354 // SignBits == BitWidth
7355 // The second case is difficult to check, therefore we only handle the
7356 // first case.
7357 if (SignBits == BitWidth + 1) {
7358 // It overflows only when both arguments are negative and the true
7359 // product is exactly the minimum negative number.
7360 // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
7361 // For simplicity we just check if at least one side is not negative.
7362 KnownBits LHSKnown = computeKnownBits(LHS, SQ);
7363 KnownBits RHSKnown = computeKnownBits(RHS, SQ);
7364 if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative())
7366 }
7368}
7369
7372 const WithCache<const Value *> &RHS,
7373 const SimplifyQuery &SQ) {
7374 ConstantRange LHSRange =
7375 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7376 ConstantRange RHSRange =
7377 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7378 return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange));
7379}
7380
7381static OverflowResult
7384 const AddOperator *Add, const SimplifyQuery &SQ) {
7385 if (Add && Add->hasNoSignedWrap()) {
7387 }
7388
7389 // If LHS and RHS each have at least two sign bits, the addition will look
7390 // like
7391 //
7392 // XX..... +
7393 // YY.....
7394 //
7395 // If the carry into the most significant position is 0, X and Y can't both
7396 // be 1 and therefore the carry out of the addition is also 0.
7397 //
7398 // If the carry into the most significant position is 1, X and Y can't both
7399 // be 0 and therefore the carry out of the addition is also 1.
7400 //
7401 // Since the carry into the most significant position is always equal to
7402 // the carry out of the addition, there is no signed overflow.
7403 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
7405
7406 ConstantRange LHSRange =
7407 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7408 ConstantRange RHSRange =
7409 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7410 OverflowResult OR =
7411 mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange));
7413 return OR;
7414
7415 // The remaining code needs Add to be available. Early returns if not so.
7416 if (!Add)
7418
7419 // If the sign of Add is the same as at least one of the operands, this add
7420 // CANNOT overflow. If this can be determined from the known bits of the
7421 // operands the above signedAddMayOverflow() check will have already done so.
7422 // The only other way to improve on the known bits is from an assumption, so
7423 // call computeKnownBitsFromContext() directly.
7424 bool LHSOrRHSKnownNonNegative =
7425 (LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative());
7426 bool LHSOrRHSKnownNegative =
7427 (LHSRange.isAllNegative() || RHSRange.isAllNegative());
7428 if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
7429 KnownBits AddKnown(LHSRange.getBitWidth());
7430 computeKnownBitsFromContext(Add, AddKnown, SQ);
7431 if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||
7432 (AddKnown.isNegative() && LHSOrRHSKnownNegative))
7434 }
7435
7437}
7438
7440 const Value *RHS,
7441 const SimplifyQuery &SQ) {
7442 // X - (X % ?)
7443 // The remainder of a value can't have greater magnitude than itself,
7444 // so the subtraction can't overflow.
7445
7446 // X - (X -nuw ?)
7447 // In the minimal case, this would simplify to "?", so there's no subtract
7448 // at all. But if this analysis is used to peek through casts, for example,
7449 // then determining no-overflow may allow other transforms.
7450
7451 // TODO: There are other patterns like this.
7452 // See simplifyICmpWithBinOpOnLHS() for candidates.
7453 if (match(RHS, m_URem(m_Specific(LHS), m_Value())) ||
7454 match(RHS, m_NUWSub(m_Specific(LHS), m_Value())))
7455 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7457
7458 if (auto C = isImpliedByDomCondition(CmpInst::ICMP_UGE, LHS, RHS, SQ.CxtI,
7459 SQ.DL)) {
7460 if (*C)
7463 }
7464
7465 ConstantRange LHSRange =
7466 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7467 ConstantRange RHSRange =
7468 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7469 return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange));
7470}
7471
7473 const Value *RHS,
7474 const SimplifyQuery &SQ) {
7475 // X - (X % ?)
7476 // The remainder of a value can't have greater magnitude than itself,
7477 // so the subtraction can't overflow.
7478
7479 // X - (X -nsw ?)
7480 // In the minimal case, this would simplify to "?", so there's no subtract
7481 // at all. But if this analysis is used to peek through casts, for example,
7482 // then determining no-overflow may allow other transforms.
7483 if (match(RHS, m_SRem(m_Specific(LHS), m_Value())) ||
7484 match(RHS, m_NSWSub(m_Specific(LHS), m_Value())))
7485 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7487
7488 // If LHS and RHS each have at least two sign bits, the subtraction
7489 // cannot overflow.
7490 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
7492
7493 ConstantRange LHSRange =
7494 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7495 ConstantRange RHSRange =
7496 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7497 return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange));
7498}
7499
7501 const DominatorTree &DT) {
7502 SmallVector<const CondBrInst *, 2> GuardingBranches;
7504
7505 for (const User *U : WO->users()) {
7506 if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) {
7507 assert(EVI->getNumIndices() == 1 && "Obvious from CI's type");
7508
7509 if (EVI->getIndices()[0] == 0)
7510 Results.push_back(EVI);
7511 else {
7512 assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type");
7513
7514 for (const auto *U : EVI->users())
7515 if (const auto *B = dyn_cast<CondBrInst>(U))
7516 GuardingBranches.push_back(B);
7517 }
7518 } else {
7519 // We are using the aggregate directly in a way we don't want to analyze
7520 // here (storing it to a global, say).
7521 return false;
7522 }
7523 }
7524
7525 auto AllUsesGuardedByBranch = [&](const CondBrInst *BI) {
7526 BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1));
7527
7528 // Check if all users of the add are provably no-wrap.
7529 for (const auto *Result : Results) {
7530 // If the extractvalue itself is not executed on overflow, the we don't
7531 // need to check each use separately, since domination is transitive.
7532 if (DT.dominates(NoWrapEdge, Result->getParent()))
7533 continue;
7534
7535 for (const auto &RU : Result->uses())
7536 if (!DT.dominates(NoWrapEdge, RU))
7537 return false;
7538 }
7539
7540 return true;
7541 };
7542
7543 return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
7544}
7545
7546/// Shifts return poison if shiftwidth is larger than the bitwidth.
7547static bool shiftAmountKnownInRange(const Value *ShiftAmount) {
7548 auto *C = dyn_cast<Constant>(ShiftAmount);
7549 if (!C)
7550 return false;
7551
7552 // Shifts return poison if shiftwidth is larger than the bitwidth.
7554 if (auto *FVTy = dyn_cast<FixedVectorType>(C->getType())) {
7555 unsigned NumElts = FVTy->getNumElements();
7556 for (unsigned i = 0; i < NumElts; ++i)
7557 ShiftAmounts.push_back(C->getAggregateElement(i));
7558 } else if (isa<ScalableVectorType>(C->getType()))
7559 return false; // Can't tell, just return false to be safe
7560 else
7561 ShiftAmounts.push_back(C);
7562
7563 bool Safe = llvm::all_of(ShiftAmounts, [](const Constant *C) {
7564 auto *CI = dyn_cast_or_null<ConstantInt>(C);
7565 return CI && CI->getValue().ult(C->getType()->getIntegerBitWidth());
7566 });
7567
7568 return Safe;
7569}
7570
7576
7578 return (unsigned(Kind) & unsigned(UndefPoisonKind::PoisonOnly)) != 0;
7579}
7580
7582 return (unsigned(Kind) & unsigned(UndefPoisonKind::UndefOnly)) != 0;
7583}
7584
7586 bool ConsiderFlagsAndMetadata) {
7587
7588 if (ConsiderFlagsAndMetadata && includesPoison(Kind) &&
7589 Op->hasPoisonGeneratingAnnotations())
7590 return true;
7591
7592 unsigned Opcode = Op->getOpcode();
7593
7594 // Check whether opcode is a poison/undef-generating operation
7595 switch (Opcode) {
7596 case Instruction::Shl:
7597 case Instruction::AShr:
7598 case Instruction::LShr:
7599 return includesPoison(Kind) && !shiftAmountKnownInRange(Op->getOperand(1));
7600 case Instruction::FPToSI:
7601 case Instruction::FPToUI:
7602 // fptosi/ui yields poison if the resulting value does not fit in the
7603 // destination type.
7604 return true;
7605 case Instruction::Call:
7606 if (auto *II = dyn_cast<IntrinsicInst>(Op)) {
7607 switch (II->getIntrinsicID()) {
7608 // NOTE: Use IntrNoCreateUndefOrPoison when possible.
7609 case Intrinsic::ctlz:
7610 case Intrinsic::cttz:
7611 case Intrinsic::abs:
7612 // We're not considering flags so it is safe to just return false.
7613 return false;
7614 case Intrinsic::sshl_sat:
7615 case Intrinsic::ushl_sat:
7616 if (!includesPoison(Kind) ||
7617 shiftAmountKnownInRange(II->getArgOperand(1)))
7618 return false;
7619 break;
7620 }
7621 }
7622 [[fallthrough]];
7623 case Instruction::CallBr:
7624 case Instruction::Invoke: {
7625 const auto *CB = cast<CallBase>(Op);
7626 return !CB->hasRetAttr(Attribute::NoUndef) &&
7627 !CB->hasFnAttr(Attribute::NoCreateUndefOrPoison);
7628 }
7629 case Instruction::InsertElement:
7630 case Instruction::ExtractElement: {
7631 // If index exceeds the length of the vector, it returns poison
7632 auto *VTy = cast<VectorType>(Op->getOperand(0)->getType());
7633 unsigned IdxOp = Op->getOpcode() == Instruction::InsertElement ? 2 : 1;
7634 auto *Idx = dyn_cast<ConstantInt>(Op->getOperand(IdxOp));
7635 if (includesPoison(Kind))
7636 return !Idx ||
7637 Idx->getValue().uge(VTy->getElementCount().getKnownMinValue());
7638 return false;
7639 }
7640 case Instruction::ShuffleVector: {
7642 ? cast<ConstantExpr>(Op)->getShuffleMask()
7643 : cast<ShuffleVectorInst>(Op)->getShuffleMask();
7644 return includesPoison(Kind) && is_contained(Mask, PoisonMaskElem);
7645 }
7646 case Instruction::FNeg:
7647 case Instruction::PHI:
7648 case Instruction::Select:
7649 case Instruction::ExtractValue:
7650 case Instruction::InsertValue:
7651 case Instruction::Freeze:
7652 case Instruction::ICmp:
7653 case Instruction::FCmp:
7654 case Instruction::GetElementPtr:
7655 return false;
7656 case Instruction::AddrSpaceCast:
7657 return true;
7658 default: {
7659 const auto *CE = dyn_cast<ConstantExpr>(Op);
7660 if (isa<CastInst>(Op) || (CE && CE->isCast()))
7661 return false;
7662 else if (Instruction::isBinaryOp(Opcode))
7663 return false;
7664 // Be conservative and return true.
7665 return true;
7666 }
7667 }
7668}
7669
7671 bool ConsiderFlagsAndMetadata) {
7672 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::UndefOrPoison,
7673 ConsiderFlagsAndMetadata);
7674}
7675
7676bool llvm::canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata) {
7677 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::PoisonOnly,
7678 ConsiderFlagsAndMetadata);
7679}
7680
7681static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V,
7682 unsigned Depth) {
7683 if (ValAssumedPoison == V)
7684 return true;
7685
7686 const unsigned MaxDepth = 2;
7687 if (Depth >= MaxDepth)
7688 return false;
7689
7690 if (const auto *I = dyn_cast<Instruction>(V)) {
7691 if (any_of(I->operands(), [=](const Use &Op) {
7692 return propagatesPoison(Op) &&
7693 directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1);
7694 }))
7695 return true;
7696
7697 // V = extractvalue V0, idx
7698 // V2 = extractvalue V0, idx2
7699 // V0's elements are all poison or not. (e.g., add_with_overflow)
7700 const WithOverflowInst *II;
7702 (match(ValAssumedPoison, m_ExtractValue(m_Specific(II))) ||
7703 llvm::is_contained(II->args(), ValAssumedPoison)))
7704 return true;
7705 }
7706 return false;
7707}
7708
7709static bool impliesPoison(const Value *ValAssumedPoison, const Value *V,
7710 unsigned Depth) {
7711 if (isGuaranteedNotToBePoison(ValAssumedPoison))
7712 return true;
7713
7714 if (directlyImpliesPoison(ValAssumedPoison, V, /* Depth */ 0))
7715 return true;
7716
7717 const unsigned MaxDepth = 2;
7718 if (Depth >= MaxDepth)
7719 return false;
7720
7721 const auto *I = dyn_cast<Instruction>(ValAssumedPoison);
7722 if (I && !canCreatePoison(cast<Operator>(I))) {
7723 return all_of(I->operands(), [=](const Value *Op) {
7724 return impliesPoison(Op, V, Depth + 1);
7725 });
7726 }
7727 return false;
7728}
7729
7730bool llvm::impliesPoison(const Value *ValAssumedPoison, const Value *V) {
7731 return ::impliesPoison(ValAssumedPoison, V, /* Depth */ 0);
7732}
7733
7734static bool programUndefinedIfUndefOrPoison(const Value *V, bool PoisonOnly);
7735
7737 const Value *V, AssumptionCache *AC, const Instruction *CtxI,
7738 const DominatorTree *DT, unsigned Depth, UndefPoisonKind Kind) {
7740 return false;
7741
7742 if (isa<MetadataAsValue>(V))
7743 return false;
7744
7745 if (const auto *A = dyn_cast<Argument>(V)) {
7746 if (A->hasAttribute(Attribute::NoUndef) ||
7747 A->hasAttribute(Attribute::Dereferenceable) ||
7748 A->hasAttribute(Attribute::DereferenceableOrNull))
7749 return true;
7750 }
7751
7752 if (auto *C = dyn_cast<Constant>(V)) {
7753 if (isa<PoisonValue>(C))
7754 return !includesPoison(Kind);
7755
7756 if (isa<UndefValue>(C))
7757 return !includesUndef(Kind);
7758
7761 return true;
7762
7763 if (C->getType()->isVectorTy()) {
7764 if (isa<ConstantExpr>(C)) {
7765 // Scalable vectors can use a ConstantExpr to build a splat.
7766 if (Constant *SplatC = C->getSplatValue())
7767 if (isa<ConstantInt>(SplatC) || isa<ConstantFP>(SplatC))
7768 return true;
7769 } else {
7770 if (includesUndef(Kind) && C->containsUndefElement())
7771 return false;
7772 if (includesPoison(Kind) && C->containsPoisonElement())
7773 return false;
7774 return !C->containsConstantExpression();
7775 }
7776 }
7777 }
7778
7779 // Strip cast operations from a pointer value.
7780 // Note that stripPointerCastsSameRepresentation can strip off getelementptr
7781 // inbounds with zero offset. To guarantee that the result isn't poison, the
7782 // stripped pointer is checked as it has to be pointing into an allocated
7783 // object or be null `null` to ensure `inbounds` getelement pointers with a
7784 // zero offset could not produce poison.
7785 // It can strip off addrspacecast that do not change bit representation as
7786 // well. We believe that such addrspacecast is equivalent to no-op.
7787 auto *StrippedV = V->stripPointerCastsSameRepresentation();
7788 if (isa<AllocaInst>(StrippedV) || isa<GlobalVariable>(StrippedV) ||
7789 isa<Function>(StrippedV) || isa<ConstantPointerNull>(StrippedV))
7790 return true;
7791
7792 auto OpCheck = [&](const Value *V) {
7793 return isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth + 1, Kind);
7794 };
7795
7796 if (auto *Opr = dyn_cast<Operator>(V)) {
7797 // If the value is a freeze instruction, then it can never
7798 // be undef or poison.
7799 if (isa<FreezeInst>(V))
7800 return true;
7801
7802 if (const auto *CB = dyn_cast<CallBase>(V)) {
7803 if (CB->hasRetAttr(Attribute::NoUndef) ||
7804 CB->hasRetAttr(Attribute::Dereferenceable) ||
7805 CB->hasRetAttr(Attribute::DereferenceableOrNull))
7806 return true;
7807 }
7808
7809 if (!::canCreateUndefOrPoison(Opr, Kind,
7810 /*ConsiderFlagsAndMetadata=*/true)) {
7811 if (const auto *PN = dyn_cast<PHINode>(V)) {
7812 unsigned Num = PN->getNumIncomingValues();
7813 bool IsWellDefined = true;
7814 for (unsigned i = 0; i < Num; ++i) {
7815 if (PN == PN->getIncomingValue(i))
7816 continue;
7817 auto *TI = PN->getIncomingBlock(i)->getTerminator();
7818 if (!isGuaranteedNotToBeUndefOrPoison(PN->getIncomingValue(i), AC, TI,
7819 DT, Depth + 1, Kind)) {
7820 IsWellDefined = false;
7821 break;
7822 }
7823 }
7824 if (IsWellDefined)
7825 return true;
7826 } else if (auto *Splat = isa<ShuffleVectorInst>(Opr) ? getSplatValue(Opr)
7827 : nullptr) {
7828 // For splats we only need to check the value being splatted.
7829 if (OpCheck(Splat))
7830 return true;
7831 } else if (all_of(Opr->operands(), OpCheck))
7832 return true;
7833 }
7834 }
7835
7836 if (auto *I = dyn_cast<LoadInst>(V))
7837 if (I->hasMetadata(LLVMContext::MD_noundef) ||
7838 I->hasMetadata(LLVMContext::MD_dereferenceable) ||
7839 I->hasMetadata(LLVMContext::MD_dereferenceable_or_null))
7840 return true;
7841
7843 return true;
7844
7845 // CxtI may be null or a cloned instruction.
7846 if (!CtxI || !CtxI->getParent() || !DT)
7847 return false;
7848
7849 auto *DNode = DT->getNode(CtxI->getParent());
7850 if (!DNode)
7851 // Unreachable block
7852 return false;
7853
7854 // If V is used as a branch condition before reaching CtxI, V cannot be
7855 // undef or poison.
7856 // br V, BB1, BB2
7857 // BB1:
7858 // CtxI ; V cannot be undef or poison here
7859 auto *Dominator = DNode->getIDom();
7860 // This check is purely for compile time reasons: we can skip the IDom walk
7861 // if what we are checking for includes undef and the value is not an integer.
7862 if (!includesUndef(Kind) || V->getType()->isIntegerTy())
7863 while (Dominator) {
7864 auto *TI = Dominator->getBlock()->getTerminatorOrNull();
7865
7866 Value *Cond = nullptr;
7867 if (auto BI = dyn_cast_or_null<CondBrInst>(TI)) {
7868 Cond = BI->getCondition();
7869 } else if (auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
7870 Cond = SI->getCondition();
7871 }
7872
7873 if (Cond) {
7874 if (Cond == V)
7875 return true;
7876 else if (!includesUndef(Kind) && isa<Operator>(Cond)) {
7877 // For poison, we can analyze further
7878 auto *Opr = cast<Operator>(Cond);
7879 if (any_of(Opr->operands(), [V](const Use &U) {
7880 return V == U && propagatesPoison(U);
7881 }))
7882 return true;
7883 }
7884 }
7885
7886 Dominator = Dominator->getIDom();
7887 }
7888
7889 if (AC && getKnowledgeValidInContext(V, {Attribute::NoUndef}, *AC, CtxI, DT))
7890 return true;
7891
7892 return false;
7893}
7894
7896 const Instruction *CtxI,
7897 const DominatorTree *DT,
7898 unsigned Depth) {
7899 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7901}
7902
7904 const Instruction *CtxI,
7905 const DominatorTree *DT, unsigned Depth) {
7906 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7908}
7909
7911 const Instruction *CtxI,
7912 const DominatorTree *DT, unsigned Depth) {
7913 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7915}
7916
7917/// Return true if undefined behavior would provably be executed on the path to
7918/// OnPathTo if Root produced a posion result. Note that this doesn't say
7919/// anything about whether OnPathTo is actually executed or whether Root is
7920/// actually poison. This can be used to assess whether a new use of Root can
7921/// be added at a location which is control equivalent with OnPathTo (such as
7922/// immediately before it) without introducing UB which didn't previously
7923/// exist. Note that a false result conveys no information.
7925 Instruction *OnPathTo,
7926 DominatorTree *DT) {
7927 // Basic approach is to assume Root is poison, propagate poison forward
7928 // through all users we can easily track, and then check whether any of those
7929 // users are provable UB and must execute before out exiting block might
7930 // exit.
7931
7932 // The set of all recursive users we've visited (which are assumed to all be
7933 // poison because of said visit)
7936 Worklist.push_back(Root);
7937 while (!Worklist.empty()) {
7938 const Instruction *I = Worklist.pop_back_val();
7939
7940 // If we know this must trigger UB on a path leading our target.
7941 if (mustTriggerUB(I, KnownPoison) && DT->dominates(I, OnPathTo))
7942 return true;
7943
7944 // If we can't analyze propagation through this instruction, just skip it
7945 // and transitive users. Safe as false is a conservative result.
7946 if (I != Root && !any_of(I->operands(), [&KnownPoison](const Use &U) {
7947 return KnownPoison.contains(U) && propagatesPoison(U);
7948 }))
7949 continue;
7950
7951 if (KnownPoison.insert(I).second)
7952 for (const User *User : I->users())
7953 Worklist.push_back(cast<Instruction>(User));
7954 }
7955
7956 // Might be non-UB, or might have a path we couldn't prove must execute on
7957 // way to exiting bb.
7958 return false;
7959}
7960
7962 const SimplifyQuery &SQ) {
7963 return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1),
7964 Add, SQ);
7965}
7966
7969 const WithCache<const Value *> &RHS,
7970 const SimplifyQuery &SQ) {
7971 return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, SQ);
7972}
7973
7975 // Note: An atomic operation isn't guaranteed to return in a reasonable amount
7976 // of time because it's possible for another thread to interfere with it for an
7977 // arbitrary length of time, but programs aren't allowed to rely on that.
7978
7979 // If there is no successor, then execution can't transfer to it.
7980 if (isa<ReturnInst>(I))
7981 return false;
7983 return false;
7984
7985 // Note: Do not add new checks here; instead, change Instruction::mayThrow or
7986 // Instruction::willReturn.
7987 //
7988 // FIXME: Move this check into Instruction::willReturn.
7989 if (isa<CatchPadInst>(I)) {
7990 switch (classifyEHPersonality(I->getFunction()->getPersonalityFn())) {
7991 default:
7992 // A catchpad may invoke exception object constructors and such, which
7993 // in some languages can be arbitrary code, so be conservative by default.
7994 return false;
7996 // For CoreCLR, it just involves a type test.
7997 return true;
7998 }
7999 }
8000
8001 // An instruction that returns without throwing must transfer control flow
8002 // to a successor.
8003 return !I->mayThrow() && I->willReturn();
8004}
8005
8007 // TODO: This is slightly conservative for invoke instruction since exiting
8008 // via an exception *is* normal control for them.
8009 for (const Instruction &I : *BB)
8011 return false;
8012 return true;
8013}
8014
8021
8024 assert(ScanLimit && "scan limit must be non-zero");
8025 for (const Instruction &I : Range) {
8026 if (--ScanLimit == 0)
8027 return false;
8029 return false;
8030 }
8031 return true;
8032}
8033
8035 const Loop *L) {
8036 // The loop header is guaranteed to be executed for every iteration.
8037 //
8038 // FIXME: Relax this constraint to cover all basic blocks that are
8039 // guaranteed to be executed at every iteration.
8040 if (I->getParent() != L->getHeader()) return false;
8041
8042 for (const Instruction &LI : *L->getHeader()) {
8043 if (&LI == I) return true;
8044 if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;
8045 }
8046 llvm_unreachable("Instruction not contained in its own parent basic block.");
8047}
8048
8050 switch (IID) {
8051 // TODO: Add more intrinsics.
8052 case Intrinsic::sadd_with_overflow:
8053 case Intrinsic::ssub_with_overflow:
8054 case Intrinsic::smul_with_overflow:
8055 case Intrinsic::uadd_with_overflow:
8056 case Intrinsic::usub_with_overflow:
8057 case Intrinsic::umul_with_overflow:
8058 // If an input is a vector containing a poison element, the
8059 // two output vectors (calculated results, overflow bits)'
8060 // corresponding lanes are poison.
8061 return true;
8062 case Intrinsic::ctpop:
8063 case Intrinsic::ctlz:
8064 case Intrinsic::cttz:
8065 case Intrinsic::abs:
8066 case Intrinsic::smax:
8067 case Intrinsic::smin:
8068 case Intrinsic::umax:
8069 case Intrinsic::umin:
8070 case Intrinsic::scmp:
8071 case Intrinsic::is_fpclass:
8072 case Intrinsic::ptrmask:
8073 case Intrinsic::ucmp:
8074 case Intrinsic::bitreverse:
8075 case Intrinsic::bswap:
8076 case Intrinsic::sadd_sat:
8077 case Intrinsic::ssub_sat:
8078 case Intrinsic::sshl_sat:
8079 case Intrinsic::uadd_sat:
8080 case Intrinsic::usub_sat:
8081 case Intrinsic::ushl_sat:
8082 case Intrinsic::smul_fix:
8083 case Intrinsic::smul_fix_sat:
8084 case Intrinsic::umul_fix:
8085 case Intrinsic::umul_fix_sat:
8086 case Intrinsic::pow:
8087 case Intrinsic::powi:
8088 case Intrinsic::sin:
8089 case Intrinsic::sinh:
8090 case Intrinsic::cos:
8091 case Intrinsic::cosh:
8092 case Intrinsic::sincos:
8093 case Intrinsic::sincospi:
8094 case Intrinsic::tan:
8095 case Intrinsic::tanh:
8096 case Intrinsic::asin:
8097 case Intrinsic::acos:
8098 case Intrinsic::atan:
8099 case Intrinsic::atan2:
8100 case Intrinsic::canonicalize:
8101 case Intrinsic::sqrt:
8102 case Intrinsic::exp:
8103 case Intrinsic::exp2:
8104 case Intrinsic::exp10:
8105 case Intrinsic::log:
8106 case Intrinsic::log2:
8107 case Intrinsic::log10:
8108 case Intrinsic::modf:
8109 case Intrinsic::floor:
8110 case Intrinsic::ceil:
8111 case Intrinsic::trunc:
8112 case Intrinsic::rint:
8113 case Intrinsic::nearbyint:
8114 case Intrinsic::round:
8115 case Intrinsic::roundeven:
8116 case Intrinsic::lrint:
8117 case Intrinsic::llrint:
8118 case Intrinsic::fshl:
8119 case Intrinsic::fshr:
8120 return true;
8121 default:
8122 return false;
8123 }
8124}
8125
8126bool llvm::propagatesPoison(const Use &PoisonOp) {
8127 const Operator *I = cast<Operator>(PoisonOp.getUser());
8128 switch (I->getOpcode()) {
8129 case Instruction::Freeze:
8130 case Instruction::PHI:
8131 case Instruction::Invoke:
8132 return false;
8133 case Instruction::Select:
8134 return PoisonOp.getOperandNo() == 0;
8135 case Instruction::Call:
8136 if (auto *II = dyn_cast<IntrinsicInst>(I))
8137 return intrinsicPropagatesPoison(II->getIntrinsicID());
8138 return false;
8139 case Instruction::ICmp:
8140 case Instruction::FCmp:
8141 case Instruction::GetElementPtr:
8142 return true;
8143 default:
8145 return true;
8146
8147 // Be conservative and return false.
8148 return false;
8149 }
8150}
8151
8152/// Enumerates all operands of \p I that are guaranteed to not be undef or
8153/// poison. If the callback \p Handle returns true, stop processing and return
8154/// true. Otherwise, return false.
8155template <typename CallableT>
8157 const CallableT &Handle) {
8158 switch (I->getOpcode()) {
8159 case Instruction::Store:
8160 if (Handle(cast<StoreInst>(I)->getPointerOperand()))
8161 return true;
8162 break;
8163
8164 case Instruction::Load:
8165 if (Handle(cast<LoadInst>(I)->getPointerOperand()))
8166 return true;
8167 break;
8168
8169 // Since dereferenceable attribute imply noundef, atomic operations
8170 // also implicitly have noundef pointers too
8171 case Instruction::AtomicCmpXchg:
8173 return true;
8174 break;
8175
8176 case Instruction::AtomicRMW:
8177 if (Handle(cast<AtomicRMWInst>(I)->getPointerOperand()))
8178 return true;
8179 break;
8180
8181 case Instruction::Call:
8182 case Instruction::Invoke: {
8183 const CallBase *CB = cast<CallBase>(I);
8184 if (CB->isIndirectCall() && Handle(CB->getCalledOperand()))
8185 return true;
8186 for (unsigned i = 0; i < CB->arg_size(); ++i)
8187 if ((CB->paramHasAttr(i, Attribute::NoUndef) ||
8188 CB->paramHasAttr(i, Attribute::Dereferenceable) ||
8189 CB->paramHasAttr(i, Attribute::DereferenceableOrNull)) &&
8190 Handle(CB->getArgOperand(i)))
8191 return true;
8192 break;
8193 }
8194 case Instruction::Ret:
8195 if (I->getFunction()->hasRetAttribute(Attribute::NoUndef) &&
8196 Handle(I->getOperand(0)))
8197 return true;
8198 break;
8199 case Instruction::Switch:
8200 if (Handle(cast<SwitchInst>(I)->getCondition()))
8201 return true;
8202 break;
8203 case Instruction::CondBr:
8204 if (Handle(cast<CondBrInst>(I)->getCondition()))
8205 return true;
8206 break;
8207 default:
8208 break;
8209 }
8210
8211 return false;
8212}
8213
8214/// Enumerates all operands of \p I that are guaranteed to not be poison.
8215template <typename CallableT>
8217 const CallableT &Handle) {
8218 if (handleGuaranteedWellDefinedOps(I, Handle))
8219 return true;
8220 switch (I->getOpcode()) {
8221 // Divisors of these operations are allowed to be partially undef.
8222 case Instruction::UDiv:
8223 case Instruction::SDiv:
8224 case Instruction::URem:
8225 case Instruction::SRem:
8226 return Handle(I->getOperand(1));
8227 default:
8228 return false;
8229 }
8230}
8231
8233 const SmallPtrSetImpl<const Value *> &KnownPoison) {
8235 I, [&](const Value *V) { return KnownPoison.count(V); });
8236}
8237
8239 bool PoisonOnly) {
8240 // We currently only look for uses of values within the same basic
8241 // block, as that makes it easier to guarantee that the uses will be
8242 // executed given that Inst is executed.
8243 //
8244 // FIXME: Expand this to consider uses beyond the same basic block. To do
8245 // this, look out for the distinction between post-dominance and strong
8246 // post-dominance.
8247 const BasicBlock *BB = nullptr;
8249 if (const auto *Inst = dyn_cast<Instruction>(V)) {
8250 BB = Inst->getParent();
8251 Begin = Inst->getIterator();
8252 Begin++;
8253 } else if (const auto *Arg = dyn_cast<Argument>(V)) {
8254 if (Arg->getParent()->isDeclaration())
8255 return false;
8256 BB = &Arg->getParent()->getEntryBlock();
8257 Begin = BB->begin();
8258 } else {
8259 return false;
8260 }
8261
8262 // Limit number of instructions we look at, to avoid scanning through large
8263 // blocks. The current limit is chosen arbitrarily.
8264 unsigned ScanLimit = 32;
8265 BasicBlock::const_iterator End = BB->end();
8266
8267 if (!PoisonOnly) {
8268 // Since undef does not propagate eagerly, be conservative & just check
8269 // whether a value is directly passed to an instruction that must take
8270 // well-defined operands.
8271
8272 for (const auto &I : make_range(Begin, End)) {
8273 if (--ScanLimit == 0)
8274 break;
8275
8276 if (handleGuaranteedWellDefinedOps(&I, [V](const Value *WellDefinedOp) {
8277 return WellDefinedOp == V;
8278 }))
8279 return true;
8280
8282 break;
8283 }
8284 return false;
8285 }
8286
8287 // Set of instructions that we have proved will yield poison if Inst
8288 // does.
8289 SmallPtrSet<const Value *, 16> YieldsPoison;
8291
8292 YieldsPoison.insert(V);
8293 Visited.insert(BB);
8294
8295 while (true) {
8296 for (const auto &I : make_range(Begin, End)) {
8297 if (--ScanLimit == 0)
8298 return false;
8299 if (mustTriggerUB(&I, YieldsPoison))
8300 return true;
8302 return false;
8303
8304 // If an operand is poison and propagates it, mark I as yielding poison.
8305 for (const Use &Op : I.operands()) {
8306 if (YieldsPoison.count(Op) && propagatesPoison(Op)) {
8307 YieldsPoison.insert(&I);
8308 break;
8309 }
8310 }
8311
8312 // Special handling for select, which returns poison if its operand 0 is
8313 // poison (handled in the loop above) *or* if both its true/false operands
8314 // are poison (handled here).
8315 if (I.getOpcode() == Instruction::Select &&
8316 YieldsPoison.count(I.getOperand(1)) &&
8317 YieldsPoison.count(I.getOperand(2))) {
8318 YieldsPoison.insert(&I);
8319 }
8320 }
8321
8322 BB = BB->getSingleSuccessor();
8323 if (!BB || !Visited.insert(BB).second)
8324 break;
8325
8326 Begin = BB->getFirstNonPHIIt();
8327 End = BB->end();
8328 }
8329 return false;
8330}
8331
8333 return ::programUndefinedIfUndefOrPoison(Inst, false);
8334}
8335
8337 return ::programUndefinedIfUndefOrPoison(Inst, true);
8338}
8339
8340static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) {
8341 if (FMF.noNaNs())
8342 return true;
8343
8344 if (auto *C = dyn_cast<ConstantFP>(V))
8345 return !C->isNaN();
8346
8347 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8348 if (!C->getElementType()->isFloatingPointTy())
8349 return false;
8350 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8351 if (C->getElementAsAPFloat(I).isNaN())
8352 return false;
8353 }
8354 return true;
8355 }
8356
8358 return true;
8359
8360 return false;
8361}
8362
8363static bool isKnownNonZero(const Value *V) {
8364 if (auto *C = dyn_cast<ConstantFP>(V))
8365 return !C->isZero();
8366
8367 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8368 if (!C->getElementType()->isFloatingPointTy())
8369 return false;
8370 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8371 if (C->getElementAsAPFloat(I).isZero())
8372 return false;
8373 }
8374 return true;
8375 }
8376
8377 return false;
8378}
8379
8380/// Match clamp pattern for float types without care about NaNs or signed zeros.
8381/// Given non-min/max outer cmp/select from the clamp pattern this
8382/// function recognizes if it can be substitued by a "canonical" min/max
8383/// pattern.
8385 Value *CmpLHS, Value *CmpRHS,
8386 Value *TrueVal, Value *FalseVal,
8387 Value *&LHS, Value *&RHS) {
8388 // Try to match
8389 // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2))
8390 // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2))
8391 // and return description of the outer Max/Min.
8392
8393 // First, check if select has inverse order:
8394 if (CmpRHS == FalseVal) {
8395 std::swap(TrueVal, FalseVal);
8396 Pred = CmpInst::getInversePredicate(Pred);
8397 }
8398
8399 // Assume success now. If there's no match, callers should not use these anyway.
8400 LHS = TrueVal;
8401 RHS = FalseVal;
8402
8403 const APFloat *FC1;
8404 if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite())
8405 return {SPF_UNKNOWN, SPNB_NA, false};
8406
8407 const APFloat *FC2;
8408 switch (Pred) {
8409 case CmpInst::FCMP_OLT:
8410 case CmpInst::FCMP_OLE:
8411 case CmpInst::FCMP_ULT:
8412 case CmpInst::FCMP_ULE:
8413 if (match(FalseVal, m_OrdOrUnordFMin(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8414 *FC1 < *FC2)
8415 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
8416 break;
8417 case CmpInst::FCMP_OGT:
8418 case CmpInst::FCMP_OGE:
8419 case CmpInst::FCMP_UGT:
8420 case CmpInst::FCMP_UGE:
8421 if (match(FalseVal, m_OrdOrUnordFMax(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8422 *FC1 > *FC2)
8423 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
8424 break;
8425 default:
8426 break;
8427 }
8428
8429 return {SPF_UNKNOWN, SPNB_NA, false};
8430}
8431
8432/// Recognize variations of:
8433/// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
8435 Value *CmpLHS, Value *CmpRHS,
8436 Value *TrueVal, Value *FalseVal) {
8437 // Swap the select operands and predicate to match the patterns below.
8438 if (CmpRHS != TrueVal) {
8439 Pred = ICmpInst::getSwappedPredicate(Pred);
8440 std::swap(TrueVal, FalseVal);
8441 }
8442 const APInt *C1;
8443 if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) {
8444 const APInt *C2;
8445 // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
8446 if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8447 C1->slt(*C2) && Pred == CmpInst::ICMP_SLT)
8448 return {SPF_SMAX, SPNB_NA, false};
8449
8450 // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
8451 if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8452 C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT)
8453 return {SPF_SMIN, SPNB_NA, false};
8454
8455 // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
8456 if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8457 C1->ult(*C2) && Pred == CmpInst::ICMP_ULT)
8458 return {SPF_UMAX, SPNB_NA, false};
8459
8460 // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
8461 if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8462 C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT)
8463 return {SPF_UMIN, SPNB_NA, false};
8464 }
8465 return {SPF_UNKNOWN, SPNB_NA, false};
8466}
8467
8468/// Recognize variations of:
8469/// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c))
8471 Value *CmpLHS, Value *CmpRHS,
8472 Value *TVal, Value *FVal,
8473 unsigned Depth) {
8474 // TODO: Allow FP min/max with nnan/nsz.
8475 assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison");
8476
8477 Value *A = nullptr, *B = nullptr;
8478 SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1);
8479 if (!SelectPatternResult::isMinOrMax(L.Flavor))
8480 return {SPF_UNKNOWN, SPNB_NA, false};
8481
8482 Value *C = nullptr, *D = nullptr;
8483 SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1);
8484 if (L.Flavor != R.Flavor)
8485 return {SPF_UNKNOWN, SPNB_NA, false};
8486
8487 // We have something like: x Pred y ? min(a, b) : min(c, d).
8488 // Try to match the compare to the min/max operations of the select operands.
8489 // First, make sure we have the right compare predicate.
8490 switch (L.Flavor) {
8491 case SPF_SMIN:
8492 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) {
8493 Pred = ICmpInst::getSwappedPredicate(Pred);
8494 std::swap(CmpLHS, CmpRHS);
8495 }
8496 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
8497 break;
8498 return {SPF_UNKNOWN, SPNB_NA, false};
8499 case SPF_SMAX:
8500 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) {
8501 Pred = ICmpInst::getSwappedPredicate(Pred);
8502 std::swap(CmpLHS, CmpRHS);
8503 }
8504 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE)
8505 break;
8506 return {SPF_UNKNOWN, SPNB_NA, false};
8507 case SPF_UMIN:
8508 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
8509 Pred = ICmpInst::getSwappedPredicate(Pred);
8510 std::swap(CmpLHS, CmpRHS);
8511 }
8512 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE)
8513 break;
8514 return {SPF_UNKNOWN, SPNB_NA, false};
8515 case SPF_UMAX:
8516 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
8517 Pred = ICmpInst::getSwappedPredicate(Pred);
8518 std::swap(CmpLHS, CmpRHS);
8519 }
8520 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
8521 break;
8522 return {SPF_UNKNOWN, SPNB_NA, false};
8523 default:
8524 return {SPF_UNKNOWN, SPNB_NA, false};
8525 }
8526
8527 // If there is a common operand in the already matched min/max and the other
8528 // min/max operands match the compare operands (either directly or inverted),
8529 // then this is min/max of the same flavor.
8530
8531 // a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8532 // ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8533 if (D == B) {
8534 if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8535 match(A, m_Not(m_Specific(CmpRHS)))))
8536 return {L.Flavor, SPNB_NA, false};
8537 }
8538 // a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8539 // ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8540 if (C == B) {
8541 if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8542 match(A, m_Not(m_Specific(CmpRHS)))))
8543 return {L.Flavor, SPNB_NA, false};
8544 }
8545 // b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8546 // ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8547 if (D == A) {
8548 if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8549 match(B, m_Not(m_Specific(CmpRHS)))))
8550 return {L.Flavor, SPNB_NA, false};
8551 }
8552 // b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8553 // ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8554 if (C == A) {
8555 if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8556 match(B, m_Not(m_Specific(CmpRHS)))))
8557 return {L.Flavor, SPNB_NA, false};
8558 }
8559
8560 return {SPF_UNKNOWN, SPNB_NA, false};
8561}
8562
8563/// If the input value is the result of a 'not' op, constant integer, or vector
8564/// splat of a constant integer, return the bitwise-not source value.
8565/// TODO: This could be extended to handle non-splat vector integer constants.
8567 Value *NotV;
8568 if (match(V, m_Not(m_Value(NotV))))
8569 return NotV;
8570
8571 const APInt *C;
8572 if (match(V, m_APInt(C)))
8573 return ConstantInt::get(V->getType(), ~(*C));
8574
8575 return nullptr;
8576}
8577
8578/// Match non-obvious integer minimum and maximum sequences.
8580 Value *CmpLHS, Value *CmpRHS,
8581 Value *TrueVal, Value *FalseVal,
8582 Value *&LHS, Value *&RHS,
8583 unsigned Depth) {
8584 // Assume success. If there's no match, callers should not use these anyway.
8585 LHS = TrueVal;
8586 RHS = FalseVal;
8587
8588 SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);
8590 return SPR;
8591
8592 SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth);
8594 return SPR;
8595
8596 // Look through 'not' ops to find disguised min/max.
8597 // (X > Y) ? ~X : ~Y ==> (~X < ~Y) ? ~X : ~Y ==> MIN(~X, ~Y)
8598 // (X < Y) ? ~X : ~Y ==> (~X > ~Y) ? ~X : ~Y ==> MAX(~X, ~Y)
8599 if (CmpLHS == getNotValue(TrueVal) && CmpRHS == getNotValue(FalseVal)) {
8600 switch (Pred) {
8601 case CmpInst::ICMP_SGT: return {SPF_SMIN, SPNB_NA, false};
8602 case CmpInst::ICMP_SLT: return {SPF_SMAX, SPNB_NA, false};
8603 case CmpInst::ICMP_UGT: return {SPF_UMIN, SPNB_NA, false};
8604 case CmpInst::ICMP_ULT: return {SPF_UMAX, SPNB_NA, false};
8605 default: break;
8606 }
8607 }
8608
8609 // (X > Y) ? ~Y : ~X ==> (~X < ~Y) ? ~Y : ~X ==> MAX(~Y, ~X)
8610 // (X < Y) ? ~Y : ~X ==> (~X > ~Y) ? ~Y : ~X ==> MIN(~Y, ~X)
8611 if (CmpLHS == getNotValue(FalseVal) && CmpRHS == getNotValue(TrueVal)) {
8612 switch (Pred) {
8613 case CmpInst::ICMP_SGT: return {SPF_SMAX, SPNB_NA, false};
8614 case CmpInst::ICMP_SLT: return {SPF_SMIN, SPNB_NA, false};
8615 case CmpInst::ICMP_UGT: return {SPF_UMAX, SPNB_NA, false};
8616 case CmpInst::ICMP_ULT: return {SPF_UMIN, SPNB_NA, false};
8617 default: break;
8618 }
8619 }
8620
8621 if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT)
8622 return {SPF_UNKNOWN, SPNB_NA, false};
8623
8624 const APInt *C1;
8625 if (!match(CmpRHS, m_APInt(C1)))
8626 return {SPF_UNKNOWN, SPNB_NA, false};
8627
8628 // An unsigned min/max can be written with a signed compare.
8629 const APInt *C2;
8630 if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) ||
8631 (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) {
8632 // Is the sign bit set?
8633 // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX
8634 // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN
8635 if (Pred == CmpInst::ICMP_SLT && C1->isZero() && C2->isMaxSignedValue())
8636 return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8637
8638 // Is the sign bit clear?
8639 // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX
8640 // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN
8641 if (Pred == CmpInst::ICMP_SGT && C1->isAllOnes() && C2->isMinSignedValue())
8642 return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8643 }
8644
8645 return {SPF_UNKNOWN, SPNB_NA, false};
8646}
8647
8648bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW,
8649 bool AllowPoison) {
8650 assert(X && Y && "Invalid operand");
8651
8652 auto IsNegationOf = [&](const Value *X, const Value *Y) {
8653 if (!match(X, m_Neg(m_Specific(Y))))
8654 return false;
8655
8656 auto *BO = cast<BinaryOperator>(X);
8657 if (NeedNSW && !BO->hasNoSignedWrap())
8658 return false;
8659
8660 auto *Zero = cast<Constant>(BO->getOperand(0));
8661 if (!AllowPoison && !Zero->isNullValue())
8662 return false;
8663
8664 return true;
8665 };
8666
8667 // X = -Y or Y = -X
8668 if (IsNegationOf(X, Y) || IsNegationOf(Y, X))
8669 return true;
8670
8671 // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A)
8672 Value *A, *B;
8673 return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) &&
8674 match(Y, m_Sub(m_Specific(B), m_Specific(A))))) ||
8675 (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) &&
8677}
8678
8679bool llvm::isKnownInversion(const Value *X, const Value *Y) {
8680 // Handle X = icmp pred A, B, Y = icmp pred A, C.
8681 Value *A, *B, *C;
8682 CmpPredicate Pred1, Pred2;
8683 if (!match(X, m_ICmp(Pred1, m_Value(A), m_Value(B))) ||
8684 !match(Y, m_c_ICmp(Pred2, m_Specific(A), m_Value(C))))
8685 return false;
8686
8687 // They must both have samesign flag or not.
8688 if (Pred1.hasSameSign() != Pred2.hasSameSign())
8689 return false;
8690
8691 if (B == C)
8692 return Pred1 == ICmpInst::getInversePredicate(Pred2);
8693
8694 // Try to infer the relationship from constant ranges.
8695 const APInt *RHSC1, *RHSC2;
8696 if (!match(B, m_APInt(RHSC1)) || !match(C, m_APInt(RHSC2)))
8697 return false;
8698
8699 // Sign bits of two RHSCs should match.
8700 if (Pred1.hasSameSign() && RHSC1->isNonNegative() != RHSC2->isNonNegative())
8701 return false;
8702
8703 const auto CR1 = ConstantRange::makeExactICmpRegion(Pred1, *RHSC1);
8704 const auto CR2 = ConstantRange::makeExactICmpRegion(Pred2, *RHSC2);
8705
8706 return CR1.inverse() == CR2;
8707}
8708
8710 SelectPatternNaNBehavior NaNBehavior,
8711 bool Ordered) {
8712 switch (Pred) {
8713 default:
8714 return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.
8715 case ICmpInst::ICMP_UGT:
8716 case ICmpInst::ICMP_UGE:
8717 return {SPF_UMAX, SPNB_NA, false};
8718 case ICmpInst::ICMP_SGT:
8719 case ICmpInst::ICMP_SGE:
8720 return {SPF_SMAX, SPNB_NA, false};
8721 case ICmpInst::ICMP_ULT:
8722 case ICmpInst::ICMP_ULE:
8723 return {SPF_UMIN, SPNB_NA, false};
8724 case ICmpInst::ICMP_SLT:
8725 case ICmpInst::ICMP_SLE:
8726 return {SPF_SMIN, SPNB_NA, false};
8727 case FCmpInst::FCMP_UGT:
8728 case FCmpInst::FCMP_UGE:
8729 case FCmpInst::FCMP_OGT:
8730 case FCmpInst::FCMP_OGE:
8731 return {SPF_FMAXNUM, NaNBehavior, Ordered};
8732 case FCmpInst::FCMP_ULT:
8733 case FCmpInst::FCMP_ULE:
8734 case FCmpInst::FCMP_OLT:
8735 case FCmpInst::FCMP_OLE:
8736 return {SPF_FMINNUM, NaNBehavior, Ordered};
8737 }
8738}
8739
8740std::optional<std::pair<CmpPredicate, Constant *>>
8743 "Only for relational integer predicates.");
8744 if (isa<UndefValue>(C))
8745 return std::nullopt;
8746
8747 Type *Type = C->getType();
8748 bool IsSigned = ICmpInst::isSigned(Pred);
8749
8751 bool WillIncrement =
8752 UnsignedPred == ICmpInst::ICMP_ULE || UnsignedPred == ICmpInst::ICMP_UGT;
8753
8754 // Check if the constant operand can be safely incremented/decremented
8755 // without overflowing/underflowing.
8756 auto ConstantIsOk = [WillIncrement, IsSigned](ConstantInt *C) {
8757 return WillIncrement ? !C->isMaxValue(IsSigned) : !C->isMinValue(IsSigned);
8758 };
8759
8760 Constant *SafeReplacementConstant = nullptr;
8761 if (auto *CI = dyn_cast<ConstantInt>(C)) {
8762 // Bail out if the constant can't be safely incremented/decremented.
8763 if (!ConstantIsOk(CI))
8764 return std::nullopt;
8765 } else if (auto *FVTy = dyn_cast<FixedVectorType>(Type)) {
8766 unsigned NumElts = FVTy->getNumElements();
8767 for (unsigned i = 0; i != NumElts; ++i) {
8768 Constant *Elt = C->getAggregateElement(i);
8769 if (!Elt)
8770 return std::nullopt;
8771
8772 if (isa<UndefValue>(Elt))
8773 continue;
8774
8775 // Bail out if we can't determine if this constant is min/max or if we
8776 // know that this constant is min/max.
8777 auto *CI = dyn_cast<ConstantInt>(Elt);
8778 if (!CI || !ConstantIsOk(CI))
8779 return std::nullopt;
8780
8781 if (!SafeReplacementConstant)
8782 SafeReplacementConstant = CI;
8783 }
8784 } else if (isa<VectorType>(C->getType())) {
8785 // Handle scalable splat
8786 Value *SplatC = C->getSplatValue();
8787 auto *CI = dyn_cast_or_null<ConstantInt>(SplatC);
8788 // Bail out if the constant can't be safely incremented/decremented.
8789 if (!CI || !ConstantIsOk(CI))
8790 return std::nullopt;
8791 } else {
8792 // ConstantExpr?
8793 return std::nullopt;
8794 }
8795
8796 // It may not be safe to change a compare predicate in the presence of
8797 // undefined elements, so replace those elements with the first safe constant
8798 // that we found.
8799 // TODO: in case of poison, it is safe; let's replace undefs only.
8800 if (C->containsUndefOrPoisonElement()) {
8801 assert(SafeReplacementConstant && "Replacement constant not set");
8802 C = Constant::replaceUndefsWith(C, SafeReplacementConstant);
8803 }
8804
8806
8807 // Increment or decrement the constant.
8808 Constant *OneOrNegOne = ConstantInt::get(Type, WillIncrement ? 1 : -1, true);
8809 Constant *NewC = ConstantExpr::getAdd(C, OneOrNegOne);
8810
8811 return std::make_pair(NewPred, NewC);
8812}
8813
8815 FastMathFlags FMF,
8816 Value *CmpLHS, Value *CmpRHS,
8817 Value *TrueVal, Value *FalseVal,
8818 Value *&LHS, Value *&RHS,
8819 unsigned Depth) {
8820 bool HasMismatchedZeros = false;
8821 if (CmpInst::isFPPredicate(Pred)) {
8822 // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one
8823 // 0.0 operand, set the compare's 0.0 operands to that same value for the
8824 // purpose of identifying min/max. Disregard vector constants with undefined
8825 // elements because those can not be back-propagated for analysis.
8826 Value *OutputZeroVal = nullptr;
8827 if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) &&
8828 !cast<Constant>(TrueVal)->containsUndefOrPoisonElement())
8829 OutputZeroVal = TrueVal;
8830 else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) &&
8831 !cast<Constant>(FalseVal)->containsUndefOrPoisonElement())
8832 OutputZeroVal = FalseVal;
8833
8834 if (OutputZeroVal) {
8835 if (match(CmpLHS, m_AnyZeroFP()) && CmpLHS != OutputZeroVal) {
8836 HasMismatchedZeros = true;
8837 CmpLHS = OutputZeroVal;
8838 }
8839 if (match(CmpRHS, m_AnyZeroFP()) && CmpRHS != OutputZeroVal) {
8840 HasMismatchedZeros = true;
8841 CmpRHS = OutputZeroVal;
8842 }
8843 }
8844 }
8845
8846 LHS = CmpLHS;
8847 RHS = CmpRHS;
8848
8849 // Signed zero may return inconsistent results between implementations.
8850 // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0
8851 // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)
8852 // Therefore, we behave conservatively and only proceed if at least one of the
8853 // operands is known to not be zero or if we don't care about signed zero.
8854 switch (Pred) {
8855 default: break;
8858 if (!HasMismatchedZeros)
8859 break;
8860 [[fallthrough]];
8863 if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8864 !isKnownNonZero(CmpRHS))
8865 return {SPF_UNKNOWN, SPNB_NA, false};
8866 }
8867
8868 SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
8869 bool Ordered = false;
8870
8871 // When given one NaN and one non-NaN input:
8872 // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
8873 // - A simple C99 (a < b ? a : b) construction will return 'b' (as the
8874 // ordered comparison fails), which could be NaN or non-NaN.
8875 // so here we discover exactly what NaN behavior is required/accepted.
8876 if (CmpInst::isFPPredicate(Pred)) {
8877 bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
8878 bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
8879
8880 if (LHSSafe && RHSSafe) {
8881 // Both operands are known non-NaN.
8882 NaNBehavior = SPNB_RETURNS_ANY;
8883 Ordered = CmpInst::isOrdered(Pred);
8884 } else if (CmpInst::isOrdered(Pred)) {
8885 // An ordered comparison will return false when given a NaN, so it
8886 // returns the RHS.
8887 Ordered = true;
8888 if (LHSSafe)
8889 // LHS is non-NaN, so if RHS is NaN then NaN will be returned.
8890 NaNBehavior = SPNB_RETURNS_NAN;
8891 else if (RHSSafe)
8892 NaNBehavior = SPNB_RETURNS_OTHER;
8893 else
8894 // Completely unsafe.
8895 return {SPF_UNKNOWN, SPNB_NA, false};
8896 } else {
8897 Ordered = false;
8898 // An unordered comparison will return true when given a NaN, so it
8899 // returns the LHS.
8900 if (LHSSafe)
8901 // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
8902 NaNBehavior = SPNB_RETURNS_OTHER;
8903 else if (RHSSafe)
8904 NaNBehavior = SPNB_RETURNS_NAN;
8905 else
8906 // Completely unsafe.
8907 return {SPF_UNKNOWN, SPNB_NA, false};
8908 }
8909 }
8910
8911 if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
8912 std::swap(CmpLHS, CmpRHS);
8913 Pred = CmpInst::getSwappedPredicate(Pred);
8914 if (NaNBehavior == SPNB_RETURNS_NAN)
8915 NaNBehavior = SPNB_RETURNS_OTHER;
8916 else if (NaNBehavior == SPNB_RETURNS_OTHER)
8917 NaNBehavior = SPNB_RETURNS_NAN;
8918 Ordered = !Ordered;
8919 }
8920
8921 // ([if]cmp X, Y) ? X : Y
8922 if (TrueVal == CmpLHS && FalseVal == CmpRHS)
8923 return getSelectPattern(Pred, NaNBehavior, Ordered);
8924
8925 if (isKnownNegation(TrueVal, FalseVal)) {
8926 // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can
8927 // match against either LHS or sext(LHS).
8928 auto MaybeSExtCmpLHS =
8929 m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS)));
8930 auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes());
8931 auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One());
8932 if (match(TrueVal, MaybeSExtCmpLHS)) {
8933 // Set the return values. If the compare uses the negated value (-X >s 0),
8934 // swap the return values because the negated value is always 'RHS'.
8935 LHS = TrueVal;
8936 RHS = FalseVal;
8937 if (match(CmpLHS, m_Neg(m_Specific(FalseVal))))
8938 std::swap(LHS, RHS);
8939
8940 // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X)
8941 // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X)
8942 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8943 return {SPF_ABS, SPNB_NA, false};
8944
8945 // (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X)
8946 if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne))
8947 return {SPF_ABS, SPNB_NA, false};
8948
8949 // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X)
8950 // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X)
8951 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8952 return {SPF_NABS, SPNB_NA, false};
8953 }
8954 else if (match(FalseVal, MaybeSExtCmpLHS)) {
8955 // Set the return values. If the compare uses the negated value (-X >s 0),
8956 // swap the return values because the negated value is always 'RHS'.
8957 LHS = FalseVal;
8958 RHS = TrueVal;
8959 if (match(CmpLHS, m_Neg(m_Specific(TrueVal))))
8960 std::swap(LHS, RHS);
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_SGT && match(CmpRHS, ZeroOrAllOnes))
8965 return {SPF_NABS, SPNB_NA, false};
8966
8967 // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X)
8968 // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X)
8969 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8970 return {SPF_ABS, SPNB_NA, false};
8971 }
8972 }
8973
8974 if (CmpInst::isIntPredicate(Pred))
8975 return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth);
8976
8977 // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar
8978 // may return either -0.0 or 0.0, so fcmp/select pair has stricter
8979 // semantics than minNum. Be conservative in such case.
8980 if (NaNBehavior != SPNB_RETURNS_ANY ||
8981 (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8982 !isKnownNonZero(CmpRHS)))
8983 return {SPF_UNKNOWN, SPNB_NA, false};
8984
8985 return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
8986}
8987
8989 Instruction::CastOps *CastOp) {
8990 const DataLayout &DL = CmpI->getDataLayout();
8991
8992 Constant *CastedTo = nullptr;
8993 switch (*CastOp) {
8994 case Instruction::ZExt:
8995 if (CmpI->isUnsigned())
8996 CastedTo = ConstantExpr::getTrunc(C, SrcTy);
8997 break;
8998 case Instruction::SExt:
8999 if (CmpI->isSigned())
9000 CastedTo = ConstantExpr::getTrunc(C, SrcTy, true);
9001 break;
9002 case Instruction::Trunc:
9003 Constant *CmpConst;
9004 if (match(CmpI->getOperand(1), m_Constant(CmpConst)) &&
9005 CmpConst->getType() == SrcTy) {
9006 // Here we have the following case:
9007 //
9008 // %cond = cmp iN %x, CmpConst
9009 // %tr = trunc iN %x to iK
9010 // %narrowsel = select i1 %cond, iK %t, iK C
9011 //
9012 // We can always move trunc after select operation:
9013 //
9014 // %cond = cmp iN %x, CmpConst
9015 // %widesel = select i1 %cond, iN %x, iN CmpConst
9016 // %tr = trunc iN %widesel to iK
9017 //
9018 // Note that C could be extended in any way because we don't care about
9019 // upper bits after truncation. It can't be abs pattern, because it would
9020 // look like:
9021 //
9022 // select i1 %cond, x, -x.
9023 //
9024 // So only min/max pattern could be matched. Such match requires widened C
9025 // == CmpConst. That is why set widened C = CmpConst, condition trunc
9026 // CmpConst == C is checked below.
9027 CastedTo = CmpConst;
9028 } else {
9029 unsigned ExtOp = CmpI->isSigned() ? Instruction::SExt : Instruction::ZExt;
9030 CastedTo = ConstantFoldCastOperand(ExtOp, C, SrcTy, DL);
9031 }
9032 break;
9033 case Instruction::FPTrunc:
9034 CastedTo = ConstantFoldCastOperand(Instruction::FPExt, C, SrcTy, DL);
9035 break;
9036 case Instruction::FPExt:
9037 CastedTo = ConstantFoldCastOperand(Instruction::FPTrunc, C, SrcTy, DL);
9038 break;
9039 case Instruction::FPToUI:
9040 CastedTo = ConstantFoldCastOperand(Instruction::UIToFP, C, SrcTy, DL);
9041 break;
9042 case Instruction::FPToSI:
9043 CastedTo = ConstantFoldCastOperand(Instruction::SIToFP, C, SrcTy, DL);
9044 break;
9045 case Instruction::UIToFP:
9046 CastedTo = ConstantFoldCastOperand(Instruction::FPToUI, C, SrcTy, DL);
9047 break;
9048 case Instruction::SIToFP:
9049 CastedTo = ConstantFoldCastOperand(Instruction::FPToSI, C, SrcTy, DL);
9050 break;
9051 default:
9052 break;
9053 }
9054
9055 if (!CastedTo)
9056 return nullptr;
9057
9058 // Make sure the cast doesn't lose any information.
9059 Constant *CastedBack =
9060 ConstantFoldCastOperand(*CastOp, CastedTo, C->getType(), DL);
9061 if (CastedBack && CastedBack != C)
9062 return nullptr;
9063
9064 return CastedTo;
9065}
9066
9067/// Helps to match a select pattern in case of a type mismatch.
9068///
9069/// The function processes the case when type of true and false values of a
9070/// select instruction differs from type of the cmp instruction operands because
9071/// of a cast instruction. The function checks if it is legal to move the cast
9072/// operation after "select". If yes, it returns the new second value of
9073/// "select" (with the assumption that cast is moved):
9074/// 1. As operand of cast instruction when both values of "select" are same cast
9075/// instructions.
9076/// 2. As restored constant (by applying reverse cast operation) when the first
9077/// value of the "select" is a cast operation and the second value is a
9078/// constant. It is implemented in lookThroughCastConst().
9079/// 3. As one operand is cast instruction and the other is not. The operands in
9080/// sel(cmp) are in different type integer.
9081/// NOTE: We return only the new second value because the first value could be
9082/// accessed as operand of cast instruction.
9083static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2,
9084 Instruction::CastOps *CastOp) {
9085 auto *Cast1 = dyn_cast<CastInst>(V1);
9086 if (!Cast1)
9087 return nullptr;
9088
9089 *CastOp = Cast1->getOpcode();
9090 Type *SrcTy = Cast1->getSrcTy();
9091 if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
9092 // If V1 and V2 are both the same cast from the same type, look through V1.
9093 if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
9094 return Cast2->getOperand(0);
9095 return nullptr;
9096 }
9097
9098 auto *C = dyn_cast<Constant>(V2);
9099 if (C)
9100 return lookThroughCastConst(CmpI, SrcTy, C, CastOp);
9101
9102 Value *CastedTo = nullptr;
9103 if (*CastOp == Instruction::Trunc) {
9104 if (match(CmpI->getOperand(1), m_ZExtOrSExt(m_Specific(V2)))) {
9105 // Here we have the following case:
9106 // %y_ext = sext iK %y to iN
9107 // %cond = cmp iN %x, %y_ext
9108 // %tr = trunc iN %x to iK
9109 // %narrowsel = select i1 %cond, iK %tr, iK %y
9110 //
9111 // We can always move trunc after select operation:
9112 // %y_ext = sext iK %y to iN
9113 // %cond = cmp iN %x, %y_ext
9114 // %widesel = select i1 %cond, iN %x, iN %y_ext
9115 // %tr = trunc iN %widesel to iK
9116 assert(V2->getType() == Cast1->getType() &&
9117 "V2 and Cast1 should be the same type.");
9118 CastedTo = CmpI->getOperand(1);
9119 }
9120 }
9121
9122 return CastedTo;
9123}
9125 Instruction::CastOps *CastOp,
9126 unsigned Depth) {
9128 return {SPF_UNKNOWN, SPNB_NA, false};
9129
9131 if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
9132
9133 CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
9134 if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};
9135
9136 Value *TrueVal = SI->getTrueValue();
9137 Value *FalseVal = SI->getFalseValue();
9138
9140 CmpI, TrueVal, FalseVal, LHS, RHS,
9141 isa<FPMathOperator>(SI) ? SI->getFastMathFlags() : FastMathFlags(),
9142 CastOp, Depth);
9143}
9144
9146 CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS,
9147 FastMathFlags FMF, Instruction::CastOps *CastOp, unsigned Depth) {
9148 CmpInst::Predicate Pred = CmpI->getPredicate();
9149 Value *CmpLHS = CmpI->getOperand(0);
9150 Value *CmpRHS = CmpI->getOperand(1);
9151 if (isa<FPMathOperator>(CmpI) && CmpI->hasNoNaNs())
9152 FMF.setNoNaNs();
9153
9154 // Bail out early.
9155 if (CmpI->isEquality())
9156 return {SPF_UNKNOWN, SPNB_NA, false};
9157
9158 // Deal with type mismatches.
9159 if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
9160 if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) {
9161 // If this is a potential fmin/fmax with a cast to integer, then ignore
9162 // -0.0 because there is no corresponding integer value.
9163 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9164 FMF.setNoSignedZeros();
9165 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9166 cast<CastInst>(TrueVal)->getOperand(0), C,
9167 LHS, RHS, Depth);
9168 }
9169 if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) {
9170 // If this is a potential fmin/fmax with a cast to integer, then ignore
9171 // -0.0 because there is no corresponding integer value.
9172 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9173 FMF.setNoSignedZeros();
9174 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9175 C, cast<CastInst>(FalseVal)->getOperand(0),
9176 LHS, RHS, Depth);
9177 }
9178 }
9179 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
9180 LHS, RHS, Depth);
9181}
9182
9184 if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT;
9185 if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT;
9186 if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT;
9187 if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT;
9188 if (SPF == SPF_FMINNUM)
9189 return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT;
9190 if (SPF == SPF_FMAXNUM)
9191 return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT;
9192 llvm_unreachable("unhandled!");
9193}
9194
9196 switch (SPF) {
9198 return Intrinsic::umin;
9200 return Intrinsic::umax;
9202 return Intrinsic::smin;
9204 return Intrinsic::smax;
9205 default:
9206 llvm_unreachable("Unexpected SPF");
9207 }
9208}
9209
9211 if (SPF == SPF_SMIN) return SPF_SMAX;
9212 if (SPF == SPF_UMIN) return SPF_UMAX;
9213 if (SPF == SPF_SMAX) return SPF_SMIN;
9214 if (SPF == SPF_UMAX) return SPF_UMIN;
9215 llvm_unreachable("unhandled!");
9216}
9217
9219 switch (MinMaxID) {
9220 case Intrinsic::smax: return Intrinsic::smin;
9221 case Intrinsic::smin: return Intrinsic::smax;
9222 case Intrinsic::umax: return Intrinsic::umin;
9223 case Intrinsic::umin: return Intrinsic::umax;
9224 // Please note that next four intrinsics may produce the same result for
9225 // original and inverted case even if X != Y due to NaN is handled specially.
9226 case Intrinsic::maximum: return Intrinsic::minimum;
9227 case Intrinsic::minimum: return Intrinsic::maximum;
9228 case Intrinsic::maxnum: return Intrinsic::minnum;
9229 case Intrinsic::minnum: return Intrinsic::maxnum;
9230 case Intrinsic::maximumnum:
9231 return Intrinsic::minimumnum;
9232 case Intrinsic::minimumnum:
9233 return Intrinsic::maximumnum;
9234 default: llvm_unreachable("Unexpected intrinsic");
9235 }
9236}
9237
9239 switch (SPF) {
9242 case SPF_UMAX: return APInt::getMaxValue(BitWidth);
9243 case SPF_UMIN: return APInt::getMinValue(BitWidth);
9244 default: llvm_unreachable("Unexpected flavor");
9245 }
9246}
9247
9248std::pair<Intrinsic::ID, bool>
9250 // Check if VL contains select instructions that can be folded into a min/max
9251 // vector intrinsic and return the intrinsic if it is possible.
9252 // TODO: Support floating point min/max.
9253 bool AllCmpSingleUse = true;
9254 SelectPatternResult SelectPattern;
9255 SelectPattern.Flavor = SPF_UNKNOWN;
9256 if (all_of(VL, [&SelectPattern, &AllCmpSingleUse](Value *I) {
9257 Value *LHS, *RHS;
9258 auto CurrentPattern = matchSelectPattern(I, LHS, RHS);
9259 if (!SelectPatternResult::isMinOrMax(CurrentPattern.Flavor))
9260 return false;
9261 if (SelectPattern.Flavor != SPF_UNKNOWN &&
9262 SelectPattern.Flavor != CurrentPattern.Flavor)
9263 return false;
9264 SelectPattern = CurrentPattern;
9265 AllCmpSingleUse &=
9267 return true;
9268 })) {
9269 switch (SelectPattern.Flavor) {
9270 case SPF_SMIN:
9271 return {Intrinsic::smin, AllCmpSingleUse};
9272 case SPF_UMIN:
9273 return {Intrinsic::umin, AllCmpSingleUse};
9274 case SPF_SMAX:
9275 return {Intrinsic::smax, AllCmpSingleUse};
9276 case SPF_UMAX:
9277 return {Intrinsic::umax, AllCmpSingleUse};
9278 case SPF_FMAXNUM:
9279 return {Intrinsic::maxnum, AllCmpSingleUse};
9280 case SPF_FMINNUM:
9281 return {Intrinsic::minnum, AllCmpSingleUse};
9282 default:
9283 llvm_unreachable("unexpected select pattern flavor");
9284 }
9285 }
9286 return {Intrinsic::not_intrinsic, false};
9287}
9288
9289template <typename InstTy>
9290static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst,
9291 Value *&Init, Value *&OtherOp) {
9292 // Handle the case of a simple two-predecessor recurrence PHI.
9293 // There's a lot more that could theoretically be done here, but
9294 // this is sufficient to catch some interesting cases.
9295 // TODO: Expand list -- gep, uadd.sat etc.
9296 if (PN->getNumIncomingValues() != 2)
9297 return false;
9298
9299 for (unsigned I = 0; I != 2; ++I) {
9300 if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
9301 Operation && Operation->getNumOperands() >= 2) {
9302 Value *LHS = Operation->getOperand(0);
9303 Value *RHS = Operation->getOperand(1);
9304 if (LHS != PN && RHS != PN)
9305 continue;
9306
9307 Inst = Operation;
9308 Init = PN->getIncomingValue(!I);
9309 OtherOp = (LHS == PN) ? RHS : LHS;
9310 return true;
9311 }
9312 }
9313 return false;
9314}
9315
9316template <typename InstTy>
9317static bool matchThreeInputRecurrence(const PHINode *PN, InstTy *&Inst,
9318 Value *&Init, Value *&OtherOp0,
9319 Value *&OtherOp1) {
9320 if (PN->getNumIncomingValues() != 2)
9321 return false;
9322
9323 for (unsigned I = 0; I != 2; ++I) {
9324 if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
9325 Operation && Operation->getNumOperands() >= 3) {
9326 Value *Op0 = Operation->getOperand(0);
9327 Value *Op1 = Operation->getOperand(1);
9328 Value *Op2 = Operation->getOperand(2);
9329
9330 if (Op0 != PN && Op1 != PN && Op2 != PN)
9331 continue;
9332
9333 Inst = Operation;
9334 Init = PN->getIncomingValue(!I);
9335 if (Op0 == PN) {
9336 OtherOp0 = Op1;
9337 OtherOp1 = Op2;
9338 } else if (Op1 == PN) {
9339 OtherOp0 = Op0;
9340 OtherOp1 = Op2;
9341 } else {
9342 OtherOp0 = Op0;
9343 OtherOp1 = Op1;
9344 }
9345 return true;
9346 }
9347 }
9348 return false;
9349}
9351 Value *&Start, Value *&Step) {
9352 // We try to match a recurrence of the form:
9353 // %iv = [Start, %entry], [%iv.next, %backedge]
9354 // %iv.next = binop %iv, Step
9355 // Or:
9356 // %iv = [Start, %entry], [%iv.next, %backedge]
9357 // %iv.next = binop Step, %iv
9358 return matchTwoInputRecurrence(P, BO, Start, Step);
9359}
9360
9362 Value *&Start, Value *&Step) {
9363 BinaryOperator *BO = nullptr;
9364 P = dyn_cast<PHINode>(I->getOperand(0));
9365 if (!P)
9366 P = dyn_cast<PHINode>(I->getOperand(1));
9367 return P && matchSimpleRecurrence(P, BO, Start, Step) && BO == I;
9368}
9369
9371 PHINode *&P, Value *&Init,
9372 Value *&OtherOp) {
9373 // Binary intrinsics only supported for now.
9374 if (I->arg_size() != 2 || I->getType() != I->getArgOperand(0)->getType() ||
9375 I->getType() != I->getArgOperand(1)->getType())
9376 return false;
9377
9378 IntrinsicInst *II = nullptr;
9379 P = dyn_cast<PHINode>(I->getArgOperand(0));
9380 if (!P)
9381 P = dyn_cast<PHINode>(I->getArgOperand(1));
9382
9383 return P && matchTwoInputRecurrence(P, II, Init, OtherOp) && II == I;
9384}
9385
9387 PHINode *&P, Value *&Init,
9388 Value *&OtherOp0,
9389 Value *&OtherOp1) {
9390 if (I->arg_size() != 3 || I->getType() != I->getArgOperand(0)->getType() ||
9391 I->getType() != I->getArgOperand(1)->getType() ||
9392 I->getType() != I->getArgOperand(2)->getType())
9393 return false;
9394 IntrinsicInst *II = nullptr;
9395 P = dyn_cast<PHINode>(I->getArgOperand(0));
9396 if (!P) {
9397 P = dyn_cast<PHINode>(I->getArgOperand(1));
9398 if (!P)
9399 P = dyn_cast<PHINode>(I->getArgOperand(2));
9400 }
9401 return P && matchThreeInputRecurrence(P, II, Init, OtherOp0, OtherOp1) &&
9402 II == I;
9403}
9404
9405/// Return true if "icmp Pred LHS RHS" is always true.
9407 const Value *RHS) {
9408 if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS)
9409 return true;
9410
9411 switch (Pred) {
9412 default:
9413 return false;
9414
9415 case CmpInst::ICMP_SLE: {
9416 const APInt *C;
9417
9418 // LHS s<= LHS +_{nsw} C if C >= 0
9419 // LHS s<= LHS | C if C >= 0
9420 if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C))) ||
9422 return !C->isNegative();
9423
9424 // LHS s<= smax(LHS, V) for any V
9426 return true;
9427
9428 // smin(RHS, V) s<= RHS for any V
9430 return true;
9431
9432 // Match A to (X +_{nsw} CA) and B to (X +_{nsw} CB)
9433 const Value *X;
9434 const APInt *CLHS, *CRHS;
9435 if (match(LHS, m_NSWAddLike(m_Value(X), m_APInt(CLHS))) &&
9437 return CLHS->sle(*CRHS);
9438
9439 return false;
9440 }
9441
9442 case CmpInst::ICMP_ULE: {
9443 // LHS u<= LHS +_{nuw} V for any V
9444 if (match(RHS, m_c_Add(m_Specific(LHS), m_Value())) &&
9446 return true;
9447
9448 // LHS u<= LHS | V for any V
9449 if (match(RHS, m_c_Or(m_Specific(LHS), m_Value())))
9450 return true;
9451
9452 // LHS u<= umax(LHS, V) for any V
9454 return true;
9455
9456 // RHS >> V u<= RHS for any V
9457 if (match(LHS, m_LShr(m_Specific(RHS), m_Value())))
9458 return true;
9459
9460 // RHS u/ C_ugt_1 u<= RHS
9461 const APInt *C;
9462 if (match(LHS, m_UDiv(m_Specific(RHS), m_APInt(C))) && C->ugt(1))
9463 return true;
9464
9465 // RHS & V u<= RHS for any V
9467 return true;
9468
9469 // umin(RHS, V) u<= RHS for any V
9471 return true;
9472
9473 // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
9474 const Value *X;
9475 const APInt *CLHS, *CRHS;
9476 if (match(LHS, m_NUWAddLike(m_Value(X), m_APInt(CLHS))) &&
9478 return CLHS->ule(*CRHS);
9479
9480 return false;
9481 }
9482 }
9483}
9484
9485/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
9486/// ALHS ARHS" is true. Otherwise, return std::nullopt.
9487static std::optional<bool>
9489 const Value *ARHS, const Value *BLHS, const Value *BRHS) {
9490 switch (Pred) {
9491 default:
9492 return std::nullopt;
9493
9494 case CmpInst::ICMP_SLT:
9495 case CmpInst::ICMP_SLE:
9496 if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS) &&
9498 return true;
9499 return std::nullopt;
9500
9501 case CmpInst::ICMP_SGT:
9502 case CmpInst::ICMP_SGE:
9503 if (isTruePredicate(CmpInst::ICMP_SLE, ALHS, BLHS) &&
9505 return true;
9506 return std::nullopt;
9507
9508 case CmpInst::ICMP_ULT:
9509 case CmpInst::ICMP_ULE:
9510 if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS) &&
9512 return true;
9513 return std::nullopt;
9514
9515 case CmpInst::ICMP_UGT:
9516 case CmpInst::ICMP_UGE:
9517 if (isTruePredicate(CmpInst::ICMP_ULE, ALHS, BLHS) &&
9519 return true;
9520 return std::nullopt;
9521 }
9522}
9523
9524/// Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
9525/// Return false if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is false.
9526/// Otherwise, return std::nullopt if we can't infer anything.
9527static std::optional<bool>
9529 CmpPredicate RPred, const ConstantRange &RCR) {
9530 auto CRImpliesPred = [&](ConstantRange CR,
9531 CmpInst::Predicate Pred) -> std::optional<bool> {
9532 // If all true values for lhs and true for rhs, lhs implies rhs
9533 if (CR.icmp(Pred, RCR))
9534 return true;
9535
9536 // If there is no overlap, lhs implies not rhs
9537 if (CR.icmp(CmpInst::getInversePredicate(Pred), RCR))
9538 return false;
9539
9540 return std::nullopt;
9541 };
9542 if (auto Res = CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9543 RPred))
9544 return Res;
9545 if (LPred.hasSameSign() ^ RPred.hasSameSign()) {
9547 : LPred.dropSameSign();
9549 : RPred.dropSameSign();
9550 return CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9551 RPred);
9552 }
9553 return std::nullopt;
9554}
9555
9556/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
9557/// is true. Return false if LHS implies RHS is false. Otherwise, return
9558/// std::nullopt if we can't infer anything.
9559static std::optional<bool>
9560isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1,
9561 CmpPredicate RPred, const Value *R0, const Value *R1,
9562 const DataLayout &DL, bool LHSIsTrue) {
9563 // The rest of the logic assumes the LHS condition is true. If that's not the
9564 // case, invert the predicate to make it so.
9565 if (!LHSIsTrue)
9566 LPred = ICmpInst::getInverseCmpPredicate(LPred);
9567
9568 // We can have non-canonical operands, so try to normalize any common operand
9569 // to L0/R0.
9570 if (L0 == R1) {
9571 std::swap(R0, R1);
9572 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9573 }
9574 if (R0 == L1) {
9575 std::swap(L0, L1);
9576 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9577 }
9578 if (L1 == R1) {
9579 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
9580 if (L0 != R0 || match(L0, m_ImmConstant())) {
9581 std::swap(L0, L1);
9582 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9583 std::swap(R0, R1);
9584 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9585 }
9586 }
9587
9588 // See if we can infer anything if operand-0 matches and we have at least one
9589 // constant.
9590 const APInt *Unused;
9591 if (L0 == R0 && (match(L1, m_APInt(Unused)) || match(R1, m_APInt(Unused)))) {
9592 // Potential TODO: We could also further use the constant range of L0/R0 to
9593 // further constraint the constant ranges. At the moment this leads to
9594 // several regressions related to not transforming `multi_use(A + C0) eq/ne
9595 // C1` (see discussion: D58633).
9597 L1, ICmpInst::isSigned(LPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,
9598 /*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1);
9600 R1, ICmpInst::isSigned(RPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,
9601 /*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1);
9602 // Even if L1/R1 are not both constant, we can still sometimes deduce
9603 // relationship from a single constant. For example X u> Y implies X != 0.
9604 if (auto R = isImpliedCondCommonOperandWithCR(LPred, LCR, RPred, RCR))
9605 return R;
9606 // If both L1/R1 were exact constant ranges and we didn't get anything
9607 // here, we won't be able to deduce this.
9608 if (match(L1, m_APInt(Unused)) && match(R1, m_APInt(Unused)))
9609 return std::nullopt;
9610 }
9611
9612 // Can we infer anything when the two compares have matching operands?
9613 if (L0 == R0 && L1 == R1)
9614 return ICmpInst::isImpliedByMatchingCmp(LPred, RPred);
9615
9616 // It only really makes sense in the context of signed comparison for "X - Y
9617 // must be positive if X >= Y and no overflow".
9618 // Take SGT as an example: L0:x > L1:y and C >= 0
9619 // ==> R0:(x -nsw y) < R1:(-C) is false
9620 CmpInst::Predicate SignedLPred = LPred.getPreferredSignedPredicate();
9621 if ((SignedLPred == ICmpInst::ICMP_SGT ||
9622 SignedLPred == ICmpInst::ICMP_SGE) &&
9623 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9624 if (match(R1, m_NonPositive()) &&
9625 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == false)
9626 return false;
9627 }
9628
9629 // Take SLT as an example: L0:x < L1:y and C <= 0
9630 // ==> R0:(x -nsw y) < R1:(-C) is true
9631 if ((SignedLPred == ICmpInst::ICMP_SLT ||
9632 SignedLPred == ICmpInst::ICMP_SLE) &&
9633 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9634 if (match(R1, m_NonNegative()) &&
9635 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == true)
9636 return true;
9637 }
9638
9639 // a - b == NonZero -> a != b
9640 // ptrtoint(a) - ptrtoint(b) == NonZero -> a != b
9641 const APInt *L1C;
9642 Value *A, *B;
9643 if (LPred == ICmpInst::ICMP_EQ && ICmpInst::isEquality(RPred) &&
9644 match(L1, m_APInt(L1C)) && !L1C->isZero() &&
9645 match(L0, m_Sub(m_Value(A), m_Value(B))) &&
9646 ((A == R0 && B == R1) || (A == R1 && B == R0) ||
9651 return RPred.dropSameSign() == ICmpInst::ICMP_NE;
9652 }
9653
9654 // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1
9655 if (L0 == R0 &&
9656 (LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) &&
9657 (RPred == ICmpInst::ICMP_ULT || RPred == ICmpInst::ICMP_UGE) &&
9658 match(L0, m_c_Add(m_Specific(L1), m_Specific(R1))))
9659 return CmpPredicate::getMatching(LPred, RPred).has_value();
9660
9661 if (auto P = CmpPredicate::getMatching(LPred, RPred))
9662 return isImpliedCondOperands(*P, L0, L1, R0, R1);
9663
9664 return std::nullopt;
9665}
9666
9667/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
9668/// is true. Return false if LHS implies RHS is false. Otherwise, return
9669/// std::nullopt if we can't infer anything.
9670static std::optional<bool>
9672 FCmpInst::Predicate RPred, const Value *R0, const Value *R1,
9673 const DataLayout &DL, bool LHSIsTrue) {
9674 // The rest of the logic assumes the LHS condition is true. If that's not the
9675 // case, invert the predicate to make it so.
9676 if (!LHSIsTrue)
9677 LPred = FCmpInst::getInversePredicate(LPred);
9678
9679 // We can have non-canonical operands, so try to normalize any common operand
9680 // to L0/R0.
9681 if (L0 == R1) {
9682 std::swap(R0, R1);
9683 RPred = FCmpInst::getSwappedPredicate(RPred);
9684 }
9685 if (R0 == L1) {
9686 std::swap(L0, L1);
9687 LPred = FCmpInst::getSwappedPredicate(LPred);
9688 }
9689 if (L1 == R1) {
9690 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
9691 if (L0 != R0 || match(L0, m_ImmConstant())) {
9692 std::swap(L0, L1);
9693 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9694 std::swap(R0, R1);
9695 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9696 }
9697 }
9698
9699 // Can we infer anything when the two compares have matching operands?
9700 if (L0 == R0 && L1 == R1) {
9701 if ((LPred & RPred) == LPred)
9702 return true;
9703 if ((LPred & ~RPred) == LPred)
9704 return false;
9705 }
9706
9707 // See if we can infer anything if operand-0 matches and we have at least one
9708 // constant.
9709 const APFloat *L1C, *R1C;
9710 if (L0 == R0 && match(L1, m_APFloat(L1C)) && match(R1, m_APFloat(R1C))) {
9711 if (std::optional<ConstantFPRange> DomCR =
9713 if (std::optional<ConstantFPRange> ImpliedCR =
9715 if (ImpliedCR->contains(*DomCR))
9716 return true;
9717 }
9718 if (std::optional<ConstantFPRange> ImpliedCR =
9720 FCmpInst::getInversePredicate(RPred), *R1C)) {
9721 if (ImpliedCR->contains(*DomCR))
9722 return false;
9723 }
9724 }
9725 }
9726
9727 return std::nullopt;
9728}
9729
9730/// Return true if LHS implies RHS is true. Return false if LHS implies RHS is
9731/// false. Otherwise, return std::nullopt if we can't infer anything. We
9732/// expect the RHS to be an icmp and the LHS to be an 'and', 'or', or a 'select'
9733/// instruction.
9734static std::optional<bool>
9736 const Value *RHSOp0, const Value *RHSOp1,
9737 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9738 // The LHS must be an 'or', 'and', or a 'select' instruction.
9739 assert((LHS->getOpcode() == Instruction::And ||
9740 LHS->getOpcode() == Instruction::Or ||
9741 LHS->getOpcode() == Instruction::Select) &&
9742 "Expected LHS to be 'and', 'or', or 'select'.");
9743
9744 assert(Depth <= MaxAnalysisRecursionDepth && "Hit recursion limit");
9745
9746 // If the result of an 'or' is false, then we know both legs of the 'or' are
9747 // false. Similarly, if the result of an 'and' is true, then we know both
9748 // legs of the 'and' are true.
9749 const Value *ALHS, *ARHS;
9750 if ((!LHSIsTrue && match(LHS, m_LogicalOr(m_Value(ALHS), m_Value(ARHS)))) ||
9751 (LHSIsTrue && match(LHS, m_LogicalAnd(m_Value(ALHS), m_Value(ARHS))))) {
9752 // FIXME: Make this non-recursion.
9753 if (std::optional<bool> Implication = isImpliedCondition(
9754 ALHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9755 return Implication;
9756 if (std::optional<bool> Implication = isImpliedCondition(
9757 ARHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9758 return Implication;
9759 return std::nullopt;
9760 }
9761 return std::nullopt;
9762}
9763
9764std::optional<bool>
9766 const Value *RHSOp0, const Value *RHSOp1,
9767 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9768 // Bail out when we hit the limit.
9770 return std::nullopt;
9771
9772 // A mismatch occurs when we compare a scalar cmp to a vector cmp, for
9773 // example.
9774 if (RHSOp0->getType()->isVectorTy() != LHS->getType()->isVectorTy())
9775 return std::nullopt;
9776
9777 assert(LHS->getType()->isIntOrIntVectorTy(1) &&
9778 "Expected integer type only!");
9779
9780 // Match not
9781 if (match(LHS, m_Not(m_Value(LHS))))
9782 LHSIsTrue = !LHSIsTrue;
9783
9784 // Both LHS and RHS are icmps.
9785 if (RHSOp0->getType()->getScalarType()->isIntOrPtrTy()) {
9786 if (const auto *LHSCmp = dyn_cast<ICmpInst>(LHS))
9787 return isImpliedCondICmps(LHSCmp->getCmpPredicate(),
9788 LHSCmp->getOperand(0), LHSCmp->getOperand(1),
9789 RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue);
9790 const Value *V;
9791 if (match(LHS, m_NUWTrunc(m_Value(V))))
9793 ConstantInt::get(V->getType(), 0), RHSPred,
9794 RHSOp0, RHSOp1, DL, LHSIsTrue);
9795 } else {
9796 assert(RHSOp0->getType()->isFPOrFPVectorTy() &&
9797 "Expected floating point type only!");
9798 if (const auto *LHSCmp = dyn_cast<FCmpInst>(LHS))
9799 return isImpliedCondFCmps(LHSCmp->getPredicate(), LHSCmp->getOperand(0),
9800 LHSCmp->getOperand(1), RHSPred, RHSOp0, RHSOp1,
9801 DL, LHSIsTrue);
9802 }
9803
9804 /// The LHS should be an 'or', 'and', or a 'select' instruction. We expect
9805 /// the RHS to be an icmp.
9806 /// FIXME: Add support for and/or/select on the RHS.
9807 if (const Instruction *LHSI = dyn_cast<Instruction>(LHS)) {
9808 if ((LHSI->getOpcode() == Instruction::And ||
9809 LHSI->getOpcode() == Instruction::Or ||
9810 LHSI->getOpcode() == Instruction::Select))
9811 return isImpliedCondAndOr(LHSI, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue,
9812 Depth);
9813 }
9814 return std::nullopt;
9815}
9816
9817std::optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS,
9818 const DataLayout &DL,
9819 bool LHSIsTrue, unsigned Depth) {
9820 // LHS ==> RHS by definition
9821 if (LHS == RHS)
9822 return LHSIsTrue;
9823
9824 // Match not
9825 bool InvertRHS = false;
9826 if (match(RHS, m_Not(m_Value(RHS)))) {
9827 if (LHS == RHS)
9828 return !LHSIsTrue;
9829 InvertRHS = true;
9830 }
9831
9832 if (const ICmpInst *RHSCmp = dyn_cast<ICmpInst>(RHS)) {
9833 if (auto Implied = isImpliedCondition(
9834 LHS, RHSCmp->getCmpPredicate(), RHSCmp->getOperand(0),
9835 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))
9836 return InvertRHS ? !*Implied : *Implied;
9837 return std::nullopt;
9838 }
9839 if (const FCmpInst *RHSCmp = dyn_cast<FCmpInst>(RHS)) {
9840 if (auto Implied = isImpliedCondition(
9841 LHS, RHSCmp->getPredicate(), RHSCmp->getOperand(0),
9842 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))
9843 return InvertRHS ? !*Implied : *Implied;
9844 return std::nullopt;
9845 }
9846
9847 const Value *V;
9848 if (match(RHS, m_NUWTrunc(m_Value(V)))) {
9849 if (auto Implied = isImpliedCondition(LHS, CmpInst::ICMP_NE, V,
9850 ConstantInt::get(V->getType(), 0), DL,
9851 LHSIsTrue, Depth))
9852 return InvertRHS ? !*Implied : *Implied;
9853 return std::nullopt;
9854 }
9855
9857 return std::nullopt;
9858
9859 // LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS2
9860 // LHS ==> !(RHS1 && RHS2) if LHS ==> !RHS1 or LHS ==> !RHS2
9861 const Value *RHS1, *RHS2;
9862 if (match(RHS, m_LogicalOr(m_Value(RHS1), m_Value(RHS2)))) {
9863 if (std::optional<bool> Imp =
9864 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9865 if (*Imp == true)
9866 return !InvertRHS;
9867 if (std::optional<bool> Imp =
9868 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9869 if (*Imp == true)
9870 return !InvertRHS;
9871 }
9872 if (match(RHS, m_LogicalAnd(m_Value(RHS1), m_Value(RHS2)))) {
9873 if (std::optional<bool> Imp =
9874 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9875 if (*Imp == false)
9876 return InvertRHS;
9877 if (std::optional<bool> Imp =
9878 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9879 if (*Imp == false)
9880 return InvertRHS;
9881 }
9882
9883 return std::nullopt;
9884}
9885
9886// Returns a pair (Condition, ConditionIsTrue), where Condition is a branch
9887// condition dominating ContextI or nullptr, if no condition is found.
9888static std::pair<Value *, bool>
9890 if (!ContextI || !ContextI->getParent())
9891 return {nullptr, false};
9892
9893 // TODO: This is a poor/cheap way to determine dominance. Should we use a
9894 // dominator tree (eg, from a SimplifyQuery) instead?
9895 const BasicBlock *ContextBB = ContextI->getParent();
9896 const BasicBlock *PredBB = ContextBB->getSinglePredecessor();
9897 if (!PredBB)
9898 return {nullptr, false};
9899
9900 // We need a conditional branch in the predecessor.
9901 Value *PredCond;
9902 BasicBlock *TrueBB, *FalseBB;
9903 if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))
9904 return {nullptr, false};
9905
9906 // The branch should get simplified. Don't bother simplifying this condition.
9907 if (TrueBB == FalseBB)
9908 return {nullptr, false};
9909
9910 assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
9911 "Predecessor block does not point to successor?");
9912
9913 // Is this condition implied by the predecessor condition?
9914 return {PredCond, TrueBB == ContextBB};
9915}
9916
9917std::optional<bool> llvm::isImpliedByDomCondition(const Value *Cond,
9918 const Instruction *ContextI,
9919 const DataLayout &DL) {
9920 assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool");
9921 auto PredCond = getDomPredecessorCondition(ContextI);
9922 if (PredCond.first)
9923 return isImpliedCondition(PredCond.first, Cond, DL, PredCond.second);
9924 return std::nullopt;
9925}
9926
9928 const Value *LHS,
9929 const Value *RHS,
9930 const Instruction *ContextI,
9931 const DataLayout &DL) {
9932 auto PredCond = getDomPredecessorCondition(ContextI);
9933 if (PredCond.first)
9934 return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL,
9935 PredCond.second);
9936 return std::nullopt;
9937}
9938
9940 APInt &Upper, const InstrInfoQuery &IIQ,
9941 bool PreferSignedRange) {
9942 unsigned Width = Lower.getBitWidth();
9943 const APInt *C;
9944 switch (BO.getOpcode()) {
9945 case Instruction::Sub:
9946 if (match(BO.getOperand(0), m_APInt(C))) {
9947 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
9948 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
9949
9950 // If the caller expects a signed compare, then try to use a signed range.
9951 // Otherwise if both no-wraps are set, use the unsigned range because it
9952 // is never larger than the signed range. Example:
9953 // "sub nuw nsw i8 -2, x" is unsigned [0, 254] vs. signed [-128, 126].
9954 // "sub nuw nsw i8 2, x" is unsigned [0, 2] vs. signed [-125, 127].
9955 if (PreferSignedRange && HasNSW && HasNUW)
9956 HasNUW = false;
9957
9958 if (HasNUW) {
9959 // 'sub nuw c, x' produces [0, C].
9960 Upper = *C + 1;
9961 } else if (HasNSW) {
9962 if (C->isNegative()) {
9963 // 'sub nsw -C, x' produces [SINT_MIN, -C - SINT_MIN].
9965 Upper = *C - APInt::getSignedMaxValue(Width);
9966 } else {
9967 // Note that sub 0, INT_MIN is not NSW. It techically is a signed wrap
9968 // 'sub nsw C, x' produces [C - SINT_MAX, SINT_MAX].
9969 Lower = *C - APInt::getSignedMaxValue(Width);
9971 }
9972 }
9973 }
9974 break;
9975 case Instruction::Add:
9976 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
9977 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
9978 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
9979
9980 // If the caller expects a signed compare, then try to use a signed
9981 // range. Otherwise if both no-wraps are set, use the unsigned range
9982 // because it is never larger than the signed range. Example: "add nuw
9983 // nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125].
9984 if (PreferSignedRange && HasNSW && HasNUW)
9985 HasNUW = false;
9986
9987 if (HasNUW) {
9988 // 'add nuw x, C' produces [C, UINT_MAX].
9989 Lower = *C;
9990 } else if (HasNSW) {
9991 if (C->isNegative()) {
9992 // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C].
9994 Upper = APInt::getSignedMaxValue(Width) + *C + 1;
9995 } else {
9996 // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX].
9997 Lower = APInt::getSignedMinValue(Width) + *C;
9998 Upper = APInt::getSignedMaxValue(Width) + 1;
9999 }
10000 }
10001 }
10002 break;
10003
10004 case Instruction::And:
10005 if (match(BO.getOperand(1), m_APInt(C)))
10006 // 'and x, C' produces [0, C].
10007 Upper = *C + 1;
10008 // X & -X is a power of two or zero. So we can cap the value at max power of
10009 // two.
10010 if (match(BO.getOperand(0), m_Neg(m_Specific(BO.getOperand(1)))) ||
10011 match(BO.getOperand(1), m_Neg(m_Specific(BO.getOperand(0)))))
10012 Upper = APInt::getSignedMinValue(Width) + 1;
10013 break;
10014
10015 case Instruction::Or:
10016 if (match(BO.getOperand(1), m_APInt(C)))
10017 // 'or x, C' produces [C, UINT_MAX].
10018 Lower = *C;
10019 break;
10020
10021 case Instruction::AShr:
10022 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10023 // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C].
10025 Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1;
10026 } else if (match(BO.getOperand(0), m_APInt(C))) {
10027 unsigned ShiftAmount = Width - 1;
10028 if (!C->isZero() && IIQ.isExact(&BO))
10029 ShiftAmount = C->countr_zero();
10030 if (C->isNegative()) {
10031 // 'ashr C, x' produces [C, C >> (Width-1)]
10032 Lower = *C;
10033 Upper = C->ashr(ShiftAmount) + 1;
10034 } else {
10035 // 'ashr C, x' produces [C >> (Width-1), C]
10036 Lower = C->ashr(ShiftAmount);
10037 Upper = *C + 1;
10038 }
10039 }
10040 break;
10041
10042 case Instruction::LShr:
10043 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10044 // 'lshr x, C' produces [0, UINT_MAX >> C].
10045 Upper = APInt::getAllOnes(Width).lshr(*C) + 1;
10046 } else if (match(BO.getOperand(0), m_APInt(C))) {
10047 // 'lshr C, x' produces [C >> (Width-1), C].
10048 unsigned ShiftAmount = Width - 1;
10049 if (!C->isZero() && IIQ.isExact(&BO))
10050 ShiftAmount = C->countr_zero();
10051 Lower = C->lshr(ShiftAmount);
10052 Upper = *C + 1;
10053 }
10054 break;
10055
10056 case Instruction::Shl:
10057 if (match(BO.getOperand(0), m_APInt(C))) {
10058 if (IIQ.hasNoUnsignedWrap(&BO)) {
10059 // 'shl nuw C, x' produces [C, C << CLZ(C)]
10060 Lower = *C;
10061 Upper = Lower.shl(Lower.countl_zero()) + 1;
10062 } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw?
10063 if (C->isNegative()) {
10064 // 'shl nsw C, x' produces [C << CLO(C)-1, C]
10065 unsigned ShiftAmount = C->countl_one() - 1;
10066 Lower = C->shl(ShiftAmount);
10067 Upper = *C + 1;
10068 } else {
10069 // 'shl nsw C, x' produces [C, C << CLZ(C)-1]
10070 unsigned ShiftAmount = C->countl_zero() - 1;
10071 Lower = *C;
10072 Upper = C->shl(ShiftAmount) + 1;
10073 }
10074 } else {
10075 // If lowbit is set, value can never be zero.
10076 if ((*C)[0])
10077 Lower = APInt::getOneBitSet(Width, 0);
10078 // If we are shifting a constant the largest it can be is if the longest
10079 // sequence of consecutive ones is shifted to the highbits (breaking
10080 // ties for which sequence is higher). At the moment we take a liberal
10081 // upper bound on this by just popcounting the constant.
10082 // TODO: There may be a bitwise trick for it longest/highest
10083 // consecutative sequence of ones (naive method is O(Width) loop).
10084 Upper = APInt::getHighBitsSet(Width, C->popcount()) + 1;
10085 }
10086 } else if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
10087 Upper = APInt::getBitsSetFrom(Width, C->getZExtValue()) + 1;
10088 }
10089 break;
10090
10091 case Instruction::SDiv:
10092 if (match(BO.getOperand(1), m_APInt(C))) {
10093 APInt IntMin = APInt::getSignedMinValue(Width);
10094 APInt IntMax = APInt::getSignedMaxValue(Width);
10095 if (C->isAllOnes()) {
10096 // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX]
10097 // where C != -1 and C != 0 and C != 1
10098 Lower = IntMin + 1;
10099 Upper = IntMax + 1;
10100 } else if (C->countl_zero() < Width - 1) {
10101 // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C]
10102 // where C != -1 and C != 0 and C != 1
10103 Lower = IntMin.sdiv(*C);
10104 Upper = IntMax.sdiv(*C);
10105 if (Lower.sgt(Upper))
10107 Upper = Upper + 1;
10108 assert(Upper != Lower && "Upper part of range has wrapped!");
10109 }
10110 } else if (match(BO.getOperand(0), m_APInt(C))) {
10111 if (C->isMinSignedValue()) {
10112 // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2].
10113 Lower = *C;
10114 Upper = Lower.lshr(1) + 1;
10115 } else {
10116 // 'sdiv C, x' produces [-|C|, |C|].
10117 Upper = C->abs() + 1;
10118 Lower = (-Upper) + 1;
10119 }
10120 }
10121 break;
10122
10123 case Instruction::UDiv:
10124 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
10125 // 'udiv x, C' produces [0, UINT_MAX / C].
10126 Upper = APInt::getMaxValue(Width).udiv(*C) + 1;
10127 } else if (match(BO.getOperand(0), m_APInt(C))) {
10128 // 'udiv C, x' produces [0, C].
10129 Upper = *C + 1;
10130 }
10131 break;
10132
10133 case Instruction::SRem:
10134 if (match(BO.getOperand(1), m_APInt(C))) {
10135 // 'srem x, C' produces (-|C|, |C|).
10136 Upper = C->abs();
10137 Lower = (-Upper) + 1;
10138 } else if (match(BO.getOperand(0), m_APInt(C))) {
10139 if (C->isNegative()) {
10140 // 'srem -|C|, x' produces [-|C|, 0].
10141 Upper = 1;
10142 Lower = *C;
10143 } else {
10144 // 'srem |C|, x' produces [0, |C|].
10145 Upper = *C + 1;
10146 }
10147 }
10148 break;
10149
10150 case Instruction::URem:
10151 if (match(BO.getOperand(1), m_APInt(C)))
10152 // 'urem x, C' produces [0, C).
10153 Upper = *C;
10154 else if (match(BO.getOperand(0), m_APInt(C)))
10155 // 'urem C, x' produces [0, C].
10156 Upper = *C + 1;
10157 break;
10158
10159 default:
10160 break;
10161 }
10162}
10163
10165 bool UseInstrInfo) {
10166 unsigned Width = II.getType()->getScalarSizeInBits();
10167 const APInt *C;
10168 switch (II.getIntrinsicID()) {
10169 case Intrinsic::ctlz:
10170 case Intrinsic::cttz: {
10171 APInt Upper(Width, Width);
10172 if (!UseInstrInfo || !match(II.getArgOperand(1), m_One()))
10173 Upper += 1;
10174 // Maximum of set/clear bits is the bit width.
10176 }
10177 case Intrinsic::ctpop:
10178 // Maximum of set/clear bits is the bit width.
10180 APInt(Width, Width) + 1);
10181 case Intrinsic::uadd_sat:
10182 // uadd.sat(x, C) produces [C, UINT_MAX].
10183 if (match(II.getOperand(0), m_APInt(C)) ||
10184 match(II.getOperand(1), m_APInt(C)))
10186 break;
10187 case Intrinsic::sadd_sat:
10188 if (match(II.getOperand(0), m_APInt(C)) ||
10189 match(II.getOperand(1), m_APInt(C))) {
10190 if (C->isNegative())
10191 // sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)].
10193 APInt::getSignedMaxValue(Width) + *C +
10194 1);
10195
10196 // sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].
10198 APInt::getSignedMaxValue(Width) + 1);
10199 }
10200 break;
10201 case Intrinsic::usub_sat:
10202 // usub.sat(C, x) produces [0, C].
10203 if (match(II.getOperand(0), m_APInt(C)))
10204 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
10205
10206 // usub.sat(x, C) produces [0, UINT_MAX - C].
10207 if (match(II.getOperand(1), m_APInt(C)))
10209 APInt::getMaxValue(Width) - *C + 1);
10210 break;
10211 case Intrinsic::ssub_sat:
10212 if (match(II.getOperand(0), m_APInt(C))) {
10213 if (C->isNegative())
10214 // ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)].
10216 *C - APInt::getSignedMinValue(Width) +
10217 1);
10218
10219 // ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].
10221 APInt::getSignedMaxValue(Width) + 1);
10222 } else if (match(II.getOperand(1), m_APInt(C))) {
10223 if (C->isNegative())
10224 // ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]:
10226 APInt::getSignedMaxValue(Width) + 1);
10227
10228 // ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].
10230 APInt::getSignedMaxValue(Width) - *C +
10231 1);
10232 }
10233 break;
10234 case Intrinsic::umin:
10235 case Intrinsic::umax:
10236 case Intrinsic::smin:
10237 case Intrinsic::smax:
10238 if (!match(II.getOperand(0), m_APInt(C)) &&
10239 !match(II.getOperand(1), m_APInt(C)))
10240 break;
10241
10242 switch (II.getIntrinsicID()) {
10243 case Intrinsic::umin:
10244 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
10245 case Intrinsic::umax:
10247 case Intrinsic::smin:
10249 *C + 1);
10250 case Intrinsic::smax:
10252 APInt::getSignedMaxValue(Width) + 1);
10253 default:
10254 llvm_unreachable("Must be min/max intrinsic");
10255 }
10256 break;
10257 case Intrinsic::abs:
10258 // If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX],
10259 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10260 if (match(II.getOperand(1), m_One()))
10262 APInt::getSignedMaxValue(Width) + 1);
10263
10265 APInt::getSignedMinValue(Width) + 1);
10266 case Intrinsic::vscale:
10267 if (!II.getParent() || !II.getFunction())
10268 break;
10269 return getVScaleRange(II.getFunction(), Width);
10270 default:
10271 break;
10272 }
10273
10274 return ConstantRange::getFull(Width);
10275}
10276
10278 const InstrInfoQuery &IIQ) {
10279 unsigned BitWidth = SI.getType()->getScalarSizeInBits();
10280 const Value *LHS = nullptr, *RHS = nullptr;
10282 if (R.Flavor == SPF_UNKNOWN)
10283 return ConstantRange::getFull(BitWidth);
10284
10285 if (R.Flavor == SelectPatternFlavor::SPF_ABS) {
10286 // If the negation part of the abs (in RHS) has the NSW flag,
10287 // then the result of abs(X) is [0..SIGNED_MAX],
10288 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10289 if (match(RHS, m_Neg(m_Specific(LHS))) &&
10293
10296 }
10297
10298 if (R.Flavor == SelectPatternFlavor::SPF_NABS) {
10299 // The result of -abs(X) is <= 0.
10301 APInt(BitWidth, 1));
10302 }
10303
10304 const APInt *C;
10305 if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C)))
10306 return ConstantRange::getFull(BitWidth);
10307
10308 switch (R.Flavor) {
10309 case SPF_UMIN:
10311 case SPF_UMAX:
10313 case SPF_SMIN:
10315 *C + 1);
10316 case SPF_SMAX:
10319 default:
10320 return ConstantRange::getFull(BitWidth);
10321 }
10322}
10323
10325 // The maximum representable value of a half is 65504. For floats the maximum
10326 // value is 3.4e38 which requires roughly 129 bits.
10327 unsigned BitWidth = I->getType()->getScalarSizeInBits();
10328 if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy())
10329 return;
10330 if (isa<FPToSIInst>(I) && BitWidth >= 17) {
10331 Lower = APInt(BitWidth, -65504, true);
10332 Upper = APInt(BitWidth, 65505);
10333 }
10334
10335 if (isa<FPToUIInst>(I) && BitWidth >= 16) {
10336 // For a fptoui the lower limit is left as 0.
10337 Upper = APInt(BitWidth, 65505);
10338 }
10339}
10340
10342 bool UseInstrInfo, AssumptionCache *AC,
10343 const Instruction *CtxI,
10344 const DominatorTree *DT,
10345 unsigned Depth) {
10346 assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction");
10347
10349 return ConstantRange::getFull(V->getType()->getScalarSizeInBits());
10350
10351 if (auto *C = dyn_cast<Constant>(V))
10352 return C->toConstantRange();
10353
10354 unsigned BitWidth = V->getType()->getScalarSizeInBits();
10355 InstrInfoQuery IIQ(UseInstrInfo);
10356 ConstantRange CR = ConstantRange::getFull(BitWidth);
10357 if (auto *BO = dyn_cast<BinaryOperator>(V)) {
10358 APInt Lower = APInt(BitWidth, 0);
10359 APInt Upper = APInt(BitWidth, 0);
10360 // TODO: Return ConstantRange.
10361 setLimitsForBinOp(*BO, Lower, Upper, IIQ, ForSigned);
10363 } else if (auto *II = dyn_cast<IntrinsicInst>(V))
10364 CR = getRangeForIntrinsic(*II, UseInstrInfo);
10365 else if (auto *SI = dyn_cast<SelectInst>(V)) {
10367 SI->getTrueValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
10369 SI->getFalseValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
10370 CR = CRTrue.unionWith(CRFalse);
10372 } else if (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {
10373 APInt Lower = APInt(BitWidth, 0);
10374 APInt Upper = APInt(BitWidth, 0);
10375 // TODO: Return ConstantRange.
10378 } else if (const auto *A = dyn_cast<Argument>(V))
10379 if (std::optional<ConstantRange> Range = A->getRange())
10380 CR = *Range;
10381
10382 if (auto *I = dyn_cast<Instruction>(V)) {
10383 if (auto *Range = IIQ.getMetadata(I, LLVMContext::MD_range))
10385
10386 if (const auto *CB = dyn_cast<CallBase>(V))
10387 if (std::optional<ConstantRange> Range = CB->getRange())
10388 CR = CR.intersectWith(*Range);
10389 }
10390
10391 if (CtxI && AC) {
10392 // Try to restrict the range based on information from assumptions.
10393 for (auto &AssumeVH : AC->assumptionsFor(V)) {
10394 if (!AssumeVH)
10395 continue;
10396 CallInst *I = cast<CallInst>(AssumeVH);
10397 assert(I->getParent()->getParent() == CtxI->getParent()->getParent() &&
10398 "Got assumption for the wrong function!");
10399 assert(I->getIntrinsicID() == Intrinsic::assume &&
10400 "must be an assume intrinsic");
10401
10402 if (!isValidAssumeForContext(I, CtxI, DT))
10403 continue;
10404 Value *Arg = I->getArgOperand(0);
10405 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
10406 // Currently we just use information from comparisons.
10407 if (!Cmp || Cmp->getOperand(0) != V)
10408 continue;
10409 // TODO: Set "ForSigned" parameter via Cmp->isSigned()?
10410 ConstantRange RHS =
10411 computeConstantRange(Cmp->getOperand(1), /* ForSigned */ false,
10412 UseInstrInfo, AC, I, DT, Depth + 1);
10413 CR = CR.intersectWith(
10414 ConstantRange::makeAllowedICmpRegion(Cmp->getPredicate(), RHS));
10415 }
10416 }
10417
10418 return CR;
10419}
10420
10421static void
10423 function_ref<void(Value *)> InsertAffected) {
10424 assert(V != nullptr);
10425 if (isa<Argument>(V) || isa<GlobalValue>(V)) {
10426 InsertAffected(V);
10427 } else if (auto *I = dyn_cast<Instruction>(V)) {
10428 InsertAffected(V);
10429
10430 // Peek through unary operators to find the source of the condition.
10431 Value *Op;
10433 m_Trunc(m_Value(Op))))) {
10435 InsertAffected(Op);
10436 }
10437 }
10438}
10439
10441 Value *Cond, bool IsAssume, function_ref<void(Value *)> InsertAffected) {
10442 auto AddAffected = [&InsertAffected](Value *V) {
10443 addValueAffectedByCondition(V, InsertAffected);
10444 };
10445
10446 auto AddCmpOperands = [&AddAffected, IsAssume](Value *LHS, Value *RHS) {
10447 if (IsAssume) {
10448 AddAffected(LHS);
10449 AddAffected(RHS);
10450 } else if (match(RHS, m_Constant()))
10451 AddAffected(LHS);
10452 };
10453
10454 SmallVector<Value *, 8> Worklist;
10456 Worklist.push_back(Cond);
10457 while (!Worklist.empty()) {
10458 Value *V = Worklist.pop_back_val();
10459 if (!Visited.insert(V).second)
10460 continue;
10461
10462 CmpPredicate Pred;
10463 Value *A, *B, *X;
10464
10465 if (IsAssume) {
10466 AddAffected(V);
10467 if (match(V, m_Not(m_Value(X))))
10468 AddAffected(X);
10469 }
10470
10471 if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
10472 // assume(A && B) is split to -> assume(A); assume(B);
10473 // assume(!(A || B)) is split to -> assume(!A); assume(!B);
10474 // Finally, assume(A || B) / assume(!(A && B)) generally don't provide
10475 // enough information to be worth handling (intersection of information as
10476 // opposed to union).
10477 if (!IsAssume) {
10478 Worklist.push_back(A);
10479 Worklist.push_back(B);
10480 }
10481 } else if (match(V, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
10482 bool HasRHSC = match(B, m_ConstantInt());
10483 if (ICmpInst::isEquality(Pred)) {
10484 AddAffected(A);
10485 if (IsAssume)
10486 AddAffected(B);
10487 if (HasRHSC) {
10488 Value *Y;
10489 // (X << C) or (X >>_s C) or (X >>_u C).
10490 if (match(A, m_Shift(m_Value(X), m_ConstantInt())))
10491 AddAffected(X);
10492 // (X & C) or (X | C).
10493 else if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10494 match(A, m_Or(m_Value(X), m_Value(Y)))) {
10495 AddAffected(X);
10496 AddAffected(Y);
10497 }
10498 // X - Y
10499 else if (match(A, m_Sub(m_Value(X), m_Value(Y)))) {
10500 AddAffected(X);
10501 AddAffected(Y);
10502 }
10503 }
10504 } else {
10505 AddCmpOperands(A, B);
10506 if (HasRHSC) {
10507 // Handle (A + C1) u< C2, which is the canonical form of
10508 // A > C3 && A < C4.
10510 AddAffected(X);
10511
10512 if (ICmpInst::isUnsigned(Pred)) {
10513 Value *Y;
10514 // X & Y u> C -> X >u C && Y >u C
10515 // X | Y u< C -> X u< C && Y u< C
10516 // X nuw+ Y u< C -> X u< C && Y u< C
10517 if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10518 match(A, m_Or(m_Value(X), m_Value(Y))) ||
10519 match(A, m_NUWAdd(m_Value(X), m_Value(Y)))) {
10520 AddAffected(X);
10521 AddAffected(Y);
10522 }
10523 // X nuw- Y u> C -> X u> C
10524 if (match(A, m_NUWSub(m_Value(X), m_Value())))
10525 AddAffected(X);
10526 }
10527 }
10528
10529 // Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported
10530 // by computeKnownFPClass().
10532 if (Pred == ICmpInst::ICMP_SLT && match(B, m_Zero()))
10533 InsertAffected(X);
10534 else if (Pred == ICmpInst::ICMP_SGT && match(B, m_AllOnes()))
10535 InsertAffected(X);
10536 }
10537 }
10538
10539 if (HasRHSC && match(A, m_Intrinsic<Intrinsic::ctpop>(m_Value(X))))
10540 AddAffected(X);
10541 } else if (match(V, m_FCmp(Pred, m_Value(A), m_Value(B)))) {
10542 AddCmpOperands(A, B);
10543
10544 // fcmp fneg(x), y
10545 // fcmp fabs(x), y
10546 // fcmp fneg(fabs(x)), y
10547 if (match(A, m_FNeg(m_Value(A))))
10548 AddAffected(A);
10549 if (match(A, m_FAbs(m_Value(A))))
10550 AddAffected(A);
10551
10553 m_Value()))) {
10554 // Handle patterns that computeKnownFPClass() support.
10555 AddAffected(A);
10556 } else if (!IsAssume && match(V, m_Trunc(m_Value(X)))) {
10557 // Assume is checked here as X is already added above for assumes in
10558 // addValueAffectedByCondition
10559 AddAffected(X);
10560 } else if (!IsAssume && match(V, m_Not(m_Value(X)))) {
10561 // Assume is checked here to avoid issues with ephemeral values
10562 Worklist.push_back(X);
10563 }
10564 }
10565}
10566
10568 // (X >> C) or/add (X & mask(C) != 0)
10569 if (const auto *BO = dyn_cast<BinaryOperator>(V)) {
10570 if (BO->getOpcode() == Instruction::Add ||
10571 BO->getOpcode() == Instruction::Or) {
10572 const Value *X;
10573 const APInt *C1, *C2;
10574 if (match(BO, m_c_BinOp(m_LShr(m_Value(X), m_APInt(C1)),
10578 m_Zero())))) &&
10579 C2->popcount() == C1->getZExtValue())
10580 return X;
10581 }
10582 }
10583 return nullptr;
10584}
10585
10587 return const_cast<Value *>(stripNullTest(const_cast<const Value *>(V)));
10588}
10589
10592 unsigned MaxCount, bool AllowUndefOrPoison) {
10595 auto Push = [&](const Value *V) -> bool {
10596 Constant *C;
10597 if (match(const_cast<Value *>(V), m_ImmConstant(C))) {
10598 if (!AllowUndefOrPoison && !isGuaranteedNotToBeUndefOrPoison(C))
10599 return false;
10600 // Check existence first to avoid unnecessary allocations.
10601 if (Constants.contains(C))
10602 return true;
10603 if (Constants.size() == MaxCount)
10604 return false;
10605 Constants.insert(C);
10606 return true;
10607 }
10608
10609 if (auto *Inst = dyn_cast<Instruction>(V)) {
10610 if (Visited.insert(Inst).second)
10611 Worklist.push_back(Inst);
10612 return true;
10613 }
10614 return false;
10615 };
10616 if (!Push(V))
10617 return false;
10618 while (!Worklist.empty()) {
10619 const Instruction *CurInst = Worklist.pop_back_val();
10620 switch (CurInst->getOpcode()) {
10621 case Instruction::Select:
10622 if (!Push(CurInst->getOperand(1)))
10623 return false;
10624 if (!Push(CurInst->getOperand(2)))
10625 return false;
10626 break;
10627 case Instruction::PHI:
10628 for (Value *IncomingValue : cast<PHINode>(CurInst)->incoming_values()) {
10629 // Fast path for recurrence PHI.
10630 if (IncomingValue == CurInst)
10631 continue;
10632 if (!Push(IncomingValue))
10633 return false;
10634 }
10635 break;
10636 default:
10637 return false;
10638 }
10639 }
10640 return true;
10641}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Register Bank Select
Rewrite undef for PHI
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis Results
This file contains the simple types necessary to represent the attributes associated with functions a...
static const Function * getParent(const Value *V)
#define X(NUM, ENUM, NAME)
Definition ELF.h:851
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Utilities for dealing with flags related to floating point properties and mode controls.
static Value * getCondition(Instruction *I)
Hexagon Common GEP
Module.h This file contains the declarations for the Module class.
static bool hasNoUnsignedWrap(BinaryOperator &I)
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
This file contains the declarations for metadata subclasses.
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
#define P(N)
PowerPC Reduce CR logical Operation
R600 Clause Merge
const SmallVectorImpl< MachineOperand > & Cond
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
std::pair< BasicBlock *, BasicBlock * > Edge
This file contains some templates that are useful if you are working with the STL at all.
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
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 isSignedMinMaxClamp(const Value *Select, const Value *&In, const APInt *&CLow, const APInt *&CHigh)
static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, bool NSW, bool NUW, unsigned Depth)
static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V, unsigned Depth)
static bool isNonEqualSelect(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst, Value *&Init, Value *&OtherOp)
static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static void computeKnownBitsFromCmp(const Value *V, CmpInst::Predicate Pred, Value *LHS, Value *RHS, KnownBits &Known, const SimplifyQuery &Q)
static SelectPatternResult matchMinMaxOfMinMax(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TVal, Value *FVal, unsigned Depth)
Recognize variations of: a < c ?
static void unionWithMinMaxIntrinsicClamp(const IntrinsicInst *II, KnownBits &Known)
static void setLimitForFPToI(const Instruction *I, APInt &Lower, APInt &Upper)
static bool isSameUnderlyingObjectInLoop(const PHINode *PN, const LoopInfo *LI)
PN defines a loop-variant pointer to an object.
static bool isNonEqualPointersWithRecursiveGEP(const Value *A, const Value *B, const SimplifyQuery &Q)
static bool isSignedMinMaxIntrinsicClamp(const IntrinsicInst *II, const APInt *&CLow, const APInt *&CHigh)
static Value * lookThroughCastConst(CmpInst *CmpI, Type *SrcTy, Constant *C, Instruction::CastOps *CastOp)
static bool handleGuaranteedWellDefinedOps(const Instruction *I, const CallableT &Handle)
Enumerates all operands of I that are guaranteed to not be undef or poison.
static bool isAbsoluteValueULEOne(const Value *V)
static void computeKnownBitsFromLerpPattern(const Value *Op0, const Value *Op1, const APInt &DemandedElts, KnownBits &KnownOut, const SimplifyQuery &Q, unsigned Depth)
Try to detect the lerp pattern: a * (b - c) + c * d where a >= 0, b >= 0, c >= 0, d >= 0,...
static KnownFPClass computeKnownFPClassFromContext(const Value *V, const SimplifyQuery &Q)
static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1, bool NSW, bool NUW, const APInt &DemandedElts, KnownBits &KnownOut, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth)
static Value * getNotValue(Value *V)
If the input value is the result of a 'not' op, constant integer, or vector splat of a constant integ...
static constexpr KnownFPClass::MinMaxKind getMinMaxKind(Intrinsic::ID IID)
static unsigned ComputeNumSignBitsImpl(const Value *V, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return the number of times the sign bit of the register is replicated into the other bits.
static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp, KnownBits &Known, const SimplifyQuery &SQ, bool Invert)
static bool isKnownNonZeroFromOperator(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static bool matchOpWithOpEqZero(Value *Op0, Value *Op1)
static bool isNonZeroRecurrence(const PHINode *PN)
Try to detect a recurrence that monotonically increases/decreases from a non-zero starting value.
static SelectPatternResult matchClamp(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal)
Recognize variations of: CLAMP(v,l,h) ==> ((v) < (l) ?
static bool shiftAmountKnownInRange(const Value *ShiftAmount)
Shifts return poison if shiftwidth is larger than the bitwidth.
static bool isEphemeralValueOf(const Instruction *I, const Value *E)
static SelectPatternResult matchMinMax(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, unsigned Depth)
Match non-obvious integer minimum and maximum sequences.
static KnownBits computeKnownBitsForHorizontalOperation(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth, const function_ref< KnownBits(const KnownBits &, const KnownBits &)> KnownBitsFunc)
static bool handleGuaranteedNonPoisonOps(const Instruction *I, const CallableT &Handle)
Enumerates all operands of I that are guaranteed to not be poison.
static std::optional< std::pair< Value *, Value * > > getInvertibleOperands(const Operator *Op1, const Operator *Op2)
If the pair of operators are the same invertible function, return the the operands of the function co...
static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS)
static void computeKnownBitsFromCond(const Value *V, Value *Cond, KnownBits &Known, const SimplifyQuery &SQ, bool Invert, unsigned Depth)
static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q)
static std::optional< bool > isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS, const Value *ARHS, const Value *BLHS, const Value *BRHS)
Return true if "icmp Pred BLHS BRHS" is true whenever "icmp PredALHS ARHS" is true.
static const Instruction * safeCxtI(const Value *V, const Instruction *CxtI)
static bool isNonEqualMul(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and the multiplication is nuw o...
static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero, const Value *Cond, bool CondIsTrue)
Return true if we can infer that V is known to be a power of 2 from dominating condition Cond (e....
static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW, bool NUW, const APInt &DemandedElts, KnownBits &Known, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth)
static bool matchThreeInputRecurrence(const PHINode *PN, InstTy *&Inst, Value *&Init, Value *&OtherOp0, Value *&OtherOp1)
static bool isKnownNonNaN(const Value *V, FastMathFlags FMF)
static ConstantRange getRangeForIntrinsic(const IntrinsicInst &II, bool UseInstrInfo)
static void computeKnownFPClassForFPTrunc(const Operator *Op, const APInt &DemandedElts, FPClassTest InterestedClasses, KnownFPClass &Known, const SimplifyQuery &Q, unsigned Depth)
static Value * BuildSubAggregate(Value *From, Value *To, Type *IndexedType, SmallVectorImpl< unsigned > &Idxs, unsigned IdxSkip, BasicBlock::iterator InsertBefore)
Value * RHS
Value * LHS
bool isFinite() const
Definition APFloat.h:1521
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1193
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1153
bool isInteger() const
Definition APFloat.h:1533
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:2011
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1604
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:235
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition APInt.h:1421
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition APInt.h:424
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1555
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition APInt.h:1406
unsigned popcount() const
Count the number of bits set.
Definition APInt.h:1685
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1400
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition APInt.h:207
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition APInt.h:1345
unsigned ceilLogBase2() const
Definition APInt.h:1779
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition APInt.h:1208
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition APInt.h:372
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1189
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:381
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition APInt.cpp:1697
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1503
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1118
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:210
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition APInt.h:217
bool isNegative() const
Determine sign of this APInt.
Definition APInt.h:330
bool intersects(const APInt &RHS) const
This operation tests if there are any pairs of corresponding bits between this APInt and RHS that are...
Definition APInt.h:1256
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1675
void clearAllBits()
Set every bit to 0.
Definition APInt.h:1411
LLVM_ABI APInt reverseBits() const
Definition APInt.cpp:778
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition APInt.h:1173
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1643
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition APInt.h:1613
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:220
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1072
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition APInt.h:357
unsigned logBase2() const
Definition APInt.h:1776
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:834
bool getBoolValue() const
Convert APInt to a boolean value.
Definition APInt.h:472
bool isMaxSignedValue() const
Determine if this is the largest signed value.
Definition APInt.h:406
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:335
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition APInt.h:1157
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:880
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition APInt.h:1264
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition APInt.h:1137
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition APInt.h:297
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:201
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition APInt.h:1403
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1244
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:287
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:240
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:858
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1228
void clearSignBit()
Set the sign bit to 0.
Definition APInt.h:1464
an instruction to allocate memory on the stack
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator end() const
Definition ArrayRef.h:131
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
iterator begin() const
Definition ArrayRef.h:130
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Definition ArrayRef.h:186
Class to represent array types.
This represents the llvm.assume intrinsic.
A cache of @llvm.assume calls within a function.
MutableArrayRef< ResultElem > assumptionsFor(const Value *V)
Access the list of assumptions which affect this value.
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:105
LLVM_ABI std::optional< unsigned > getVScaleRangeMax() const
Returns the maximum value for the vscale_range attribute or std::nullopt when unknown.
LLVM_ABI unsigned getVScaleRangeMin() const
Returns the minimum value for the vscale_range attribute.
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition Attributes.h:261
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator end()
Definition BasicBlock.h:474
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:461
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
InstListType::const_iterator const_iterator
Definition BasicBlock.h:171
LLVM_ABI const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
LLVM_ABI const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Definition BasicBlock.h:237
LLVM_ABI Instruction::BinaryOps getBinaryOp() const
Returns the binary operation underlying the intrinsic.
BinaryOps getOpcode() const
Definition InstrTypes.h:374
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
LLVM_ABI bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
LLVM_ABI bool isIndirectCall() const
Return true if the callsite is an indirect call.
bool onlyReadsMemory(unsigned OpNo) const
Value * getCalledOperand() const
Value * getArgOperand(unsigned i) const
LLVM_ABI Intrinsic::ID getIntrinsicID() const
Returns the intrinsic ID of the intrinsic called or Intrinsic::not_intrinsic if the called function i...
unsigned arg_size() const
This class represents a function call, abstracting a target machine's calling convention.
This is the base class for all instructions that perform data casts.
Definition InstrTypes.h:448
This class is the base class for the comparison instructions.
Definition InstrTypes.h:664
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
@ ICMP_SLT
signed less than
Definition InstrTypes.h:705
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:706
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:682
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition InstrTypes.h:691
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:680
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:681
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:700
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:699
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:703
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition InstrTypes.h:690
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:701
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition InstrTypes.h:688
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:683
@ ICMP_NE
not equal
Definition InstrTypes.h:698
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:704
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:702
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition InstrTypes.h:689
bool isSigned() const
Definition InstrTypes.h:930
static LLVM_ABI bool isEquality(Predicate pred)
Determine if this is an equals/not equals predicate.
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition InstrTypes.h:827
bool isTrueWhenEqual() const
This is just a convenience.
Definition InstrTypes.h:942
static bool isFPPredicate(Predicate P)
Definition InstrTypes.h:770
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition InstrTypes.h:789
Predicate getPredicate() const
Return the predicate for this instruction.
Definition InstrTypes.h:765
Predicate getFlippedStrictnessPredicate() const
For predicate of kind "is X or equal to 0" returns the predicate "is X".
Definition InstrTypes.h:893
static bool isIntPredicate(Predicate P)
Definition InstrTypes.h:776
static LLVM_ABI bool isOrdered(Predicate predicate)
Determine if the predicate is an ordered operation.
bool isUnsigned() const
Definition InstrTypes.h:936
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
static LLVM_ABI std::optional< CmpPredicate > getMatching(CmpPredicate A, CmpPredicate B)
Compares two CmpPredicates taking samesign into account and returns the canonicalized CmpPredicate if...
LLVM_ABI CmpInst::Predicate getPreferredSignedPredicate() const
Attempts to return a signed CmpInst::Predicate from the CmpPredicate.
CmpInst::Predicate dropSameSign() const
Drops samesign information.
bool hasSameSign() const
Query samesign information, for optimizations.
Conditional Branch instruction.
An array constant whose element type is a simple 1/2/4/8-byte integer, bytes or float/double,...
Definition Constants.h:846
ConstantDataSequential - A vector or array constant whose element type is a simple 1/2/4/8-byte integ...
Definition Constants.h:736
StringRef getAsString() const
If this array is isString(), then this method returns the array as a StringRef.
Definition Constants.h:812
A vector constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition Constants.h:932
static LLVM_ABI Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static LLVM_ABI Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static LLVM_ABI std::optional< ConstantFPRange > makeExactFCmpRegion(FCmpInst::Predicate Pred, const APFloat &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:420
This is the shared class of boolean and integer constants.
Definition Constants.h:87
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition Constants.h:168
This class represents a range of values.
PreferredRangeType
If represented precisely, the result of some range operations may consist of multiple disjoint ranges...
static LLVM_ABI ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
LLVM_ABI OverflowResult unsignedSubMayOverflow(const ConstantRange &Other) const
Return whether unsigned sub of the two ranges always/never overflows.
LLVM_ABI bool isAllNegative() const
Return true if all values in this range are negative.
LLVM_ABI OverflowResult unsignedAddMayOverflow(const ConstantRange &Other) const
Return whether unsigned add of the two ranges always/never overflows.
LLVM_ABI KnownBits toKnownBits() const
Return known bits for values in this range.
LLVM_ABI bool icmp(CmpInst::Predicate Pred, const ConstantRange &Other) const
Does the predicate Pred hold between ranges this and Other?
LLVM_ABI OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const
Return whether unsigned mul of the two ranges always/never overflows.
LLVM_ABI bool isAllNonNegative() const
Return true if all values in this range are non-negative.
static LLVM_ABI ConstantRange makeAllowedICmpRegion(CmpInst::Predicate Pred, const ConstantRange &Other)
Produce the smallest range such that all values that may satisfy the given predicate with any value c...
LLVM_ABI ConstantRange unionWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the union of this range with another range.
static LLVM_ABI ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred, const APInt &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
LLVM_ABI bool contains(const APInt &Val) const
Return true if the specified value is in the set.
LLVM_ABI OverflowResult signedAddMayOverflow(const ConstantRange &Other) const
Return whether signed add of the two ranges always/never overflows.
LLVM_ABI ConstantRange intersectWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the intersection of this range with another range.
OverflowResult
Represents whether an operation on the given constant range is known to always or never overflow.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
static ConstantRange getNonEmpty(APInt Lower, APInt Upper)
Create non-empty constant range with the given bounds.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
LLVM_ABI OverflowResult signedSubMayOverflow(const ConstantRange &Other) const
Return whether signed sub of the two ranges always/never overflows.
LLVM_ABI ConstantRange sub(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a subtraction of a value in this r...
This is an important base class in LLVM.
Definition Constant.h:43
static LLVM_ABI Constant * replaceUndefsWith(Constant *C, Constant *Replacement)
Try to replace undefined constant C or undefined elements in C with Replacement.
LLVM_ABI Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
LLVM_ABI Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
LLVM_ABI bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition Constants.cpp:74
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
bool isLittleEndian() const
Layout endianness...
Definition DataLayout.h:215
unsigned getAddressSizeInBits(unsigned AS) const
The size in bits of an address in for the given AS.
Definition DataLayout.h:511
LLVM_ABI const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
LLVM_ABI unsigned getIndexTypeSizeInBits(Type *Ty) const
The size in bits of the index used in GEP calculation for this type.
LLVM_ABI unsigned getPointerTypeSizeInBits(Type *) const
The pointer representation size in bits for this type.
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
Definition DataLayout.h:784
ArrayRef< CondBrInst * > conditionsFor(const Value *V) const
Access the list of branches which affect this value.
DomTreeNodeBase * getIDom() const
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:159
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
This instruction extracts a struct member or array element value from an aggregate value.
ArrayRef< unsigned > getIndices() const
unsigned getNumIndices() const
static LLVM_ABI Type * getIndexedType(Type *Agg, ArrayRef< unsigned > Idxs)
Returns the type of the element that would be extracted with an extractvalue instruction with the spe...
This instruction compares its operands according to the predicate given to the constructor.
Utility class for floating point operations which can have information about relaxed accuracy require...
Definition Operator.h:200
Convenience struct for specifying and reasoning about fast-math flags.
Definition FMF.h:23
bool noSignedZeros() const
Definition FMF.h:70
bool noInfs() const
Definition FMF.h:69
void setNoSignedZeros(bool B=true)
Definition FMF.h:87
void setNoNaNs(bool B=true)
Definition FMF.h:81
bool noNaNs() const
Definition FMF.h:68
const BasicBlock & getEntryBlock() const
Definition Function.h:809
bool hasNoSync() const
Determine if the call can synchroize with other threads.
Definition Function.h:645
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
PointerType * getType() const
Global values are always pointers.
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this global belongs to.
Definition Globals.cpp:141
Type * getValueType() const
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
This instruction compares its operands according to the predicate given to the constructor.
CmpPredicate getSwappedCmpPredicate() const
CmpPredicate getInverseCmpPredicate() const
Predicate getFlippedSignednessPredicate() const
For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->EQ.
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
static LLVM_ABI std::optional< bool > isImpliedByMatchingCmp(CmpPredicate Pred1, CmpPredicate Pred2)
Determine if Pred1 implies Pred2 is true, false, or if nothing can be inferred about the implication,...
bool isRelational() const
Return true if the predicate is relational (not EQ or NE).
Predicate getUnsignedPredicate() const
For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
This instruction inserts a struct field of array element value into an aggregate value.
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
LLVM_ABI bool hasNoNaNs() const LLVM_READONLY
Determine whether the no-NaNs flag is set.
LLVM_ABI bool hasNoUnsignedWrap() const LLVM_READONLY
Determine whether the no unsigned wrap flag is set.
LLVM_ABI bool hasNoSignedWrap() const LLVM_READONLY
Determine whether the no signed wrap flag is set.
bool isBinaryOp() const
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI bool isExact() const LLVM_READONLY
Determine whether the exact flag is set.
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
LLVM_ABI bool comesBefore(const Instruction *Other) const
Given an instruction Other in the same basic block as this instruction, return true if this instructi...
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
bool isUnaryOp() const
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
A wrapper class for inspecting calls to intrinsic functions.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
An instruction for reading from memory.
Value * getPointerOperand()
Align getAlign() const
Return the alignment of the access that is being performed.
bool isLoopHeader(const BlockT *BB) const
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
Metadata node.
Definition Metadata.h:1080
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1444
This is a utility class that provides an abstraction for the common functionality between Instruction...
Definition Operator.h:33
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
Definition Operator.h:43
Utility class for integer operators which may exhibit overflow - Add, Sub, Mul, and Shl.
Definition Operator.h:78
iterator_range< const_block_iterator > blocks() const
Value * getIncomingValueForBlock(const BasicBlock *BB) const
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A udiv, sdiv, lshr, or ashr instruction, which can be marked as "exact", indicating that no bits are ...
Definition Operator.h:154
bool isExact() const
Test whether this division is known to be exact, with zero remainder.
Definition Operator.h:173
This class represents the LLVM 'select' instruction.
const Value * getFalseValue() const
const Value * getCondition() const
const Value * getTrueValue() const
This instruction constructs a fixed permutation of two input vectors.
VectorType * getType() const
Overload to return most specific vector type.
static LLVM_ABI void getShuffleMask(const Constant *Mask, SmallVectorImpl< int > &Result)
Convert the input shuffle mask operand to a vector of integers.
size_type size() const
Definition SmallPtrSet.h:99
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void reserve(size_type N)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:591
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition DataLayout.h:736
TypeSize getElementOffset(unsigned Idx) const
Definition DataLayout.h:767
Class to represent struct types.
unsigned getNumElements() const
Random access to the elements.
Type * getElementType(unsigned N) const
Provides information about what library functions are available for the current target.
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM_ABI unsigned getIntegerBitWidth() const
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:290
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition Type.h:263
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:284
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
Definition Type.h:155
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
LLVM_ABI uint64_t getArrayNumElements() const
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:311
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:370
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:201
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition Type.h:328
bool isHalfTy() const
Return true if this is 'half', a 16-bit IEEE fp type.
Definition Type.h:144
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:236
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
Definition Type.h:158
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
Definition Type.h:287
bool isIntOrPtrTy() const
Return true if this is an integer type or a pointer type.
Definition Type.h:272
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:257
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition Type.cpp:317
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
Definition Type.h:227
LLVM_ABI const fltSemantics & getFltSemantics() const
Definition Type.cpp:110
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM_ABI unsigned getOperandNo() const
Return the operand # of this use in its User.
Definition Use.cpp:35
User * getUser() const
Returns the User that contains this Use.
Definition Use.h:61
op_range operands()
Definition User.h:267
Value * getOperand(unsigned i) const
Definition User.h:207
unsigned getNumOperands() const
Definition User.h:229
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
const Value * stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, APInt &Offset) const
This is a wrapper around stripAndAccumulateConstantOffsets with the in-bounds requirement set to fals...
Definition Value.h:737
iterator_range< user_iterator > users()
Definition Value.h:426
LLVM_ABI const Value * stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, bool AllowInvariantGroup=false, function_ref< bool(Value &Value, APInt &Offset)> ExternalAnalysis=nullptr, bool LookThroughIntToPtr=false) const
Accumulate the constant offset this value has compared to a base pointer.
const KnownBits & getKnownBits(const SimplifyQuery &Q) const
Definition WithCache.h:59
PointerType getValue() const
Definition WithCache.h:57
Represents an op.with.overflow intrinsic.
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:168
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:165
An efficient, type-erasing, non-owning reference to a callable.
TypeSize getSequentialElementStride(const DataLayout &DL) const
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:123
A range adaptor for a pair of iterators.
CallInst * Call
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
#define UINT64_MAX
Definition DataTypes.h:77
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI APInt ScaleBitMask(const APInt &A, unsigned NewBitWidth, bool MatchAllBits=false)
Splat/Merge neighboring bits to widen/narrow the bitmask represented by.
Definition APInt.cpp:3049
const APInt & umax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be unsigned.
Definition APInt.h:2281
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
SpecificConstantMatch m_ZeroInt()
Convenience matchers for specific integer values.
BinaryOp_match< SpecificConstantMatch, SrcTy, TargetOpcode::G_SUB > m_Neg(const SrcTy &&Src)
Matches a register negated by a G_SUB.
BinaryOp_match< SrcTy, SpecificConstantMatch, TargetOpcode::G_XOR, true > m_Not(const SrcTy &&Src)
Matches a register not-ed by a G_XOR.
OneUse_match< SubPat > m_OneUse(const SubPat &SP)
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
cst_pred_ty< is_lowbit_mask > m_LowBitMask()
Match an integer or vector with only the low bit(s) set.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
PtrToIntSameSize_match< OpTy > m_PtrToIntSameSize(const DataLayout &DL, const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, FCmpInst > m_FCmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
cst_pred_ty< is_sign_mask > m_SignMask()
Match an integer or vector with only the sign bit(s) set.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWAdd(const LHS &L, const RHS &R)
auto m_PtrToIntOrAddr(const OpTy &Op)
Matches PtrToInt or PtrToAddr.
BinaryOp_match< LHS, RHS, Instruction::FSub > m_FSub(const LHS &L, const RHS &R)
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
BinaryOp_match< LHS, RHS, Instruction::URem > m_URem(const LHS &L, const RHS &R)
auto m_LogicalOp()
Matches either L && R or L || R where L and R are arbitrary values.
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
ap_match< APInt > m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
BinaryOp_match< LHS, RHS, Instruction::And, true > m_c_And(const LHS &L, const RHS &R)
Matches an And with LHS and RHS in either order.
cst_pred_ty< is_power2_or_zero > m_Power2OrZero()
Match an integer or vector of 0 or power-of-2 values.
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWSub(const LHS &L, const RHS &R)
bool match(Val *V, const Pattern &P)
BinOpPred_match< LHS, RHS, is_idiv_op > m_IDiv(const LHS &L, const RHS &R)
Matches integer division operations.
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
ap_match< APFloat > m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
CmpClass_match< LHS, RHS, ICmpInst, true > m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
auto match_fn(const Pattern &P)
A match functor that can be used as a UnaryPredicate in functional algorithms like all_of.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true > m_c_NUWAdd(const LHS &L, const RHS &R)
cst_pred_ty< is_nonnegative > m_NonNegative()
Match an integer or vector of non-negative values.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
IntrinsicID_match m_Intrinsic()
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
IntrinsicID_match m_VScale()
Matches a call to llvm.vscale().
match_combine_or< MaxMin_match< FCmpInst, LHS, RHS, ofmin_pred_ty >, MaxMin_match< FCmpInst, LHS, RHS, ufmin_pred_ty > > m_OrdOrUnordFMin(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point minimum function.
ExtractValue_match< Ind, Val_t > m_ExtractValue(const Val_t &V)
Match a single index ExtractValue instruction.
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > m_SMin(const LHS &L, const RHS &R)
bind_ty< WithOverflowInst > m_WithOverflowInst(WithOverflowInst *&I)
Match a with overflow intrinsic, capturing it if we match.
BinaryOp_match< LHS, RHS, Instruction::Xor, true > m_c_Xor(const LHS &L, const RHS &R)
Matches an Xor with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty, true > m_c_SMin(const LHS &L, const RHS &R)
Matches an SMin with LHS and RHS in either order.
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty, true > m_c_UMax(const LHS &L, const RHS &R)
Matches a UMax with LHS and RHS in either order.
SpecificCmpClass_match< LHS, RHS, ICmpInst > m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
BinaryOp_match< LHS, RHS, Instruction::UDiv > m_UDiv(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty > m_UMax(const LHS &L, const RHS &R)
brc_match< Cond_t, bind_ty< BasicBlock >, bind_ty< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
match_immconstant_ty m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
NoWrapTrunc_match< OpTy, TruncInst::NoUnsignedWrap > m_NUWTrunc(const OpTy &Op)
Matches trunc nuw.
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty, true > m_c_UMin(const LHS &L, const RHS &R)
Matches a UMin with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Add, true > m_c_Add(const LHS &L, const RHS &R)
Matches a Add with LHS and RHS in either order.
match_combine_or< BinaryOp_match< LHS, RHS, Instruction::Add >, DisjointOr_match< LHS, RHS > > m_AddLike(const LHS &L, const RHS &R)
Match either "add" or "or disjoint".
match_combine_or< MaxMin_match< FCmpInst, LHS, RHS, ofmax_pred_ty >, MaxMin_match< FCmpInst, LHS, RHS, ufmax_pred_ty > > m_OrdOrUnordFMax(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point maximum function.
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty, true > m_c_SMax(const LHS &L, const RHS &R)
Matches an SMax with LHS and RHS in either order.
CastOperator_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
match_combine_or< match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty, true >, MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty, true > >, match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty, true >, MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty, true > > > m_c_MaxOrMin(const LHS &L, const RHS &R)
cstfp_pred_ty< custom_checkfn< APFloat > > m_CheckedFp(function_ref< bool(const APFloat &)> CheckFn)
Match a float or vector where CheckFn(ele) for each element is true.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWSub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty > m_SMax(const LHS &L, const RHS &R)
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap >, DisjointOr_match< LHS, RHS > > m_NSWAddLike(const LHS &L, const RHS &R)
Match either "add nsw" or "or disjoint".
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap > m_NSWAdd(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, SExtInst > > m_ZExtOrSExt(const OpTy &Op)
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match 'fneg X' as 'fsub -0.0, X'.
BinOpPred_match< LHS, RHS, is_shift_op > m_Shift(const LHS &L, const RHS &R)
Matches shift operations.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
BinOpPred_match< LHS, RHS, is_irem_op > m_IRem(const LHS &L, const RHS &R)
Matches integer remainder operations.
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
class_match< BasicBlock > m_BasicBlock()
Match an arbitrary basic block value and ignore it.
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
cst_pred_ty< is_nonpositive > m_NonPositive()
Match an integer or vector of non-positive values.
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap >, DisjointOr_match< LHS, RHS > > m_NUWAddLike(const LHS &L, const RHS &R)
Match either "add nuw" or "or disjoint".
ElementWiseBitCast_match< OpTy > m_ElementWiseBitCast(const OpTy &Op)
m_Intrinsic_Ty< Opnd0 >::Ty m_FAbs(const Opnd0 &Op0)
BinaryOp_match< LHS, RHS, Instruction::Mul, true > m_c_Mul(const LHS &L, const RHS &R)
Matches a Mul with LHS and RHS in either order.
CastOperator_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > m_UMin(const LHS &L, const RHS &R)
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
static unsigned decodeVSEW(unsigned VSEW)
LLVM_ABI unsigned getSEWLMULRatio(unsigned SEW, VLMUL VLMul)
static constexpr unsigned RVVBitsPerBlock
initializer< Ty > init(const Ty &Val)
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:668
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI bool haveNoCommonBitsSet(const WithCache< const Value * > &LHSCache, const WithCache< const Value * > &RHSCache, const SimplifyQuery &SQ)
Return true if LHS and RHS have no common bits set.
LLVM_ABI bool mustExecuteUBIfPoisonOnPathTo(Instruction *Root, Instruction *OnPathTo, DominatorTree *DT)
Return true if undefined behavior would provable be executed on the path to OnPathTo if Root produced...
LLVM_ABI Intrinsic::ID getInverseMinMaxIntrinsic(Intrinsic::ID MinMaxID)
LLVM_ABI bool willNotFreeBetween(const Instruction *Assume, const Instruction *CtxI)
Returns true, if no instruction between Assume and CtxI may free memory and the function is marked as...
@ Offset
Definition DWP.cpp:532
@ Length
Definition DWP.cpp:532
@ NeverOverflows
Never overflows.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
LLVM_ABI KnownFPClass computeKnownFPClass(const Value *V, const APInt &DemandedElts, FPClassTest InterestedClasses, const SimplifyQuery &SQ, unsigned Depth=0)
Determine which floating-point classes are valid for V, and return them in KnownFPClass bit sets.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1739
MaybeAlign getAlign(const CallInst &I, unsigned Index)
LLVM_ABI bool isValidAssumeForContext(const Instruction *I, const Instruction *CxtI, const DominatorTree *DT=nullptr, bool AllowEphemerals=false)
Return true if it is valid to use the assumptions provided by an assume intrinsic,...
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1669
LLVM_ABI bool canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
LLVM_ABI bool mustTriggerUB(const Instruction *I, const SmallPtrSetImpl< const Value * > &KnownPoison)
Return true if the given instruction must trigger undefined behavior when I is executed with any oper...
LLVM_ABI bool isKnownNeverInfinity(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point scalar value is not an infinity or if the floating-point vector val...
LLVM_ABI void computeKnownBitsFromContext(const Value *V, KnownBits &Known, const SimplifyQuery &Q, unsigned Depth=0)
Merge bits known from context-dependent facts into Known.
LLVM_ABI bool isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI)
LLVM_ABI bool isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS, bool &TrueIfSigned)
Given an exploded icmp instruction, return true if the comparison only checks the sign bit.
LLVM_ABI const Value * getArgumentAliasingToReturnedPointer(const CallBase *Call, bool MustPreserveNullness)
This function returns call pointer argument that is considered the same by aliasing rules.
LLVM_ABI bool isAssumeLikeIntrinsic(const Instruction *I)
Return true if it is an intrinsic that cannot be speculated but also cannot trap.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2554
LLVM_ABI AllocaInst * findAllocaForValue(Value *V, bool OffsetZero=false)
Returns unique alloca where the value comes from, or nullptr.
LLVM_ABI APInt getMinMaxLimit(SelectPatternFlavor SPF, unsigned BitWidth)
Return the minimum or maximum constant value for the specified integer min/max flavor and type.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI bool isOnlyUsedInZeroComparison(const Instruction *CxtI)
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
LLVM_ABI bool getConstantStringInfo(const Value *V, StringRef &Str, bool TrimAtNul=true)
This function computes the length of a null-terminated C string pointed to by V.
LLVM_ABI bool isDereferenceableAndAlignedPointer(const Value *V, Type *Ty, Align Alignment, const DataLayout &DL, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Returns true if V is always a dereferenceable pointer with alignment greater or equal than requested.
Definition Loads.cpp:229
LLVM_ABI bool onlyUsedByLifetimeMarkersOrDroppableInsts(const Value *V)
Return true if the only users of this pointer are lifetime markers or droppable instructions.
LLVM_ABI Constant * ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset)
LLVM_ABI Value * stripNullTest(Value *V)
Returns the inner value X if the expression has the form f(X) where f(X) == 0 if and only if X == 0,...
LLVM_ABI bool getUnderlyingObjectsForCodeGen(const Value *V, SmallVectorImpl< Value * > &Objects)
This is a wrapper around getUnderlyingObjects and adds support for basic ptrtoint+arithmetic+inttoptr...
LLVM_ABI std::pair< Intrinsic::ID, bool > canConvertToMinOrMaxIntrinsic(ArrayRef< Value * > VL)
Check if the values in VL are select instructions that can be converted to a min or max (vector) intr...
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ABI bool getConstantDataArrayInfo(const Value *V, ConstantDataArraySlice &Slice, unsigned ElementSize, uint64_t Offset=0)
Returns true if the value V is a pointer into a ConstantDataArray.
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition bit.h:323
LLVM_ABI bool isGuaranteedToExecuteForEveryIteration(const Instruction *I, const Loop *L)
Return true if this function can prove that the instruction I is executed for every iteration of the ...
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2208
LLVM_ABI bool mustSuppressSpeculation(const LoadInst &LI)
Return true if speculation of the given load must be suppressed to avoid ordering or interfering with...
Definition Loads.cpp:431
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition MathExtras.h:284
gep_type_iterator gep_type_end(const User *GEP)
int ilogb(const APFloat &Arg)
Returns the exponent of the internal representation of the APFloat.
Definition APFloat.h:1601
LLVM_ABI bool isSafeToSpeculativelyExecute(const Instruction *I, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true, bool IgnoreUBImplyingAttrs=true)
Return true if the instruction does not have any effects besides calculating the result and does not ...
LLVM_ABI Value * getSplatValue(const Value *V)
Get splat value if the input is a splat vector or return nullptr.
LLVM_ABI CmpInst::Predicate getMinMaxPred(SelectPatternFlavor SPF, bool Ordered=false)
Return the canonical comparison predicate for the specified minimum/maximum flavor.
bool isa_and_nonnull(const Y &Val)
Definition Casting.h:676
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:337
LLVM_ABI bool canIgnoreSignBitOfZero(const Use &U)
Return true if the sign bit of the FP value can be ignored by the user when the value is zero.
LLVM_ABI bool isGuaranteedNotToBeUndef(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be undef, but may be poison.
LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
std::tuple< Value *, FPClassTest, FPClassTest > fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS, FPClassTest RHSClass, bool LookThroughSrc=true)
LLVM_ABI ConstantRange computeConstantRange(const Value *V, bool ForSigned, bool UseInstrInfo=true, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Determine the possible constant range of an integer or vector of integer value.
const Value * getPointerOperand(const Value *V)
A helper function that returns the pointer operand of a load, store or GEP instruction.
LLVM_ABI bool MaskedValueIsZero(const Value *V, const APInt &Mask, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if 'V & Mask' is known to be zero.
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:202
LLVM_ABI bool isOverflowIntrinsicNoWrap(const WithOverflowInst *WO, const DominatorTree &DT)
Returns true if the arithmetic part of the WO 's result is used only along the paths control dependen...
LLVM_ABI RetainedKnowledge getKnowledgeFromBundle(AssumeInst &Assume, const CallBase::BundleOpInfo &BOI)
This extracts the Knowledge from an element of an operand bundle.
LLVM_ABI bool matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO, Value *&Start, Value *&Step)
Attempt to match a simple first order recurrence cycle of the form: iv = phi Ty [Start,...
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1746
LLVM_ABI OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ, bool IsNSW=false)
LLVM_ABI bool getShuffleDemandedElts(int SrcWidth, ArrayRef< int > Mask, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS, bool AllowUndefElts=false)
Transform a shuffle mask's output demanded element mask into demanded element masks for the 2 operand...
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:331
bool isGuard(const User *U)
Returns true iff U has semantics of a guard expressed in a form of call of llvm.experimental....
LLVM_ABI SelectPatternFlavor getInverseMinMaxFlavor(SelectPatternFlavor SPF)
Return the inverse minimum/maximum flavor of the specified flavor.
constexpr unsigned MaxAnalysisRecursionDepth
LLVM_ABI void adjustKnownBitsForSelectArm(KnownBits &Known, Value *Cond, Value *Arm, bool Invert, const SimplifyQuery &Q, unsigned Depth=0)
Adjust Known for the given select Arm to include information from the select Cond.
LLVM_ABI bool isKnownNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be negative (i.e.
LLVM_ABI OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
SelectPatternFlavor
Specific patterns of select instructions we can match.
@ SPF_ABS
Floating point maxnum.
@ SPF_NABS
Absolute value.
@ SPF_FMAXNUM
Floating point minnum.
@ SPF_UMIN
Signed minimum.
@ SPF_UMAX
Signed maximum.
@ SPF_SMAX
Unsigned minimum.
@ SPF_UNKNOWN
@ SPF_FMINNUM
Unsigned maximum.
LLVM_ABI bool isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(const CallBase *Call, bool MustPreserveNullness)
{launder,strip}.invariant.group returns pointer that aliases its argument, and it only captures point...
LLVM_ABI bool impliesPoison(const Value *ValAssumedPoison, const Value *V)
Return true if V is poison given that ValAssumedPoison is already poison.
LLVM_ABI void getHorizDemandedEltsForFirstOperand(unsigned VectorBitWidth, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS)
Compute the demanded elements mask of horizontal binary operations.
LLVM_ABI SelectPatternResult getSelectPattern(CmpInst::Predicate Pred, SelectPatternNaNBehavior NaNBehavior=SPNB_NA, bool Ordered=false)
Determine the pattern for predicate X Pred Y ? X : Y.
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI bool programUndefinedIfPoison(const Instruction *Inst)
LLVM_ABI SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
LLVM_ABI bool matchSimpleBinaryIntrinsicRecurrence(const IntrinsicInst *I, PHINode *&P, Value *&Init, Value *&OtherOp)
Attempt to match a simple value-accumulating recurrence of the form: llvm.intrinsic....
LLVM_ABI bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
LLVM_ABI bool cannotBeNegativeZero(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if we can prove that the specified FP value is never equal to -0.0.
LLVM_ABI bool programUndefinedIfUndefOrPoison(const Instruction *Inst)
Return true if this function can prove that if Inst is executed and yields a poison value or undef bi...
LLVM_ABI void adjustKnownFPClassForSelectArm(KnownFPClass &Known, Value *Cond, Value *Arm, bool Invert, const SimplifyQuery &Q, unsigned Depth=0)
Adjust Known for the given select Arm to include information from the select Cond.
generic_gep_type_iterator<> gep_type_iterator
LLVM_ABI bool collectPossibleValues(const Value *V, SmallPtrSetImpl< const Constant * > &Constants, unsigned MaxCount, bool AllowUndefOrPoison=true)
Enumerates all possible immediate values of V and inserts them into the set Constants.
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
LLVM_ABI uint64_t GetStringLength(const Value *V, unsigned CharSize=8)
If we can compute the length of the string pointed to by the specified pointer, return 'len+1'.
LLVM_ABI OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
LLVM_ABI ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
LLVM_ABI Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
LLVM_ABI bool canCreateUndefOrPoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
canCreateUndefOrPoison returns true if Op can create undef or poison from non-undef & non-poison oper...
LLVM_ABI bool matchSimpleTernaryIntrinsicRecurrence(const IntrinsicInst *I, PHINode *&P, Value *&Init, Value *&OtherOp0, Value *&OtherOp1)
Attempt to match a simple value-accumulating recurrence of the form: llvm.intrinsic....
LLVM_ABI EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
LLVM_ABI bool isKnownInversion(const Value *X, const Value *Y)
Return true iff:
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI bool intrinsicPropagatesPoison(Intrinsic::ID IID)
Return whether this intrinsic propagates poison for all operands.
LLVM_ABI bool isNotCrossLaneOperation(const Instruction *I)
Return true if the instruction doesn't potentially cross vector lanes.
LLVM_ABI bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth=0)
Return true if the given value is known to be non-zero when defined.
constexpr int PoisonMaskElem
LLVM_ABI RetainedKnowledge getKnowledgeValidInContext(const Value *V, ArrayRef< Attribute::AttrKind > AttrKinds, AssumptionCache &AC, const Instruction *CtxI, const DominatorTree *DT=nullptr)
Return a valid Knowledge associated to the Value V if its Attribute kind is in AttrKinds and the know...
LLVM_ABI bool isSafeToSpeculativelyExecuteWithOpcode(unsigned Opcode, const Instruction *Inst, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true, bool IgnoreUBImplyingAttrs=true)
This returns the same result as isSafeToSpeculativelyExecute if Opcode is the actual opcode of Inst.
LLVM_ABI bool onlyUsedByLifetimeMarkers(const Value *V)
Return true if the only users of this pointer are lifetime markers.
LLVM_ABI Intrinsic::ID getIntrinsicForCallSite(const CallBase &CB, const TargetLibraryInfo *TLI)
Map a call instruction to an intrinsic ID.
@ Other
Any other memory.
Definition ModRef.h:68
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
LLVM_ABI const Value * getUnderlyingObjectAggressive(const Value *V)
Like getUnderlyingObject(), but will try harder to find a single underlying object.
LLVM_ABI Intrinsic::ID getMinMaxIntrinsic(SelectPatternFlavor SPF)
Convert given SPF to equivalent min/max intrinsic.
LLVM_ABI SelectPatternResult matchDecomposedSelectPattern(CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, FastMathFlags FMF=FastMathFlags(), Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Determine the pattern that a select with the given compare as its predicate and given values as its t...
LLVM_ABI OverflowResult computeOverflowForSignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
LLVM_ABI bool propagatesPoison(const Use &PoisonOp)
Return true if PoisonOp's user yields poison or raises UB if its operand PoisonOp is poison.
@ Add
Sum of integers.
LLVM_ABI ConstantRange computeConstantRangeIncludingKnownBits(const WithCache< const Value * > &V, bool ForSigned, const SimplifyQuery &SQ)
Combine constant ranges from computeConstantRange() and computeKnownBits().
SelectPatternNaNBehavior
Behavior when a floating point min/max is given one NaN and one non-NaN as input.
@ SPNB_RETURNS_NAN
NaN behavior not applicable.
@ SPNB_RETURNS_OTHER
Given one NaN input, returns the NaN.
@ SPNB_RETURNS_ANY
Given one NaN input, returns the non-NaN.
LLVM_ABI bool isKnownNonEqual(const Value *V1, const Value *V2, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the given values are known to be non-equal when defined.
DWARFExpression::Operation Op
LLVM_ABI bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Return the number of times the sign bit of the register is replicated into the other bits.
constexpr unsigned BitWidth
LLVM_ABI KnownBits analyzeKnownBitsFromAndXorOr(const Operator *I, const KnownBits &KnownLHS, const KnownBits &KnownRHS, const SimplifyQuery &SQ, unsigned Depth=0)
Using KnownBits LHS/RHS produce the known bits for logic op (and/xor/or).
LLVM_ABI OverflowResult computeOverflowForUnsignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
LLVM_ABI bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I)
Return true if this function can prove that the instruction I will always transfer execution to one o...
LLVM_ABI bool isKnownNeverInfOrNaN(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point value can never contain a NaN or infinity.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI bool isKnownNeverNaN(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point scalar value is not a NaN or if the floating-point vector value has...
gep_type_iterator gep_type_begin(const User *GEP)
LLVM_ABI Value * isBytewiseValue(Value *V, const DataLayout &DL)
If the specified value can be set by repeating the same byte in memory, return the i8 value that it i...
LLVM_ABI std::optional< std::pair< CmpPredicate, Constant * > > getFlippedStrictnessPredicateAndConstant(CmpPredicate Pred, Constant *C)
Convert an integer comparison with a constant RHS into an equivalent form with the strictness flipped...
LLVM_ABI unsigned ComputeMaxSignificantBits(const Value *Op, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Get the upper bound on bit size for this Value Op as a signed integer.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1947
LLVM_ABI bool isKnownIntegral(const Value *V, const SimplifyQuery &SQ, FastMathFlags FMF)
Return true if the floating-point value V is known to be an integer value.
LLVM_ABI OverflowResult computeOverflowForUnsignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:197
LLVM_ABI bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Return true if the given value is known to have exactly one bit set when defined.
LLVM_ABI std::optional< bool > isImpliedByDomCondition(const Value *Cond, const Instruction *ContextI, const DataLayout &DL)
Return the boolean condition value in the context of the given instruction if it is known based on do...
LLVM_ABI bool isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be poison, but may be undef.
LLVM_ABI void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, KnownBits &Known)
Compute known bits from the range metadata.
LLVM_ABI Value * FindInsertedValue(Value *V, ArrayRef< unsigned > idx_range, std::optional< BasicBlock::iterator > InsertBefore=std::nullopt)
Given an aggregate and an sequence of indices, see if the scalar value indexed is already around as a...
LLVM_ABI bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW=false, bool AllowPoison=true)
Return true if the two given values are negation.
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
LLVM_ABI bool isKnownPositive(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be positive (i.e.
LLVM_ABI Constant * ConstantFoldIntegerCast(Constant *C, Type *DestTy, bool IsSigned, const DataLayout &DL)
Constant fold a zext, sext or trunc, depending on IsSigned and whether the DestTy is wider or narrowe...
LLVM_ABI bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the give value is known to be non-negative.
LLVM_ABI bool cannotBeOrderedLessThanZero(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if we can prove that the specified FP value is either NaN or never less than -0....
LLVM_ABI void getUnderlyingObjects(const Value *V, SmallVectorImpl< const Value * > &Objects, const LoopInfo *LI=nullptr, unsigned MaxLookup=MaxLookupSearchDepth)
This method is similar to getUnderlyingObject except that it can look through phi and select instruct...
LLVM_ABI bool mayHaveNonDefUseDependency(const Instruction &I)
Returns true if the result or effects of the given instructions I depend values not reachable through...
LLVM_ABI bool isTriviallyVectorizable(Intrinsic::ID ID)
Identify if the intrinsic is trivially vectorizable.
LLVM_ABI bool isIdentifiedObject(const Value *V)
Return true if this pointer refers to a distinct and identifiable object.
LLVM_ABI std::optional< bool > isImpliedCondition(const Value *LHS, const Value *RHS, const DataLayout &DL, bool LHSIsTrue=true, unsigned Depth=0)
Return true if RHS is known to be implied true by LHS.
LLVM_ABI std::optional< bool > computeKnownFPSignBit(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return false if we can prove that the specified FP value's sign bit is 0.
LLVM_ABI bool canIgnoreSignBitOfNaN(const Use &U)
Return true if the sign bit of the FP value can be ignored by the user when the value is NaN.
LLVM_ABI void findValuesAffectedByCondition(Value *Cond, bool IsAssume, function_ref< void(Value *)> InsertAffected)
Call InsertAffected on all Values whose known bits / value may be affected by the condition Cond.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:872
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
SmallPtrSet< Value *, 4 > AffectedValues
Represents offset+length into a ConstantDataArray.
const ConstantDataArray * Array
ConstantDataArray pointer.
Represent subnormal handling kind for floating point instruction inputs and outputs.
static constexpr DenormalMode getDynamic()
InstrInfoQuery provides an interface to query additional information for instructions like metadata o...
bool isExact(const BinaryOperator *Op) const
MDNode * getMetadata(const Instruction *I, unsigned KindID) const
bool hasNoSignedZeros(const InstT *Op) const
bool hasNoSignedWrap(const InstT *Op) const
bool hasNoUnsignedWrap(const InstT *Op) const
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition KnownBits.h:317
static LLVM_ABI KnownBits sadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)
KnownBits anyextOrTrunc(unsigned BitWidth) const
Return known bits for an "any" extension or truncation of the value we're tracking.
Definition KnownBits.h:192
static LLVM_ABI KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition KnownBits.h:271
static LLVM_ABI KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition KnownBits.h:108
bool isZero() const
Returns true if value is all zero.
Definition KnownBits.h:80
LLVM_ABI KnownBits blsi() const
Compute known bits for X & -X, which has only the lowest bit set of X set.
void makeNonNegative()
Make this value non-negative.
Definition KnownBits.h:127
static LLVM_ABI KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
unsigned countMinLeadingOnes() const
Returns the minimum number of leading one bits.
Definition KnownBits.h:267
LLVM_ABI KnownBits reduceAdd(unsigned NumElts) const
Compute known bits for horizontal add for a vector with NumElts elements, where each element has the ...
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition KnownBits.h:258
static LLVM_ABI KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
static LLVM_ABI KnownBits ssub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.ssub.sat(LHS, RHS)
static LLVM_ABI KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
bool isUnknown() const
Returns true if we don't know any bits.
Definition KnownBits.h:66
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition KnownBits.h:290
LLVM_ABI KnownBits blsmsk() const
Compute known bits for X ^ (X - 1), which has all bits up to and including the lowest set bit of X se...
void makeNegative()
Make this value negative.
Definition KnownBits.h:122
void setAllConflict()
Make all bits known to be both zero and one.
Definition KnownBits.h:99
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition KnownBits.h:167
KnownBits byteSwap() const
Definition KnownBits.h:547
bool hasConflict() const
Returns true if there is conflicting information.
Definition KnownBits.h:51
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition KnownBits.h:305
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition KnownBits.h:86
KnownBits reverseBits() const
Definition KnownBits.h:551
unsigned getBitWidth() const
Get the bit width of this value.
Definition KnownBits.h:44
static LLVM_ABI KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition KnownBits.h:178
bool isConstant() const
Returns true if we know the value of all bits.
Definition KnownBits.h:54
void resetAll()
Resets the known state of all bits.
Definition KnownBits.h:74
static KnownBits add(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false, bool SelfAdd=false)
Compute knownbits resulting from addition of LHS and RHS.
Definition KnownBits.h:363
KnownBits unionWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for either this or RHS or both.
Definition KnownBits.h:337
static LLVM_ABI KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition KnownBits.h:111
bool isEven() const
Return if the value is known even (the low bit is 0).
Definition KnownBits.h:164
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition KnownBits.h:241
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition KnownBits.h:327
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition KnownBits.h:186
unsigned countMinTrailingOnes() const
Returns the minimum number of trailing one bits.
Definition KnownBits.h:261
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition KnownBits.h:202
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition KnownBits.h:264
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:148
static LLVM_ABI KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
static LLVM_ABI KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
static LLVM_ABI KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
static LLVM_ABI KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
APInt getMinValue() const
Return the minimal unsigned value possible given these KnownBits.
Definition KnownBits.h:132
static LLVM_ABI KnownBits computeForAddSub(bool Add, bool NSW, bool NUW, const KnownBits &LHS, const KnownBits &RHS)
Compute known bits resulting from adding LHS and RHS.
Definition KnownBits.cpp:61
static LLVM_ABI KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition KnownBits.h:342
bool isNegative() const
Returns true if this value is known to be negative.
Definition KnownBits.h:105
static KnownBits sub(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from subtraction of LHS and RHS.
Definition KnownBits.h:378
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition KnownBits.h:296
void setAllOnes()
Make all bits known to be one and discard any previous information.
Definition KnownBits.h:92
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition KnownBits.h:235
static LLVM_ABI KnownBits uadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.uadd.sat(LHS, RHS)
static LLVM_ABI KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
KnownBits anyext(unsigned BitWidth) const
Return known bits for an "any" extension of the value we're tracking, where we don't know anything ab...
Definition KnownBits.h:173
static LLVM_ABI KnownBits clmul(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for clmul(LHS, RHS).
LLVM_ABI KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
static LLVM_ABI std::optional< bool > sgt(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SGT result.
static LLVM_ABI std::optional< bool > uge(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_UGE result.
static LLVM_ABI KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
static LLVM_ABI KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
KnownBits sextOrTrunc(unsigned BitWidth) const
Return known bits for a sign extension or truncation of the value we're tracking.
Definition KnownBits.h:212
bool isKnownNeverInfOrNaN() const
Return true if it's known this can never be an infinity or nan.
FPClassTest KnownFPClasses
Floating-point classes the value could be one of.
bool isKnownNeverInfinity() const
Return true if it's known this can never be an infinity.
bool cannotBeOrderedGreaterThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never greater tha...
static LLVM_ABI KnownFPClass sin(const KnownFPClass &Src)
Report known values for sin.
static LLVM_ABI KnownFPClass fdiv_self(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fdiv x, x.
static constexpr FPClassTest OrderedGreaterThanZeroMask
static constexpr FPClassTest OrderedLessThanZeroMask
void knownNot(FPClassTest RuleOut)
static LLVM_ABI KnownFPClass fmul(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fmul.
static LLVM_ABI KnownFPClass fadd_self(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fadd x, x.
void copysign(const KnownFPClass &Sign)
static KnownFPClass square(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
static LLVM_ABI KnownFPClass fsub(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fsub.
KnownFPClass unionWith(const KnownFPClass &RHS) const
static LLVM_ABI KnownFPClass canonicalize(const KnownFPClass &Src, DenormalMode DenormMode=DenormalMode::getDynamic())
Apply the canonicalize intrinsic to this value.
LLVM_ABI bool isKnownNeverLogicalZero(DenormalMode Mode) const
Return true if it's known this can never be interpreted as a zero.
static LLVM_ABI KnownFPClass log(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for log/log2/log10.
static LLVM_ABI KnownFPClass fdiv(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fdiv.
static LLVM_ABI KnownFPClass roundToIntegral(const KnownFPClass &Src, bool IsTrunc, bool IsMultiUnitFPType)
Propagate known class for rounding intrinsics (trunc, floor, ceil, rint, nearbyint,...
static LLVM_ABI KnownFPClass cos(const KnownFPClass &Src)
Report known values for cos.
static LLVM_ABI KnownFPClass ldexp(const KnownFPClass &Src, const KnownBits &N, const fltSemantics &Flt, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for ldexp.
static LLVM_ABI KnownFPClass minMaxLike(const KnownFPClass &LHS, const KnownFPClass &RHS, MinMaxKind Kind, DenormalMode DenormMode=DenormalMode::getDynamic())
bool isUnknown() const
KnownFPClass intersectWith(const KnownFPClass &RHS) const
static LLVM_ABI KnownFPClass exp(const KnownFPClass &Src)
Report known values for exp, exp2 and exp10.
static LLVM_ABI KnownFPClass frexp_mant(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for mantissa component of frexp.
std::optional< bool > SignBit
std::nullopt if the sign bit is unknown, true if the sign bit is definitely set or false if the sign ...
bool isKnownNeverNaN() const
Return true if it's known this can never be a nan.
bool isKnownNever(FPClassTest Mask) const
Return true if it's known this can never be one of the mask entries.
static LLVM_ABI KnownFPClass fpext(const KnownFPClass &KnownSrc, const fltSemantics &DstTy, const fltSemantics &SrcTy)
Propagate known class for fpext.
bool isKnownNeverNegZero() const
Return true if it's known this can never be a negative zero.
static LLVM_ABI KnownFPClass fma(const KnownFPClass &LHS, const KnownFPClass &RHS, const KnownFPClass &Addend, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fma.
void propagateNaN(const KnownFPClass &Src, bool PreserveSign=false)
static LLVM_ABI KnownFPClass fptrunc(const KnownFPClass &KnownSrc)
Propagate known class for fptrunc.
bool cannotBeOrderedLessThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never less than -...
void signBitMustBeOne()
Assume the sign bit is one.
void signBitMustBeZero()
Assume the sign bit is zero.
static LLVM_ABI KnownFPClass sqrt(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for sqrt.
LLVM_ABI bool isKnownNeverLogicalPosZero(DenormalMode Mode) const
Return true if it's known this can never be interpreted as a positive zero.
bool isKnownNeverPosInfinity() const
Return true if it's known this can never be +infinity.
static LLVM_ABI KnownFPClass fadd(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fadd.
LLVM_ABI bool isKnownNeverLogicalNegZero(DenormalMode Mode) const
Return true if it's known this can never be interpreted as a negative zero.
static LLVM_ABI KnownFPClass bitcast(const fltSemantics &FltSemantics, const KnownBits &Bits)
Report known values for a bitcast into a float with provided semantics.
static LLVM_ABI KnownFPClass fma_square(const KnownFPClass &Squared, const KnownFPClass &Addend, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fma squared, squared, addend.
static LLVM_ABI KnownFPClass frem_self(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for frem.
static LLVM_ABI KnownFPClass powi(const KnownFPClass &Src, const KnownBits &N)
Propagate known class for powi.
Represent one information held inside an operand bundle of an llvm.assume.
SelectPatternFlavor Flavor
static bool isMinOrMax(SelectPatternFlavor SPF)
When implementing this min/max pattern as fcmp; select, does the fcmp have to be ordered?
const DataLayout & DL
SimplifyQuery getWithoutCondContext() const
const Instruction * CxtI
const DominatorTree * DT
SimplifyQuery getWithInstruction(const Instruction *I) const
AssumptionCache * AC
const DomConditionCache * DC
const InstrInfoQuery IIQ
const CondContext * CC